index.html (3.10): Add note about mips atomicity.h.
[official-gcc.git] / gcc / c-decl.c
blob11f7499c7748c076da9760386628257eaccb608e
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 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 location_t pending_invalid_xref_location;
70 /* While defining an enum type, this is 1 plus the last enumerator
71 constant value. Note that will do not have to save this or `enum_overflow'
72 around nested function definition since such a definition could only
73 occur in an enum value expression and we don't use these variables in
74 that case. */
76 static tree enum_next_value;
78 /* Nonzero means that there was overflow computing enum_next_value. */
80 static int enum_overflow;
82 /* Parsing a function declarator leaves a list of parameter names
83 or a chain or parameter decls here. */
85 static tree last_function_parms;
87 /* Parsing a function declarator leaves here a chain of structure
88 and enum types declared in the parmlist. */
90 static tree last_function_parm_tags;
92 /* After parsing the declarator that starts a function definition,
93 `start_function' puts here the list of parameter names or chain of decls.
94 `store_parm_decls' finds it here. */
96 static tree current_function_parms;
98 /* Similar, for last_function_parm_tags. */
99 static tree current_function_parm_tags;
101 /* Similar, for the file and line that the prototype came from if this is
102 an old-style definition. */
103 static const char *current_function_prototype_file;
104 static int current_function_prototype_line;
106 /* The current statement tree. */
108 static GTY(()) struct stmt_tree_s c_stmt_tree;
110 /* The current scope statement stack. */
112 static GTY(()) tree c_scope_stmt_stack;
114 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
115 that have names. Here so we can clear out their names' definitions
116 at the end of the function. */
118 static GTY(()) tree named_labels;
120 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
122 static GTY(()) tree shadowed_labels;
124 /* A list of external DECLs that appeared at block scope when there was
125 some other global meaning for that identifier. */
126 static GTY(()) tree truly_local_externals;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement that specifies a return value is seen. */
131 int current_function_returns_value;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a return statement with no argument is seen. */
136 int current_function_returns_null;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a call to a noreturn function is seen. */
141 int current_function_returns_abnormally;
143 /* Set to nonzero by `grokdeclarator' for a function
144 whose return type is defaulted, if warnings for this are desired. */
146 static int warn_about_return_type;
148 /* Nonzero when starting a function declared `extern inline'. */
150 static int current_extern_inline;
152 /* For each binding contour we allocate a binding_level structure
153 * which records the names defined in that contour.
154 * Contours include:
155 * 0) the global one
156 * 1) one for each function definition,
157 * where internal declarations of the parameters appear.
158 * 2) one for each compound statement,
159 * to record its declarations.
161 * The current meaning of a name can be found by searching the levels from
162 * the current one out to the global one.
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 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, a list of shadowed outer-level tag definitions
187 to be restored when this level is popped.
188 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
189 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
190 tree shadowed_tags;
192 /* For each level (except not the global one),
193 a chain of BLOCK nodes for all the levels
194 that were entered and exited one level down. */
195 tree blocks;
197 /* The binding level which this one is contained in (inherits from). */
198 struct binding_level *level_chain;
200 /* Nonzero if we are currently filling this level with parameter
201 declarations. */
202 char parm_flag;
204 /* Nonzero if this is the outermost block scope of a function body.
205 This scope contains both the parameters and the local variables
206 declared in the outermost block. */
207 char function_body;
209 /* Nonzero means make a BLOCK for this level regardless of all else. */
210 char keep;
212 /* Nonzero means make a BLOCK if this level has any subblocks. */
213 char keep_if_subblocks;
215 /* List of decls in `names' that have incomplete structure or
216 union types. */
217 tree incomplete_list;
219 /* A list of decls giving the (reversed) specified order of parms,
220 not including any forward-decls in the parmlist.
221 This is so we can put the parms in proper order for assign_parms. */
222 tree parm_order;
225 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
227 /* The binding level currently in effect. */
229 static GTY(()) struct binding_level *current_binding_level;
231 /* A chain of binding_level structures awaiting reuse. */
233 static GTY((deletable (""))) struct binding_level *free_binding_level;
235 /* The outermost binding level, for names of file scope.
236 This is created when the compiler is started and exists
237 through the entire run. */
239 static GTY(()) struct binding_level *global_binding_level;
241 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
243 static int keep_next_level_flag;
245 /* Nonzero means make a BLOCK for the next level pushed
246 if it has subblocks. */
248 static int keep_next_if_subblocks;
250 /* The chain of outer levels of label scopes.
251 This uses the same data structure used for binding levels,
252 but it works differently: each link in the chain records
253 saved values of named_labels and shadowed_labels for
254 a label binding level outside the current one. */
256 static GTY(()) struct binding_level *label_level_chain;
258 /* Functions called automatically at the beginning and end of execution. */
260 tree static_ctors, static_dtors;
262 /* Forward declarations. */
264 static struct binding_level *make_binding_level PARAMS ((void));
265 static void pop_binding_level PARAMS ((struct binding_level **));
266 static int duplicate_decls PARAMS ((tree, tree, int));
267 static int redeclaration_error_message PARAMS ((tree, tree));
268 static void implicit_decl_warning PARAMS ((tree));
269 static void storedecls PARAMS ((tree));
270 static void storetags PARAMS ((tree));
271 static tree lookup_tag PARAMS ((enum tree_code, tree, int));
272 static tree lookup_name_current_level PARAMS ((tree));
273 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
274 int));
275 static tree grokparms PARAMS ((tree, int));
276 static void layout_array_type PARAMS ((tree));
277 static tree c_make_fname_decl PARAMS ((tree, int));
278 static void c_expand_body_1 PARAMS ((tree, int));
279 static tree any_external_decl PARAMS ((tree));
280 static void record_external_decl PARAMS ((tree));
281 static void warn_if_shadowing PARAMS ((tree, tree));
282 static void clone_underlying_type PARAMS ((tree));
283 static bool flexible_array_type_p PARAMS ((tree));
285 /* States indicating how grokdeclarator() should handle declspecs marked
286 with __attribute__((deprecated)). An object declared as
287 __attribute__((deprecated)) suppresses warnings of uses of other
288 deprecated items. */
290 enum deprecated_states {
291 DEPRECATED_NORMAL,
292 DEPRECATED_SUPPRESS
295 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
297 void
298 c_print_identifier (file, node, indent)
299 FILE *file;
300 tree node;
301 int indent;
303 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
304 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
305 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
306 if (C_IS_RESERVED_WORD (node))
308 tree rid = ridpointers[C_RID_CODE (node)];
309 indent_to (file, indent + 4);
310 fprintf (file, "rid ");
311 fprintf (file, HOST_PTR_PRINTF, (void *)rid);
312 fprintf (file, " \"%s\"", IDENTIFIER_POINTER (rid));
316 /* Hook called at end of compilation to assume 1 elt
317 for a top-level tentative array defn that wasn't complete before. */
319 void
320 c_finish_incomplete_decl (decl)
321 tree decl;
323 if (TREE_CODE (decl) == VAR_DECL)
325 tree type = TREE_TYPE (decl);
326 if (type != error_mark_node
327 && TREE_CODE (type) == ARRAY_TYPE
328 && ! DECL_EXTERNAL (decl)
329 && TYPE_DOMAIN (type) == 0)
331 warning_with_decl (decl, "array `%s' assumed to have one element");
333 complete_array_type (type, NULL_TREE, 1);
335 layout_decl (decl, 0);
340 /* Reuse or create a struct for this binding level. */
342 static struct binding_level *
343 make_binding_level ()
345 struct binding_level *result;
346 if (free_binding_level)
348 result = free_binding_level;
349 free_binding_level = result->level_chain;
350 memset (result, 0, sizeof(struct binding_level));
352 else
353 result = (struct binding_level *)
354 ggc_alloc_cleared (sizeof (struct binding_level));
356 return result;
359 /* Remove a binding level from a list and add it to the level chain. */
361 static void
362 pop_binding_level (lp)
363 struct binding_level **lp;
365 struct binding_level *l = *lp;
366 *lp = l->level_chain;
368 memset (l, 0, sizeof (struct binding_level));
369 l->level_chain = free_binding_level;
370 free_binding_level = l;
373 /* Nonzero if we are currently in the global binding level. */
376 global_bindings_p ()
378 return current_binding_level == global_binding_level;
381 void
382 keep_next_level ()
384 keep_next_level_flag = 1;
387 /* Nonzero if the current level needs to have a BLOCK made. */
390 kept_level_p ()
392 return ((current_binding_level->keep_if_subblocks
393 && current_binding_level->blocks != 0)
394 || current_binding_level->keep
395 || current_binding_level->names != 0
396 || current_binding_level->tags != 0);
399 /* Identify this binding level as a level of parameters.
400 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
401 But it turns out there is no way to pass the right value for
402 DEFINITION_FLAG, so we ignore it. */
404 void
405 declare_parm_level (definition_flag)
406 int definition_flag ATTRIBUTE_UNUSED;
408 current_binding_level->parm_flag = 1;
411 /* Nonzero if currently making parm declarations. */
414 in_parm_level_p ()
416 return current_binding_level->parm_flag;
419 /* Enter a new binding level. */
421 void
422 pushlevel (dummy)
423 int dummy ATTRIBUTE_UNUSED;
425 /* If this is the top level of a function, make sure that
426 NAMED_LABELS is 0. */
428 if (current_binding_level == global_binding_level)
429 named_labels = 0;
431 if (keep_next_if_subblocks)
433 /* This is the transition from the parameters to the top level
434 of the function body. These are the same scope
435 (C99 6.2.1p4,6) so we do not push another binding level.
437 XXX Note kludge - keep_next_if_subblocks is set only by
438 store_parm_decls, which in turn is called when and only
439 when we are about to encounter the opening curly brace for
440 the function body. */
441 current_binding_level->parm_flag = 0;
442 current_binding_level->function_body = 1;
443 current_binding_level->keep |= keep_next_level_flag;
444 current_binding_level->keep_if_subblocks = 1;
446 keep_next_level_flag = 0;
447 keep_next_if_subblocks = 0;
449 else
451 struct binding_level *newlevel = make_binding_level ();
453 newlevel->keep = keep_next_level_flag;
454 newlevel->level_chain = current_binding_level;
455 current_binding_level = newlevel;
456 keep_next_level_flag = 0;
460 /* Exit a binding level.
461 Pop the level off, and restore the state of the identifier-decl mappings
462 that were in effect when this level was entered.
464 If KEEP is nonzero, this level had explicit declarations, so
465 and create a "block" (a BLOCK node) for the level
466 to record its declarations and subblocks for symbol table output.
468 If FUNCTIONBODY is nonzero, this level is the body of a function,
469 so create a block as if KEEP were set and also clear out all
470 label names.
472 If REVERSE is nonzero, reverse the order of decls before putting
473 them into the BLOCK. */
475 tree
476 poplevel (keep, reverse, functionbody)
477 int keep;
478 int reverse;
479 int functionbody;
481 tree link;
482 tree block;
483 tree decl;
484 tree decls = current_binding_level->names;
485 tree tags = current_binding_level->tags;
486 tree subblocks = current_binding_level->blocks;
488 functionbody |= current_binding_level->function_body;
489 keep |= (current_binding_level->keep || functionbody
490 || (current_binding_level->keep_if_subblocks && subblocks != 0));
492 /* We used to warn about unused variables in expand_end_bindings,
493 i.e. while generating RTL. But in function-at-a-time mode we may
494 choose to never expand a function at all (e.g. auto inlining), so
495 we do this explicitly now. */
496 warn_about_unused_variables (decls);
498 /* Clear out the name-meanings declared on this level.
499 Propagate TREE_ADDRESSABLE from nested functions to their
500 containing functions. */
501 for (link = decls; link; link = TREE_CHAIN (link))
503 if (DECL_NAME (link) != 0)
505 if (DECL_EXTERNAL (link))
506 /* External decls stay in the symbol-value slot but are
507 inaccessible. */
508 C_DECL_INVISIBLE (link) = 1;
509 else
510 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
513 if (TREE_CODE (link) == FUNCTION_DECL
514 && ! TREE_ASM_WRITTEN (link)
515 && DECL_INITIAL (link) != 0
516 && TREE_ADDRESSABLE (link)
517 && DECL_ABSTRACT_ORIGIN (link) != 0
518 && DECL_ABSTRACT_ORIGIN (link) != link)
519 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
522 /* Clear out the tag-meanings declared on this level. */
523 for (link = tags; link; link = TREE_CHAIN (link))
524 if (TREE_PURPOSE (link))
525 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
527 /* Restore all name-meanings of the outer levels
528 that were shadowed by this level. */
530 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
531 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
533 /* Restore all tag-meanings of the outer levels
534 that were shadowed by this level. */
536 for (link = current_binding_level->shadowed_tags; link;
537 link = TREE_CHAIN (link))
538 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
540 /* If this is the top level block of a function, remove all
541 PARM_DECLs from current_binding_level->names; they are already
542 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
543 not be put in BLOCK_VARS, and furthermore reversing them will
544 cause trouble later. They are all together at the end of the
545 list. */
546 if (functionbody && decls)
548 if (TREE_CODE (decls) == PARM_DECL)
549 decls = 0;
550 else
552 link = decls;
553 while (TREE_CHAIN (link)
554 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
555 link = TREE_CHAIN (link);
557 TREE_CHAIN (link) = 0;
561 /* Get the decls in the order they were written.
562 Usually current_binding_level->names is in reverse order.
563 But parameter decls were previously put in forward order. */
565 if (reverse)
566 decls = nreverse (decls);
568 /* If there were any declarations or structure tags in that level,
569 or if this level is a function body,
570 create a BLOCK to record them for the life of this function. */
572 block = 0;
573 if (keep)
575 block = make_node (BLOCK);
576 BLOCK_VARS (block) = decls;
577 BLOCK_SUBBLOCKS (block) = subblocks;
578 TREE_USED (block) = 1;
581 /* In each subblock, record that this is its superior. */
583 for (link = subblocks; link; link = TREE_CHAIN (link))
584 BLOCK_SUPERCONTEXT (link) = block;
586 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
587 binding contour so that they point to the appropriate construct, i.e.
588 either to the current FUNCTION_DECL node, or else to the BLOCK node
589 we just constructed.
591 Note that for tagged types whose scope is just the formal parameter
592 list for some function type specification, we can't properly set
593 their TYPE_CONTEXTs here, because we don't have a pointer to the
594 appropriate FUNCTION_TYPE node readily available to us. For those
595 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
596 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
597 node which will represent the "scope" for these "parameter list local"
598 tagged types. */
600 decl = functionbody ? current_function_decl : block;
601 if (decl)
602 for (link = tags; link; link = TREE_CHAIN (link))
603 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
605 /* If the level being exited is the top level of a function, check
606 over all the labels, and clear out the current (function local)
607 meanings of their names. Then add them to BLOCK_VARS. */
609 if (functionbody)
611 for (link = named_labels; link; link = TREE_CHAIN (link))
613 tree label = TREE_VALUE (link);
615 if (DECL_INITIAL (label) == 0)
617 error_with_decl (label, "label `%s' used but not defined");
618 /* Avoid crashing later. */
619 define_label (input_filename, input_line,
620 DECL_NAME (label));
622 else if (warn_unused_label && !TREE_USED (label))
623 warning_with_decl (label, "label `%s' defined but not used");
624 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
626 /* Put the labels into the "variables" of the
627 top-level block, so debugger can see them. */
628 TREE_CHAIN (label) = BLOCK_VARS (block);
629 BLOCK_VARS (block) = label;
633 /* Pop the current level, and free the structure for reuse. */
635 pop_binding_level (&current_binding_level);
637 /* Dispose of the block that we just made inside some higher level. */
638 if (functionbody)
639 DECL_INITIAL (current_function_decl) = block;
640 else if (block)
641 current_binding_level->blocks
642 = chainon (current_binding_level->blocks, block);
643 /* If we did not make a block for the level just exited,
644 any blocks made for inner levels
645 (since they cannot be recorded as subblocks in that level)
646 must be carried forward so they will later become subblocks
647 of something else. */
648 else if (subblocks)
649 current_binding_level->blocks
650 = chainon (current_binding_level->blocks, subblocks);
652 return block;
655 /* Insert BLOCK at the end of the list of subblocks of the
656 current binding level. This is used when a BIND_EXPR is expanded,
657 to handle the BLOCK node inside the BIND_EXPR. */
659 void
660 insert_block (block)
661 tree block;
663 TREE_USED (block) = 1;
664 current_binding_level->blocks
665 = chainon (current_binding_level->blocks, block);
668 /* Set the BLOCK node for the innermost scope (the one we are
669 currently in). The RTL expansion machinery requires us to provide
670 this hook, but it is not useful in function-at-a-time mode. */
672 void
673 set_block (block)
674 tree block ATTRIBUTE_UNUSED;
678 void
679 push_label_level ()
681 struct binding_level *newlevel;
683 newlevel = make_binding_level ();
685 /* Add this level to the front of the chain (stack) of label levels. */
687 newlevel->level_chain = label_level_chain;
688 label_level_chain = newlevel;
690 newlevel->names = named_labels;
691 newlevel->shadowed = shadowed_labels;
692 named_labels = 0;
693 shadowed_labels = 0;
696 void
697 pop_label_level ()
699 struct binding_level *level = label_level_chain;
700 tree link, prev;
702 /* Clear out the definitions of the declared labels in this level.
703 Leave in the list any ordinary, non-declared labels. */
704 for (link = named_labels, prev = 0; link;)
706 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
708 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
710 error_with_decl (TREE_VALUE (link),
711 "label `%s' used but not defined");
712 /* Avoid crashing later. */
713 define_label (input_filename, input_line,
714 DECL_NAME (TREE_VALUE (link)));
716 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
717 warning_with_decl (TREE_VALUE (link),
718 "label `%s' defined but not used");
719 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
721 /* Delete this element from the list. */
722 link = TREE_CHAIN (link);
723 if (prev)
724 TREE_CHAIN (prev) = link;
725 else
726 named_labels = link;
728 else
730 prev = link;
731 link = TREE_CHAIN (link);
735 /* Bring back all the labels that were shadowed. */
736 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
737 if (DECL_NAME (TREE_VALUE (link)) != 0)
738 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
739 = TREE_VALUE (link);
741 named_labels = chainon (named_labels, level->names);
742 shadowed_labels = level->shadowed;
744 /* Pop the current level, and free the structure for reuse. */
745 pop_binding_level (&label_level_chain);
748 /* Push a definition or a declaration of struct, union or enum tag "name".
749 "type" should be the type node.
750 We assume that the tag "name" is not already defined.
752 Note that the definition may really be just a forward reference.
753 In that case, the TYPE_SIZE will be zero. */
755 void
756 pushtag (name, type)
757 tree name, type;
759 struct binding_level *b = current_binding_level;
761 if (name)
763 /* Record the identifier as the type's name if it has none. */
765 if (TYPE_NAME (type) == 0)
766 TYPE_NAME (type) = name;
768 if (IDENTIFIER_TAG_VALUE (name))
769 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
770 b->shadowed_tags);
771 IDENTIFIER_TAG_VALUE (name) = type;
774 b->tags = tree_cons (name, type, b->tags);
776 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
777 tagged type we just added to the current binding level. This fake
778 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
779 to output a representation of a tagged type, and it also gives
780 us a convenient place to record the "scope start" address for the
781 tagged type. */
783 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
785 /* An approximation for now, so we can tell this is a function-scope tag.
786 This will be updated in poplevel. */
787 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
790 /* Handle when a new declaration NEWDECL
791 has the same name as an old one OLDDECL
792 in the same binding contour.
793 Prints an error message if appropriate.
795 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
796 Otherwise, return 0.
798 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
799 and OLDDECL is in an outer binding level and should thus not be changed. */
801 static int
802 duplicate_decls (newdecl, olddecl, different_binding_level)
803 tree newdecl, olddecl;
804 int different_binding_level;
806 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
807 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
808 && DECL_INITIAL (newdecl) != 0);
809 tree oldtype = TREE_TYPE (olddecl);
810 tree newtype = TREE_TYPE (newdecl);
811 int errmsg = 0;
813 if (DECL_P (olddecl))
815 if (TREE_CODE (newdecl) == FUNCTION_DECL
816 && TREE_CODE (olddecl) == FUNCTION_DECL
817 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
819 if (DECL_DECLARED_INLINE_P (newdecl)
820 && DECL_UNINLINABLE (newdecl)
821 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
822 /* Already warned elsewhere. */;
823 else if (DECL_DECLARED_INLINE_P (olddecl)
824 && DECL_UNINLINABLE (olddecl)
825 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
826 /* Already warned. */;
827 else if (DECL_DECLARED_INLINE_P (newdecl)
828 && ! DECL_DECLARED_INLINE_P (olddecl)
829 && DECL_UNINLINABLE (olddecl)
830 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
832 warning_with_decl (newdecl,
833 "function `%s' redeclared as inline");
834 warning_with_decl (olddecl,
835 "previous declaration of function `%s' with attribute noinline");
837 else if (DECL_DECLARED_INLINE_P (olddecl)
838 && DECL_UNINLINABLE (newdecl)
839 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
841 warning_with_decl (newdecl,
842 "function `%s' redeclared with attribute noinline");
843 warning_with_decl (olddecl,
844 "previous declaration of function `%s' was inline");
848 DECL_ATTRIBUTES (newdecl)
849 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
852 if (TREE_CODE (newtype) == ERROR_MARK
853 || TREE_CODE (oldtype) == ERROR_MARK)
854 types_match = 0;
856 /* New decl is completely inconsistent with the old one =>
857 tell caller to replace the old one.
858 This is always an error except in the case of shadowing a builtin. */
859 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
861 if (TREE_CODE (olddecl) == FUNCTION_DECL
862 && DECL_BUILT_IN (olddecl))
864 /* If you declare a built-in or predefined function name as static,
865 the old definition is overridden,
866 but optionally warn this was a bad choice of name. */
867 if (!TREE_PUBLIC (newdecl))
869 if (warn_shadow)
870 warning_with_decl (newdecl, "shadowing built-in function `%s'");
872 else
873 warning_with_decl (newdecl,
874 "built-in function `%s' declared as non-function");
876 else
878 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
879 error_with_decl (olddecl, "previous declaration of `%s'");
882 return 0;
885 /* For real parm decl following a forward decl, return 1 so old decl
886 will be reused. Only allow this to happen once. */
887 if (types_match && TREE_CODE (newdecl) == PARM_DECL
888 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
890 TREE_ASM_WRITTEN (olddecl) = 0;
891 return 1;
894 /* The new declaration is the same kind of object as the old one.
895 The declarations may partially match. Print warnings if they don't
896 match enough. Ultimately, copy most of the information from the new
897 decl to the old one, and keep using the old one. */
899 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
901 /* A function declaration for a built-in function. */
902 if (!TREE_PUBLIC (newdecl))
904 /* If you declare a built-in function name as static, the
905 built-in definition is overridden,
906 but optionally warn this was a bad choice of name. */
907 if (warn_shadow)
908 warning_with_decl (newdecl, "shadowing built-in function `%s'");
909 /* Discard the old built-in function. */
910 return 0;
912 else if (!types_match)
914 /* Accept the return type of the new declaration if same modes. */
915 tree oldreturntype = TREE_TYPE (oldtype);
916 tree newreturntype = TREE_TYPE (newtype);
918 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
920 /* Function types may be shared, so we can't just modify
921 the return type of olddecl's function type. */
922 tree trytype
923 = build_function_type (newreturntype,
924 TYPE_ARG_TYPES (oldtype));
925 trytype = build_type_attribute_variant (trytype,
926 TYPE_ATTRIBUTES (oldtype));
928 types_match = comptypes (newtype, trytype);
929 if (types_match)
930 oldtype = trytype;
932 /* Accept harmless mismatch in first argument type also.
933 This is for the ffs and fprintf builtins. */
934 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
935 && TYPE_ARG_TYPES (oldtype) != 0
936 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
937 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
938 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
939 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
941 /* Function types may be shared, so we can't just modify
942 the return type of olddecl's function type. */
943 tree trytype
944 = build_function_type (TREE_TYPE (oldtype),
945 tree_cons (NULL_TREE,
946 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
947 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
948 trytype = build_type_attribute_variant (trytype,
949 TYPE_ATTRIBUTES (oldtype));
951 types_match = comptypes (newtype, trytype);
952 if (types_match)
953 oldtype = trytype;
955 if (! different_binding_level)
956 TREE_TYPE (olddecl) = oldtype;
958 else if (TYPE_ARG_TYPES (oldtype) == NULL
959 && TYPE_ARG_TYPES (newtype) != NULL)
961 /* For bcmp, bzero, fputs the builtin type has arguments not
962 specified. Use the ones from the prototype so that type checking
963 is done for them. */
964 tree trytype
965 = build_function_type (TREE_TYPE (oldtype),
966 TYPE_ARG_TYPES (newtype));
967 trytype = build_type_attribute_variant (trytype,
968 TYPE_ATTRIBUTES (oldtype));
970 oldtype = trytype;
971 if (! different_binding_level)
972 TREE_TYPE (olddecl) = oldtype;
974 if (!types_match)
976 /* If types don't match for a built-in, throw away the built-in. */
977 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
978 return 0;
981 else if (TREE_CODE (olddecl) == FUNCTION_DECL
982 && DECL_SOURCE_LINE (olddecl) == 0)
984 /* A function declaration for a predeclared function
985 that isn't actually built in. */
986 if (!TREE_PUBLIC (newdecl))
988 /* If you declare it as static, the
989 default definition is overridden. */
990 return 0;
992 else if (!types_match)
994 /* If the types don't match, preserve volatility indication.
995 Later on, we will discard everything else about the
996 default declaration. */
997 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1000 /* Permit char *foo () to match void *foo (...) if not pedantic,
1001 if one of them came from a system header file. */
1002 else if (!types_match
1003 && TREE_CODE (olddecl) == FUNCTION_DECL
1004 && TREE_CODE (newdecl) == FUNCTION_DECL
1005 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1006 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1007 && (DECL_IN_SYSTEM_HEADER (olddecl)
1008 || DECL_IN_SYSTEM_HEADER (newdecl))
1009 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1010 && TYPE_ARG_TYPES (oldtype) == 0
1011 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1012 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1014 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1015 && TYPE_ARG_TYPES (newtype) == 0
1016 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1017 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1019 if (pedantic)
1020 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1021 /* Make sure we keep void * as ret type, not char *. */
1022 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1023 TREE_TYPE (newdecl) = newtype = oldtype;
1025 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1026 we will come back here again. */
1027 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1029 /* Permit void foo (...) to match int foo (...) if the latter is the
1030 definition and implicit int was used. See c-torture/compile/920625-2.c. */
1031 else if (!types_match && new_is_definition
1032 && TREE_CODE (olddecl) == FUNCTION_DECL
1033 && TREE_CODE (newdecl) == FUNCTION_DECL
1034 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1035 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1036 && C_FUNCTION_IMPLICIT_INT (newdecl))
1038 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1039 /* Make sure we keep void as the return type. */
1040 TREE_TYPE (newdecl) = newtype = oldtype;
1041 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1043 else if (!types_match
1044 /* Permit char *foo (int, ...); followed by char *foo ();
1045 if not pedantic. */
1046 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1047 && ! pedantic
1048 /* Return types must still match. */
1049 && comptypes (TREE_TYPE (oldtype),
1050 TREE_TYPE (newtype))
1051 && TYPE_ARG_TYPES (newtype) == 0))
1053 error_with_decl (newdecl, "conflicting types for `%s'");
1054 /* Check for function type mismatch
1055 involving an empty arglist vs a nonempty one. */
1056 if (TREE_CODE (olddecl) == FUNCTION_DECL
1057 && comptypes (TREE_TYPE (oldtype),
1058 TREE_TYPE (newtype))
1059 && ((TYPE_ARG_TYPES (oldtype) == 0
1060 && DECL_INITIAL (olddecl) == 0)
1062 (TYPE_ARG_TYPES (newtype) == 0
1063 && DECL_INITIAL (newdecl) == 0)))
1065 /* Classify the problem further. */
1066 tree t = TYPE_ARG_TYPES (oldtype);
1067 if (t == 0)
1068 t = TYPE_ARG_TYPES (newtype);
1069 for (; t; t = TREE_CHAIN (t))
1071 tree type = TREE_VALUE (t);
1073 if (TREE_CHAIN (t) == 0
1074 && TYPE_MAIN_VARIANT (type) != void_type_node)
1076 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1077 break;
1080 if (c_type_promotes_to (type) != type)
1082 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1083 break;
1087 if (C_DECL_IMPLICIT (olddecl))
1088 error_with_decl (olddecl, "previous implicit declaration of `%s'");
1089 else
1090 error_with_decl (olddecl, "previous declaration of `%s'");
1092 /* This is safer because the initializer might contain references
1093 to variables that were declared between olddecl and newdecl. This
1094 will make the initializer invalid for olddecl in case it gets
1095 assigned to olddecl below. */
1096 if (TREE_CODE (newdecl) == VAR_DECL)
1097 DECL_INITIAL (newdecl) = 0;
1099 /* TLS cannot follow non-TLS declaration. */
1100 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1101 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1103 error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1104 error_with_decl (olddecl, "previous declaration of `%s'");
1106 /* non-TLS declaration cannot follow TLS declaration. */
1107 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1108 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1110 error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1111 error_with_decl (olddecl, "previous declaration of `%s'");
1113 else
1115 errmsg = redeclaration_error_message (newdecl, olddecl);
1116 if (errmsg)
1118 switch (errmsg)
1120 case 1:
1121 error_with_decl (newdecl, "redefinition of `%s'");
1122 break;
1123 case 2:
1124 error_with_decl (newdecl, "redeclaration of `%s'");
1125 break;
1126 case 3:
1127 error_with_decl (newdecl, "conflicting declarations of `%s'");
1128 break;
1129 default:
1130 abort ();
1133 error_with_decl (olddecl,
1134 ((DECL_INITIAL (olddecl)
1135 && current_binding_level == global_binding_level)
1136 ? "`%s' previously defined here"
1137 : "`%s' previously declared here"));
1138 return 0;
1140 else if (TREE_CODE (newdecl) == TYPE_DECL
1141 && (DECL_IN_SYSTEM_HEADER (olddecl)
1142 || DECL_IN_SYSTEM_HEADER (newdecl)))
1144 warning_with_decl (newdecl, "redefinition of `%s'");
1145 warning_with_decl
1146 (olddecl,
1147 ((DECL_INITIAL (olddecl)
1148 && current_binding_level == global_binding_level)
1149 ? "`%s' previously defined here"
1150 : "`%s' previously declared here"));
1152 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1153 && DECL_INITIAL (olddecl) != 0
1154 && TYPE_ARG_TYPES (oldtype) == 0
1155 && TYPE_ARG_TYPES (newtype) != 0
1156 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1158 tree type, parm;
1159 int nargs;
1160 /* Prototype decl follows defn w/o prototype. */
1162 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1163 type = TYPE_ARG_TYPES (newtype),
1164 nargs = 1;
1166 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1168 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1169 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1171 warning_with_decl (newdecl, "prototype for `%s' follows");
1172 warning_with_decl (olddecl, "non-prototype definition here");
1173 break;
1175 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1176 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1178 error_with_decl (newdecl,
1179 "prototype for `%s' follows and number of arguments doesn't match");
1180 error_with_decl (olddecl, "non-prototype definition here");
1181 errmsg = 1;
1182 break;
1184 /* Type for passing arg must be consistent
1185 with that declared for the arg. */
1186 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1188 error_with_decl (newdecl,
1189 "prototype for `%s' follows and argument %d doesn't match",
1190 nargs);
1191 error_with_decl (olddecl, "non-prototype definition here");
1192 errmsg = 1;
1193 break;
1197 /* Warn about mismatches in various flags. */
1198 else
1200 /* Warn if function is now inline
1201 but was previously declared not inline and has been called. */
1202 if (TREE_CODE (olddecl) == FUNCTION_DECL
1203 && ! DECL_DECLARED_INLINE_P (olddecl)
1204 && DECL_DECLARED_INLINE_P (newdecl)
1205 && TREE_USED (olddecl))
1206 warning_with_decl (newdecl,
1207 "`%s' declared inline after being called");
1208 if (TREE_CODE (olddecl) == FUNCTION_DECL
1209 && ! DECL_DECLARED_INLINE_P (olddecl)
1210 && DECL_DECLARED_INLINE_P (newdecl)
1211 && DECL_INITIAL (olddecl) != 0)
1212 warning_with_decl (newdecl,
1213 "`%s' declared inline after its definition");
1215 /* If pedantic, warn when static declaration follows a non-static
1216 declaration. Otherwise, do so only for functions. */
1217 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1218 && TREE_PUBLIC (olddecl)
1219 && !TREE_PUBLIC (newdecl))
1220 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1222 /* If warn_traditional, warn when a non-static function
1223 declaration follows a static one. */
1224 if (warn_traditional && !in_system_header
1225 && TREE_CODE (olddecl) == FUNCTION_DECL
1226 && !TREE_PUBLIC (olddecl)
1227 && TREE_PUBLIC (newdecl))
1228 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1230 /* Warn when const declaration follows a non-const
1231 declaration, but not for functions. */
1232 if (TREE_CODE (olddecl) != FUNCTION_DECL
1233 && !TREE_READONLY (olddecl)
1234 && TREE_READONLY (newdecl))
1235 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1236 /* These bits are logically part of the type, for variables.
1237 But not for functions
1238 (where qualifiers are not valid ANSI anyway). */
1239 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1240 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1241 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1242 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1246 /* Optionally warn about more than one declaration for the same name. */
1247 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1248 /* Don't warn about a function declaration
1249 followed by a definition. */
1250 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1251 && DECL_INITIAL (olddecl) == 0)
1252 /* Don't warn about extern decl followed by (tentative) definition. */
1253 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1255 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1256 warning_with_decl (olddecl, "previous declaration of `%s'");
1259 /* Copy all the DECL_... slots specified in the new decl
1260 except for any that we copy here from the old type.
1262 Past this point, we don't change OLDTYPE and NEWTYPE
1263 even if we change the types of NEWDECL and OLDDECL. */
1265 if (types_match)
1267 /* When copying info to olddecl, we store into write_olddecl
1268 instead. This allows us to avoid modifying olddecl when
1269 different_binding_level is true. */
1270 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1272 /* Merge the data types specified in the two decls. */
1273 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1275 if (different_binding_level)
1277 if (TYPE_ARG_TYPES (oldtype) != 0
1278 && TYPE_ARG_TYPES (newtype) == 0)
1279 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1280 else
1281 TREE_TYPE (newdecl)
1282 = build_type_attribute_variant
1283 (newtype,
1284 merge_attributes (TYPE_ATTRIBUTES (newtype),
1285 TYPE_ATTRIBUTES (oldtype)));
1287 else
1288 TREE_TYPE (newdecl)
1289 = TREE_TYPE (olddecl)
1290 = common_type (newtype, oldtype);
1293 /* Lay the type out, unless already done. */
1294 if (oldtype != TREE_TYPE (newdecl))
1296 if (TREE_TYPE (newdecl) != error_mark_node)
1297 layout_type (TREE_TYPE (newdecl));
1298 if (TREE_CODE (newdecl) != FUNCTION_DECL
1299 && TREE_CODE (newdecl) != TYPE_DECL
1300 && TREE_CODE (newdecl) != CONST_DECL)
1301 layout_decl (newdecl, 0);
1303 else
1305 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1306 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1307 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1308 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1309 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1310 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1312 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1313 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1317 /* Keep the old rtl since we can safely use it. */
1318 COPY_DECL_RTL (olddecl, newdecl);
1320 /* Merge the type qualifiers. */
1321 if (TREE_READONLY (newdecl))
1322 TREE_READONLY (write_olddecl) = 1;
1324 if (TREE_THIS_VOLATILE (newdecl))
1326 TREE_THIS_VOLATILE (write_olddecl) = 1;
1327 if (TREE_CODE (newdecl) == VAR_DECL
1328 /* If an automatic variable is re-declared in the same
1329 function scope, but the old declaration was not
1330 volatile, make_var_volatile() would crash because the
1331 variable would have been assigned to a pseudo, not a
1332 MEM. Since this duplicate declaration is invalid
1333 anyway, we just skip the call. */
1334 && errmsg == 0)
1335 make_var_volatile (newdecl);
1338 /* Keep source location of definition rather than declaration. */
1339 /* When called with different_binding_level set, keep the old
1340 information so that meaningful diagnostics can be given. */
1341 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1342 && ! different_binding_level)
1344 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1345 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1348 /* Merge the unused-warning information. */
1349 if (DECL_IN_SYSTEM_HEADER (olddecl))
1350 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1351 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1352 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1354 /* Merge the initialization information. */
1355 /* When called with different_binding_level set, don't copy over
1356 DECL_INITIAL, so that we don't accidentally change function
1357 declarations into function definitions. */
1358 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1359 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1361 /* Merge the section attribute.
1362 We want to issue an error if the sections conflict but that must be
1363 done later in decl_attributes since we are called before attributes
1364 are assigned. */
1365 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1366 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1368 /* Copy the assembler name.
1369 Currently, it can only be defined in the prototype. */
1370 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1372 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1374 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1375 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1376 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1377 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1378 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1379 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1380 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1381 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1382 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1385 /* If cannot merge, then use the new type and qualifiers,
1386 and don't preserve the old rtl. */
1387 else if (! different_binding_level)
1389 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1390 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1391 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1392 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1395 /* Merge the storage class information. */
1396 merge_weak (newdecl, olddecl);
1398 /* For functions, static overrides non-static. */
1399 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1401 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1402 /* This is since we don't automatically
1403 copy the attributes of NEWDECL into OLDDECL. */
1404 /* No need to worry about different_binding_level here because
1405 then TREE_PUBLIC (newdecl) was true. */
1406 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1407 /* If this clears `static', clear it in the identifier too. */
1408 if (! TREE_PUBLIC (olddecl))
1409 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1411 if (DECL_EXTERNAL (newdecl))
1413 if (! different_binding_level)
1415 /* Don't mess with these flags on local externs; they remain
1416 external even if there's a declaration at file scope which
1417 isn't. */
1418 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1419 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1421 /* An extern decl does not override previous storage class. */
1422 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1423 if (! DECL_EXTERNAL (newdecl))
1424 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1426 else
1428 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1429 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1432 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1434 /* If we're redefining a function previously defined as extern
1435 inline, make sure we emit debug info for the inline before we
1436 throw it away, in case it was inlined into a function that hasn't
1437 been written out yet. */
1438 if (new_is_definition && DECL_INITIAL (olddecl))
1440 if (TREE_USED (olddecl))
1441 (*debug_hooks->outlining_inline_function) (olddecl);
1443 /* The new defn must not be inline. */
1444 DECL_INLINE (newdecl) = 0;
1445 DECL_UNINLINABLE (newdecl) = 1;
1447 else
1449 /* If either decl says `inline', this fn is inline,
1450 unless its definition was passed already. */
1451 if (DECL_DECLARED_INLINE_P (newdecl)
1452 || DECL_DECLARED_INLINE_P (olddecl))
1453 DECL_DECLARED_INLINE_P (newdecl) = 1;
1455 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1456 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1459 if (DECL_BUILT_IN (olddecl))
1461 /* Get rid of any built-in function if new arg types don't match it
1462 or if we have a function definition. */
1463 if (! types_match || new_is_definition)
1465 if (! different_binding_level)
1467 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1468 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1471 else
1473 /* If redeclaring a builtin function, and not a definition,
1474 it stays built in. */
1475 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1476 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1480 /* Also preserve various other info from the definition. */
1481 if (! new_is_definition)
1483 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1484 /* When called with different_binding_level set, don't copy over
1485 DECL_INITIAL, so that we don't accidentally change function
1486 declarations into function definitions. */
1487 if (! different_binding_level)
1488 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1489 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1490 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1491 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1492 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1494 /* Set DECL_INLINE on the declaration if we've got a body
1495 from which to instantiate. */
1496 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1498 DECL_INLINE (newdecl) = 1;
1499 DECL_ABSTRACT_ORIGIN (newdecl)
1500 = (different_binding_level
1501 ? DECL_ORIGIN (olddecl)
1502 : DECL_ABSTRACT_ORIGIN (olddecl));
1505 else
1507 /* If a previous declaration said inline, mark the
1508 definition as inlinable. */
1509 if (DECL_DECLARED_INLINE_P (newdecl)
1510 && ! DECL_UNINLINABLE (newdecl))
1511 DECL_INLINE (newdecl) = 1;
1514 if (different_binding_level)
1515 return 0;
1517 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1518 But preserve OLDDECL's DECL_UID. */
1520 unsigned olddecl_uid = DECL_UID (olddecl);
1522 memcpy ((char *) olddecl + sizeof (struct tree_common),
1523 (char *) newdecl + sizeof (struct tree_common),
1524 sizeof (struct tree_decl) - sizeof (struct tree_common));
1525 DECL_UID (olddecl) = olddecl_uid;
1528 /* NEWDECL contains the merged attribute lists.
1529 Update OLDDECL to be the same. */
1530 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1532 return 1;
1535 /* Return any external DECL associated with ID, whether or not it is
1536 currently in scope. */
1537 static tree
1538 any_external_decl (id)
1539 tree id;
1541 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1542 tree t;
1544 if (decl && TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1545 return decl;
1547 t = purpose_member (id, truly_local_externals);
1548 if (t)
1549 return TREE_VALUE (t);
1551 return 0;
1554 /* Record an external decl DECL. This only does something if a
1555 shadowing decl already exists. */
1556 static void
1557 record_external_decl (decl)
1558 tree decl;
1560 tree name = DECL_NAME (decl);
1561 if (!IDENTIFIER_SYMBOL_VALUE (name))
1562 return;
1564 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1567 /* Check whether decl-node X shadows an existing declaration.
1568 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1569 which might be a NULL_TREE. */
1570 static void
1571 warn_if_shadowing (x, old)
1572 tree x, old;
1574 const char *name;
1576 /* Nothing to shadow? */
1577 if (old == 0
1578 /* Shadow warnings not wanted? */
1579 || !warn_shadow
1580 /* No shadow warnings for internally generated vars. */
1581 || DECL_SOURCE_LINE (x) == 0
1582 /* No shadow warnings for vars made for inlining. */
1583 || DECL_FROM_INLINE (x)
1584 /* Don't warn about the parm names in function declarator
1585 within a function declarator.
1586 It would be nice to avoid warning in any function
1587 declarator in a declaration, as opposed to a definition,
1588 but there is no way to tell it's not a definition. */
1589 || (TREE_CODE (x) == PARM_DECL
1590 && current_binding_level->level_chain->parm_flag))
1591 return;
1593 name = IDENTIFIER_POINTER (DECL_NAME (x));
1595 if (TREE_CODE (old) == PARM_DECL)
1596 shadow_warning (SW_PARAM, name, old);
1597 else if (DECL_CONTEXT (old) == 0)
1598 shadow_warning (SW_GLOBAL, name, old);
1599 else
1600 shadow_warning (SW_LOCAL, name, old);
1604 /* Subroutine of pushdecl.
1606 X is a TYPE_DECL for a typedef statement. Create a brand new
1607 ..._TYPE node (which will be just a variant of the existing
1608 ..._TYPE node with identical properties) and then install X
1609 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1611 The whole point here is to end up with a situation where each
1612 and every ..._TYPE node the compiler creates will be uniquely
1613 associated with AT MOST one node representing a typedef name.
1614 This way, even though the compiler substitutes corresponding
1615 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1616 early on, later parts of the compiler can always do the reverse
1617 translation and get back the corresponding typedef name. For
1618 example, given:
1620 typedef struct S MY_TYPE;
1621 MY_TYPE object;
1623 Later parts of the compiler might only know that `object' was of
1624 type `struct S' if it were not for code just below. With this
1625 code however, later parts of the compiler see something like:
1627 struct S' == struct S
1628 typedef struct S' MY_TYPE;
1629 struct S' object;
1631 And they can then deduce (from the node for type struct S') that
1632 the original object declaration was:
1634 MY_TYPE object;
1636 Being able to do this is important for proper support of protoize,
1637 and also for generating precise symbolic debugging information
1638 which takes full account of the programmer's (typedef) vocabulary.
1640 Obviously, we don't want to generate a duplicate ..._TYPE node if
1641 the TYPE_DECL node that we are now processing really represents a
1642 standard built-in type.
1644 Since all standard types are effectively declared at line zero
1645 in the source file, we can easily check to see if we are working
1646 on a standard type by checking the current value of lineno. */
1648 static void
1649 clone_underlying_type (x)
1650 tree x;
1652 if (DECL_SOURCE_LINE (x) == 0)
1654 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1655 TYPE_NAME (TREE_TYPE (x)) = x;
1657 else if (TREE_TYPE (x) != error_mark_node
1658 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1660 tree tt = TREE_TYPE (x);
1661 DECL_ORIGINAL_TYPE (x) = tt;
1662 tt = build_type_copy (tt);
1663 TYPE_NAME (tt) = x;
1664 TREE_USED (tt) = TREE_USED (x);
1665 TREE_TYPE (x) = tt;
1669 /* Record a decl-node X as belonging to the current lexical scope.
1670 Check for errors (such as an incompatible declaration for the same
1671 name already seen in the same scope).
1673 Returns either X or an old decl for the same name.
1674 If an old decl is returned, it may have been smashed
1675 to agree with what X says. */
1677 tree
1678 pushdecl (x)
1679 tree x;
1681 tree name = DECL_NAME (x);
1682 struct binding_level *scope = current_binding_level;
1684 #ifdef ENABLE_CHECKING
1685 if (error_mark_node == 0)
1686 /* Called too early. */
1687 abort ();
1688 #endif
1690 /* Functions need the lang_decl data. */
1691 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1692 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1693 ggc_alloc_cleared (sizeof (struct lang_decl));
1695 /* A local extern declaration for a function doesn't constitute nesting.
1696 A local auto declaration does, since it's a forward decl
1697 for a nested function coming later. */
1698 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1699 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
1700 DECL_CONTEXT (x) = 0;
1701 else
1702 DECL_CONTEXT (x) = current_function_decl;
1704 if (name)
1706 tree old;
1708 if (warn_nested_externs
1709 && scope != global_binding_level
1710 && DECL_EXTERNAL (x)
1711 && !DECL_IN_SYSTEM_HEADER (x))
1712 warning ("nested extern declaration of `%s'",
1713 IDENTIFIER_POINTER (name));
1715 old = lookup_name_current_level (name);
1716 if (old && duplicate_decls (x, old, 0))
1717 return old;
1718 if (DECL_EXTERNAL (x) || scope == global_binding_level)
1720 /* Find and check against a previous, not-in-scope, external
1721 decl for this identifier. (C99 s???: If two declarations
1722 with external linkage, referring to the same object, have
1723 incompatible types, the behavior is undefined). */
1724 tree ext = any_external_decl (name);
1725 if (ext)
1727 if (duplicate_decls (x, ext, scope != global_binding_level))
1728 x = copy_node (ext);
1730 else
1731 record_external_decl (x);
1734 if (TREE_CODE (x) == TYPE_DECL)
1735 clone_underlying_type (x);
1737 /* If storing a local value, there may already be one
1738 (inherited). If so, record it for restoration when this
1739 binding level ends. Take care not to do this if we are
1740 replacing an older decl in the same binding level (i.e.
1741 duplicate_decls returned false, above). */
1742 if (scope != global_binding_level
1743 && IDENTIFIER_SYMBOL_VALUE (name)
1744 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1746 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1747 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1748 scope->shadowed);
1751 /* Install the new declaration in the requested binding level. */
1752 IDENTIFIER_SYMBOL_VALUE (name) = x;
1753 C_DECL_INVISIBLE (x) = 0;
1755 /* Keep list of variables in this level with incomplete type.
1756 If the input is erroneous, we can have error_mark in the type
1757 slot (e.g. "f(void a, ...)") - that doesn't count as an
1758 incomplete type. */
1759 if (TREE_TYPE (x) != error_mark_node
1760 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1762 tree element = TREE_TYPE (x);
1764 while (TREE_CODE (element) == ARRAY_TYPE)
1765 element = TREE_TYPE (element);
1766 if (TREE_CODE (element) == RECORD_TYPE
1767 || TREE_CODE (element) == UNION_TYPE)
1768 scope->incomplete_list = tree_cons (NULL_TREE, x,
1769 scope->incomplete_list);
1773 /* Put decls on list in reverse order.
1774 We will reverse them later if necessary. */
1775 TREE_CHAIN (x) = scope->names;
1776 scope->names = x;
1778 return x;
1781 /* Record X as belonging to the global scope (C99 "file scope").
1782 This is used only internally by the Objective C front end,
1783 and is limited to its needs. It will hork if there is _any_
1784 visible binding for X (not just a global one). */
1785 tree
1786 pushdecl_top_level (x)
1787 tree x;
1789 tree name, old;
1791 if (TREE_CODE (x) != VAR_DECL)
1792 abort ();
1794 name = DECL_NAME (x);
1795 old = IDENTIFIER_SYMBOL_VALUE (name);
1797 if (old)
1799 if (DECL_CONTEXT (old))
1800 abort ();
1802 if (!duplicate_decls (x, old, 0))
1803 abort ();
1805 return old;
1808 DECL_CONTEXT (x) = 0;
1809 IDENTIFIER_SYMBOL_VALUE (name) = x;
1810 TREE_CHAIN (x) = global_binding_level->names;
1811 global_binding_level->names = x;
1812 return x;
1815 /* Record X as belonging to the outermost scope of the current
1816 function. This is used only internally, by c_make_fname_decl and
1817 build_external_ref, and is limited to their needs. The NAME is
1818 provided as a separate argument because build_external_ref wants to
1819 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1820 called; if there is any preexisting decl for this identifier, it is
1821 an ICE. */
1822 tree
1823 pushdecl_function_level (x, name)
1824 tree x;
1825 tree name;
1827 struct binding_level *scope;
1829 scope = current_binding_level;
1830 while (scope->function_body == 0)
1831 scope = scope->level_chain;
1832 if (!scope)
1833 abort ();
1835 if (x == error_mark_node)
1836 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1837 scope->shadowed);
1838 else if (TREE_CODE (x) == VAR_DECL)
1840 if (name != DECL_NAME (x))
1841 abort ();
1842 if (IDENTIFIER_SYMBOL_VALUE (name))
1843 abort ();
1845 DECL_CONTEXT (x) = current_function_decl;
1846 TREE_CHAIN (x) = scope->names;
1847 scope->names = x;
1850 IDENTIFIER_SYMBOL_VALUE (name) = x;
1851 return x;
1854 /* Generate an implicit declaration for identifier FUNCTIONID as a
1855 function of type int (). */
1857 tree
1858 implicitly_declare (functionid)
1859 tree functionid;
1861 tree decl = any_external_decl (functionid);
1863 if (decl && decl != error_mark_node)
1865 /* Implicit declaration of a function already declared
1866 (somehow) in a different scope, or as a built-in.
1867 If this is the first time this has happened, warn;
1868 then recycle the old declaration. */
1869 if (!C_DECL_IMPLICIT (decl))
1871 implicit_decl_warning (DECL_NAME (decl));
1872 if (DECL_CONTEXT (decl))
1873 warning_with_decl (decl, "previous declaration of `%s'");
1874 C_DECL_IMPLICIT (decl) = 1;
1876 /* If this function is global, then it must already be in the
1877 global binding level, so there's no need to push it again. */
1878 if (current_binding_level == global_binding_level)
1879 return decl;
1880 /* If this is a local declaration, make a copy; we can't have
1881 the same DECL listed in two different binding levels. */
1882 return pushdecl (copy_node (decl));
1885 /* Not seen before. */
1886 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1887 DECL_EXTERNAL (decl) = 1;
1888 TREE_PUBLIC (decl) = 1;
1889 C_DECL_IMPLICIT (decl) = 1;
1890 implicit_decl_warning (functionid);
1892 /* ANSI standard says implicit declarations are in the innermost block.
1893 So we record the decl in the standard fashion. */
1894 decl = pushdecl (decl);
1896 /* No need to call objc_check_decl here - it's a function type. */
1897 rest_of_decl_compilation (decl, NULL, 0, 0);
1899 /* Write a record describing this implicit function declaration to the
1900 prototypes file (if requested). */
1902 gen_aux_info_record (decl, 0, 1, 0);
1904 /* Possibly apply some default attributes to this implicit declaration. */
1905 decl_attributes (&decl, NULL_TREE, 0);
1907 return decl;
1910 static void
1911 implicit_decl_warning (id)
1912 tree id;
1914 const char *name = IDENTIFIER_POINTER (id);
1915 if (mesg_implicit_function_declaration == 2)
1916 error ("implicit declaration of function `%s'", name);
1917 else if (mesg_implicit_function_declaration == 1)
1918 warning ("implicit declaration of function `%s'", name);
1921 /* Return zero if the declaration NEWDECL is valid
1922 when the declaration OLDDECL (assumed to be for the same name)
1923 has already been seen.
1924 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1925 and 3 if it is a conflicting declaration. */
1927 static int
1928 redeclaration_error_message (newdecl, olddecl)
1929 tree newdecl, olddecl;
1931 if (TREE_CODE (newdecl) == TYPE_DECL)
1933 /* Do not complain about type redeclarations where at least one
1934 declaration was in a system header. */
1935 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1936 return 0;
1937 return 1;
1939 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1941 /* Declarations of functions can insist on internal linkage
1942 but they can't be inconsistent with internal linkage,
1943 so there can be no error on that account.
1944 However defining the same name twice is no good. */
1945 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1946 /* However, defining once as extern inline and a second
1947 time in another way is ok. */
1948 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1949 && ! (DECL_DECLARED_INLINE_P (newdecl)
1950 && DECL_EXTERNAL (newdecl))))
1951 return 1;
1952 return 0;
1954 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
1956 /* Objects declared at top level: */
1957 /* If at least one is a reference, it's ok. */
1958 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1959 return 0;
1960 /* Reject two definitions. */
1961 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1962 return 1;
1963 /* Now we have two tentative defs, or one tentative and one real def. */
1964 /* Insist that the linkage match. */
1965 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1966 return 3;
1967 return 0;
1969 else if (current_binding_level->parm_flag
1970 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1971 return 0;
1972 else
1974 /* Newdecl has block scope. If olddecl has block scope also, then
1975 reject two definitions, and reject a definition together with an
1976 external reference. Otherwise, it is OK, because newdecl must
1977 be an extern reference to olddecl. */
1978 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1979 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1980 return 2;
1981 return 0;
1985 /* Get the LABEL_DECL corresponding to identifier ID as a label.
1986 Create one if none exists so far for the current function.
1987 This function is called for both label definitions and label references. */
1989 tree
1990 lookup_label (id)
1991 tree id;
1993 tree decl = IDENTIFIER_LABEL_VALUE (id);
1995 if (current_function_decl == 0)
1997 error ("label %s referenced outside of any function",
1998 IDENTIFIER_POINTER (id));
1999 return 0;
2002 /* Use a label already defined or ref'd with this name. */
2003 if (decl != 0)
2005 /* But not if it is inherited and wasn't declared to be inheritable. */
2006 if (DECL_CONTEXT (decl) != current_function_decl
2007 && ! C_DECLARED_LABEL_FLAG (decl))
2008 return shadow_label (id);
2009 return decl;
2012 decl = build_decl (LABEL_DECL, id, void_type_node);
2014 /* A label not explicitly declared must be local to where it's ref'd. */
2015 DECL_CONTEXT (decl) = current_function_decl;
2017 DECL_MODE (decl) = VOIDmode;
2019 /* Say where one reference is to the label,
2020 for the sake of the error if it is not defined. */
2021 DECL_SOURCE_LOCATION (decl) = input_location;
2023 IDENTIFIER_LABEL_VALUE (id) = decl;
2025 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2027 return decl;
2030 /* Make a label named NAME in the current function,
2031 shadowing silently any that may be inherited from containing functions
2032 or containing scopes.
2034 Note that valid use, if the label being shadowed
2035 comes from another scope in the same function,
2036 requires calling declare_nonlocal_label right away. */
2038 tree
2039 shadow_label (name)
2040 tree name;
2042 tree decl = IDENTIFIER_LABEL_VALUE (name);
2044 if (decl != 0)
2046 tree dup;
2048 /* Check to make sure that the label hasn't already been declared
2049 at this label scope */
2050 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2051 if (TREE_VALUE (dup) == decl)
2053 error ("duplicate label declaration `%s'",
2054 IDENTIFIER_POINTER (name));
2055 error_with_decl (TREE_VALUE (dup),
2056 "this is a previous declaration");
2057 /* Just use the previous declaration. */
2058 return lookup_label (name);
2061 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2062 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2065 return lookup_label (name);
2068 /* Define a label, specifying the location in the source file.
2069 Return the LABEL_DECL node for the label, if the definition is valid.
2070 Otherwise return 0. */
2072 tree
2073 define_label (filename, line, name)
2074 const char *filename;
2075 int line;
2076 tree name;
2078 tree decl = lookup_label (name);
2080 /* If label with this name is known from an outer context, shadow it. */
2081 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2083 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2084 IDENTIFIER_LABEL_VALUE (name) = 0;
2085 decl = lookup_label (name);
2088 if (warn_traditional && !in_system_header && lookup_name (name))
2089 warning_with_file_and_line (filename, line,
2090 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2091 IDENTIFIER_POINTER (name));
2093 if (DECL_INITIAL (decl) != 0)
2095 error_with_file_and_line (filename, line, "duplicate label `%s'",
2096 IDENTIFIER_POINTER (name));
2097 return 0;
2099 else
2101 /* Mark label as having been defined. */
2102 DECL_INITIAL (decl) = error_mark_node;
2103 /* Say where in the source. */
2104 DECL_SOURCE_FILE (decl) = filename;
2105 DECL_SOURCE_LINE (decl) = line;
2106 return decl;
2110 /* Return the list of declarations of the current level.
2111 Note that this list is in reverse order unless/until
2112 you nreverse it; and when you do nreverse it, you must
2113 store the result back using `storedecls' or you will lose. */
2115 tree
2116 getdecls ()
2118 return current_binding_level->names;
2121 /* Return the list of type-tags (for structs, etc) of the current level. */
2123 tree
2124 gettags ()
2126 return current_binding_level->tags;
2129 /* Store the list of declarations of the current level.
2130 This is done for the parameter declarations of a function being defined,
2131 after they are modified in the light of any missing parameters. */
2133 static void
2134 storedecls (decls)
2135 tree decls;
2137 current_binding_level->names = decls;
2140 /* Similarly, store the list of tags of the current level. */
2142 static void
2143 storetags (tags)
2144 tree tags;
2146 current_binding_level->tags = tags;
2149 /* Given NAME, an IDENTIFIER_NODE,
2150 return the structure (or union or enum) definition for that name.
2151 If THISLEVEL_ONLY is nonzero, searches only the current_binding_level.
2152 CODE says which kind of type the caller wants;
2153 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2154 If the wrong kind of type is found, an error is reported. */
2156 static tree
2157 lookup_tag (code, name, thislevel_only)
2158 enum tree_code code;
2159 tree name;
2160 int thislevel_only;
2162 tree tag = IDENTIFIER_TAG_VALUE (name);
2163 int thislevel = 0;
2165 if (!tag)
2166 return 0;
2168 /* We only care about whether it's in this level if
2169 thislevel_only was set or it might be a type clash. */
2170 if (thislevel_only || TREE_CODE (tag) != code)
2172 if (current_binding_level == global_binding_level
2173 || purpose_member (name, current_binding_level->tags))
2174 thislevel = 1;
2177 if (thislevel_only && !thislevel)
2178 return 0;
2180 if (TREE_CODE (tag) != code)
2182 /* Definition isn't the kind we were looking for. */
2183 pending_invalid_xref = name;
2184 pending_invalid_xref_location = input_location;
2186 /* If in the same binding level as a declaration as a tag
2187 of a different type, this must not be allowed to
2188 shadow that tag, so give the error immediately.
2189 (For example, "struct foo; union foo;" is invalid.) */
2190 if (thislevel)
2191 pending_xref_error ();
2193 return tag;
2196 /* Print an error message now
2197 for a recent invalid struct, union or enum cross reference.
2198 We don't print them immediately because they are not invalid
2199 when used in the `struct foo;' construct for shadowing. */
2201 void
2202 pending_xref_error ()
2204 if (pending_invalid_xref != 0)
2205 error_with_file_and_line (pending_invalid_xref_location.file,
2206 pending_invalid_xref_location.line,
2207 "`%s' defined as wrong kind of tag",
2208 IDENTIFIER_POINTER (pending_invalid_xref));
2209 pending_invalid_xref = 0;
2213 /* Look up NAME in the current binding level and its superiors
2214 in the namespace of variables, functions and typedefs.
2215 Return a ..._DECL node of some kind representing its definition,
2216 or return 0 if it is undefined. */
2218 tree
2219 lookup_name (name)
2220 tree name;
2222 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2223 if (decl == 0 || decl == error_mark_node)
2224 return decl;
2225 if (C_DECL_INVISIBLE (decl))
2226 return 0;
2227 return decl;
2230 /* Similar to `lookup_name' but look only at the current binding level. */
2232 static tree
2233 lookup_name_current_level (name)
2234 tree name;
2236 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2238 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2239 return 0;
2241 if (current_binding_level == global_binding_level)
2242 return decl;
2244 /* Scan the current scope for a decl with name NAME. */
2245 if (chain_member (decl, current_binding_level->names))
2246 return decl;
2248 return 0;
2251 /* Create the predefined scalar types of C,
2252 and some nodes representing standard constants (0, 1, (void *) 0).
2253 Initialize the global binding level.
2254 Make definitions for built-in primitive functions. */
2256 void
2257 c_init_decl_processing ()
2259 tree endlink;
2260 tree ptr_ftype_void, ptr_ftype_ptr;
2262 /* Adds some ggc roots, and reserved words for c-parse.in. */
2263 c_parse_init ();
2265 current_function_decl = NULL;
2266 named_labels = NULL;
2267 current_binding_level = NULL_BINDING_LEVEL;
2268 free_binding_level = NULL_BINDING_LEVEL;
2270 /* Make the binding_level structure for global names. */
2271 pushlevel (0);
2272 global_binding_level = current_binding_level;
2274 build_common_tree_nodes (flag_signed_char);
2276 c_common_nodes_and_builtins ();
2278 boolean_type_node = integer_type_node;
2279 boolean_true_node = integer_one_node;
2280 boolean_false_node = integer_zero_node;
2282 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2283 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2284 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2285 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2286 TYPE_PRECISION (c_bool_type_node) = 1;
2287 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2288 c_bool_type_node));
2289 c_bool_false_node = build_int_2 (0, 0);
2290 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2291 c_bool_true_node = build_int_2 (1, 0);
2292 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2294 endlink = void_list_node;
2295 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2296 ptr_ftype_ptr
2297 = build_function_type (ptr_type_node,
2298 tree_cons (NULL_TREE, ptr_type_node, endlink));
2300 pedantic_lvalues = pedantic;
2302 make_fname_decl = c_make_fname_decl;
2303 start_fname_decls ();
2306 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2307 decl, NAME is the initialization string and TYPE_DEP indicates whether
2308 NAME depended on the type of the function. As we don't yet implement
2309 delayed emission of static data, we mark the decl as emitted
2310 so it is not placed in the output. Anything using it must therefore pull
2311 out the STRING_CST initializer directly. This does mean that these names
2312 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2314 static tree
2315 c_make_fname_decl (id, type_dep)
2316 tree id;
2317 int type_dep;
2319 const char *name = fname_as_string (type_dep);
2320 tree decl, type, init;
2321 size_t length = strlen (name);
2323 type = build_array_type
2324 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2325 build_index_type (size_int (length)));
2327 decl = build_decl (VAR_DECL, id, type);
2329 TREE_STATIC (decl) = 1;
2330 TREE_READONLY (decl) = 1;
2331 DECL_ARTIFICIAL (decl) = 1;
2333 init = build_string (length + 1, name);
2334 TREE_TYPE (init) = type;
2335 DECL_INITIAL (decl) = init;
2337 TREE_USED (decl) = 1;
2339 if (current_function_decl)
2340 pushdecl_function_level (decl, DECL_NAME (decl));
2342 finish_decl (decl, init, NULL_TREE);
2344 return decl;
2347 /* Return a definition for a builtin function named NAME and whose data type
2348 is TYPE. TYPE should be a function type with argument types.
2349 FUNCTION_CODE tells later passes how to compile calls to this function.
2350 See tree.h for its possible values.
2352 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2353 the name to be called if we can't opencode the function. If
2354 ATTRS is nonzero, use that for the function's attribute list. */
2356 tree
2357 builtin_function (name, type, function_code, class, library_name, attrs)
2358 const char *name;
2359 tree type;
2360 int function_code;
2361 enum built_in_class class;
2362 const char *library_name;
2363 tree attrs;
2365 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2366 DECL_EXTERNAL (decl) = 1;
2367 TREE_PUBLIC (decl) = 1;
2368 if (library_name)
2369 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2370 make_decl_rtl (decl, NULL);
2371 pushdecl (decl);
2372 DECL_BUILT_IN_CLASS (decl) = class;
2373 DECL_FUNCTION_CODE (decl) = function_code;
2375 /* Warn if a function in the namespace for users
2376 is used without an occasion to consider it declared. */
2377 if (name[0] != '_' || name[1] != '_')
2378 C_DECL_INVISIBLE (decl) = 1;
2380 /* Possibly apply some default attributes to this built-in function. */
2381 if (attrs)
2382 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2383 else
2384 decl_attributes (&decl, NULL_TREE, 0);
2386 return decl;
2389 /* Apply default attributes to a function, if a system function with default
2390 attributes. */
2392 void
2393 c_insert_default_attributes (decl)
2394 tree decl;
2396 if (!TREE_PUBLIC (decl))
2397 return;
2398 c_common_insert_default_attributes (decl);
2401 /* Called when a declaration is seen that contains no names to declare.
2402 If its type is a reference to a structure, union or enum inherited
2403 from a containing scope, shadow that tag name for the current scope
2404 with a forward reference.
2405 If its type defines a new named structure or union
2406 or defines an enum, it is valid but we need not do anything here.
2407 Otherwise, it is an error. */
2409 void
2410 shadow_tag (declspecs)
2411 tree declspecs;
2413 shadow_tag_warned (declspecs, 0);
2416 void
2417 shadow_tag_warned (declspecs, warned)
2418 tree declspecs;
2419 int warned;
2420 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2421 no pedwarn. */
2423 int found_tag = 0;
2424 tree link;
2425 tree specs, attrs;
2427 pending_invalid_xref = 0;
2429 /* Remove the attributes from declspecs, since they will confuse the
2430 following code. */
2431 split_specs_attrs (declspecs, &specs, &attrs);
2433 for (link = specs; link; link = TREE_CHAIN (link))
2435 tree value = TREE_VALUE (link);
2436 enum tree_code code = TREE_CODE (value);
2438 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2439 /* Used to test also that TYPE_SIZE (value) != 0.
2440 That caused warning for `struct foo;' at top level in the file. */
2442 tree name = TYPE_NAME (value);
2443 tree t;
2445 found_tag++;
2447 if (name == 0)
2449 if (warned != 1 && code != ENUMERAL_TYPE)
2450 /* Empty unnamed enum OK */
2452 pedwarn ("unnamed struct/union that defines no instances");
2453 warned = 1;
2456 else
2458 t = lookup_tag (code, name, 1);
2460 if (t == 0)
2462 t = make_node (code);
2463 pushtag (name, t);
2467 else
2469 if (!warned && ! in_system_header)
2471 warning ("useless keyword or type name in empty declaration");
2472 warned = 2;
2477 if (found_tag > 1)
2478 error ("two types specified in one empty declaration");
2480 if (warned != 1)
2482 if (found_tag == 0)
2483 pedwarn ("empty declaration");
2487 /* Construct an array declarator. EXPR is the expression inside [], or
2488 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2489 to the pointer to which a parameter array is converted). STATIC_P is
2490 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2491 is nonzero is the array is [*], a VLA of unspecified length which is
2492 nevertheless a complete type (not currently implemented by GCC),
2493 zero otherwise. The declarator is constructed as an ARRAY_REF
2494 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2495 left of the [] (filled by in set_array_declarator_type) and operand 1
2496 is the expression inside; whose TREE_TYPE is the type qualifiers and
2497 which has TREE_STATIC set if "static" is used. */
2499 tree
2500 build_array_declarator (expr, quals, static_p, vla_unspec_p)
2501 tree expr;
2502 tree quals;
2503 int static_p;
2504 int vla_unspec_p;
2506 tree decl;
2507 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2508 TREE_TYPE (decl) = quals;
2509 TREE_STATIC (decl) = (static_p ? 1 : 0);
2510 if (pedantic && !flag_isoc99)
2512 if (static_p || quals != NULL_TREE)
2513 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2514 if (vla_unspec_p)
2515 pedwarn ("ISO C90 does not support `[*]' array declarators");
2517 if (vla_unspec_p)
2518 warning ("GCC does not yet properly implement `[*]' array declarators");
2519 return decl;
2522 /* Set the type of an array declarator. DECL is the declarator, as
2523 constructed by build_array_declarator; TYPE is what appears on the left
2524 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2525 abstract declarator, zero otherwise; this is used to reject static and
2526 type qualifiers in abstract declarators, where they are not in the
2527 C99 grammar. */
2529 tree
2530 set_array_declarator_type (decl, type, abstract_p)
2531 tree decl;
2532 tree type;
2533 int abstract_p;
2535 TREE_OPERAND (decl, 0) = type;
2536 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2537 error ("static or type qualifiers in abstract declarator");
2538 return decl;
2541 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2543 tree
2544 groktypename (typename)
2545 tree typename;
2547 tree specs, attrs;
2549 if (TREE_CODE (typename) != TREE_LIST)
2550 return typename;
2552 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2554 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2556 /* Apply attributes. */
2557 decl_attributes (&typename, attrs, 0);
2559 return typename;
2562 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2564 tree
2565 groktypename_in_parm_context (typename)
2566 tree typename;
2568 if (TREE_CODE (typename) != TREE_LIST)
2569 return typename;
2570 return grokdeclarator (TREE_VALUE (typename),
2571 TREE_PURPOSE (typename),
2572 PARM, 0);
2575 /* Decode a declarator in an ordinary declaration or data definition.
2576 This is called as soon as the type information and variable name
2577 have been parsed, before parsing the initializer if any.
2578 Here we create the ..._DECL node, fill in its type,
2579 and put it on the list of decls for the current context.
2580 The ..._DECL node is returned as the value.
2582 Exception: for arrays where the length is not specified,
2583 the type is left null, to be filled in by `finish_decl'.
2585 Function definitions do not come here; they go to start_function
2586 instead. However, external and forward declarations of functions
2587 do go through here. Structure field declarations are done by
2588 grokfield and not through here. */
2590 tree
2591 start_decl (declarator, declspecs, initialized, attributes)
2592 tree declarator, declspecs;
2593 int initialized;
2594 tree attributes;
2596 tree decl;
2597 tree tem;
2599 /* An object declared as __attribute__((deprecated)) suppresses
2600 warnings of uses of other deprecated items. */
2601 if (lookup_attribute ("deprecated", attributes))
2602 deprecated_state = DEPRECATED_SUPPRESS;
2604 decl = grokdeclarator (declarator, declspecs,
2605 NORMAL, initialized);
2607 deprecated_state = DEPRECATED_NORMAL;
2609 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2610 && MAIN_NAME_P (DECL_NAME (decl)))
2611 warning_with_decl (decl, "`%s' is usually a function");
2613 if (initialized)
2614 /* Is it valid for this decl to have an initializer at all?
2615 If not, set INITIALIZED to zero, which will indirectly
2616 tell `finish_decl' to ignore the initializer once it is parsed. */
2617 switch (TREE_CODE (decl))
2619 case TYPE_DECL:
2620 error ("typedef `%s' is initialized (use __typeof__ instead)",
2621 IDENTIFIER_POINTER (DECL_NAME (decl)));
2622 initialized = 0;
2623 break;
2625 case FUNCTION_DECL:
2626 error ("function `%s' is initialized like a variable",
2627 IDENTIFIER_POINTER (DECL_NAME (decl)));
2628 initialized = 0;
2629 break;
2631 case PARM_DECL:
2632 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2633 error ("parameter `%s' is initialized",
2634 IDENTIFIER_POINTER (DECL_NAME (decl)));
2635 initialized = 0;
2636 break;
2638 default:
2639 /* Don't allow initializations for incomplete types
2640 except for arrays which might be completed by the initialization. */
2642 /* This can happen if the array size is an undefined macro. We already
2643 gave a warning, so we don't need another one. */
2644 if (TREE_TYPE (decl) == error_mark_node)
2645 initialized = 0;
2646 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2648 /* A complete type is ok if size is fixed. */
2650 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2651 || C_DECL_VARIABLE_SIZE (decl))
2653 error ("variable-sized object may not be initialized");
2654 initialized = 0;
2657 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2659 error ("variable `%s' has initializer but incomplete type",
2660 IDENTIFIER_POINTER (DECL_NAME (decl)));
2661 initialized = 0;
2663 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2665 error ("elements of array `%s' have incomplete type",
2666 IDENTIFIER_POINTER (DECL_NAME (decl)));
2667 initialized = 0;
2671 if (initialized)
2673 #if 0
2674 /* Seems redundant with grokdeclarator. */
2675 if (current_binding_level != global_binding_level
2676 && DECL_EXTERNAL (decl)
2677 && TREE_CODE (decl) != FUNCTION_DECL)
2678 warning ("declaration of `%s' has `extern' and is initialized",
2679 IDENTIFIER_POINTER (DECL_NAME (decl)));
2680 #endif
2681 DECL_EXTERNAL (decl) = 0;
2682 if (current_binding_level == global_binding_level)
2683 TREE_STATIC (decl) = 1;
2685 /* Tell `pushdecl' this is an initialized decl
2686 even though we don't yet have the initializer expression.
2687 Also tell `finish_decl' it may store the real initializer. */
2688 DECL_INITIAL (decl) = error_mark_node;
2691 /* If this is a function declaration, write a record describing it to the
2692 prototypes file (if requested). */
2694 if (TREE_CODE (decl) == FUNCTION_DECL)
2695 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2697 /* ANSI specifies that a tentative definition which is not merged with
2698 a non-tentative definition behaves exactly like a definition with an
2699 initializer equal to zero. (Section 3.7.2)
2701 -fno-common gives strict ANSI behavior, though this tends to break
2702 a large body of code that grew up without this rule.
2704 Thread-local variables are never common, since there's no entrenched
2705 body of code to break, and it allows more efficient variable references
2706 in the presence of dynamic linking. */
2708 if (TREE_CODE (decl) == VAR_DECL
2709 && !initialized
2710 && TREE_PUBLIC (decl)
2711 && !DECL_THREAD_LOCAL (decl)
2712 && !flag_no_common)
2713 DECL_COMMON (decl) = 1;
2715 /* Set attributes here so if duplicate decl, will have proper attributes. */
2716 decl_attributes (&decl, attributes, 0);
2718 /* If #pragma weak was used, mark the decl weak now. */
2719 if (current_binding_level == global_binding_level)
2720 maybe_apply_pragma_weak (decl);
2722 if (TREE_CODE (decl) == FUNCTION_DECL
2723 && DECL_DECLARED_INLINE_P (decl)
2724 && DECL_UNINLINABLE (decl)
2725 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2726 warning_with_decl (decl,
2727 "inline function `%s' given attribute noinline");
2729 /* Add this decl to the current binding level.
2730 TEM may equal DECL or it may be a previous decl of the same name. */
2731 tem = pushdecl (decl);
2733 /* For a local variable, define the RTL now. */
2734 if (current_binding_level != global_binding_level
2735 /* But not if this is a duplicate decl
2736 and we preserved the rtl from the previous one
2737 (which may or may not happen). */
2738 && !DECL_RTL_SET_P (tem)
2739 && !DECL_CONTEXT (tem))
2741 if (TREE_TYPE (tem) != error_mark_node
2742 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2743 expand_decl (tem);
2744 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2745 && DECL_INITIAL (tem) != 0)
2746 expand_decl (tem);
2749 return tem;
2752 /* Finish processing of a declaration;
2753 install its initial value.
2754 If the length of an array type is not known before,
2755 it must be determined now, from the initial value, or it is an error. */
2757 void
2758 finish_decl (decl, init, asmspec_tree)
2759 tree decl, init;
2760 tree asmspec_tree;
2762 tree type = TREE_TYPE (decl);
2763 int was_incomplete = (DECL_SIZE (decl) == 0);
2764 const char *asmspec = 0;
2766 /* If a name was specified, get the string. */
2767 if (current_binding_level == global_binding_level)
2768 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2769 if (asmspec_tree)
2770 asmspec = TREE_STRING_POINTER (asmspec_tree);
2772 /* If `start_decl' didn't like having an initialization, ignore it now. */
2773 if (init != 0 && DECL_INITIAL (decl) == 0)
2774 init = 0;
2776 /* Don't crash if parm is initialized. */
2777 if (TREE_CODE (decl) == PARM_DECL)
2778 init = 0;
2780 if (init)
2781 store_init_value (decl, init);
2783 /* Deduce size of array from initialization, if not already known */
2784 if (TREE_CODE (type) == ARRAY_TYPE
2785 && TYPE_DOMAIN (type) == 0
2786 && TREE_CODE (decl) != TYPE_DECL)
2788 int do_default
2789 = (TREE_STATIC (decl)
2790 /* Even if pedantic, an external linkage array
2791 may have incomplete type at first. */
2792 ? pedantic && !TREE_PUBLIC (decl)
2793 : !DECL_EXTERNAL (decl));
2794 int failure
2795 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2797 /* Get the completed type made by complete_array_type. */
2798 type = TREE_TYPE (decl);
2800 if (failure == 1)
2801 error_with_decl (decl, "initializer fails to determine size of `%s'");
2803 else if (failure == 2)
2805 if (do_default)
2806 error_with_decl (decl, "array size missing in `%s'");
2807 /* If a `static' var's size isn't known,
2808 make it extern as well as static, so it does not get
2809 allocated.
2810 If it is not `static', then do not mark extern;
2811 finish_incomplete_decl will give it a default size
2812 and it will get allocated. */
2813 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2814 DECL_EXTERNAL (decl) = 1;
2817 /* TYPE_MAX_VALUE is always one less than the number of elements
2818 in the array, because we start counting at zero. Therefore,
2819 warn only if the value is less than zero. */
2820 else if (pedantic && TYPE_DOMAIN (type) != 0
2821 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2822 error_with_decl (decl, "zero or negative size array `%s'");
2824 layout_decl (decl, 0);
2827 if (TREE_CODE (decl) == VAR_DECL)
2829 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2830 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2831 layout_decl (decl, 0);
2833 if (DECL_SIZE (decl) == 0
2834 /* Don't give an error if we already gave one earlier. */
2835 && TREE_TYPE (decl) != error_mark_node
2836 && (TREE_STATIC (decl)
2838 /* A static variable with an incomplete type
2839 is an error if it is initialized.
2840 Also if it is not file scope.
2841 Otherwise, let it through, but if it is not `extern'
2842 then it may cause an error message later. */
2843 (DECL_INITIAL (decl) != 0
2844 || DECL_CONTEXT (decl) != 0)
2846 /* An automatic variable with an incomplete type
2847 is an error. */
2848 !DECL_EXTERNAL (decl)))
2850 error_with_decl (decl, "storage size of `%s' isn't known");
2851 TREE_TYPE (decl) = error_mark_node;
2854 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2855 && DECL_SIZE (decl) != 0)
2857 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2858 constant_expression_warning (DECL_SIZE (decl));
2859 else
2860 error_with_decl (decl, "storage size of `%s' isn't constant");
2863 if (TREE_USED (type))
2864 TREE_USED (decl) = 1;
2867 /* If this is a function and an assembler name is specified, reset DECL_RTL
2868 so we can give it its new name. Also, update built_in_decls if it
2869 was a normal built-in. */
2870 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2872 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2874 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2875 SET_DECL_RTL (builtin, NULL_RTX);
2876 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (asmspec));
2877 #ifdef TARGET_MEM_FUNCTIONS
2878 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2879 init_block_move_fn (asmspec);
2880 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2881 init_block_clear_fn (asmspec);
2882 #else
2883 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2884 init_block_move_fn (asmspec);
2885 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2886 init_block_clear_fn (asmspec);
2887 #endif
2889 SET_DECL_RTL (decl, NULL_RTX);
2890 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2893 /* Output the assembler code and/or RTL code for variables and functions,
2894 unless the type is an undefined structure or union.
2895 If not, it will get done when the type is completed. */
2897 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2899 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2900 if (flag_objc)
2901 objc_check_decl (decl);
2903 if (!DECL_CONTEXT (decl))
2905 if (DECL_INITIAL (decl) == NULL_TREE
2906 || DECL_INITIAL (decl) == error_mark_node)
2907 /* Don't output anything
2908 when a tentative file-scope definition is seen.
2909 But at end of compilation, do output code for them. */
2910 DECL_DEFER_OUTPUT (decl) = 1;
2911 rest_of_decl_compilation (decl, asmspec,
2912 (DECL_CONTEXT (decl) == 0
2913 || TREE_ASM_WRITTEN (decl)), 0);
2915 else
2917 /* This is a local variable. If there is an ASMSPEC, the
2918 user has requested that we handle it specially. */
2919 if (asmspec)
2921 /* In conjunction with an ASMSPEC, the `register'
2922 keyword indicates that we should place the variable
2923 in a particular register. */
2924 if (DECL_REGISTER (decl))
2925 DECL_C_HARD_REGISTER (decl) = 1;
2927 /* If this is not a static variable, issue a warning.
2928 It doesn't make any sense to give an ASMSPEC for an
2929 ordinary, non-register local variable. Historically,
2930 GCC has accepted -- but ignored -- the ASMSPEC in
2931 this case. */
2932 if (TREE_CODE (decl) == VAR_DECL
2933 && !DECL_REGISTER (decl)
2934 && !TREE_STATIC (decl))
2935 warning_with_decl (decl,
2936 "ignoring asm-specifier for non-static local variable `%s'");
2937 else
2938 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2941 if (TREE_CODE (decl) != FUNCTION_DECL)
2942 add_decl_stmt (decl);
2945 if (DECL_CONTEXT (decl) != 0)
2947 /* Recompute the RTL of a local array now
2948 if it used to be an incomplete type. */
2949 if (was_incomplete
2950 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2952 /* If we used it already as memory, it must stay in memory. */
2953 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2954 /* If it's still incomplete now, no init will save it. */
2955 if (DECL_SIZE (decl) == 0)
2956 DECL_INITIAL (decl) = 0;
2961 if (TREE_CODE (decl) == TYPE_DECL)
2963 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2964 if (flag_objc)
2965 objc_check_decl (decl);
2966 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
2969 /* At the end of a declaration, throw away any variable type sizes
2970 of types defined inside that declaration. There is no use
2971 computing them in the following function definition. */
2972 if (current_binding_level == global_binding_level)
2973 get_pending_sizes ();
2976 /* Given a parsed parameter declaration,
2977 decode it into a PARM_DECL and push that on the current binding level.
2978 Also, for the sake of forward parm decls,
2979 record the given order of parms in `parm_order'. */
2981 void
2982 push_parm_decl (parm)
2983 tree parm;
2985 tree decl;
2986 int old_immediate_size_expand = immediate_size_expand;
2987 /* Don't try computing parm sizes now -- wait till fn is called. */
2988 immediate_size_expand = 0;
2990 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2991 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2992 decl_attributes (&decl, TREE_VALUE (parm), 0);
2994 #if 0
2995 if (DECL_NAME (decl))
2997 tree olddecl;
2998 olddecl = lookup_name (DECL_NAME (decl));
2999 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3000 pedwarn_with_decl (decl,
3001 "ISO C forbids parameter `%s' shadowing typedef");
3003 #endif
3005 decl = pushdecl (decl);
3007 immediate_size_expand = old_immediate_size_expand;
3009 current_binding_level->parm_order
3010 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3012 /* Add this decl to the current binding level. */
3013 finish_decl (decl, NULL_TREE, NULL_TREE);
3016 /* Clear the given order of parms in `parm_order'.
3017 Used at start of parm list,
3018 and also at semicolon terminating forward decls. */
3020 void
3021 clear_parm_order ()
3023 current_binding_level->parm_order = NULL_TREE;
3026 static GTY(()) int compound_literal_number;
3028 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3029 literal, which may be an incomplete array type completed by the
3030 initializer; INIT is a CONSTRUCTOR that initializes the compound
3031 literal. */
3033 tree
3034 build_compound_literal (type, init)
3035 tree type;
3036 tree init;
3038 /* We do not use start_decl here because we have a type, not a declarator;
3039 and do not use finish_decl because the decl should be stored inside
3040 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3041 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3042 tree complit;
3043 tree stmt;
3044 DECL_EXTERNAL (decl) = 0;
3045 TREE_PUBLIC (decl) = 0;
3046 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3047 DECL_CONTEXT (decl) = current_function_decl;
3048 TREE_USED (decl) = 1;
3049 TREE_TYPE (decl) = type;
3050 TREE_READONLY (decl) = TREE_READONLY (type);
3051 store_init_value (decl, init);
3053 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3055 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3056 if (failure)
3057 abort ();
3060 type = TREE_TYPE (decl);
3061 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3062 return error_mark_node;
3064 stmt = build_stmt (DECL_STMT, decl);
3065 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3066 TREE_SIDE_EFFECTS (complit) = 1;
3068 layout_decl (decl, 0);
3070 if (TREE_STATIC (decl))
3072 /* This decl needs a name for the assembler output. We also need
3073 a unique suffix to be added to the name. */
3074 char *name;
3076 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3077 compound_literal_number);
3078 compound_literal_number++;
3079 DECL_NAME (decl) = get_identifier (name);
3080 DECL_DEFER_OUTPUT (decl) = 1;
3081 DECL_COMDAT (decl) = 1;
3082 DECL_ARTIFICIAL (decl) = 1;
3083 pushdecl (decl);
3084 rest_of_decl_compilation (decl, NULL, 1, 0);
3087 return complit;
3090 /* Make TYPE a complete type based on INITIAL_VALUE.
3091 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3092 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3095 complete_array_type (type, initial_value, do_default)
3096 tree type;
3097 tree initial_value;
3098 int do_default;
3100 tree maxindex = NULL_TREE;
3101 int value = 0;
3103 if (initial_value)
3105 /* Note MAXINDEX is really the maximum index,
3106 one less than the size. */
3107 if (TREE_CODE (initial_value) == STRING_CST)
3109 int eltsize
3110 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3111 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3112 / eltsize) - 1, 0);
3114 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3116 tree elts = CONSTRUCTOR_ELTS (initial_value);
3117 maxindex = build_int_2 (-1, -1);
3118 for (; elts; elts = TREE_CHAIN (elts))
3120 if (TREE_PURPOSE (elts))
3121 maxindex = TREE_PURPOSE (elts);
3122 else
3123 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3124 maxindex, integer_one_node));
3126 maxindex = copy_node (maxindex);
3128 else
3130 /* Make an error message unless that happened already. */
3131 if (initial_value != error_mark_node)
3132 value = 1;
3134 /* Prevent further error messages. */
3135 maxindex = build_int_2 (0, 0);
3139 if (!maxindex)
3141 if (do_default)
3142 maxindex = build_int_2 (0, 0);
3143 value = 2;
3146 if (maxindex)
3148 TYPE_DOMAIN (type) = build_index_type (maxindex);
3149 if (!TREE_TYPE (maxindex))
3150 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3153 /* Lay out the type now that we can get the real answer. */
3155 layout_type (type);
3157 return value;
3160 /* Determine whether TYPE is a structure with a flexible array member,
3161 or a union containing such a structure (possibly recursively). */
3163 static bool
3164 flexible_array_type_p (type)
3165 tree type;
3167 tree x;
3168 switch (TREE_CODE (type))
3170 case RECORD_TYPE:
3171 x = TYPE_FIELDS (type);
3172 if (x == NULL_TREE)
3173 return false;
3174 while (TREE_CHAIN (x) != NULL_TREE)
3175 x = TREE_CHAIN (x);
3176 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3177 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3178 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3179 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3180 return true;
3181 return false;
3182 case UNION_TYPE:
3183 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3185 if (flexible_array_type_p (TREE_TYPE (x)))
3186 return true;
3188 return false;
3189 default:
3190 return false;
3194 /* Given declspecs and a declarator,
3195 determine the name and type of the object declared
3196 and construct a ..._DECL node for it.
3197 (In one case we can return a ..._TYPE node instead.
3198 For invalid input we sometimes return 0.)
3200 DECLSPECS is a chain of tree_list nodes whose value fields
3201 are the storage classes and type specifiers.
3203 DECL_CONTEXT says which syntactic context this declaration is in:
3204 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3205 FUNCDEF for a function definition. Like NORMAL but a few different
3206 error messages in each case. Return value may be zero meaning
3207 this definition is too screwy to try to parse.
3208 PARM for a parameter declaration (either within a function prototype
3209 or before a function body). Make a PARM_DECL, or return void_type_node.
3210 TYPENAME if for a typename (in a cast or sizeof).
3211 Don't make a DECL node; just return the ..._TYPE node.
3212 FIELD for a struct or union field; make a FIELD_DECL.
3213 BITFIELD for a field with specified width.
3214 INITIALIZED is 1 if the decl has an initializer.
3216 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3217 It may also be so in the PARM case, for a prototype where the
3218 argument type is specified but not the name.
3220 This function is where the complicated C meanings of `static'
3221 and `extern' are interpreted. */
3223 static tree
3224 grokdeclarator (declarator, declspecs, decl_context, initialized)
3225 tree declspecs;
3226 tree declarator;
3227 enum decl_context decl_context;
3228 int initialized;
3230 int specbits = 0;
3231 tree spec;
3232 tree type = NULL_TREE;
3233 int longlong = 0;
3234 int constp;
3235 int restrictp;
3236 int volatilep;
3237 int type_quals = TYPE_UNQUALIFIED;
3238 int inlinep;
3239 int explicit_int = 0;
3240 int explicit_char = 0;
3241 int defaulted_int = 0;
3242 tree typedef_decl = 0;
3243 const char *name;
3244 tree typedef_type = 0;
3245 int funcdef_flag = 0;
3246 enum tree_code innermost_code = ERROR_MARK;
3247 int bitfield = 0;
3248 int size_varies = 0;
3249 tree decl_attr = NULL_TREE;
3250 tree array_ptr_quals = NULL_TREE;
3251 int array_parm_static = 0;
3252 tree returned_attrs = NULL_TREE;
3254 if (decl_context == BITFIELD)
3255 bitfield = 1, decl_context = FIELD;
3257 if (decl_context == FUNCDEF)
3258 funcdef_flag = 1, decl_context = NORMAL;
3260 /* Look inside a declarator for the name being declared
3261 and get it as a string, for an error message. */
3263 tree decl = declarator;
3264 name = 0;
3266 while (decl)
3267 switch (TREE_CODE (decl))
3269 case ARRAY_REF:
3270 case INDIRECT_REF:
3271 case CALL_EXPR:
3272 innermost_code = TREE_CODE (decl);
3273 decl = TREE_OPERAND (decl, 0);
3274 break;
3276 case TREE_LIST:
3277 decl = TREE_VALUE (decl);
3278 break;
3280 case IDENTIFIER_NODE:
3281 name = IDENTIFIER_POINTER (decl);
3282 decl = 0;
3283 break;
3285 default:
3286 abort ();
3288 if (name == 0)
3289 name = "type name";
3292 /* A function definition's declarator must have the form of
3293 a function declarator. */
3295 if (funcdef_flag && innermost_code != CALL_EXPR)
3296 return 0;
3298 /* Anything declared one level down from the top level
3299 must be one of the parameters of a function
3300 (because the body is at least two levels down). */
3302 /* If this looks like a function definition, make it one,
3303 even if it occurs where parms are expected.
3304 Then store_parm_decls will reject it and not use it as a parm. */
3305 if (decl_context == NORMAL && !funcdef_flag
3306 && current_binding_level->parm_flag)
3307 decl_context = PARM;
3309 /* Look through the decl specs and record which ones appear.
3310 Some typespecs are defined as built-in typenames.
3311 Others, the ones that are modifiers of other types,
3312 are represented by bits in SPECBITS: set the bits for
3313 the modifiers that appear. Storage class keywords are also in SPECBITS.
3315 If there is a typedef name or a type, store the type in TYPE.
3316 This includes builtin typedefs such as `int'.
3318 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3319 and did not come from a user typedef.
3321 Set LONGLONG if `long' is mentioned twice. */
3323 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3325 tree id = TREE_VALUE (spec);
3327 /* If the entire declaration is itself tagged as deprecated then
3328 suppress reports of deprecated items. */
3329 if (id && TREE_DEPRECATED (id))
3331 if (deprecated_state != DEPRECATED_SUPPRESS)
3332 warn_deprecated_use (id);
3335 if (id == ridpointers[(int) RID_INT])
3336 explicit_int = 1;
3337 if (id == ridpointers[(int) RID_CHAR])
3338 explicit_char = 1;
3340 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3342 enum rid i = C_RID_CODE (id);
3343 if ((int) i <= (int) RID_LAST_MODIFIER)
3345 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3347 if (longlong)
3348 error ("`long long long' is too long for GCC");
3349 else
3351 if (pedantic && !flag_isoc99 && ! in_system_header
3352 && warn_long_long)
3353 pedwarn ("ISO C90 does not support `long long'");
3354 longlong = 1;
3357 else if (specbits & (1 << (int) i))
3359 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3361 if (!flag_isoc99)
3362 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3364 else
3365 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3368 /* Diagnose "__thread extern". Recall that this list
3369 is in the reverse order seen in the text. */
3370 if (i == RID_THREAD
3371 && (specbits & (1 << (int) RID_EXTERN
3372 | 1 << (int) RID_STATIC)))
3374 if (specbits & 1 << (int) RID_EXTERN)
3375 error ("`__thread' before `extern'");
3376 else
3377 error ("`__thread' before `static'");
3380 specbits |= 1 << (int) i;
3381 goto found;
3384 if (type)
3385 error ("two or more data types in declaration of `%s'", name);
3386 /* Actual typedefs come to us as TYPE_DECL nodes. */
3387 else if (TREE_CODE (id) == TYPE_DECL)
3389 if (TREE_TYPE (id) == error_mark_node)
3390 ; /* Allow the type to default to int to avoid cascading errors. */
3391 else
3393 type = TREE_TYPE (id);
3394 decl_attr = DECL_ATTRIBUTES (id);
3395 typedef_decl = id;
3398 /* Built-in types come as identifiers. */
3399 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3401 tree t = lookup_name (id);
3402 if (TREE_TYPE (t) == error_mark_node)
3404 else if (!t || TREE_CODE (t) != TYPE_DECL)
3405 error ("`%s' fails to be a typedef or built in type",
3406 IDENTIFIER_POINTER (id));
3407 else
3409 type = TREE_TYPE (t);
3410 typedef_decl = t;
3413 else if (TREE_CODE (id) != ERROR_MARK)
3414 type = id;
3416 found:
3420 typedef_type = type;
3421 if (type)
3422 size_varies = C_TYPE_VARIABLE_SIZE (type);
3424 /* No type at all: default to `int', and set DEFAULTED_INT
3425 because it was not a user-defined typedef. */
3427 if (type == 0)
3429 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3430 | (1 << (int) RID_SIGNED)
3431 | (1 << (int) RID_UNSIGNED)
3432 | (1 << (int) RID_COMPLEX))))
3433 /* Don't warn about typedef foo = bar. */
3434 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3435 && ! in_system_header)
3437 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3438 and this is a function, or if -Wimplicit; prefer the former
3439 warning since it is more explicit. */
3440 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3441 && funcdef_flag)
3442 warn_about_return_type = 1;
3443 else if (warn_implicit_int || flag_isoc99)
3444 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3445 name);
3448 defaulted_int = 1;
3449 type = integer_type_node;
3452 /* Now process the modifiers that were specified
3453 and check for invalid combinations. */
3455 /* Long double is a special combination. */
3457 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3458 && TYPE_MAIN_VARIANT (type) == double_type_node)
3460 specbits &= ~(1 << (int) RID_LONG);
3461 type = long_double_type_node;
3464 /* Check all other uses of type modifiers. */
3466 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3467 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3469 int ok = 0;
3471 if ((specbits & 1 << (int) RID_LONG)
3472 && (specbits & 1 << (int) RID_SHORT))
3473 error ("both long and short specified for `%s'", name);
3474 else if (((specbits & 1 << (int) RID_LONG)
3475 || (specbits & 1 << (int) RID_SHORT))
3476 && explicit_char)
3477 error ("long or short specified with char for `%s'", name);
3478 else if (((specbits & 1 << (int) RID_LONG)
3479 || (specbits & 1 << (int) RID_SHORT))
3480 && TREE_CODE (type) == REAL_TYPE)
3482 static int already = 0;
3484 error ("long or short specified with floating type for `%s'", name);
3485 if (! already && ! pedantic)
3487 error ("the only valid combination is `long double'");
3488 already = 1;
3491 else if ((specbits & 1 << (int) RID_SIGNED)
3492 && (specbits & 1 << (int) RID_UNSIGNED))
3493 error ("both signed and unsigned specified for `%s'", name);
3494 else if (TREE_CODE (type) != INTEGER_TYPE)
3495 error ("long, short, signed or unsigned invalid for `%s'", name);
3496 else
3498 ok = 1;
3499 if (!explicit_int && !defaulted_int && !explicit_char)
3501 error ("long, short, signed or unsigned used invalidly for `%s'",
3502 name);
3503 ok = 0;
3507 /* Discard the type modifiers if they are invalid. */
3508 if (! ok)
3510 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3511 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3512 longlong = 0;
3516 if ((specbits & (1 << (int) RID_COMPLEX))
3517 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3519 error ("complex invalid for `%s'", name);
3520 specbits &= ~(1 << (int) RID_COMPLEX);
3523 /* Decide whether an integer type is signed or not.
3524 Optionally treat bitfields as signed by default. */
3525 if (specbits & 1 << (int) RID_UNSIGNED
3526 || (bitfield && ! flag_signed_bitfields
3527 && (explicit_int || defaulted_int || explicit_char
3528 /* A typedef for plain `int' without `signed'
3529 can be controlled just like plain `int'. */
3530 || ! (typedef_decl != 0
3531 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3532 && TREE_CODE (type) != ENUMERAL_TYPE
3533 && !(specbits & 1 << (int) RID_SIGNED)))
3535 if (longlong)
3536 type = long_long_unsigned_type_node;
3537 else if (specbits & 1 << (int) RID_LONG)
3538 type = long_unsigned_type_node;
3539 else if (specbits & 1 << (int) RID_SHORT)
3540 type = short_unsigned_type_node;
3541 else if (type == char_type_node)
3542 type = unsigned_char_type_node;
3543 else if (typedef_decl)
3544 type = c_common_unsigned_type (type);
3545 else
3546 type = unsigned_type_node;
3548 else if ((specbits & 1 << (int) RID_SIGNED)
3549 && type == char_type_node)
3550 type = signed_char_type_node;
3551 else if (longlong)
3552 type = long_long_integer_type_node;
3553 else if (specbits & 1 << (int) RID_LONG)
3554 type = long_integer_type_node;
3555 else if (specbits & 1 << (int) RID_SHORT)
3556 type = short_integer_type_node;
3558 if (specbits & 1 << (int) RID_COMPLEX)
3560 if (pedantic && !flag_isoc99)
3561 pedwarn ("ISO C90 does not support complex types");
3562 /* If we just have "complex", it is equivalent to
3563 "complex double", but if any modifiers at all are specified it is
3564 the complex form of TYPE. E.g, "complex short" is
3565 "complex short int". */
3567 if (defaulted_int && ! longlong
3568 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3569 | (1 << (int) RID_SIGNED)
3570 | (1 << (int) RID_UNSIGNED))))
3572 if (pedantic)
3573 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3574 type = complex_double_type_node;
3576 else if (type == integer_type_node)
3578 if (pedantic)
3579 pedwarn ("ISO C does not support complex integer types");
3580 type = complex_integer_type_node;
3582 else if (type == float_type_node)
3583 type = complex_float_type_node;
3584 else if (type == double_type_node)
3585 type = complex_double_type_node;
3586 else if (type == long_double_type_node)
3587 type = complex_long_double_type_node;
3588 else
3590 if (pedantic)
3591 pedwarn ("ISO C does not support complex integer types");
3592 type = build_complex_type (type);
3596 /* Figure out the type qualifiers for the declaration. There are
3597 two ways a declaration can become qualified. One is something
3598 like `const int i' where the `const' is explicit. Another is
3599 something like `typedef const int CI; CI i' where the type of the
3600 declaration contains the `const'. */
3601 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3602 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3603 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3604 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3605 if (constp > 1 && ! flag_isoc99)
3606 pedwarn ("duplicate `const'");
3607 if (restrictp > 1 && ! flag_isoc99)
3608 pedwarn ("duplicate `restrict'");
3609 if (volatilep > 1 && ! flag_isoc99)
3610 pedwarn ("duplicate `volatile'");
3611 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3612 type = TYPE_MAIN_VARIANT (type);
3613 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3614 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3615 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3617 /* Warn if two storage classes are given. Default to `auto'. */
3620 int nclasses = 0;
3622 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3623 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3624 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3625 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3626 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3628 /* "static __thread" and "extern __thread" are allowed. */
3629 if ((specbits & (1 << (int) RID_THREAD
3630 | 1 << (int) RID_STATIC
3631 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3632 nclasses++;
3634 /* Warn about storage classes that are invalid for certain
3635 kinds of declarations (parameters, typenames, etc.). */
3637 if (nclasses > 1)
3638 error ("multiple storage classes in declaration of `%s'", name);
3639 else if (funcdef_flag
3640 && (specbits
3641 & ((1 << (int) RID_REGISTER)
3642 | (1 << (int) RID_AUTO)
3643 | (1 << (int) RID_TYPEDEF)
3644 | (1 << (int) RID_THREAD))))
3646 if (specbits & 1 << (int) RID_AUTO
3647 && (pedantic || current_binding_level == global_binding_level))
3648 pedwarn ("function definition declared `auto'");
3649 if (specbits & 1 << (int) RID_REGISTER)
3650 error ("function definition declared `register'");
3651 if (specbits & 1 << (int) RID_TYPEDEF)
3652 error ("function definition declared `typedef'");
3653 if (specbits & 1 << (int) RID_THREAD)
3654 error ("function definition declared `__thread'");
3655 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3656 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3658 else if (decl_context != NORMAL && nclasses > 0)
3660 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3662 else
3664 switch (decl_context)
3666 case FIELD:
3667 error ("storage class specified for structure field `%s'",
3668 name);
3669 break;
3670 case PARM:
3671 error ("storage class specified for parameter `%s'", name);
3672 break;
3673 default:
3674 error ("storage class specified for typename");
3675 break;
3677 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3678 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3679 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3682 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3684 /* `extern' with initialization is invalid if not at top level. */
3685 if (current_binding_level == global_binding_level)
3686 warning ("`%s' initialized and declared `extern'", name);
3687 else
3688 error ("`%s' has both `extern' and initializer", name);
3690 else if (current_binding_level == global_binding_level)
3692 if (specbits & 1 << (int) RID_AUTO)
3693 error ("top-level declaration of `%s' specifies `auto'", name);
3695 else
3697 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3698 error ("nested function `%s' declared `extern'", name);
3699 else if ((specbits & (1 << (int) RID_THREAD
3700 | 1 << (int) RID_EXTERN
3701 | 1 << (int) RID_STATIC))
3702 == (1 << (int) RID_THREAD))
3704 error ("function-scope `%s' implicitly auto and declared `__thread'",
3705 name);
3706 specbits &= ~(1 << (int) RID_THREAD);
3711 /* Now figure out the structure of the declarator proper.
3712 Descend through it, creating more complex types, until we reach
3713 the declared identifier (or NULL_TREE, in an absolute declarator). */
3715 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3717 if (type == error_mark_node)
3719 declarator = TREE_OPERAND (declarator, 0);
3720 continue;
3723 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3724 an INDIRECT_REF (for *...),
3725 a CALL_EXPR (for ...(...)),
3726 a TREE_LIST (for nested attributes),
3727 an identifier (for the name being declared)
3728 or a null pointer (for the place in an absolute declarator
3729 where the name was omitted).
3730 For the last two cases, we have just exited the loop.
3732 At this point, TYPE is the type of elements of an array,
3733 or for a function to return, or for a pointer to point to.
3734 After this sequence of ifs, TYPE is the type of the
3735 array or function or pointer, and DECLARATOR has had its
3736 outermost layer removed. */
3738 if (array_ptr_quals != NULL_TREE || array_parm_static)
3740 /* Only the innermost declarator (making a parameter be of
3741 array type which is converted to pointer type)
3742 may have static or type qualifiers. */
3743 error ("static or type qualifiers in non-parameter array declarator");
3744 array_ptr_quals = NULL_TREE;
3745 array_parm_static = 0;
3748 if (TREE_CODE (declarator) == TREE_LIST)
3750 /* We encode a declarator with embedded attributes using
3751 a TREE_LIST. */
3752 tree attrs = TREE_PURPOSE (declarator);
3753 tree inner_decl;
3754 int attr_flags = 0;
3755 declarator = TREE_VALUE (declarator);
3756 inner_decl = declarator;
3757 while (inner_decl != NULL_TREE
3758 && TREE_CODE (inner_decl) == TREE_LIST)
3759 inner_decl = TREE_VALUE (inner_decl);
3760 if (inner_decl == NULL_TREE
3761 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3762 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3763 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3764 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3765 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3766 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3767 returned_attrs = decl_attributes (&type,
3768 chainon (returned_attrs, attrs),
3769 attr_flags);
3771 else if (TREE_CODE (declarator) == ARRAY_REF)
3773 tree itype = NULL_TREE;
3774 tree size = TREE_OPERAND (declarator, 1);
3775 /* The index is a signed object `sizetype' bits wide. */
3776 tree index_type = c_common_signed_type (sizetype);
3778 array_ptr_quals = TREE_TYPE (declarator);
3779 array_parm_static = TREE_STATIC (declarator);
3781 declarator = TREE_OPERAND (declarator, 0);
3783 /* Check for some types that there cannot be arrays of. */
3785 if (VOID_TYPE_P (type))
3787 error ("declaration of `%s' as array of voids", name);
3788 type = error_mark_node;
3791 if (TREE_CODE (type) == FUNCTION_TYPE)
3793 error ("declaration of `%s' as array of functions", name);
3794 type = error_mark_node;
3797 if (pedantic && flexible_array_type_p (type))
3798 pedwarn ("invalid use of structure with flexible array member");
3800 if (size == error_mark_node)
3801 type = error_mark_node;
3803 if (type == error_mark_node)
3804 continue;
3806 /* If size was specified, set ITYPE to a range-type for that size.
3807 Otherwise, ITYPE remains null. finish_decl may figure it out
3808 from an initial value. */
3810 if (size)
3812 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3813 STRIP_TYPE_NOPS (size);
3815 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3817 error ("size of array `%s' has non-integer type", name);
3818 size = integer_one_node;
3821 if (pedantic && integer_zerop (size))
3822 pedwarn ("ISO C forbids zero-size array `%s'", name);
3824 if (TREE_CODE (size) == INTEGER_CST)
3826 constant_expression_warning (size);
3827 if (tree_int_cst_sgn (size) < 0)
3829 error ("size of array `%s' is negative", name);
3830 size = integer_one_node;
3833 else
3835 /* Make sure the array size remains visibly nonconstant
3836 even if it is (eg) a const variable with known value. */
3837 size_varies = 1;
3839 if (!flag_isoc99 && pedantic)
3841 if (TREE_CONSTANT (size))
3842 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3843 name);
3844 else
3845 pedwarn ("ISO C90 forbids variable-size array `%s'",
3846 name);
3850 if (integer_zerop (size))
3852 /* A zero-length array cannot be represented with an
3853 unsigned index type, which is what we'll get with
3854 build_index_type. Create an open-ended range instead. */
3855 itype = build_range_type (sizetype, size, NULL_TREE);
3857 else
3859 /* Compute the maximum valid index, that is, size - 1.
3860 Do the calculation in index_type, so that if it is
3861 a variable the computations will be done in the
3862 proper mode. */
3863 itype = fold (build (MINUS_EXPR, index_type,
3864 convert (index_type, size),
3865 convert (index_type, size_one_node)));
3867 /* If that overflowed, the array is too big.
3868 ??? While a size of INT_MAX+1 technically shouldn't
3869 cause an overflow (because we subtract 1), the overflow
3870 is recorded during the conversion to index_type, before
3871 the subtraction. Handling this case seems like an
3872 unnecessary complication. */
3873 if (TREE_OVERFLOW (itype))
3875 error ("size of array `%s' is too large", name);
3876 type = error_mark_node;
3877 continue;
3880 if (size_varies)
3882 /* We must be able to distinguish the
3883 SAVE_EXPR_CONTEXT for the variably-sized type
3884 so that we can set it correctly in
3885 set_save_expr_context. The convention is
3886 that all SAVE_EXPRs that need to be reset
3887 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3888 tree cfd = current_function_decl;
3889 if (decl_context == PARM)
3890 current_function_decl = NULL_TREE;
3891 itype = variable_size (itype);
3892 if (decl_context == PARM)
3893 current_function_decl = cfd;
3895 itype = build_index_type (itype);
3898 else if (decl_context == FIELD)
3900 if (pedantic && !flag_isoc99 && !in_system_header)
3901 pedwarn ("ISO C90 does not support flexible array members");
3903 /* ISO C99 Flexible array members are effectively identical
3904 to GCC's zero-length array extension. */
3905 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3908 /* If pedantic, complain about arrays of incomplete types. */
3910 if (pedantic && !COMPLETE_TYPE_P (type))
3911 pedwarn ("array type has incomplete element type");
3913 #if 0
3914 /* We shouldn't have a function type here at all!
3915 Functions aren't allowed as array elements. */
3916 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3917 && (constp || volatilep))
3918 pedwarn ("ISO C forbids const or volatile function types");
3919 #endif
3921 /* Build the array type itself, then merge any constancy or
3922 volatility into the target type. We must do it in this order
3923 to ensure that the TYPE_MAIN_VARIANT field of the array type
3924 is set correctly. */
3926 type = build_array_type (type, itype);
3927 if (type_quals)
3928 type = c_build_qualified_type (type, type_quals);
3930 if (size_varies)
3931 C_TYPE_VARIABLE_SIZE (type) = 1;
3933 /* The GCC extension for zero-length arrays differs from
3934 ISO flexible array members in that sizeof yields zero. */
3935 if (size && integer_zerop (size))
3937 layout_type (type);
3938 TYPE_SIZE (type) = bitsize_zero_node;
3939 TYPE_SIZE_UNIT (type) = size_zero_node;
3941 if (decl_context != PARM
3942 && (array_ptr_quals != NULL_TREE || array_parm_static))
3944 error ("static or type qualifiers in non-parameter array declarator");
3945 array_ptr_quals = NULL_TREE;
3946 array_parm_static = 0;
3949 else if (TREE_CODE (declarator) == CALL_EXPR)
3951 tree arg_types;
3953 /* Declaring a function type.
3954 Make sure we have a valid type for the function to return. */
3955 if (type == error_mark_node)
3956 continue;
3958 size_varies = 0;
3960 /* Warn about some types functions can't return. */
3962 if (TREE_CODE (type) == FUNCTION_TYPE)
3964 error ("`%s' declared as function returning a function", name);
3965 type = integer_type_node;
3967 if (TREE_CODE (type) == ARRAY_TYPE)
3969 error ("`%s' declared as function returning an array", name);
3970 type = integer_type_node;
3973 /* Construct the function type and go to the next
3974 inner layer of declarator. */
3976 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3977 funcdef_flag
3978 /* Say it's a definition
3979 only for the CALL_EXPR
3980 closest to the identifier. */
3981 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3982 /* Type qualifiers before the return type of the function
3983 qualify the return type, not the function type. */
3984 if (type_quals)
3986 /* Type qualifiers on a function return type are normally
3987 permitted by the standard but have no effect, so give a
3988 warning at -Wextra. Qualifiers on a void return type have
3989 meaning as a GNU extension, and are banned on function
3990 definitions in ISO C. FIXME: strictly we shouldn't
3991 pedwarn for qualified void return types except on function
3992 definitions, but not doing so could lead to the undesirable
3993 state of a "volatile void" function return type not being
3994 warned about, and a use of the function being compiled
3995 with GNU semantics, with no diagnostics under -pedantic. */
3996 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3997 pedwarn ("ISO C forbids qualified void function return type");
3998 else if (extra_warnings
3999 && !(VOID_TYPE_P (type)
4000 && type_quals == TYPE_QUAL_VOLATILE))
4001 warning ("type qualifiers ignored on function return type");
4003 type = c_build_qualified_type (type, type_quals);
4005 type_quals = TYPE_UNQUALIFIED;
4007 type = build_function_type (type, arg_types);
4008 declarator = TREE_OPERAND (declarator, 0);
4010 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4011 the formal parameter list of this FUNCTION_TYPE to point to
4012 the FUNCTION_TYPE node itself. */
4015 tree link;
4017 for (link = last_function_parm_tags;
4018 link;
4019 link = TREE_CHAIN (link))
4020 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4023 else if (TREE_CODE (declarator) == INDIRECT_REF)
4025 /* Merge any constancy or volatility into the target type
4026 for the pointer. */
4028 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4029 && type_quals)
4030 pedwarn ("ISO C forbids qualified function types");
4031 if (type_quals)
4032 type = c_build_qualified_type (type, type_quals);
4033 type_quals = TYPE_UNQUALIFIED;
4034 size_varies = 0;
4036 type = build_pointer_type (type);
4038 /* Process a list of type modifier keywords
4039 (such as const or volatile) that were given inside the `*'. */
4041 if (TREE_TYPE (declarator))
4043 tree typemodlist;
4044 int erred = 0;
4046 constp = 0;
4047 volatilep = 0;
4048 restrictp = 0;
4049 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4050 typemodlist = TREE_CHAIN (typemodlist))
4052 tree qualifier = TREE_VALUE (typemodlist);
4054 if (C_IS_RESERVED_WORD (qualifier))
4056 if (C_RID_CODE (qualifier) == RID_CONST)
4057 constp++;
4058 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4059 volatilep++;
4060 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4061 restrictp++;
4062 else
4063 erred++;
4065 else
4066 erred++;
4069 if (erred)
4070 error ("invalid type modifier within pointer declarator");
4071 if (constp > 1 && ! flag_isoc99)
4072 pedwarn ("duplicate `const'");
4073 if (volatilep > 1 && ! flag_isoc99)
4074 pedwarn ("duplicate `volatile'");
4075 if (restrictp > 1 && ! flag_isoc99)
4076 pedwarn ("duplicate `restrict'");
4078 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4079 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4080 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4083 declarator = TREE_OPERAND (declarator, 0);
4085 else
4086 abort ();
4090 /* Now TYPE has the actual type. */
4092 /* Did array size calculations overflow? */
4094 if (TREE_CODE (type) == ARRAY_TYPE
4095 && COMPLETE_TYPE_P (type)
4096 && TREE_OVERFLOW (TYPE_SIZE (type)))
4098 error ("size of array `%s' is too large", name);
4099 /* If we proceed with the array type as it is, we'll eventually
4100 crash in tree_low_cst(). */
4101 type = error_mark_node;
4104 /* If this is declaring a typedef name, return a TYPE_DECL. */
4106 if (specbits & (1 << (int) RID_TYPEDEF))
4108 tree decl;
4109 /* Note that the grammar rejects storage classes
4110 in typenames, fields or parameters */
4111 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4112 && type_quals)
4113 pedwarn ("ISO C forbids qualified function types");
4114 if (type_quals)
4115 type = c_build_qualified_type (type, type_quals);
4116 decl = build_decl (TYPE_DECL, declarator, type);
4117 if ((specbits & (1 << (int) RID_SIGNED))
4118 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4119 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4120 decl_attributes (&decl, returned_attrs, 0);
4121 return decl;
4124 /* Detect the case of an array type of unspecified size
4125 which came, as such, direct from a typedef name.
4126 We must copy the type, so that each identifier gets
4127 a distinct type, so that each identifier's size can be
4128 controlled separately by its own initializer. */
4130 if (type != 0 && typedef_type != 0
4131 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4132 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4134 type = build_array_type (TREE_TYPE (type), 0);
4135 if (size_varies)
4136 C_TYPE_VARIABLE_SIZE (type) = 1;
4139 /* If this is a type name (such as, in a cast or sizeof),
4140 compute the type and return it now. */
4142 if (decl_context == TYPENAME)
4144 /* Note that the grammar rejects storage classes
4145 in typenames, fields or parameters */
4146 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4147 && type_quals)
4148 pedwarn ("ISO C forbids const or volatile function types");
4149 if (type_quals)
4150 type = c_build_qualified_type (type, type_quals);
4151 decl_attributes (&type, returned_attrs, 0);
4152 return type;
4155 /* Aside from typedefs and type names (handle above),
4156 `void' at top level (not within pointer)
4157 is allowed only in public variables.
4158 We don't complain about parms either, but that is because
4159 a better error message can be made later. */
4161 if (VOID_TYPE_P (type) && decl_context != PARM
4162 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4163 && ((specbits & (1 << (int) RID_EXTERN))
4164 || (current_binding_level == global_binding_level
4165 && !(specbits
4166 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4168 error ("variable or field `%s' declared void", name);
4169 type = integer_type_node;
4172 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4173 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4176 tree decl;
4178 if (decl_context == PARM)
4180 tree type_as_written;
4181 tree promoted_type;
4183 /* A parameter declared as an array of T is really a pointer to T.
4184 One declared as a function is really a pointer to a function. */
4186 if (TREE_CODE (type) == ARRAY_TYPE)
4188 /* Transfer const-ness of array into that of type pointed to. */
4189 type = TREE_TYPE (type);
4190 if (type_quals)
4191 type = c_build_qualified_type (type, type_quals);
4192 type = build_pointer_type (type);
4193 type_quals = TYPE_UNQUALIFIED;
4194 if (array_ptr_quals)
4196 tree new_ptr_quals, new_ptr_attrs;
4197 int erred = 0;
4198 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4199 /* We don't yet implement attributes in this context. */
4200 if (new_ptr_attrs != NULL_TREE)
4201 warning ("attributes in parameter array declarator ignored");
4203 constp = 0;
4204 volatilep = 0;
4205 restrictp = 0;
4206 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4208 tree qualifier = TREE_VALUE (new_ptr_quals);
4210 if (C_IS_RESERVED_WORD (qualifier))
4212 if (C_RID_CODE (qualifier) == RID_CONST)
4213 constp++;
4214 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4215 volatilep++;
4216 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4217 restrictp++;
4218 else
4219 erred++;
4221 else
4222 erred++;
4225 if (erred)
4226 error ("invalid type modifier within array declarator");
4228 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4229 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4230 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4232 size_varies = 0;
4234 else if (TREE_CODE (type) == FUNCTION_TYPE)
4236 if (pedantic && type_quals)
4237 pedwarn ("ISO C forbids qualified function types");
4238 if (type_quals)
4239 type = c_build_qualified_type (type, type_quals);
4240 type = build_pointer_type (type);
4241 type_quals = TYPE_UNQUALIFIED;
4243 else if (type_quals)
4244 type = c_build_qualified_type (type, type_quals);
4246 type_as_written = type;
4248 decl = build_decl (PARM_DECL, declarator, type);
4249 if (size_varies)
4250 C_DECL_VARIABLE_SIZE (decl) = 1;
4252 /* Compute the type actually passed in the parmlist,
4253 for the case where there is no prototype.
4254 (For example, shorts and chars are passed as ints.)
4255 When there is a prototype, this is overridden later. */
4257 if (type == error_mark_node)
4258 promoted_type = type;
4259 else
4260 promoted_type = c_type_promotes_to (type);
4262 DECL_ARG_TYPE (decl) = promoted_type;
4263 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4265 else if (decl_context == FIELD)
4267 /* Structure field. It may not be a function. */
4269 if (TREE_CODE (type) == FUNCTION_TYPE)
4271 error ("field `%s' declared as a function", name);
4272 type = build_pointer_type (type);
4274 else if (TREE_CODE (type) != ERROR_MARK
4275 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4277 error ("field `%s' has incomplete type", name);
4278 type = error_mark_node;
4280 /* Move type qualifiers down to element of an array. */
4281 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4283 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4284 type_quals),
4285 TYPE_DOMAIN (type));
4286 #if 0
4287 /* Leave the field const or volatile as well. */
4288 type_quals = TYPE_UNQUALIFIED;
4289 #endif
4291 decl = build_decl (FIELD_DECL, declarator, type);
4292 DECL_NONADDRESSABLE_P (decl) = bitfield;
4294 if (size_varies)
4295 C_DECL_VARIABLE_SIZE (decl) = 1;
4297 else if (TREE_CODE (type) == FUNCTION_TYPE)
4299 /* Every function declaration is "external"
4300 except for those which are inside a function body
4301 in which `auto' is used.
4302 That is a case not specified by ANSI C,
4303 and we use it for forward declarations for nested functions. */
4304 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4305 || current_binding_level == global_binding_level);
4307 if (specbits & (1 << (int) RID_AUTO)
4308 && (pedantic || current_binding_level == global_binding_level))
4309 pedwarn ("invalid storage class for function `%s'", name);
4310 if (specbits & (1 << (int) RID_REGISTER))
4311 error ("invalid storage class for function `%s'", name);
4312 if (specbits & (1 << (int) RID_THREAD))
4313 error ("invalid storage class for function `%s'", name);
4314 /* Function declaration not at top level.
4315 Storage classes other than `extern' are not allowed
4316 and `extern' makes no difference. */
4317 if (current_binding_level != global_binding_level
4318 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4319 && pedantic)
4320 pedwarn ("invalid storage class for function `%s'", name);
4322 decl = build_decl (FUNCTION_DECL, declarator, type);
4323 decl = build_decl_attribute_variant (decl, decl_attr);
4325 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4326 ggc_alloc_cleared (sizeof (struct lang_decl));
4328 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4329 pedwarn ("ISO C forbids qualified function types");
4331 /* GNU C interprets a `volatile void' return type to indicate
4332 that the function does not return. */
4333 if ((type_quals & TYPE_QUAL_VOLATILE)
4334 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4335 warning ("`noreturn' function returns non-void value");
4337 if (extern_ref)
4338 DECL_EXTERNAL (decl) = 1;
4339 /* Record absence of global scope for `static' or `auto'. */
4340 TREE_PUBLIC (decl)
4341 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4343 if (defaulted_int)
4344 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4346 /* Record presence of `inline', if it is reasonable. */
4347 if (MAIN_NAME_P (declarator))
4349 if (inlinep)
4350 warning ("cannot inline function `main'");
4352 else if (inlinep)
4354 /* Assume that otherwise the function can be inlined. */
4355 DECL_DECLARED_INLINE_P (decl) = 1;
4357 /* Do not mark bare declarations as DECL_INLINE. Doing so
4358 in the presence of multiple declarations can result in
4359 the abstract origin pointing between the declarations,
4360 which will confuse dwarf2out. */
4361 if (initialized)
4363 DECL_INLINE (decl) = 1;
4364 if (specbits & (1 << (int) RID_EXTERN))
4365 current_extern_inline = 1;
4368 /* If -finline-functions, assume it can be inlined. This does
4369 two things: let the function be deferred until it is actually
4370 needed, and let dwarf2 know that the function is inlinable. */
4371 else if (flag_inline_trees == 2 && initialized)
4373 if (!DECL_INLINE (decl))
4374 DID_INLINE_FUNC (decl) = 1;
4375 DECL_INLINE (decl) = 1;
4376 DECL_DECLARED_INLINE_P (decl) = 0;
4379 else
4381 /* It's a variable. */
4382 /* An uninitialized decl with `extern' is a reference. */
4383 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4385 /* Move type qualifiers down to element of an array. */
4386 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4388 int saved_align = TYPE_ALIGN(type);
4389 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4390 type_quals),
4391 TYPE_DOMAIN (type));
4392 TYPE_ALIGN (type) = saved_align;
4393 #if 0 /* Leave the variable const or volatile as well. */
4394 type_quals = TYPE_UNQUALIFIED;
4395 #endif
4397 else if (type_quals)
4398 type = c_build_qualified_type (type, type_quals);
4400 /* It is invalid to create an `extern' declaration for a
4401 variable if there is a global declaration that is
4402 `static'. */
4403 if (extern_ref && current_binding_level != global_binding_level)
4405 tree global_decl;
4407 global_decl = identifier_global_value (declarator);
4408 if (global_decl
4409 && TREE_CODE (global_decl) == VAR_DECL
4410 && !TREE_PUBLIC (global_decl))
4411 error ("variable previously declared `static' redeclared "
4412 "`extern'");
4415 decl = build_decl (VAR_DECL, declarator, type);
4416 if (size_varies)
4417 C_DECL_VARIABLE_SIZE (decl) = 1;
4419 if (inlinep)
4420 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4422 DECL_EXTERNAL (decl) = extern_ref;
4424 /* At top level, the presence of a `static' or `register' storage
4425 class specifier, or the absence of all storage class specifiers
4426 makes this declaration a definition (perhaps tentative). Also,
4427 the absence of both `static' and `register' makes it public. */
4428 if (current_binding_level == global_binding_level)
4430 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4431 | (1 << (int) RID_REGISTER)));
4432 TREE_STATIC (decl) = !extern_ref;
4434 /* Not at top level, only `static' makes a static definition. */
4435 else
4437 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4438 TREE_PUBLIC (decl) = extern_ref;
4441 if (specbits & 1 << (int) RID_THREAD)
4443 if (targetm.have_tls)
4444 DECL_THREAD_LOCAL (decl) = 1;
4445 else
4446 /* A mere warning is sure to result in improper semantics
4447 at runtime. Don't bother to allow this to compile. */
4448 error ("thread-local storage not supported for this target");
4452 /* Record `register' declaration for warnings on &
4453 and in case doing stupid register allocation. */
4455 if (specbits & (1 << (int) RID_REGISTER))
4456 DECL_REGISTER (decl) = 1;
4458 /* Record constancy and volatility. */
4459 c_apply_type_quals_to_decl (type_quals, decl);
4461 /* If a type has volatile components, it should be stored in memory.
4462 Otherwise, the fact that those components are volatile
4463 will be ignored, and would even crash the compiler. */
4464 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4465 c_mark_addressable (decl);
4467 decl_attributes (&decl, returned_attrs, 0);
4469 return decl;
4473 /* Decode the parameter-list info for a function type or function definition.
4474 The argument is the value returned by `get_parm_info' (or made in parse.y
4475 if there is an identifier list instead of a parameter decl list).
4476 These two functions are separate because when a function returns
4477 or receives functions then each is called multiple times but the order
4478 of calls is different. The last call to `grokparms' is always the one
4479 that contains the formal parameter names of a function definition.
4481 Store in `last_function_parms' a chain of the decls of parms.
4482 Also store in `last_function_parm_tags' a chain of the struct, union,
4483 and enum tags declared among the parms.
4485 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4487 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4488 a mere declaration. A nonempty identifier-list gets an error message
4489 when FUNCDEF_FLAG is zero. */
4491 static tree
4492 grokparms (parms_info, funcdef_flag)
4493 tree parms_info;
4494 int funcdef_flag;
4496 tree first_parm = TREE_CHAIN (parms_info);
4498 last_function_parms = TREE_PURPOSE (parms_info);
4499 last_function_parm_tags = TREE_VALUE (parms_info);
4501 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4502 && !in_system_header)
4503 warning ("function declaration isn't a prototype");
4505 if (first_parm != 0
4506 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4508 if (! funcdef_flag)
4509 pedwarn ("parameter names (without types) in function declaration");
4511 last_function_parms = first_parm;
4512 return 0;
4514 else
4516 tree parm;
4517 tree typelt;
4518 /* We no longer test FUNCDEF_FLAG.
4519 If the arg types are incomplete in a declaration,
4520 they must include undefined tags.
4521 These tags can never be defined in the scope of the declaration,
4522 so the types can never be completed,
4523 and no call can be compiled successfully. */
4524 #if 0
4525 /* In a fcn definition, arg types must be complete. */
4526 if (funcdef_flag)
4527 #endif
4528 for (parm = last_function_parms, typelt = first_parm;
4529 parm;
4530 parm = TREE_CHAIN (parm))
4531 /* Skip over any enumeration constants declared here. */
4532 if (TREE_CODE (parm) == PARM_DECL)
4534 /* Barf if the parameter itself has an incomplete type. */
4535 tree type = TREE_VALUE (typelt);
4536 if (type == error_mark_node)
4537 continue;
4538 if (!COMPLETE_TYPE_P (type))
4540 if (funcdef_flag && DECL_NAME (parm) != 0)
4541 error ("parameter `%s' has incomplete type",
4542 IDENTIFIER_POINTER (DECL_NAME (parm)));
4543 else
4544 warning ("parameter has incomplete type");
4545 if (funcdef_flag)
4547 TREE_VALUE (typelt) = error_mark_node;
4548 TREE_TYPE (parm) = error_mark_node;
4551 #if 0
4552 /* This has been replaced by parm_tags_warning, which
4553 uses a more accurate criterion for what to warn
4554 about. */
4555 else
4557 /* Now warn if is a pointer to an incomplete type. */
4558 while (TREE_CODE (type) == POINTER_TYPE
4559 || TREE_CODE (type) == REFERENCE_TYPE)
4560 type = TREE_TYPE (type);
4561 type = TYPE_MAIN_VARIANT (type);
4562 if (!COMPLETE_TYPE_P (type))
4564 if (DECL_NAME (parm) != 0)
4565 warning ("parameter `%s' points to incomplete type",
4566 IDENTIFIER_POINTER (DECL_NAME (parm)));
4567 else
4568 warning ("parameter points to incomplete type");
4571 #endif
4572 typelt = TREE_CHAIN (typelt);
4575 return first_parm;
4579 /* Return a tree_list node with info on a parameter list just parsed.
4580 The TREE_PURPOSE is a chain of decls of those parms.
4581 The TREE_VALUE is a list of structure, union and enum tags defined.
4582 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4583 This tree_list node is later fed to `grokparms'.
4585 VOID_AT_END nonzero means append `void' to the end of the type-list.
4586 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4588 tree
4589 get_parm_info (void_at_end)
4590 int void_at_end;
4592 tree decl, t;
4593 tree types = 0;
4594 int erred = 0;
4595 tree tags = gettags ();
4596 tree parms = getdecls ();
4597 tree new_parms = 0;
4598 tree order = current_binding_level->parm_order;
4600 /* Just `void' (and no ellipsis) is special. There are really no parms.
4601 But if the `void' is qualified (by `const' or `volatile') or has a
4602 storage class specifier (`register'), then the behavior is undefined;
4603 by not counting it as the special case of `void' we will cause an
4604 error later. Typedefs for `void' are OK (see DR#157). */
4605 if (void_at_end && parms != 0
4606 && TREE_CHAIN (parms) == 0
4607 && VOID_TYPE_P (TREE_TYPE (parms))
4608 && ! TREE_THIS_VOLATILE (parms)
4609 && ! TREE_READONLY (parms)
4610 && ! DECL_REGISTER (parms)
4611 && DECL_NAME (parms) == 0)
4613 parms = NULL_TREE;
4614 storedecls (NULL_TREE);
4615 return tree_cons (NULL_TREE, NULL_TREE,
4616 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4619 /* Extract enumerator values and other non-parms declared with the parms.
4620 Likewise any forward parm decls that didn't have real parm decls. */
4621 for (decl = parms; decl;)
4623 tree next = TREE_CHAIN (decl);
4625 if (TREE_CODE (decl) != PARM_DECL)
4627 TREE_CHAIN (decl) = new_parms;
4628 new_parms = decl;
4630 else if (TREE_ASM_WRITTEN (decl))
4632 error_with_decl (decl,
4633 "parameter `%s' has just a forward declaration");
4634 TREE_CHAIN (decl) = new_parms;
4635 new_parms = decl;
4637 decl = next;
4640 /* Put the parm decls back in the order they were in in the parm list. */
4641 for (t = order; t; t = TREE_CHAIN (t))
4643 if (TREE_CHAIN (t))
4644 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4645 else
4646 TREE_CHAIN (TREE_VALUE (t)) = 0;
4649 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4650 new_parms);
4652 /* Store the parmlist in the binding level since the old one
4653 is no longer a valid list. (We have changed the chain pointers.) */
4654 storedecls (new_parms);
4656 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4657 /* There may also be declarations for enumerators if an enumeration
4658 type is declared among the parms. Ignore them here. */
4659 if (TREE_CODE (decl) == PARM_DECL)
4661 /* Since there is a prototype,
4662 args are passed in their declared types. */
4663 tree type = TREE_TYPE (decl);
4664 DECL_ARG_TYPE (decl) = type;
4665 if (PROMOTE_PROTOTYPES
4666 && INTEGRAL_TYPE_P (type)
4667 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4668 DECL_ARG_TYPE (decl) = integer_type_node;
4670 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4671 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4672 && DECL_NAME (decl) == 0)
4674 error ("`void' in parameter list must be the entire list");
4675 erred = 1;
4679 if (void_at_end)
4680 return tree_cons (new_parms, tags,
4681 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4683 return tree_cons (new_parms, tags, nreverse (types));
4686 /* At end of parameter list, warn about any struct, union or enum tags
4687 defined within. Do so because these types cannot ever become complete. */
4689 void
4690 parmlist_tags_warning ()
4692 tree elt;
4693 static int already;
4695 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4697 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4698 /* An anonymous union parm type is meaningful as a GNU extension.
4699 So don't warn for that. */
4700 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4701 continue;
4702 if (TREE_PURPOSE (elt) != 0)
4704 if (code == RECORD_TYPE)
4705 warning ("`struct %s' declared inside parameter list",
4706 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4707 else if (code == UNION_TYPE)
4708 warning ("`union %s' declared inside parameter list",
4709 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4710 else
4711 warning ("`enum %s' declared inside parameter list",
4712 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4714 else
4716 /* For translation these need to be separate warnings */
4717 if (code == RECORD_TYPE)
4718 warning ("anonymous struct declared inside parameter list");
4719 else if (code == UNION_TYPE)
4720 warning ("anonymous union declared inside parameter list");
4721 else
4722 warning ("anonymous enum declared inside parameter list");
4724 if (! already)
4726 warning ("its scope is only this definition or declaration, which is probably not what you want");
4727 already = 1;
4732 /* Get the struct, enum or union (CODE says which) with tag NAME.
4733 Define the tag as a forward-reference if it is not defined. */
4735 tree
4736 xref_tag (code, name)
4737 enum tree_code code;
4738 tree name;
4740 /* If a cross reference is requested, look up the type
4741 already defined for this tag and return it. */
4743 tree ref = lookup_tag (code, name, 0);
4744 /* If this is the right type of tag, return what we found.
4745 (This reference will be shadowed by shadow_tag later if appropriate.)
4746 If this is the wrong type of tag, do not return it. If it was the
4747 wrong type in the same binding level, we will have had an error
4748 message already; if in a different binding level and declaring
4749 a name, pending_xref_error will give an error message; but if in a
4750 different binding level and not declaring a name, this tag should
4751 shadow the previous declaration of a different type of tag, and
4752 this would not work properly if we return the reference found.
4753 (For example, with "struct foo" in an outer scope, "union foo;"
4754 must shadow that tag with a new one of union type.) */
4755 if (ref && TREE_CODE (ref) == code)
4756 return ref;
4758 /* If no such tag is yet defined, create a forward-reference node
4759 and record it as the "definition".
4760 When a real declaration of this type is found,
4761 the forward-reference will be altered into a real type. */
4763 ref = make_node (code);
4764 if (code == ENUMERAL_TYPE)
4766 /* Give the type a default layout like unsigned int
4767 to avoid crashing if it does not get defined. */
4768 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4769 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4770 TYPE_USER_ALIGN (ref) = 0;
4771 TREE_UNSIGNED (ref) = 1;
4772 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4773 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4774 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4777 pushtag (name, ref);
4779 return ref;
4782 /* Make sure that the tag NAME is defined *in the current binding level*
4783 at least as a forward reference.
4784 CODE says which kind of tag NAME ought to be. */
4786 tree
4787 start_struct (code, name)
4788 enum tree_code code;
4789 tree name;
4791 /* If there is already a tag defined at this binding level
4792 (as a forward reference), just return it. */
4794 tree ref = 0;
4796 if (name != 0)
4797 ref = lookup_tag (code, name, 1);
4798 if (ref && TREE_CODE (ref) == code)
4800 if (TYPE_FIELDS (ref))
4802 if (code == UNION_TYPE)
4803 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4804 else
4805 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4808 else
4810 /* Otherwise create a forward-reference just so the tag is in scope. */
4812 ref = make_node (code);
4813 pushtag (name, ref);
4816 C_TYPE_BEING_DEFINED (ref) = 1;
4817 TYPE_PACKED (ref) = flag_pack_struct;
4818 return ref;
4821 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4822 of a structure component, returning a FIELD_DECL node.
4823 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4825 This is done during the parsing of the struct declaration.
4826 The FIELD_DECL nodes are chained together and the lot of them
4827 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4829 tree
4830 grokfield (filename, line, declarator, declspecs, width)
4831 const char *filename ATTRIBUTE_UNUSED;
4832 int line ATTRIBUTE_UNUSED;
4833 tree declarator, declspecs, width;
4835 tree value;
4837 if (declarator == NULL_TREE && width == NULL_TREE)
4839 /* This is an unnamed decl.
4841 If we have something of the form "union { list } ;" then this
4842 is the anonymous union extension. Similarly for struct.
4844 If this is something of the form "struct foo;", then
4845 If MS extensions are enabled, this is handled as an
4846 anonymous struct.
4847 Otherwise this is a forward declaration of a structure tag.
4849 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4850 If MS extensions are enabled and foo names a structure, then
4851 again this is an anonymous struct.
4852 Otherwise this is an error.
4854 Oh what a horrid tangled web we weave. I wonder if MS consciously
4855 took this from Plan 9 or if it was an accident of implementation
4856 that took root before someone noticed the bug... */
4858 tree type = TREE_VALUE (declspecs);
4860 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4861 type = TREE_TYPE (type);
4862 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4864 if (flag_ms_extensions)
4865 ; /* ok */
4866 else if (flag_iso)
4867 goto warn_unnamed_field;
4868 else if (TYPE_NAME (type) == NULL)
4869 ; /* ok */
4870 else
4871 goto warn_unnamed_field;
4873 else
4875 warn_unnamed_field:
4876 warning ("declaration does not declare anything");
4877 return NULL_TREE;
4881 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4883 finish_decl (value, NULL_TREE, NULL_TREE);
4884 DECL_INITIAL (value) = width;
4886 if (flag_objc)
4887 objc_check_decl (value);
4888 return value;
4891 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4892 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4893 ATTRIBUTES are attributes to be applied to the structure. */
4895 tree
4896 finish_struct (t, fieldlist, attributes)
4897 tree t;
4898 tree fieldlist;
4899 tree attributes;
4901 tree x;
4902 int toplevel = global_binding_level == current_binding_level;
4903 int saw_named_field;
4905 /* If this type was previously laid out as a forward reference,
4906 make sure we lay it out again. */
4908 TYPE_SIZE (t) = 0;
4910 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4912 /* Nameless union parm types are useful as GCC extension. */
4913 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4914 /* Otherwise, warn about any struct or union def. in parmlist. */
4915 if (in_parm_level_p ())
4917 if (pedantic)
4918 pedwarn ("%s defined inside parms",
4919 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4920 else
4921 warning ("%s defined inside parms",
4922 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4925 if (pedantic)
4927 for (x = fieldlist; x; x = TREE_CHAIN (x))
4928 if (DECL_NAME (x) != 0)
4929 break;
4931 if (x == 0)
4932 pedwarn ("%s has no %s",
4933 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4934 fieldlist ? _("named members") : _("members"));
4937 /* Install struct as DECL_CONTEXT of each field decl.
4938 Also process specified field sizes,m which is found in the DECL_INITIAL.
4939 Store 0 there, except for ": 0" fields (so we can find them
4940 and delete them, below). */
4942 saw_named_field = 0;
4943 for (x = fieldlist; x; x = TREE_CHAIN (x))
4945 DECL_CONTEXT (x) = t;
4946 DECL_PACKED (x) |= TYPE_PACKED (t);
4948 /* If any field is const, the structure type is pseudo-const. */
4949 if (TREE_READONLY (x))
4950 C_TYPE_FIELDS_READONLY (t) = 1;
4951 else
4953 /* A field that is pseudo-const makes the structure likewise. */
4954 tree t1 = TREE_TYPE (x);
4955 while (TREE_CODE (t1) == ARRAY_TYPE)
4956 t1 = TREE_TYPE (t1);
4957 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4958 && C_TYPE_FIELDS_READONLY (t1))
4959 C_TYPE_FIELDS_READONLY (t) = 1;
4962 /* Any field that is volatile means variables of this type must be
4963 treated in some ways as volatile. */
4964 if (TREE_THIS_VOLATILE (x))
4965 C_TYPE_FIELDS_VOLATILE (t) = 1;
4967 /* Any field of nominal variable size implies structure is too. */
4968 if (C_DECL_VARIABLE_SIZE (x))
4969 C_TYPE_VARIABLE_SIZE (t) = 1;
4971 /* Detect invalid nested redefinition. */
4972 if (TREE_TYPE (x) == t)
4973 error ("nested redefinition of `%s'",
4974 IDENTIFIER_POINTER (TYPE_NAME (t)));
4976 /* Detect invalid bit-field size. */
4977 if (DECL_INITIAL (x))
4978 STRIP_NOPS (DECL_INITIAL (x));
4979 if (DECL_INITIAL (x))
4981 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4982 constant_expression_warning (DECL_INITIAL (x));
4983 else
4985 error_with_decl (x,
4986 "bit-field `%s' width not an integer constant");
4987 DECL_INITIAL (x) = NULL;
4991 /* Detect invalid bit-field type. */
4992 if (DECL_INITIAL (x)
4993 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4994 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4995 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4997 error_with_decl (x, "bit-field `%s' has invalid type");
4998 DECL_INITIAL (x) = NULL;
5001 if (DECL_INITIAL (x) && pedantic
5002 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5003 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5004 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5005 /* Accept an enum that's equivalent to int or unsigned int. */
5006 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5007 && (TYPE_PRECISION (TREE_TYPE (x))
5008 == TYPE_PRECISION (integer_type_node))))
5009 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5011 /* Detect and ignore out of range field width and process valid
5012 field widths. */
5013 if (DECL_INITIAL (x))
5015 int max_width
5016 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5017 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5019 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5020 error_with_decl (x, "negative width in bit-field `%s'");
5021 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5022 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5023 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5024 error_with_decl (x, "zero width for bit-field `%s'");
5025 else
5027 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5028 unsigned HOST_WIDE_INT width
5029 = tree_low_cst (DECL_INITIAL (x), 1);
5031 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5032 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5033 TREE_UNSIGNED (TREE_TYPE (x)))
5034 || (width
5035 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5036 TREE_UNSIGNED (TREE_TYPE (x))))))
5037 warning_with_decl (x,
5038 "`%s' is narrower than values of its type");
5040 DECL_SIZE (x) = bitsize_int (width);
5041 DECL_BIT_FIELD (x) = 1;
5042 SET_DECL_C_BIT_FIELD (x);
5046 DECL_INITIAL (x) = 0;
5048 /* Detect flexible array member in an invalid context. */
5049 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5050 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5051 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5052 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5054 if (TREE_CODE (t) == UNION_TYPE)
5055 error_with_decl (x, "flexible array member in union");
5056 else if (TREE_CHAIN (x) != NULL_TREE)
5057 error_with_decl (x, "flexible array member not at end of struct");
5058 else if (! saw_named_field)
5059 error_with_decl (x, "flexible array member in otherwise empty struct");
5062 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5063 && flexible_array_type_p (TREE_TYPE (x)))
5064 pedwarn_with_decl (x, "invalid use of structure with flexible array member");
5066 if (DECL_NAME (x))
5067 saw_named_field = 1;
5070 /* Delete all duplicate fields from the fieldlist */
5071 for (x = fieldlist; x && TREE_CHAIN (x);)
5072 /* Anonymous fields aren't duplicates. */
5073 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5074 x = TREE_CHAIN (x);
5075 else
5077 tree y = fieldlist;
5079 while (1)
5081 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5082 break;
5083 if (y == x)
5084 break;
5085 y = TREE_CHAIN (y);
5087 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5089 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5090 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5092 else
5093 x = TREE_CHAIN (x);
5096 /* Now we have the nearly final fieldlist. Record it,
5097 then lay out the structure or union (including the fields). */
5099 TYPE_FIELDS (t) = fieldlist;
5101 layout_type (t);
5103 /* Delete all zero-width bit-fields from the fieldlist */
5105 tree *fieldlistp = &fieldlist;
5106 while (*fieldlistp)
5107 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5108 *fieldlistp = TREE_CHAIN (*fieldlistp);
5109 else
5110 fieldlistp = &TREE_CHAIN (*fieldlistp);
5113 /* Now we have the truly final field list.
5114 Store it in this type and in the variants. */
5116 TYPE_FIELDS (t) = fieldlist;
5118 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5120 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5121 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5122 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5123 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5126 /* If this was supposed to be a transparent union, but we can't
5127 make it one, warn and turn off the flag. */
5128 if (TREE_CODE (t) == UNION_TYPE
5129 && TYPE_TRANSPARENT_UNION (t)
5130 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5132 TYPE_TRANSPARENT_UNION (t) = 0;
5133 warning ("union cannot be made transparent");
5136 /* If this structure or union completes the type of any previous
5137 variable declaration, lay it out and output its rtl. */
5139 if (current_binding_level->incomplete_list != NULL_TREE)
5141 tree prev = NULL_TREE;
5143 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5145 tree decl = TREE_VALUE (x);
5147 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5148 && TREE_CODE (decl) != TYPE_DECL)
5150 layout_decl (decl, 0);
5151 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5152 if (flag_objc)
5153 objc_check_decl (decl);
5154 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5155 if (! toplevel)
5156 expand_decl (decl);
5157 /* Unlink X from the incomplete list. */
5158 if (prev)
5159 TREE_CHAIN (prev) = TREE_CHAIN (x);
5160 else
5161 current_binding_level->incomplete_list = TREE_CHAIN (x);
5163 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5164 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5166 tree element = TREE_TYPE (decl);
5167 while (TREE_CODE (element) == ARRAY_TYPE)
5168 element = TREE_TYPE (element);
5169 if (element == t)
5171 layout_array_type (TREE_TYPE (decl));
5172 if (TREE_CODE (decl) != TYPE_DECL)
5174 layout_decl (decl, 0);
5175 if (flag_objc)
5176 objc_check_decl (decl);
5177 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5178 if (! toplevel)
5179 expand_decl (decl);
5181 /* Unlink X from the incomplete list. */
5182 if (prev)
5183 TREE_CHAIN (prev) = TREE_CHAIN (x);
5184 else
5185 current_binding_level->incomplete_list = TREE_CHAIN (x);
5191 /* Finish debugging output for this type. */
5192 rest_of_type_compilation (t, toplevel);
5194 return t;
5197 /* Lay out the type T, and its element type, and so on. */
5199 static void
5200 layout_array_type (t)
5201 tree t;
5203 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5204 layout_array_type (TREE_TYPE (t));
5205 layout_type (t);
5208 /* Begin compiling the definition of an enumeration type.
5209 NAME is its name (or null if anonymous).
5210 Returns the type object, as yet incomplete.
5211 Also records info about it so that build_enumerator
5212 may be used to declare the individual values as they are read. */
5214 tree
5215 start_enum (name)
5216 tree name;
5218 tree enumtype = 0;
5220 /* If this is the real definition for a previous forward reference,
5221 fill in the contents in the same object that used to be the
5222 forward reference. */
5224 if (name != 0)
5225 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5227 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5229 enumtype = make_node (ENUMERAL_TYPE);
5230 pushtag (name, enumtype);
5233 C_TYPE_BEING_DEFINED (enumtype) = 1;
5235 if (TYPE_VALUES (enumtype) != 0)
5237 /* This enum is a named one that has been declared already. */
5238 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5240 /* Completely replace its old definition.
5241 The old enumerators remain defined, however. */
5242 TYPE_VALUES (enumtype) = 0;
5245 enum_next_value = integer_zero_node;
5246 enum_overflow = 0;
5248 if (flag_short_enums)
5249 TYPE_PACKED (enumtype) = 1;
5251 return enumtype;
5254 /* After processing and defining all the values of an enumeration type,
5255 install their decls in the enumeration type and finish it off.
5256 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5257 and ATTRIBUTES are the specified attributes.
5258 Returns ENUMTYPE. */
5260 tree
5261 finish_enum (enumtype, values, attributes)
5262 tree enumtype;
5263 tree values;
5264 tree attributes;
5266 tree pair, tem;
5267 tree minnode = 0, maxnode = 0, enum_value_type;
5268 int precision, unsign;
5269 int toplevel = (global_binding_level == current_binding_level);
5271 if (in_parm_level_p ())
5272 warning ("enum defined inside parms");
5274 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5276 /* Calculate the maximum value of any enumerator in this type. */
5278 if (values == error_mark_node)
5279 minnode = maxnode = integer_zero_node;
5280 else
5282 minnode = maxnode = TREE_VALUE (values);
5283 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5285 tree value = TREE_VALUE (pair);
5286 if (tree_int_cst_lt (maxnode, value))
5287 maxnode = value;
5288 if (tree_int_cst_lt (value, minnode))
5289 minnode = value;
5293 /* Construct the final type of this enumeration. It is the same
5294 as one of the integral types - the narrowest one that fits, except
5295 that normally we only go as narrow as int - and signed iff any of
5296 the values are negative. */
5297 unsign = (tree_int_cst_sgn (minnode) >= 0);
5298 precision = MAX (min_precision (minnode, unsign),
5299 min_precision (maxnode, unsign));
5300 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5302 tree narrowest = c_common_type_for_size (precision, unsign);
5303 if (narrowest == 0)
5305 warning ("enumeration values exceed range of largest integer");
5306 narrowest = long_long_integer_type_node;
5309 precision = TYPE_PRECISION (narrowest);
5311 else
5312 precision = TYPE_PRECISION (integer_type_node);
5314 if (precision == TYPE_PRECISION (integer_type_node))
5315 enum_value_type = c_common_type_for_size (precision, 0);
5316 else
5317 enum_value_type = enumtype;
5319 TYPE_MIN_VALUE (enumtype) = minnode;
5320 TYPE_MAX_VALUE (enumtype) = maxnode;
5321 TYPE_PRECISION (enumtype) = precision;
5322 TREE_UNSIGNED (enumtype) = unsign;
5323 TYPE_SIZE (enumtype) = 0;
5324 layout_type (enumtype);
5326 if (values != error_mark_node)
5328 /* Change the type of the enumerators to be the enum type. We
5329 need to do this irrespective of the size of the enum, for
5330 proper type checking. Replace the DECL_INITIALs of the
5331 enumerators, and the value slots of the list, with copies
5332 that have the enum type; they cannot be modified in place
5333 because they may be shared (e.g. integer_zero_node) Finally,
5334 change the purpose slots to point to the names of the decls. */
5335 for (pair = values; pair; pair = TREE_CHAIN (pair))
5337 tree enu = TREE_PURPOSE (pair);
5339 TREE_TYPE (enu) = enumtype;
5341 /* The ISO C Standard mandates enumerators to have type int,
5342 even though the underlying type of an enum type is
5343 unspecified. Here we convert any enumerators that fit in
5344 an int to type int, to avoid promotions to unsigned types
5345 when comparing integers with enumerators that fit in the
5346 int range. When -pedantic is given, build_enumerator()
5347 would have already taken care of those that don't fit. */
5348 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5349 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5350 else
5351 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5353 TREE_PURPOSE (pair) = DECL_NAME (enu);
5354 TREE_VALUE (pair) = DECL_INITIAL (enu);
5357 TYPE_VALUES (enumtype) = values;
5360 /* Fix up all variant types of this enum type. */
5361 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5363 if (tem == enumtype)
5364 continue;
5365 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5366 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5367 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5368 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5369 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5370 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5371 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5372 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5373 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5374 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5377 /* Finish debugging output for this type. */
5378 rest_of_type_compilation (enumtype, toplevel);
5380 return enumtype;
5383 /* Build and install a CONST_DECL for one value of the
5384 current enumeration type (one that was begun with start_enum).
5385 Return a tree-list containing the CONST_DECL and its value.
5386 Assignment of sequential values by default is handled here. */
5388 tree
5389 build_enumerator (name, value)
5390 tree name, value;
5392 tree decl, type;
5394 /* Validate and default VALUE. */
5396 /* Remove no-op casts from the value. */
5397 if (value)
5398 STRIP_TYPE_NOPS (value);
5400 if (value != 0)
5402 if (TREE_CODE (value) == INTEGER_CST)
5404 value = default_conversion (value);
5405 constant_expression_warning (value);
5407 else
5409 error ("enumerator value for `%s' not integer constant",
5410 IDENTIFIER_POINTER (name));
5411 value = 0;
5415 /* Default based on previous value. */
5416 /* It should no longer be possible to have NON_LVALUE_EXPR
5417 in the default. */
5418 if (value == 0)
5420 value = enum_next_value;
5421 if (enum_overflow)
5422 error ("overflow in enumeration values");
5425 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5427 pedwarn ("ISO C restricts enumerator values to range of `int'");
5428 value = convert (integer_type_node, value);
5431 /* Set basis for default for next value. */
5432 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5433 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5435 /* Now create a declaration for the enum value name. */
5437 type = TREE_TYPE (value);
5438 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5439 TYPE_PRECISION (integer_type_node)),
5440 (TYPE_PRECISION (type)
5441 >= TYPE_PRECISION (integer_type_node)
5442 && TREE_UNSIGNED (type)));
5444 decl = build_decl (CONST_DECL, name, type);
5445 DECL_INITIAL (decl) = convert (type, value);
5446 pushdecl (decl);
5448 return tree_cons (decl, value, NULL_TREE);
5452 /* Create the FUNCTION_DECL for a function definition.
5453 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5454 the declaration; they describe the function's name and the type it returns,
5455 but twisted together in a fashion that parallels the syntax of C.
5457 This function creates a binding context for the function body
5458 as well as setting up the FUNCTION_DECL in current_function_decl.
5460 Returns 1 on success. If the DECLARATOR is not suitable for a function
5461 (it defines a datum instead), we return 0, which tells
5462 yyparse to report a parse error. */
5465 start_function (declspecs, declarator, attributes)
5466 tree declarator, declspecs, attributes;
5468 tree decl1, old_decl;
5469 tree restype;
5470 int old_immediate_size_expand = immediate_size_expand;
5472 current_function_returns_value = 0; /* Assume, until we see it does. */
5473 current_function_returns_null = 0;
5474 current_function_returns_abnormally = 0;
5475 warn_about_return_type = 0;
5476 current_extern_inline = 0;
5477 named_labels = 0;
5478 shadowed_labels = 0;
5480 /* Don't expand any sizes in the return type of the function. */
5481 immediate_size_expand = 0;
5483 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5485 /* If the declarator is not suitable for a function definition,
5486 cause a syntax error. */
5487 if (decl1 == 0)
5489 immediate_size_expand = old_immediate_size_expand;
5490 return 0;
5493 decl_attributes (&decl1, attributes, 0);
5495 /* If #pragma weak was used, mark the decl weak now. */
5496 if (current_binding_level == global_binding_level)
5497 maybe_apply_pragma_weak (decl1);
5499 if (DECL_DECLARED_INLINE_P (decl1)
5500 && DECL_UNINLINABLE (decl1)
5501 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5502 warning_with_decl (decl1,
5503 "inline function `%s' given attribute noinline");
5505 announce_function (decl1);
5507 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5509 error ("return type is an incomplete type");
5510 /* Make it return void instead. */
5511 TREE_TYPE (decl1)
5512 = build_function_type (void_type_node,
5513 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5516 if (warn_about_return_type)
5517 pedwarn_c99 ("return type defaults to `int'");
5519 /* Save the parm names or decls from this function's declarator
5520 where store_parm_decls will find them. */
5521 current_function_parms = last_function_parms;
5522 current_function_parm_tags = last_function_parm_tags;
5524 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5525 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5526 DECL_INITIAL (decl1) = error_mark_node;
5528 /* If this definition isn't a prototype and we had a prototype declaration
5529 before, copy the arg type info from that prototype.
5530 But not if what we had before was a builtin function. */
5531 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5532 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5533 && !DECL_BUILT_IN (old_decl)
5534 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5535 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5536 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5538 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5539 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5540 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5543 /* Optionally warn of old-fashioned def with no previous prototype. */
5544 if (warn_strict_prototypes
5545 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5546 && C_DECL_ISNT_PROTOTYPE (old_decl))
5547 warning ("function declaration isn't a prototype");
5548 /* Optionally warn of any global def with no previous prototype. */
5549 else if (warn_missing_prototypes
5550 && TREE_PUBLIC (decl1)
5551 && ! MAIN_NAME_P (DECL_NAME (decl1))
5552 && C_DECL_ISNT_PROTOTYPE (old_decl))
5553 warning_with_decl (decl1, "no previous prototype for `%s'");
5554 /* Optionally warn of any def with no previous prototype
5555 if the function has already been used. */
5556 else if (warn_missing_prototypes
5557 && old_decl != 0 && TREE_USED (old_decl)
5558 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5559 warning_with_decl (decl1,
5560 "`%s' was used with no prototype before its definition");
5561 /* Optionally warn of any global def with no previous declaration. */
5562 else if (warn_missing_declarations
5563 && TREE_PUBLIC (decl1)
5564 && old_decl == 0
5565 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5566 warning_with_decl (decl1, "no previous declaration for `%s'");
5567 /* Optionally warn of any def with no previous declaration
5568 if the function has already been used. */
5569 else if (warn_missing_declarations
5570 && old_decl != 0 && TREE_USED (old_decl)
5571 && C_DECL_IMPLICIT (old_decl))
5572 warning_with_decl (decl1,
5573 "`%s' was used with no declaration before its definition");
5575 /* This is a definition, not a reference.
5576 So normally clear DECL_EXTERNAL.
5577 However, `extern inline' acts like a declaration
5578 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5579 DECL_EXTERNAL (decl1) = current_extern_inline;
5581 /* This function exists in static storage.
5582 (This does not mean `static' in the C sense!) */
5583 TREE_STATIC (decl1) = 1;
5585 /* A nested function is not global. */
5586 if (current_function_decl != 0)
5587 TREE_PUBLIC (decl1) = 0;
5589 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5590 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5592 tree args;
5593 int argct = 0;
5595 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5596 != integer_type_node)
5597 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5599 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5600 args = TREE_CHAIN (args))
5602 tree type = args ? TREE_VALUE (args) : 0;
5604 if (type == void_type_node)
5605 break;
5607 ++argct;
5608 switch (argct)
5610 case 1:
5611 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5612 pedwarn_with_decl (decl1,
5613 "first argument of `%s' should be `int'");
5614 break;
5616 case 2:
5617 if (TREE_CODE (type) != POINTER_TYPE
5618 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5619 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5620 != char_type_node))
5621 pedwarn_with_decl (decl1,
5622 "second argument of `%s' should be `char **'");
5623 break;
5625 case 3:
5626 if (TREE_CODE (type) != POINTER_TYPE
5627 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5628 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5629 != char_type_node))
5630 pedwarn_with_decl (decl1,
5631 "third argument of `%s' should probably be `char **'");
5632 break;
5636 /* It is intentional that this message does not mention the third
5637 argument because it's only mentioned in an appendix of the
5638 standard. */
5639 if (argct > 0 && (argct < 2 || argct > 3))
5640 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5642 if (! TREE_PUBLIC (decl1))
5643 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5646 /* Record the decl so that the function name is defined.
5647 If we already have a decl for this name, and it is a FUNCTION_DECL,
5648 use the old decl. */
5650 current_function_decl = pushdecl (decl1);
5652 pushlevel (0);
5653 declare_parm_level (1);
5655 make_decl_rtl (current_function_decl, NULL);
5657 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5658 /* Promote the value to int before returning it. */
5659 if (c_promoting_integer_type_p (restype))
5661 /* It retains unsignedness if not really getting wider. */
5662 if (TREE_UNSIGNED (restype)
5663 && (TYPE_PRECISION (restype)
5664 == TYPE_PRECISION (integer_type_node)))
5665 restype = unsigned_type_node;
5666 else
5667 restype = integer_type_node;
5669 DECL_RESULT (current_function_decl)
5670 = build_decl (RESULT_DECL, NULL_TREE, restype);
5672 /* If this fcn was already referenced via a block-scope `extern' decl
5673 (or an implicit decl), propagate certain information about the usage. */
5674 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5675 TREE_ADDRESSABLE (current_function_decl) = 1;
5677 immediate_size_expand = old_immediate_size_expand;
5679 start_fname_decls ();
5681 return 1;
5684 /* Store the parameter declarations into the current function declaration.
5685 This is called after parsing the parameter declarations, before
5686 digesting the body of the function.
5688 For an old-style definition, modify the function's type
5689 to specify at least the number of arguments. */
5691 void
5692 store_parm_decls ()
5694 tree fndecl = current_function_decl;
5695 tree parm;
5697 /* This is either a chain of PARM_DECLs (if a prototype was used)
5698 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5699 tree specparms = current_function_parms;
5701 /* This is a list of types declared among parms in a prototype. */
5702 tree parmtags = current_function_parm_tags;
5704 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5705 tree parmdecls = getdecls ();
5707 /* This is a chain of any other decls that came in among the parm
5708 declarations. If a parm is declared with enum {foo, bar} x;
5709 then CONST_DECLs for foo and bar are put here. */
5710 tree nonparms = 0;
5712 /* The function containing FNDECL, if any. */
5713 tree context = decl_function_context (fndecl);
5715 /* Nonzero if this definition is written with a prototype. */
5716 int prototype = 0;
5718 int saved_warn_shadow = warn_shadow;
5720 /* Don't re-emit shadow warnings. */
5721 warn_shadow = 0;
5723 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5725 /* This case is when the function was defined with an ANSI prototype.
5726 The parms already have decls, so we need not do anything here
5727 except record them as in effect
5728 and complain if any redundant old-style parm decls were written. */
5730 tree next;
5731 tree others = 0;
5733 prototype = 1;
5735 if (parmdecls != 0)
5737 tree decl, link;
5739 error_with_decl (fndecl,
5740 "parm types given both in parmlist and separately");
5741 /* Get rid of the erroneous decls; don't keep them on
5742 the list of parms, since they might not be PARM_DECLs. */
5743 for (decl = current_binding_level->names;
5744 decl; decl = TREE_CHAIN (decl))
5745 if (DECL_NAME (decl))
5746 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = 0;
5747 for (link = current_binding_level->shadowed;
5748 link; link = TREE_CHAIN (link))
5749 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5750 current_binding_level->names = 0;
5751 current_binding_level->shadowed = 0;
5754 specparms = nreverse (specparms);
5755 for (parm = specparms; parm; parm = next)
5757 next = TREE_CHAIN (parm);
5758 if (TREE_CODE (parm) == PARM_DECL)
5760 if (DECL_NAME (parm) == 0)
5761 error_with_decl (parm, "parameter name omitted");
5762 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5763 && VOID_TYPE_P (TREE_TYPE (parm)))
5765 error_with_decl (parm, "parameter `%s' declared void");
5766 /* Change the type to error_mark_node so this parameter
5767 will be ignored by assign_parms. */
5768 TREE_TYPE (parm) = error_mark_node;
5770 pushdecl (parm);
5772 else
5774 /* If we find an enum constant or a type tag,
5775 put it aside for the moment. */
5776 TREE_CHAIN (parm) = 0;
5777 others = chainon (others, parm);
5781 /* Get the decls in their original chain order
5782 and record in the function. */
5783 DECL_ARGUMENTS (fndecl) = getdecls ();
5785 #if 0
5786 /* If this function takes a variable number of arguments,
5787 add a phony parameter to the end of the parm list,
5788 to represent the position of the first unnamed argument. */
5789 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
5790 != void_type_node)
5792 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
5793 /* Let's hope the address of the unnamed parm
5794 won't depend on its type. */
5795 TREE_TYPE (dummy) = integer_type_node;
5796 DECL_ARG_TYPE (dummy) = integer_type_node;
5797 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
5799 #endif
5801 /* Now pushdecl the enum constants. */
5802 for (parm = others; parm; parm = next)
5804 next = TREE_CHAIN (parm);
5805 if (DECL_NAME (parm) == 0)
5807 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5809 else if (TREE_CODE (parm) != PARM_DECL)
5810 pushdecl (parm);
5813 storetags (chainon (parmtags, gettags ()));
5815 else
5817 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5818 each with a parm name as the TREE_VALUE.
5820 PARMDECLS is a chain of declarations for parameters.
5821 Warning! It can also contain CONST_DECLs which are not parameters
5822 but are names of enumerators of any enum types
5823 declared among the parameters.
5825 First match each formal parameter name with its declaration.
5826 Associate decls with the names and store the decls
5827 into the TREE_PURPOSE slots. */
5829 /* We use DECL_WEAK as a flag to show which parameters have been
5830 seen already since it is not used on PARM_DECL or CONST_DECL. */
5831 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5832 DECL_WEAK (parm) = 0;
5834 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5836 tree tail, found = NULL;
5838 if (TREE_VALUE (parm) == 0)
5840 error_with_decl (fndecl,
5841 "parameter name missing from parameter list");
5842 TREE_PURPOSE (parm) = 0;
5843 continue;
5846 /* See if any of the parmdecls specifies this parm by name.
5847 Ignore any enumerator decls. */
5848 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5849 if (DECL_NAME (tail) == TREE_VALUE (parm)
5850 && TREE_CODE (tail) == PARM_DECL)
5852 found = tail;
5853 break;
5856 /* If declaration already marked, we have a duplicate name.
5857 Complain, and don't use this decl twice. */
5858 if (found && DECL_WEAK (found))
5860 error_with_decl (found, "multiple parameters named `%s'");
5861 found = 0;
5864 /* If the declaration says "void", complain and ignore it. */
5865 if (found && VOID_TYPE_P (TREE_TYPE (found)))
5867 error_with_decl (found, "parameter `%s' declared void");
5868 TREE_TYPE (found) = integer_type_node;
5869 DECL_ARG_TYPE (found) = integer_type_node;
5870 layout_decl (found, 0);
5873 /* If no declaration found, default to int. */
5874 if (!found)
5876 found = build_decl (PARM_DECL, TREE_VALUE (parm),
5877 integer_type_node);
5878 DECL_ARG_TYPE (found) = TREE_TYPE (found);
5879 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
5880 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
5881 if (flag_isoc99)
5882 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
5883 else if (extra_warnings)
5884 warning_with_decl (found, "type of `%s' defaults to `int'");
5885 pushdecl (found);
5888 TREE_PURPOSE (parm) = found;
5890 /* Mark this decl as "already found". */
5891 DECL_WEAK (found) = 1;
5894 /* Put anything which is on the parmdecls chain and which is
5895 not a PARM_DECL onto the list NONPARMS. (The types of
5896 non-parm things which might appear on the list include
5897 enumerators and NULL-named TYPE_DECL nodes.) Complain about
5898 any actual PARM_DECLs not matched with any names. */
5900 nonparms = 0;
5901 for (parm = parmdecls; parm;)
5903 tree next = TREE_CHAIN (parm);
5904 TREE_CHAIN (parm) = 0;
5906 if (TREE_CODE (parm) != PARM_DECL)
5907 nonparms = chainon (nonparms, parm);
5908 else
5910 /* Complain about args with incomplete types. */
5911 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5913 error_with_decl (parm, "parameter `%s' has incomplete type");
5914 TREE_TYPE (parm) = error_mark_node;
5917 if (! DECL_WEAK (parm))
5919 error_with_decl (parm,
5920 "declaration for parameter `%s' but no such parameter");
5921 /* Pretend the parameter was not missing.
5922 This gets us to a standard state and minimizes
5923 further error messages. */
5924 specparms
5925 = chainon (specparms,
5926 tree_cons (parm, NULL_TREE, NULL_TREE));
5930 parm = next;
5933 /* Chain the declarations together in the order of the list of
5934 names. Store that chain in the function decl, replacing the
5935 list of names. */
5936 parm = specparms;
5937 DECL_ARGUMENTS (fndecl) = 0;
5939 tree last;
5940 for (last = 0; parm; parm = TREE_CHAIN (parm))
5941 if (TREE_PURPOSE (parm))
5943 if (last == 0)
5944 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
5945 else
5946 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5947 last = TREE_PURPOSE (parm);
5948 TREE_CHAIN (last) = 0;
5952 /* If there was a previous prototype,
5953 set the DECL_ARG_TYPE of each argument according to
5954 the type previously specified, and report any mismatches. */
5956 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5958 tree type;
5959 for (parm = DECL_ARGUMENTS (fndecl),
5960 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5961 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5962 != void_type_node));
5963 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5965 if (parm == 0 || type == 0
5966 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5968 error ("number of arguments doesn't match prototype");
5969 error_with_file_and_line (current_function_prototype_file,
5970 current_function_prototype_line,
5971 "prototype declaration");
5972 break;
5974 /* Type for passing arg must be consistent with that
5975 declared for the arg. ISO C says we take the unqualified
5976 type for parameters declared with qualified type. */
5977 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5978 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
5980 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5981 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5983 /* Adjust argument to match prototype. E.g. a previous
5984 `int foo(float);' prototype causes
5985 `int foo(x) float x; {...}' to be treated like
5986 `int foo(float x) {...}'. This is particularly
5987 useful for argument types like uid_t. */
5988 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5990 if (PROMOTE_PROTOTYPES
5991 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5992 && TYPE_PRECISION (TREE_TYPE (parm))
5993 < TYPE_PRECISION (integer_type_node))
5994 DECL_ARG_TYPE (parm) = integer_type_node;
5996 if (pedantic)
5998 pedwarn ("promoted argument `%s' doesn't match prototype",
5999 IDENTIFIER_POINTER (DECL_NAME (parm)));
6000 warning_with_file_and_line
6001 (current_function_prototype_file,
6002 current_function_prototype_line,
6003 "prototype declaration");
6006 else
6008 error ("argument `%s' doesn't match prototype",
6009 IDENTIFIER_POINTER (DECL_NAME (parm)));
6010 error_with_file_and_line (current_function_prototype_file,
6011 current_function_prototype_line,
6012 "prototype declaration");
6016 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6019 /* Otherwise, create a prototype that would match. */
6021 else
6023 tree actual = 0, last = 0, type;
6025 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6027 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6028 if (last)
6029 TREE_CHAIN (last) = type;
6030 else
6031 actual = type;
6032 last = type;
6034 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6035 if (last)
6036 TREE_CHAIN (last) = type;
6037 else
6038 actual = type;
6040 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6041 of the type of this function, but we need to avoid having this
6042 affect the types of other similarly-typed functions, so we must
6043 first force the generation of an identical (but separate) type
6044 node for the relevant function type. The new node we create
6045 will be a variant of the main variant of the original function
6046 type. */
6048 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6050 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6053 /* Now store the final chain of decls for the arguments
6054 as the decl-chain of the current lexical scope.
6055 Put the enumerators in as well, at the front so that
6056 DECL_ARGUMENTS is not modified. */
6058 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6061 /* Make sure the binding level for the top of the function body
6062 gets a BLOCK if there are any in the function.
6063 Otherwise, the dbx output is wrong. */
6065 keep_next_if_subblocks = 1;
6067 /* ??? This might be an improvement,
6068 but needs to be thought about some more. */
6069 #if 0
6070 keep_next_level_flag = 1;
6071 #endif
6073 /* Write a record describing this function definition to the prototypes
6074 file (if requested). */
6076 gen_aux_info_record (fndecl, 1, 0, prototype);
6078 /* Initialize the RTL code for the function. */
6079 init_function_start (fndecl, input_filename, input_line);
6081 /* Begin the statement tree for this function. */
6082 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6084 /* If this is a nested function, save away the sizes of any
6085 variable-size types so that we can expand them when generating
6086 RTL. */
6087 if (context)
6089 tree t;
6091 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6092 = nreverse (get_pending_sizes ());
6093 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6095 t = TREE_CHAIN (t))
6096 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6099 /* This function is being processed in whole-function mode. */
6100 cfun->x_whole_function_mode_p = 1;
6102 /* Even though we're inside a function body, we still don't want to
6103 call expand_expr to calculate the size of a variable-sized array.
6104 We haven't necessarily assigned RTL to all variables yet, so it's
6105 not safe to try to expand expressions involving them. */
6106 immediate_size_expand = 0;
6107 cfun->x_dont_save_pending_sizes_p = 1;
6109 warn_shadow = saved_warn_shadow;
6112 /* Finish up a function declaration and compile that function
6113 all the way to assembler language output. The free the storage
6114 for the function definition.
6116 This is called after parsing the body of the function definition.
6118 NESTED is nonzero if the function being finished is nested in another.
6119 CAN_DEFER_P is nonzero if the function may be deferred. */
6121 void
6122 finish_function (nested, can_defer_p)
6123 int nested;
6124 int can_defer_p;
6126 tree fndecl = current_function_decl;
6128 /* When a function declaration is totally empty, e.g.
6129 void foo(void) { }
6130 (the argument list is irrelevant) the compstmt rule will not
6131 bother calling pushlevel/poplevel, which means we get here with
6132 the binding_level stack out of sync. Detect this situation by
6133 noticing that the current_binding_level is still as
6134 store_parm_decls left it, and do a dummy push/pop to get back to
6135 consistency. Note that the call to pushlevel does not actually
6136 push another binding level - see there for details. */
6137 if (current_binding_level->parm_flag && keep_next_if_subblocks)
6139 pushlevel (0);
6140 poplevel (1, 0, 1);
6143 #if 0
6144 /* This caused &foo to be of type ptr-to-const-function which then
6145 got a warning when stored in a ptr-to-function variable. */
6146 TREE_READONLY (fndecl) = 1;
6147 #endif
6149 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6151 /* Must mark the RESULT_DECL as being in this function. */
6153 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6155 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6157 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6158 != integer_type_node)
6160 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6161 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6162 if (! warn_main)
6163 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6165 else
6167 #ifdef DEFAULT_MAIN_RETURN
6168 /* Make it so that `main' always returns success by default. */
6169 DEFAULT_MAIN_RETURN;
6170 #else
6171 if (flag_isoc99)
6172 c_expand_return (integer_zero_node);
6173 #endif
6177 finish_fname_decls ();
6179 /* Tie off the statement tree for this function. */
6180 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6182 /* Complain if there's just no return statement. */
6183 if (warn_return_type
6184 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6185 && !current_function_returns_value && !current_function_returns_null
6186 /* Don't complain if we abort. */
6187 && !current_function_returns_abnormally
6188 /* Don't warn for main(). */
6189 && !MAIN_NAME_P (DECL_NAME (fndecl))
6190 /* Or if they didn't actually specify a return type. */
6191 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6192 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6193 inline function, as we might never be compiled separately. */
6194 && DECL_INLINE (fndecl))
6195 warning ("no return statement in function returning non-void");
6197 /* Clear out memory we no longer need. */
6198 free_after_parsing (cfun);
6199 /* Since we never call rest_of_compilation, we never clear
6200 CFUN. Do so explicitly. */
6201 free_after_compilation (cfun);
6202 cfun = NULL;
6204 if (flag_unit_at_a_time && can_defer_p)
6206 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6207 current_function_decl = NULL;
6208 return;
6211 if (! nested)
6213 /* Function is parsed.
6214 Generate RTL for the body of this function or defer
6215 it for later expansion. */
6216 int uninlinable = 1;
6218 /* There's no reason to do any of the work here if we're only doing
6219 semantic analysis; this code just generates RTL. */
6220 if (flag_syntax_only)
6222 current_function_decl = NULL;
6223 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6224 return;
6227 if (flag_inline_trees)
6229 /* First, cache whether the current function is inlinable. Some
6230 predicates depend on cfun and current_function_decl to
6231 function completely. */
6232 timevar_push (TV_INTEGRATION);
6233 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6235 if (can_defer_p
6236 /* We defer functions marked inline *even if* the function
6237 itself is not inlinable. This is because we don't yet
6238 know if the function will actually be used; we may be
6239 able to avoid emitting it entirely. */
6240 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6241 /* Save function tree for inlining. Should return 0 if the
6242 language does not support function deferring or the
6243 function could not be deferred. */
6244 && defer_fn (fndecl))
6246 /* Let the back-end know that this function exists. */
6247 (*debug_hooks->deferred_inline_function) (fndecl);
6248 timevar_pop (TV_INTEGRATION);
6249 current_function_decl = NULL;
6250 return;
6253 /* Then, inline any functions called in it. */
6254 optimize_inline_calls (fndecl);
6255 timevar_pop (TV_INTEGRATION);
6258 c_expand_body (fndecl);
6260 /* Keep the function body if it's needed for inlining or dumping. */
6261 if (uninlinable && !dump_enabled_p (TDI_all))
6263 /* Allow the body of the function to be garbage collected. */
6264 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6267 /* Let the error reporting routines know that we're outside a
6268 function. For a nested function, this value is used in
6269 c_pop_function_context and then reset via pop_function_context. */
6270 current_function_decl = NULL;
6274 /* Generate the RTL for a deferred function FNDECL. */
6276 void
6277 c_expand_deferred_function (fndecl)
6278 tree fndecl;
6280 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6281 function was deferred, e.g. in duplicate_decls. */
6282 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6284 if (flag_inline_trees)
6286 timevar_push (TV_INTEGRATION);
6287 optimize_inline_calls (fndecl);
6288 timevar_pop (TV_INTEGRATION);
6290 c_expand_body (fndecl);
6291 current_function_decl = NULL;
6295 /* Called to move the SAVE_EXPRs for parameter declarations in a
6296 nested function into the nested function. DATA is really the
6297 nested FUNCTION_DECL. */
6299 static tree
6300 set_save_expr_context (tree *tp,
6301 int *walk_subtrees,
6302 void *data)
6304 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6305 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6306 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6307 circularity. */
6308 else if (DECL_P (*tp))
6309 *walk_subtrees = 0;
6311 return NULL_TREE;
6314 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6315 then we are already in the process of generating RTL for another
6316 function. If can_defer_p is zero, we won't attempt to defer the
6317 generation of RTL. */
6319 static void
6320 c_expand_body_1 (fndecl, nested_p)
6321 tree fndecl;
6322 int nested_p;
6324 timevar_push (TV_EXPAND);
6326 if (nested_p)
6328 /* Make sure that we will evaluate variable-sized types involved
6329 in our function's type. */
6330 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6331 /* Squirrel away our current state. */
6332 push_function_context ();
6335 /* Initialize the RTL code for the function. */
6336 current_function_decl = fndecl;
6337 input_location = DECL_SOURCE_LOCATION (fndecl);
6338 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6340 /* This function is being processed in whole-function mode. */
6341 cfun->x_whole_function_mode_p = 1;
6343 /* Even though we're inside a function body, we still don't want to
6344 call expand_expr to calculate the size of a variable-sized array.
6345 We haven't necessarily assigned RTL to all variables yet, so it's
6346 not safe to try to expand expressions involving them. */
6347 immediate_size_expand = 0;
6348 cfun->x_dont_save_pending_sizes_p = 1;
6350 /* Set up parameters and prepare for return, for the function. */
6351 expand_function_start (fndecl, 0);
6353 /* If the function has a variably modified type, there may be
6354 SAVE_EXPRs in the parameter types. Their context must be set to
6355 refer to this function; they cannot be expanded in the containing
6356 function. */
6357 if (decl_function_context (fndecl)
6358 && variably_modified_type_p (TREE_TYPE (fndecl)))
6359 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6360 NULL);
6362 /* If this function is `main', emit a call to `__main'
6363 to run global initializers, etc. */
6364 if (DECL_NAME (fndecl)
6365 && MAIN_NAME_P (DECL_NAME (fndecl))
6366 && DECL_CONTEXT (fndecl) == NULL_TREE)
6367 expand_main_function ();
6369 /* Generate the RTL for this function. */
6370 expand_stmt (DECL_SAVED_TREE (fndecl));
6372 /* We hard-wired immediate_size_expand to zero above.
6373 expand_function_end will decrement this variable. So, we set the
6374 variable to one here, so that after the decrement it will remain
6375 zero. */
6376 immediate_size_expand = 1;
6378 /* Allow language dialects to perform special processing. */
6379 if (lang_expand_function_end)
6380 (*lang_expand_function_end) ();
6382 /* Generate rtl for function exit. */
6383 expand_function_end (input_filename, input_line, 0);
6385 /* If this is a nested function, protect the local variables in the stack
6386 above us from being collected while we're compiling this function. */
6387 if (nested_p)
6388 ggc_push_context ();
6390 /* Run the optimizers and output the assembler code for this function. */
6391 rest_of_compilation (fndecl);
6393 /* Undo the GC context switch. */
6394 if (nested_p)
6395 ggc_pop_context ();
6397 /* With just -Wextra, complain only if function returns both with
6398 and without a value. */
6399 if (extra_warnings
6400 && current_function_returns_value
6401 && current_function_returns_null)
6402 warning ("this function may return with or without a value");
6404 /* If requested, warn about function definitions where the function will
6405 return a value (usually of some struct or union type) which itself will
6406 take up a lot of stack space. */
6408 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6410 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6412 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6413 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6414 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6415 larger_than_size))
6417 unsigned int size_as_int
6418 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6420 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6421 warning_with_decl (fndecl,
6422 "size of return value of `%s' is %u bytes",
6423 size_as_int);
6424 else
6425 warning_with_decl (fndecl,
6426 "size of return value of `%s' is larger than %d bytes",
6427 larger_than_size);
6431 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6432 && ! flag_inline_trees)
6434 /* Stop pointing to the local nodes about to be freed.
6435 But DECL_INITIAL must remain nonzero so we know this
6436 was an actual function definition.
6437 For a nested function, this is done in c_pop_function_context.
6438 If rest_of_compilation set this to 0, leave it 0. */
6439 if (DECL_INITIAL (fndecl) != 0)
6440 DECL_INITIAL (fndecl) = error_mark_node;
6442 DECL_ARGUMENTS (fndecl) = 0;
6445 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6447 if (targetm.have_ctors_dtors)
6448 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6449 DEFAULT_INIT_PRIORITY);
6450 else
6451 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6454 if (DECL_STATIC_DESTRUCTOR (fndecl))
6456 if (targetm.have_ctors_dtors)
6457 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6458 DEFAULT_INIT_PRIORITY);
6459 else
6460 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6463 if (nested_p)
6464 /* Return to the enclosing function. */
6465 pop_function_context ();
6466 timevar_pop (TV_EXPAND);
6469 /* Like c_expand_body_1 but only for unnested functions. */
6471 void
6472 c_expand_body (fndecl)
6473 tree fndecl;
6475 c_expand_body_1 (fndecl, 0);
6478 /* Check the declarations given in a for-loop for satisfying the C99
6479 constraints. */
6480 void
6481 check_for_loop_decls ()
6483 tree t;
6485 if (!flag_isoc99)
6487 /* If we get here, declarations have been used in a for loop without
6488 the C99 for loop scope. This doesn't make much sense, so don't
6489 allow it. */
6490 error ("`for' loop initial declaration used outside C99 mode");
6491 return;
6493 /* C99 subclause 6.8.5 paragraph 3:
6495 [#3] The declaration part of a for statement shall only
6496 declare identifiers for objects having storage class auto or
6497 register.
6499 It isn't clear whether, in this sentence, "identifiers" binds to
6500 "shall only declare" or to "objects" - that is, whether all identifiers
6501 declared must be identifiers for objects, or whether the restriction
6502 only applies to those that are. (A question on this in comp.std.c
6503 in November 2000 received no answer.) We implement the strictest
6504 interpretation, to avoid creating an extension which later causes
6505 problems. */
6507 for (t = gettags (); t; t = TREE_CHAIN (t))
6509 if (TREE_PURPOSE (t) != 0)
6511 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6513 if (code == RECORD_TYPE)
6514 error ("`struct %s' declared in `for' loop initial declaration",
6515 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6516 else if (code == UNION_TYPE)
6517 error ("`union %s' declared in `for' loop initial declaration",
6518 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6519 else
6520 error ("`enum %s' declared in `for' loop initial declaration",
6521 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6525 for (t = getdecls (); t; t = TREE_CHAIN (t))
6527 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6528 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6529 else if (TREE_STATIC (t))
6530 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6531 else if (DECL_EXTERNAL (t))
6532 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6536 /* Save and restore the variables in this file and elsewhere
6537 that keep track of the progress of compilation of the current function.
6538 Used for nested functions. */
6540 struct language_function GTY(())
6542 struct c_language_function base;
6543 tree named_labels;
6544 tree shadowed_labels;
6545 int returns_value;
6546 int returns_null;
6547 int returns_abnormally;
6548 int warn_about_return_type;
6549 int extern_inline;
6550 struct binding_level *binding_level;
6553 /* Save and reinitialize the variables
6554 used during compilation of a C function. */
6556 void
6557 c_push_function_context (f)
6558 struct function *f;
6560 struct language_function *p;
6561 p = ((struct language_function *)
6562 ggc_alloc (sizeof (struct language_function)));
6563 f->language = p;
6565 p->base.x_stmt_tree = c_stmt_tree;
6566 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6567 p->named_labels = named_labels;
6568 p->shadowed_labels = shadowed_labels;
6569 p->returns_value = current_function_returns_value;
6570 p->returns_null = current_function_returns_null;
6571 p->returns_abnormally = current_function_returns_abnormally;
6572 p->warn_about_return_type = warn_about_return_type;
6573 p->extern_inline = current_extern_inline;
6574 p->binding_level = current_binding_level;
6577 /* Restore the variables used during compilation of a C function. */
6579 void
6580 c_pop_function_context (f)
6581 struct function *f;
6583 struct language_function *p = f->language;
6584 tree link;
6586 /* Bring back all the labels that were shadowed. */
6587 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6588 if (DECL_NAME (TREE_VALUE (link)) != 0)
6589 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6590 = TREE_VALUE (link);
6592 if (DECL_SAVED_INSNS (current_function_decl) == 0
6593 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6595 /* Stop pointing to the local nodes about to be freed. */
6596 /* But DECL_INITIAL must remain nonzero so we know this
6597 was an actual function definition. */
6598 DECL_INITIAL (current_function_decl) = error_mark_node;
6599 DECL_ARGUMENTS (current_function_decl) = 0;
6602 c_stmt_tree = p->base.x_stmt_tree;
6603 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6604 named_labels = p->named_labels;
6605 shadowed_labels = p->shadowed_labels;
6606 current_function_returns_value = p->returns_value;
6607 current_function_returns_null = p->returns_null;
6608 current_function_returns_abnormally = p->returns_abnormally;
6609 warn_about_return_type = p->warn_about_return_type;
6610 current_extern_inline = p->extern_inline;
6611 current_binding_level = p->binding_level;
6613 f->language = NULL;
6616 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6618 void
6619 c_dup_lang_specific_decl (decl)
6620 tree decl;
6622 struct lang_decl *ld;
6624 if (!DECL_LANG_SPECIFIC (decl))
6625 return;
6627 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6628 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6629 sizeof (struct lang_decl));
6630 DECL_LANG_SPECIFIC (decl) = ld;
6633 /* The functions below are required for functionality of doing
6634 function at once processing in the C front end. Currently these
6635 functions are not called from anywhere in the C front end, but as
6636 these changes continue, that will change. */
6638 /* Returns nonzero if the current statement is a full expression,
6639 i.e. temporaries created during that statement should be destroyed
6640 at the end of the statement. */
6643 stmts_are_full_exprs_p ()
6645 return 0;
6648 /* Returns the stmt_tree (if any) to which statements are currently
6649 being added. If there is no active statement-tree, NULL is
6650 returned. */
6652 stmt_tree
6653 current_stmt_tree ()
6655 return &c_stmt_tree;
6658 /* Returns the stack of SCOPE_STMTs for the current function. */
6660 tree *
6661 current_scope_stmt_stack ()
6663 return &c_scope_stmt_stack;
6666 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6667 C. */
6670 anon_aggr_type_p (node)
6671 tree node ATTRIBUTE_UNUSED;
6673 return 0;
6676 /* Dummy function in place of callback used by C++. */
6678 void
6679 extract_interface_info ()
6683 /* Return a new COMPOUND_STMT, after adding it to the current
6684 statement tree. */
6686 tree
6687 c_begin_compound_stmt ()
6689 tree stmt;
6691 /* Create the COMPOUND_STMT. */
6692 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6694 return stmt;
6697 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6698 common code. */
6700 void
6701 c_expand_decl_stmt (t)
6702 tree t;
6704 tree decl = DECL_STMT_DECL (t);
6706 /* Expand nested functions. */
6707 if (TREE_CODE (decl) == FUNCTION_DECL
6708 && DECL_CONTEXT (decl) == current_function_decl
6709 && DECL_SAVED_TREE (decl))
6710 c_expand_body_1 (decl, 1);
6713 /* Return the global value of T as a symbol. */
6715 tree
6716 identifier_global_value (t)
6717 tree t;
6719 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6720 if (decl == 0 || DECL_CONTEXT (decl) == 0)
6721 return decl;
6723 /* Shadowed by something else; find the true global value. */
6724 for (decl = global_binding_level->names; decl; decl = TREE_CHAIN (decl))
6725 if (DECL_NAME (decl) == t)
6726 return decl;
6728 /* Only local values for this decl. */
6729 return 0;
6732 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6733 otherwise the name is found in ridpointers from RID_INDEX. */
6735 void
6736 record_builtin_type (rid_index, name, type)
6737 enum rid rid_index;
6738 const char *name;
6739 tree type;
6741 tree id;
6742 if (name == 0)
6743 id = ridpointers[(int) rid_index];
6744 else
6745 id = get_identifier (name);
6746 pushdecl (build_decl (TYPE_DECL, id, type));
6749 /* Build the void_list_node (void_type_node having been created). */
6750 tree
6751 build_void_list_node ()
6753 tree t = build_tree_list (NULL_TREE, void_type_node);
6754 return t;
6757 /* Return something to represent absolute declarators containing a *.
6758 TARGET is the absolute declarator that the * contains.
6759 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6760 to apply to the pointer type, represented as identifiers, possible mixed
6761 with attributes.
6763 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6764 if attributes are present) and whose type is the modifier list. */
6766 tree
6767 make_pointer_declarator (type_quals_attrs, target)
6768 tree type_quals_attrs, target;
6770 tree quals, attrs;
6771 tree itarget = target;
6772 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6773 if (attrs != NULL_TREE)
6774 itarget = tree_cons (attrs, target, NULL_TREE);
6775 return build1 (INDIRECT_REF, quals, itarget);
6778 #include "gt-c-decl.h"