PR 10066
[official-gcc.git] / gcc / c-decl.c
bloba4b435e82917c103ea1a9fdeaabd8dc6410c0156
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 "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "timevar.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "cgraph.h"
53 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
54 enum decl_context
55 { NORMAL, /* Ordinary declaration */
56 FUNCDEF, /* Function definition */
57 PARM, /* Declaration of parm before function body */
58 FIELD, /* Declaration inside struct or union */
59 BITFIELD, /* Likewise but with specified width */
60 TYPENAME}; /* Typename (inside cast or sizeof) */
63 /* Nonzero if we have seen an invalid cross reference
64 to a struct, union, or enum, but not yet printed the message. */
66 tree pending_invalid_xref;
67 /* File and line to appear in the eventual error message. */
68 const char *pending_invalid_xref_file;
69 int pending_invalid_xref_line;
71 /* While defining an enum type, this is 1 plus the last enumerator
72 constant value. Note that will do not have to save this or `enum_overflow'
73 around nested function definition since such a definition could only
74 occur in an enum value expression and we don't use these variables in
75 that case. */
77 static tree enum_next_value;
79 /* Nonzero means that there was overflow computing enum_next_value. */
81 static int enum_overflow;
83 /* Parsing a function declarator leaves a list of parameter names
84 or a chain or parameter decls here. */
86 static tree last_function_parms;
88 /* Parsing a function declarator leaves here a chain of structure
89 and enum types declared in the parmlist. */
91 static tree last_function_parm_tags;
93 /* After parsing the declarator that starts a function definition,
94 `start_function' puts here the list of parameter names or chain of decls.
95 `store_parm_decls' finds it here. */
97 static tree current_function_parms;
99 /* Similar, for last_function_parm_tags. */
100 static tree current_function_parm_tags;
102 /* Similar, for the file and line that the prototype came from if this is
103 an old-style definition. */
104 static const char *current_function_prototype_file;
105 static int current_function_prototype_line;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* The current scope statement stack. */
113 static GTY(()) tree c_scope_stmt_stack;
115 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
116 that have names. Here so we can clear out their names' definitions
117 at the end of the function. */
119 static GTY(()) tree named_labels;
121 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
123 static GTY(()) tree shadowed_labels;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement that specifies a return value is seen. */
128 int current_function_returns_value;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement with no argument is seen. */
133 int current_function_returns_null;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a call to a noreturn function is seen. */
138 int current_function_returns_abnormally;
140 /* Set to nonzero by `grokdeclarator' for a function
141 whose return type is defaulted, if warnings for this are desired. */
143 static int warn_about_return_type;
145 /* Nonzero when starting a function declared `extern inline'. */
147 static int current_extern_inline;
149 /* For each binding contour we allocate a binding_level structure
150 * which records the names defined in that contour.
151 * Contours include:
152 * 0) the global one
153 * 1) one for each function definition,
154 * where internal declarations of the parameters appear.
155 * 2) one for each compound statement,
156 * to record its declarations.
158 * The current meaning of a name can be found by searching the levels from
159 * the current one out to the global one.
162 /* Note that the information in the `names' component of the global contour
163 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
165 struct binding_level GTY(())
167 /* A chain of _DECL nodes for all variables, constants, functions,
168 and typedef types. These are in the reverse of the order supplied.
170 tree names;
172 /* A list of structure, union and enum definitions,
173 * for looking up tag names.
174 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
175 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
176 * or ENUMERAL_TYPE node.
178 tree tags;
180 /* For each level, a list of shadowed outer-level local definitions
181 to be restored when this level is popped.
182 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
183 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
184 tree shadowed;
186 /* For each level (except not the global one),
187 a chain of BLOCK nodes for all the levels
188 that were entered and exited one level down. */
189 tree blocks;
191 /* The BLOCK node for this level, if one has been preallocated.
192 If 0, the BLOCK is allocated (if needed) when the level is popped. */
193 tree this_block;
195 /* The binding level which this one is contained in (inherits from). */
196 struct binding_level *level_chain;
198 /* Nonzero for the level that holds the parameters of a function. */
199 char parm_flag;
201 /* Nonzero if this level "doesn't exist" for tags. */
202 char tag_transparent;
204 /* Nonzero if sublevels of this level "don't exist" for tags.
205 This is set in the parm level of a function definition
206 while reading the function body, so that the outermost block
207 of the function body will be tag-transparent. */
208 char subblocks_tag_transparent;
210 /* Nonzero means make a BLOCK for this level regardless of all else. */
211 char keep;
213 /* Nonzero means make a BLOCK if this level has any subblocks. */
214 char keep_if_subblocks;
216 /* List of decls in `names' that have incomplete structure or
217 union types. */
218 tree incomplete_list;
220 /* A list of decls giving the (reversed) specified order of parms,
221 not including any forward-decls in the parmlist.
222 This is so we can put the parms in proper order for assign_parms. */
223 tree parm_order;
226 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
228 /* The binding level currently in effect. */
230 static GTY(()) struct binding_level *current_binding_level;
232 /* A chain of binding_level structures awaiting reuse. */
234 static GTY((deletable (""))) struct binding_level *free_binding_level;
236 /* The outermost binding level, for names of file scope.
237 This is created when the compiler is started and exists
238 through the entire run. */
240 static GTY(()) struct binding_level *global_binding_level;
242 /* Binding level structures are initialized by copying this one. */
244 static struct binding_level clear_binding_level
245 = {NULL, NULL, NULL, NULL, NULL, NULL_BINDING_LEVEL, 0, 0, 0, 0, 0, NULL,
246 NULL};
248 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
250 static int keep_next_level_flag;
252 /* Nonzero means make a BLOCK for the next level pushed
253 if it has subblocks. */
255 static int keep_next_if_subblocks;
257 /* The chain of outer levels of label scopes.
258 This uses the same data structure used for binding levels,
259 but it works differently: each link in the chain records
260 saved values of named_labels and shadowed_labels for
261 a label binding level outside the current one. */
263 static GTY(()) struct binding_level *label_level_chain;
265 /* Functions called automatically at the beginning and end of execution. */
267 tree static_ctors, static_dtors;
269 /* Forward declarations. */
271 static struct binding_level *make_binding_level PARAMS ((void));
272 static struct binding_level *get_function_binding_level PARAMS ((void));
273 static void pop_binding_level PARAMS ((struct binding_level **));
274 static void clear_limbo_values PARAMS ((tree));
275 static int duplicate_decls PARAMS ((tree, tree, int));
276 static int redeclaration_error_message PARAMS ((tree, tree));
277 static void storedecls PARAMS ((tree));
278 static void storetags PARAMS ((tree));
279 static tree lookup_tag PARAMS ((enum tree_code, tree,
280 struct binding_level *, int));
281 static tree lookup_tag_reverse PARAMS ((tree));
282 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
283 int));
284 static tree grokparms PARAMS ((tree, int));
285 static void layout_array_type PARAMS ((tree));
286 static tree c_make_fname_decl PARAMS ((tree, int));
287 static void c_expand_body_1 PARAMS ((tree, int));
288 static void warn_if_shadowing PARAMS ((tree, tree));
289 static bool flexible_array_type_p PARAMS ((tree));
291 /* States indicating how grokdeclarator() should handle declspecs marked
292 with __attribute__((deprecated)). An object declared as
293 __attribute__((deprecated)) suppresses warnings of uses of other
294 deprecated items. */
296 enum deprecated_states {
297 DEPRECATED_NORMAL,
298 DEPRECATED_SUPPRESS
301 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
303 void
304 c_print_identifier (file, node, indent)
305 FILE *file;
306 tree node;
307 int indent;
309 print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
310 print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
311 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
312 print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
313 print_node (file, "limbo value", IDENTIFIER_LIMBO_VALUE (node), indent + 4);
314 if (C_IS_RESERVED_WORD (node))
316 tree rid = ridpointers[C_RID_CODE (node)];
317 indent_to (file, indent + 4);
318 fprintf (file, "rid ");
319 fprintf (file, HOST_PTR_PRINTF, (void *)rid);
320 fprintf (file, " \"%s\"", IDENTIFIER_POINTER (rid));
324 /* Hook called at end of compilation to assume 1 elt
325 for a top-level tentative array defn that wasn't complete before. */
327 void
328 c_finish_incomplete_decl (decl)
329 tree decl;
331 if (TREE_CODE (decl) == VAR_DECL)
333 tree type = TREE_TYPE (decl);
334 if (type != error_mark_node
335 && TREE_CODE (type) == ARRAY_TYPE
336 && ! DECL_EXTERNAL (decl)
337 && TYPE_DOMAIN (type) == 0)
339 warning_with_decl (decl, "array `%s' assumed to have one element");
341 complete_array_type (type, NULL_TREE, 1);
343 layout_decl (decl, 0);
348 /* Reuse or create a struct for this binding level. */
350 static struct binding_level *
351 make_binding_level ()
353 if (free_binding_level)
355 struct binding_level *result = free_binding_level;
356 free_binding_level = result->level_chain;
357 return result;
359 else
360 return (struct binding_level *) ggc_alloc (sizeof (struct binding_level));
363 /* Return the outermost binding level for the current function. */
364 static struct binding_level *
365 get_function_binding_level ()
367 struct binding_level *b = current_binding_level;
369 while (b->level_chain->parm_flag == 0)
370 b = b->level_chain;
371 return b;
374 /* Remove a binding level from a list and add it to the level chain. */
376 static void
377 pop_binding_level (lp)
378 struct binding_level **lp;
380 struct binding_level *l = *lp;
381 *lp = l->level_chain;
383 memset (l, 0, sizeof (struct binding_level));
384 l->level_chain = free_binding_level;
385 free_binding_level = l;
388 /* Nonzero if we are currently in the global binding level. */
391 global_bindings_p ()
393 return current_binding_level == global_binding_level;
396 void
397 keep_next_level ()
399 keep_next_level_flag = 1;
402 /* Nonzero if the current level needs to have a BLOCK made. */
405 kept_level_p ()
407 return ((current_binding_level->keep_if_subblocks
408 && current_binding_level->blocks != 0)
409 || current_binding_level->keep
410 || current_binding_level->names != 0
411 || (current_binding_level->tags != 0
412 && !current_binding_level->tag_transparent));
415 /* Identify this binding level as a level of parameters.
416 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
417 But it turns out there is no way to pass the right value for
418 DEFINITION_FLAG, so we ignore it. */
420 void
421 declare_parm_level (definition_flag)
422 int definition_flag ATTRIBUTE_UNUSED;
424 current_binding_level->parm_flag = 1;
427 /* Nonzero if currently making parm declarations. */
430 in_parm_level_p ()
432 return current_binding_level->parm_flag;
435 /* Enter a new binding level.
436 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
437 not for that of tags. */
439 void
440 pushlevel (tag_transparent)
441 int tag_transparent;
443 struct binding_level *newlevel = NULL_BINDING_LEVEL;
445 /* If this is the top level of a function,
446 just make sure that NAMED_LABELS is 0. */
448 if (current_binding_level == global_binding_level)
450 named_labels = 0;
453 newlevel = make_binding_level ();
455 /* Add this level to the front of the chain (stack) of levels that
456 are active. */
458 *newlevel = clear_binding_level;
459 newlevel->tag_transparent
460 = (tag_transparent
461 || (current_binding_level
462 ? current_binding_level->subblocks_tag_transparent
463 : 0));
464 newlevel->level_chain = current_binding_level;
465 current_binding_level = newlevel;
466 newlevel->keep = keep_next_level_flag;
467 keep_next_level_flag = 0;
468 newlevel->keep_if_subblocks = keep_next_if_subblocks;
469 keep_next_if_subblocks = 0;
472 /* Clear the limbo values of all identifiers defined in BLOCK or a subblock. */
474 static void
475 clear_limbo_values (block)
476 tree block;
478 tree tem;
480 for (tem = BLOCK_VARS (block); tem; tem = TREE_CHAIN (tem))
481 if (DECL_NAME (tem) != 0)
482 IDENTIFIER_LIMBO_VALUE (DECL_NAME (tem)) = 0;
484 for (tem = BLOCK_SUBBLOCKS (block); tem; tem = TREE_CHAIN (tem))
485 clear_limbo_values (tem);
488 /* Exit a binding level.
489 Pop the level off, and restore the state of the identifier-decl mappings
490 that were in effect when this level was entered.
492 If KEEP is nonzero, this level had explicit declarations, so
493 and create a "block" (a BLOCK node) for the level
494 to record its declarations and subblocks for symbol table output.
496 If FUNCTIONBODY is nonzero, this level is the body of a function,
497 so create a block as if KEEP were set and also clear out all
498 label names.
500 If REVERSE is nonzero, reverse the order of decls before putting
501 them into the BLOCK. */
503 tree
504 poplevel (keep, reverse, functionbody)
505 int keep;
506 int reverse;
507 int functionbody;
509 tree link;
510 /* The chain of decls was accumulated in reverse order.
511 Put it into forward order, just for cleanliness. */
512 tree decls;
513 tree tags = current_binding_level->tags;
514 tree subblocks = current_binding_level->blocks;
515 tree block = 0;
516 tree decl;
517 int block_previously_created;
519 keep |= current_binding_level->keep;
521 /* This warning is turned off because it causes warnings for
522 declarations like `extern struct foo *x'. */
523 #if 0
524 /* Warn about incomplete structure types in this level. */
525 for (link = tags; link; link = TREE_CHAIN (link))
526 if (!COMPLETE_TYPE_P (TREE_VALUE (link)))
528 tree type = TREE_VALUE (link);
529 tree type_name = TYPE_NAME (type);
530 char *id = IDENTIFIER_POINTER (TREE_CODE (type_name) == IDENTIFIER_NODE
531 ? type_name
532 : DECL_NAME (type_name));
533 switch (TREE_CODE (type))
535 case RECORD_TYPE:
536 error ("`struct %s' incomplete in scope ending here", id);
537 break;
538 case UNION_TYPE:
539 error ("`union %s' incomplete in scope ending here", id);
540 break;
541 case ENUMERAL_TYPE:
542 error ("`enum %s' incomplete in scope ending here", id);
543 break;
546 #endif /* 0 */
548 /* Get the decls in the order they were written.
549 Usually current_binding_level->names is in reverse order.
550 But parameter decls were previously put in forward order. */
552 if (reverse)
553 current_binding_level->names
554 = decls = nreverse (current_binding_level->names);
555 else
556 decls = current_binding_level->names;
558 /* Output any nested inline functions within this block
559 if they weren't already output. */
561 for (decl = decls; decl; decl = TREE_CHAIN (decl))
562 if (TREE_CODE (decl) == FUNCTION_DECL
563 && ! TREE_ASM_WRITTEN (decl)
564 && DECL_INITIAL (decl) != 0
565 && TREE_ADDRESSABLE (decl))
567 /* If this decl was copied from a file-scope decl
568 on account of a block-scope extern decl,
569 propagate TREE_ADDRESSABLE to the file-scope decl.
571 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
572 true, since then the decl goes through save_for_inline_copying. */
573 if (DECL_ABSTRACT_ORIGIN (decl) != 0
574 && DECL_ABSTRACT_ORIGIN (decl) != decl)
575 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
578 /* We used to warn about unused variables in expand_end_bindings,
579 i.e. while generating RTL. But in function-at-a-time mode we may
580 choose to never expand a function at all (e.g. auto inlining), so
581 we do this explicitly now. */
582 warn_about_unused_variables (getdecls ());
584 /* If there were any declarations or structure tags in that level,
585 or if this level is a function body,
586 create a BLOCK to record them for the life of this function. */
588 block = 0;
589 block_previously_created = (current_binding_level->this_block != 0);
590 if (block_previously_created)
591 block = current_binding_level->this_block;
592 else if (keep || functionbody
593 || (current_binding_level->keep_if_subblocks && subblocks != 0))
594 block = make_node (BLOCK);
595 if (block != 0)
597 BLOCK_VARS (block) = decls;
598 BLOCK_SUBBLOCKS (block) = subblocks;
601 /* In each subblock, record that this is its superior. */
603 for (link = subblocks; link; link = TREE_CHAIN (link))
604 BLOCK_SUPERCONTEXT (link) = block;
606 /* Clear out the meanings of the local variables of this level. */
608 for (link = decls; link; link = TREE_CHAIN (link))
610 if (DECL_NAME (link) != 0)
612 /* If the ident. was used or addressed via a local extern decl,
613 don't forget that fact. */
614 if (DECL_EXTERNAL (link))
616 if (TREE_USED (link))
617 TREE_USED (DECL_NAME (link)) = 1;
618 if (TREE_ADDRESSABLE (link))
619 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
621 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
625 /* Restore all name-meanings of the outer levels
626 that were shadowed by this level. */
628 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
629 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
631 /* If the level being exited is the top level of a function,
632 check over all the labels, and clear out the current
633 (function local) meanings of their names. */
635 if (functionbody)
637 clear_limbo_values (block);
639 /* If this is the top level block of a function,
640 the vars are the function's parameters.
641 Don't leave them in the BLOCK because they are
642 found in the FUNCTION_DECL instead. */
644 BLOCK_VARS (block) = 0;
646 /* Clear out the definitions of all label names,
647 since their scopes end here,
648 and add them to BLOCK_VARS. */
650 for (link = named_labels; link; link = TREE_CHAIN (link))
652 tree label = TREE_VALUE (link);
654 if (DECL_INITIAL (label) == 0)
656 error_with_decl (label, "label `%s' used but not defined");
657 /* Avoid crashing later. */
658 define_label (input_filename, lineno,
659 DECL_NAME (label));
661 else if (warn_unused_label && !TREE_USED (label))
662 warning_with_decl (label, "label `%s' defined but not used");
663 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
665 /* Put the labels into the "variables" of the
666 top-level block, so debugger can see them. */
667 TREE_CHAIN (label) = BLOCK_VARS (block);
668 BLOCK_VARS (block) = label;
672 /* Pop the current level, and free the structure for reuse. */
674 pop_binding_level (&current_binding_level);
676 /* Dispose of the block that we just made inside some higher level. */
677 if (functionbody)
678 DECL_INITIAL (current_function_decl) = block;
679 else if (block)
681 if (!block_previously_created)
682 current_binding_level->blocks
683 = chainon (current_binding_level->blocks, block);
685 /* If we did not make a block for the level just exited,
686 any blocks made for inner levels
687 (since they cannot be recorded as subblocks in that level)
688 must be carried forward so they will later become subblocks
689 of something else. */
690 else if (subblocks)
691 current_binding_level->blocks
692 = chainon (current_binding_level->blocks, subblocks);
694 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
695 binding contour so that they point to the appropriate construct, i.e.
696 either to the current FUNCTION_DECL node, or else to the BLOCK node
697 we just constructed.
699 Note that for tagged types whose scope is just the formal parameter
700 list for some function type specification, we can't properly set
701 their TYPE_CONTEXTs here, because we don't have a pointer to the
702 appropriate FUNCTION_TYPE node readily available to us. For those
703 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
704 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
705 node which will represent the "scope" for these "parameter list local"
706 tagged types. */
708 if (functionbody)
709 for (link = tags; link; link = TREE_CHAIN (link))
710 TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
711 else if (block)
712 for (link = tags; link; link = TREE_CHAIN (link))
713 TYPE_CONTEXT (TREE_VALUE (link)) = block;
715 if (block)
716 TREE_USED (block) = 1;
718 return block;
721 /* Insert BLOCK at the end of the list of subblocks of the
722 current binding level. This is used when a BIND_EXPR is expanded,
723 to handle the BLOCK node inside the BIND_EXPR. */
725 void
726 insert_block (block)
727 tree block;
729 TREE_USED (block) = 1;
730 current_binding_level->blocks
731 = chainon (current_binding_level->blocks, block);
734 /* Set the BLOCK node for the innermost scope
735 (the one we are currently in). */
737 void
738 set_block (block)
739 tree block;
741 current_binding_level->this_block = block;
742 current_binding_level->names = chainon (current_binding_level->names,
743 BLOCK_VARS (block));
744 current_binding_level->blocks = chainon (current_binding_level->blocks,
745 BLOCK_SUBBLOCKS (block));
748 void
749 push_label_level ()
751 struct binding_level *newlevel;
753 newlevel = make_binding_level ();
755 /* Add this level to the front of the chain (stack) of label levels. */
757 newlevel->level_chain = label_level_chain;
758 label_level_chain = newlevel;
760 newlevel->names = named_labels;
761 newlevel->shadowed = shadowed_labels;
762 named_labels = 0;
763 shadowed_labels = 0;
766 void
767 pop_label_level ()
769 struct binding_level *level = label_level_chain;
770 tree link, prev;
772 /* Clear out the definitions of the declared labels in this level.
773 Leave in the list any ordinary, non-declared labels. */
774 for (link = named_labels, prev = 0; link;)
776 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
778 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
780 error_with_decl (TREE_VALUE (link),
781 "label `%s' used but not defined");
782 /* Avoid crashing later. */
783 define_label (input_filename, lineno,
784 DECL_NAME (TREE_VALUE (link)));
786 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
787 warning_with_decl (TREE_VALUE (link),
788 "label `%s' defined but not used");
789 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
791 /* Delete this element from the list. */
792 link = TREE_CHAIN (link);
793 if (prev)
794 TREE_CHAIN (prev) = link;
795 else
796 named_labels = link;
798 else
800 prev = link;
801 link = TREE_CHAIN (link);
805 /* Bring back all the labels that were shadowed. */
806 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
807 if (DECL_NAME (TREE_VALUE (link)) != 0)
808 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
809 = TREE_VALUE (link);
811 named_labels = chainon (named_labels, level->names);
812 shadowed_labels = level->shadowed;
814 /* Pop the current level, and free the structure for reuse. */
815 pop_binding_level (&label_level_chain);
818 /* Push a definition or a declaration of struct, union or enum tag "name".
819 "type" should be the type node.
820 We assume that the tag "name" is not already defined.
822 Note that the definition may really be just a forward reference.
823 In that case, the TYPE_SIZE will be zero. */
825 void
826 pushtag (name, type)
827 tree name, type;
829 struct binding_level *b;
831 /* Find the proper binding level for this type tag. */
833 for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
834 continue;
836 if (name)
838 /* Record the identifier as the type's name if it has none. */
840 if (TYPE_NAME (type) == 0)
841 TYPE_NAME (type) = name;
844 b->tags = tree_cons (name, type, b->tags);
846 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
847 tagged type we just added to the current binding level. This fake
848 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
849 to output a representation of a tagged type, and it also gives
850 us a convenient place to record the "scope start" address for the
851 tagged type. */
853 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
855 /* An approximation for now, so we can tell this is a function-scope tag.
856 This will be updated in poplevel. */
857 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
860 /* Handle when a new declaration NEWDECL
861 has the same name as an old one OLDDECL
862 in the same binding contour.
863 Prints an error message if appropriate.
865 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
866 Otherwise, return 0.
868 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
869 and OLDDECL is in an outer binding level and should thus not be changed. */
871 static int
872 duplicate_decls (newdecl, olddecl, different_binding_level)
873 tree newdecl, olddecl;
874 int different_binding_level;
876 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
877 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
878 && DECL_INITIAL (newdecl) != 0);
879 tree oldtype = TREE_TYPE (olddecl);
880 tree newtype = TREE_TYPE (newdecl);
881 int errmsg = 0;
883 if (DECL_P (olddecl))
885 if (TREE_CODE (newdecl) == FUNCTION_DECL
886 && TREE_CODE (olddecl) == FUNCTION_DECL
887 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
889 if (DECL_DECLARED_INLINE_P (newdecl)
890 && DECL_UNINLINABLE (newdecl)
891 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
892 /* Already warned elsewhere. */;
893 else if (DECL_DECLARED_INLINE_P (olddecl)
894 && DECL_UNINLINABLE (olddecl)
895 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
896 /* Already warned. */;
897 else if (DECL_DECLARED_INLINE_P (newdecl)
898 && ! DECL_DECLARED_INLINE_P (olddecl)
899 && DECL_UNINLINABLE (olddecl)
900 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
902 warning_with_decl (newdecl,
903 "function `%s' redeclared as inline");
904 warning_with_decl (olddecl,
905 "previous declaration of function `%s' with attribute noinline");
907 else if (DECL_DECLARED_INLINE_P (olddecl)
908 && DECL_UNINLINABLE (newdecl)
909 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
911 warning_with_decl (newdecl,
912 "function `%s' redeclared with attribute noinline");
913 warning_with_decl (olddecl,
914 "previous declaration of function `%s' was inline");
918 DECL_ATTRIBUTES (newdecl)
919 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
922 if (TREE_CODE (newtype) == ERROR_MARK
923 || TREE_CODE (oldtype) == ERROR_MARK)
924 types_match = 0;
926 /* New decl is completely inconsistent with the old one =>
927 tell caller to replace the old one.
928 This is always an error except in the case of shadowing a builtin. */
929 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
931 if (TREE_CODE (olddecl) == FUNCTION_DECL
932 && (DECL_BUILT_IN (olddecl)
933 || DECL_BUILT_IN_NONANSI (olddecl)))
935 /* If you declare a built-in or predefined function name as static,
936 the old definition is overridden,
937 but optionally warn this was a bad choice of name. */
938 if (!TREE_PUBLIC (newdecl))
940 if (!warn_shadow)
942 else if (DECL_BUILT_IN (olddecl))
943 warning_with_decl (newdecl, "shadowing built-in function `%s'");
944 else
945 warning_with_decl (newdecl, "shadowing library function `%s'");
947 /* Likewise, if the built-in is not ansi, then programs can
948 override it even globally without an error. */
949 else if (! DECL_BUILT_IN (olddecl))
950 warning_with_decl (newdecl,
951 "library function `%s' declared as non-function");
953 else if (DECL_BUILT_IN_NONANSI (olddecl))
954 warning_with_decl (newdecl,
955 "built-in function `%s' declared as non-function");
956 else
957 warning_with_decl (newdecl,
958 "built-in function `%s' declared as non-function");
960 else
962 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
963 error_with_decl (olddecl, "previous declaration of `%s'");
966 return 0;
969 /* For real parm decl following a forward decl,
970 return 1 so old decl will be reused. */
971 if (types_match && TREE_CODE (newdecl) == PARM_DECL
972 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
973 return 1;
975 /* The new declaration is the same kind of object as the old one.
976 The declarations may partially match. Print warnings if they don't
977 match enough. Ultimately, copy most of the information from the new
978 decl to the old one, and keep using the old one. */
980 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
982 /* A function declaration for a built-in function. */
983 if (!TREE_PUBLIC (newdecl))
985 /* If you declare a built-in function name as static, the
986 built-in definition is overridden,
987 but optionally warn this was a bad choice of name. */
988 if (warn_shadow)
989 warning_with_decl (newdecl, "shadowing built-in function `%s'");
990 /* Discard the old built-in function. */
991 return 0;
993 else if (!types_match)
995 /* Accept the return type of the new declaration if same modes. */
996 tree oldreturntype = TREE_TYPE (oldtype);
997 tree newreturntype = TREE_TYPE (newtype);
999 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
1001 /* Function types may be shared, so we can't just modify
1002 the return type of olddecl's function type. */
1003 tree trytype
1004 = build_function_type (newreturntype,
1005 TYPE_ARG_TYPES (oldtype));
1006 trytype = build_type_attribute_variant (trytype,
1007 TYPE_ATTRIBUTES (oldtype));
1009 types_match = comptypes (newtype, trytype);
1010 if (types_match)
1011 oldtype = trytype;
1013 /* Accept harmless mismatch in first argument type also.
1014 This is for the ffs and fprintf builtins. */
1015 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
1016 && TYPE_ARG_TYPES (oldtype) != 0
1017 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
1018 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
1019 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
1020 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
1022 /* Function types may be shared, so we can't just modify
1023 the return type of olddecl's function type. */
1024 tree trytype
1025 = build_function_type (TREE_TYPE (oldtype),
1026 tree_cons (NULL_TREE,
1027 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
1028 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
1029 trytype = build_type_attribute_variant (trytype,
1030 TYPE_ATTRIBUTES (oldtype));
1032 types_match = comptypes (newtype, trytype);
1033 if (types_match)
1034 oldtype = trytype;
1036 if (! different_binding_level)
1037 TREE_TYPE (olddecl) = oldtype;
1039 else if (TYPE_ARG_TYPES (oldtype) == NULL
1040 && TYPE_ARG_TYPES (newtype) != NULL)
1042 /* For bcmp, bzero, fputs the builtin type has arguments not
1043 specified. Use the ones from the prototype so that type checking
1044 is done for them. */
1045 tree trytype
1046 = build_function_type (TREE_TYPE (oldtype),
1047 TYPE_ARG_TYPES (newtype));
1048 trytype = build_type_attribute_variant (trytype,
1049 TYPE_ATTRIBUTES (oldtype));
1051 oldtype = trytype;
1052 if (! different_binding_level)
1053 TREE_TYPE (olddecl) = oldtype;
1055 if (!types_match)
1057 /* If types don't match for a built-in, throw away the built-in. */
1058 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1059 return 0;
1062 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1063 && DECL_SOURCE_LINE (olddecl) == 0)
1065 /* A function declaration for a predeclared function
1066 that isn't actually built in. */
1067 if (!TREE_PUBLIC (newdecl))
1069 /* If you declare it as static, the
1070 default definition is overridden. */
1071 return 0;
1073 else if (!types_match)
1075 /* If the types don't match, preserve volatility indication.
1076 Later on, we will discard everything else about the
1077 default declaration. */
1078 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1081 /* Permit char *foo () to match void *foo (...) if not pedantic,
1082 if one of them came from a system header file. */
1083 else if (!types_match
1084 && TREE_CODE (olddecl) == FUNCTION_DECL
1085 && TREE_CODE (newdecl) == FUNCTION_DECL
1086 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1087 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1088 && (DECL_IN_SYSTEM_HEADER (olddecl)
1089 || DECL_IN_SYSTEM_HEADER (newdecl))
1090 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1091 && TYPE_ARG_TYPES (oldtype) == 0
1092 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1093 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1095 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1096 && TYPE_ARG_TYPES (newtype) == 0
1097 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1098 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1100 if (pedantic)
1101 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1102 /* Make sure we keep void * as ret type, not char *. */
1103 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1104 TREE_TYPE (newdecl) = newtype = oldtype;
1106 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1107 we will come back here again. */
1108 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1110 else if (!types_match
1111 /* Permit char *foo (int, ...); followed by char *foo ();
1112 if not pedantic. */
1113 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1114 && ! pedantic
1115 /* Return types must still match. */
1116 && comptypes (TREE_TYPE (oldtype),
1117 TREE_TYPE (newtype))
1118 && TYPE_ARG_TYPES (newtype) == 0))
1120 error_with_decl (newdecl, "conflicting types for `%s'");
1121 /* Check for function type mismatch
1122 involving an empty arglist vs a nonempty one. */
1123 if (TREE_CODE (olddecl) == FUNCTION_DECL
1124 && comptypes (TREE_TYPE (oldtype),
1125 TREE_TYPE (newtype))
1126 && ((TYPE_ARG_TYPES (oldtype) == 0
1127 && DECL_INITIAL (olddecl) == 0)
1129 (TYPE_ARG_TYPES (newtype) == 0
1130 && DECL_INITIAL (newdecl) == 0)))
1132 /* Classify the problem further. */
1133 tree t = TYPE_ARG_TYPES (oldtype);
1134 if (t == 0)
1135 t = TYPE_ARG_TYPES (newtype);
1136 for (; t; t = TREE_CHAIN (t))
1138 tree type = TREE_VALUE (t);
1140 if (TREE_CHAIN (t) == 0
1141 && TYPE_MAIN_VARIANT (type) != void_type_node)
1143 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1144 break;
1147 if (c_type_promotes_to (type) != type)
1149 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1150 break;
1154 error_with_decl (olddecl, "previous declaration of `%s'");
1156 /* This is safer because the initializer might contain references
1157 to variables that were declared between olddecl and newdecl. This
1158 will make the initializer invalid for olddecl in case it gets
1159 assigned to olddecl below. */
1160 if (TREE_CODE (newdecl) == VAR_DECL)
1161 DECL_INITIAL (newdecl) = 0;
1163 /* TLS cannot follow non-TLS declaration. */
1164 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1165 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1167 error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1168 error_with_decl (olddecl, "previous declaration of `%s'");
1170 /* non-TLS declaration cannot follow TLS declaration. */
1171 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1172 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1174 error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1175 error_with_decl (olddecl, "previous declaration of `%s'");
1177 else
1179 errmsg = redeclaration_error_message (newdecl, olddecl);
1180 if (errmsg)
1182 switch (errmsg)
1184 case 1:
1185 error_with_decl (newdecl, "redefinition of `%s'");
1186 break;
1187 case 2:
1188 error_with_decl (newdecl, "redeclaration of `%s'");
1189 break;
1190 case 3:
1191 error_with_decl (newdecl, "conflicting declarations of `%s'");
1192 break;
1193 default:
1194 abort ();
1197 error_with_decl (olddecl,
1198 ((DECL_INITIAL (olddecl)
1199 && current_binding_level == global_binding_level)
1200 ? "`%s' previously defined here"
1201 : "`%s' previously declared here"));
1202 return 0;
1204 else if (TREE_CODE (newdecl) == TYPE_DECL
1205 && (DECL_IN_SYSTEM_HEADER (olddecl)
1206 || DECL_IN_SYSTEM_HEADER (newdecl)))
1208 warning_with_decl (newdecl, "redefinition of `%s'");
1209 warning_with_decl
1210 (olddecl,
1211 ((DECL_INITIAL (olddecl)
1212 && current_binding_level == global_binding_level)
1213 ? "`%s' previously defined here"
1214 : "`%s' previously declared here"));
1216 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1217 && DECL_INITIAL (olddecl) != 0
1218 && TYPE_ARG_TYPES (oldtype) == 0
1219 && TYPE_ARG_TYPES (newtype) != 0
1220 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1222 tree type, parm;
1223 int nargs;
1224 /* Prototype decl follows defn w/o prototype. */
1226 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1227 type = TYPE_ARG_TYPES (newtype),
1228 nargs = 1;
1230 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1232 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1233 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1235 warning_with_decl (newdecl, "prototype for `%s' follows");
1236 warning_with_decl (olddecl, "non-prototype definition here");
1237 break;
1239 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1240 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1242 error_with_decl (newdecl,
1243 "prototype for `%s' follows and number of arguments doesn't match");
1244 error_with_decl (olddecl, "non-prototype definition here");
1245 errmsg = 1;
1246 break;
1248 /* Type for passing arg must be consistent
1249 with that declared for the arg. */
1250 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1252 error_with_decl (newdecl,
1253 "prototype for `%s' follows and argument %d doesn't match",
1254 nargs);
1255 error_with_decl (olddecl, "non-prototype definition here");
1256 errmsg = 1;
1257 break;
1261 /* Warn about mismatches in various flags. */
1262 else
1264 /* Warn if function is now inline
1265 but was previously declared not inline and has been called. */
1266 if (TREE_CODE (olddecl) == FUNCTION_DECL
1267 && ! DECL_DECLARED_INLINE_P (olddecl)
1268 && DECL_DECLARED_INLINE_P (newdecl)
1269 && TREE_USED (olddecl))
1270 warning_with_decl (newdecl,
1271 "`%s' declared inline after being called");
1272 if (TREE_CODE (olddecl) == FUNCTION_DECL
1273 && ! DECL_DECLARED_INLINE_P (olddecl)
1274 && DECL_DECLARED_INLINE_P (newdecl)
1275 && DECL_INITIAL (olddecl) != 0)
1276 warning_with_decl (newdecl,
1277 "`%s' declared inline after its definition");
1279 /* If pedantic, warn when static declaration follows a non-static
1280 declaration. Otherwise, do so only for functions. */
1281 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1282 && TREE_PUBLIC (olddecl)
1283 && !TREE_PUBLIC (newdecl))
1284 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1286 /* If warn_traditional, warn when a non-static function
1287 declaration follows a static one. */
1288 if (warn_traditional && !in_system_header
1289 && TREE_CODE (olddecl) == FUNCTION_DECL
1290 && !TREE_PUBLIC (olddecl)
1291 && TREE_PUBLIC (newdecl))
1292 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1294 /* Warn when const declaration follows a non-const
1295 declaration, but not for functions. */
1296 if (TREE_CODE (olddecl) != FUNCTION_DECL
1297 && !TREE_READONLY (olddecl)
1298 && TREE_READONLY (newdecl))
1299 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1300 /* These bits are logically part of the type, for variables.
1301 But not for functions
1302 (where qualifiers are not valid ANSI anyway). */
1303 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1304 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1305 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1306 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1310 /* Optionally warn about more than one declaration for the same name. */
1311 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1312 /* Don't warn about a function declaration
1313 followed by a definition. */
1314 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1315 && DECL_INITIAL (olddecl) == 0)
1316 /* Don't warn about extern decl followed by (tentative) definition. */
1317 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1319 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1320 warning_with_decl (olddecl, "previous declaration of `%s'");
1323 /* Copy all the DECL_... slots specified in the new decl
1324 except for any that we copy here from the old type.
1326 Past this point, we don't change OLDTYPE and NEWTYPE
1327 even if we change the types of NEWDECL and OLDDECL. */
1329 if (types_match)
1331 /* When copying info to olddecl, we store into write_olddecl
1332 instead. This allows us to avoid modifying olddecl when
1333 different_binding_level is true. */
1334 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1336 /* Merge the data types specified in the two decls. */
1337 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1339 if (different_binding_level)
1341 if (TYPE_ARG_TYPES (oldtype) != 0
1342 && TYPE_ARG_TYPES (newtype) == 0)
1343 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1344 else
1345 TREE_TYPE (newdecl)
1346 = build_type_attribute_variant
1347 (newtype,
1348 merge_attributes (TYPE_ATTRIBUTES (newtype),
1349 TYPE_ATTRIBUTES (oldtype)));
1351 else
1352 TREE_TYPE (newdecl)
1353 = TREE_TYPE (olddecl)
1354 = common_type (newtype, oldtype);
1357 /* Lay the type out, unless already done. */
1358 if (oldtype != TREE_TYPE (newdecl))
1360 if (TREE_TYPE (newdecl) != error_mark_node)
1361 layout_type (TREE_TYPE (newdecl));
1362 if (TREE_CODE (newdecl) != FUNCTION_DECL
1363 && TREE_CODE (newdecl) != TYPE_DECL
1364 && TREE_CODE (newdecl) != CONST_DECL)
1365 layout_decl (newdecl, 0);
1367 else
1369 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1370 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1371 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1372 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1373 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1374 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1376 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1377 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1381 /* Keep the old rtl since we can safely use it. */
1382 COPY_DECL_RTL (olddecl, newdecl);
1384 /* Merge the type qualifiers. */
1385 if (TREE_READONLY (newdecl))
1386 TREE_READONLY (write_olddecl) = 1;
1388 if (TREE_THIS_VOLATILE (newdecl))
1390 TREE_THIS_VOLATILE (write_olddecl) = 1;
1391 if (TREE_CODE (newdecl) == VAR_DECL
1392 /* If an automatic variable is re-declared in the same
1393 function scope, but the old declaration was not
1394 volatile, make_var_volatile() would crash because the
1395 variable would have been assigned to a pseudo, not a
1396 MEM. Since this duplicate declaration is invalid
1397 anyway, we just skip the call. */
1398 && errmsg == 0)
1399 make_var_volatile (newdecl);
1402 /* Keep source location of definition rather than declaration. */
1403 /* When called with different_binding_level set, keep the old
1404 information so that meaningful diagnostics can be given. */
1405 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1406 && ! different_binding_level)
1408 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1409 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1412 /* Merge the unused-warning information. */
1413 if (DECL_IN_SYSTEM_HEADER (olddecl))
1414 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1415 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1416 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1418 /* Merge the initialization information. */
1419 /* When called with different_binding_level set, don't copy over
1420 DECL_INITIAL, so that we don't accidentally change function
1421 declarations into function definitions. */
1422 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1423 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1425 /* Merge the section attribute.
1426 We want to issue an error if the sections conflict but that must be
1427 done later in decl_attributes since we are called before attributes
1428 are assigned. */
1429 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1430 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1432 /* Copy the assembler name.
1433 Currently, it can only be defined in the prototype. */
1434 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1436 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1438 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1439 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1440 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1441 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1442 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1445 /* If cannot merge, then use the new type and qualifiers,
1446 and don't preserve the old rtl. */
1447 else if (! different_binding_level)
1449 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1450 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1451 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1452 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1455 /* Merge the storage class information. */
1456 merge_weak (newdecl, olddecl);
1458 /* For functions, static overrides non-static. */
1459 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1461 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1462 /* This is since we don't automatically
1463 copy the attributes of NEWDECL into OLDDECL. */
1464 /* No need to worry about different_binding_level here because
1465 then TREE_PUBLIC (newdecl) was true. */
1466 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1467 /* If this clears `static', clear it in the identifier too. */
1468 if (! TREE_PUBLIC (olddecl))
1469 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1471 if (DECL_EXTERNAL (newdecl))
1473 if (! different_binding_level)
1475 /* Don't mess with these flags on local externs; they remain
1476 external even if there's a declaration at file scope which
1477 isn't. */
1478 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1479 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1481 /* An extern decl does not override previous storage class. */
1482 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1483 if (! DECL_EXTERNAL (newdecl))
1484 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1486 else
1488 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1489 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1492 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1494 /* If we're redefining a function previously defined as extern
1495 inline, make sure we emit debug info for the inline before we
1496 throw it away, in case it was inlined into a function that hasn't
1497 been written out yet. */
1498 if (new_is_definition && DECL_INITIAL (olddecl))
1500 if (TREE_USED (olddecl))
1501 (*debug_hooks->outlining_inline_function) (olddecl);
1503 /* The new defn must not be inline. */
1504 DECL_INLINE (newdecl) = 0;
1505 DECL_UNINLINABLE (newdecl) = 1;
1507 else
1509 /* If either decl says `inline', this fn is inline,
1510 unless its definition was passed already. */
1511 if (DECL_DECLARED_INLINE_P (newdecl)
1512 || DECL_DECLARED_INLINE_P (olddecl))
1513 DECL_DECLARED_INLINE_P (newdecl) = 1;
1515 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1516 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1519 if (DECL_BUILT_IN (olddecl))
1521 /* Get rid of any built-in function if new arg types don't match it
1522 or if we have a function definition. */
1523 if (! types_match || new_is_definition)
1525 if (! different_binding_level)
1527 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1528 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1531 else
1533 /* If redeclaring a builtin function, and not a definition,
1534 it stays built in. */
1535 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1536 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1540 /* Also preserve various other info from the definition. */
1541 if (! new_is_definition)
1543 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1544 /* When called with different_binding_level set, don't copy over
1545 DECL_INITIAL, so that we don't accidentally change function
1546 declarations into function definitions. */
1547 if (! different_binding_level)
1548 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1549 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1550 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1551 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1552 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1554 /* Set DECL_INLINE on the declaration if we've got a body
1555 from which to instantiate. */
1556 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1558 DECL_INLINE (newdecl) = 1;
1559 DECL_ABSTRACT_ORIGIN (newdecl)
1560 = (different_binding_level
1561 ? DECL_ORIGIN (olddecl)
1562 : DECL_ABSTRACT_ORIGIN (olddecl));
1565 else
1567 /* If a previous declaration said inline, mark the
1568 definition as inlinable. */
1569 if (DECL_DECLARED_INLINE_P (newdecl)
1570 && ! DECL_UNINLINABLE (newdecl))
1571 DECL_INLINE (newdecl) = 1;
1574 if (different_binding_level)
1575 return 0;
1577 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1578 But preserve OLDDECL's DECL_UID. */
1580 unsigned olddecl_uid = DECL_UID (olddecl);
1582 memcpy ((char *) olddecl + sizeof (struct tree_common),
1583 (char *) newdecl + sizeof (struct tree_common),
1584 sizeof (struct tree_decl) - sizeof (struct tree_common));
1585 DECL_UID (olddecl) = olddecl_uid;
1588 /* NEWDECL contains the merged attribute lists.
1589 Update OLDDECL to be the same. */
1590 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1592 return 1;
1595 /* Check whether decl-node X shadows an existing declaration.
1596 OLDLOCAL is the old IDENTIFIER_LOCAL_VALUE of the DECL_NAME of X,
1597 which might be a NULL_TREE. */
1598 static void
1599 warn_if_shadowing (x, oldlocal)
1600 tree x, oldlocal;
1602 tree name;
1604 if (DECL_EXTERNAL (x))
1605 return;
1607 name = DECL_NAME (x);
1609 /* Warn if shadowing an argument at the top level of the body. */
1610 if (oldlocal != 0
1611 /* This warning doesn't apply to the parms of a nested fcn. */
1612 && ! current_binding_level->parm_flag
1613 /* Check that this is one level down from the parms. */
1614 && current_binding_level->level_chain->parm_flag
1615 /* Check that the decl being shadowed
1616 comes from the parm level, one level up. */
1617 && chain_member (oldlocal, current_binding_level->level_chain->names))
1619 if (TREE_CODE (oldlocal) == PARM_DECL)
1620 pedwarn ("declaration of `%s' shadows a parameter",
1621 IDENTIFIER_POINTER (name));
1622 else
1623 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1624 IDENTIFIER_POINTER (name));
1626 /* Maybe warn if shadowing something else. */
1627 else if (warn_shadow
1628 /* No shadow warnings for internally generated vars. */
1629 && DECL_SOURCE_LINE (x) != 0
1630 /* No shadow warnings for vars made for inlining. */
1631 && ! DECL_FROM_INLINE (x))
1633 if (TREE_CODE (x) == PARM_DECL
1634 && current_binding_level->level_chain->parm_flag)
1635 /* Don't warn about the parm names in function declarator
1636 within a function declarator.
1637 It would be nice to avoid warning in any function
1638 declarator in a declaration, as opposed to a definition,
1639 but there is no way to tell it's not a definition. */
1641 else if (oldlocal)
1643 if (TREE_CODE (oldlocal) == PARM_DECL)
1644 shadow_warning ("a parameter", name, oldlocal);
1645 else
1646 shadow_warning ("a previous local", name, oldlocal);
1648 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1649 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1650 shadow_warning ("a global declaration", name,
1651 IDENTIFIER_GLOBAL_VALUE (name));
1655 /* Record a decl-node X as belonging to the current lexical scope.
1656 Check for errors (such as an incompatible declaration for the same
1657 name already seen in the same scope).
1659 Returns either X or an old decl for the same name.
1660 If an old decl is returned, it may have been smashed
1661 to agree with what X says. */
1663 tree
1664 pushdecl (x)
1665 tree x;
1667 tree t;
1668 tree name = DECL_NAME (x);
1669 struct binding_level *b = current_binding_level;
1671 /* Functions need the lang_decl data. */
1672 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1673 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1674 ggc_alloc_cleared (sizeof (struct lang_decl));
1676 DECL_CONTEXT (x) = current_function_decl;
1677 /* A local extern declaration for a function doesn't constitute nesting.
1678 A local auto declaration does, since it's a forward decl
1679 for a nested function coming later. */
1680 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1681 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
1682 DECL_CONTEXT (x) = 0;
1684 if (name)
1686 int different_binding_level = 0;
1688 if (warn_nested_externs
1689 && DECL_EXTERNAL (x)
1690 && b != global_binding_level
1691 && x != IDENTIFIER_IMPLICIT_DECL (name)
1692 /* No error messages for __FUNCTION__ and __PRETTY_FUNCTION__. */
1693 && !DECL_IN_SYSTEM_HEADER (x))
1694 warning ("nested extern declaration of `%s'",
1695 IDENTIFIER_POINTER (name));
1697 t = lookup_name_current_level (name);
1698 if (! t && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
1700 t = IDENTIFIER_GLOBAL_VALUE (name);
1701 /* Type decls at global scope don't conflict with externs declared
1702 inside lexical blocks. */
1703 if (! t || TREE_CODE (t) == TYPE_DECL)
1704 /* If there's no visible global declaration, try for an
1705 invisible one. */
1706 t = IDENTIFIER_LIMBO_VALUE (name);
1707 different_binding_level = 1;
1709 if (t != 0 && t == error_mark_node)
1710 /* error_mark_node is 0 for a while during initialization! */
1712 t = 0;
1713 error_with_decl (x, "`%s' used prior to declaration");
1716 /* If this decl is `static' and an implicit decl was seen previously,
1717 warn. */
1718 if (TREE_PUBLIC (name)
1719 /* Don't test for DECL_EXTERNAL, because grokdeclarator
1720 sets this for all functions. */
1721 && ! TREE_PUBLIC (x)
1722 && (TREE_CODE (x) == FUNCTION_DECL || b == global_binding_level)
1723 /* We used to warn also for explicit extern followed by static,
1724 but sometimes you need to do it that way. */
1725 && IDENTIFIER_IMPLICIT_DECL (name) != 0)
1727 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1728 IDENTIFIER_POINTER (name));
1729 pedwarn_with_file_and_line
1730 (DECL_SOURCE_FILE (IDENTIFIER_IMPLICIT_DECL (name)),
1731 DECL_SOURCE_LINE (IDENTIFIER_IMPLICIT_DECL (name)),
1732 "previous declaration of `%s'",
1733 IDENTIFIER_POINTER (name));
1734 TREE_THIS_VOLATILE (name) = 1;
1737 if (t != 0 && duplicate_decls (x, t, different_binding_level))
1739 if (TREE_CODE (t) == PARM_DECL)
1741 /* Don't allow more than one "real" duplicate
1742 of a forward parm decl. */
1743 TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
1744 return t;
1746 return t;
1749 /* If we are processing a typedef statement, generate a whole new
1750 ..._TYPE node (which will be just a variant of the existing
1751 ..._TYPE node with identical properties) and then install the
1752 TYPE_DECL node generated to represent the typedef name as the
1753 TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1755 The whole point here is to end up with a situation where each
1756 and every ..._TYPE node the compiler creates will be uniquely
1757 associated with AT MOST one node representing a typedef name.
1758 This way, even though the compiler substitutes corresponding
1759 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1760 early on, later parts of the compiler can always do the reverse
1761 translation and get back the corresponding typedef name. For
1762 example, given:
1764 typedef struct S MY_TYPE;
1765 MY_TYPE object;
1767 Later parts of the compiler might only know that `object' was of
1768 type `struct S' if it were not for code just below. With this
1769 code however, later parts of the compiler see something like:
1771 struct S' == struct S
1772 typedef struct S' MY_TYPE;
1773 struct S' object;
1775 And they can then deduce (from the node for type struct S') that
1776 the original object declaration was:
1778 MY_TYPE object;
1780 Being able to do this is important for proper support of protoize,
1781 and also for generating precise symbolic debugging information
1782 which takes full account of the programmer's (typedef) vocabulary.
1784 Obviously, we don't want to generate a duplicate ..._TYPE node if
1785 the TYPE_DECL node that we are now processing really represents a
1786 standard built-in type.
1788 Since all standard types are effectively declared at line zero
1789 in the source file, we can easily check to see if we are working
1790 on a standard type by checking the current value of lineno. */
1792 if (TREE_CODE (x) == TYPE_DECL)
1794 if (DECL_SOURCE_LINE (x) == 0)
1796 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1797 TYPE_NAME (TREE_TYPE (x)) = x;
1799 else if (TREE_TYPE (x) != error_mark_node
1800 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1802 tree tt = TREE_TYPE (x);
1803 DECL_ORIGINAL_TYPE (x) = tt;
1804 tt = build_type_copy (tt);
1805 TYPE_NAME (tt) = x;
1806 TREE_USED (tt) = TREE_USED (x);
1807 TREE_TYPE (x) = tt;
1811 /* Multiple external decls of the same identifier ought to match.
1812 We get warnings about inline functions where they are defined.
1813 Avoid duplicate warnings where they are used. */
1814 if (TREE_PUBLIC (x)
1815 && ! (TREE_CODE (x) == FUNCTION_DECL && DECL_INLINE (x)))
1817 tree decl;
1819 if (IDENTIFIER_LIMBO_VALUE (name) != 0)
1820 /* Decls in limbo are always extern, so no need to check that. */
1821 decl = IDENTIFIER_LIMBO_VALUE (name);
1822 else
1823 decl = 0;
1825 if (decl && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl))
1826 /* If old decl is built-in, we already warned if we should. */
1827 && !DECL_BUILT_IN (decl))
1829 pedwarn_with_decl (x,
1830 "type mismatch with previous external decl");
1831 pedwarn_with_decl (decl, "previous external decl of `%s'");
1835 /* If a function has had an implicit declaration, and then is defined,
1836 make sure they are compatible. */
1838 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1839 && IDENTIFIER_GLOBAL_VALUE (name) == 0
1840 && TREE_CODE (x) == FUNCTION_DECL
1841 && ! comptypes (TREE_TYPE (x),
1842 TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
1844 warning_with_decl (x, "type mismatch with previous implicit declaration");
1845 warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
1846 "previous implicit declaration of `%s'");
1849 /* This name is new in its binding level.
1850 Install the new declaration and return it. */
1851 if (b == global_binding_level)
1853 /* Install a global value. */
1855 /* If the first global decl has external linkage,
1856 warn if we later see static one. */
1857 if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
1858 TREE_PUBLIC (name) = 1;
1860 IDENTIFIER_GLOBAL_VALUE (name) = x;
1862 /* We no longer care about any previous block level declarations. */
1863 IDENTIFIER_LIMBO_VALUE (name) = 0;
1865 /* Don't forget if the function was used via an implicit decl. */
1866 if (IDENTIFIER_IMPLICIT_DECL (name)
1867 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
1868 TREE_USED (x) = 1, TREE_USED (name) = 1;
1870 /* Don't forget if its address was taken in that way. */
1871 if (IDENTIFIER_IMPLICIT_DECL (name)
1872 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
1873 TREE_ADDRESSABLE (x) = 1;
1875 /* Warn about mismatches against previous implicit decl. */
1876 if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1877 /* If this real decl matches the implicit, don't complain. */
1878 && ! (TREE_CODE (x) == FUNCTION_DECL
1879 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (x)))
1880 == integer_type_node)))
1881 pedwarn ("`%s' was previously implicitly declared to return `int'",
1882 IDENTIFIER_POINTER (name));
1884 /* If this decl is `static' and an `extern' was seen previously,
1885 that is erroneous. */
1886 if (TREE_PUBLIC (name)
1887 && ! TREE_PUBLIC (x) && ! DECL_EXTERNAL (x))
1889 /* Okay to redeclare an ANSI built-in as static. */
1890 if (t != 0 && DECL_BUILT_IN (t))
1892 /* Okay to declare a non-ANSI built-in as anything. */
1893 else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
1895 /* Okay to have global type decl after an earlier extern
1896 declaration inside a lexical block. */
1897 else if (TREE_CODE (x) == TYPE_DECL)
1899 else if (IDENTIFIER_IMPLICIT_DECL (name))
1901 if (! TREE_THIS_VOLATILE (name))
1902 pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1903 IDENTIFIER_POINTER (name));
1905 else
1906 pedwarn ("`%s' was declared `extern' and later `static'",
1907 IDENTIFIER_POINTER (name));
1910 else
1912 /* Here to install a non-global value. */
1913 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1914 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
1916 IDENTIFIER_LOCAL_VALUE (name) = x;
1918 /* If this is an extern function declaration, see if we
1919 have a global definition or declaration for the function. */
1920 if (oldlocal == 0
1921 && oldglobal != 0
1922 && TREE_CODE (x) == FUNCTION_DECL
1923 && TREE_CODE (oldglobal) == FUNCTION_DECL
1924 && DECL_EXTERNAL (x)
1925 && ! DECL_DECLARED_INLINE_P (x))
1927 /* We have one. Their types must agree. */
1928 if (! comptypes (TREE_TYPE (x),
1929 TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
1930 pedwarn_with_decl (x, "extern declaration of `%s' doesn't match global one");
1931 else
1933 /* Inner extern decl is inline if global one is.
1934 Copy enough to really inline it. */
1935 if (DECL_DECLARED_INLINE_P (oldglobal))
1937 DECL_DECLARED_INLINE_P (x)
1938 = DECL_DECLARED_INLINE_P (oldglobal);
1939 DECL_INLINE (x) = DECL_INLINE (oldglobal);
1940 DECL_INITIAL (x) = (current_function_decl == oldglobal
1941 ? 0 : DECL_INITIAL (oldglobal));
1942 DECL_SAVED_INSNS (x) = DECL_SAVED_INSNS (oldglobal);
1943 DECL_NUM_STMTS (x) = DECL_NUM_STMTS (oldglobal);
1944 DECL_ARGUMENTS (x) = DECL_ARGUMENTS (oldglobal);
1945 DECL_RESULT (x) = DECL_RESULT (oldglobal);
1946 TREE_ASM_WRITTEN (x) = TREE_ASM_WRITTEN (oldglobal);
1947 DECL_ABSTRACT_ORIGIN (x)
1948 = DECL_ABSTRACT_ORIGIN (oldglobal);
1950 /* Inner extern decl is built-in if global one is. */
1951 if (DECL_BUILT_IN (oldglobal))
1953 DECL_BUILT_IN_CLASS (x) = DECL_BUILT_IN_CLASS (oldglobal);
1954 DECL_FUNCTION_CODE (x) = DECL_FUNCTION_CODE (oldglobal);
1956 /* Keep the arg types from a file-scope fcn defn. */
1957 if (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
1958 && DECL_INITIAL (oldglobal)
1959 && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)
1960 TREE_TYPE (x) = TREE_TYPE (oldglobal);
1964 #if 0
1965 /* This case is probably sometimes the right thing to do. */
1966 /* If we have a local external declaration,
1967 then any file-scope declaration should not
1968 have been static. */
1969 if (oldlocal == 0 && oldglobal != 0
1970 && !TREE_PUBLIC (oldglobal)
1971 && DECL_EXTERNAL (x) && TREE_PUBLIC (x))
1972 warning ("`%s' locally external but globally static",
1973 IDENTIFIER_POINTER (name));
1974 #endif
1976 /* If we have a local external declaration,
1977 and no file-scope declaration has yet been seen,
1978 then if we later have a file-scope decl it must not be static. */
1979 if (oldlocal == 0
1980 && DECL_EXTERNAL (x)
1981 && TREE_PUBLIC (x))
1983 if (oldglobal == 0)
1984 TREE_PUBLIC (name) = 1;
1986 /* Save this decl, so that we can do type checking against
1987 other decls after it falls out of scope.
1989 Only save it once. This prevents temporary decls created in
1990 expand_inline_function from being used here, since this
1991 will have been set when the inline function was parsed.
1992 It also helps give slightly better warnings. */
1993 if (IDENTIFIER_LIMBO_VALUE (name) == 0)
1994 IDENTIFIER_LIMBO_VALUE (name) = x;
1997 warn_if_shadowing (x, oldlocal);
1999 /* If storing a local value, there may already be one (inherited).
2000 If so, record it for restoration when this binding level ends. */
2001 if (oldlocal != 0)
2002 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
2005 /* Keep list of variables in this level with incomplete type.
2006 If the input is erroneous, we can have error_mark in the type
2007 slot (e.g. "f(void a, ...)") - that doesn't count as an
2008 incomplete type. */
2009 if (TREE_TYPE (x) != error_mark_node
2010 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2012 tree element = TREE_TYPE (x);
2014 while (TREE_CODE (element) == ARRAY_TYPE)
2015 element = TREE_TYPE (element);
2016 if (TREE_CODE (element) == RECORD_TYPE
2017 || TREE_CODE (element) == UNION_TYPE)
2018 b->incomplete_list = tree_cons (NULL_TREE, x, b->incomplete_list);
2022 /* Put decls on list in reverse order.
2023 We will reverse them later if necessary. */
2024 TREE_CHAIN (x) = b->names;
2025 b->names = x;
2027 return x;
2030 /* Record that the local value of NAME is shadowed at function scope.
2031 This is used by build_external_ref in c-typeck.c. */
2032 void
2033 record_function_scope_shadow (name)
2034 tree name;
2036 struct binding_level *b = get_function_binding_level ();
2037 b->shadowed = tree_cons (name, IDENTIFIER_LOCAL_VALUE (name),
2038 b->shadowed);
2041 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
2043 tree
2044 pushdecl_top_level (x)
2045 tree x;
2047 tree t;
2048 struct binding_level *b = current_binding_level;
2050 current_binding_level = global_binding_level;
2051 t = pushdecl (x);
2052 current_binding_level = b;
2053 return t;
2056 /* Generate an implicit declaration for identifier FUNCTIONID
2057 as a function of type int (). Print a warning if appropriate. */
2059 tree
2060 implicitly_declare (functionid)
2061 tree functionid;
2063 tree decl;
2064 int traditional_warning = 0;
2065 /* Only one "implicit declaration" warning per identifier. */
2066 int implicit_warning;
2068 /* We used to reuse an old implicit decl here,
2069 but this loses with inline functions because it can clobber
2070 the saved decl chains. */
2071 #if 0
2072 if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
2073 decl = IDENTIFIER_IMPLICIT_DECL (functionid);
2074 else
2075 #endif
2076 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2078 /* Warn of implicit decl following explicit local extern decl.
2079 This is probably a program designed for traditional C. */
2080 if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
2081 traditional_warning = 1;
2083 /* Warn once of an implicit declaration. */
2084 implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
2086 DECL_EXTERNAL (decl) = 1;
2087 TREE_PUBLIC (decl) = 1;
2089 /* Record that we have an implicit decl and this is it. */
2090 IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
2092 /* ANSI standard says implicit declarations are in the innermost block.
2093 So we record the decl in the standard fashion. */
2094 pushdecl (decl);
2096 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2097 if (flag_objc)
2098 objc_check_decl (decl);
2100 rest_of_decl_compilation (decl, NULL, 0, 0);
2102 if (implicit_warning)
2103 implicit_decl_warning (functionid);
2104 else if (warn_traditional && traditional_warning)
2105 warning ("function `%s' was previously declared within a block",
2106 IDENTIFIER_POINTER (functionid));
2108 /* Write a record describing this implicit function declaration to the
2109 prototypes file (if requested). */
2111 gen_aux_info_record (decl, 0, 1, 0);
2113 /* Possibly apply some default attributes to this implicit declaration. */
2114 decl_attributes (&decl, NULL_TREE, 0);
2116 return decl;
2119 void
2120 implicit_decl_warning (id)
2121 tree id;
2123 const char *name = IDENTIFIER_POINTER (id);
2124 if (mesg_implicit_function_declaration == 2)
2125 error ("implicit declaration of function `%s'", name);
2126 else if (mesg_implicit_function_declaration == 1)
2127 warning ("implicit declaration of function `%s'", name);
2130 /* Return zero if the declaration NEWDECL is valid
2131 when the declaration OLDDECL (assumed to be for the same name)
2132 has already been seen.
2133 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
2134 and 3 if it is a conflicting declaration. */
2136 static int
2137 redeclaration_error_message (newdecl, olddecl)
2138 tree newdecl, olddecl;
2140 if (TREE_CODE (newdecl) == TYPE_DECL)
2142 /* Do not complain about type redeclarations where at least one
2143 declaration was in a system header. */
2144 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
2145 return 0;
2146 return 1;
2148 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2150 /* Declarations of functions can insist on internal linkage
2151 but they can't be inconsistent with internal linkage,
2152 so there can be no error on that account.
2153 However defining the same name twice is no good. */
2154 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
2155 /* However, defining once as extern inline and a second
2156 time in another way is ok. */
2157 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
2158 && ! (DECL_DECLARED_INLINE_P (newdecl)
2159 && DECL_EXTERNAL (newdecl))))
2160 return 1;
2161 return 0;
2163 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
2165 /* Objects declared at top level: */
2166 /* If at least one is a reference, it's ok. */
2167 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2168 return 0;
2169 /* Reject two definitions. */
2170 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
2171 return 1;
2172 /* Now we have two tentative defs, or one tentative and one real def. */
2173 /* Insist that the linkage match. */
2174 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2175 return 3;
2176 return 0;
2178 else if (current_binding_level->parm_flag
2179 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2180 return 0;
2181 else
2183 /* Newdecl has block scope. If olddecl has block scope also, then
2184 reject two definitions, and reject a definition together with an
2185 external reference. Otherwise, it is OK, because newdecl must
2186 be an extern reference to olddecl. */
2187 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2188 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2189 return 2;
2190 return 0;
2194 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2195 Create one if none exists so far for the current function.
2196 This function is called for both label definitions and label references. */
2198 tree
2199 lookup_label (id)
2200 tree id;
2202 tree decl = IDENTIFIER_LABEL_VALUE (id);
2204 if (current_function_decl == 0)
2206 error ("label %s referenced outside of any function",
2207 IDENTIFIER_POINTER (id));
2208 return 0;
2211 /* Use a label already defined or ref'd with this name. */
2212 if (decl != 0)
2214 /* But not if it is inherited and wasn't declared to be inheritable. */
2215 if (DECL_CONTEXT (decl) != current_function_decl
2216 && ! C_DECLARED_LABEL_FLAG (decl))
2217 return shadow_label (id);
2218 return decl;
2221 decl = build_decl (LABEL_DECL, id, void_type_node);
2223 /* A label not explicitly declared must be local to where it's ref'd. */
2224 DECL_CONTEXT (decl) = current_function_decl;
2226 DECL_MODE (decl) = VOIDmode;
2228 /* Say where one reference is to the label,
2229 for the sake of the error if it is not defined. */
2230 DECL_SOURCE_LINE (decl) = lineno;
2231 DECL_SOURCE_FILE (decl) = input_filename;
2233 IDENTIFIER_LABEL_VALUE (id) = decl;
2235 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2237 return decl;
2240 /* Make a label named NAME in the current function,
2241 shadowing silently any that may be inherited from containing functions
2242 or containing scopes.
2244 Note that valid use, if the label being shadowed
2245 comes from another scope in the same function,
2246 requires calling declare_nonlocal_label right away. */
2248 tree
2249 shadow_label (name)
2250 tree name;
2252 tree decl = IDENTIFIER_LABEL_VALUE (name);
2254 if (decl != 0)
2256 tree dup;
2258 /* Check to make sure that the label hasn't already been declared
2259 at this label scope */
2260 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2261 if (TREE_VALUE (dup) == decl)
2263 error ("duplicate label declaration `%s'",
2264 IDENTIFIER_POINTER (name));
2265 error_with_decl (TREE_VALUE (dup),
2266 "this is a previous declaration");
2267 /* Just use the previous declaration. */
2268 return lookup_label (name);
2271 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2272 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2275 return lookup_label (name);
2278 /* Define a label, specifying the location in the source file.
2279 Return the LABEL_DECL node for the label, if the definition is valid.
2280 Otherwise return 0. */
2282 tree
2283 define_label (filename, line, name)
2284 const char *filename;
2285 int line;
2286 tree name;
2288 tree decl = lookup_label (name);
2290 /* If label with this name is known from an outer context, shadow it. */
2291 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2293 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2294 IDENTIFIER_LABEL_VALUE (name) = 0;
2295 decl = lookup_label (name);
2298 if (warn_traditional && !in_system_header && lookup_name (name))
2299 warning_with_file_and_line (filename, line,
2300 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2301 IDENTIFIER_POINTER (name));
2303 if (DECL_INITIAL (decl) != 0)
2305 error_with_file_and_line (filename, line, "duplicate label `%s'",
2306 IDENTIFIER_POINTER (name));
2307 return 0;
2309 else
2311 /* Mark label as having been defined. */
2312 DECL_INITIAL (decl) = error_mark_node;
2313 /* Say where in the source. */
2314 DECL_SOURCE_FILE (decl) = filename;
2315 DECL_SOURCE_LINE (decl) = line;
2316 return decl;
2320 /* Return the list of declarations of the current level.
2321 Note that this list is in reverse order unless/until
2322 you nreverse it; and when you do nreverse it, you must
2323 store the result back using `storedecls' or you will lose. */
2325 tree
2326 getdecls ()
2328 return current_binding_level->names;
2331 /* Return the list of type-tags (for structs, etc) of the current level. */
2333 tree
2334 gettags ()
2336 return current_binding_level->tags;
2339 /* Store the list of declarations of the current level.
2340 This is done for the parameter declarations of a function being defined,
2341 after they are modified in the light of any missing parameters. */
2343 static void
2344 storedecls (decls)
2345 tree decls;
2347 current_binding_level->names = decls;
2350 /* Similarly, store the list of tags of the current level. */
2352 static void
2353 storetags (tags)
2354 tree tags;
2356 current_binding_level->tags = tags;
2359 /* Given NAME, an IDENTIFIER_NODE,
2360 return the structure (or union or enum) definition for that name.
2361 Searches binding levels from BINDING_LEVEL up to the global level.
2362 If THISLEVEL_ONLY is nonzero, searches only the specified context
2363 (but skips any tag-transparent contexts to find one that is
2364 meaningful for tags).
2365 CODE says which kind of type the caller wants;
2366 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2367 If the wrong kind of type is found, an error is reported. */
2369 static tree
2370 lookup_tag (code, name, binding_level, thislevel_only)
2371 enum tree_code code;
2372 struct binding_level *binding_level;
2373 tree name;
2374 int thislevel_only;
2376 struct binding_level *level;
2377 int thislevel = 1;
2379 for (level = binding_level; level; level = level->level_chain)
2381 tree tail;
2382 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2384 if (TREE_PURPOSE (tail) == name)
2386 if (TREE_CODE (TREE_VALUE (tail)) != code)
2388 /* Definition isn't the kind we were looking for. */
2389 pending_invalid_xref = name;
2390 pending_invalid_xref_file = input_filename;
2391 pending_invalid_xref_line = lineno;
2392 /* If in the same binding level as a declaration as a tag
2393 of a different type, this must not be allowed to
2394 shadow that tag, so give the error immediately.
2395 (For example, "struct foo; union foo;" is invalid.) */
2396 if (thislevel)
2397 pending_xref_error ();
2399 return TREE_VALUE (tail);
2402 if (! level->tag_transparent)
2404 if (thislevel_only)
2405 return NULL_TREE;
2406 thislevel = 0;
2409 return NULL_TREE;
2412 /* Print an error message now
2413 for a recent invalid struct, union or enum cross reference.
2414 We don't print them immediately because they are not invalid
2415 when used in the `struct foo;' construct for shadowing. */
2417 void
2418 pending_xref_error ()
2420 if (pending_invalid_xref != 0)
2421 error_with_file_and_line (pending_invalid_xref_file,
2422 pending_invalid_xref_line,
2423 "`%s' defined as wrong kind of tag",
2424 IDENTIFIER_POINTER (pending_invalid_xref));
2425 pending_invalid_xref = 0;
2428 /* Given a type, find the tag that was defined for it and return the tag name.
2429 Otherwise return 0. */
2431 static tree
2432 lookup_tag_reverse (type)
2433 tree type;
2435 struct binding_level *level;
2437 for (level = current_binding_level; level; level = level->level_chain)
2439 tree tail;
2440 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2442 if (TREE_VALUE (tail) == type)
2443 return TREE_PURPOSE (tail);
2446 return NULL_TREE;
2449 /* Look up NAME in the current binding level and its superiors
2450 in the namespace of variables, functions and typedefs.
2451 Return a ..._DECL node of some kind representing its definition,
2452 or return 0 if it is undefined. */
2454 tree
2455 lookup_name (name)
2456 tree name;
2458 tree val;
2460 if (current_binding_level != global_binding_level
2461 && IDENTIFIER_LOCAL_VALUE (name))
2462 val = IDENTIFIER_LOCAL_VALUE (name);
2463 else
2464 val = IDENTIFIER_GLOBAL_VALUE (name);
2465 return val;
2468 /* Similar to `lookup_name' but look only at current binding level. */
2470 tree
2471 lookup_name_current_level (name)
2472 tree name;
2474 tree t;
2476 if (current_binding_level == global_binding_level)
2477 return IDENTIFIER_GLOBAL_VALUE (name);
2479 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2480 return 0;
2482 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2483 if (DECL_NAME (t) == name)
2484 break;
2486 return t;
2489 /* Create the predefined scalar types of C,
2490 and some nodes representing standard constants (0, 1, (void *) 0).
2491 Initialize the global binding level.
2492 Make definitions for built-in primitive functions. */
2494 void
2495 c_init_decl_processing ()
2497 tree endlink;
2498 tree ptr_ftype_void, ptr_ftype_ptr;
2500 /* Adds some ggc roots, and reserved words for c-parse.in. */
2501 c_parse_init ();
2503 current_function_decl = NULL;
2504 named_labels = NULL;
2505 current_binding_level = NULL_BINDING_LEVEL;
2506 free_binding_level = NULL_BINDING_LEVEL;
2508 /* Make the binding_level structure for global names. */
2509 pushlevel (0);
2510 global_binding_level = current_binding_level;
2512 build_common_tree_nodes (flag_signed_char);
2514 c_common_nodes_and_builtins ();
2516 boolean_type_node = integer_type_node;
2517 boolean_true_node = integer_one_node;
2518 boolean_false_node = integer_zero_node;
2520 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2521 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2522 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2523 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2524 TYPE_PRECISION (c_bool_type_node) = 1;
2525 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2526 c_bool_type_node));
2527 c_bool_false_node = build_int_2 (0, 0);
2528 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2529 c_bool_true_node = build_int_2 (1, 0);
2530 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2532 endlink = void_list_node;
2533 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2534 ptr_ftype_ptr
2535 = build_function_type (ptr_type_node,
2536 tree_cons (NULL_TREE, ptr_type_node, endlink));
2538 pedantic_lvalues = pedantic;
2540 make_fname_decl = c_make_fname_decl;
2541 start_fname_decls ();
2544 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2545 decl, NAME is the initialization string and TYPE_DEP indicates whether
2546 NAME depended on the type of the function. As we don't yet implement
2547 delayed emission of static data, we mark the decl as emitted
2548 so it is not placed in the output. Anything using it must therefore pull
2549 out the STRING_CST initializer directly. This does mean that these names
2550 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2552 static tree
2553 c_make_fname_decl (id, type_dep)
2554 tree id;
2555 int type_dep;
2557 const char *name = fname_as_string (type_dep);
2558 tree decl, type, init;
2559 size_t length = strlen (name);
2561 type = build_array_type
2562 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2563 build_index_type (size_int (length)));
2565 decl = build_decl (VAR_DECL, id, type);
2566 /* We don't push the decl, so have to set its context here. */
2567 DECL_CONTEXT (decl) = current_function_decl;
2569 TREE_STATIC (decl) = 1;
2570 TREE_READONLY (decl) = 1;
2571 DECL_ARTIFICIAL (decl) = 1;
2573 init = build_string (length + 1, name);
2574 TREE_TYPE (init) = type;
2575 DECL_INITIAL (decl) = init;
2577 TREE_USED (decl) = 1;
2579 if (current_function_decl)
2581 /* Add the decls to the outermost block. */
2582 struct binding_level *old = current_binding_level;
2583 current_binding_level = get_function_binding_level ();
2584 pushdecl (decl);
2585 current_binding_level = old;
2588 finish_decl (decl, init, NULL_TREE);
2590 return decl;
2593 /* Return a definition for a builtin function named NAME and whose data type
2594 is TYPE. TYPE should be a function type with argument types.
2595 FUNCTION_CODE tells later passes how to compile calls to this function.
2596 See tree.h for its possible values.
2598 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2599 the name to be called if we can't opencode the function. If
2600 ATTRS is nonzero, use that for the function's attribute list. */
2602 tree
2603 builtin_function (name, type, function_code, class, library_name, attrs)
2604 const char *name;
2605 tree type;
2606 int function_code;
2607 enum built_in_class class;
2608 const char *library_name;
2609 tree attrs;
2611 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2612 DECL_EXTERNAL (decl) = 1;
2613 TREE_PUBLIC (decl) = 1;
2614 if (library_name)
2615 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2616 make_decl_rtl (decl, NULL);
2617 pushdecl (decl);
2618 DECL_BUILT_IN_CLASS (decl) = class;
2619 DECL_FUNCTION_CODE (decl) = function_code;
2621 /* Warn if a function in the namespace for users
2622 is used without an occasion to consider it declared. */
2623 if (name[0] != '_' || name[1] != '_')
2624 C_DECL_ANTICIPATED (decl) = 1;
2626 /* Possibly apply some default attributes to this built-in function. */
2627 if (attrs)
2628 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2629 else
2630 decl_attributes (&decl, NULL_TREE, 0);
2632 return decl;
2635 /* Apply default attributes to a function, if a system function with default
2636 attributes. */
2638 void
2639 c_insert_default_attributes (decl)
2640 tree decl;
2642 if (!TREE_PUBLIC (decl))
2643 return;
2644 c_common_insert_default_attributes (decl);
2647 /* Called when a declaration is seen that contains no names to declare.
2648 If its type is a reference to a structure, union or enum inherited
2649 from a containing scope, shadow that tag name for the current scope
2650 with a forward reference.
2651 If its type defines a new named structure or union
2652 or defines an enum, it is valid but we need not do anything here.
2653 Otherwise, it is an error. */
2655 void
2656 shadow_tag (declspecs)
2657 tree declspecs;
2659 shadow_tag_warned (declspecs, 0);
2662 void
2663 shadow_tag_warned (declspecs, warned)
2664 tree declspecs;
2665 int warned;
2666 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2667 no pedwarn. */
2669 int found_tag = 0;
2670 tree link;
2671 tree specs, attrs;
2673 pending_invalid_xref = 0;
2675 /* Remove the attributes from declspecs, since they will confuse the
2676 following code. */
2677 split_specs_attrs (declspecs, &specs, &attrs);
2679 for (link = specs; link; link = TREE_CHAIN (link))
2681 tree value = TREE_VALUE (link);
2682 enum tree_code code = TREE_CODE (value);
2684 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2685 /* Used to test also that TYPE_SIZE (value) != 0.
2686 That caused warning for `struct foo;' at top level in the file. */
2688 tree name = lookup_tag_reverse (value);
2689 tree t;
2691 found_tag++;
2693 if (name == 0)
2695 if (warned != 1 && code != ENUMERAL_TYPE)
2696 /* Empty unnamed enum OK */
2698 pedwarn ("unnamed struct/union that defines no instances");
2699 warned = 1;
2702 else
2704 t = lookup_tag (code, name, current_binding_level, 1);
2706 if (t == 0)
2708 t = make_node (code);
2709 pushtag (name, t);
2713 else
2715 if (!warned && ! in_system_header)
2717 warning ("useless keyword or type name in empty declaration");
2718 warned = 2;
2723 if (found_tag > 1)
2724 error ("two types specified in one empty declaration");
2726 if (warned != 1)
2728 if (found_tag == 0)
2729 pedwarn ("empty declaration");
2733 /* Construct an array declarator. EXPR is the expression inside [], or
2734 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2735 to the pointer to which a parameter array is converted). STATIC_P is
2736 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2737 is nonzero is the array is [*], a VLA of unspecified length which is
2738 nevertheless a complete type (not currently implemented by GCC),
2739 zero otherwise. The declarator is constructed as an ARRAY_REF
2740 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2741 left of the [] (filled by in set_array_declarator_type) and operand 1
2742 is the expression inside; whose TREE_TYPE is the type qualifiers and
2743 which has TREE_STATIC set if "static" is used. */
2745 tree
2746 build_array_declarator (expr, quals, static_p, vla_unspec_p)
2747 tree expr;
2748 tree quals;
2749 int static_p;
2750 int vla_unspec_p;
2752 tree decl;
2753 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2754 TREE_TYPE (decl) = quals;
2755 TREE_STATIC (decl) = (static_p ? 1 : 0);
2756 if (pedantic && !flag_isoc99)
2758 if (static_p || quals != NULL_TREE)
2759 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2760 if (vla_unspec_p)
2761 pedwarn ("ISO C90 does not support `[*]' array declarators");
2763 if (vla_unspec_p)
2764 warning ("GCC does not yet properly implement `[*]' array declarators");
2765 return decl;
2768 /* Set the type of an array declarator. DECL is the declarator, as
2769 constructed by build_array_declarator; TYPE is what appears on the left
2770 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2771 abstract declarator, zero otherwise; this is used to reject static and
2772 type qualifiers in abstract declarators, where they are not in the
2773 C99 grammar. */
2775 tree
2776 set_array_declarator_type (decl, type, abstract_p)
2777 tree decl;
2778 tree type;
2779 int abstract_p;
2781 TREE_OPERAND (decl, 0) = type;
2782 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2783 error ("static or type qualifiers in abstract declarator");
2784 return decl;
2787 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2789 tree
2790 groktypename (typename)
2791 tree typename;
2793 tree specs, attrs;
2795 if (TREE_CODE (typename) != TREE_LIST)
2796 return typename;
2798 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2800 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2802 /* Apply attributes. */
2803 decl_attributes (&typename, attrs, 0);
2805 return typename;
2808 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2810 tree
2811 groktypename_in_parm_context (typename)
2812 tree typename;
2814 if (TREE_CODE (typename) != TREE_LIST)
2815 return typename;
2816 return grokdeclarator (TREE_VALUE (typename),
2817 TREE_PURPOSE (typename),
2818 PARM, 0);
2821 /* Decode a declarator in an ordinary declaration or data definition.
2822 This is called as soon as the type information and variable name
2823 have been parsed, before parsing the initializer if any.
2824 Here we create the ..._DECL node, fill in its type,
2825 and put it on the list of decls for the current context.
2826 The ..._DECL node is returned as the value.
2828 Exception: for arrays where the length is not specified,
2829 the type is left null, to be filled in by `finish_decl'.
2831 Function definitions do not come here; they go to start_function
2832 instead. However, external and forward declarations of functions
2833 do go through here. Structure field declarations are done by
2834 grokfield and not through here. */
2836 tree
2837 start_decl (declarator, declspecs, initialized, attributes)
2838 tree declarator, declspecs;
2839 int initialized;
2840 tree attributes;
2842 tree decl;
2843 tree tem;
2845 /* An object declared as __attribute__((deprecated)) suppresses
2846 warnings of uses of other deprecated items. */
2847 if (lookup_attribute ("deprecated", attributes))
2848 deprecated_state = DEPRECATED_SUPPRESS;
2850 decl = grokdeclarator (declarator, declspecs,
2851 NORMAL, initialized);
2853 deprecated_state = DEPRECATED_NORMAL;
2855 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2856 && MAIN_NAME_P (DECL_NAME (decl)))
2857 warning_with_decl (decl, "`%s' is usually a function");
2859 if (initialized)
2860 /* Is it valid for this decl to have an initializer at all?
2861 If not, set INITIALIZED to zero, which will indirectly
2862 tell `finish_decl' to ignore the initializer once it is parsed. */
2863 switch (TREE_CODE (decl))
2865 case TYPE_DECL:
2866 error ("typedef `%s' is initialized (use __typeof__ instead)",
2867 IDENTIFIER_POINTER (DECL_NAME (decl)));
2868 initialized = 0;
2869 break;
2871 case FUNCTION_DECL:
2872 error ("function `%s' is initialized like a variable",
2873 IDENTIFIER_POINTER (DECL_NAME (decl)));
2874 initialized = 0;
2875 break;
2877 case PARM_DECL:
2878 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2879 error ("parameter `%s' is initialized",
2880 IDENTIFIER_POINTER (DECL_NAME (decl)));
2881 initialized = 0;
2882 break;
2884 default:
2885 /* Don't allow initializations for incomplete types
2886 except for arrays which might be completed by the initialization. */
2888 /* This can happen if the array size is an undefined macro. We already
2889 gave a warning, so we don't need another one. */
2890 if (TREE_TYPE (decl) == error_mark_node)
2891 initialized = 0;
2892 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2894 /* A complete type is ok if size is fixed. */
2896 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2897 || C_DECL_VARIABLE_SIZE (decl))
2899 error ("variable-sized object may not be initialized");
2900 initialized = 0;
2903 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2905 error ("variable `%s' has initializer but incomplete type",
2906 IDENTIFIER_POINTER (DECL_NAME (decl)));
2907 initialized = 0;
2909 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2911 error ("elements of array `%s' have incomplete type",
2912 IDENTIFIER_POINTER (DECL_NAME (decl)));
2913 initialized = 0;
2917 if (initialized)
2919 #if 0
2920 /* Seems redundant with grokdeclarator. */
2921 if (current_binding_level != global_binding_level
2922 && DECL_EXTERNAL (decl)
2923 && TREE_CODE (decl) != FUNCTION_DECL)
2924 warning ("declaration of `%s' has `extern' and is initialized",
2925 IDENTIFIER_POINTER (DECL_NAME (decl)));
2926 #endif
2927 DECL_EXTERNAL (decl) = 0;
2928 if (current_binding_level == global_binding_level)
2929 TREE_STATIC (decl) = 1;
2931 /* Tell `pushdecl' this is an initialized decl
2932 even though we don't yet have the initializer expression.
2933 Also tell `finish_decl' it may store the real initializer. */
2934 DECL_INITIAL (decl) = error_mark_node;
2937 /* If this is a function declaration, write a record describing it to the
2938 prototypes file (if requested). */
2940 if (TREE_CODE (decl) == FUNCTION_DECL)
2941 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2943 /* ANSI specifies that a tentative definition which is not merged with
2944 a non-tentative definition behaves exactly like a definition with an
2945 initializer equal to zero. (Section 3.7.2)
2947 -fno-common gives strict ANSI behavior, though this tends to break
2948 a large body of code that grew up without this rule.
2950 Thread-local variables are never common, since there's no entrenched
2951 body of code to break, and it allows more efficient variable references
2952 in the presence of dynamic linking. */
2954 if (TREE_CODE (decl) == VAR_DECL
2955 && !initialized
2956 && TREE_PUBLIC (decl)
2957 && !DECL_THREAD_LOCAL (decl)
2958 && !flag_no_common)
2959 DECL_COMMON (decl) = 1;
2961 /* Set attributes here so if duplicate decl, will have proper attributes. */
2962 decl_attributes (&decl, attributes, 0);
2964 /* If #pragma weak was used, mark the decl weak now. */
2965 if (current_binding_level == global_binding_level)
2966 maybe_apply_pragma_weak (decl);
2968 if (TREE_CODE (decl) == FUNCTION_DECL
2969 && DECL_DECLARED_INLINE_P (decl)
2970 && DECL_UNINLINABLE (decl)
2971 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2972 warning_with_decl (decl,
2973 "inline function `%s' given attribute noinline");
2975 /* Add this decl to the current binding level.
2976 TEM may equal DECL or it may be a previous decl of the same name. */
2977 tem = pushdecl (decl);
2979 /* For a local variable, define the RTL now. */
2980 if (current_binding_level != global_binding_level
2981 /* But not if this is a duplicate decl
2982 and we preserved the rtl from the previous one
2983 (which may or may not happen). */
2984 && !DECL_RTL_SET_P (tem)
2985 && !DECL_CONTEXT (tem))
2987 if (TREE_TYPE (tem) != error_mark_node
2988 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2989 expand_decl (tem);
2990 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2991 && DECL_INITIAL (tem) != 0)
2992 expand_decl (tem);
2995 return tem;
2998 /* Finish processing of a declaration;
2999 install its initial value.
3000 If the length of an array type is not known before,
3001 it must be determined now, from the initial value, or it is an error. */
3003 void
3004 finish_decl (decl, init, asmspec_tree)
3005 tree decl, init;
3006 tree asmspec_tree;
3008 tree type = TREE_TYPE (decl);
3009 int was_incomplete = (DECL_SIZE (decl) == 0);
3010 const char *asmspec = 0;
3012 /* If a name was specified, get the string. */
3013 if (current_binding_level == global_binding_level)
3014 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3015 if (asmspec_tree)
3016 asmspec = TREE_STRING_POINTER (asmspec_tree);
3018 /* If `start_decl' didn't like having an initialization, ignore it now. */
3019 if (init != 0 && DECL_INITIAL (decl) == 0)
3020 init = 0;
3022 /* Don't crash if parm is initialized. */
3023 if (TREE_CODE (decl) == PARM_DECL)
3024 init = 0;
3026 if (init)
3027 store_init_value (decl, init);
3029 /* Deduce size of array from initialization, if not already known */
3030 if (TREE_CODE (type) == ARRAY_TYPE
3031 && TYPE_DOMAIN (type) == 0
3032 && TREE_CODE (decl) != TYPE_DECL)
3034 int do_default
3035 = (TREE_STATIC (decl)
3036 /* Even if pedantic, an external linkage array
3037 may have incomplete type at first. */
3038 ? pedantic && !TREE_PUBLIC (decl)
3039 : !DECL_EXTERNAL (decl));
3040 int failure
3041 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3043 /* Get the completed type made by complete_array_type. */
3044 type = TREE_TYPE (decl);
3046 if (failure == 1)
3047 error_with_decl (decl, "initializer fails to determine size of `%s'");
3049 else if (failure == 2)
3051 if (do_default)
3052 error_with_decl (decl, "array size missing in `%s'");
3053 /* If a `static' var's size isn't known,
3054 make it extern as well as static, so it does not get
3055 allocated.
3056 If it is not `static', then do not mark extern;
3057 finish_incomplete_decl will give it a default size
3058 and it will get allocated. */
3059 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3060 DECL_EXTERNAL (decl) = 1;
3063 /* TYPE_MAX_VALUE is always one less than the number of elements
3064 in the array, because we start counting at zero. Therefore,
3065 warn only if the value is less than zero. */
3066 else if (pedantic && TYPE_DOMAIN (type) != 0
3067 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3068 error_with_decl (decl, "zero or negative size array `%s'");
3070 layout_decl (decl, 0);
3073 if (TREE_CODE (decl) == VAR_DECL)
3075 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3076 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3077 layout_decl (decl, 0);
3079 if (DECL_SIZE (decl) == 0
3080 /* Don't give an error if we already gave one earlier. */
3081 && TREE_TYPE (decl) != error_mark_node
3082 && (TREE_STATIC (decl)
3084 /* A static variable with an incomplete type
3085 is an error if it is initialized.
3086 Also if it is not file scope.
3087 Otherwise, let it through, but if it is not `extern'
3088 then it may cause an error message later. */
3089 (DECL_INITIAL (decl) != 0
3090 || DECL_CONTEXT (decl) != 0)
3092 /* An automatic variable with an incomplete type
3093 is an error. */
3094 !DECL_EXTERNAL (decl)))
3096 error_with_decl (decl, "storage size of `%s' isn't known");
3097 TREE_TYPE (decl) = error_mark_node;
3100 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3101 && DECL_SIZE (decl) != 0)
3103 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3104 constant_expression_warning (DECL_SIZE (decl));
3105 else
3106 error_with_decl (decl, "storage size of `%s' isn't constant");
3109 if (TREE_USED (type))
3110 TREE_USED (decl) = 1;
3113 /* If this is a function and an assembler name is specified, it isn't
3114 builtin any more. Also reset DECL_RTL so we can give it its new
3115 name. */
3116 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3118 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
3119 SET_DECL_RTL (decl, NULL_RTX);
3120 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3123 /* Output the assembler code and/or RTL code for variables and functions,
3124 unless the type is an undefined structure or union.
3125 If not, it will get done when the type is completed. */
3127 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3129 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3130 if (flag_objc)
3131 objc_check_decl (decl);
3133 if (!DECL_CONTEXT (decl))
3135 if (DECL_INITIAL (decl) == NULL_TREE
3136 || DECL_INITIAL (decl) == error_mark_node)
3137 /* Don't output anything
3138 when a tentative file-scope definition is seen.
3139 But at end of compilation, do output code for them. */
3140 DECL_DEFER_OUTPUT (decl) = 1;
3141 rest_of_decl_compilation (decl, asmspec,
3142 (DECL_CONTEXT (decl) == 0
3143 || TREE_ASM_WRITTEN (decl)), 0);
3145 else
3147 /* This is a local variable. If there is an ASMSPEC, the
3148 user has requested that we handle it specially. */
3149 if (asmspec)
3151 /* In conjunction with an ASMSPEC, the `register'
3152 keyword indicates that we should place the variable
3153 in a particular register. */
3154 if (DECL_REGISTER (decl))
3155 DECL_C_HARD_REGISTER (decl) = 1;
3157 /* If this is not a static variable, issue a warning.
3158 It doesn't make any sense to give an ASMSPEC for an
3159 ordinary, non-register local variable. Historically,
3160 GCC has accepted -- but ignored -- the ASMSPEC in
3161 this case. */
3162 if (TREE_CODE (decl) == VAR_DECL
3163 && !DECL_REGISTER (decl)
3164 && !TREE_STATIC (decl))
3165 warning_with_decl (decl,
3166 "ignoring asm-specifier for non-static local variable `%s'");
3167 else
3168 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
3171 if (TREE_CODE (decl) != FUNCTION_DECL)
3172 add_decl_stmt (decl);
3175 if (DECL_CONTEXT (decl) != 0)
3177 /* Recompute the RTL of a local array now
3178 if it used to be an incomplete type. */
3179 if (was_incomplete
3180 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3182 /* If we used it already as memory, it must stay in memory. */
3183 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3184 /* If it's still incomplete now, no init will save it. */
3185 if (DECL_SIZE (decl) == 0)
3186 DECL_INITIAL (decl) = 0;
3191 if (TREE_CODE (decl) == TYPE_DECL)
3193 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3194 if (flag_objc)
3195 objc_check_decl (decl);
3196 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
3199 /* At the end of a declaration, throw away any variable type sizes
3200 of types defined inside that declaration. There is no use
3201 computing them in the following function definition. */
3202 if (current_binding_level == global_binding_level)
3203 get_pending_sizes ();
3206 /* Given a parsed parameter declaration,
3207 decode it into a PARM_DECL and push that on the current binding level.
3208 Also, for the sake of forward parm decls,
3209 record the given order of parms in `parm_order'. */
3211 void
3212 push_parm_decl (parm)
3213 tree parm;
3215 tree decl;
3216 int old_immediate_size_expand = immediate_size_expand;
3217 /* Don't try computing parm sizes now -- wait till fn is called. */
3218 immediate_size_expand = 0;
3220 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3221 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3222 decl_attributes (&decl, TREE_VALUE (parm), 0);
3224 #if 0
3225 if (DECL_NAME (decl))
3227 tree olddecl;
3228 olddecl = lookup_name (DECL_NAME (decl));
3229 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3230 pedwarn_with_decl (decl,
3231 "ISO C forbids parameter `%s' shadowing typedef");
3233 #endif
3235 decl = pushdecl (decl);
3237 immediate_size_expand = old_immediate_size_expand;
3239 current_binding_level->parm_order
3240 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3242 /* Add this decl to the current binding level. */
3243 finish_decl (decl, NULL_TREE, NULL_TREE);
3246 /* Clear the given order of parms in `parm_order'.
3247 Used at start of parm list,
3248 and also at semicolon terminating forward decls. */
3250 void
3251 clear_parm_order ()
3253 current_binding_level->parm_order = NULL_TREE;
3256 static GTY(()) int compound_literal_number;
3258 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3259 literal, which may be an incomplete array type completed by the
3260 initializer; INIT is a CONSTRUCTOR that initializes the compound
3261 literal. */
3263 tree
3264 build_compound_literal (type, init)
3265 tree type;
3266 tree init;
3268 /* We do not use start_decl here because we have a type, not a declarator;
3269 and do not use finish_decl because the decl should be stored inside
3270 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3271 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3272 tree complit;
3273 tree stmt;
3274 DECL_EXTERNAL (decl) = 0;
3275 TREE_PUBLIC (decl) = 0;
3276 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3277 DECL_CONTEXT (decl) = current_function_decl;
3278 TREE_USED (decl) = 1;
3279 TREE_TYPE (decl) = type;
3280 TREE_READONLY (decl) = TREE_READONLY (type);
3281 store_init_value (decl, init);
3283 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3285 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3286 if (failure)
3287 abort ();
3290 type = TREE_TYPE (decl);
3291 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3292 return error_mark_node;
3294 stmt = build_stmt (DECL_STMT, decl);
3295 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3296 TREE_SIDE_EFFECTS (complit) = 1;
3298 layout_decl (decl, 0);
3300 if (TREE_STATIC (decl))
3302 /* This decl needs a name for the assembler output. We also need
3303 a unique suffix to be added to the name. */
3304 char *name;
3306 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3307 compound_literal_number);
3308 compound_literal_number++;
3309 DECL_NAME (decl) = get_identifier (name);
3310 DECL_DEFER_OUTPUT (decl) = 1;
3311 DECL_COMDAT (decl) = 1;
3312 DECL_ARTIFICIAL (decl) = 1;
3313 pushdecl (decl);
3314 rest_of_decl_compilation (decl, NULL, 1, 0);
3317 return complit;
3320 /* Make TYPE a complete type based on INITIAL_VALUE.
3321 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3322 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3325 complete_array_type (type, initial_value, do_default)
3326 tree type;
3327 tree initial_value;
3328 int do_default;
3330 tree maxindex = NULL_TREE;
3331 int value = 0;
3333 if (initial_value)
3335 /* Note MAXINDEX is really the maximum index,
3336 one less than the size. */
3337 if (TREE_CODE (initial_value) == STRING_CST)
3339 int eltsize
3340 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3341 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3342 / eltsize) - 1, 0);
3344 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3346 tree elts = CONSTRUCTOR_ELTS (initial_value);
3347 maxindex = build_int_2 (-1, -1);
3348 for (; elts; elts = TREE_CHAIN (elts))
3350 if (TREE_PURPOSE (elts))
3351 maxindex = TREE_PURPOSE (elts);
3352 else
3353 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3354 maxindex, integer_one_node));
3356 maxindex = copy_node (maxindex);
3358 else
3360 /* Make an error message unless that happened already. */
3361 if (initial_value != error_mark_node)
3362 value = 1;
3364 /* Prevent further error messages. */
3365 maxindex = build_int_2 (0, 0);
3369 if (!maxindex)
3371 if (do_default)
3372 maxindex = build_int_2 (0, 0);
3373 value = 2;
3376 if (maxindex)
3378 TYPE_DOMAIN (type) = build_index_type (maxindex);
3379 if (!TREE_TYPE (maxindex))
3380 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3383 /* Lay out the type now that we can get the real answer. */
3385 layout_type (type);
3387 return value;
3390 /* Determine whether TYPE is a structure with a flexible array member,
3391 or a union containing such a structure (possibly recursively). */
3393 static bool
3394 flexible_array_type_p (type)
3395 tree type;
3397 tree x;
3398 switch (TREE_CODE (type))
3400 case RECORD_TYPE:
3401 x = TYPE_FIELDS (type);
3402 if (x == NULL_TREE)
3403 return false;
3404 while (TREE_CHAIN (x) != NULL_TREE)
3405 x = TREE_CHAIN (x);
3406 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3407 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3408 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3409 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3410 return true;
3411 return false;
3412 case UNION_TYPE:
3413 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3415 if (flexible_array_type_p (TREE_TYPE (x)))
3416 return true;
3418 return false;
3419 default:
3420 return false;
3424 /* Given declspecs and a declarator,
3425 determine the name and type of the object declared
3426 and construct a ..._DECL node for it.
3427 (In one case we can return a ..._TYPE node instead.
3428 For invalid input we sometimes return 0.)
3430 DECLSPECS is a chain of tree_list nodes whose value fields
3431 are the storage classes and type specifiers.
3433 DECL_CONTEXT says which syntactic context this declaration is in:
3434 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3435 FUNCDEF for a function definition. Like NORMAL but a few different
3436 error messages in each case. Return value may be zero meaning
3437 this definition is too screwy to try to parse.
3438 PARM for a parameter declaration (either within a function prototype
3439 or before a function body). Make a PARM_DECL, or return void_type_node.
3440 TYPENAME if for a typename (in a cast or sizeof).
3441 Don't make a DECL node; just return the ..._TYPE node.
3442 FIELD for a struct or union field; make a FIELD_DECL.
3443 BITFIELD for a field with specified width.
3444 INITIALIZED is 1 if the decl has an initializer.
3446 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3447 It may also be so in the PARM case, for a prototype where the
3448 argument type is specified but not the name.
3450 This function is where the complicated C meanings of `static'
3451 and `extern' are interpreted. */
3453 static tree
3454 grokdeclarator (declarator, declspecs, decl_context, initialized)
3455 tree declspecs;
3456 tree declarator;
3457 enum decl_context decl_context;
3458 int initialized;
3460 int specbits = 0;
3461 tree spec;
3462 tree type = NULL_TREE;
3463 int longlong = 0;
3464 int constp;
3465 int restrictp;
3466 int volatilep;
3467 int type_quals = TYPE_UNQUALIFIED;
3468 int inlinep;
3469 int explicit_int = 0;
3470 int explicit_char = 0;
3471 int defaulted_int = 0;
3472 tree typedef_decl = 0;
3473 const char *name;
3474 tree typedef_type = 0;
3475 int funcdef_flag = 0;
3476 enum tree_code innermost_code = ERROR_MARK;
3477 int bitfield = 0;
3478 int size_varies = 0;
3479 tree decl_attr = NULL_TREE;
3480 tree array_ptr_quals = NULL_TREE;
3481 int array_parm_static = 0;
3482 tree returned_attrs = NULL_TREE;
3484 if (decl_context == BITFIELD)
3485 bitfield = 1, decl_context = FIELD;
3487 if (decl_context == FUNCDEF)
3488 funcdef_flag = 1, decl_context = NORMAL;
3490 /* Look inside a declarator for the name being declared
3491 and get it as a string, for an error message. */
3493 tree decl = declarator;
3494 name = 0;
3496 while (decl)
3497 switch (TREE_CODE (decl))
3499 case ARRAY_REF:
3500 case INDIRECT_REF:
3501 case CALL_EXPR:
3502 innermost_code = TREE_CODE (decl);
3503 decl = TREE_OPERAND (decl, 0);
3504 break;
3506 case TREE_LIST:
3507 decl = TREE_VALUE (decl);
3508 break;
3510 case IDENTIFIER_NODE:
3511 name = IDENTIFIER_POINTER (decl);
3512 decl = 0;
3513 break;
3515 default:
3516 abort ();
3518 if (name == 0)
3519 name = "type name";
3522 /* A function definition's declarator must have the form of
3523 a function declarator. */
3525 if (funcdef_flag && innermost_code != CALL_EXPR)
3526 return 0;
3528 /* Anything declared one level down from the top level
3529 must be one of the parameters of a function
3530 (because the body is at least two levels down). */
3532 /* If this looks like a function definition, make it one,
3533 even if it occurs where parms are expected.
3534 Then store_parm_decls will reject it and not use it as a parm. */
3535 if (decl_context == NORMAL && !funcdef_flag
3536 && current_binding_level->parm_flag)
3537 decl_context = PARM;
3539 /* Look through the decl specs and record which ones appear.
3540 Some typespecs are defined as built-in typenames.
3541 Others, the ones that are modifiers of other types,
3542 are represented by bits in SPECBITS: set the bits for
3543 the modifiers that appear. Storage class keywords are also in SPECBITS.
3545 If there is a typedef name or a type, store the type in TYPE.
3546 This includes builtin typedefs such as `int'.
3548 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3549 and did not come from a user typedef.
3551 Set LONGLONG if `long' is mentioned twice. */
3553 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3555 tree id = TREE_VALUE (spec);
3557 /* If the entire declaration is itself tagged as deprecated then
3558 suppress reports of deprecated items. */
3559 if (id && TREE_DEPRECATED (id))
3561 if (deprecated_state != DEPRECATED_SUPPRESS)
3562 warn_deprecated_use (id);
3565 if (id == ridpointers[(int) RID_INT])
3566 explicit_int = 1;
3567 if (id == ridpointers[(int) RID_CHAR])
3568 explicit_char = 1;
3570 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3572 enum rid i = C_RID_CODE (id);
3573 if ((int) i <= (int) RID_LAST_MODIFIER)
3575 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3577 if (longlong)
3578 error ("`long long long' is too long for GCC");
3579 else
3581 if (pedantic && !flag_isoc99 && ! in_system_header
3582 && warn_long_long)
3583 pedwarn ("ISO C90 does not support `long long'");
3584 longlong = 1;
3587 else if (specbits & (1 << (int) i))
3589 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3591 if (!flag_isoc99)
3592 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3594 else
3595 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3598 /* Diagnose "__thread extern". Recall that this list
3599 is in the reverse order seen in the text. */
3600 if (i == RID_THREAD
3601 && (specbits & (1 << (int) RID_EXTERN
3602 | 1 << (int) RID_STATIC)))
3604 if (specbits & 1 << (int) RID_EXTERN)
3605 error ("`__thread' before `extern'");
3606 else
3607 error ("`__thread' before `static'");
3610 specbits |= 1 << (int) i;
3611 goto found;
3614 if (type)
3615 error ("two or more data types in declaration of `%s'", name);
3616 /* Actual typedefs come to us as TYPE_DECL nodes. */
3617 else if (TREE_CODE (id) == TYPE_DECL)
3619 if (TREE_TYPE (id) == error_mark_node)
3620 ; /* Allow the type to default to int to avoid cascading errors. */
3621 else
3623 type = TREE_TYPE (id);
3624 decl_attr = DECL_ATTRIBUTES (id);
3625 typedef_decl = id;
3628 /* Built-in types come as identifiers. */
3629 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3631 tree t = lookup_name (id);
3632 if (TREE_TYPE (t) == error_mark_node)
3634 else if (!t || TREE_CODE (t) != TYPE_DECL)
3635 error ("`%s' fails to be a typedef or built in type",
3636 IDENTIFIER_POINTER (id));
3637 else
3639 type = TREE_TYPE (t);
3640 typedef_decl = t;
3643 else if (TREE_CODE (id) != ERROR_MARK)
3644 type = id;
3646 found:
3650 typedef_type = type;
3651 if (type)
3652 size_varies = C_TYPE_VARIABLE_SIZE (type);
3654 /* No type at all: default to `int', and set DEFAULTED_INT
3655 because it was not a user-defined typedef. */
3657 if (type == 0)
3659 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3660 | (1 << (int) RID_SIGNED)
3661 | (1 << (int) RID_UNSIGNED)
3662 | (1 << (int) RID_COMPLEX))))
3663 /* Don't warn about typedef foo = bar. */
3664 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3665 && ! in_system_header)
3667 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3668 and this is a function, or if -Wimplicit; prefer the former
3669 warning since it is more explicit. */
3670 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3671 && funcdef_flag)
3672 warn_about_return_type = 1;
3673 else if (warn_implicit_int || flag_isoc99)
3674 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3675 name);
3678 defaulted_int = 1;
3679 type = integer_type_node;
3682 /* Now process the modifiers that were specified
3683 and check for invalid combinations. */
3685 /* Long double is a special combination. */
3687 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3688 && TYPE_MAIN_VARIANT (type) == double_type_node)
3690 specbits &= ~(1 << (int) RID_LONG);
3691 type = long_double_type_node;
3694 /* Check all other uses of type modifiers. */
3696 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3697 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3699 int ok = 0;
3701 if ((specbits & 1 << (int) RID_LONG)
3702 && (specbits & 1 << (int) RID_SHORT))
3703 error ("both long and short specified for `%s'", name);
3704 else if (((specbits & 1 << (int) RID_LONG)
3705 || (specbits & 1 << (int) RID_SHORT))
3706 && explicit_char)
3707 error ("long or short specified with char for `%s'", name);
3708 else if (((specbits & 1 << (int) RID_LONG)
3709 || (specbits & 1 << (int) RID_SHORT))
3710 && TREE_CODE (type) == REAL_TYPE)
3712 static int already = 0;
3714 error ("long or short specified with floating type for `%s'", name);
3715 if (! already && ! pedantic)
3717 error ("the only valid combination is `long double'");
3718 already = 1;
3721 else if ((specbits & 1 << (int) RID_SIGNED)
3722 && (specbits & 1 << (int) RID_UNSIGNED))
3723 error ("both signed and unsigned specified for `%s'", name);
3724 else if (TREE_CODE (type) != INTEGER_TYPE)
3725 error ("long, short, signed or unsigned invalid for `%s'", name);
3726 else
3728 ok = 1;
3729 if (!explicit_int && !defaulted_int && !explicit_char)
3731 error ("long, short, signed or unsigned used invalidly for `%s'",
3732 name);
3733 ok = 0;
3737 /* Discard the type modifiers if they are invalid. */
3738 if (! ok)
3740 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3741 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3742 longlong = 0;
3746 if ((specbits & (1 << (int) RID_COMPLEX))
3747 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3749 error ("complex invalid for `%s'", name);
3750 specbits &= ~(1 << (int) RID_COMPLEX);
3753 /* Decide whether an integer type is signed or not.
3754 Optionally treat bitfields as signed by default. */
3755 if (specbits & 1 << (int) RID_UNSIGNED
3756 || (bitfield && ! flag_signed_bitfields
3757 && (explicit_int || defaulted_int || explicit_char
3758 /* A typedef for plain `int' without `signed'
3759 can be controlled just like plain `int'. */
3760 || ! (typedef_decl != 0
3761 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3762 && TREE_CODE (type) != ENUMERAL_TYPE
3763 && !(specbits & 1 << (int) RID_SIGNED)))
3765 if (longlong)
3766 type = long_long_unsigned_type_node;
3767 else if (specbits & 1 << (int) RID_LONG)
3768 type = long_unsigned_type_node;
3769 else if (specbits & 1 << (int) RID_SHORT)
3770 type = short_unsigned_type_node;
3771 else if (type == char_type_node)
3772 type = unsigned_char_type_node;
3773 else if (typedef_decl)
3774 type = c_common_unsigned_type (type);
3775 else
3776 type = unsigned_type_node;
3778 else if ((specbits & 1 << (int) RID_SIGNED)
3779 && type == char_type_node)
3780 type = signed_char_type_node;
3781 else if (longlong)
3782 type = long_long_integer_type_node;
3783 else if (specbits & 1 << (int) RID_LONG)
3784 type = long_integer_type_node;
3785 else if (specbits & 1 << (int) RID_SHORT)
3786 type = short_integer_type_node;
3788 if (specbits & 1 << (int) RID_COMPLEX)
3790 if (pedantic && !flag_isoc99)
3791 pedwarn ("ISO C90 does not support complex types");
3792 /* If we just have "complex", it is equivalent to
3793 "complex double", but if any modifiers at all are specified it is
3794 the complex form of TYPE. E.g, "complex short" is
3795 "complex short int". */
3797 if (defaulted_int && ! longlong
3798 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3799 | (1 << (int) RID_SIGNED)
3800 | (1 << (int) RID_UNSIGNED))))
3802 if (pedantic)
3803 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3804 type = complex_double_type_node;
3806 else if (type == integer_type_node)
3808 if (pedantic)
3809 pedwarn ("ISO C does not support complex integer types");
3810 type = complex_integer_type_node;
3812 else if (type == float_type_node)
3813 type = complex_float_type_node;
3814 else if (type == double_type_node)
3815 type = complex_double_type_node;
3816 else if (type == long_double_type_node)
3817 type = complex_long_double_type_node;
3818 else
3820 if (pedantic)
3821 pedwarn ("ISO C does not support complex integer types");
3822 type = build_complex_type (type);
3826 /* Figure out the type qualifiers for the declaration. There are
3827 two ways a declaration can become qualified. One is something
3828 like `const int i' where the `const' is explicit. Another is
3829 something like `typedef const int CI; CI i' where the type of the
3830 declaration contains the `const'. */
3831 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3832 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3833 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3834 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3835 if (constp > 1 && ! flag_isoc99)
3836 pedwarn ("duplicate `const'");
3837 if (restrictp > 1 && ! flag_isoc99)
3838 pedwarn ("duplicate `restrict'");
3839 if (volatilep > 1 && ! flag_isoc99)
3840 pedwarn ("duplicate `volatile'");
3841 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3842 type = TYPE_MAIN_VARIANT (type);
3843 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3844 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3845 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3847 /* Warn if two storage classes are given. Default to `auto'. */
3850 int nclasses = 0;
3852 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3853 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3854 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3855 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3856 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3858 /* "static __thread" and "extern __thread" are allowed. */
3859 if ((specbits & (1 << (int) RID_THREAD
3860 | 1 << (int) RID_STATIC
3861 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3862 nclasses++;
3864 /* Warn about storage classes that are invalid for certain
3865 kinds of declarations (parameters, typenames, etc.). */
3867 if (nclasses > 1)
3868 error ("multiple storage classes in declaration of `%s'", name);
3869 else if (funcdef_flag
3870 && (specbits
3871 & ((1 << (int) RID_REGISTER)
3872 | (1 << (int) RID_AUTO)
3873 | (1 << (int) RID_TYPEDEF)
3874 | (1 << (int) RID_THREAD))))
3876 if (specbits & 1 << (int) RID_AUTO
3877 && (pedantic || current_binding_level == global_binding_level))
3878 pedwarn ("function definition declared `auto'");
3879 if (specbits & 1 << (int) RID_REGISTER)
3880 error ("function definition declared `register'");
3881 if (specbits & 1 << (int) RID_TYPEDEF)
3882 error ("function definition declared `typedef'");
3883 if (specbits & 1 << (int) RID_THREAD)
3884 error ("function definition declared `__thread'");
3885 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3886 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3888 else if (decl_context != NORMAL && nclasses > 0)
3890 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3892 else
3894 switch (decl_context)
3896 case FIELD:
3897 error ("storage class specified for structure field `%s'",
3898 name);
3899 break;
3900 case PARM:
3901 error ("storage class specified for parameter `%s'", name);
3902 break;
3903 default:
3904 error ("storage class specified for typename");
3905 break;
3907 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3908 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3909 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3912 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3914 /* `extern' with initialization is invalid if not at top level. */
3915 if (current_binding_level == global_binding_level)
3916 warning ("`%s' initialized and declared `extern'", name);
3917 else
3918 error ("`%s' has both `extern' and initializer", name);
3920 else if (current_binding_level == global_binding_level)
3922 if (specbits & 1 << (int) RID_AUTO)
3923 error ("top-level declaration of `%s' specifies `auto'", name);
3925 else
3927 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3928 error ("nested function `%s' declared `extern'", name);
3929 else if ((specbits & (1 << (int) RID_THREAD
3930 | 1 << (int) RID_EXTERN
3931 | 1 << (int) RID_STATIC))
3932 == (1 << (int) RID_THREAD))
3934 error ("function-scope `%s' implicitly auto and declared `__thread'",
3935 name);
3936 specbits &= ~(1 << (int) RID_THREAD);
3941 /* Now figure out the structure of the declarator proper.
3942 Descend through it, creating more complex types, until we reach
3943 the declared identifier (or NULL_TREE, in an absolute declarator). */
3945 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3947 if (type == error_mark_node)
3949 declarator = TREE_OPERAND (declarator, 0);
3950 continue;
3953 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3954 an INDIRECT_REF (for *...),
3955 a CALL_EXPR (for ...(...)),
3956 a TREE_LIST (for nested attributes),
3957 an identifier (for the name being declared)
3958 or a null pointer (for the place in an absolute declarator
3959 where the name was omitted).
3960 For the last two cases, we have just exited the loop.
3962 At this point, TYPE is the type of elements of an array,
3963 or for a function to return, or for a pointer to point to.
3964 After this sequence of ifs, TYPE is the type of the
3965 array or function or pointer, and DECLARATOR has had its
3966 outermost layer removed. */
3968 if (array_ptr_quals != NULL_TREE || array_parm_static)
3970 /* Only the innermost declarator (making a parameter be of
3971 array type which is converted to pointer type)
3972 may have static or type qualifiers. */
3973 error ("static or type qualifiers in non-parameter array declarator");
3974 array_ptr_quals = NULL_TREE;
3975 array_parm_static = 0;
3978 if (TREE_CODE (declarator) == TREE_LIST)
3980 /* We encode a declarator with embedded attributes using
3981 a TREE_LIST. */
3982 tree attrs = TREE_PURPOSE (declarator);
3983 tree inner_decl;
3984 int attr_flags = 0;
3985 declarator = TREE_VALUE (declarator);
3986 inner_decl = declarator;
3987 while (inner_decl != NULL_TREE
3988 && TREE_CODE (inner_decl) == TREE_LIST)
3989 inner_decl = TREE_VALUE (inner_decl);
3990 if (inner_decl == NULL_TREE
3991 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3992 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3993 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3994 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3995 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3996 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3997 returned_attrs = decl_attributes (&type,
3998 chainon (returned_attrs, attrs),
3999 attr_flags);
4001 else if (TREE_CODE (declarator) == ARRAY_REF)
4003 tree itype = NULL_TREE;
4004 tree size = TREE_OPERAND (declarator, 1);
4005 /* The index is a signed object `sizetype' bits wide. */
4006 tree index_type = c_common_signed_type (sizetype);
4008 array_ptr_quals = TREE_TYPE (declarator);
4009 array_parm_static = TREE_STATIC (declarator);
4011 declarator = TREE_OPERAND (declarator, 0);
4013 /* Check for some types that there cannot be arrays of. */
4015 if (VOID_TYPE_P (type))
4017 error ("declaration of `%s' as array of voids", name);
4018 type = error_mark_node;
4021 if (TREE_CODE (type) == FUNCTION_TYPE)
4023 error ("declaration of `%s' as array of functions", name);
4024 type = error_mark_node;
4027 if (pedantic && flexible_array_type_p (type))
4028 pedwarn ("invalid use of structure with flexible array member");
4030 if (size == error_mark_node)
4031 type = error_mark_node;
4033 if (type == error_mark_node)
4034 continue;
4036 /* If size was specified, set ITYPE to a range-type for that size.
4037 Otherwise, ITYPE remains null. finish_decl may figure it out
4038 from an initial value. */
4040 if (size)
4042 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4043 STRIP_TYPE_NOPS (size);
4045 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4047 error ("size of array `%s' has non-integer type", name);
4048 size = integer_one_node;
4051 if (pedantic && integer_zerop (size))
4052 pedwarn ("ISO C forbids zero-size array `%s'", name);
4054 if (TREE_CODE (size) == INTEGER_CST)
4056 constant_expression_warning (size);
4057 if (tree_int_cst_sgn (size) < 0)
4059 error ("size of array `%s' is negative", name);
4060 size = integer_one_node;
4063 else
4065 /* Make sure the array size remains visibly nonconstant
4066 even if it is (eg) a const variable with known value. */
4067 size_varies = 1;
4069 if (!flag_isoc99 && pedantic)
4071 if (TREE_CONSTANT (size))
4072 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4073 name);
4074 else
4075 pedwarn ("ISO C90 forbids variable-size array `%s'",
4076 name);
4080 if (integer_zerop (size))
4082 /* A zero-length array cannot be represented with an
4083 unsigned index type, which is what we'll get with
4084 build_index_type. Create an open-ended range instead. */
4085 itype = build_range_type (sizetype, size, NULL_TREE);
4087 else
4089 /* Compute the maximum valid index, that is, size - 1.
4090 Do the calculation in index_type, so that if it is
4091 a variable the computations will be done in the
4092 proper mode. */
4093 itype = fold (build (MINUS_EXPR, index_type,
4094 convert (index_type, size),
4095 convert (index_type, size_one_node)));
4097 /* If that overflowed, the array is too big.
4098 ??? While a size of INT_MAX+1 technically shouldn't
4099 cause an overflow (because we subtract 1), the overflow
4100 is recorded during the conversion to index_type, before
4101 the subtraction. Handling this case seems like an
4102 unnecessary complication. */
4103 if (TREE_OVERFLOW (itype))
4105 error ("size of array `%s' is too large", name);
4106 type = error_mark_node;
4107 continue;
4110 if (size_varies)
4111 itype = variable_size (itype);
4112 itype = build_index_type (itype);
4115 else if (decl_context == FIELD)
4117 if (pedantic && !flag_isoc99 && !in_system_header)
4118 pedwarn ("ISO C90 does not support flexible array members");
4120 /* ISO C99 Flexible array members are effectively identical
4121 to GCC's zero-length array extension. */
4122 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4125 /* If pedantic, complain about arrays of incomplete types. */
4127 if (pedantic && !COMPLETE_TYPE_P (type))
4128 pedwarn ("array type has incomplete element type");
4130 #if 0
4131 /* We shouldn't have a function type here at all!
4132 Functions aren't allowed as array elements. */
4133 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4134 && (constp || volatilep))
4135 pedwarn ("ISO C forbids const or volatile function types");
4136 #endif
4138 /* Build the array type itself, then merge any constancy or
4139 volatility into the target type. We must do it in this order
4140 to ensure that the TYPE_MAIN_VARIANT field of the array type
4141 is set correctly. */
4143 type = build_array_type (type, itype);
4144 if (type_quals)
4145 type = c_build_qualified_type (type, type_quals);
4147 if (size_varies)
4148 C_TYPE_VARIABLE_SIZE (type) = 1;
4150 /* The GCC extension for zero-length arrays differs from
4151 ISO flexible array members in that sizeof yields zero. */
4152 if (size && integer_zerop (size))
4154 layout_type (type);
4155 TYPE_SIZE (type) = bitsize_zero_node;
4156 TYPE_SIZE_UNIT (type) = size_zero_node;
4158 if (decl_context != PARM
4159 && (array_ptr_quals != NULL_TREE || array_parm_static))
4161 error ("static or type qualifiers in non-parameter array declarator");
4162 array_ptr_quals = NULL_TREE;
4163 array_parm_static = 0;
4166 else if (TREE_CODE (declarator) == CALL_EXPR)
4168 tree arg_types;
4170 /* Declaring a function type.
4171 Make sure we have a valid type for the function to return. */
4172 if (type == error_mark_node)
4173 continue;
4175 size_varies = 0;
4177 /* Warn about some types functions can't return. */
4179 if (TREE_CODE (type) == FUNCTION_TYPE)
4181 error ("`%s' declared as function returning a function", name);
4182 type = integer_type_node;
4184 if (TREE_CODE (type) == ARRAY_TYPE)
4186 error ("`%s' declared as function returning an array", name);
4187 type = integer_type_node;
4190 /* Construct the function type and go to the next
4191 inner layer of declarator. */
4193 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4194 funcdef_flag
4195 /* Say it's a definition
4196 only for the CALL_EXPR
4197 closest to the identifier. */
4198 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4199 /* Type qualifiers before the return type of the function
4200 qualify the return type, not the function type. */
4201 if (type_quals)
4203 /* Type qualifiers on a function return type are normally
4204 permitted by the standard but have no effect, so give a
4205 warning at -Wextra. Qualifiers on a void return type have
4206 meaning as a GNU extension, and are banned on function
4207 definitions in ISO C. FIXME: strictly we shouldn't
4208 pedwarn for qualified void return types except on function
4209 definitions, but not doing so could lead to the undesirable
4210 state of a "volatile void" function return type not being
4211 warned about, and a use of the function being compiled
4212 with GNU semantics, with no diagnostics under -pedantic. */
4213 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4214 pedwarn ("ISO C forbids qualified void function return type");
4215 else if (extra_warnings
4216 && !(VOID_TYPE_P (type)
4217 && type_quals == TYPE_QUAL_VOLATILE))
4218 warning ("type qualifiers ignored on function return type");
4220 type = c_build_qualified_type (type, type_quals);
4222 type_quals = TYPE_UNQUALIFIED;
4224 type = build_function_type (type, arg_types);
4225 declarator = TREE_OPERAND (declarator, 0);
4227 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4228 the formal parameter list of this FUNCTION_TYPE to point to
4229 the FUNCTION_TYPE node itself. */
4232 tree link;
4234 for (link = last_function_parm_tags;
4235 link;
4236 link = TREE_CHAIN (link))
4237 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4240 else if (TREE_CODE (declarator) == INDIRECT_REF)
4242 /* Merge any constancy or volatility into the target type
4243 for the pointer. */
4245 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4246 && type_quals)
4247 pedwarn ("ISO C forbids qualified function types");
4248 if (type_quals)
4249 type = c_build_qualified_type (type, type_quals);
4250 type_quals = TYPE_UNQUALIFIED;
4251 size_varies = 0;
4253 type = build_pointer_type (type);
4255 /* Process a list of type modifier keywords
4256 (such as const or volatile) that were given inside the `*'. */
4258 if (TREE_TYPE (declarator))
4260 tree typemodlist;
4261 int erred = 0;
4263 constp = 0;
4264 volatilep = 0;
4265 restrictp = 0;
4266 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4267 typemodlist = TREE_CHAIN (typemodlist))
4269 tree qualifier = TREE_VALUE (typemodlist);
4271 if (C_IS_RESERVED_WORD (qualifier))
4273 if (C_RID_CODE (qualifier) == RID_CONST)
4274 constp++;
4275 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4276 volatilep++;
4277 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4278 restrictp++;
4279 else
4280 erred++;
4282 else
4283 erred++;
4286 if (erred)
4287 error ("invalid type modifier within pointer declarator");
4288 if (constp > 1 && ! flag_isoc99)
4289 pedwarn ("duplicate `const'");
4290 if (volatilep > 1 && ! flag_isoc99)
4291 pedwarn ("duplicate `volatile'");
4292 if (restrictp > 1 && ! flag_isoc99)
4293 pedwarn ("duplicate `restrict'");
4295 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4296 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4297 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4300 declarator = TREE_OPERAND (declarator, 0);
4302 else
4303 abort ();
4307 /* Now TYPE has the actual type. */
4309 /* Did array size calculations overflow? */
4311 if (TREE_CODE (type) == ARRAY_TYPE
4312 && COMPLETE_TYPE_P (type)
4313 && TREE_OVERFLOW (TYPE_SIZE (type)))
4315 error ("size of array `%s' is too large", name);
4316 /* If we proceed with the array type as it is, we'll eventually
4317 crash in tree_low_cst(). */
4318 type = error_mark_node;
4321 /* If this is declaring a typedef name, return a TYPE_DECL. */
4323 if (specbits & (1 << (int) RID_TYPEDEF))
4325 tree decl;
4326 /* Note that the grammar rejects storage classes
4327 in typenames, fields or parameters */
4328 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4329 && type_quals)
4330 pedwarn ("ISO C forbids qualified function types");
4331 if (type_quals)
4332 type = c_build_qualified_type (type, type_quals);
4333 decl = build_decl (TYPE_DECL, declarator, type);
4334 if ((specbits & (1 << (int) RID_SIGNED))
4335 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4336 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4337 decl_attributes (&decl, returned_attrs, 0);
4338 return decl;
4341 /* Detect the case of an array type of unspecified size
4342 which came, as such, direct from a typedef name.
4343 We must copy the type, so that each identifier gets
4344 a distinct type, so that each identifier's size can be
4345 controlled separately by its own initializer. */
4347 if (type != 0 && typedef_type != 0
4348 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4349 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4351 type = build_array_type (TREE_TYPE (type), 0);
4352 if (size_varies)
4353 C_TYPE_VARIABLE_SIZE (type) = 1;
4356 /* If this is a type name (such as, in a cast or sizeof),
4357 compute the type and return it now. */
4359 if (decl_context == TYPENAME)
4361 /* Note that the grammar rejects storage classes
4362 in typenames, fields or parameters */
4363 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4364 && type_quals)
4365 pedwarn ("ISO C forbids const or volatile function types");
4366 if (type_quals)
4367 type = c_build_qualified_type (type, type_quals);
4368 decl_attributes (&type, returned_attrs, 0);
4369 return type;
4372 /* Aside from typedefs and type names (handle above),
4373 `void' at top level (not within pointer)
4374 is allowed only in public variables.
4375 We don't complain about parms either, but that is because
4376 a better error message can be made later. */
4378 if (VOID_TYPE_P (type) && decl_context != PARM
4379 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4380 && ((specbits & (1 << (int) RID_EXTERN))
4381 || (current_binding_level == global_binding_level
4382 && !(specbits
4383 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4385 error ("variable or field `%s' declared void", name);
4386 type = integer_type_node;
4389 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4390 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4393 tree decl;
4395 if (decl_context == PARM)
4397 tree type_as_written;
4398 tree promoted_type;
4400 /* A parameter declared as an array of T is really a pointer to T.
4401 One declared as a function is really a pointer to a function. */
4403 if (TREE_CODE (type) == ARRAY_TYPE)
4405 /* Transfer const-ness of array into that of type pointed to. */
4406 type = TREE_TYPE (type);
4407 if (type_quals)
4408 type = c_build_qualified_type (type, type_quals);
4409 type = build_pointer_type (type);
4410 type_quals = TYPE_UNQUALIFIED;
4411 if (array_ptr_quals)
4413 tree new_ptr_quals, new_ptr_attrs;
4414 int erred = 0;
4415 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4416 /* We don't yet implement attributes in this context. */
4417 if (new_ptr_attrs != NULL_TREE)
4418 warning ("attributes in parameter array declarator ignored");
4420 constp = 0;
4421 volatilep = 0;
4422 restrictp = 0;
4423 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4425 tree qualifier = TREE_VALUE (new_ptr_quals);
4427 if (C_IS_RESERVED_WORD (qualifier))
4429 if (C_RID_CODE (qualifier) == RID_CONST)
4430 constp++;
4431 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4432 volatilep++;
4433 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4434 restrictp++;
4435 else
4436 erred++;
4438 else
4439 erred++;
4442 if (erred)
4443 error ("invalid type modifier within array declarator");
4445 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4446 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4447 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4449 size_varies = 0;
4451 else if (TREE_CODE (type) == FUNCTION_TYPE)
4453 if (pedantic && type_quals)
4454 pedwarn ("ISO C forbids qualified function types");
4455 if (type_quals)
4456 type = c_build_qualified_type (type, type_quals);
4457 type = build_pointer_type (type);
4458 type_quals = TYPE_UNQUALIFIED;
4460 else if (type_quals)
4461 type = c_build_qualified_type (type, type_quals);
4463 type_as_written = type;
4465 decl = build_decl (PARM_DECL, declarator, type);
4466 if (size_varies)
4467 C_DECL_VARIABLE_SIZE (decl) = 1;
4469 /* Compute the type actually passed in the parmlist,
4470 for the case where there is no prototype.
4471 (For example, shorts and chars are passed as ints.)
4472 When there is a prototype, this is overridden later. */
4474 if (type == error_mark_node)
4475 promoted_type = type;
4476 else
4477 promoted_type = c_type_promotes_to (type);
4479 DECL_ARG_TYPE (decl) = promoted_type;
4480 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4482 else if (decl_context == FIELD)
4484 /* Structure field. It may not be a function. */
4486 if (TREE_CODE (type) == FUNCTION_TYPE)
4488 error ("field `%s' declared as a function", name);
4489 type = build_pointer_type (type);
4491 else if (TREE_CODE (type) != ERROR_MARK
4492 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4494 error ("field `%s' has incomplete type", name);
4495 type = error_mark_node;
4497 /* Move type qualifiers down to element of an array. */
4498 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4500 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4501 type_quals),
4502 TYPE_DOMAIN (type));
4503 #if 0
4504 /* Leave the field const or volatile as well. */
4505 type_quals = TYPE_UNQUALIFIED;
4506 #endif
4508 decl = build_decl (FIELD_DECL, declarator, type);
4509 DECL_NONADDRESSABLE_P (decl) = bitfield;
4511 if (size_varies)
4512 C_DECL_VARIABLE_SIZE (decl) = 1;
4514 else if (TREE_CODE (type) == FUNCTION_TYPE)
4516 /* Every function declaration is "external"
4517 except for those which are inside a function body
4518 in which `auto' is used.
4519 That is a case not specified by ANSI C,
4520 and we use it for forward declarations for nested functions. */
4521 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4522 || current_binding_level == global_binding_level);
4524 if (specbits & (1 << (int) RID_AUTO)
4525 && (pedantic || current_binding_level == global_binding_level))
4526 pedwarn ("invalid storage class for function `%s'", name);
4527 if (specbits & (1 << (int) RID_REGISTER))
4528 error ("invalid storage class for function `%s'", name);
4529 if (specbits & (1 << (int) RID_THREAD))
4530 error ("invalid storage class for function `%s'", name);
4531 /* Function declaration not at top level.
4532 Storage classes other than `extern' are not allowed
4533 and `extern' makes no difference. */
4534 if (current_binding_level != global_binding_level
4535 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4536 && pedantic)
4537 pedwarn ("invalid storage class for function `%s'", name);
4539 decl = build_decl (FUNCTION_DECL, declarator, type);
4540 decl = build_decl_attribute_variant (decl, decl_attr);
4542 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4543 ggc_alloc_cleared (sizeof (struct lang_decl));
4545 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4546 pedwarn ("ISO C forbids qualified function types");
4548 /* GNU C interprets a `volatile void' return type to indicate
4549 that the function does not return. */
4550 if ((type_quals & TYPE_QUAL_VOLATILE)
4551 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4552 warning ("`noreturn' function returns non-void value");
4554 if (extern_ref)
4555 DECL_EXTERNAL (decl) = 1;
4556 /* Record absence of global scope for `static' or `auto'. */
4557 TREE_PUBLIC (decl)
4558 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4560 if (defaulted_int)
4561 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4563 /* Record presence of `inline', if it is reasonable. */
4564 if (MAIN_NAME_P (declarator))
4566 if (inlinep)
4567 warning ("cannot inline function `main'");
4569 else if (inlinep)
4571 /* Assume that otherwise the function can be inlined. */
4572 DECL_DECLARED_INLINE_P (decl) = 1;
4574 /* Do not mark bare declarations as DECL_INLINE. Doing so
4575 in the presence of multiple declarations can result in
4576 the abstract origin pointing between the declarations,
4577 which will confuse dwarf2out. */
4578 if (initialized)
4580 DECL_INLINE (decl) = 1;
4581 if (specbits & (1 << (int) RID_EXTERN))
4582 current_extern_inline = 1;
4585 /* If -finline-functions, assume it can be inlined. This does
4586 two things: let the function be deferred until it is actually
4587 needed, and let dwarf2 know that the function is inlinable. */
4588 else if (flag_inline_trees == 2 && initialized)
4590 if (!DECL_INLINE (decl))
4591 DID_INLINE_FUNC (decl) = 1;
4592 DECL_INLINE (decl) = 1;
4593 DECL_DECLARED_INLINE_P (decl) = 0;
4596 else
4598 /* It's a variable. */
4599 /* An uninitialized decl with `extern' is a reference. */
4600 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4602 /* Move type qualifiers down to element of an array. */
4603 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4605 int saved_align = TYPE_ALIGN(type);
4606 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4607 type_quals),
4608 TYPE_DOMAIN (type));
4609 TYPE_ALIGN (type) = saved_align;
4610 #if 0 /* Leave the variable const or volatile as well. */
4611 type_quals = TYPE_UNQUALIFIED;
4612 #endif
4614 else if (type_quals)
4615 type = c_build_qualified_type (type, type_quals);
4617 decl = build_decl (VAR_DECL, declarator, type);
4618 if (size_varies)
4619 C_DECL_VARIABLE_SIZE (decl) = 1;
4621 if (inlinep)
4622 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4624 DECL_EXTERNAL (decl) = extern_ref;
4626 /* At top level, the presence of a `static' or `register' storage
4627 class specifier, or the absence of all storage class specifiers
4628 makes this declaration a definition (perhaps tentative). Also,
4629 the absence of both `static' and `register' makes it public. */
4630 if (current_binding_level == global_binding_level)
4632 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4633 | (1 << (int) RID_REGISTER)));
4634 TREE_STATIC (decl) = !extern_ref;
4636 /* Not at top level, only `static' makes a static definition. */
4637 else
4639 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4640 TREE_PUBLIC (decl) = extern_ref;
4643 if (specbits & 1 << (int) RID_THREAD)
4645 if (targetm.have_tls)
4646 DECL_THREAD_LOCAL (decl) = 1;
4647 else
4648 /* A mere warning is sure to result in improper semantics
4649 at runtime. Don't bother to allow this to compile. */
4650 error ("thread-local storage not supported for this target");
4654 /* Record `register' declaration for warnings on &
4655 and in case doing stupid register allocation. */
4657 if (specbits & (1 << (int) RID_REGISTER))
4658 DECL_REGISTER (decl) = 1;
4660 /* Record constancy and volatility. */
4661 c_apply_type_quals_to_decl (type_quals, decl);
4663 /* If a type has volatile components, it should be stored in memory.
4664 Otherwise, the fact that those components are volatile
4665 will be ignored, and would even crash the compiler. */
4666 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4667 c_mark_addressable (decl);
4669 decl_attributes (&decl, returned_attrs, 0);
4671 return decl;
4675 /* Decode the parameter-list info for a function type or function definition.
4676 The argument is the value returned by `get_parm_info' (or made in parse.y
4677 if there is an identifier list instead of a parameter decl list).
4678 These two functions are separate because when a function returns
4679 or receives functions then each is called multiple times but the order
4680 of calls is different. The last call to `grokparms' is always the one
4681 that contains the formal parameter names of a function definition.
4683 Store in `last_function_parms' a chain of the decls of parms.
4684 Also store in `last_function_parm_tags' a chain of the struct, union,
4685 and enum tags declared among the parms.
4687 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4689 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4690 a mere declaration. A nonempty identifier-list gets an error message
4691 when FUNCDEF_FLAG is zero. */
4693 static tree
4694 grokparms (parms_info, funcdef_flag)
4695 tree parms_info;
4696 int funcdef_flag;
4698 tree first_parm = TREE_CHAIN (parms_info);
4700 last_function_parms = TREE_PURPOSE (parms_info);
4701 last_function_parm_tags = TREE_VALUE (parms_info);
4703 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4704 && !in_system_header)
4705 warning ("function declaration isn't a prototype");
4707 if (first_parm != 0
4708 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4710 if (! funcdef_flag)
4711 pedwarn ("parameter names (without types) in function declaration");
4713 last_function_parms = first_parm;
4714 return 0;
4716 else
4718 tree parm;
4719 tree typelt;
4720 /* We no longer test FUNCDEF_FLAG.
4721 If the arg types are incomplete in a declaration,
4722 they must include undefined tags.
4723 These tags can never be defined in the scope of the declaration,
4724 so the types can never be completed,
4725 and no call can be compiled successfully. */
4726 #if 0
4727 /* In a fcn definition, arg types must be complete. */
4728 if (funcdef_flag)
4729 #endif
4730 for (parm = last_function_parms, typelt = first_parm;
4731 parm;
4732 parm = TREE_CHAIN (parm))
4733 /* Skip over any enumeration constants declared here. */
4734 if (TREE_CODE (parm) == PARM_DECL)
4736 /* Barf if the parameter itself has an incomplete type. */
4737 tree type = TREE_VALUE (typelt);
4738 if (type == error_mark_node)
4739 continue;
4740 if (!COMPLETE_TYPE_P (type))
4742 if (funcdef_flag && DECL_NAME (parm) != 0)
4743 error ("parameter `%s' has incomplete type",
4744 IDENTIFIER_POINTER (DECL_NAME (parm)));
4745 else
4746 warning ("parameter has incomplete type");
4747 if (funcdef_flag)
4749 TREE_VALUE (typelt) = error_mark_node;
4750 TREE_TYPE (parm) = error_mark_node;
4753 #if 0
4754 /* This has been replaced by parm_tags_warning, which
4755 uses a more accurate criterion for what to warn
4756 about. */
4757 else
4759 /* Now warn if is a pointer to an incomplete type. */
4760 while (TREE_CODE (type) == POINTER_TYPE
4761 || TREE_CODE (type) == REFERENCE_TYPE)
4762 type = TREE_TYPE (type);
4763 type = TYPE_MAIN_VARIANT (type);
4764 if (!COMPLETE_TYPE_P (type))
4766 if (DECL_NAME (parm) != 0)
4767 warning ("parameter `%s' points to incomplete type",
4768 IDENTIFIER_POINTER (DECL_NAME (parm)));
4769 else
4770 warning ("parameter points to incomplete type");
4773 #endif
4774 typelt = TREE_CHAIN (typelt);
4777 return first_parm;
4781 /* Return a tree_list node with info on a parameter list just parsed.
4782 The TREE_PURPOSE is a chain of decls of those parms.
4783 The TREE_VALUE is a list of structure, union and enum tags defined.
4784 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4785 This tree_list node is later fed to `grokparms'.
4787 VOID_AT_END nonzero means append `void' to the end of the type-list.
4788 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4790 tree
4791 get_parm_info (void_at_end)
4792 int void_at_end;
4794 tree decl, t;
4795 tree types = 0;
4796 int erred = 0;
4797 tree tags = gettags ();
4798 tree parms = getdecls ();
4799 tree new_parms = 0;
4800 tree order = current_binding_level->parm_order;
4802 /* Just `void' (and no ellipsis) is special. There are really no parms.
4803 But if the `void' is qualified (by `const' or `volatile') or has a
4804 storage class specifier (`register'), then the behavior is undefined;
4805 by not counting it as the special case of `void' we will cause an
4806 error later. Typedefs for `void' are OK (see DR#157). */
4807 if (void_at_end && parms != 0
4808 && TREE_CHAIN (parms) == 0
4809 && VOID_TYPE_P (TREE_TYPE (parms))
4810 && ! TREE_THIS_VOLATILE (parms)
4811 && ! TREE_READONLY (parms)
4812 && ! DECL_REGISTER (parms)
4813 && DECL_NAME (parms) == 0)
4815 parms = NULL_TREE;
4816 storedecls (NULL_TREE);
4817 return tree_cons (NULL_TREE, NULL_TREE,
4818 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4821 /* Extract enumerator values and other non-parms declared with the parms.
4822 Likewise any forward parm decls that didn't have real parm decls. */
4823 for (decl = parms; decl;)
4825 tree next = TREE_CHAIN (decl);
4827 if (TREE_CODE (decl) != PARM_DECL)
4829 TREE_CHAIN (decl) = new_parms;
4830 new_parms = decl;
4832 else if (TREE_ASM_WRITTEN (decl))
4834 error_with_decl (decl,
4835 "parameter `%s' has just a forward declaration");
4836 TREE_CHAIN (decl) = new_parms;
4837 new_parms = decl;
4839 decl = next;
4842 /* Put the parm decls back in the order they were in in the parm list. */
4843 for (t = order; t; t = TREE_CHAIN (t))
4845 if (TREE_CHAIN (t))
4846 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4847 else
4848 TREE_CHAIN (TREE_VALUE (t)) = 0;
4851 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4852 new_parms);
4854 /* Store the parmlist in the binding level since the old one
4855 is no longer a valid list. (We have changed the chain pointers.) */
4856 storedecls (new_parms);
4858 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4859 /* There may also be declarations for enumerators if an enumeration
4860 type is declared among the parms. Ignore them here. */
4861 if (TREE_CODE (decl) == PARM_DECL)
4863 /* Since there is a prototype,
4864 args are passed in their declared types. */
4865 tree type = TREE_TYPE (decl);
4866 DECL_ARG_TYPE (decl) = type;
4867 if (PROMOTE_PROTOTYPES
4868 && INTEGRAL_TYPE_P (type)
4869 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4870 DECL_ARG_TYPE (decl) = integer_type_node;
4872 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4873 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4874 && DECL_NAME (decl) == 0)
4876 error ("`void' in parameter list must be the entire list");
4877 erred = 1;
4881 if (void_at_end)
4882 return tree_cons (new_parms, tags,
4883 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4885 return tree_cons (new_parms, tags, nreverse (types));
4888 /* At end of parameter list, warn about any struct, union or enum tags
4889 defined within. Do so because these types cannot ever become complete. */
4891 void
4892 parmlist_tags_warning ()
4894 tree elt;
4895 static int already;
4897 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4899 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4900 /* An anonymous union parm type is meaningful as a GNU extension.
4901 So don't warn for that. */
4902 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4903 continue;
4904 if (TREE_PURPOSE (elt) != 0)
4906 if (code == RECORD_TYPE)
4907 warning ("`struct %s' declared inside parameter list",
4908 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4909 else if (code == UNION_TYPE)
4910 warning ("`union %s' declared inside parameter list",
4911 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4912 else
4913 warning ("`enum %s' declared inside parameter list",
4914 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4916 else
4918 /* For translation these need to be separate warnings */
4919 if (code == RECORD_TYPE)
4920 warning ("anonymous struct declared inside parameter list");
4921 else if (code == UNION_TYPE)
4922 warning ("anonymous union declared inside parameter list");
4923 else
4924 warning ("anonymous enum declared inside parameter list");
4926 if (! already)
4928 warning ("its scope is only this definition or declaration, which is probably not what you want");
4929 already = 1;
4934 /* Get the struct, enum or union (CODE says which) with tag NAME.
4935 Define the tag as a forward-reference if it is not defined. */
4937 tree
4938 xref_tag (code, name)
4939 enum tree_code code;
4940 tree name;
4942 /* If a cross reference is requested, look up the type
4943 already defined for this tag and return it. */
4945 tree ref = lookup_tag (code, name, current_binding_level, 0);
4946 /* If this is the right type of tag, return what we found.
4947 (This reference will be shadowed by shadow_tag later if appropriate.)
4948 If this is the wrong type of tag, do not return it. If it was the
4949 wrong type in the same binding level, we will have had an error
4950 message already; if in a different binding level and declaring
4951 a name, pending_xref_error will give an error message; but if in a
4952 different binding level and not declaring a name, this tag should
4953 shadow the previous declaration of a different type of tag, and
4954 this would not work properly if we return the reference found.
4955 (For example, with "struct foo" in an outer scope, "union foo;"
4956 must shadow that tag with a new one of union type.) */
4957 if (ref && TREE_CODE (ref) == code)
4958 return ref;
4960 /* If no such tag is yet defined, create a forward-reference node
4961 and record it as the "definition".
4962 When a real declaration of this type is found,
4963 the forward-reference will be altered into a real type. */
4965 ref = make_node (code);
4966 if (code == ENUMERAL_TYPE)
4968 /* Give the type a default layout like unsigned int
4969 to avoid crashing if it does not get defined. */
4970 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4971 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4972 TYPE_USER_ALIGN (ref) = 0;
4973 TREE_UNSIGNED (ref) = 1;
4974 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4975 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4976 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4979 pushtag (name, ref);
4981 return ref;
4984 /* Make sure that the tag NAME is defined *in the current binding level*
4985 at least as a forward reference.
4986 CODE says which kind of tag NAME ought to be. */
4988 tree
4989 start_struct (code, name)
4990 enum tree_code code;
4991 tree name;
4993 /* If there is already a tag defined at this binding level
4994 (as a forward reference), just return it. */
4996 tree ref = 0;
4998 if (name != 0)
4999 ref = lookup_tag (code, name, current_binding_level, 1);
5000 if (ref && TREE_CODE (ref) == code)
5002 if (TYPE_FIELDS (ref))
5004 if (code == UNION_TYPE)
5005 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5006 else
5007 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5010 else
5012 /* Otherwise create a forward-reference just so the tag is in scope. */
5014 ref = make_node (code);
5015 pushtag (name, ref);
5018 C_TYPE_BEING_DEFINED (ref) = 1;
5019 TYPE_PACKED (ref) = flag_pack_struct;
5020 return ref;
5023 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5024 of a structure component, returning a FIELD_DECL node.
5025 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
5027 This is done during the parsing of the struct declaration.
5028 The FIELD_DECL nodes are chained together and the lot of them
5029 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5031 tree
5032 grokfield (filename, line, declarator, declspecs, width)
5033 const char *filename ATTRIBUTE_UNUSED;
5034 int line ATTRIBUTE_UNUSED;
5035 tree declarator, declspecs, width;
5037 tree value;
5039 if (declarator == NULL_TREE && width == NULL_TREE)
5041 /* This is an unnamed decl.
5043 If we have something of the form "union { list } ;" then this
5044 is the anonymous union extension. Similarly for struct.
5046 If this is something of the form "struct foo;", then
5047 If MS extensions are enabled, this is handled as an
5048 anonymous struct.
5049 Otherwise this is a forward declaration of a structure tag.
5051 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5052 If MS extensions are enabled and foo names a structure, then
5053 again this is an anonymous struct.
5054 Otherwise this is an error.
5056 Oh what a horrid tangled web we weave. I wonder if MS consciously
5057 took this from Plan 9 or if it was an accident of implementation
5058 that took root before someone noticed the bug... */
5060 tree type = TREE_VALUE (declspecs);
5062 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5063 type = TREE_TYPE (type);
5064 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5066 if (flag_ms_extensions)
5067 ; /* ok */
5068 else if (flag_iso)
5069 goto warn_unnamed_field;
5070 else if (TYPE_NAME (type) == NULL)
5071 ; /* ok */
5072 else
5073 goto warn_unnamed_field;
5075 else
5077 warn_unnamed_field:
5078 warning ("declaration does not declare anything");
5079 return NULL_TREE;
5083 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
5085 finish_decl (value, NULL_TREE, NULL_TREE);
5086 DECL_INITIAL (value) = width;
5088 if (flag_objc)
5089 objc_check_decl (value);
5090 return value;
5093 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5094 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5095 ATTRIBUTES are attributes to be applied to the structure. */
5097 tree
5098 finish_struct (t, fieldlist, attributes)
5099 tree t;
5100 tree fieldlist;
5101 tree attributes;
5103 tree x;
5104 int toplevel = global_binding_level == current_binding_level;
5105 int saw_named_field;
5107 /* If this type was previously laid out as a forward reference,
5108 make sure we lay it out again. */
5110 TYPE_SIZE (t) = 0;
5112 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5114 /* Nameless union parm types are useful as GCC extension. */
5115 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
5116 /* Otherwise, warn about any struct or union def. in parmlist. */
5117 if (in_parm_level_p ())
5119 if (pedantic)
5120 pedwarn ("%s defined inside parms",
5121 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5122 else
5123 warning ("%s defined inside parms",
5124 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5127 if (pedantic)
5129 for (x = fieldlist; x; x = TREE_CHAIN (x))
5130 if (DECL_NAME (x) != 0)
5131 break;
5133 if (x == 0)
5134 pedwarn ("%s has no %s",
5135 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5136 fieldlist ? _("named members") : _("members"));
5139 /* Install struct as DECL_CONTEXT of each field decl.
5140 Also process specified field sizes,m which is found in the DECL_INITIAL.
5141 Store 0 there, except for ": 0" fields (so we can find them
5142 and delete them, below). */
5144 saw_named_field = 0;
5145 for (x = fieldlist; x; x = TREE_CHAIN (x))
5147 DECL_CONTEXT (x) = t;
5148 DECL_PACKED (x) |= TYPE_PACKED (t);
5150 /* If any field is const, the structure type is pseudo-const. */
5151 if (TREE_READONLY (x))
5152 C_TYPE_FIELDS_READONLY (t) = 1;
5153 else
5155 /* A field that is pseudo-const makes the structure likewise. */
5156 tree t1 = TREE_TYPE (x);
5157 while (TREE_CODE (t1) == ARRAY_TYPE)
5158 t1 = TREE_TYPE (t1);
5159 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5160 && C_TYPE_FIELDS_READONLY (t1))
5161 C_TYPE_FIELDS_READONLY (t) = 1;
5164 /* Any field that is volatile means variables of this type must be
5165 treated in some ways as volatile. */
5166 if (TREE_THIS_VOLATILE (x))
5167 C_TYPE_FIELDS_VOLATILE (t) = 1;
5169 /* Any field of nominal variable size implies structure is too. */
5170 if (C_DECL_VARIABLE_SIZE (x))
5171 C_TYPE_VARIABLE_SIZE (t) = 1;
5173 /* Detect invalid nested redefinition. */
5174 if (TREE_TYPE (x) == t)
5175 error ("nested redefinition of `%s'",
5176 IDENTIFIER_POINTER (TYPE_NAME (t)));
5178 /* Detect invalid bit-field size. */
5179 if (DECL_INITIAL (x))
5180 STRIP_NOPS (DECL_INITIAL (x));
5181 if (DECL_INITIAL (x))
5183 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5184 constant_expression_warning (DECL_INITIAL (x));
5185 else
5187 error_with_decl (x,
5188 "bit-field `%s' width not an integer constant");
5189 DECL_INITIAL (x) = NULL;
5193 /* Detect invalid bit-field type. */
5194 if (DECL_INITIAL (x)
5195 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5196 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5197 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5199 error_with_decl (x, "bit-field `%s' has invalid type");
5200 DECL_INITIAL (x) = NULL;
5203 if (DECL_INITIAL (x) && pedantic
5204 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5205 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5206 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5207 /* Accept an enum that's equivalent to int or unsigned int. */
5208 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5209 && (TYPE_PRECISION (TREE_TYPE (x))
5210 == TYPE_PRECISION (integer_type_node))))
5211 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5213 /* Detect and ignore out of range field width and process valid
5214 field widths. */
5215 if (DECL_INITIAL (x))
5217 int max_width
5218 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5219 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5221 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5222 error_with_decl (x, "negative width in bit-field `%s'");
5223 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5224 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5225 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5226 error_with_decl (x, "zero width for bit-field `%s'");
5227 else
5229 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5230 unsigned HOST_WIDE_INT width
5231 = tree_low_cst (DECL_INITIAL (x), 1);
5233 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5234 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5235 TREE_UNSIGNED (TREE_TYPE (x)))
5236 || (width
5237 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5238 TREE_UNSIGNED (TREE_TYPE (x))))))
5239 warning_with_decl (x,
5240 "`%s' is narrower than values of its type");
5242 DECL_SIZE (x) = bitsize_int (width);
5243 DECL_BIT_FIELD (x) = 1;
5244 SET_DECL_C_BIT_FIELD (x);
5246 if (width == 0
5247 && ! (* targetm.ms_bitfield_layout_p) (t))
5249 /* field size 0 => force desired amount of alignment. */
5250 #ifdef EMPTY_FIELD_BOUNDARY
5251 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
5252 #endif
5253 #ifdef PCC_BITFIELD_TYPE_MATTERS
5254 if (PCC_BITFIELD_TYPE_MATTERS)
5256 DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
5257 TYPE_ALIGN (TREE_TYPE (x)));
5258 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5260 #endif
5265 else if (TREE_TYPE (x) != error_mark_node)
5267 unsigned int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
5268 : TYPE_ALIGN (TREE_TYPE (x)));
5270 /* Non-bit-fields are aligned for their type, except packed
5271 fields which require only BITS_PER_UNIT alignment. */
5272 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
5273 if (! DECL_PACKED (x))
5274 DECL_USER_ALIGN (x) |= TYPE_USER_ALIGN (TREE_TYPE (x));
5277 DECL_INITIAL (x) = 0;
5279 /* Detect flexible array member in an invalid context. */
5280 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5281 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5282 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5283 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5285 if (TREE_CODE (t) == UNION_TYPE)
5286 error_with_decl (x, "flexible array member in union");
5287 else if (TREE_CHAIN (x) != NULL_TREE)
5288 error_with_decl (x, "flexible array member not at end of struct");
5289 else if (! saw_named_field)
5290 error_with_decl (x, "flexible array member in otherwise empty struct");
5293 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5294 && flexible_array_type_p (TREE_TYPE (x)))
5295 pedwarn_with_decl (x, "invalid use of structure with flexible array member");
5297 if (DECL_NAME (x))
5298 saw_named_field = 1;
5301 /* Delete all duplicate fields from the fieldlist */
5302 for (x = fieldlist; x && TREE_CHAIN (x);)
5303 /* Anonymous fields aren't duplicates. */
5304 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5305 x = TREE_CHAIN (x);
5306 else
5308 tree y = fieldlist;
5310 while (1)
5312 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5313 break;
5314 if (y == x)
5315 break;
5316 y = TREE_CHAIN (y);
5318 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5320 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5321 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5323 else
5324 x = TREE_CHAIN (x);
5327 /* Now we have the nearly final fieldlist. Record it,
5328 then lay out the structure or union (including the fields). */
5330 TYPE_FIELDS (t) = fieldlist;
5332 layout_type (t);
5334 /* Delete all zero-width bit-fields from the fieldlist */
5336 tree *fieldlistp = &fieldlist;
5337 while (*fieldlistp)
5338 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5339 *fieldlistp = TREE_CHAIN (*fieldlistp);
5340 else
5341 fieldlistp = &TREE_CHAIN (*fieldlistp);
5344 /* Now we have the truly final field list.
5345 Store it in this type and in the variants. */
5347 TYPE_FIELDS (t) = fieldlist;
5349 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5351 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5352 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5353 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5354 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5357 /* If this was supposed to be a transparent union, but we can't
5358 make it one, warn and turn off the flag. */
5359 if (TREE_CODE (t) == UNION_TYPE
5360 && TYPE_TRANSPARENT_UNION (t)
5361 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5363 TYPE_TRANSPARENT_UNION (t) = 0;
5364 warning ("union cannot be made transparent");
5367 /* If this structure or union completes the type of any previous
5368 variable declaration, lay it out and output its rtl. */
5370 if (current_binding_level->incomplete_list != NULL_TREE)
5372 tree prev = NULL_TREE;
5374 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5376 tree decl = TREE_VALUE (x);
5378 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5379 && TREE_CODE (decl) != TYPE_DECL)
5381 layout_decl (decl, 0);
5382 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5383 if (flag_objc)
5384 objc_check_decl (decl);
5385 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5386 if (! toplevel)
5387 expand_decl (decl);
5388 /* Unlink X from the incomplete list. */
5389 if (prev)
5390 TREE_CHAIN (prev) = TREE_CHAIN (x);
5391 else
5392 current_binding_level->incomplete_list = TREE_CHAIN (x);
5394 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5395 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5397 tree element = TREE_TYPE (decl);
5398 while (TREE_CODE (element) == ARRAY_TYPE)
5399 element = TREE_TYPE (element);
5400 if (element == t)
5402 layout_array_type (TREE_TYPE (decl));
5403 if (TREE_CODE (decl) != TYPE_DECL)
5405 layout_decl (decl, 0);
5406 if (flag_objc)
5407 objc_check_decl (decl);
5408 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5409 if (! toplevel)
5410 expand_decl (decl);
5412 /* Unlink X from the incomplete list. */
5413 if (prev)
5414 TREE_CHAIN (prev) = TREE_CHAIN (x);
5415 else
5416 current_binding_level->incomplete_list = TREE_CHAIN (x);
5422 /* Finish debugging output for this type. */
5423 rest_of_type_compilation (t, toplevel);
5425 return t;
5428 /* Lay out the type T, and its element type, and so on. */
5430 static void
5431 layout_array_type (t)
5432 tree t;
5434 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5435 layout_array_type (TREE_TYPE (t));
5436 layout_type (t);
5439 /* Begin compiling the definition of an enumeration type.
5440 NAME is its name (or null if anonymous).
5441 Returns the type object, as yet incomplete.
5442 Also records info about it so that build_enumerator
5443 may be used to declare the individual values as they are read. */
5445 tree
5446 start_enum (name)
5447 tree name;
5449 tree enumtype = 0;
5451 /* If this is the real definition for a previous forward reference,
5452 fill in the contents in the same object that used to be the
5453 forward reference. */
5455 if (name != 0)
5456 enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
5458 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5460 enumtype = make_node (ENUMERAL_TYPE);
5461 pushtag (name, enumtype);
5464 C_TYPE_BEING_DEFINED (enumtype) = 1;
5466 if (TYPE_VALUES (enumtype) != 0)
5468 /* This enum is a named one that has been declared already. */
5469 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5471 /* Completely replace its old definition.
5472 The old enumerators remain defined, however. */
5473 TYPE_VALUES (enumtype) = 0;
5476 enum_next_value = integer_zero_node;
5477 enum_overflow = 0;
5479 if (flag_short_enums)
5480 TYPE_PACKED (enumtype) = 1;
5482 return enumtype;
5485 /* After processing and defining all the values of an enumeration type,
5486 install their decls in the enumeration type and finish it off.
5487 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5488 and ATTRIBUTES are the specified attributes.
5489 Returns ENUMTYPE. */
5491 tree
5492 finish_enum (enumtype, values, attributes)
5493 tree enumtype;
5494 tree values;
5495 tree attributes;
5497 tree pair, tem;
5498 tree minnode = 0, maxnode = 0, enum_value_type;
5499 int precision, unsign;
5500 int toplevel = (global_binding_level == current_binding_level);
5502 if (in_parm_level_p ())
5503 warning ("enum defined inside parms");
5505 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5507 /* Calculate the maximum value of any enumerator in this type. */
5509 if (values == error_mark_node)
5510 minnode = maxnode = integer_zero_node;
5511 else
5513 minnode = maxnode = TREE_VALUE (values);
5514 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5516 tree value = TREE_VALUE (pair);
5517 if (tree_int_cst_lt (maxnode, value))
5518 maxnode = value;
5519 if (tree_int_cst_lt (value, minnode))
5520 minnode = value;
5524 /* Construct the final type of this enumeration. It is the same
5525 as one of the integral types - the narrowest one that fits, except
5526 that normally we only go as narrow as int - and signed iff any of
5527 the values are negative. */
5528 unsign = (tree_int_cst_sgn (minnode) >= 0);
5529 precision = MAX (min_precision (minnode, unsign),
5530 min_precision (maxnode, unsign));
5531 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5533 tree narrowest = c_common_type_for_size (precision, unsign);
5534 if (narrowest == 0)
5536 warning ("enumeration values exceed range of largest integer");
5537 narrowest = long_long_integer_type_node;
5540 precision = TYPE_PRECISION (narrowest);
5542 else
5543 precision = TYPE_PRECISION (integer_type_node);
5545 if (precision == TYPE_PRECISION (integer_type_node))
5546 enum_value_type = c_common_type_for_size (precision, 0);
5547 else
5548 enum_value_type = enumtype;
5550 TYPE_MIN_VALUE (enumtype) = minnode;
5551 TYPE_MAX_VALUE (enumtype) = maxnode;
5552 TYPE_PRECISION (enumtype) = precision;
5553 TREE_UNSIGNED (enumtype) = unsign;
5554 TYPE_SIZE (enumtype) = 0;
5555 layout_type (enumtype);
5557 if (values != error_mark_node)
5559 /* Change the type of the enumerators to be the enum type. We
5560 need to do this irrespective of the size of the enum, for
5561 proper type checking. Replace the DECL_INITIALs of the
5562 enumerators, and the value slots of the list, with copies
5563 that have the enum type; they cannot be modified in place
5564 because they may be shared (e.g. integer_zero_node) Finally,
5565 change the purpose slots to point to the names of the decls. */
5566 for (pair = values; pair; pair = TREE_CHAIN (pair))
5568 tree enu = TREE_PURPOSE (pair);
5570 TREE_TYPE (enu) = enumtype;
5571 DECL_SIZE (enu) = TYPE_SIZE (enumtype);
5572 DECL_SIZE_UNIT (enu) = TYPE_SIZE_UNIT (enumtype);
5573 DECL_ALIGN (enu) = TYPE_ALIGN (enumtype);
5574 DECL_USER_ALIGN (enu) = TYPE_USER_ALIGN (enumtype);
5575 DECL_MODE (enu) = TYPE_MODE (enumtype);
5577 /* The ISO C Standard mandates enumerators to have type int,
5578 even though the underlying type of an enum type is
5579 unspecified. Here we convert any enumerators that fit in
5580 an int to type int, to avoid promotions to unsigned types
5581 when comparing integers with enumerators that fit in the
5582 int range. When -pedantic is given, build_enumerator()
5583 would have already taken care of those that don't fit. */
5584 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5585 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5586 else
5587 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5589 TREE_PURPOSE (pair) = DECL_NAME (enu);
5590 TREE_VALUE (pair) = DECL_INITIAL (enu);
5593 TYPE_VALUES (enumtype) = values;
5596 /* Fix up all variant types of this enum type. */
5597 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5599 if (tem == enumtype)
5600 continue;
5601 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5602 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5603 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5604 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5605 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5606 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5607 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5608 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5609 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5610 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5613 /* Finish debugging output for this type. */
5614 rest_of_type_compilation (enumtype, toplevel);
5616 return enumtype;
5619 /* Build and install a CONST_DECL for one value of the
5620 current enumeration type (one that was begun with start_enum).
5621 Return a tree-list containing the CONST_DECL and its value.
5622 Assignment of sequential values by default is handled here. */
5624 tree
5625 build_enumerator (name, value)
5626 tree name, value;
5628 tree decl, type;
5630 /* Validate and default VALUE. */
5632 /* Remove no-op casts from the value. */
5633 if (value)
5634 STRIP_TYPE_NOPS (value);
5636 if (value != 0)
5638 if (TREE_CODE (value) == INTEGER_CST)
5640 value = default_conversion (value);
5641 constant_expression_warning (value);
5643 else
5645 error ("enumerator value for `%s' not integer constant",
5646 IDENTIFIER_POINTER (name));
5647 value = 0;
5651 /* Default based on previous value. */
5652 /* It should no longer be possible to have NON_LVALUE_EXPR
5653 in the default. */
5654 if (value == 0)
5656 value = enum_next_value;
5657 if (enum_overflow)
5658 error ("overflow in enumeration values");
5661 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5663 pedwarn ("ISO C restricts enumerator values to range of `int'");
5664 value = convert (integer_type_node, value);
5667 /* Set basis for default for next value. */
5668 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5669 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5671 /* Now create a declaration for the enum value name. */
5673 type = TREE_TYPE (value);
5674 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5675 TYPE_PRECISION (integer_type_node)),
5676 (TYPE_PRECISION (type)
5677 >= TYPE_PRECISION (integer_type_node)
5678 && TREE_UNSIGNED (type)));
5680 decl = build_decl (CONST_DECL, name, type);
5681 DECL_INITIAL (decl) = convert (type, value);
5682 pushdecl (decl);
5684 return tree_cons (decl, value, NULL_TREE);
5688 /* Create the FUNCTION_DECL for a function definition.
5689 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5690 the declaration; they describe the function's name and the type it returns,
5691 but twisted together in a fashion that parallels the syntax of C.
5693 This function creates a binding context for the function body
5694 as well as setting up the FUNCTION_DECL in current_function_decl.
5696 Returns 1 on success. If the DECLARATOR is not suitable for a function
5697 (it defines a datum instead), we return 0, which tells
5698 yyparse to report a parse error. */
5701 start_function (declspecs, declarator, attributes)
5702 tree declarator, declspecs, attributes;
5704 tree decl1, old_decl;
5705 tree restype;
5706 int old_immediate_size_expand = immediate_size_expand;
5708 current_function_returns_value = 0; /* Assume, until we see it does. */
5709 current_function_returns_null = 0;
5710 current_function_returns_abnormally = 0;
5711 warn_about_return_type = 0;
5712 current_extern_inline = 0;
5713 named_labels = 0;
5714 shadowed_labels = 0;
5716 /* Don't expand any sizes in the return type of the function. */
5717 immediate_size_expand = 0;
5719 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5721 /* If the declarator is not suitable for a function definition,
5722 cause a syntax error. */
5723 if (decl1 == 0)
5725 immediate_size_expand = old_immediate_size_expand;
5726 return 0;
5729 decl_attributes (&decl1, attributes, 0);
5731 /* If #pragma weak was used, mark the decl weak now. */
5732 if (current_binding_level == global_binding_level)
5733 maybe_apply_pragma_weak (decl1);
5735 if (DECL_DECLARED_INLINE_P (decl1)
5736 && DECL_UNINLINABLE (decl1)
5737 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5738 warning_with_decl (decl1,
5739 "inline function `%s' given attribute noinline");
5741 announce_function (decl1);
5743 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5745 error ("return type is an incomplete type");
5746 /* Make it return void instead. */
5747 TREE_TYPE (decl1)
5748 = build_function_type (void_type_node,
5749 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5752 if (warn_about_return_type)
5753 pedwarn_c99 ("return type defaults to `int'");
5755 /* Save the parm names or decls from this function's declarator
5756 where store_parm_decls will find them. */
5757 current_function_parms = last_function_parms;
5758 current_function_parm_tags = last_function_parm_tags;
5760 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5761 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5762 DECL_INITIAL (decl1) = error_mark_node;
5764 /* If this definition isn't a prototype and we had a prototype declaration
5765 before, copy the arg type info from that prototype.
5766 But not if what we had before was a builtin function. */
5767 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5768 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5769 && !DECL_BUILT_IN (old_decl)
5770 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5771 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5772 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5774 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5775 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5776 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5779 /* If there is no explicit declaration, look for any out-of-scope implicit
5780 declarations. */
5781 if (old_decl == 0)
5782 old_decl = IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1));
5784 /* Optionally warn of old-fashioned def with no previous prototype. */
5785 if (warn_strict_prototypes
5786 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5787 && !(old_decl != 0
5788 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
5789 || (DECL_BUILT_IN (old_decl)
5790 && ! C_DECL_ANTICIPATED (old_decl)))))
5791 warning ("function declaration isn't a prototype");
5792 /* Optionally warn of any global def with no previous prototype. */
5793 else if (warn_missing_prototypes
5794 && TREE_PUBLIC (decl1)
5795 && !(old_decl != 0
5796 && (TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0
5797 || (DECL_BUILT_IN (old_decl)
5798 && ! C_DECL_ANTICIPATED (old_decl))))
5799 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5800 warning_with_decl (decl1, "no previous prototype for `%s'");
5801 /* Optionally warn of any def with no previous prototype
5802 if the function has already been used. */
5803 else if (warn_missing_prototypes
5804 && old_decl != 0 && TREE_USED (old_decl)
5805 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5806 warning_with_decl (decl1,
5807 "`%s' was used with no prototype before its definition");
5808 /* Optionally warn of any global def with no previous declaration. */
5809 else if (warn_missing_declarations
5810 && TREE_PUBLIC (decl1)
5811 && old_decl == 0
5812 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5813 warning_with_decl (decl1, "no previous declaration for `%s'");
5814 /* Optionally warn of any def with no previous declaration
5815 if the function has already been used. */
5816 else if (warn_missing_declarations
5817 && old_decl != 0 && TREE_USED (old_decl)
5818 && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
5819 warning_with_decl (decl1,
5820 "`%s' was used with no declaration before its definition");
5822 /* This is a definition, not a reference.
5823 So normally clear DECL_EXTERNAL.
5824 However, `extern inline' acts like a declaration
5825 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5826 DECL_EXTERNAL (decl1) = current_extern_inline;
5828 /* This function exists in static storage.
5829 (This does not mean `static' in the C sense!) */
5830 TREE_STATIC (decl1) = 1;
5832 /* A nested function is not global. */
5833 if (current_function_decl != 0)
5834 TREE_PUBLIC (decl1) = 0;
5836 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5837 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5839 tree args;
5840 int argct = 0;
5842 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5843 != integer_type_node)
5844 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5846 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5847 args = TREE_CHAIN (args))
5849 tree type = args ? TREE_VALUE (args) : 0;
5851 if (type == void_type_node)
5852 break;
5854 ++argct;
5855 switch (argct)
5857 case 1:
5858 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5859 pedwarn_with_decl (decl1,
5860 "first argument of `%s' should be `int'");
5861 break;
5863 case 2:
5864 if (TREE_CODE (type) != POINTER_TYPE
5865 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5866 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5867 != char_type_node))
5868 pedwarn_with_decl (decl1,
5869 "second argument of `%s' should be `char **'");
5870 break;
5872 case 3:
5873 if (TREE_CODE (type) != POINTER_TYPE
5874 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5875 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5876 != char_type_node))
5877 pedwarn_with_decl (decl1,
5878 "third argument of `%s' should probably be `char **'");
5879 break;
5883 /* It is intentional that this message does not mention the third
5884 argument because it's only mentioned in an appendix of the
5885 standard. */
5886 if (argct > 0 && (argct < 2 || argct > 3))
5887 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5889 if (! TREE_PUBLIC (decl1))
5890 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5893 /* Record the decl so that the function name is defined.
5894 If we already have a decl for this name, and it is a FUNCTION_DECL,
5895 use the old decl. */
5897 current_function_decl = pushdecl (decl1);
5899 pushlevel (0);
5900 declare_parm_level (1);
5901 current_binding_level->subblocks_tag_transparent = 1;
5903 make_decl_rtl (current_function_decl, NULL);
5905 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5906 /* Promote the value to int before returning it. */
5907 if (c_promoting_integer_type_p (restype))
5909 /* It retains unsignedness if not really getting wider. */
5910 if (TREE_UNSIGNED (restype)
5911 && (TYPE_PRECISION (restype)
5912 == TYPE_PRECISION (integer_type_node)))
5913 restype = unsigned_type_node;
5914 else
5915 restype = integer_type_node;
5917 DECL_RESULT (current_function_decl)
5918 = build_decl (RESULT_DECL, NULL_TREE, restype);
5920 /* If this fcn was already referenced via a block-scope `extern' decl
5921 (or an implicit decl), propagate certain information about the usage. */
5922 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5923 TREE_ADDRESSABLE (current_function_decl) = 1;
5925 immediate_size_expand = old_immediate_size_expand;
5927 start_fname_decls ();
5929 return 1;
5932 /* Store the parameter declarations into the current function declaration.
5933 This is called after parsing the parameter declarations, before
5934 digesting the body of the function.
5936 For an old-style definition, modify the function's type
5937 to specify at least the number of arguments. */
5939 void
5940 store_parm_decls ()
5942 tree fndecl = current_function_decl;
5943 tree parm;
5945 /* This is either a chain of PARM_DECLs (if a prototype was used)
5946 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5947 tree specparms = current_function_parms;
5949 /* This is a list of types declared among parms in a prototype. */
5950 tree parmtags = current_function_parm_tags;
5952 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5953 tree parmdecls = getdecls ();
5955 /* This is a chain of any other decls that came in among the parm
5956 declarations. If a parm is declared with enum {foo, bar} x;
5957 then CONST_DECLs for foo and bar are put here. */
5958 tree nonparms = 0;
5960 /* The function containing FNDECL, if any. */
5961 tree context = decl_function_context (fndecl);
5963 /* Nonzero if this definition is written with a prototype. */
5964 int prototype = 0;
5966 int saved_warn_shadow = warn_shadow;
5968 /* Don't re-emit shadow warnings. */
5969 warn_shadow = 0;
5971 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5973 /* This case is when the function was defined with an ANSI prototype.
5974 The parms already have decls, so we need not do anything here
5975 except record them as in effect
5976 and complain if any redundant old-style parm decls were written. */
5978 tree next;
5979 tree others = 0;
5981 prototype = 1;
5983 if (parmdecls != 0)
5985 tree decl, link;
5987 error_with_decl (fndecl,
5988 "parm types given both in parmlist and separately");
5989 /* Get rid of the erroneous decls; don't keep them on
5990 the list of parms, since they might not be PARM_DECLs. */
5991 for (decl = current_binding_level->names;
5992 decl; decl = TREE_CHAIN (decl))
5993 if (DECL_NAME (decl))
5994 IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) = 0;
5995 for (link = current_binding_level->shadowed;
5996 link; link = TREE_CHAIN (link))
5997 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5998 current_binding_level->names = 0;
5999 current_binding_level->shadowed = 0;
6002 specparms = nreverse (specparms);
6003 for (parm = specparms; parm; parm = next)
6005 next = TREE_CHAIN (parm);
6006 if (TREE_CODE (parm) == PARM_DECL)
6008 if (DECL_NAME (parm) == 0)
6009 error_with_decl (parm, "parameter name omitted");
6010 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
6011 && VOID_TYPE_P (TREE_TYPE (parm)))
6013 error_with_decl (parm, "parameter `%s' declared void");
6014 /* Change the type to error_mark_node so this parameter
6015 will be ignored by assign_parms. */
6016 TREE_TYPE (parm) = error_mark_node;
6018 pushdecl (parm);
6020 else
6022 /* If we find an enum constant or a type tag,
6023 put it aside for the moment. */
6024 TREE_CHAIN (parm) = 0;
6025 others = chainon (others, parm);
6029 /* Get the decls in their original chain order
6030 and record in the function. */
6031 DECL_ARGUMENTS (fndecl) = getdecls ();
6033 #if 0
6034 /* If this function takes a variable number of arguments,
6035 add a phony parameter to the end of the parm list,
6036 to represent the position of the first unnamed argument. */
6037 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
6038 != void_type_node)
6040 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
6041 /* Let's hope the address of the unnamed parm
6042 won't depend on its type. */
6043 TREE_TYPE (dummy) = integer_type_node;
6044 DECL_ARG_TYPE (dummy) = integer_type_node;
6045 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
6047 #endif
6049 /* Now pushdecl the enum constants. */
6050 for (parm = others; parm; parm = next)
6052 next = TREE_CHAIN (parm);
6053 if (DECL_NAME (parm) == 0)
6055 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
6057 else if (TREE_CODE (parm) != PARM_DECL)
6058 pushdecl (parm);
6061 storetags (chainon (parmtags, gettags ()));
6063 else
6065 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
6066 each with a parm name as the TREE_VALUE.
6068 PARMDECLS is a chain of declarations for parameters.
6069 Warning! It can also contain CONST_DECLs which are not parameters
6070 but are names of enumerators of any enum types
6071 declared among the parameters.
6073 First match each formal parameter name with its declaration.
6074 Associate decls with the names and store the decls
6075 into the TREE_PURPOSE slots. */
6077 /* We use DECL_WEAK as a flag to show which parameters have been
6078 seen already since it is not used on PARM_DECL or CONST_DECL. */
6079 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
6080 DECL_WEAK (parm) = 0;
6082 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
6084 tree tail, found = NULL;
6086 if (TREE_VALUE (parm) == 0)
6088 error_with_decl (fndecl,
6089 "parameter name missing from parameter list");
6090 TREE_PURPOSE (parm) = 0;
6091 continue;
6094 /* See if any of the parmdecls specifies this parm by name.
6095 Ignore any enumerator decls. */
6096 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
6097 if (DECL_NAME (tail) == TREE_VALUE (parm)
6098 && TREE_CODE (tail) == PARM_DECL)
6100 found = tail;
6101 break;
6104 /* If declaration already marked, we have a duplicate name.
6105 Complain, and don't use this decl twice. */
6106 if (found && DECL_WEAK (found))
6108 error_with_decl (found, "multiple parameters named `%s'");
6109 found = 0;
6112 /* If the declaration says "void", complain and ignore it. */
6113 if (found && VOID_TYPE_P (TREE_TYPE (found)))
6115 error_with_decl (found, "parameter `%s' declared void");
6116 TREE_TYPE (found) = integer_type_node;
6117 DECL_ARG_TYPE (found) = integer_type_node;
6118 layout_decl (found, 0);
6121 /* If no declaration found, default to int. */
6122 if (!found)
6124 found = build_decl (PARM_DECL, TREE_VALUE (parm),
6125 integer_type_node);
6126 DECL_ARG_TYPE (found) = TREE_TYPE (found);
6127 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
6128 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
6129 if (flag_isoc99)
6130 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
6131 else if (extra_warnings)
6132 warning_with_decl (found, "type of `%s' defaults to `int'");
6133 pushdecl (found);
6136 TREE_PURPOSE (parm) = found;
6138 /* Mark this decl as "already found". */
6139 DECL_WEAK (found) = 1;
6142 /* Put anything which is on the parmdecls chain and which is
6143 not a PARM_DECL onto the list NONPARMS. (The types of
6144 non-parm things which might appear on the list include
6145 enumerators and NULL-named TYPE_DECL nodes.) Complain about
6146 any actual PARM_DECLs not matched with any names. */
6148 nonparms = 0;
6149 for (parm = parmdecls; parm;)
6151 tree next = TREE_CHAIN (parm);
6152 TREE_CHAIN (parm) = 0;
6154 if (TREE_CODE (parm) != PARM_DECL)
6155 nonparms = chainon (nonparms, parm);
6156 else
6158 /* Complain about args with incomplete types. */
6159 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6161 error_with_decl (parm, "parameter `%s' has incomplete type");
6162 TREE_TYPE (parm) = error_mark_node;
6165 if (! DECL_WEAK (parm))
6167 error_with_decl (parm,
6168 "declaration for parameter `%s' but no such parameter");
6169 /* Pretend the parameter was not missing.
6170 This gets us to a standard state and minimizes
6171 further error messages. */
6172 specparms
6173 = chainon (specparms,
6174 tree_cons (parm, NULL_TREE, NULL_TREE));
6178 parm = next;
6181 /* Chain the declarations together in the order of the list of
6182 names. Store that chain in the function decl, replacing the
6183 list of names. */
6184 parm = specparms;
6185 DECL_ARGUMENTS (fndecl) = 0;
6187 tree last;
6188 for (last = 0; parm; parm = TREE_CHAIN (parm))
6189 if (TREE_PURPOSE (parm))
6191 if (last == 0)
6192 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6193 else
6194 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6195 last = TREE_PURPOSE (parm);
6196 TREE_CHAIN (last) = 0;
6200 /* If there was a previous prototype,
6201 set the DECL_ARG_TYPE of each argument according to
6202 the type previously specified, and report any mismatches. */
6204 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6206 tree type;
6207 for (parm = DECL_ARGUMENTS (fndecl),
6208 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6209 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6210 != void_type_node));
6211 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6213 if (parm == 0 || type == 0
6214 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6216 error ("number of arguments doesn't match prototype");
6217 error_with_file_and_line (current_function_prototype_file,
6218 current_function_prototype_line,
6219 "prototype declaration");
6220 break;
6222 /* Type for passing arg must be consistent with that
6223 declared for the arg. ISO C says we take the unqualified
6224 type for parameters declared with qualified type. */
6225 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6226 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6228 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6229 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6231 /* Adjust argument to match prototype. E.g. a previous
6232 `int foo(float);' prototype causes
6233 `int foo(x) float x; {...}' to be treated like
6234 `int foo(float x) {...}'. This is particularly
6235 useful for argument types like uid_t. */
6236 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6238 if (PROMOTE_PROTOTYPES
6239 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6240 && TYPE_PRECISION (TREE_TYPE (parm))
6241 < TYPE_PRECISION (integer_type_node))
6242 DECL_ARG_TYPE (parm) = integer_type_node;
6244 if (pedantic)
6246 pedwarn ("promoted argument `%s' doesn't match prototype",
6247 IDENTIFIER_POINTER (DECL_NAME (parm)));
6248 warning_with_file_and_line
6249 (current_function_prototype_file,
6250 current_function_prototype_line,
6251 "prototype declaration");
6254 else
6256 error ("argument `%s' doesn't match prototype",
6257 IDENTIFIER_POINTER (DECL_NAME (parm)));
6258 error_with_file_and_line (current_function_prototype_file,
6259 current_function_prototype_line,
6260 "prototype declaration");
6264 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6267 /* Otherwise, create a prototype that would match. */
6269 else
6271 tree actual = 0, last = 0, type;
6273 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6275 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6276 if (last)
6277 TREE_CHAIN (last) = type;
6278 else
6279 actual = type;
6280 last = type;
6282 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6283 if (last)
6284 TREE_CHAIN (last) = type;
6285 else
6286 actual = type;
6288 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6289 of the type of this function, but we need to avoid having this
6290 affect the types of other similarly-typed functions, so we must
6291 first force the generation of an identical (but separate) type
6292 node for the relevant function type. The new node we create
6293 will be a variant of the main variant of the original function
6294 type. */
6296 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6298 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6301 /* Now store the final chain of decls for the arguments
6302 as the decl-chain of the current lexical scope.
6303 Put the enumerators in as well, at the front so that
6304 DECL_ARGUMENTS is not modified. */
6306 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6309 /* Make sure the binding level for the top of the function body
6310 gets a BLOCK if there are any in the function.
6311 Otherwise, the dbx output is wrong. */
6313 keep_next_if_subblocks = 1;
6315 /* ??? This might be an improvement,
6316 but needs to be thought about some more. */
6317 #if 0
6318 keep_next_level_flag = 1;
6319 #endif
6321 /* Write a record describing this function definition to the prototypes
6322 file (if requested). */
6324 gen_aux_info_record (fndecl, 1, 0, prototype);
6326 /* Initialize the RTL code for the function. */
6327 init_function_start (fndecl, input_filename, lineno);
6329 /* Begin the statement tree for this function. */
6330 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6332 /* If this is a nested function, save away the sizes of any
6333 variable-size types so that we can expand them when generating
6334 RTL. */
6335 if (context)
6337 tree t;
6339 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6340 = nreverse (get_pending_sizes ());
6341 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6343 t = TREE_CHAIN (t))
6344 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6347 /* This function is being processed in whole-function mode. */
6348 cfun->x_whole_function_mode_p = 1;
6350 /* Even though we're inside a function body, we still don't want to
6351 call expand_expr to calculate the size of a variable-sized array.
6352 We haven't necessarily assigned RTL to all variables yet, so it's
6353 not safe to try to expand expressions involving them. */
6354 immediate_size_expand = 0;
6355 cfun->x_dont_save_pending_sizes_p = 1;
6357 warn_shadow = saved_warn_shadow;
6360 /* Finish up a function declaration and compile that function
6361 all the way to assembler language output. The free the storage
6362 for the function definition.
6364 This is called after parsing the body of the function definition.
6366 NESTED is nonzero if the function being finished is nested in another.
6367 CAN_DEFER_P is nonzero if the function may be deferred. */
6369 void
6370 finish_function (nested, can_defer_p)
6371 int nested;
6372 int can_defer_p;
6374 tree fndecl = current_function_decl;
6376 #if 0
6377 /* This caused &foo to be of type ptr-to-const-function which then
6378 got a warning when stored in a ptr-to-function variable. */
6379 TREE_READONLY (fndecl) = 1;
6380 #endif
6382 poplevel (1, 0, 1);
6383 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6385 /* Must mark the RESULT_DECL as being in this function. */
6387 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6389 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6391 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6392 != integer_type_node)
6394 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6395 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6396 if (! warn_main)
6397 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6399 else
6401 #ifdef DEFAULT_MAIN_RETURN
6402 /* Make it so that `main' always returns success by default. */
6403 DEFAULT_MAIN_RETURN;
6404 #else
6405 if (flag_isoc99)
6406 c_expand_return (integer_zero_node);
6407 #endif
6411 finish_fname_decls ();
6413 /* Tie off the statement tree for this function. */
6414 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6416 /* Complain if there's just no return statement. */
6417 if (warn_return_type
6418 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6419 && !current_function_returns_value && !current_function_returns_null
6420 /* Don't complain if we abort. */
6421 && !current_function_returns_abnormally
6422 /* Don't warn for main(). */
6423 && !MAIN_NAME_P (DECL_NAME (fndecl))
6424 /* Or if they didn't actually specify a return type. */
6425 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6426 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6427 inline function, as we might never be compiled separately. */
6428 && DECL_INLINE (fndecl))
6429 warning ("no return statement in function returning non-void");
6431 /* Clear out memory we no longer need. */
6432 free_after_parsing (cfun);
6433 /* Since we never call rest_of_compilation, we never clear
6434 CFUN. Do so explicitly. */
6435 free_after_compilation (cfun);
6436 cfun = NULL;
6438 if (flag_unit_at_a_time && can_defer_p)
6440 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6441 current_function_decl = NULL;
6442 return;
6445 if (! nested)
6447 /* Function is parsed.
6448 Generate RTL for the body of this function or defer
6449 it for later expansion. */
6450 int uninlinable = 1;
6452 /* There's no reason to do any of the work here if we're only doing
6453 semantic analysis; this code just generates RTL. */
6454 if (flag_syntax_only)
6456 current_function_decl = NULL;
6457 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6458 return;
6461 if (flag_inline_trees)
6463 /* First, cache whether the current function is inlinable. Some
6464 predicates depend on cfun and current_function_decl to
6465 function completely. */
6466 timevar_push (TV_INTEGRATION);
6467 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6469 if (! uninlinable && can_defer_p
6470 /* Save function tree for inlining. Should return 0 if the
6471 language does not support function deferring or the
6472 function could not be deferred. */
6473 && defer_fn (fndecl))
6475 /* Let the back-end know that this function exists. */
6476 (*debug_hooks->deferred_inline_function) (fndecl);
6477 timevar_pop (TV_INTEGRATION);
6478 current_function_decl = NULL;
6479 return;
6482 /* Then, inline any functions called in it. */
6483 optimize_inline_calls (fndecl);
6484 timevar_pop (TV_INTEGRATION);
6487 c_expand_body (fndecl);
6489 if (uninlinable)
6491 /* Allow the body of the function to be garbage collected. */
6492 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6495 /* Let the error reporting routines know that we're outside a
6496 function. For a nested function, this value is used in
6497 c_pop_function_context and then reset via pop_function_context. */
6498 current_function_decl = NULL;
6502 /* Generate the RTL for a deferred function FNDECL. */
6504 void
6505 c_expand_deferred_function (fndecl)
6506 tree fndecl;
6508 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6509 function was deferred, e.g. in duplicate_decls. */
6510 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6512 if (flag_inline_trees)
6514 timevar_push (TV_INTEGRATION);
6515 optimize_inline_calls (fndecl);
6516 timevar_pop (TV_INTEGRATION);
6518 c_expand_body (fndecl);
6519 current_function_decl = NULL;
6523 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6524 then we are already in the process of generating RTL for another
6525 function. If can_defer_p is zero, we won't attempt to defer the
6526 generation of RTL. */
6528 static void
6529 c_expand_body_1 (fndecl, nested_p)
6530 tree fndecl;
6531 int nested_p;
6533 timevar_push (TV_EXPAND);
6535 if (nested_p)
6537 /* Make sure that we will evaluate variable-sized types involved
6538 in our function's type. */
6539 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6540 /* Squirrel away our current state. */
6541 push_function_context ();
6544 /* Initialize the RTL code for the function. */
6545 current_function_decl = fndecl;
6546 input_filename = DECL_SOURCE_FILE (fndecl);
6547 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6548 lineno = DECL_SOURCE_LINE (fndecl);
6550 /* This function is being processed in whole-function mode. */
6551 cfun->x_whole_function_mode_p = 1;
6553 /* Even though we're inside a function body, we still don't want to
6554 call expand_expr to calculate the size of a variable-sized array.
6555 We haven't necessarily assigned RTL to all variables yet, so it's
6556 not safe to try to expand expressions involving them. */
6557 immediate_size_expand = 0;
6558 cfun->x_dont_save_pending_sizes_p = 1;
6560 /* Set up parameters and prepare for return, for the function. */
6561 expand_function_start (fndecl, 0);
6563 /* If this function is `main', emit a call to `__main'
6564 to run global initializers, etc. */
6565 if (DECL_NAME (fndecl)
6566 && MAIN_NAME_P (DECL_NAME (fndecl))
6567 && DECL_CONTEXT (fndecl) == NULL_TREE)
6568 expand_main_function ();
6570 /* Generate the RTL for this function. */
6571 expand_stmt (DECL_SAVED_TREE (fndecl));
6573 /* We hard-wired immediate_size_expand to zero above.
6574 expand_function_end will decrement this variable. So, we set the
6575 variable to one here, so that after the decrement it will remain
6576 zero. */
6577 immediate_size_expand = 1;
6579 /* Allow language dialects to perform special processing. */
6580 if (lang_expand_function_end)
6581 (*lang_expand_function_end) ();
6583 /* Generate rtl for function exit. */
6584 expand_function_end (input_filename, lineno, 0);
6586 /* If this is a nested function, protect the local variables in the stack
6587 above us from being collected while we're compiling this function. */
6588 if (nested_p)
6589 ggc_push_context ();
6591 /* Run the optimizers and output the assembler code for this function. */
6592 rest_of_compilation (fndecl);
6594 /* Undo the GC context switch. */
6595 if (nested_p)
6596 ggc_pop_context ();
6598 /* With just -Wextra, complain only if function returns both with
6599 and without a value. */
6600 if (extra_warnings
6601 && current_function_returns_value
6602 && current_function_returns_null)
6603 warning ("this function may return with or without a value");
6605 /* If requested, warn about function definitions where the function will
6606 return a value (usually of some struct or union type) which itself will
6607 take up a lot of stack space. */
6609 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6611 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6613 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6614 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6615 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6616 larger_than_size))
6618 unsigned int size_as_int
6619 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6621 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6622 warning_with_decl (fndecl,
6623 "size of return value of `%s' is %u bytes",
6624 size_as_int);
6625 else
6626 warning_with_decl (fndecl,
6627 "size of return value of `%s' is larger than %d bytes",
6628 larger_than_size);
6632 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6633 && ! flag_inline_trees)
6635 /* Stop pointing to the local nodes about to be freed.
6636 But DECL_INITIAL must remain nonzero so we know this
6637 was an actual function definition.
6638 For a nested function, this is done in c_pop_function_context.
6639 If rest_of_compilation set this to 0, leave it 0. */
6640 if (DECL_INITIAL (fndecl) != 0)
6641 DECL_INITIAL (fndecl) = error_mark_node;
6643 DECL_ARGUMENTS (fndecl) = 0;
6646 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6648 if (targetm.have_ctors_dtors)
6649 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6650 DEFAULT_INIT_PRIORITY);
6651 else
6652 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6655 if (DECL_STATIC_DESTRUCTOR (fndecl))
6657 if (targetm.have_ctors_dtors)
6658 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6659 DEFAULT_INIT_PRIORITY);
6660 else
6661 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6664 if (nested_p)
6665 /* Return to the enclosing function. */
6666 pop_function_context ();
6667 timevar_pop (TV_EXPAND);
6670 /* Like c_expand_body_1 but only for unnested functions. */
6672 void
6673 c_expand_body (fndecl)
6674 tree fndecl;
6676 c_expand_body_1 (fndecl, 0);
6679 /* Check the declarations given in a for-loop for satisfying the C99
6680 constraints. */
6681 void
6682 check_for_loop_decls ()
6684 tree t;
6686 if (!flag_isoc99)
6688 /* If we get here, declarations have been used in a for loop without
6689 the C99 for loop scope. This doesn't make much sense, so don't
6690 allow it. */
6691 error ("`for' loop initial declaration used outside C99 mode");
6692 return;
6694 /* C99 subclause 6.8.5 paragraph 3:
6696 [#3] The declaration part of a for statement shall only
6697 declare identifiers for objects having storage class auto or
6698 register.
6700 It isn't clear whether, in this sentence, "identifiers" binds to
6701 "shall only declare" or to "objects" - that is, whether all identifiers
6702 declared must be identifiers for objects, or whether the restriction
6703 only applies to those that are. (A question on this in comp.std.c
6704 in November 2000 received no answer.) We implement the strictest
6705 interpretation, to avoid creating an extension which later causes
6706 problems. */
6708 for (t = gettags (); t; t = TREE_CHAIN (t))
6710 if (TREE_PURPOSE (t) != 0)
6712 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6714 if (code == RECORD_TYPE)
6715 error ("`struct %s' declared in `for' loop initial declaration",
6716 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6717 else if (code == UNION_TYPE)
6718 error ("`union %s' declared in `for' loop initial declaration",
6719 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6720 else
6721 error ("`enum %s' declared in `for' loop initial declaration",
6722 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6726 for (t = getdecls (); t; t = TREE_CHAIN (t))
6728 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6729 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6730 else if (TREE_STATIC (t))
6731 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6732 else if (DECL_EXTERNAL (t))
6733 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6737 /* Save and restore the variables in this file and elsewhere
6738 that keep track of the progress of compilation of the current function.
6739 Used for nested functions. */
6741 struct language_function GTY(())
6743 struct c_language_function base;
6744 tree named_labels;
6745 tree shadowed_labels;
6746 int returns_value;
6747 int returns_null;
6748 int returns_abnormally;
6749 int warn_about_return_type;
6750 int extern_inline;
6751 struct binding_level *binding_level;
6754 /* Save and reinitialize the variables
6755 used during compilation of a C function. */
6757 void
6758 c_push_function_context (f)
6759 struct function *f;
6761 struct language_function *p;
6762 p = ((struct language_function *)
6763 ggc_alloc (sizeof (struct language_function)));
6764 f->language = p;
6766 p->base.x_stmt_tree = c_stmt_tree;
6767 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6768 p->named_labels = named_labels;
6769 p->shadowed_labels = shadowed_labels;
6770 p->returns_value = current_function_returns_value;
6771 p->returns_null = current_function_returns_null;
6772 p->returns_abnormally = current_function_returns_abnormally;
6773 p->warn_about_return_type = warn_about_return_type;
6774 p->extern_inline = current_extern_inline;
6775 p->binding_level = current_binding_level;
6778 /* Restore the variables used during compilation of a C function. */
6780 void
6781 c_pop_function_context (f)
6782 struct function *f;
6784 struct language_function *p = f->language;
6785 tree link;
6787 /* Bring back all the labels that were shadowed. */
6788 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6789 if (DECL_NAME (TREE_VALUE (link)) != 0)
6790 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6791 = TREE_VALUE (link);
6793 if (DECL_SAVED_INSNS (current_function_decl) == 0
6794 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6796 /* Stop pointing to the local nodes about to be freed. */
6797 /* But DECL_INITIAL must remain nonzero so we know this
6798 was an actual function definition. */
6799 DECL_INITIAL (current_function_decl) = error_mark_node;
6800 DECL_ARGUMENTS (current_function_decl) = 0;
6803 c_stmt_tree = p->base.x_stmt_tree;
6804 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6805 named_labels = p->named_labels;
6806 shadowed_labels = p->shadowed_labels;
6807 current_function_returns_value = p->returns_value;
6808 current_function_returns_null = p->returns_null;
6809 current_function_returns_abnormally = p->returns_abnormally;
6810 warn_about_return_type = p->warn_about_return_type;
6811 current_extern_inline = p->extern_inline;
6812 current_binding_level = p->binding_level;
6814 f->language = NULL;
6817 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6819 void
6820 c_dup_lang_specific_decl (decl)
6821 tree decl;
6823 struct lang_decl *ld;
6825 if (!DECL_LANG_SPECIFIC (decl))
6826 return;
6828 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6829 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6830 sizeof (struct lang_decl));
6831 DECL_LANG_SPECIFIC (decl) = ld;
6834 /* The functions below are required for functionality of doing
6835 function at once processing in the C front end. Currently these
6836 functions are not called from anywhere in the C front end, but as
6837 these changes continue, that will change. */
6839 /* Returns nonzero if the current statement is a full expression,
6840 i.e. temporaries created during that statement should be destroyed
6841 at the end of the statement. */
6844 stmts_are_full_exprs_p ()
6846 return 0;
6849 /* Returns the stmt_tree (if any) to which statements are currently
6850 being added. If there is no active statement-tree, NULL is
6851 returned. */
6853 stmt_tree
6854 current_stmt_tree ()
6856 return &c_stmt_tree;
6859 /* Returns the stack of SCOPE_STMTs for the current function. */
6861 tree *
6862 current_scope_stmt_stack ()
6864 return &c_scope_stmt_stack;
6867 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6868 C. */
6871 anon_aggr_type_p (node)
6872 tree node ATTRIBUTE_UNUSED;
6874 return 0;
6877 /* Dummy function in place of callback used by C++. */
6879 void
6880 extract_interface_info ()
6884 /* Return a new COMPOUND_STMT, after adding it to the current
6885 statement tree. */
6887 tree
6888 c_begin_compound_stmt ()
6890 tree stmt;
6892 /* Create the COMPOUND_STMT. */
6893 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6895 return stmt;
6898 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6899 common code. */
6901 void
6902 c_expand_decl_stmt (t)
6903 tree t;
6905 tree decl = DECL_STMT_DECL (t);
6907 /* Expand nested functions. */
6908 if (TREE_CODE (decl) == FUNCTION_DECL
6909 && DECL_CONTEXT (decl) == current_function_decl
6910 && DECL_SAVED_TREE (decl))
6911 c_expand_body_1 (decl, 1);
6914 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
6915 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
6917 tree
6918 identifier_global_value (t)
6919 tree t;
6921 return IDENTIFIER_GLOBAL_VALUE (t);
6924 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6925 otherwise the name is found in ridpointers from RID_INDEX. */
6927 void
6928 record_builtin_type (rid_index, name, type)
6929 enum rid rid_index;
6930 const char *name;
6931 tree type;
6933 tree id;
6934 if (name == 0)
6935 id = ridpointers[(int) rid_index];
6936 else
6937 id = get_identifier (name);
6938 pushdecl (build_decl (TYPE_DECL, id, type));
6941 /* Build the void_list_node (void_type_node having been created). */
6942 tree
6943 build_void_list_node ()
6945 tree t = build_tree_list (NULL_TREE, void_type_node);
6946 return t;
6949 /* Return something to represent absolute declarators containing a *.
6950 TARGET is the absolute declarator that the * contains.
6951 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6952 to apply to the pointer type, represented as identifiers, possible mixed
6953 with attributes.
6955 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6956 if attributes are present) and whose type is the modifier list. */
6958 tree
6959 make_pointer_declarator (type_quals_attrs, target)
6960 tree type_quals_attrs, target;
6962 tree quals, attrs;
6963 tree itarget = target;
6964 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6965 if (attrs != NULL_TREE)
6966 itarget = tree_cons (attrs, target, NULL_TREE);
6967 return build1 (INDIRECT_REF, quals, itarget);
6970 #include "gt-c-decl.h"