PR c++/11509
[official-gcc.git] / gcc / c-decl.c
blob80e6b68fa2ede6cf39250c3246c309f5e69c7415
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"
52 #include "hashtab.h"
53 #include "libfuncs.h"
54 #include "except.h"
56 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
57 enum decl_context
58 { NORMAL, /* Ordinary declaration */
59 FUNCDEF, /* Function definition */
60 PARM, /* Declaration of parm before function body */
61 FIELD, /* Declaration inside struct or union */
62 BITFIELD, /* Likewise but with specified width */
63 TYPENAME}; /* Typename (inside cast or sizeof) */
66 /* Nonzero if we have seen an invalid cross reference
67 to a struct, union, or enum, but not yet printed the message. */
69 tree pending_invalid_xref;
70 /* File and line to appear in the eventual error message. */
71 location_t pending_invalid_xref_location;
73 /* While defining an enum type, this is 1 plus the last enumerator
74 constant value. Note that will do not have to save this or `enum_overflow'
75 around nested function definition since such a definition could only
76 occur in an enum value expression and we don't use these variables in
77 that case. */
79 static tree enum_next_value;
81 /* Nonzero means that there was overflow computing enum_next_value. */
83 static int enum_overflow;
85 /* Parsing a function declarator leaves a list of parameter names
86 or a chain or parameter decls here. */
88 static tree last_function_parms;
90 /* Parsing a function declarator leaves here a chain of structure
91 and enum types declared in the parmlist. */
93 static tree last_function_parm_tags;
95 /* After parsing the declarator that starts a function definition,
96 `start_function' puts here the list of parameter names or chain of decls.
97 `store_parm_decls' finds it here. */
99 static tree current_function_parms;
101 /* Similar, for last_function_parm_tags. */
102 static tree current_function_parm_tags;
104 /* Similar, for the file and line that the prototype came from if this is
105 an old-style definition. */
106 static location_t current_function_prototype_locus;
108 /* The current statement tree. */
110 static GTY(()) struct stmt_tree_s c_stmt_tree;
112 /* The current scope statement stack. */
114 static GTY(()) tree c_scope_stmt_stack;
116 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
117 that have names. Here so we can clear out their names' definitions
118 at the end of the function. */
120 static GTY(()) tree named_labels;
122 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
124 static GTY(()) tree shadowed_labels;
126 /* A list of external DECLs that appeared at block scope when there was
127 some other global meaning for that identifier. */
128 static GTY(()) tree truly_local_externals;
130 /* A list of the builtin file-scope DECLs. */
132 static GTY(()) tree builtin_decls;
134 /* A DECL for the current file-scope context. */
136 static GTY(()) tree current_file_decl;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a return statement that specifies a return value is seen. */
141 int current_function_returns_value;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement with no argument is seen. */
146 int current_function_returns_null;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a call to a noreturn function is seen. */
151 int current_function_returns_abnormally;
153 /* Set to nonzero by `grokdeclarator' for a function
154 whose return type is defaulted, if warnings for this are desired. */
156 static int warn_about_return_type;
158 /* Nonzero when starting a function declared `extern inline'. */
160 static int current_extern_inline;
162 /* For each binding contour we allocate a binding_level structure
163 * which records the names defined in that contour.
164 * Contours include:
165 * 0) the global one
166 * 1) one for each function definition,
167 * where internal declarations of the parameters appear.
168 * 2) one for each compound statement,
169 * to record its declarations.
171 * The current meaning of a name can be found by searching the levels from
172 * the current one out to the global one.
175 struct binding_level GTY(())
177 /* A chain of _DECL nodes for all variables, constants, functions,
178 and typedef types. These are in the reverse of the order supplied.
180 tree names;
182 /* A list of structure, union and enum definitions,
183 * for looking up tag names.
184 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
185 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
186 * or ENUMERAL_TYPE node.
188 tree tags;
190 /* For each level, a list of shadowed outer-level definitions
191 to be restored when this level is popped.
192 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
193 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
194 tree shadowed;
196 /* For each level, a list of shadowed outer-level tag definitions
197 to be restored when this level is popped.
198 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
199 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
200 tree shadowed_tags;
202 /* For each level (except not the global one),
203 a chain of BLOCK nodes for all the levels
204 that were entered and exited one level down. */
205 tree blocks;
207 /* The binding level which this one is contained in (inherits from). */
208 struct binding_level *level_chain;
210 /* Nonzero if we are currently filling this level with parameter
211 declarations. */
212 char parm_flag;
214 /* Nonzero if this is the outermost block scope of a function body.
215 This scope contains both the parameters and the local variables
216 declared in the outermost block. */
217 char function_body;
219 /* Nonzero means make a BLOCK for this level regardless of all else. */
220 char keep;
222 /* Nonzero means make a BLOCK if this level has any subblocks. */
223 char keep_if_subblocks;
225 /* List of decls in `names' that have incomplete structure or
226 union types. */
227 tree incomplete_list;
229 /* A list of decls giving the (reversed) specified order of parms,
230 not including any forward-decls in the parmlist.
231 This is so we can put the parms in proper order for assign_parms. */
232 tree parm_order;
235 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
237 /* The binding level currently in effect. */
239 static GTY(()) struct binding_level *current_binding_level;
241 /* A chain of binding_level structures awaiting reuse. */
243 static GTY((deletable (""))) struct binding_level *free_binding_level;
245 /* The outermost binding level, for names of file scope.
246 This is created when the compiler is started and exists
247 through the entire run. */
249 static GTY(()) struct binding_level *global_binding_level;
251 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
253 static int keep_next_level_flag;
255 /* Nonzero means make a BLOCK for the next level pushed
256 if it has subblocks. */
258 static int keep_next_if_subblocks;
260 /* The chain of outer levels of label scopes.
261 This uses the same data structure used for binding levels,
262 but it works differently: each link in the chain records
263 saved values of named_labels and shadowed_labels for
264 a label binding level outside the current one. */
266 static GTY(()) struct binding_level *label_level_chain;
268 /* Functions called automatically at the beginning and end of execution. */
270 tree static_ctors, static_dtors;
272 /* Forward declarations. */
274 static struct binding_level *make_binding_level (void);
275 static void pop_binding_level (struct binding_level **);
276 static int duplicate_decls (tree, tree, int, int);
277 static int redeclaration_error_message (tree, tree);
278 static void implicit_decl_warning (tree);
279 static void storedecls (tree);
280 static void storetags (tree);
281 static tree lookup_tag (enum tree_code, tree, int);
282 static tree lookup_name_current_level (tree);
283 static tree grokdeclarator (tree, tree, enum decl_context, int);
284 static tree grokparms (tree, int);
285 static void layout_array_type (tree);
286 static tree c_make_fname_decl (tree, int);
287 static void c_expand_body_1 (tree, int);
288 static tree any_external_decl (tree);
289 static void record_external_decl (tree);
290 static void warn_if_shadowing (tree, tree);
291 static void clone_underlying_type (tree);
292 static bool flexible_array_type_p (tree);
293 static hashval_t link_hash_hash (const void *);
294 static int link_hash_eq (const void *, const void *);
296 /* States indicating how grokdeclarator() should handle declspecs marked
297 with __attribute__((deprecated)). An object declared as
298 __attribute__((deprecated)) suppresses warnings of uses of other
299 deprecated items. */
301 enum deprecated_states {
302 DEPRECATED_NORMAL,
303 DEPRECATED_SUPPRESS
306 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
308 void
309 c_print_identifier (FILE *file, tree node, int indent)
311 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
312 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
313 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
314 if (C_IS_RESERVED_WORD (node))
316 tree rid = ridpointers[C_RID_CODE (node)];
317 indent_to (file, indent + 4);
318 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
319 (void *) rid, IDENTIFIER_POINTER (rid));
323 /* Hook called at end of compilation to assume 1 elt
324 for a top-level tentative array defn that wasn't complete before. */
326 void
327 c_finish_incomplete_decl (tree decl)
329 if (TREE_CODE (decl) == VAR_DECL)
331 tree type = TREE_TYPE (decl);
332 if (type != error_mark_node
333 && TREE_CODE (type) == ARRAY_TYPE
334 && ! DECL_EXTERNAL (decl)
335 && TYPE_DOMAIN (type) == 0)
337 warning_with_decl (decl, "array `%s' assumed to have one element");
339 complete_array_type (type, NULL_TREE, 1);
341 layout_decl (decl, 0);
346 /* Reuse or create a struct for this binding level. */
348 static struct binding_level *
349 make_binding_level (void)
351 struct binding_level *result;
352 if (free_binding_level)
354 result = free_binding_level;
355 free_binding_level = result->level_chain;
356 memset (result, 0, sizeof(struct binding_level));
358 else
359 result = (struct binding_level *)
360 ggc_alloc_cleared (sizeof (struct binding_level));
362 return result;
365 /* Remove a binding level from a list and add it to the level chain. */
367 static void
368 pop_binding_level (struct binding_level **lp)
370 struct binding_level *l = *lp;
371 *lp = l->level_chain;
373 memset (l, 0, sizeof (struct binding_level));
374 l->level_chain = free_binding_level;
375 free_binding_level = l;
378 /* Nonzero if we are currently in the global binding level. */
381 global_bindings_p (void)
383 return current_binding_level == global_binding_level;
386 void
387 keep_next_level (void)
389 keep_next_level_flag = 1;
392 /* Nonzero if the current level needs to have a BLOCK made. */
395 kept_level_p (void)
397 return ((current_binding_level->keep_if_subblocks
398 && current_binding_level->blocks != 0)
399 || current_binding_level->keep
400 || current_binding_level->names != 0
401 || current_binding_level->tags != 0);
404 /* Identify this binding level as a level of parameters.
405 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
406 But it turns out there is no way to pass the right value for
407 DEFINITION_FLAG, so we ignore it. */
409 void
410 declare_parm_level (int definition_flag ATTRIBUTE_UNUSED)
412 current_binding_level->parm_flag = 1;
415 /* Nonzero if currently making parm declarations. */
418 in_parm_level_p (void)
420 return current_binding_level->parm_flag;
423 /* Enter a new binding level. */
425 void
426 pushlevel (int dummy ATTRIBUTE_UNUSED)
428 /* If this is the top level of a function, make sure that
429 NAMED_LABELS is 0. */
431 if (current_binding_level == global_binding_level)
432 named_labels = 0;
434 if (keep_next_if_subblocks)
436 /* This is the transition from the parameters to the top level
437 of the function body. These are the same scope
438 (C99 6.2.1p4,6) so we do not push another binding level.
440 XXX Note kludge - keep_next_if_subblocks is set only by
441 store_parm_decls, which in turn is called when and only
442 when we are about to encounter the opening curly brace for
443 the function body. */
444 current_binding_level->parm_flag = 0;
445 current_binding_level->function_body = 1;
446 current_binding_level->keep |= keep_next_level_flag;
447 current_binding_level->keep_if_subblocks = 1;
449 keep_next_level_flag = 0;
450 keep_next_if_subblocks = 0;
452 else
454 struct binding_level *newlevel = make_binding_level ();
456 newlevel->keep = keep_next_level_flag;
457 newlevel->level_chain = current_binding_level;
458 current_binding_level = newlevel;
459 keep_next_level_flag = 0;
463 /* Exit a binding level.
464 Pop the level off, and restore the state of the identifier-decl mappings
465 that were in effect when this level was entered.
467 If KEEP is nonzero, this level had explicit declarations, so
468 and create a "block" (a BLOCK node) for the level
469 to record its declarations and subblocks for symbol table output.
471 If FUNCTIONBODY is nonzero, this level is the body of a function,
472 so create a block as if KEEP were set and also clear out all
473 label names.
475 If REVERSE is nonzero, reverse the order of decls before putting
476 them into the BLOCK. */
478 tree
479 poplevel (int keep, int reverse, 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 No warnings when the global scope is popped because the global
497 scope isn't popped for the last translation unit, so the warnings
498 are done in c_write_global_declaration. */
499 if (current_binding_level != global_binding_level)
500 warn_about_unused_variables (decls);
502 /* Clear out the name-meanings declared on this level.
503 Propagate TREE_ADDRESSABLE from nested functions to their
504 containing functions. */
505 for (link = decls; link; link = TREE_CHAIN (link))
507 if (DECL_NAME (link) != 0)
509 if (DECL_EXTERNAL (link)
510 && current_binding_level != global_binding_level)
511 /* External decls stay in the symbol-value slot but are
512 inaccessible. */
513 C_DECL_INVISIBLE (link) = 1;
514 else
515 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
518 if (TREE_CODE (link) == FUNCTION_DECL
519 && ! TREE_ASM_WRITTEN (link)
520 && DECL_INITIAL (link) != 0
521 && TREE_ADDRESSABLE (link)
522 && DECL_ABSTRACT_ORIGIN (link) != 0
523 && DECL_ABSTRACT_ORIGIN (link) != link)
524 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
527 /* Clear out the tag-meanings declared on this level. */
528 for (link = tags; link; link = TREE_CHAIN (link))
529 if (TREE_PURPOSE (link))
530 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
532 /* Restore all name-meanings of the outer levels
533 that were shadowed by this level. */
535 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
536 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
538 /* Restore all tag-meanings of the outer levels
539 that were shadowed by this level. */
541 for (link = current_binding_level->shadowed_tags; link;
542 link = TREE_CHAIN (link))
543 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
545 /* If this is the top level block of a function, remove all
546 PARM_DECLs from current_binding_level->names; they are already
547 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
548 not be put in BLOCK_VARS, and furthermore reversing them will
549 cause trouble later. They are all together at the end of the
550 list. */
551 if (functionbody && decls)
553 if (TREE_CODE (decls) == PARM_DECL)
554 decls = 0;
555 else
557 link = decls;
558 while (TREE_CHAIN (link)
559 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
560 link = TREE_CHAIN (link);
562 TREE_CHAIN (link) = 0;
566 /* Get the decls in the order they were written.
567 Usually current_binding_level->names is in reverse order.
568 But parameter decls were previously put in forward order. */
570 if (reverse)
571 decls = nreverse (decls);
573 /* If there were any declarations or structure tags in that level,
574 or if this level is a function body,
575 create a BLOCK to record them for the life of this function. */
577 block = 0;
578 if (keep)
580 block = make_node (BLOCK);
581 BLOCK_VARS (block) = decls;
582 BLOCK_SUBBLOCKS (block) = subblocks;
583 TREE_USED (block) = 1;
586 /* In each subblock, record that this is its superior. */
588 for (link = subblocks; link; link = TREE_CHAIN (link))
589 BLOCK_SUPERCONTEXT (link) = block;
591 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
592 binding contour so that they point to the appropriate construct, i.e.
593 either to the current FUNCTION_DECL node, or else to the BLOCK node
594 we just constructed.
596 Note that for tagged types whose scope is just the formal parameter
597 list for some function type specification, we can't properly set
598 their TYPE_CONTEXTs here, because we don't have a pointer to the
599 appropriate FUNCTION_TYPE node readily available to us. For those
600 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
601 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
602 node which will represent the "scope" for these "parameter list local"
603 tagged types. */
605 decl = functionbody ? current_function_decl : block;
606 if (decl)
607 for (link = tags; link; link = TREE_CHAIN (link))
608 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
610 /* If the level being exited is the top level of a function, check
611 over all the labels, and clear out the current (function local)
612 meanings of their names. Then add them to BLOCK_VARS. */
614 if (functionbody)
616 for (link = named_labels; link; link = TREE_CHAIN (link))
618 tree label = TREE_VALUE (link);
620 if (DECL_INITIAL (label) == 0)
622 error_with_decl (label, "label `%s' used but not defined");
623 /* Avoid crashing later. */
624 define_label (input_location, DECL_NAME (label));
626 else if (warn_unused_label && !TREE_USED (label))
627 warning_with_decl (label, "label `%s' defined but not used");
628 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
630 /* Put the labels into the "variables" of the
631 top-level block, so debugger can see them. */
632 TREE_CHAIN (label) = BLOCK_VARS (block);
633 BLOCK_VARS (block) = label;
637 /* Pop the current level, and free the structure for reuse. */
639 pop_binding_level (&current_binding_level);
641 /* Dispose of the block that we just made inside some higher level. */
642 if (functionbody)
643 DECL_INITIAL (current_function_decl) = block;
644 else if (block && current_binding_level)
645 current_binding_level->blocks
646 = chainon (current_binding_level->blocks, block);
647 /* If we did not make a block for the level just exited,
648 any blocks made for inner levels
649 (since they cannot be recorded as subblocks in that level)
650 must be carried forward so they will later become subblocks
651 of something else. */
652 else if (! block && subblocks)
653 current_binding_level->blocks
654 = chainon (current_binding_level->blocks, subblocks);
656 return block;
659 /* Insert BLOCK at the end of the list of subblocks of the
660 current binding level. This is used when a BIND_EXPR is expanded,
661 to handle the BLOCK node inside the BIND_EXPR. */
663 void
664 insert_block (tree block)
666 TREE_USED (block) = 1;
667 current_binding_level->blocks
668 = chainon (current_binding_level->blocks, block);
671 /* Set the BLOCK node for the innermost scope (the one we are
672 currently in). The RTL expansion machinery requires us to provide
673 this hook, but it is not useful in function-at-a-time mode. */
675 void
676 set_block (tree block ATTRIBUTE_UNUSED)
680 void
681 push_label_level (void)
683 struct binding_level *newlevel;
685 newlevel = make_binding_level ();
687 /* Add this level to the front of the chain (stack) of label levels. */
689 newlevel->level_chain = label_level_chain;
690 label_level_chain = newlevel;
692 newlevel->names = named_labels;
693 newlevel->shadowed = shadowed_labels;
694 named_labels = 0;
695 shadowed_labels = 0;
698 void
699 pop_label_level (void)
701 struct binding_level *level = label_level_chain;
702 tree link, prev;
704 /* Clear out the definitions of the declared labels in this level.
705 Leave in the list any ordinary, non-declared labels. */
706 for (link = named_labels, prev = 0; link;)
708 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
710 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
712 error_with_decl (TREE_VALUE (link),
713 "label `%s' used but not defined");
714 /* Avoid crashing later. */
715 define_label (input_location, DECL_NAME (TREE_VALUE (link)));
717 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
718 warning_with_decl (TREE_VALUE (link),
719 "label `%s' defined but not used");
720 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
722 /* Delete this element from the list. */
723 link = TREE_CHAIN (link);
724 if (prev)
725 TREE_CHAIN (prev) = link;
726 else
727 named_labels = link;
729 else
731 prev = link;
732 link = TREE_CHAIN (link);
736 /* Bring back all the labels that were shadowed. */
737 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
738 if (DECL_NAME (TREE_VALUE (link)) != 0)
739 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
740 = TREE_VALUE (link);
742 named_labels = chainon (named_labels, level->names);
743 shadowed_labels = level->shadowed;
745 /* Pop the current level, and free the structure for reuse. */
746 pop_binding_level (&label_level_chain);
749 /* Push a definition or a declaration of struct, union or enum tag "name".
750 "type" should be the type node.
751 We assume that the tag "name" is not already defined.
753 Note that the definition may really be just a forward reference.
754 In that case, the TYPE_SIZE will be zero. */
756 void
757 pushtag (tree name, tree 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 (tree newdecl, tree olddecl, int different_binding_level,
803 int different_tu)
805 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
806 : COMPARE_STRICT);
807 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
808 comptype_flags);
809 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
810 && DECL_INITIAL (newdecl) != 0);
811 tree oldtype = TREE_TYPE (olddecl);
812 tree newtype = TREE_TYPE (newdecl);
813 int errmsg = 0;
815 if (DECL_P (olddecl))
817 if (TREE_CODE (newdecl) == FUNCTION_DECL
818 && TREE_CODE (olddecl) == FUNCTION_DECL
819 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
821 if (DECL_DECLARED_INLINE_P (newdecl)
822 && DECL_UNINLINABLE (newdecl)
823 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
824 /* Already warned elsewhere. */;
825 else if (DECL_DECLARED_INLINE_P (olddecl)
826 && DECL_UNINLINABLE (olddecl)
827 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
828 /* Already warned. */;
829 else if (DECL_DECLARED_INLINE_P (newdecl)
830 && ! DECL_DECLARED_INLINE_P (olddecl)
831 && DECL_UNINLINABLE (olddecl)
832 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
834 warning_with_decl (newdecl,
835 "function `%s' redeclared as inline");
836 warning_with_decl (olddecl,
837 "previous declaration of function `%s' with attribute noinline");
839 else if (DECL_DECLARED_INLINE_P (olddecl)
840 && DECL_UNINLINABLE (newdecl)
841 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
843 warning_with_decl (newdecl,
844 "function `%s' redeclared with attribute noinline");
845 warning_with_decl (olddecl,
846 "previous declaration of function `%s' was inline");
850 DECL_ATTRIBUTES (newdecl)
851 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
854 if (TREE_CODE (newtype) == ERROR_MARK
855 || TREE_CODE (oldtype) == ERROR_MARK)
856 types_match = 0;
858 /* New decl is completely inconsistent with the old one =>
859 tell caller to replace the old one.
860 This is always an error except in the case of shadowing a builtin. */
861 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
863 if (TREE_CODE (olddecl) == FUNCTION_DECL
864 && DECL_BUILT_IN (olddecl))
866 /* If you declare a built-in or predefined function name as static,
867 the old definition is overridden,
868 but optionally warn this was a bad choice of name. */
869 if (!TREE_PUBLIC (newdecl))
871 if (warn_shadow)
872 warning_with_decl (newdecl, "shadowing built-in function `%s'");
874 else
875 warning_with_decl (newdecl,
876 "built-in function `%s' declared as non-function");
878 else
880 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
881 error_with_decl (olddecl, "previous declaration of `%s'");
884 return 0;
887 /* For real parm decl following a forward decl, return 1 so old decl
888 will be reused. Only allow this to happen once. */
889 if (types_match && TREE_CODE (newdecl) == PARM_DECL
890 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
892 TREE_ASM_WRITTEN (olddecl) = 0;
893 return 1;
896 /* The new declaration is the same kind of object as the old one.
897 The declarations may partially match. Print warnings if they don't
898 match enough. Ultimately, copy most of the information from the new
899 decl to the old one, and keep using the old one. */
901 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
903 /* A function declaration for a built-in function. */
904 if (!TREE_PUBLIC (newdecl))
906 /* If you declare a built-in function name as static, the
907 built-in definition is overridden,
908 but optionally warn this was a bad choice of name. */
909 if (warn_shadow)
910 warning_with_decl (newdecl, "shadowing built-in function `%s'");
911 /* Discard the old built-in function. */
912 return 0;
914 else if (!types_match)
916 /* Accept the return type of the new declaration if same modes. */
917 tree oldreturntype = TREE_TYPE (oldtype);
918 tree newreturntype = TREE_TYPE (newtype);
920 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
922 /* Function types may be shared, so we can't just modify
923 the return type of olddecl's function type. */
924 tree trytype
925 = build_function_type (newreturntype,
926 TYPE_ARG_TYPES (oldtype));
927 trytype = build_type_attribute_variant (trytype,
928 TYPE_ATTRIBUTES (oldtype));
930 types_match = comptypes (newtype, trytype, comptype_flags);
931 if (types_match)
932 oldtype = trytype;
934 /* Accept harmless mismatch in first argument type also.
935 This is for the ffs and fprintf builtins. */
936 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
937 && TYPE_ARG_TYPES (oldtype) != 0
938 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
939 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
940 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
941 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
943 /* Function types may be shared, so we can't just modify
944 the return type of olddecl's function type. */
945 tree trytype
946 = build_function_type (TREE_TYPE (oldtype),
947 tree_cons (NULL_TREE,
948 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
949 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
950 trytype = build_type_attribute_variant (trytype,
951 TYPE_ATTRIBUTES (oldtype));
953 types_match = comptypes (newtype, trytype, comptype_flags);
954 if (types_match)
955 oldtype = trytype;
957 if (! different_binding_level)
958 TREE_TYPE (olddecl) = oldtype;
960 else if (TYPE_ARG_TYPES (oldtype) == NULL
961 && TYPE_ARG_TYPES (newtype) != NULL)
963 /* For bcmp, bzero, fputs the builtin type has arguments not
964 specified. Use the ones from the prototype so that type checking
965 is done for them. */
966 tree trytype
967 = build_function_type (TREE_TYPE (oldtype),
968 TYPE_ARG_TYPES (newtype));
969 trytype = build_type_attribute_variant (trytype,
970 TYPE_ATTRIBUTES (oldtype));
972 oldtype = trytype;
973 if (! different_binding_level)
974 TREE_TYPE (olddecl) = oldtype;
976 if (!types_match)
978 /* If types don't match for a built-in, throw away the built-in. */
979 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
980 return 0;
983 else if (TREE_CODE (olddecl) == FUNCTION_DECL
984 && DECL_SOURCE_LINE (olddecl) == 0)
986 /* A function declaration for a predeclared function
987 that isn't actually built in. */
988 if (!TREE_PUBLIC (newdecl))
990 /* If you declare it as static, the
991 default definition is overridden. */
992 return 0;
994 else if (!types_match)
996 /* If the types don't match, preserve volatility indication.
997 Later on, we will discard everything else about the
998 default declaration. */
999 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1002 /* Permit char *foo () to match void *foo (...) if not pedantic,
1003 if one of them came from a system header file. */
1004 else if (!types_match
1005 && TREE_CODE (olddecl) == FUNCTION_DECL
1006 && TREE_CODE (newdecl) == FUNCTION_DECL
1007 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1008 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1009 && (DECL_IN_SYSTEM_HEADER (olddecl)
1010 || DECL_IN_SYSTEM_HEADER (newdecl))
1011 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1012 && TYPE_ARG_TYPES (oldtype) == 0
1013 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1014 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1016 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1017 && TYPE_ARG_TYPES (newtype) == 0
1018 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1019 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1021 if (pedantic)
1022 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1023 /* Make sure we keep void * as ret type, not char *. */
1024 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1025 TREE_TYPE (newdecl) = newtype = oldtype;
1027 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1028 we will come back here again. */
1029 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1031 /* Permit void foo (...) to match int foo (...) if the latter is the
1032 definition and implicit int was used. See c-torture/compile/920625-2.c. */
1033 else if (!types_match && new_is_definition
1034 && TREE_CODE (olddecl) == FUNCTION_DECL
1035 && TREE_CODE (newdecl) == FUNCTION_DECL
1036 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1037 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1038 && C_FUNCTION_IMPLICIT_INT (newdecl))
1040 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1041 /* Make sure we keep void as the return type. */
1042 TREE_TYPE (newdecl) = newtype = oldtype;
1043 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1045 else if (!types_match
1046 /* Permit char *foo (int, ...); followed by char *foo ();
1047 if not pedantic. */
1048 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1049 && ! pedantic
1050 /* Return types must still match. */
1051 && comptypes (TREE_TYPE (oldtype),
1052 TREE_TYPE (newtype), comptype_flags)
1053 && TYPE_ARG_TYPES (newtype) == 0))
1055 error_with_decl (newdecl, "conflicting types for `%s'");
1056 /* Check for function type mismatch
1057 involving an empty arglist vs a nonempty one. */
1058 if (TREE_CODE (olddecl) == FUNCTION_DECL
1059 && comptypes (TREE_TYPE (oldtype),
1060 TREE_TYPE (newtype), comptype_flags)
1061 && ((TYPE_ARG_TYPES (oldtype) == 0
1062 && DECL_INITIAL (olddecl) == 0)
1064 (TYPE_ARG_TYPES (newtype) == 0
1065 && DECL_INITIAL (newdecl) == 0)))
1067 /* Classify the problem further. */
1068 tree t = TYPE_ARG_TYPES (oldtype);
1069 if (t == 0)
1070 t = TYPE_ARG_TYPES (newtype);
1071 for (; t; t = TREE_CHAIN (t))
1073 tree type = TREE_VALUE (t);
1075 if (TREE_CHAIN (t) == 0
1076 && TYPE_MAIN_VARIANT (type) != void_type_node)
1078 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1079 break;
1082 if (c_type_promotes_to (type) != type)
1084 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1085 break;
1089 if (C_DECL_IMPLICIT (olddecl))
1090 error_with_decl (olddecl, "previous implicit declaration of `%s'");
1091 else
1092 error_with_decl (olddecl, "previous declaration of `%s'");
1094 /* This is safer because the initializer might contain references
1095 to variables that were declared between olddecl and newdecl. This
1096 will make the initializer invalid for olddecl in case it gets
1097 assigned to olddecl below. */
1098 if (TREE_CODE (newdecl) == VAR_DECL)
1099 DECL_INITIAL (newdecl) = 0;
1101 /* TLS cannot follow non-TLS declaration. */
1102 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1103 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1105 error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1106 error_with_decl (olddecl, "previous declaration of `%s'");
1108 /* non-TLS declaration cannot follow TLS declaration. */
1109 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1110 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1112 error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1113 error_with_decl (olddecl, "previous declaration of `%s'");
1115 else
1117 errmsg = redeclaration_error_message (newdecl, olddecl);
1118 if (errmsg)
1120 switch (errmsg)
1122 case 1:
1123 error_with_decl (newdecl, "redefinition of `%s'");
1124 break;
1125 case 2:
1126 error_with_decl (newdecl, "redeclaration of `%s'");
1127 break;
1128 case 3:
1129 error_with_decl (newdecl, "conflicting declarations of `%s'");
1130 break;
1131 default:
1132 abort ();
1135 error_with_decl (olddecl,
1136 ((DECL_INITIAL (olddecl)
1137 && current_binding_level == global_binding_level)
1138 ? "`%s' previously defined here"
1139 : "`%s' previously declared here"));
1140 return 0;
1142 else if (TREE_CODE (newdecl) == TYPE_DECL
1143 && (DECL_IN_SYSTEM_HEADER (olddecl)
1144 || DECL_IN_SYSTEM_HEADER (newdecl)))
1146 warning_with_decl (newdecl, "redefinition of `%s'");
1147 warning_with_decl
1148 (olddecl,
1149 ((DECL_INITIAL (olddecl)
1150 && current_binding_level == global_binding_level)
1151 ? "`%s' previously defined here"
1152 : "`%s' previously declared here"));
1154 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1155 && DECL_INITIAL (olddecl) != 0
1156 && TYPE_ARG_TYPES (oldtype) == 0
1157 && TYPE_ARG_TYPES (newtype) != 0
1158 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1160 tree type, parm;
1161 int nargs;
1162 /* Prototype decl follows defn w/o prototype. */
1164 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1165 type = TYPE_ARG_TYPES (newtype),
1166 nargs = 1;
1168 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1170 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1171 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1173 warning_with_decl (newdecl, "prototype for `%s' follows");
1174 warning_with_decl (olddecl, "non-prototype definition here");
1175 break;
1177 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1178 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1180 error_with_decl (newdecl,
1181 "prototype for `%s' follows and number of arguments doesn't match");
1182 error_with_decl (olddecl, "non-prototype definition here");
1183 errmsg = 1;
1184 break;
1186 /* Type for passing arg must be consistent
1187 with that declared for the arg. */
1188 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1189 comptype_flags))
1191 error_with_decl (newdecl,
1192 "prototype for `%s' follows and argument %d doesn't match",
1193 nargs);
1194 error_with_decl (olddecl, "non-prototype definition here");
1195 errmsg = 1;
1196 break;
1200 /* Warn about mismatches in various flags. */
1201 else
1203 /* Warn if function is now inline
1204 but was previously declared not inline and has been called. */
1205 if (TREE_CODE (olddecl) == FUNCTION_DECL
1206 && ! DECL_DECLARED_INLINE_P (olddecl)
1207 && DECL_DECLARED_INLINE_P (newdecl)
1208 && TREE_USED (olddecl))
1209 warning_with_decl (newdecl,
1210 "`%s' declared inline after being called");
1211 if (TREE_CODE (olddecl) == FUNCTION_DECL
1212 && ! DECL_DECLARED_INLINE_P (olddecl)
1213 && DECL_DECLARED_INLINE_P (newdecl)
1214 && DECL_INITIAL (olddecl) != 0)
1215 warning_with_decl (newdecl,
1216 "`%s' declared inline after its definition");
1218 /* If pedantic, warn when static declaration follows a non-static
1219 declaration. Otherwise, do so only for functions. */
1220 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1221 && TREE_PUBLIC (olddecl)
1222 && !TREE_PUBLIC (newdecl))
1223 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1225 /* If warn_traditional, warn when a non-static function
1226 declaration follows a static one. */
1227 if (warn_traditional && !in_system_header
1228 && TREE_CODE (olddecl) == FUNCTION_DECL
1229 && !TREE_PUBLIC (olddecl)
1230 && TREE_PUBLIC (newdecl))
1231 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1233 /* Warn when const declaration follows a non-const
1234 declaration, but not for functions. */
1235 if (TREE_CODE (olddecl) != FUNCTION_DECL
1236 && !TREE_READONLY (olddecl)
1237 && TREE_READONLY (newdecl))
1238 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1239 /* These bits are logically part of the type, for variables.
1240 But not for functions
1241 (where qualifiers are not valid ANSI anyway). */
1242 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1243 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1244 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1245 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1249 /* Optionally warn about more than one declaration for the same name. */
1250 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1251 /* Don't warn about a function declaration
1252 followed by a definition. */
1253 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1254 && DECL_INITIAL (olddecl) == 0)
1255 /* Don't warn about extern decl followed by (tentative) definition. */
1256 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1258 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1259 warning_with_decl (olddecl, "previous declaration of `%s'");
1262 /* Copy all the DECL_... slots specified in the new decl
1263 except for any that we copy here from the old type.
1265 Past this point, we don't change OLDTYPE and NEWTYPE
1266 even if we change the types of NEWDECL and OLDDECL. */
1268 if (types_match)
1270 /* When copying info to olddecl, we store into write_olddecl
1271 instead. This allows us to avoid modifying olddecl when
1272 different_binding_level is true. */
1273 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1275 /* Merge the data types specified in the two decls. */
1276 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1278 if (different_binding_level)
1280 if (TYPE_ARG_TYPES (oldtype) != 0
1281 && TYPE_ARG_TYPES (newtype) == 0)
1282 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1283 else
1284 TREE_TYPE (newdecl)
1285 = build_type_attribute_variant
1286 (newtype,
1287 merge_attributes (TYPE_ATTRIBUTES (newtype),
1288 TYPE_ATTRIBUTES (oldtype)));
1290 else
1291 TREE_TYPE (newdecl)
1292 = TREE_TYPE (olddecl)
1293 = common_type (newtype, oldtype);
1296 /* Lay the type out, unless already done. */
1297 if (oldtype != TREE_TYPE (newdecl))
1299 if (TREE_TYPE (newdecl) != error_mark_node)
1300 layout_type (TREE_TYPE (newdecl));
1301 if (TREE_CODE (newdecl) != FUNCTION_DECL
1302 && TREE_CODE (newdecl) != TYPE_DECL
1303 && TREE_CODE (newdecl) != CONST_DECL)
1304 layout_decl (newdecl, 0);
1306 else
1308 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1309 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1310 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1311 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1312 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1313 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1315 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1316 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1320 /* Keep the old rtl since we can safely use it. */
1321 COPY_DECL_RTL (olddecl, newdecl);
1323 /* Merge the type qualifiers. */
1324 if (TREE_READONLY (newdecl))
1325 TREE_READONLY (write_olddecl) = 1;
1327 if (TREE_THIS_VOLATILE (newdecl))
1329 TREE_THIS_VOLATILE (write_olddecl) = 1;
1330 if (TREE_CODE (newdecl) == VAR_DECL
1331 /* If an automatic variable is re-declared in the same
1332 function scope, but the old declaration was not
1333 volatile, make_var_volatile() would crash because the
1334 variable would have been assigned to a pseudo, not a
1335 MEM. Since this duplicate declaration is invalid
1336 anyway, we just skip the call. */
1337 && errmsg == 0)
1338 make_var_volatile (newdecl);
1341 /* Keep source location of definition rather than declaration. */
1342 /* When called with different_binding_level set, keep the old
1343 information so that meaningful diagnostics can be given. */
1344 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1345 && ! different_binding_level)
1347 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1348 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1351 /* Merge the unused-warning information. */
1352 if (DECL_IN_SYSTEM_HEADER (olddecl))
1353 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1354 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1355 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1357 /* Merge the initialization information. */
1358 /* When called with different_binding_level set, don't copy over
1359 DECL_INITIAL, so that we don't accidentally change function
1360 declarations into function definitions. */
1361 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1362 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1364 /* Merge the section attribute.
1365 We want to issue an error if the sections conflict but that must be
1366 done later in decl_attributes since we are called before attributes
1367 are assigned. */
1368 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1369 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1371 /* Copy the assembler name.
1372 Currently, it can only be defined in the prototype. */
1373 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1375 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1377 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1378 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1379 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1380 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1381 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1382 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1383 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1384 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1385 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1388 /* If cannot merge, then use the new type and qualifiers,
1389 and don't preserve the old rtl. */
1390 else if (! different_binding_level)
1392 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1393 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1394 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1395 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1398 /* Merge the storage class information. */
1399 merge_weak (newdecl, olddecl);
1401 /* For functions, static overrides non-static. */
1402 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1404 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1405 /* This is since we don't automatically
1406 copy the attributes of NEWDECL into OLDDECL. */
1407 /* No need to worry about different_binding_level here because
1408 then TREE_PUBLIC (newdecl) was true. */
1409 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1410 /* If this clears `static', clear it in the identifier too. */
1411 if (! TREE_PUBLIC (olddecl))
1412 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1414 if (DECL_EXTERNAL (newdecl))
1416 if (! different_binding_level || different_tu)
1418 /* Don't mess with these flags on local externs; they remain
1419 external even if there's a declaration at file scope which
1420 isn't. */
1421 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1422 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1424 /* An extern decl does not override previous storage class. */
1425 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1426 if (! DECL_EXTERNAL (newdecl))
1428 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1429 /* If we have two non-EXTERNAL file-scope decls that are
1430 the same, only one of them should be written out. */
1431 if (different_tu)
1432 TREE_ASM_WRITTEN (newdecl) = 1;
1435 else
1437 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1438 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1441 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1443 /* If we're redefining a function previously defined as extern
1444 inline, make sure we emit debug info for the inline before we
1445 throw it away, in case it was inlined into a function that hasn't
1446 been written out yet. */
1447 if (new_is_definition && DECL_INITIAL (olddecl))
1449 if (TREE_USED (olddecl))
1450 (*debug_hooks->outlining_inline_function) (olddecl);
1452 /* The new defn must not be inline. */
1453 DECL_INLINE (newdecl) = 0;
1454 DECL_UNINLINABLE (newdecl) = 1;
1456 else
1458 /* If either decl says `inline', this fn is inline,
1459 unless its definition was passed already. */
1460 if (DECL_DECLARED_INLINE_P (newdecl)
1461 || DECL_DECLARED_INLINE_P (olddecl))
1462 DECL_DECLARED_INLINE_P (newdecl) = 1;
1464 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1465 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1468 if (DECL_BUILT_IN (olddecl))
1470 /* Get rid of any built-in function if new arg types don't match it
1471 or if we have a function definition. */
1472 if (! types_match || new_is_definition)
1474 if (! different_binding_level)
1476 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1477 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1480 else
1482 /* If redeclaring a builtin function, and not a definition,
1483 it stays built in. */
1484 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1485 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1489 /* Also preserve various other info from the definition. */
1490 if (! new_is_definition)
1492 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1493 /* When called with different_binding_level set, don't copy over
1494 DECL_INITIAL, so that we don't accidentally change function
1495 declarations into function definitions. */
1496 if (! different_binding_level)
1497 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1498 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1499 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1500 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1501 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1503 /* Set DECL_INLINE on the declaration if we've got a body
1504 from which to instantiate. */
1505 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1507 DECL_INLINE (newdecl) = 1;
1508 DECL_ABSTRACT_ORIGIN (newdecl)
1509 = (different_binding_level
1510 ? DECL_ORIGIN (olddecl)
1511 : DECL_ABSTRACT_ORIGIN (olddecl));
1514 else
1516 /* If a previous declaration said inline, mark the
1517 definition as inlinable. */
1518 if (DECL_DECLARED_INLINE_P (newdecl)
1519 && ! DECL_UNINLINABLE (newdecl))
1520 DECL_INLINE (newdecl) = 1;
1523 if (different_binding_level)
1524 return 0;
1526 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1527 But preserve OLDDECL's DECL_UID. */
1529 unsigned olddecl_uid = DECL_UID (olddecl);
1531 memcpy ((char *) olddecl + sizeof (struct tree_common),
1532 (char *) newdecl + sizeof (struct tree_common),
1533 sizeof (struct tree_decl) - sizeof (struct tree_common));
1534 DECL_UID (olddecl) = olddecl_uid;
1537 /* NEWDECL contains the merged attribute lists.
1538 Update OLDDECL to be the same. */
1539 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1541 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1542 so that encode_section_info has a chance to look at the new decl
1543 flags and attributes. */
1544 if (DECL_RTL_SET_P (olddecl)
1545 && (TREE_CODE (olddecl) == FUNCTION_DECL
1546 || (TREE_CODE (olddecl) == VAR_DECL
1547 && TREE_STATIC (olddecl))))
1548 make_decl_rtl (olddecl, NULL);
1550 return 1;
1553 /* Return any external DECL associated with ID, whether or not it is
1554 currently in scope. */
1556 static tree
1557 any_external_decl (tree id)
1559 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1560 tree t;
1562 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1563 return 0;
1564 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1565 return decl;
1567 t = purpose_member (id, truly_local_externals);
1568 if (t)
1569 return TREE_VALUE (t);
1571 return 0;
1574 /* Record an external decl DECL. This only does something if a
1575 shadowing decl already exists. */
1576 static void
1577 record_external_decl (tree decl)
1579 tree name = DECL_NAME (decl);
1580 if (!IDENTIFIER_SYMBOL_VALUE (name))
1581 return;
1583 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1586 /* Check whether decl-node X shadows an existing declaration.
1587 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1588 which might be a NULL_TREE. */
1589 static void
1590 warn_if_shadowing (tree x, tree old)
1592 const char *name;
1594 /* Nothing to shadow? */
1595 if (old == 0
1596 /* Shadow warnings not wanted? */
1597 || !warn_shadow
1598 /* No shadow warnings for internally generated vars. */
1599 || DECL_SOURCE_LINE (x) == 0
1600 /* No shadow warnings for vars made for inlining. */
1601 || DECL_FROM_INLINE (x)
1602 /* Don't warn about the parm names in function declarator
1603 within a function declarator.
1604 It would be nice to avoid warning in any function
1605 declarator in a declaration, as opposed to a definition,
1606 but there is no way to tell it's not a definition. */
1607 || (TREE_CODE (x) == PARM_DECL
1608 && current_binding_level->level_chain->parm_flag))
1609 return;
1611 name = IDENTIFIER_POINTER (DECL_NAME (x));
1613 if (TREE_CODE (old) == PARM_DECL)
1614 shadow_warning (SW_PARAM, name, old);
1615 else if (C_DECL_FILE_SCOPE (old))
1616 shadow_warning (SW_GLOBAL, name, old);
1617 else
1618 shadow_warning (SW_LOCAL, name, old);
1622 /* Subroutine of pushdecl.
1624 X is a TYPE_DECL for a typedef statement. Create a brand new
1625 ..._TYPE node (which will be just a variant of the existing
1626 ..._TYPE node with identical properties) and then install X
1627 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1629 The whole point here is to end up with a situation where each
1630 and every ..._TYPE node the compiler creates will be uniquely
1631 associated with AT MOST one node representing a typedef name.
1632 This way, even though the compiler substitutes corresponding
1633 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1634 early on, later parts of the compiler can always do the reverse
1635 translation and get back the corresponding typedef name. For
1636 example, given:
1638 typedef struct S MY_TYPE;
1639 MY_TYPE object;
1641 Later parts of the compiler might only know that `object' was of
1642 type `struct S' if it were not for code just below. With this
1643 code however, later parts of the compiler see something like:
1645 struct S' == struct S
1646 typedef struct S' MY_TYPE;
1647 struct S' object;
1649 And they can then deduce (from the node for type struct S') that
1650 the original object declaration was:
1652 MY_TYPE object;
1654 Being able to do this is important for proper support of protoize,
1655 and also for generating precise symbolic debugging information
1656 which takes full account of the programmer's (typedef) vocabulary.
1658 Obviously, we don't want to generate a duplicate ..._TYPE node if
1659 the TYPE_DECL node that we are now processing really represents a
1660 standard built-in type.
1662 Since all standard types are effectively declared at line zero
1663 in the source file, we can easily check to see if we are working
1664 on a standard type by checking the current value of lineno. */
1666 static void
1667 clone_underlying_type (tree x)
1669 if (DECL_SOURCE_LINE (x) == 0)
1671 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1672 TYPE_NAME (TREE_TYPE (x)) = x;
1674 else if (TREE_TYPE (x) != error_mark_node
1675 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1677 tree tt = TREE_TYPE (x);
1678 DECL_ORIGINAL_TYPE (x) = tt;
1679 tt = build_type_copy (tt);
1680 TYPE_NAME (tt) = x;
1681 TREE_USED (tt) = TREE_USED (x);
1682 TREE_TYPE (x) = tt;
1686 /* Record a decl-node X as belonging to the current lexical scope.
1687 Check for errors (such as an incompatible declaration for the same
1688 name already seen in the same scope).
1690 Returns either X or an old decl for the same name.
1691 If an old decl is returned, it may have been smashed
1692 to agree with what X says. */
1694 tree
1695 pushdecl (tree x)
1697 tree name = DECL_NAME (x);
1698 struct binding_level *scope = current_binding_level;
1700 #ifdef ENABLE_CHECKING
1701 if (error_mark_node == 0)
1702 /* Called too early. */
1703 abort ();
1704 #endif
1706 /* Functions need the lang_decl data. */
1707 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1708 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1709 ggc_alloc_cleared (sizeof (struct lang_decl));
1711 /* A local extern declaration for a function doesn't constitute nesting.
1712 A local auto declaration does, since it's a forward decl
1713 for a nested function coming later. */
1714 if (current_function_decl == NULL
1715 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1716 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1717 DECL_CONTEXT (x) = current_file_decl;
1718 else
1719 DECL_CONTEXT (x) = current_function_decl;
1721 if (name)
1723 tree old;
1725 if (warn_nested_externs
1726 && scope != global_binding_level
1727 && DECL_EXTERNAL (x)
1728 && !DECL_IN_SYSTEM_HEADER (x))
1729 warning ("nested extern declaration of `%s'",
1730 IDENTIFIER_POINTER (name));
1732 old = lookup_name_current_level (name);
1733 if (old && duplicate_decls (x, old, 0, false))
1734 return old;
1735 if (DECL_EXTERNAL (x) || scope == global_binding_level)
1737 /* Find and check against a previous, not-in-scope, external
1738 decl for this identifier. (C99 s???: If two declarations
1739 with external linkage, referring to the same object, have
1740 incompatible types, the behavior is undefined). */
1741 tree ext = any_external_decl (name);
1742 if (ext)
1744 if (duplicate_decls (x, ext, scope != global_binding_level,
1745 false))
1746 x = copy_node (ext);
1748 else
1749 record_external_decl (x);
1752 if (TREE_CODE (x) == TYPE_DECL)
1753 clone_underlying_type (x);
1755 /* If storing a local value, there may already be one
1756 (inherited). If so, record it for restoration when this
1757 binding level ends. Take care not to do this if we are
1758 replacing an older decl in the same binding level (i.e.
1759 duplicate_decls returned false, above). */
1760 if (scope != global_binding_level
1761 && IDENTIFIER_SYMBOL_VALUE (name)
1762 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1764 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1765 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1766 scope->shadowed);
1769 /* Install the new declaration in the requested binding level. */
1770 IDENTIFIER_SYMBOL_VALUE (name) = x;
1771 C_DECL_INVISIBLE (x) = 0;
1773 /* Keep list of variables in this level with incomplete type.
1774 If the input is erroneous, we can have error_mark in the type
1775 slot (e.g. "f(void a, ...)") - that doesn't count as an
1776 incomplete type. */
1777 if (TREE_TYPE (x) != error_mark_node
1778 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1780 tree element = TREE_TYPE (x);
1782 while (TREE_CODE (element) == ARRAY_TYPE)
1783 element = TREE_TYPE (element);
1784 if (TREE_CODE (element) == RECORD_TYPE
1785 || TREE_CODE (element) == UNION_TYPE)
1786 scope->incomplete_list = tree_cons (NULL_TREE, x,
1787 scope->incomplete_list);
1791 /* Put decls on list in reverse order.
1792 We will reverse them later if necessary. */
1793 TREE_CHAIN (x) = scope->names;
1794 scope->names = x;
1796 return x;
1799 /* Record X as belonging to the global scope (C99 "file scope").
1800 This is used only internally by the Objective-C front end,
1801 and is limited to its needs. It will hork if there is _any_
1802 visible binding for X (not just a global one). */
1803 tree
1804 pushdecl_top_level (tree x)
1806 tree name, old;
1808 if (TREE_CODE (x) != VAR_DECL)
1809 abort ();
1811 name = DECL_NAME (x);
1812 old = IDENTIFIER_SYMBOL_VALUE (name);
1814 if (old)
1816 if (DECL_CONTEXT (old))
1817 abort ();
1819 if (!duplicate_decls (x, old, 0, false))
1820 abort ();
1822 return old;
1825 DECL_CONTEXT (x) = current_file_decl;
1826 IDENTIFIER_SYMBOL_VALUE (name) = x;
1827 TREE_CHAIN (x) = global_binding_level->names;
1828 global_binding_level->names = x;
1829 return x;
1832 /* Record X as belonging to the outermost scope of the current
1833 function. This is used only internally, by c_make_fname_decl and
1834 build_external_ref, and is limited to their needs. The NAME is
1835 provided as a separate argument because build_external_ref wants to
1836 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1837 called; if there is any preexisting decl for this identifier, it is
1838 an ICE. */
1839 tree
1840 pushdecl_function_level (tree x, tree name)
1842 struct binding_level *scope;
1844 scope = current_binding_level;
1845 while (scope->function_body == 0)
1846 scope = scope->level_chain;
1847 if (!scope)
1848 abort ();
1850 if (x == error_mark_node)
1851 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1852 scope->shadowed);
1853 else if (TREE_CODE (x) == VAR_DECL)
1855 if (name != DECL_NAME (x))
1856 abort ();
1857 if (IDENTIFIER_SYMBOL_VALUE (name))
1858 abort ();
1860 DECL_CONTEXT (x) = current_function_decl;
1861 TREE_CHAIN (x) = scope->names;
1862 scope->names = x;
1865 IDENTIFIER_SYMBOL_VALUE (name) = x;
1866 return x;
1869 /* Generate an implicit declaration for identifier FUNCTIONID as a
1870 function of type int (). */
1872 tree
1873 implicitly_declare (tree functionid)
1875 tree decl = any_external_decl (functionid);
1877 if (decl && decl != error_mark_node)
1879 /* Implicit declaration of a function already declared
1880 (somehow) in a different scope, or as a built-in.
1881 If this is the first time this has happened, warn;
1882 then recycle the old declaration. */
1883 if (!C_DECL_IMPLICIT (decl))
1885 implicit_decl_warning (DECL_NAME (decl));
1886 if (! C_DECL_FILE_SCOPE (decl))
1887 warning_with_decl (decl, "previous declaration of `%s'");
1888 C_DECL_IMPLICIT (decl) = 1;
1890 /* If this function is global, then it must already be in the
1891 global binding level, so there's no need to push it again. */
1892 if (current_binding_level == global_binding_level)
1893 return decl;
1894 /* If this is a local declaration, make a copy; we can't have
1895 the same DECL listed in two different binding levels. */
1896 return pushdecl (copy_node (decl));
1899 /* Not seen before. */
1900 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1901 DECL_EXTERNAL (decl) = 1;
1902 TREE_PUBLIC (decl) = 1;
1903 C_DECL_IMPLICIT (decl) = 1;
1904 implicit_decl_warning (functionid);
1906 /* ANSI standard says implicit declarations are in the innermost block.
1907 So we record the decl in the standard fashion. */
1908 decl = pushdecl (decl);
1910 /* No need to call objc_check_decl here - it's a function type. */
1911 rest_of_decl_compilation (decl, NULL, 0, 0);
1913 /* Write a record describing this implicit function declaration to the
1914 prototypes file (if requested). */
1916 gen_aux_info_record (decl, 0, 1, 0);
1918 /* Possibly apply some default attributes to this implicit declaration. */
1919 decl_attributes (&decl, NULL_TREE, 0);
1921 return decl;
1924 static void
1925 implicit_decl_warning (tree id)
1927 const char *name = IDENTIFIER_POINTER (id);
1928 if (mesg_implicit_function_declaration == 2)
1929 error ("implicit declaration of function `%s'", name);
1930 else if (mesg_implicit_function_declaration == 1)
1931 warning ("implicit declaration of function `%s'", name);
1934 /* Return zero if the declaration NEWDECL is valid
1935 when the declaration OLDDECL (assumed to be for the same name)
1936 has already been seen.
1937 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1938 and 3 if it is a conflicting declaration. */
1940 static int
1941 redeclaration_error_message (tree newdecl, tree olddecl)
1943 if (TREE_CODE (newdecl) == TYPE_DECL)
1945 /* Do not complain about type redeclarations where at least one
1946 declaration was in a system header. */
1947 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1948 return 0;
1949 return 1;
1951 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1953 /* Declarations of functions can insist on internal linkage
1954 but they can't be inconsistent with internal linkage,
1955 so there can be no error on that account.
1956 However defining the same name twice is no good. */
1957 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1958 /* However, defining once as extern inline and a second
1959 time in another way is ok. */
1960 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1961 && ! (DECL_DECLARED_INLINE_P (newdecl)
1962 && DECL_EXTERNAL (newdecl))))
1963 return 1;
1964 return 0;
1966 else if (C_DECL_FILE_SCOPE (newdecl))
1968 /* Objects declared at top level: */
1969 /* If at least one is a reference, it's ok. */
1970 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1971 return 0;
1972 /* Reject two definitions. */
1973 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1974 return 1;
1975 /* Now we have two tentative defs, or one tentative and one real def. */
1976 /* Insist that the linkage match. */
1977 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1978 return 3;
1979 return 0;
1981 else if (current_binding_level->parm_flag
1982 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1983 return 0;
1984 else
1986 /* Newdecl has block scope. If olddecl has block scope also, then
1987 reject two definitions, and reject a definition together with an
1988 external reference. Otherwise, it is OK, because newdecl must
1989 be an extern reference to olddecl. */
1990 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1991 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1992 return 2;
1993 return 0;
1997 /* Get the LABEL_DECL corresponding to identifier ID as a label.
1998 Create one if none exists so far for the current function.
1999 This function is called for both label definitions and label references. */
2001 tree
2002 lookup_label (tree id)
2004 tree decl = IDENTIFIER_LABEL_VALUE (id);
2006 if (current_function_decl == 0)
2008 error ("label %s referenced outside of any function",
2009 IDENTIFIER_POINTER (id));
2010 return 0;
2013 /* Use a label already defined or ref'd with this name. */
2014 if (decl != 0)
2016 /* But not if it is inherited and wasn't declared to be inheritable. */
2017 if (DECL_CONTEXT (decl) != current_function_decl
2018 && ! C_DECLARED_LABEL_FLAG (decl))
2019 return shadow_label (id);
2020 return decl;
2023 decl = build_decl (LABEL_DECL, id, void_type_node);
2025 /* A label not explicitly declared must be local to where it's ref'd. */
2026 DECL_CONTEXT (decl) = current_function_decl;
2028 DECL_MODE (decl) = VOIDmode;
2030 /* Say where one reference is to the label,
2031 for the sake of the error if it is not defined. */
2032 DECL_SOURCE_LOCATION (decl) = input_location;
2034 IDENTIFIER_LABEL_VALUE (id) = decl;
2036 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2038 return decl;
2041 /* Make a label named NAME in the current function,
2042 shadowing silently any that may be inherited from containing functions
2043 or containing scopes.
2045 Note that valid use, if the label being shadowed
2046 comes from another scope in the same function,
2047 requires calling declare_nonlocal_label right away. */
2049 tree
2050 shadow_label (tree name)
2052 tree decl = IDENTIFIER_LABEL_VALUE (name);
2054 if (decl != 0)
2056 tree dup;
2058 /* Check to make sure that the label hasn't already been declared
2059 at this label scope */
2060 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2061 if (TREE_VALUE (dup) == decl)
2063 error ("duplicate label declaration `%s'",
2064 IDENTIFIER_POINTER (name));
2065 error_with_decl (TREE_VALUE (dup),
2066 "this is a previous declaration");
2067 /* Just use the previous declaration. */
2068 return lookup_label (name);
2071 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2072 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2075 return lookup_label (name);
2078 /* Define a label, specifying the location in the source file.
2079 Return the LABEL_DECL node for the label, if the definition is valid.
2080 Otherwise return 0. */
2082 tree
2083 define_label (location_t location, tree name)
2085 tree decl = lookup_label (name);
2087 /* If label with this name is known from an outer context, shadow it. */
2088 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2090 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2091 IDENTIFIER_LABEL_VALUE (name) = 0;
2092 decl = lookup_label (name);
2095 if (warn_traditional && !in_system_header && lookup_name (name))
2096 warning ("%Htraditional C lacks a separate namespace for labels, "
2097 "identifier `%s' conflicts", &location, IDENTIFIER_POINTER (name));
2099 if (DECL_INITIAL (decl) != 0)
2101 error ("%Hduplicate label `%s'", &location, IDENTIFIER_POINTER (name));
2102 return 0;
2104 else
2106 /* Mark label as having been defined. */
2107 DECL_INITIAL (decl) = error_mark_node;
2108 /* Say where in the source. */
2109 DECL_SOURCE_LOCATION (decl) = location;
2110 return decl;
2114 /* Return the list of declarations of the current level.
2115 Note that this list is in reverse order unless/until
2116 you nreverse it; and when you do nreverse it, you must
2117 store the result back using `storedecls' or you will lose. */
2119 tree
2120 getdecls (void)
2122 return current_binding_level->names;
2125 /* Return the list of type-tags (for structs, etc) of the current level. */
2127 tree
2128 gettags (void)
2130 return current_binding_level->tags;
2133 /* Store the list of declarations of the current level.
2134 This is done for the parameter declarations of a function being defined,
2135 after they are modified in the light of any missing parameters. */
2137 static void
2138 storedecls (tree decls)
2140 current_binding_level->names = decls;
2143 /* Similarly, store the list of tags of the current level. */
2145 static void
2146 storetags (tree tags)
2148 current_binding_level->tags = tags;
2151 /* Given NAME, an IDENTIFIER_NODE,
2152 return the structure (or union or enum) definition for that name.
2153 If THISLEVEL_ONLY is nonzero, searches only the current_binding_level.
2154 CODE says which kind of type the caller wants;
2155 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2156 If the wrong kind of type is found, an error is reported. */
2158 static tree
2159 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2161 tree tag = IDENTIFIER_TAG_VALUE (name);
2162 int thislevel = 0;
2164 if (!tag)
2165 return 0;
2167 /* We only care about whether it's in this level if
2168 thislevel_only was set or it might be a type clash. */
2169 if (thislevel_only || TREE_CODE (tag) != code)
2171 if (current_binding_level == global_binding_level
2172 || purpose_member (name, current_binding_level->tags))
2173 thislevel = 1;
2176 if (thislevel_only && !thislevel)
2177 return 0;
2179 if (TREE_CODE (tag) != code)
2181 /* Definition isn't the kind we were looking for. */
2182 pending_invalid_xref = name;
2183 pending_invalid_xref_location = input_location;
2185 /* If in the same binding level as a declaration as a tag
2186 of a different type, this must not be allowed to
2187 shadow that tag, so give the error immediately.
2188 (For example, "struct foo; union foo;" is invalid.) */
2189 if (thislevel)
2190 pending_xref_error ();
2192 return tag;
2195 /* Print an error message now
2196 for a recent invalid struct, union or enum cross reference.
2197 We don't print them immediately because they are not invalid
2198 when used in the `struct foo;' construct for shadowing. */
2200 void
2201 pending_xref_error (void)
2203 if (pending_invalid_xref != 0)
2204 error ("%H`%s' defined as wrong kind of tag",
2205 &pending_invalid_xref_location,
2206 IDENTIFIER_POINTER (pending_invalid_xref));
2207 pending_invalid_xref = 0;
2211 /* Look up NAME in the current binding level and its superiors
2212 in the namespace of variables, functions and typedefs.
2213 Return a ..._DECL node of some kind representing its definition,
2214 or return 0 if it is undefined. */
2216 tree
2217 lookup_name (tree name)
2219 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2220 if (decl == 0 || decl == error_mark_node)
2221 return decl;
2222 if (C_DECL_INVISIBLE (decl))
2223 return 0;
2224 return decl;
2227 /* Similar to `lookup_name' but look only at the current binding level. */
2229 static tree
2230 lookup_name_current_level (tree name)
2232 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2234 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2235 return 0;
2237 if (current_binding_level == global_binding_level)
2238 return decl;
2240 /* Scan the current scope for a decl with name NAME. */
2241 if (chain_member (decl, current_binding_level->names))
2242 return decl;
2244 return 0;
2247 /* Create the predefined scalar types of C,
2248 and some nodes representing standard constants (0, 1, (void *) 0).
2249 Initialize the global binding level.
2250 Make definitions for built-in primitive functions. */
2252 void
2253 c_init_decl_processing (void)
2255 tree endlink;
2256 tree ptr_ftype_void, ptr_ftype_ptr;
2257 location_t save_loc = input_location;
2259 /* Adds some ggc roots, and reserved words for c-parse.in. */
2260 c_parse_init ();
2262 current_function_decl = NULL;
2263 named_labels = NULL;
2264 current_binding_level = NULL_BINDING_LEVEL;
2265 free_binding_level = NULL_BINDING_LEVEL;
2267 /* Make the binding_level structure for global names. */
2268 pushlevel (0);
2269 global_binding_level = current_binding_level;
2270 /* Declarations from c_common_nodes_and_builtins must not be associated
2271 with this input file, lest we get differences between using and not
2272 using preprocessed headers. */
2273 input_location.file = "<internal>";
2274 input_location.line = 0;
2276 /* Make the DECL for the toplevel file scope. */
2277 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2279 build_common_tree_nodes (flag_signed_char);
2281 c_common_nodes_and_builtins ();
2283 boolean_type_node = integer_type_node;
2284 boolean_true_node = integer_one_node;
2285 boolean_false_node = integer_zero_node;
2287 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2288 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2289 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2290 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2291 TYPE_PRECISION (c_bool_type_node) = 1;
2292 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2293 c_bool_type_node));
2294 c_bool_false_node = build_int_2 (0, 0);
2295 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2296 c_bool_true_node = build_int_2 (1, 0);
2297 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2299 endlink = void_list_node;
2300 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2301 ptr_ftype_ptr
2302 = build_function_type (ptr_type_node,
2303 tree_cons (NULL_TREE, ptr_type_node, endlink));
2305 input_location = save_loc;
2307 pedantic_lvalues = pedantic;
2309 make_fname_decl = c_make_fname_decl;
2310 start_fname_decls ();
2312 builtin_decls = global_binding_level->names;
2315 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2316 decl, NAME is the initialization string and TYPE_DEP indicates whether
2317 NAME depended on the type of the function. As we don't yet implement
2318 delayed emission of static data, we mark the decl as emitted
2319 so it is not placed in the output. Anything using it must therefore pull
2320 out the STRING_CST initializer directly. This does mean that these names
2321 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2323 static tree
2324 c_make_fname_decl (tree id, int type_dep)
2326 const char *name = fname_as_string (type_dep);
2327 tree decl, type, init;
2328 size_t length = strlen (name);
2330 type = build_array_type
2331 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2332 build_index_type (size_int (length)));
2334 decl = build_decl (VAR_DECL, id, type);
2336 TREE_STATIC (decl) = 1;
2337 TREE_READONLY (decl) = 1;
2338 DECL_ARTIFICIAL (decl) = 1;
2340 init = build_string (length + 1, name);
2341 TREE_TYPE (init) = type;
2342 DECL_INITIAL (decl) = init;
2344 TREE_USED (decl) = 1;
2346 if (current_function_decl)
2347 pushdecl_function_level (decl, DECL_NAME (decl));
2349 finish_decl (decl, init, NULL_TREE);
2351 return decl;
2354 /* Return a definition for a builtin function named NAME and whose data type
2355 is TYPE. TYPE should be a function type with argument types.
2356 FUNCTION_CODE tells later passes how to compile calls to this function.
2357 See tree.h for its possible values.
2359 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2360 the name to be called if we can't opencode the function. If
2361 ATTRS is nonzero, use that for the function's attribute list. */
2363 tree
2364 builtin_function (const char *name, tree type, int function_code,
2365 enum built_in_class class, const char *library_name,
2366 tree attrs)
2368 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2369 DECL_EXTERNAL (decl) = 1;
2370 TREE_PUBLIC (decl) = 1;
2371 if (library_name)
2372 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2373 make_decl_rtl (decl, NULL);
2374 pushdecl (decl);
2375 DECL_BUILT_IN_CLASS (decl) = class;
2376 DECL_FUNCTION_CODE (decl) = function_code;
2378 /* Warn if a function in the namespace for users
2379 is used without an occasion to consider it declared. */
2380 if (name[0] != '_' || name[1] != '_')
2381 C_DECL_INVISIBLE (decl) = 1;
2383 /* Possibly apply some default attributes to this built-in function. */
2384 if (attrs)
2385 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2386 else
2387 decl_attributes (&decl, NULL_TREE, 0);
2389 return decl;
2392 /* Apply default attributes to a function, if a system function with default
2393 attributes. */
2395 void
2396 c_insert_default_attributes (tree decl)
2398 if (!TREE_PUBLIC (decl))
2399 return;
2400 c_common_insert_default_attributes (decl);
2403 /* Called when a declaration is seen that contains no names to declare.
2404 If its type is a reference to a structure, union or enum inherited
2405 from a containing scope, shadow that tag name for the current scope
2406 with a forward reference.
2407 If its type defines a new named structure or union
2408 or defines an enum, it is valid but we need not do anything here.
2409 Otherwise, it is an error. */
2411 void
2412 shadow_tag (tree declspecs)
2414 shadow_tag_warned (declspecs, 0);
2417 void
2418 shadow_tag_warned (tree declspecs, int warned)
2421 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2422 no pedwarn. */
2424 int found_tag = 0;
2425 tree link;
2426 tree specs, attrs;
2428 pending_invalid_xref = 0;
2430 /* Remove the attributes from declspecs, since they will confuse the
2431 following code. */
2432 split_specs_attrs (declspecs, &specs, &attrs);
2434 for (link = specs; link; link = TREE_CHAIN (link))
2436 tree value = TREE_VALUE (link);
2437 enum tree_code code = TREE_CODE (value);
2439 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2440 /* Used to test also that TYPE_SIZE (value) != 0.
2441 That caused warning for `struct foo;' at top level in the file. */
2443 tree name = TYPE_NAME (value);
2444 tree t;
2446 found_tag++;
2448 if (name == 0)
2450 if (warned != 1 && code != ENUMERAL_TYPE)
2451 /* Empty unnamed enum OK */
2453 pedwarn ("unnamed struct/union that defines no instances");
2454 warned = 1;
2457 else
2459 t = lookup_tag (code, name, 1);
2461 if (t == 0)
2463 t = make_node (code);
2464 pushtag (name, t);
2468 else
2470 if (!warned && ! in_system_header)
2472 warning ("useless keyword or type name in empty declaration");
2473 warned = 2;
2478 if (found_tag > 1)
2479 error ("two types specified in one empty declaration");
2481 if (warned != 1)
2483 if (found_tag == 0)
2484 pedwarn ("empty declaration");
2488 /* Construct an array declarator. EXPR is the expression inside [], or
2489 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2490 to the pointer to which a parameter array is converted). STATIC_P is
2491 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2492 is nonzero is the array is [*], a VLA of unspecified length which is
2493 nevertheless a complete type (not currently implemented by GCC),
2494 zero otherwise. The declarator is constructed as an ARRAY_REF
2495 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2496 left of the [] (filled by in set_array_declarator_type) and operand 1
2497 is the expression inside; whose TREE_TYPE is the type qualifiers and
2498 which has TREE_STATIC set if "static" is used. */
2500 tree
2501 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2503 tree decl;
2504 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2505 TREE_TYPE (decl) = quals;
2506 TREE_STATIC (decl) = (static_p ? 1 : 0);
2507 if (pedantic && !flag_isoc99)
2509 if (static_p || quals != NULL_TREE)
2510 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2511 if (vla_unspec_p)
2512 pedwarn ("ISO C90 does not support `[*]' array declarators");
2514 if (vla_unspec_p)
2515 warning ("GCC does not yet properly implement `[*]' array declarators");
2516 return decl;
2519 /* Set the type of an array declarator. DECL is the declarator, as
2520 constructed by build_array_declarator; TYPE is what appears on the left
2521 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2522 abstract declarator, zero otherwise; this is used to reject static and
2523 type qualifiers in abstract declarators, where they are not in the
2524 C99 grammar. */
2526 tree
2527 set_array_declarator_type (tree decl, tree type, int abstract_p)
2529 TREE_OPERAND (decl, 0) = type;
2530 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2531 error ("static or type qualifiers in abstract declarator");
2532 return decl;
2535 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2537 tree
2538 groktypename (tree typename)
2540 tree specs, attrs;
2542 if (TREE_CODE (typename) != TREE_LIST)
2543 return typename;
2545 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2547 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2549 /* Apply attributes. */
2550 decl_attributes (&typename, attrs, 0);
2552 return typename;
2555 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2557 tree
2558 groktypename_in_parm_context (tree typename)
2560 if (TREE_CODE (typename) != TREE_LIST)
2561 return typename;
2562 return grokdeclarator (TREE_VALUE (typename),
2563 TREE_PURPOSE (typename),
2564 PARM, 0);
2567 /* Decode a declarator in an ordinary declaration or data definition.
2568 This is called as soon as the type information and variable name
2569 have been parsed, before parsing the initializer if any.
2570 Here we create the ..._DECL node, fill in its type,
2571 and put it on the list of decls for the current context.
2572 The ..._DECL node is returned as the value.
2574 Exception: for arrays where the length is not specified,
2575 the type is left null, to be filled in by `finish_decl'.
2577 Function definitions do not come here; they go to start_function
2578 instead. However, external and forward declarations of functions
2579 do go through here. Structure field declarations are done by
2580 grokfield and not through here. */
2582 tree
2583 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2585 tree decl;
2586 tree tem;
2588 /* An object declared as __attribute__((deprecated)) suppresses
2589 warnings of uses of other deprecated items. */
2590 if (lookup_attribute ("deprecated", attributes))
2591 deprecated_state = DEPRECATED_SUPPRESS;
2593 decl = grokdeclarator (declarator, declspecs,
2594 NORMAL, initialized);
2596 deprecated_state = DEPRECATED_NORMAL;
2598 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2599 && MAIN_NAME_P (DECL_NAME (decl)))
2600 warning_with_decl (decl, "`%s' is usually a function");
2602 if (initialized)
2603 /* Is it valid for this decl to have an initializer at all?
2604 If not, set INITIALIZED to zero, which will indirectly
2605 tell `finish_decl' to ignore the initializer once it is parsed. */
2606 switch (TREE_CODE (decl))
2608 case TYPE_DECL:
2609 error ("typedef `%s' is initialized (use __typeof__ instead)",
2610 IDENTIFIER_POINTER (DECL_NAME (decl)));
2611 initialized = 0;
2612 break;
2614 case FUNCTION_DECL:
2615 error ("function `%s' is initialized like a variable",
2616 IDENTIFIER_POINTER (DECL_NAME (decl)));
2617 initialized = 0;
2618 break;
2620 case PARM_DECL:
2621 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2622 error ("parameter `%s' is initialized",
2623 IDENTIFIER_POINTER (DECL_NAME (decl)));
2624 initialized = 0;
2625 break;
2627 default:
2628 /* Don't allow initializations for incomplete types
2629 except for arrays which might be completed by the initialization. */
2631 /* This can happen if the array size is an undefined macro. We already
2632 gave a warning, so we don't need another one. */
2633 if (TREE_TYPE (decl) == error_mark_node)
2634 initialized = 0;
2635 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2637 /* A complete type is ok if size is fixed. */
2639 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2640 || C_DECL_VARIABLE_SIZE (decl))
2642 error ("variable-sized object may not be initialized");
2643 initialized = 0;
2646 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2648 error ("variable `%s' has initializer but incomplete type",
2649 IDENTIFIER_POINTER (DECL_NAME (decl)));
2650 initialized = 0;
2652 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2654 error ("elements of array `%s' have incomplete type",
2655 IDENTIFIER_POINTER (DECL_NAME (decl)));
2656 initialized = 0;
2660 if (initialized)
2662 #if 0
2663 /* Seems redundant with grokdeclarator. */
2664 if (current_binding_level != global_binding_level
2665 && DECL_EXTERNAL (decl)
2666 && TREE_CODE (decl) != FUNCTION_DECL)
2667 warning ("declaration of `%s' has `extern' and is initialized",
2668 IDENTIFIER_POINTER (DECL_NAME (decl)));
2669 #endif
2670 DECL_EXTERNAL (decl) = 0;
2671 if (current_binding_level == global_binding_level)
2672 TREE_STATIC (decl) = 1;
2674 /* Tell `pushdecl' this is an initialized decl
2675 even though we don't yet have the initializer expression.
2676 Also tell `finish_decl' it may store the real initializer. */
2677 DECL_INITIAL (decl) = error_mark_node;
2680 /* If this is a function declaration, write a record describing it to the
2681 prototypes file (if requested). */
2683 if (TREE_CODE (decl) == FUNCTION_DECL)
2684 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2686 /* ANSI specifies that a tentative definition which is not merged with
2687 a non-tentative definition behaves exactly like a definition with an
2688 initializer equal to zero. (Section 3.7.2)
2690 -fno-common gives strict ANSI behavior, though this tends to break
2691 a large body of code that grew up without this rule.
2693 Thread-local variables are never common, since there's no entrenched
2694 body of code to break, and it allows more efficient variable references
2695 in the presence of dynamic linking. */
2697 if (TREE_CODE (decl) == VAR_DECL
2698 && !initialized
2699 && TREE_PUBLIC (decl)
2700 && !DECL_THREAD_LOCAL (decl)
2701 && !flag_no_common)
2702 DECL_COMMON (decl) = 1;
2704 /* Set attributes here so if duplicate decl, will have proper attributes. */
2705 decl_attributes (&decl, attributes, 0);
2707 /* If #pragma weak was used, mark the decl weak now. */
2708 if (current_binding_level == global_binding_level)
2709 maybe_apply_pragma_weak (decl);
2711 if (TREE_CODE (decl) == FUNCTION_DECL
2712 && DECL_DECLARED_INLINE_P (decl)
2713 && DECL_UNINLINABLE (decl)
2714 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2715 warning_with_decl (decl,
2716 "inline function `%s' given attribute noinline");
2718 /* Add this decl to the current binding level.
2719 TEM may equal DECL or it may be a previous decl of the same name. */
2720 tem = pushdecl (decl);
2722 /* For a local variable, define the RTL now. */
2723 if (current_binding_level != global_binding_level
2724 /* But not if this is a duplicate decl
2725 and we preserved the rtl from the previous one
2726 (which may or may not happen). */
2727 && !DECL_RTL_SET_P (tem)
2728 && C_DECL_FILE_SCOPE (tem))
2730 if (TREE_TYPE (tem) != error_mark_node
2731 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2732 expand_decl (tem);
2733 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2734 && DECL_INITIAL (tem) != 0)
2735 expand_decl (tem);
2738 return tem;
2741 /* Finish processing of a declaration;
2742 install its initial value.
2743 If the length of an array type is not known before,
2744 it must be determined now, from the initial value, or it is an error. */
2746 void
2747 finish_decl (tree decl, tree init, tree asmspec_tree)
2749 tree type = TREE_TYPE (decl);
2750 int was_incomplete = (DECL_SIZE (decl) == 0);
2751 const char *asmspec = 0;
2753 /* If a name was specified, get the string. */
2754 if (current_binding_level == global_binding_level)
2755 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2756 if (asmspec_tree)
2757 asmspec = TREE_STRING_POINTER (asmspec_tree);
2759 /* If `start_decl' didn't like having an initialization, ignore it now. */
2760 if (init != 0 && DECL_INITIAL (decl) == 0)
2761 init = 0;
2763 /* Don't crash if parm is initialized. */
2764 if (TREE_CODE (decl) == PARM_DECL)
2765 init = 0;
2767 if (init)
2768 store_init_value (decl, init);
2770 /* Deduce size of array from initialization, if not already known */
2771 if (TREE_CODE (type) == ARRAY_TYPE
2772 && TYPE_DOMAIN (type) == 0
2773 && TREE_CODE (decl) != TYPE_DECL)
2775 int do_default
2776 = (TREE_STATIC (decl)
2777 /* Even if pedantic, an external linkage array
2778 may have incomplete type at first. */
2779 ? pedantic && !TREE_PUBLIC (decl)
2780 : !DECL_EXTERNAL (decl));
2781 int failure
2782 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2784 /* Get the completed type made by complete_array_type. */
2785 type = TREE_TYPE (decl);
2787 if (failure == 1)
2788 error_with_decl (decl, "initializer fails to determine size of `%s'");
2790 else if (failure == 2)
2792 if (do_default)
2793 error_with_decl (decl, "array size missing in `%s'");
2794 /* If a `static' var's size isn't known,
2795 make it extern as well as static, so it does not get
2796 allocated.
2797 If it is not `static', then do not mark extern;
2798 finish_incomplete_decl will give it a default size
2799 and it will get allocated. */
2800 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2801 DECL_EXTERNAL (decl) = 1;
2804 /* TYPE_MAX_VALUE is always one less than the number of elements
2805 in the array, because we start counting at zero. Therefore,
2806 warn only if the value is less than zero. */
2807 else if (pedantic && TYPE_DOMAIN (type) != 0
2808 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2809 error_with_decl (decl, "zero or negative size array `%s'");
2811 layout_decl (decl, 0);
2814 if (TREE_CODE (decl) == VAR_DECL)
2816 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2817 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2818 layout_decl (decl, 0);
2820 if (DECL_SIZE (decl) == 0
2821 /* Don't give an error if we already gave one earlier. */
2822 && TREE_TYPE (decl) != error_mark_node
2823 && (TREE_STATIC (decl)
2825 /* A static variable with an incomplete type
2826 is an error if it is initialized.
2827 Also if it is not file scope.
2828 Otherwise, let it through, but if it is not `extern'
2829 then it may cause an error message later. */
2830 (DECL_INITIAL (decl) != 0
2831 || !C_DECL_FILE_SCOPE (decl))
2833 /* An automatic variable with an incomplete type
2834 is an error. */
2835 !DECL_EXTERNAL (decl)))
2837 error_with_decl (decl, "storage size of `%s' isn't known");
2838 TREE_TYPE (decl) = error_mark_node;
2841 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2842 && DECL_SIZE (decl) != 0)
2844 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2845 constant_expression_warning (DECL_SIZE (decl));
2846 else
2847 error_with_decl (decl, "storage size of `%s' isn't constant");
2850 if (TREE_USED (type))
2851 TREE_USED (decl) = 1;
2854 /* If this is a function and an assembler name is specified, reset DECL_RTL
2855 so we can give it its new name. Also, update built_in_decls if it
2856 was a normal built-in. */
2857 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2859 /* ASMSPEC is given, and not the name of a register. Mark the
2860 name with a star so assemble_name won't munge it. */
2861 char *starred = alloca (strlen (asmspec) + 2);
2862 starred[0] = '*';
2863 strcpy (starred + 1, asmspec);
2865 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2867 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2868 SET_DECL_RTL (builtin, NULL_RTX);
2869 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2870 #ifdef TARGET_MEM_FUNCTIONS
2871 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2872 init_block_move_fn (starred);
2873 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2874 init_block_clear_fn (starred);
2875 #else
2876 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2877 init_block_move_fn (starred);
2878 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2879 init_block_clear_fn (starred);
2880 #endif
2882 SET_DECL_RTL (decl, NULL_RTX);
2883 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2886 /* Output the assembler code and/or RTL code for variables and functions,
2887 unless the type is an undefined structure or union.
2888 If not, it will get done when the type is completed. */
2890 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2892 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2893 if (c_dialect_objc ())
2894 objc_check_decl (decl);
2896 if (C_DECL_FILE_SCOPE (decl))
2898 if (DECL_INITIAL (decl) == NULL_TREE
2899 || DECL_INITIAL (decl) == error_mark_node)
2900 /* Don't output anything
2901 when a tentative file-scope definition is seen.
2902 But at end of compilation, do output code for them. */
2903 DECL_DEFER_OUTPUT (decl) = 1;
2904 rest_of_decl_compilation (decl, asmspec, true, 0);
2906 else
2908 /* This is a local variable. If there is an ASMSPEC, the
2909 user has requested that we handle it specially. */
2910 if (asmspec)
2912 /* In conjunction with an ASMSPEC, the `register'
2913 keyword indicates that we should place the variable
2914 in a particular register. */
2915 if (DECL_REGISTER (decl))
2916 DECL_C_HARD_REGISTER (decl) = 1;
2918 /* If this is not a static variable, issue a warning.
2919 It doesn't make any sense to give an ASMSPEC for an
2920 ordinary, non-register local variable. Historically,
2921 GCC has accepted -- but ignored -- the ASMSPEC in
2922 this case. */
2923 if (TREE_CODE (decl) == VAR_DECL
2924 && !DECL_REGISTER (decl)
2925 && !TREE_STATIC (decl))
2926 warning_with_decl (decl,
2927 "ignoring asm-specifier for non-static local variable `%s'");
2928 else
2929 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2932 if (TREE_CODE (decl) != FUNCTION_DECL)
2933 add_decl_stmt (decl);
2936 if (!C_DECL_FILE_SCOPE (decl))
2938 /* Recompute the RTL of a local array now
2939 if it used to be an incomplete type. */
2940 if (was_incomplete
2941 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2943 /* If we used it already as memory, it must stay in memory. */
2944 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2945 /* If it's still incomplete now, no init will save it. */
2946 if (DECL_SIZE (decl) == 0)
2947 DECL_INITIAL (decl) = 0;
2952 /* If this was marked 'used', be sure it will be output. */
2953 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2954 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2956 if (TREE_CODE (decl) == TYPE_DECL)
2958 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2959 if (c_dialect_objc ())
2960 objc_check_decl (decl);
2961 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2964 /* At the end of a declaration, throw away any variable type sizes
2965 of types defined inside that declaration. There is no use
2966 computing them in the following function definition. */
2967 if (current_binding_level == global_binding_level)
2968 get_pending_sizes ();
2970 /* Install a cleanup (aka destructor) if one was given. */
2971 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2973 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2974 if (attr)
2976 static bool eh_initialized_p;
2978 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2979 tree cleanup_decl = lookup_name (cleanup_id);
2980 tree cleanup;
2982 /* Build "cleanup(&decl)" for the destructor. */
2983 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2984 cleanup = build_tree_list (NULL_TREE, cleanup);
2985 cleanup = build_function_call (cleanup_decl, cleanup);
2987 /* Don't warn about decl unused; the cleanup uses it. */
2988 TREE_USED (decl) = 1;
2990 /* Initialize EH, if we've been told to do so. */
2991 if (flag_exceptions && !eh_initialized_p)
2993 eh_initialized_p = true;
2994 eh_personality_libfunc
2995 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2996 ? "__gcc_personality_sj0"
2997 : "__gcc_personality_v0");
2998 using_eh_for_cleanups ();
3001 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3006 /* Given a parsed parameter declaration,
3007 decode it into a PARM_DECL and push that on the current binding level.
3008 Also, for the sake of forward parm decls,
3009 record the given order of parms in `parm_order'. */
3011 void
3012 push_parm_decl (tree parm)
3014 tree decl;
3015 int old_immediate_size_expand = immediate_size_expand;
3016 /* Don't try computing parm sizes now -- wait till fn is called. */
3017 immediate_size_expand = 0;
3019 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3020 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3021 decl_attributes (&decl, TREE_VALUE (parm), 0);
3023 #if 0
3024 if (DECL_NAME (decl))
3026 tree olddecl;
3027 olddecl = lookup_name (DECL_NAME (decl));
3028 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
3029 pedwarn_with_decl (decl,
3030 "ISO C forbids parameter `%s' shadowing typedef");
3032 #endif
3034 decl = pushdecl (decl);
3036 immediate_size_expand = old_immediate_size_expand;
3038 current_binding_level->parm_order
3039 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3041 /* Add this decl to the current binding level. */
3042 finish_decl (decl, NULL_TREE, NULL_TREE);
3045 /* Clear the given order of parms in `parm_order'.
3046 Used at start of parm list,
3047 and also at semicolon terminating forward decls. */
3049 void
3050 clear_parm_order (void)
3052 current_binding_level->parm_order = NULL_TREE;
3055 static GTY(()) int compound_literal_number;
3057 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3058 literal, which may be an incomplete array type completed by the
3059 initializer; INIT is a CONSTRUCTOR that initializes the compound
3060 literal. */
3062 tree
3063 build_compound_literal (tree type, tree init)
3065 /* We do not use start_decl here because we have a type, not a declarator;
3066 and do not use finish_decl because the decl should be stored inside
3067 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3068 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3069 tree complit;
3070 tree stmt;
3071 DECL_EXTERNAL (decl) = 0;
3072 TREE_PUBLIC (decl) = 0;
3073 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3074 DECL_CONTEXT (decl) = current_function_decl;
3075 TREE_USED (decl) = 1;
3076 TREE_TYPE (decl) = type;
3077 TREE_READONLY (decl) = TREE_READONLY (type);
3078 store_init_value (decl, init);
3080 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3082 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3083 if (failure)
3084 abort ();
3087 type = TREE_TYPE (decl);
3088 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3089 return error_mark_node;
3091 stmt = build_stmt (DECL_STMT, decl);
3092 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3093 TREE_SIDE_EFFECTS (complit) = 1;
3095 layout_decl (decl, 0);
3097 if (TREE_STATIC (decl))
3099 /* This decl needs a name for the assembler output. We also need
3100 a unique suffix to be added to the name. */
3101 char *name;
3103 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3104 compound_literal_number);
3105 compound_literal_number++;
3106 DECL_NAME (decl) = get_identifier (name);
3107 DECL_DEFER_OUTPUT (decl) = 1;
3108 DECL_COMDAT (decl) = 1;
3109 DECL_ARTIFICIAL (decl) = 1;
3110 pushdecl (decl);
3111 rest_of_decl_compilation (decl, NULL, 1, 0);
3114 return complit;
3117 /* Make TYPE a complete type based on INITIAL_VALUE.
3118 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3119 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3122 complete_array_type (tree type, tree initial_value, int do_default)
3124 tree maxindex = NULL_TREE;
3125 int value = 0;
3127 if (initial_value)
3129 /* Note MAXINDEX is really the maximum index,
3130 one less than the size. */
3131 if (TREE_CODE (initial_value) == STRING_CST)
3133 int eltsize
3134 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3135 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3136 / eltsize) - 1, 0);
3138 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3140 tree elts = CONSTRUCTOR_ELTS (initial_value);
3141 maxindex = build_int_2 (-1, -1);
3142 for (; elts; elts = TREE_CHAIN (elts))
3144 if (TREE_PURPOSE (elts))
3145 maxindex = TREE_PURPOSE (elts);
3146 else
3147 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3148 maxindex, integer_one_node));
3150 maxindex = copy_node (maxindex);
3152 else
3154 /* Make an error message unless that happened already. */
3155 if (initial_value != error_mark_node)
3156 value = 1;
3158 /* Prevent further error messages. */
3159 maxindex = build_int_2 (0, 0);
3163 if (!maxindex)
3165 if (do_default)
3166 maxindex = build_int_2 (0, 0);
3167 value = 2;
3170 if (maxindex)
3172 TYPE_DOMAIN (type) = build_index_type (maxindex);
3173 if (!TREE_TYPE (maxindex))
3174 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3177 /* Lay out the type now that we can get the real answer. */
3179 layout_type (type);
3181 return value;
3184 /* Determine whether TYPE is a structure with a flexible array member,
3185 or a union containing such a structure (possibly recursively). */
3187 static bool
3188 flexible_array_type_p (tree type)
3190 tree x;
3191 switch (TREE_CODE (type))
3193 case RECORD_TYPE:
3194 x = TYPE_FIELDS (type);
3195 if (x == NULL_TREE)
3196 return false;
3197 while (TREE_CHAIN (x) != NULL_TREE)
3198 x = TREE_CHAIN (x);
3199 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3200 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3201 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3202 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3203 return true;
3204 return false;
3205 case UNION_TYPE:
3206 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3208 if (flexible_array_type_p (TREE_TYPE (x)))
3209 return true;
3211 return false;
3212 default:
3213 return false;
3217 /* Given declspecs and a declarator,
3218 determine the name and type of the object declared
3219 and construct a ..._DECL node for it.
3220 (In one case we can return a ..._TYPE node instead.
3221 For invalid input we sometimes return 0.)
3223 DECLSPECS is a chain of tree_list nodes whose value fields
3224 are the storage classes and type specifiers.
3226 DECL_CONTEXT says which syntactic context this declaration is in:
3227 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3228 FUNCDEF for a function definition. Like NORMAL but a few different
3229 error messages in each case. Return value may be zero meaning
3230 this definition is too screwy to try to parse.
3231 PARM for a parameter declaration (either within a function prototype
3232 or before a function body). Make a PARM_DECL, or return void_type_node.
3233 TYPENAME if for a typename (in a cast or sizeof).
3234 Don't make a DECL node; just return the ..._TYPE node.
3235 FIELD for a struct or union field; make a FIELD_DECL.
3236 BITFIELD for a field with specified width.
3237 INITIALIZED is 1 if the decl has an initializer.
3239 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3240 It may also be so in the PARM case, for a prototype where the
3241 argument type is specified but not the name.
3243 This function is where the complicated C meanings of `static'
3244 and `extern' are interpreted. */
3246 static tree
3247 grokdeclarator (tree declarator, tree declspecs,
3248 enum decl_context decl_context, int initialized)
3250 int specbits = 0;
3251 tree spec;
3252 tree type = NULL_TREE;
3253 int longlong = 0;
3254 int constp;
3255 int restrictp;
3256 int volatilep;
3257 int type_quals = TYPE_UNQUALIFIED;
3258 int inlinep;
3259 int explicit_int = 0;
3260 int explicit_char = 0;
3261 int defaulted_int = 0;
3262 tree typedef_decl = 0;
3263 const char *name;
3264 tree typedef_type = 0;
3265 int funcdef_flag = 0;
3266 enum tree_code innermost_code = ERROR_MARK;
3267 int bitfield = 0;
3268 int size_varies = 0;
3269 tree decl_attr = NULL_TREE;
3270 tree array_ptr_quals = NULL_TREE;
3271 int array_parm_static = 0;
3272 tree returned_attrs = NULL_TREE;
3274 if (decl_context == BITFIELD)
3275 bitfield = 1, decl_context = FIELD;
3277 if (decl_context == FUNCDEF)
3278 funcdef_flag = 1, decl_context = NORMAL;
3280 /* Look inside a declarator for the name being declared
3281 and get it as a string, for an error message. */
3283 tree decl = declarator;
3284 name = 0;
3286 while (decl)
3287 switch (TREE_CODE (decl))
3289 case ARRAY_REF:
3290 case INDIRECT_REF:
3291 case CALL_EXPR:
3292 innermost_code = TREE_CODE (decl);
3293 decl = TREE_OPERAND (decl, 0);
3294 break;
3296 case TREE_LIST:
3297 decl = TREE_VALUE (decl);
3298 break;
3300 case IDENTIFIER_NODE:
3301 name = IDENTIFIER_POINTER (decl);
3302 decl = 0;
3303 break;
3305 default:
3306 abort ();
3308 if (name == 0)
3309 name = "type name";
3312 /* A function definition's declarator must have the form of
3313 a function declarator. */
3315 if (funcdef_flag && innermost_code != CALL_EXPR)
3316 return 0;
3318 /* Anything declared one level down from the top level
3319 must be one of the parameters of a function
3320 (because the body is at least two levels down). */
3322 /* If this looks like a function definition, make it one,
3323 even if it occurs where parms are expected.
3324 Then store_parm_decls will reject it and not use it as a parm. */
3325 if (decl_context == NORMAL && !funcdef_flag
3326 && current_binding_level->parm_flag)
3327 decl_context = PARM;
3329 /* Look through the decl specs and record which ones appear.
3330 Some typespecs are defined as built-in typenames.
3331 Others, the ones that are modifiers of other types,
3332 are represented by bits in SPECBITS: set the bits for
3333 the modifiers that appear. Storage class keywords are also in SPECBITS.
3335 If there is a typedef name or a type, store the type in TYPE.
3336 This includes builtin typedefs such as `int'.
3338 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3339 and did not come from a user typedef.
3341 Set LONGLONG if `long' is mentioned twice. */
3343 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3345 tree id = TREE_VALUE (spec);
3347 /* If the entire declaration is itself tagged as deprecated then
3348 suppress reports of deprecated items. */
3349 if (id && TREE_DEPRECATED (id))
3351 if (deprecated_state != DEPRECATED_SUPPRESS)
3352 warn_deprecated_use (id);
3355 if (id == ridpointers[(int) RID_INT])
3356 explicit_int = 1;
3357 if (id == ridpointers[(int) RID_CHAR])
3358 explicit_char = 1;
3360 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3362 enum rid i = C_RID_CODE (id);
3363 if ((int) i <= (int) RID_LAST_MODIFIER)
3365 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3367 if (longlong)
3368 error ("`long long long' is too long for GCC");
3369 else
3371 if (pedantic && !flag_isoc99 && ! in_system_header
3372 && warn_long_long)
3373 pedwarn ("ISO C90 does not support `long long'");
3374 longlong = 1;
3377 else if (specbits & (1 << (int) i))
3379 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3381 if (!flag_isoc99)
3382 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3384 else
3385 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3388 /* Diagnose "__thread extern". Recall that this list
3389 is in the reverse order seen in the text. */
3390 if (i == RID_THREAD
3391 && (specbits & (1 << (int) RID_EXTERN
3392 | 1 << (int) RID_STATIC)))
3394 if (specbits & 1 << (int) RID_EXTERN)
3395 error ("`__thread' before `extern'");
3396 else
3397 error ("`__thread' before `static'");
3400 specbits |= 1 << (int) i;
3401 goto found;
3404 if (type)
3405 error ("two or more data types in declaration of `%s'", name);
3406 /* Actual typedefs come to us as TYPE_DECL nodes. */
3407 else if (TREE_CODE (id) == TYPE_DECL)
3409 if (TREE_TYPE (id) == error_mark_node)
3410 ; /* Allow the type to default to int to avoid cascading errors. */
3411 else
3413 type = TREE_TYPE (id);
3414 decl_attr = DECL_ATTRIBUTES (id);
3415 typedef_decl = id;
3418 /* Built-in types come as identifiers. */
3419 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3421 tree t = lookup_name (id);
3422 if (TREE_TYPE (t) == error_mark_node)
3424 else if (!t || TREE_CODE (t) != TYPE_DECL)
3425 error ("`%s' fails to be a typedef or built in type",
3426 IDENTIFIER_POINTER (id));
3427 else
3429 type = TREE_TYPE (t);
3430 typedef_decl = t;
3433 else if (TREE_CODE (id) != ERROR_MARK)
3434 type = id;
3436 found:
3440 typedef_type = type;
3441 if (type)
3442 size_varies = C_TYPE_VARIABLE_SIZE (type);
3444 /* No type at all: default to `int', and set DEFAULTED_INT
3445 because it was not a user-defined typedef. */
3447 if (type == 0)
3449 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3450 | (1 << (int) RID_SIGNED)
3451 | (1 << (int) RID_UNSIGNED)
3452 | (1 << (int) RID_COMPLEX))))
3453 /* Don't warn about typedef foo = bar. */
3454 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3455 && ! in_system_header)
3457 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3458 and this is a function, or if -Wimplicit; prefer the former
3459 warning since it is more explicit. */
3460 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3461 && funcdef_flag)
3462 warn_about_return_type = 1;
3463 else if (warn_implicit_int || flag_isoc99)
3464 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3465 name);
3468 defaulted_int = 1;
3469 type = integer_type_node;
3472 /* Now process the modifiers that were specified
3473 and check for invalid combinations. */
3475 /* Long double is a special combination. */
3477 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3478 && TYPE_MAIN_VARIANT (type) == double_type_node)
3480 specbits &= ~(1 << (int) RID_LONG);
3481 type = long_double_type_node;
3484 /* Check all other uses of type modifiers. */
3486 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3487 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3489 int ok = 0;
3491 if ((specbits & 1 << (int) RID_LONG)
3492 && (specbits & 1 << (int) RID_SHORT))
3493 error ("both long and short specified for `%s'", name);
3494 else if (((specbits & 1 << (int) RID_LONG)
3495 || (specbits & 1 << (int) RID_SHORT))
3496 && explicit_char)
3497 error ("long or short specified with char for `%s'", name);
3498 else if (((specbits & 1 << (int) RID_LONG)
3499 || (specbits & 1 << (int) RID_SHORT))
3500 && TREE_CODE (type) == REAL_TYPE)
3502 static int already = 0;
3504 error ("long or short specified with floating type for `%s'", name);
3505 if (! already && ! pedantic)
3507 error ("the only valid combination is `long double'");
3508 already = 1;
3511 else if ((specbits & 1 << (int) RID_SIGNED)
3512 && (specbits & 1 << (int) RID_UNSIGNED))
3513 error ("both signed and unsigned specified for `%s'", name);
3514 else if (TREE_CODE (type) != INTEGER_TYPE)
3515 error ("long, short, signed or unsigned invalid for `%s'", name);
3516 else
3518 ok = 1;
3519 if (!explicit_int && !defaulted_int && !explicit_char)
3521 error ("long, short, signed or unsigned used invalidly for `%s'",
3522 name);
3523 ok = 0;
3527 /* Discard the type modifiers if they are invalid. */
3528 if (! ok)
3530 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3531 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3532 longlong = 0;
3536 if ((specbits & (1 << (int) RID_COMPLEX))
3537 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3539 error ("complex invalid for `%s'", name);
3540 specbits &= ~(1 << (int) RID_COMPLEX);
3543 /* Decide whether an integer type is signed or not.
3544 Optionally treat bitfields as signed by default. */
3545 if (specbits & 1 << (int) RID_UNSIGNED
3546 || (bitfield && ! flag_signed_bitfields
3547 && (explicit_int || defaulted_int || explicit_char
3548 /* A typedef for plain `int' without `signed'
3549 can be controlled just like plain `int'. */
3550 || ! (typedef_decl != 0
3551 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3552 && TREE_CODE (type) != ENUMERAL_TYPE
3553 && !(specbits & 1 << (int) RID_SIGNED)))
3555 if (longlong)
3556 type = long_long_unsigned_type_node;
3557 else if (specbits & 1 << (int) RID_LONG)
3558 type = long_unsigned_type_node;
3559 else if (specbits & 1 << (int) RID_SHORT)
3560 type = short_unsigned_type_node;
3561 else if (type == char_type_node)
3562 type = unsigned_char_type_node;
3563 else if (typedef_decl)
3564 type = c_common_unsigned_type (type);
3565 else
3566 type = unsigned_type_node;
3568 else if ((specbits & 1 << (int) RID_SIGNED)
3569 && type == char_type_node)
3570 type = signed_char_type_node;
3571 else if (longlong)
3572 type = long_long_integer_type_node;
3573 else if (specbits & 1 << (int) RID_LONG)
3574 type = long_integer_type_node;
3575 else if (specbits & 1 << (int) RID_SHORT)
3576 type = short_integer_type_node;
3578 if (specbits & 1 << (int) RID_COMPLEX)
3580 if (pedantic && !flag_isoc99)
3581 pedwarn ("ISO C90 does not support complex types");
3582 /* If we just have "complex", it is equivalent to
3583 "complex double", but if any modifiers at all are specified it is
3584 the complex form of TYPE. E.g, "complex short" is
3585 "complex short int". */
3587 if (defaulted_int && ! longlong
3588 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3589 | (1 << (int) RID_SIGNED)
3590 | (1 << (int) RID_UNSIGNED))))
3592 if (pedantic)
3593 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3594 type = complex_double_type_node;
3596 else if (type == integer_type_node)
3598 if (pedantic)
3599 pedwarn ("ISO C does not support complex integer types");
3600 type = complex_integer_type_node;
3602 else if (type == float_type_node)
3603 type = complex_float_type_node;
3604 else if (type == double_type_node)
3605 type = complex_double_type_node;
3606 else if (type == long_double_type_node)
3607 type = complex_long_double_type_node;
3608 else
3610 if (pedantic)
3611 pedwarn ("ISO C does not support complex integer types");
3612 type = build_complex_type (type);
3616 /* Figure out the type qualifiers for the declaration. There are
3617 two ways a declaration can become qualified. One is something
3618 like `const int i' where the `const' is explicit. Another is
3619 something like `typedef const int CI; CI i' where the type of the
3620 declaration contains the `const'. */
3621 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3622 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3623 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3624 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3625 if (constp > 1 && ! flag_isoc99)
3626 pedwarn ("duplicate `const'");
3627 if (restrictp > 1 && ! flag_isoc99)
3628 pedwarn ("duplicate `restrict'");
3629 if (volatilep > 1 && ! flag_isoc99)
3630 pedwarn ("duplicate `volatile'");
3631 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3632 type = TYPE_MAIN_VARIANT (type);
3633 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3634 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3635 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3637 /* Warn if two storage classes are given. Default to `auto'. */
3640 int nclasses = 0;
3642 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3643 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3644 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3645 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3646 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3648 /* "static __thread" and "extern __thread" are allowed. */
3649 if ((specbits & (1 << (int) RID_THREAD
3650 | 1 << (int) RID_STATIC
3651 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3652 nclasses++;
3654 /* Warn about storage classes that are invalid for certain
3655 kinds of declarations (parameters, typenames, etc.). */
3657 if (nclasses > 1)
3658 error ("multiple storage classes in declaration of `%s'", name);
3659 else if (funcdef_flag
3660 && (specbits
3661 & ((1 << (int) RID_REGISTER)
3662 | (1 << (int) RID_AUTO)
3663 | (1 << (int) RID_TYPEDEF)
3664 | (1 << (int) RID_THREAD))))
3666 if (specbits & 1 << (int) RID_AUTO
3667 && (pedantic || current_binding_level == global_binding_level))
3668 pedwarn ("function definition declared `auto'");
3669 if (specbits & 1 << (int) RID_REGISTER)
3670 error ("function definition declared `register'");
3671 if (specbits & 1 << (int) RID_TYPEDEF)
3672 error ("function definition declared `typedef'");
3673 if (specbits & 1 << (int) RID_THREAD)
3674 error ("function definition declared `__thread'");
3675 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3676 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3678 else if (decl_context != NORMAL && nclasses > 0)
3680 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3682 else
3684 switch (decl_context)
3686 case FIELD:
3687 error ("storage class specified for structure field `%s'",
3688 name);
3689 break;
3690 case PARM:
3691 error ("storage class specified for parameter `%s'", name);
3692 break;
3693 default:
3694 error ("storage class specified for typename");
3695 break;
3697 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3698 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3699 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3702 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3704 /* `extern' with initialization is invalid if not at top level. */
3705 if (current_binding_level == global_binding_level)
3706 warning ("`%s' initialized and declared `extern'", name);
3707 else
3708 error ("`%s' has both `extern' and initializer", name);
3710 else if (current_binding_level == global_binding_level)
3712 if (specbits & 1 << (int) RID_AUTO)
3713 error ("top-level declaration of `%s' specifies `auto'", name);
3715 else
3717 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3718 error ("nested function `%s' declared `extern'", name);
3719 else if ((specbits & (1 << (int) RID_THREAD
3720 | 1 << (int) RID_EXTERN
3721 | 1 << (int) RID_STATIC))
3722 == (1 << (int) RID_THREAD))
3724 error ("function-scope `%s' implicitly auto and declared `__thread'",
3725 name);
3726 specbits &= ~(1 << (int) RID_THREAD);
3731 /* Now figure out the structure of the declarator proper.
3732 Descend through it, creating more complex types, until we reach
3733 the declared identifier (or NULL_TREE, in an absolute declarator). */
3735 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3737 if (type == error_mark_node)
3739 declarator = TREE_OPERAND (declarator, 0);
3740 continue;
3743 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3744 an INDIRECT_REF (for *...),
3745 a CALL_EXPR (for ...(...)),
3746 a TREE_LIST (for nested attributes),
3747 an identifier (for the name being declared)
3748 or a null pointer (for the place in an absolute declarator
3749 where the name was omitted).
3750 For the last two cases, we have just exited the loop.
3752 At this point, TYPE is the type of elements of an array,
3753 or for a function to return, or for a pointer to point to.
3754 After this sequence of ifs, TYPE is the type of the
3755 array or function or pointer, and DECLARATOR has had its
3756 outermost layer removed. */
3758 if (array_ptr_quals != NULL_TREE || array_parm_static)
3760 /* Only the innermost declarator (making a parameter be of
3761 array type which is converted to pointer type)
3762 may have static or type qualifiers. */
3763 error ("static or type qualifiers in non-parameter array declarator");
3764 array_ptr_quals = NULL_TREE;
3765 array_parm_static = 0;
3768 if (TREE_CODE (declarator) == TREE_LIST)
3770 /* We encode a declarator with embedded attributes using
3771 a TREE_LIST. */
3772 tree attrs = TREE_PURPOSE (declarator);
3773 tree inner_decl;
3774 int attr_flags = 0;
3775 declarator = TREE_VALUE (declarator);
3776 inner_decl = declarator;
3777 while (inner_decl != NULL_TREE
3778 && TREE_CODE (inner_decl) == TREE_LIST)
3779 inner_decl = TREE_VALUE (inner_decl);
3780 if (inner_decl == NULL_TREE
3781 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3782 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3783 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3784 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3785 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3786 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3787 returned_attrs = decl_attributes (&type,
3788 chainon (returned_attrs, attrs),
3789 attr_flags);
3791 else if (TREE_CODE (declarator) == ARRAY_REF)
3793 tree itype = NULL_TREE;
3794 tree size = TREE_OPERAND (declarator, 1);
3795 /* The index is a signed object `sizetype' bits wide. */
3796 tree index_type = c_common_signed_type (sizetype);
3798 array_ptr_quals = TREE_TYPE (declarator);
3799 array_parm_static = TREE_STATIC (declarator);
3801 declarator = TREE_OPERAND (declarator, 0);
3803 /* Check for some types that there cannot be arrays of. */
3805 if (VOID_TYPE_P (type))
3807 error ("declaration of `%s' as array of voids", name);
3808 type = error_mark_node;
3811 if (TREE_CODE (type) == FUNCTION_TYPE)
3813 error ("declaration of `%s' as array of functions", name);
3814 type = error_mark_node;
3817 if (pedantic && flexible_array_type_p (type))
3818 pedwarn ("invalid use of structure with flexible array member");
3820 if (size == error_mark_node)
3821 type = error_mark_node;
3823 if (type == error_mark_node)
3824 continue;
3826 /* If size was specified, set ITYPE to a range-type for that size.
3827 Otherwise, ITYPE remains null. finish_decl may figure it out
3828 from an initial value. */
3830 if (size)
3832 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3833 STRIP_TYPE_NOPS (size);
3835 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3837 error ("size of array `%s' has non-integer type", name);
3838 size = integer_one_node;
3841 if (pedantic && integer_zerop (size))
3842 pedwarn ("ISO C forbids zero-size array `%s'", name);
3844 if (TREE_CODE (size) == INTEGER_CST)
3846 constant_expression_warning (size);
3847 if (tree_int_cst_sgn (size) < 0)
3849 error ("size of array `%s' is negative", name);
3850 size = integer_one_node;
3853 else
3855 /* Make sure the array size remains visibly nonconstant
3856 even if it is (eg) a const variable with known value. */
3857 size_varies = 1;
3859 if (!flag_isoc99 && pedantic)
3861 if (TREE_CONSTANT (size))
3862 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3863 name);
3864 else
3865 pedwarn ("ISO C90 forbids variable-size array `%s'",
3866 name);
3870 if (integer_zerop (size))
3872 /* A zero-length array cannot be represented with an
3873 unsigned index type, which is what we'll get with
3874 build_index_type. Create an open-ended range instead. */
3875 itype = build_range_type (sizetype, size, NULL_TREE);
3877 else
3879 /* Compute the maximum valid index, that is, size - 1.
3880 Do the calculation in index_type, so that if it is
3881 a variable the computations will be done in the
3882 proper mode. */
3883 itype = fold (build (MINUS_EXPR, index_type,
3884 convert (index_type, size),
3885 convert (index_type, size_one_node)));
3887 /* If that overflowed, the array is too big.
3888 ??? While a size of INT_MAX+1 technically shouldn't
3889 cause an overflow (because we subtract 1), the overflow
3890 is recorded during the conversion to index_type, before
3891 the subtraction. Handling this case seems like an
3892 unnecessary complication. */
3893 if (TREE_OVERFLOW (itype))
3895 error ("size of array `%s' is too large", name);
3896 type = error_mark_node;
3897 continue;
3900 if (size_varies)
3902 /* We must be able to distinguish the
3903 SAVE_EXPR_CONTEXT for the variably-sized type
3904 so that we can set it correctly in
3905 set_save_expr_context. The convention is
3906 that all SAVE_EXPRs that need to be reset
3907 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3908 tree cfd = current_function_decl;
3909 if (decl_context == PARM)
3910 current_function_decl = NULL_TREE;
3911 itype = variable_size (itype);
3912 if (decl_context == PARM)
3913 current_function_decl = cfd;
3915 itype = build_index_type (itype);
3918 else if (decl_context == FIELD)
3920 if (pedantic && !flag_isoc99 && !in_system_header)
3921 pedwarn ("ISO C90 does not support flexible array members");
3923 /* ISO C99 Flexible array members are effectively identical
3924 to GCC's zero-length array extension. */
3925 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3928 /* If pedantic, complain about arrays of incomplete types. */
3930 if (pedantic && !COMPLETE_TYPE_P (type))
3931 pedwarn ("array type has incomplete element type");
3933 #if 0
3934 /* We shouldn't have a function type here at all!
3935 Functions aren't allowed as array elements. */
3936 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3937 && (constp || volatilep))
3938 pedwarn ("ISO C forbids const or volatile function types");
3939 #endif
3941 /* Build the array type itself, then merge any constancy or
3942 volatility into the target type. We must do it in this order
3943 to ensure that the TYPE_MAIN_VARIANT field of the array type
3944 is set correctly. */
3946 type = build_array_type (type, itype);
3947 if (type_quals)
3948 type = c_build_qualified_type (type, type_quals);
3950 if (size_varies)
3951 C_TYPE_VARIABLE_SIZE (type) = 1;
3953 /* The GCC extension for zero-length arrays differs from
3954 ISO flexible array members in that sizeof yields zero. */
3955 if (size && integer_zerop (size))
3957 layout_type (type);
3958 TYPE_SIZE (type) = bitsize_zero_node;
3959 TYPE_SIZE_UNIT (type) = size_zero_node;
3961 if (decl_context != PARM
3962 && (array_ptr_quals != NULL_TREE || array_parm_static))
3964 error ("static or type qualifiers in non-parameter array declarator");
3965 array_ptr_quals = NULL_TREE;
3966 array_parm_static = 0;
3969 else if (TREE_CODE (declarator) == CALL_EXPR)
3971 tree arg_types;
3973 /* Declaring a function type.
3974 Make sure we have a valid type for the function to return. */
3975 if (type == error_mark_node)
3976 continue;
3978 size_varies = 0;
3980 /* Warn about some types functions can't return. */
3982 if (TREE_CODE (type) == FUNCTION_TYPE)
3984 error ("`%s' declared as function returning a function", name);
3985 type = integer_type_node;
3987 if (TREE_CODE (type) == ARRAY_TYPE)
3989 error ("`%s' declared as function returning an array", name);
3990 type = integer_type_node;
3993 /* Construct the function type and go to the next
3994 inner layer of declarator. */
3996 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3997 funcdef_flag
3998 /* Say it's a definition
3999 only for the CALL_EXPR
4000 closest to the identifier. */
4001 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4002 /* Type qualifiers before the return type of the function
4003 qualify the return type, not the function type. */
4004 if (type_quals)
4006 /* Type qualifiers on a function return type are normally
4007 permitted by the standard but have no effect, so give a
4008 warning at -Wextra. Qualifiers on a void return type have
4009 meaning as a GNU extension, and are banned on function
4010 definitions in ISO C. FIXME: strictly we shouldn't
4011 pedwarn for qualified void return types except on function
4012 definitions, but not doing so could lead to the undesirable
4013 state of a "volatile void" function return type not being
4014 warned about, and a use of the function being compiled
4015 with GNU semantics, with no diagnostics under -pedantic. */
4016 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4017 pedwarn ("ISO C forbids qualified void function return type");
4018 else if (extra_warnings
4019 && !(VOID_TYPE_P (type)
4020 && type_quals == TYPE_QUAL_VOLATILE))
4021 warning ("type qualifiers ignored on function return type");
4023 type = c_build_qualified_type (type, type_quals);
4025 type_quals = TYPE_UNQUALIFIED;
4027 type = build_function_type (type, arg_types);
4028 declarator = TREE_OPERAND (declarator, 0);
4030 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4031 the formal parameter list of this FUNCTION_TYPE to point to
4032 the FUNCTION_TYPE node itself. */
4035 tree link;
4037 for (link = last_function_parm_tags;
4038 link;
4039 link = TREE_CHAIN (link))
4040 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4043 else if (TREE_CODE (declarator) == INDIRECT_REF)
4045 /* Merge any constancy or volatility into the target type
4046 for the pointer. */
4048 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4049 && type_quals)
4050 pedwarn ("ISO C forbids qualified function types");
4051 if (type_quals)
4052 type = c_build_qualified_type (type, type_quals);
4053 type_quals = TYPE_UNQUALIFIED;
4054 size_varies = 0;
4056 type = build_pointer_type (type);
4058 /* Process a list of type modifier keywords
4059 (such as const or volatile) that were given inside the `*'. */
4061 if (TREE_TYPE (declarator))
4063 tree typemodlist;
4064 int erred = 0;
4066 constp = 0;
4067 volatilep = 0;
4068 restrictp = 0;
4069 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4070 typemodlist = TREE_CHAIN (typemodlist))
4072 tree qualifier = TREE_VALUE (typemodlist);
4074 if (C_IS_RESERVED_WORD (qualifier))
4076 if (C_RID_CODE (qualifier) == RID_CONST)
4077 constp++;
4078 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4079 volatilep++;
4080 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4081 restrictp++;
4082 else
4083 erred++;
4085 else
4086 erred++;
4089 if (erred)
4090 error ("invalid type modifier within pointer declarator");
4091 if (constp > 1 && ! flag_isoc99)
4092 pedwarn ("duplicate `const'");
4093 if (volatilep > 1 && ! flag_isoc99)
4094 pedwarn ("duplicate `volatile'");
4095 if (restrictp > 1 && ! flag_isoc99)
4096 pedwarn ("duplicate `restrict'");
4098 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4099 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4100 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4103 declarator = TREE_OPERAND (declarator, 0);
4105 else
4106 abort ();
4110 /* Now TYPE has the actual type. */
4112 /* Did array size calculations overflow? */
4114 if (TREE_CODE (type) == ARRAY_TYPE
4115 && COMPLETE_TYPE_P (type)
4116 && TREE_OVERFLOW (TYPE_SIZE (type)))
4118 error ("size of array `%s' is too large", name);
4119 /* If we proceed with the array type as it is, we'll eventually
4120 crash in tree_low_cst(). */
4121 type = error_mark_node;
4124 /* If this is declaring a typedef name, return a TYPE_DECL. */
4126 if (specbits & (1 << (int) RID_TYPEDEF))
4128 tree decl;
4129 /* Note that the grammar rejects storage classes
4130 in typenames, fields or parameters */
4131 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4132 && type_quals)
4133 pedwarn ("ISO C forbids qualified function types");
4134 if (type_quals)
4135 type = c_build_qualified_type (type, type_quals);
4136 decl = build_decl (TYPE_DECL, declarator, type);
4137 if ((specbits & (1 << (int) RID_SIGNED))
4138 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4139 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4140 decl_attributes (&decl, returned_attrs, 0);
4141 return decl;
4144 /* Detect the case of an array type of unspecified size
4145 which came, as such, direct from a typedef name.
4146 We must copy the type, so that each identifier gets
4147 a distinct type, so that each identifier's size can be
4148 controlled separately by its own initializer. */
4150 if (type != 0 && typedef_type != 0
4151 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4152 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4154 type = build_array_type (TREE_TYPE (type), 0);
4155 if (size_varies)
4156 C_TYPE_VARIABLE_SIZE (type) = 1;
4159 /* If this is a type name (such as, in a cast or sizeof),
4160 compute the type and return it now. */
4162 if (decl_context == TYPENAME)
4164 /* Note that the grammar rejects storage classes
4165 in typenames, fields or parameters */
4166 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4167 && type_quals)
4168 pedwarn ("ISO C forbids const or volatile function types");
4169 if (type_quals)
4170 type = c_build_qualified_type (type, type_quals);
4171 decl_attributes (&type, returned_attrs, 0);
4172 return type;
4175 /* Aside from typedefs and type names (handle above),
4176 `void' at top level (not within pointer)
4177 is allowed only in public variables.
4178 We don't complain about parms either, but that is because
4179 a better error message can be made later. */
4181 if (VOID_TYPE_P (type) && decl_context != PARM
4182 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4183 && ((specbits & (1 << (int) RID_EXTERN))
4184 || (current_binding_level == global_binding_level
4185 && !(specbits
4186 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4188 error ("variable or field `%s' declared void", name);
4189 type = integer_type_node;
4192 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4193 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4196 tree decl;
4198 if (decl_context == PARM)
4200 tree type_as_written;
4201 tree promoted_type;
4203 /* A parameter declared as an array of T is really a pointer to T.
4204 One declared as a function is really a pointer to a function. */
4206 if (TREE_CODE (type) == ARRAY_TYPE)
4208 /* Transfer const-ness of array into that of type pointed to. */
4209 type = TREE_TYPE (type);
4210 if (type_quals)
4211 type = c_build_qualified_type (type, type_quals);
4212 type = build_pointer_type (type);
4213 type_quals = TYPE_UNQUALIFIED;
4214 if (array_ptr_quals)
4216 tree new_ptr_quals, new_ptr_attrs;
4217 int erred = 0;
4218 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4219 /* We don't yet implement attributes in this context. */
4220 if (new_ptr_attrs != NULL_TREE)
4221 warning ("attributes in parameter array declarator ignored");
4223 constp = 0;
4224 volatilep = 0;
4225 restrictp = 0;
4226 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4228 tree qualifier = TREE_VALUE (new_ptr_quals);
4230 if (C_IS_RESERVED_WORD (qualifier))
4232 if (C_RID_CODE (qualifier) == RID_CONST)
4233 constp++;
4234 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4235 volatilep++;
4236 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4237 restrictp++;
4238 else
4239 erred++;
4241 else
4242 erred++;
4245 if (erred)
4246 error ("invalid type modifier within array declarator");
4248 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4249 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4250 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4252 size_varies = 0;
4254 else if (TREE_CODE (type) == FUNCTION_TYPE)
4256 if (pedantic && type_quals)
4257 pedwarn ("ISO C forbids qualified function types");
4258 if (type_quals)
4259 type = c_build_qualified_type (type, type_quals);
4260 type = build_pointer_type (type);
4261 type_quals = TYPE_UNQUALIFIED;
4263 else if (type_quals)
4264 type = c_build_qualified_type (type, type_quals);
4266 type_as_written = type;
4268 decl = build_decl (PARM_DECL, declarator, type);
4269 if (size_varies)
4270 C_DECL_VARIABLE_SIZE (decl) = 1;
4272 /* Compute the type actually passed in the parmlist,
4273 for the case where there is no prototype.
4274 (For example, shorts and chars are passed as ints.)
4275 When there is a prototype, this is overridden later. */
4277 if (type == error_mark_node)
4278 promoted_type = type;
4279 else
4280 promoted_type = c_type_promotes_to (type);
4282 DECL_ARG_TYPE (decl) = promoted_type;
4283 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4285 else if (decl_context == FIELD)
4287 /* Structure field. It may not be a function. */
4289 if (TREE_CODE (type) == FUNCTION_TYPE)
4291 error ("field `%s' declared as a function", name);
4292 type = build_pointer_type (type);
4294 else if (TREE_CODE (type) != ERROR_MARK
4295 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4297 error ("field `%s' has incomplete type", name);
4298 type = error_mark_node;
4300 /* Move type qualifiers down to element of an array. */
4301 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4303 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4304 type_quals),
4305 TYPE_DOMAIN (type));
4306 #if 0
4307 /* Leave the field const or volatile as well. */
4308 type_quals = TYPE_UNQUALIFIED;
4309 #endif
4311 decl = build_decl (FIELD_DECL, declarator, type);
4312 DECL_NONADDRESSABLE_P (decl) = bitfield;
4314 if (size_varies)
4315 C_DECL_VARIABLE_SIZE (decl) = 1;
4317 else if (TREE_CODE (type) == FUNCTION_TYPE)
4319 /* Every function declaration is "external"
4320 except for those which are inside a function body
4321 in which `auto' is used.
4322 That is a case not specified by ANSI C,
4323 and we use it for forward declarations for nested functions. */
4324 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4325 || current_binding_level == global_binding_level);
4327 if (specbits & (1 << (int) RID_AUTO)
4328 && (pedantic || current_binding_level == global_binding_level))
4329 pedwarn ("invalid storage class for function `%s'", name);
4330 if (specbits & (1 << (int) RID_REGISTER))
4331 error ("invalid storage class for function `%s'", name);
4332 if (specbits & (1 << (int) RID_THREAD))
4333 error ("invalid storage class for function `%s'", name);
4334 /* Function declaration not at top level.
4335 Storage classes other than `extern' are not allowed
4336 and `extern' makes no difference. */
4337 if (current_binding_level != global_binding_level
4338 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4339 && pedantic)
4340 pedwarn ("invalid storage class for function `%s'", name);
4342 decl = build_decl (FUNCTION_DECL, declarator, type);
4343 decl = build_decl_attribute_variant (decl, decl_attr);
4345 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4346 ggc_alloc_cleared (sizeof (struct lang_decl));
4348 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4349 pedwarn ("ISO C forbids qualified function types");
4351 /* GNU C interprets a `volatile void' return type to indicate
4352 that the function does not return. */
4353 if ((type_quals & TYPE_QUAL_VOLATILE)
4354 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4355 warning ("`noreturn' function returns non-void value");
4357 if (extern_ref)
4358 DECL_EXTERNAL (decl) = 1;
4359 /* Record absence of global scope for `static' or `auto'. */
4360 TREE_PUBLIC (decl)
4361 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4363 if (defaulted_int)
4364 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4366 /* Record presence of `inline', if it is reasonable. */
4367 if (MAIN_NAME_P (declarator))
4369 if (inlinep)
4370 warning ("cannot inline function `main'");
4372 else if (inlinep)
4374 /* Assume that otherwise the function can be inlined. */
4375 DECL_DECLARED_INLINE_P (decl) = 1;
4377 /* Do not mark bare declarations as DECL_INLINE. Doing so
4378 in the presence of multiple declarations can result in
4379 the abstract origin pointing between the declarations,
4380 which will confuse dwarf2out. */
4381 if (initialized)
4383 DECL_INLINE (decl) = 1;
4384 if (specbits & (1 << (int) RID_EXTERN))
4385 current_extern_inline = 1;
4388 /* If -finline-functions, assume it can be inlined. This does
4389 two things: let the function be deferred until it is actually
4390 needed, and let dwarf2 know that the function is inlinable. */
4391 else if (flag_inline_trees == 2 && initialized)
4393 if (!DECL_INLINE (decl))
4394 DID_INLINE_FUNC (decl) = 1;
4395 DECL_INLINE (decl) = 1;
4396 DECL_DECLARED_INLINE_P (decl) = 0;
4399 else
4401 /* It's a variable. */
4402 /* An uninitialized decl with `extern' is a reference. */
4403 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4405 /* Move type qualifiers down to element of an array. */
4406 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4408 int saved_align = TYPE_ALIGN(type);
4409 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4410 type_quals),
4411 TYPE_DOMAIN (type));
4412 TYPE_ALIGN (type) = saved_align;
4413 #if 0 /* Leave the variable const or volatile as well. */
4414 type_quals = TYPE_UNQUALIFIED;
4415 #endif
4417 else if (type_quals)
4418 type = c_build_qualified_type (type, type_quals);
4420 /* It is invalid to create an `extern' declaration for a
4421 variable if there is a global declaration that is
4422 `static'. */
4423 if (extern_ref && current_binding_level != global_binding_level)
4425 tree global_decl;
4427 global_decl = identifier_global_value (declarator);
4428 if (global_decl
4429 && TREE_CODE (global_decl) == VAR_DECL
4430 && !TREE_PUBLIC (global_decl))
4431 error ("variable previously declared `static' redeclared "
4432 "`extern'");
4435 decl = build_decl (VAR_DECL, declarator, type);
4436 if (size_varies)
4437 C_DECL_VARIABLE_SIZE (decl) = 1;
4439 if (inlinep)
4440 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4442 DECL_EXTERNAL (decl) = extern_ref;
4444 /* At top level, the presence of a `static' or `register' storage
4445 class specifier, or the absence of all storage class specifiers
4446 makes this declaration a definition (perhaps tentative). Also,
4447 the absence of both `static' and `register' makes it public. */
4448 if (current_binding_level == global_binding_level)
4450 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4451 | (1 << (int) RID_REGISTER)));
4452 TREE_STATIC (decl) = !extern_ref;
4454 /* Not at top level, only `static' makes a static definition. */
4455 else
4457 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4458 TREE_PUBLIC (decl) = extern_ref;
4461 if (specbits & 1 << (int) RID_THREAD)
4463 if (targetm.have_tls)
4464 DECL_THREAD_LOCAL (decl) = 1;
4465 else
4466 /* A mere warning is sure to result in improper semantics
4467 at runtime. Don't bother to allow this to compile. */
4468 error ("thread-local storage not supported for this target");
4472 /* Record `register' declaration for warnings on &
4473 and in case doing stupid register allocation. */
4475 if (specbits & (1 << (int) RID_REGISTER))
4476 DECL_REGISTER (decl) = 1;
4478 /* Record constancy and volatility. */
4479 c_apply_type_quals_to_decl (type_quals, decl);
4481 /* If a type has volatile components, it should be stored in memory.
4482 Otherwise, the fact that those components are volatile
4483 will be ignored, and would even crash the compiler. */
4484 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4485 c_mark_addressable (decl);
4487 decl_attributes (&decl, returned_attrs, 0);
4489 return decl;
4493 /* Decode the parameter-list info for a function type or function definition.
4494 The argument is the value returned by `get_parm_info' (or made in parse.y
4495 if there is an identifier list instead of a parameter decl list).
4496 These two functions are separate because when a function returns
4497 or receives functions then each is called multiple times but the order
4498 of calls is different. The last call to `grokparms' is always the one
4499 that contains the formal parameter names of a function definition.
4501 Store in `last_function_parms' a chain of the decls of parms.
4502 Also store in `last_function_parm_tags' a chain of the struct, union,
4503 and enum tags declared among the parms.
4505 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4507 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4508 a mere declaration. A nonempty identifier-list gets an error message
4509 when FUNCDEF_FLAG is zero. */
4511 static tree
4512 grokparms (tree parms_info, int funcdef_flag)
4514 tree first_parm = TREE_CHAIN (parms_info);
4516 last_function_parms = TREE_PURPOSE (parms_info);
4517 last_function_parm_tags = TREE_VALUE (parms_info);
4519 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4520 && !in_system_header)
4521 warning ("function declaration isn't a prototype");
4523 if (first_parm != 0
4524 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4526 if (! funcdef_flag)
4527 pedwarn ("parameter names (without types) in function declaration");
4529 last_function_parms = first_parm;
4530 return 0;
4532 else
4534 tree parm;
4535 tree typelt;
4536 /* We no longer test FUNCDEF_FLAG.
4537 If the arg types are incomplete in a declaration,
4538 they must include undefined tags.
4539 These tags can never be defined in the scope of the declaration,
4540 so the types can never be completed,
4541 and no call can be compiled successfully. */
4542 #if 0
4543 /* In a fcn definition, arg types must be complete. */
4544 if (funcdef_flag)
4545 #endif
4546 for (parm = last_function_parms, typelt = first_parm;
4547 parm;
4548 parm = TREE_CHAIN (parm))
4549 /* Skip over any enumeration constants declared here. */
4550 if (TREE_CODE (parm) == PARM_DECL)
4552 /* Barf if the parameter itself has an incomplete type. */
4553 tree type = TREE_VALUE (typelt);
4554 if (type == error_mark_node)
4555 continue;
4556 if (!COMPLETE_TYPE_P (type))
4558 if (funcdef_flag && DECL_NAME (parm) != 0)
4559 error ("parameter `%s' has incomplete type",
4560 IDENTIFIER_POINTER (DECL_NAME (parm)));
4561 else
4562 warning ("parameter has incomplete type");
4563 if (funcdef_flag)
4565 TREE_VALUE (typelt) = error_mark_node;
4566 TREE_TYPE (parm) = error_mark_node;
4569 #if 0
4570 /* This has been replaced by parm_tags_warning, which
4571 uses a more accurate criterion for what to warn
4572 about. */
4573 else
4575 /* Now warn if is a pointer to an incomplete type. */
4576 while (TREE_CODE (type) == POINTER_TYPE
4577 || TREE_CODE (type) == REFERENCE_TYPE)
4578 type = TREE_TYPE (type);
4579 type = TYPE_MAIN_VARIANT (type);
4580 if (!COMPLETE_TYPE_P (type))
4582 if (DECL_NAME (parm) != 0)
4583 warning ("parameter `%s' points to incomplete type",
4584 IDENTIFIER_POINTER (DECL_NAME (parm)));
4585 else
4586 warning ("parameter points to incomplete type");
4589 #endif
4590 typelt = TREE_CHAIN (typelt);
4593 return first_parm;
4597 /* Return a tree_list node with info on a parameter list just parsed.
4598 The TREE_PURPOSE is a chain of decls of those parms.
4599 The TREE_VALUE is a list of structure, union and enum tags defined.
4600 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4601 This tree_list node is later fed to `grokparms'.
4603 VOID_AT_END nonzero means append `void' to the end of the type-list.
4604 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4606 tree
4607 get_parm_info (int void_at_end)
4609 tree decl, t;
4610 tree types = 0;
4611 int erred = 0;
4612 tree tags = gettags ();
4613 tree parms = getdecls ();
4614 tree new_parms = 0;
4615 tree order = current_binding_level->parm_order;
4617 /* Just `void' (and no ellipsis) is special. There are really no parms.
4618 But if the `void' is qualified (by `const' or `volatile') or has a
4619 storage class specifier (`register'), then the behavior is undefined;
4620 by not counting it as the special case of `void' we will cause an
4621 error later. Typedefs for `void' are OK (see DR#157). */
4622 if (void_at_end && parms != 0
4623 && TREE_CHAIN (parms) == 0
4624 && VOID_TYPE_P (TREE_TYPE (parms))
4625 && ! TREE_THIS_VOLATILE (parms)
4626 && ! TREE_READONLY (parms)
4627 && ! DECL_REGISTER (parms)
4628 && DECL_NAME (parms) == 0)
4630 parms = NULL_TREE;
4631 storedecls (NULL_TREE);
4632 return tree_cons (NULL_TREE, NULL_TREE,
4633 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4636 /* Extract enumerator values and other non-parms declared with the parms.
4637 Likewise any forward parm decls that didn't have real parm decls. */
4638 for (decl = parms; decl;)
4640 tree next = TREE_CHAIN (decl);
4642 if (TREE_CODE (decl) != PARM_DECL)
4644 TREE_CHAIN (decl) = new_parms;
4645 new_parms = decl;
4647 else if (TREE_ASM_WRITTEN (decl))
4649 error_with_decl (decl,
4650 "parameter `%s' has just a forward declaration");
4651 TREE_CHAIN (decl) = new_parms;
4652 new_parms = decl;
4654 decl = next;
4657 /* Put the parm decls back in the order they were in in the parm list. */
4658 for (t = order; t; t = TREE_CHAIN (t))
4660 if (TREE_CHAIN (t))
4661 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4662 else
4663 TREE_CHAIN (TREE_VALUE (t)) = 0;
4666 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4667 new_parms);
4669 /* Store the parmlist in the binding level since the old one
4670 is no longer a valid list. (We have changed the chain pointers.) */
4671 storedecls (new_parms);
4673 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4674 /* There may also be declarations for enumerators if an enumeration
4675 type is declared among the parms. Ignore them here. */
4676 if (TREE_CODE (decl) == PARM_DECL)
4678 /* Since there is a prototype,
4679 args are passed in their declared types. */
4680 tree type = TREE_TYPE (decl);
4681 DECL_ARG_TYPE (decl) = type;
4682 if (PROMOTE_PROTOTYPES
4683 && INTEGRAL_TYPE_P (type)
4684 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4685 DECL_ARG_TYPE (decl) = integer_type_node;
4687 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4688 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4689 && DECL_NAME (decl) == 0)
4691 error ("`void' in parameter list must be the entire list");
4692 erred = 1;
4696 if (void_at_end)
4697 return tree_cons (new_parms, tags,
4698 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4700 return tree_cons (new_parms, tags, nreverse (types));
4703 /* At end of parameter list, warn about any struct, union or enum tags
4704 defined within. Do so because these types cannot ever become complete. */
4706 void
4707 parmlist_tags_warning (void)
4709 tree elt;
4710 static int already;
4712 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4714 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4715 /* An anonymous union parm type is meaningful as a GNU extension.
4716 So don't warn for that. */
4717 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4718 continue;
4719 if (TREE_PURPOSE (elt) != 0)
4721 if (code == RECORD_TYPE)
4722 warning ("`struct %s' declared inside parameter list",
4723 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4724 else if (code == UNION_TYPE)
4725 warning ("`union %s' declared inside parameter list",
4726 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4727 else
4728 warning ("`enum %s' declared inside parameter list",
4729 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4731 else
4733 /* For translation these need to be separate warnings */
4734 if (code == RECORD_TYPE)
4735 warning ("anonymous struct declared inside parameter list");
4736 else if (code == UNION_TYPE)
4737 warning ("anonymous union declared inside parameter list");
4738 else
4739 warning ("anonymous enum declared inside parameter list");
4741 if (! already)
4743 warning ("its scope is only this definition or declaration, which is probably not what you want");
4744 already = 1;
4749 /* Get the struct, enum or union (CODE says which) with tag NAME.
4750 Define the tag as a forward-reference if it is not defined. */
4752 tree
4753 xref_tag (enum tree_code code, tree name)
4755 /* If a cross reference is requested, look up the type
4756 already defined for this tag and return it. */
4758 tree ref = lookup_tag (code, name, 0);
4759 /* If this is the right type of tag, return what we found.
4760 (This reference will be shadowed by shadow_tag later if appropriate.)
4761 If this is the wrong type of tag, do not return it. If it was the
4762 wrong type in the same binding level, we will have had an error
4763 message already; if in a different binding level and declaring
4764 a name, pending_xref_error will give an error message; but if in a
4765 different binding level and not declaring a name, this tag should
4766 shadow the previous declaration of a different type of tag, and
4767 this would not work properly if we return the reference found.
4768 (For example, with "struct foo" in an outer scope, "union foo;"
4769 must shadow that tag with a new one of union type.) */
4770 if (ref && TREE_CODE (ref) == code)
4771 return ref;
4773 /* If no such tag is yet defined, create a forward-reference node
4774 and record it as the "definition".
4775 When a real declaration of this type is found,
4776 the forward-reference will be altered into a real type. */
4778 ref = make_node (code);
4779 if (code == ENUMERAL_TYPE)
4781 /* Give the type a default layout like unsigned int
4782 to avoid crashing if it does not get defined. */
4783 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4784 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4785 TYPE_USER_ALIGN (ref) = 0;
4786 TREE_UNSIGNED (ref) = 1;
4787 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4788 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4789 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4792 pushtag (name, ref);
4794 return ref;
4797 /* Make sure that the tag NAME is defined *in the current binding level*
4798 at least as a forward reference.
4799 CODE says which kind of tag NAME ought to be. */
4801 tree
4802 start_struct (enum tree_code code, tree name)
4804 /* If there is already a tag defined at this binding level
4805 (as a forward reference), just return it. */
4807 tree ref = 0;
4809 if (name != 0)
4810 ref = lookup_tag (code, name, 1);
4811 if (ref && TREE_CODE (ref) == code)
4813 if (TYPE_FIELDS (ref))
4815 if (code == UNION_TYPE)
4816 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4817 else
4818 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4821 else
4823 /* Otherwise create a forward-reference just so the tag is in scope. */
4825 ref = make_node (code);
4826 pushtag (name, ref);
4829 C_TYPE_BEING_DEFINED (ref) = 1;
4830 TYPE_PACKED (ref) = flag_pack_struct;
4831 return ref;
4834 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4835 of a structure component, returning a FIELD_DECL node.
4836 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4838 This is done during the parsing of the struct declaration.
4839 The FIELD_DECL nodes are chained together and the lot of them
4840 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4842 tree
4843 grokfield (tree declarator, tree declspecs, tree width)
4845 tree value;
4847 if (declarator == NULL_TREE && width == NULL_TREE)
4849 /* This is an unnamed decl.
4851 If we have something of the form "union { list } ;" then this
4852 is the anonymous union extension. Similarly for struct.
4854 If this is something of the form "struct foo;", then
4855 If MS extensions are enabled, this is handled as an
4856 anonymous struct.
4857 Otherwise this is a forward declaration of a structure tag.
4859 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4860 If MS extensions are enabled and foo names a structure, then
4861 again this is an anonymous struct.
4862 Otherwise this is an error.
4864 Oh what a horrid tangled web we weave. I wonder if MS consciously
4865 took this from Plan 9 or if it was an accident of implementation
4866 that took root before someone noticed the bug... */
4868 tree type = TREE_VALUE (declspecs);
4870 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4871 type = TREE_TYPE (type);
4872 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4874 if (flag_ms_extensions)
4875 ; /* ok */
4876 else if (flag_iso)
4877 goto warn_unnamed_field;
4878 else if (TYPE_NAME (type) == NULL)
4879 ; /* ok */
4880 else
4881 goto warn_unnamed_field;
4883 else
4885 warn_unnamed_field:
4886 warning ("declaration does not declare anything");
4887 return NULL_TREE;
4891 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4893 finish_decl (value, NULL_TREE, NULL_TREE);
4894 DECL_INITIAL (value) = width;
4896 if (c_dialect_objc ())
4897 objc_check_decl (value);
4898 return value;
4901 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4902 the list such that this does not present a problem later. */
4904 static void
4905 detect_field_duplicates (tree fieldlist)
4907 tree x, y;
4908 int timeout = 10;
4910 /* First, see if there are more than "a few" fields.
4911 This is trivially true if there are zero or one fields. */
4912 if (!fieldlist)
4913 return;
4914 x = TREE_CHAIN (fieldlist);
4915 if (!x)
4916 return;
4917 do {
4918 timeout--;
4919 x = TREE_CHAIN (x);
4920 } while (timeout > 0 && x);
4922 /* If there were "few" fields, avoid the overhead of allocating
4923 a hash table. Instead just do the nested traversal thing. */
4924 if (timeout > 0)
4926 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4927 if (DECL_NAME (x))
4929 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4930 if (DECL_NAME (y) == DECL_NAME (x))
4932 error_with_decl (x, "duplicate member `%s'");
4933 DECL_NAME (x) = NULL_TREE;
4937 else
4939 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4940 void **slot;
4942 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4943 if ((y = DECL_NAME (x)) != 0)
4945 slot = htab_find_slot (htab, y, INSERT);
4946 if (*slot)
4948 error_with_decl (x, "duplicate member `%s'");
4949 DECL_NAME (x) = NULL_TREE;
4951 *slot = y;
4954 htab_delete (htab);
4958 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4959 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4960 ATTRIBUTES are attributes to be applied to the structure. */
4962 tree
4963 finish_struct (tree t, tree fieldlist, tree attributes)
4965 tree x;
4966 int toplevel = global_binding_level == current_binding_level;
4967 int saw_named_field;
4969 /* If this type was previously laid out as a forward reference,
4970 make sure we lay it out again. */
4972 TYPE_SIZE (t) = 0;
4974 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4976 /* Nameless union parm types are useful as GCC extension. */
4977 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4978 /* Otherwise, warn about any struct or union def. in parmlist. */
4979 if (in_parm_level_p ())
4981 if (pedantic)
4982 pedwarn ("%s defined inside parms",
4983 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4984 else
4985 warning ("%s defined inside parms",
4986 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4989 if (pedantic)
4991 for (x = fieldlist; x; x = TREE_CHAIN (x))
4992 if (DECL_NAME (x) != 0)
4993 break;
4995 if (x == 0)
4996 pedwarn ("%s has no %s",
4997 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4998 fieldlist ? _("named members") : _("members"));
5001 /* Install struct as DECL_CONTEXT of each field decl.
5002 Also process specified field sizes,m which is found in the DECL_INITIAL.
5003 Store 0 there, except for ": 0" fields (so we can find them
5004 and delete them, below). */
5006 saw_named_field = 0;
5007 for (x = fieldlist; x; x = TREE_CHAIN (x))
5009 DECL_CONTEXT (x) = t;
5010 DECL_PACKED (x) |= TYPE_PACKED (t);
5012 /* If any field is const, the structure type is pseudo-const. */
5013 if (TREE_READONLY (x))
5014 C_TYPE_FIELDS_READONLY (t) = 1;
5015 else
5017 /* A field that is pseudo-const makes the structure likewise. */
5018 tree t1 = TREE_TYPE (x);
5019 while (TREE_CODE (t1) == ARRAY_TYPE)
5020 t1 = TREE_TYPE (t1);
5021 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5022 && C_TYPE_FIELDS_READONLY (t1))
5023 C_TYPE_FIELDS_READONLY (t) = 1;
5026 /* Any field that is volatile means variables of this type must be
5027 treated in some ways as volatile. */
5028 if (TREE_THIS_VOLATILE (x))
5029 C_TYPE_FIELDS_VOLATILE (t) = 1;
5031 /* Any field of nominal variable size implies structure is too. */
5032 if (C_DECL_VARIABLE_SIZE (x))
5033 C_TYPE_VARIABLE_SIZE (t) = 1;
5035 /* Detect invalid nested redefinition. */
5036 if (TREE_TYPE (x) == t)
5037 error ("nested redefinition of `%s'",
5038 IDENTIFIER_POINTER (TYPE_NAME (t)));
5040 /* Detect invalid bit-field size. */
5041 if (DECL_INITIAL (x))
5042 STRIP_NOPS (DECL_INITIAL (x));
5043 if (DECL_INITIAL (x))
5045 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5046 constant_expression_warning (DECL_INITIAL (x));
5047 else
5049 error_with_decl (x,
5050 "bit-field `%s' width not an integer constant");
5051 DECL_INITIAL (x) = NULL;
5055 /* Detect invalid bit-field type. */
5056 if (DECL_INITIAL (x)
5057 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5058 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5059 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5061 error_with_decl (x, "bit-field `%s' has invalid type");
5062 DECL_INITIAL (x) = NULL;
5065 if (DECL_INITIAL (x) && pedantic
5066 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5067 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5068 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5069 /* Accept an enum that's equivalent to int or unsigned int. */
5070 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5071 && (TYPE_PRECISION (TREE_TYPE (x))
5072 == TYPE_PRECISION (integer_type_node))))
5073 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5075 /* Detect and ignore out of range field width and process valid
5076 field widths. */
5077 if (DECL_INITIAL (x))
5079 int max_width
5080 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5081 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5083 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5084 error_with_decl (x, "negative width in bit-field `%s'");
5085 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5086 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5087 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5088 error_with_decl (x, "zero width for bit-field `%s'");
5089 else
5091 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5092 unsigned HOST_WIDE_INT width
5093 = tree_low_cst (DECL_INITIAL (x), 1);
5095 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5096 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5097 TREE_UNSIGNED (TREE_TYPE (x)))
5098 || (width
5099 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5100 TREE_UNSIGNED (TREE_TYPE (x))))))
5101 warning_with_decl (x,
5102 "`%s' is narrower than values of its type");
5104 DECL_SIZE (x) = bitsize_int (width);
5105 DECL_BIT_FIELD (x) = 1;
5106 SET_DECL_C_BIT_FIELD (x);
5110 DECL_INITIAL (x) = 0;
5112 /* Detect flexible array member in an invalid context. */
5113 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5114 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5115 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5116 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5118 if (TREE_CODE (t) == UNION_TYPE)
5119 error_with_decl (x, "flexible array member in union");
5120 else if (TREE_CHAIN (x) != NULL_TREE)
5121 error_with_decl (x, "flexible array member not at end of struct");
5122 else if (! saw_named_field)
5123 error_with_decl (x, "flexible array member in otherwise empty struct");
5126 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5127 && flexible_array_type_p (TREE_TYPE (x)))
5128 pedwarn_with_decl (x, "invalid use of structure with flexible array member");
5130 if (DECL_NAME (x))
5131 saw_named_field = 1;
5134 detect_field_duplicates (fieldlist);
5136 /* Now we have the nearly final fieldlist. Record it,
5137 then lay out the structure or union (including the fields). */
5139 TYPE_FIELDS (t) = fieldlist;
5141 layout_type (t);
5143 /* Delete all zero-width bit-fields from the fieldlist */
5145 tree *fieldlistp = &fieldlist;
5146 while (*fieldlistp)
5147 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5148 *fieldlistp = TREE_CHAIN (*fieldlistp);
5149 else
5150 fieldlistp = &TREE_CHAIN (*fieldlistp);
5153 /* Now we have the truly final field list.
5154 Store it in this type and in the variants. */
5156 TYPE_FIELDS (t) = fieldlist;
5158 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5160 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5161 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5162 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5163 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5166 /* If this was supposed to be a transparent union, but we can't
5167 make it one, warn and turn off the flag. */
5168 if (TREE_CODE (t) == UNION_TYPE
5169 && TYPE_TRANSPARENT_UNION (t)
5170 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5172 TYPE_TRANSPARENT_UNION (t) = 0;
5173 warning ("union cannot be made transparent");
5176 /* If this structure or union completes the type of any previous
5177 variable declaration, lay it out and output its rtl. */
5179 if (current_binding_level->incomplete_list != NULL_TREE)
5181 tree prev = NULL_TREE;
5183 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5185 tree decl = TREE_VALUE (x);
5187 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5188 && TREE_CODE (decl) != TYPE_DECL)
5190 layout_decl (decl, 0);
5191 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5192 if (c_dialect_objc ())
5193 objc_check_decl (decl);
5194 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5195 if (! toplevel)
5196 expand_decl (decl);
5197 /* Unlink X from the incomplete list. */
5198 if (prev)
5199 TREE_CHAIN (prev) = TREE_CHAIN (x);
5200 else
5201 current_binding_level->incomplete_list = TREE_CHAIN (x);
5203 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5204 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5206 tree element = TREE_TYPE (decl);
5207 while (TREE_CODE (element) == ARRAY_TYPE)
5208 element = TREE_TYPE (element);
5209 if (element == t)
5211 layout_array_type (TREE_TYPE (decl));
5212 if (TREE_CODE (decl) != TYPE_DECL)
5214 layout_decl (decl, 0);
5215 if (c_dialect_objc ())
5216 objc_check_decl (decl);
5217 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5218 if (! toplevel)
5219 expand_decl (decl);
5221 /* Unlink X from the incomplete list. */
5222 if (prev)
5223 TREE_CHAIN (prev) = TREE_CHAIN (x);
5224 else
5225 current_binding_level->incomplete_list = TREE_CHAIN (x);
5231 /* Finish debugging output for this type. */
5232 rest_of_type_compilation (t, toplevel);
5234 return t;
5237 /* Lay out the type T, and its element type, and so on. */
5239 static void
5240 layout_array_type (tree t)
5242 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5243 layout_array_type (TREE_TYPE (t));
5244 layout_type (t);
5247 /* Begin compiling the definition of an enumeration type.
5248 NAME is its name (or null if anonymous).
5249 Returns the type object, as yet incomplete.
5250 Also records info about it so that build_enumerator
5251 may be used to declare the individual values as they are read. */
5253 tree
5254 start_enum (tree name)
5256 tree enumtype = 0;
5258 /* If this is the real definition for a previous forward reference,
5259 fill in the contents in the same object that used to be the
5260 forward reference. */
5262 if (name != 0)
5263 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5265 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5267 enumtype = make_node (ENUMERAL_TYPE);
5268 pushtag (name, enumtype);
5271 C_TYPE_BEING_DEFINED (enumtype) = 1;
5273 if (TYPE_VALUES (enumtype) != 0)
5275 /* This enum is a named one that has been declared already. */
5276 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5278 /* Completely replace its old definition.
5279 The old enumerators remain defined, however. */
5280 TYPE_VALUES (enumtype) = 0;
5283 enum_next_value = integer_zero_node;
5284 enum_overflow = 0;
5286 if (flag_short_enums)
5287 TYPE_PACKED (enumtype) = 1;
5289 return enumtype;
5292 /* After processing and defining all the values of an enumeration type,
5293 install their decls in the enumeration type and finish it off.
5294 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5295 and ATTRIBUTES are the specified attributes.
5296 Returns ENUMTYPE. */
5298 tree
5299 finish_enum (tree enumtype, tree values, tree attributes)
5301 tree pair, tem;
5302 tree minnode = 0, maxnode = 0, enum_value_type;
5303 int precision, unsign;
5304 int toplevel = (global_binding_level == current_binding_level);
5306 if (in_parm_level_p ())
5307 warning ("enum defined inside parms");
5309 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5311 /* Calculate the maximum value of any enumerator in this type. */
5313 if (values == error_mark_node)
5314 minnode = maxnode = integer_zero_node;
5315 else
5317 minnode = maxnode = TREE_VALUE (values);
5318 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5320 tree value = TREE_VALUE (pair);
5321 if (tree_int_cst_lt (maxnode, value))
5322 maxnode = value;
5323 if (tree_int_cst_lt (value, minnode))
5324 minnode = value;
5328 /* Construct the final type of this enumeration. It is the same
5329 as one of the integral types - the narrowest one that fits, except
5330 that normally we only go as narrow as int - and signed iff any of
5331 the values are negative. */
5332 unsign = (tree_int_cst_sgn (minnode) >= 0);
5333 precision = MAX (min_precision (minnode, unsign),
5334 min_precision (maxnode, unsign));
5335 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5337 tree narrowest = c_common_type_for_size (precision, unsign);
5338 if (narrowest == 0)
5340 warning ("enumeration values exceed range of largest integer");
5341 narrowest = long_long_integer_type_node;
5344 precision = TYPE_PRECISION (narrowest);
5346 else
5347 precision = TYPE_PRECISION (integer_type_node);
5349 if (precision == TYPE_PRECISION (integer_type_node))
5350 enum_value_type = c_common_type_for_size (precision, 0);
5351 else
5352 enum_value_type = enumtype;
5354 TYPE_MIN_VALUE (enumtype) = minnode;
5355 TYPE_MAX_VALUE (enumtype) = maxnode;
5356 TYPE_PRECISION (enumtype) = precision;
5357 TREE_UNSIGNED (enumtype) = unsign;
5358 TYPE_SIZE (enumtype) = 0;
5359 layout_type (enumtype);
5361 if (values != error_mark_node)
5363 /* Change the type of the enumerators to be the enum type. We
5364 need to do this irrespective of the size of the enum, for
5365 proper type checking. Replace the DECL_INITIALs of the
5366 enumerators, and the value slots of the list, with copies
5367 that have the enum type; they cannot be modified in place
5368 because they may be shared (e.g. integer_zero_node) Finally,
5369 change the purpose slots to point to the names of the decls. */
5370 for (pair = values; pair; pair = TREE_CHAIN (pair))
5372 tree enu = TREE_PURPOSE (pair);
5374 TREE_TYPE (enu) = enumtype;
5376 /* The ISO C Standard mandates enumerators to have type int,
5377 even though the underlying type of an enum type is
5378 unspecified. Here we convert any enumerators that fit in
5379 an int to type int, to avoid promotions to unsigned types
5380 when comparing integers with enumerators that fit in the
5381 int range. When -pedantic is given, build_enumerator()
5382 would have already taken care of those that don't fit. */
5383 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5384 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5385 else
5386 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5388 TREE_PURPOSE (pair) = DECL_NAME (enu);
5389 TREE_VALUE (pair) = DECL_INITIAL (enu);
5392 TYPE_VALUES (enumtype) = values;
5395 /* Fix up all variant types of this enum type. */
5396 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5398 if (tem == enumtype)
5399 continue;
5400 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5401 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5402 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5403 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5404 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5405 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5406 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5407 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5408 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5409 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5412 /* Finish debugging output for this type. */
5413 rest_of_type_compilation (enumtype, toplevel);
5415 return enumtype;
5418 /* Build and install a CONST_DECL for one value of the
5419 current enumeration type (one that was begun with start_enum).
5420 Return a tree-list containing the CONST_DECL and its value.
5421 Assignment of sequential values by default is handled here. */
5423 tree
5424 build_enumerator (tree name, tree value)
5426 tree decl, type;
5428 /* Validate and default VALUE. */
5430 /* Remove no-op casts from the value. */
5431 if (value)
5432 STRIP_TYPE_NOPS (value);
5434 if (value != 0)
5436 if (TREE_CODE (value) == INTEGER_CST)
5438 value = default_conversion (value);
5439 constant_expression_warning (value);
5441 else
5443 error ("enumerator value for `%s' not integer constant",
5444 IDENTIFIER_POINTER (name));
5445 value = 0;
5449 /* Default based on previous value. */
5450 /* It should no longer be possible to have NON_LVALUE_EXPR
5451 in the default. */
5452 if (value == 0)
5454 value = enum_next_value;
5455 if (enum_overflow)
5456 error ("overflow in enumeration values");
5459 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5461 pedwarn ("ISO C restricts enumerator values to range of `int'");
5462 value = convert (integer_type_node, value);
5465 /* Set basis for default for next value. */
5466 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5467 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5469 /* Now create a declaration for the enum value name. */
5471 type = TREE_TYPE (value);
5472 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5473 TYPE_PRECISION (integer_type_node)),
5474 (TYPE_PRECISION (type)
5475 >= TYPE_PRECISION (integer_type_node)
5476 && TREE_UNSIGNED (type)));
5478 decl = build_decl (CONST_DECL, name, type);
5479 DECL_INITIAL (decl) = convert (type, value);
5480 pushdecl (decl);
5482 return tree_cons (decl, value, NULL_TREE);
5486 /* Create the FUNCTION_DECL for a function definition.
5487 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5488 the declaration; they describe the function's name and the type it returns,
5489 but twisted together in a fashion that parallels the syntax of C.
5491 This function creates a binding context for the function body
5492 as well as setting up the FUNCTION_DECL in current_function_decl.
5494 Returns 1 on success. If the DECLARATOR is not suitable for a function
5495 (it defines a datum instead), we return 0, which tells
5496 yyparse to report a parse error. */
5499 start_function (tree declspecs, tree declarator, tree attributes)
5501 tree decl1, old_decl;
5502 tree restype;
5503 int old_immediate_size_expand = immediate_size_expand;
5505 current_function_returns_value = 0; /* Assume, until we see it does. */
5506 current_function_returns_null = 0;
5507 current_function_returns_abnormally = 0;
5508 warn_about_return_type = 0;
5509 current_extern_inline = 0;
5510 named_labels = 0;
5511 shadowed_labels = 0;
5513 /* Don't expand any sizes in the return type of the function. */
5514 immediate_size_expand = 0;
5516 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5518 /* If the declarator is not suitable for a function definition,
5519 cause a syntax error. */
5520 if (decl1 == 0)
5522 immediate_size_expand = old_immediate_size_expand;
5523 return 0;
5526 decl_attributes (&decl1, attributes, 0);
5528 /* If #pragma weak was used, mark the decl weak now. */
5529 if (current_binding_level == global_binding_level)
5530 maybe_apply_pragma_weak (decl1);
5532 if (DECL_DECLARED_INLINE_P (decl1)
5533 && DECL_UNINLINABLE (decl1)
5534 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5535 warning_with_decl (decl1,
5536 "inline function `%s' given attribute noinline");
5538 announce_function (decl1);
5540 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5542 error ("return type is an incomplete type");
5543 /* Make it return void instead. */
5544 TREE_TYPE (decl1)
5545 = build_function_type (void_type_node,
5546 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5549 if (warn_about_return_type)
5550 pedwarn_c99 ("return type defaults to `int'");
5552 /* Save the parm names or decls from this function's declarator
5553 where store_parm_decls will find them. */
5554 current_function_parms = last_function_parms;
5555 current_function_parm_tags = last_function_parm_tags;
5557 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5558 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5559 DECL_INITIAL (decl1) = error_mark_node;
5561 /* If this definition isn't a prototype and we had a prototype declaration
5562 before, copy the arg type info from that prototype.
5563 But not if what we had before was a builtin function. */
5564 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5565 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5566 && !DECL_BUILT_IN (old_decl)
5567 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5568 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5569 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5571 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5572 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5575 /* Optionally warn of old-fashioned def with no previous prototype. */
5576 if (warn_strict_prototypes
5577 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5578 && C_DECL_ISNT_PROTOTYPE (old_decl))
5579 warning ("function declaration isn't a prototype");
5580 /* Optionally warn of any global def with no previous prototype. */
5581 else if (warn_missing_prototypes
5582 && TREE_PUBLIC (decl1)
5583 && ! MAIN_NAME_P (DECL_NAME (decl1))
5584 && C_DECL_ISNT_PROTOTYPE (old_decl))
5585 warning_with_decl (decl1, "no previous prototype for `%s'");
5586 /* Optionally warn of any def with no previous prototype
5587 if the function has already been used. */
5588 else if (warn_missing_prototypes
5589 && old_decl != 0 && TREE_USED (old_decl)
5590 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5591 warning_with_decl (decl1,
5592 "`%s' was used with no prototype before its definition");
5593 /* Optionally warn of any global def with no previous declaration. */
5594 else if (warn_missing_declarations
5595 && TREE_PUBLIC (decl1)
5596 && old_decl == 0
5597 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5598 warning_with_decl (decl1, "no previous declaration for `%s'");
5599 /* Optionally warn of any def with no previous declaration
5600 if the function has already been used. */
5601 else if (warn_missing_declarations
5602 && old_decl != 0 && TREE_USED (old_decl)
5603 && C_DECL_IMPLICIT (old_decl))
5604 warning_with_decl (decl1,
5605 "`%s' was used with no declaration before its definition");
5607 /* This is a definition, not a reference.
5608 So normally clear DECL_EXTERNAL.
5609 However, `extern inline' acts like a declaration
5610 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5611 DECL_EXTERNAL (decl1) = current_extern_inline;
5613 /* This function exists in static storage.
5614 (This does not mean `static' in the C sense!) */
5615 TREE_STATIC (decl1) = 1;
5617 /* A nested function is not global. */
5618 if (current_function_decl != 0)
5619 TREE_PUBLIC (decl1) = 0;
5621 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5622 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5624 tree args;
5625 int argct = 0;
5627 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5628 != integer_type_node)
5629 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5631 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5632 args = TREE_CHAIN (args))
5634 tree type = args ? TREE_VALUE (args) : 0;
5636 if (type == void_type_node)
5637 break;
5639 ++argct;
5640 switch (argct)
5642 case 1:
5643 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5644 pedwarn_with_decl (decl1,
5645 "first argument of `%s' should be `int'");
5646 break;
5648 case 2:
5649 if (TREE_CODE (type) != POINTER_TYPE
5650 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5651 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5652 != char_type_node))
5653 pedwarn_with_decl (decl1,
5654 "second argument of `%s' should be `char **'");
5655 break;
5657 case 3:
5658 if (TREE_CODE (type) != POINTER_TYPE
5659 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5660 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5661 != char_type_node))
5662 pedwarn_with_decl (decl1,
5663 "third argument of `%s' should probably be `char **'");
5664 break;
5668 /* It is intentional that this message does not mention the third
5669 argument because it's only mentioned in an appendix of the
5670 standard. */
5671 if (argct > 0 && (argct < 2 || argct > 3))
5672 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5674 if (! TREE_PUBLIC (decl1))
5675 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5678 /* Record the decl so that the function name is defined.
5679 If we already have a decl for this name, and it is a FUNCTION_DECL,
5680 use the old decl. */
5682 current_function_decl = pushdecl (decl1);
5684 pushlevel (0);
5685 declare_parm_level (1);
5687 make_decl_rtl (current_function_decl, NULL);
5689 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5690 /* Promote the value to int before returning it. */
5691 if (c_promoting_integer_type_p (restype))
5693 /* It retains unsignedness if not really getting wider. */
5694 if (TREE_UNSIGNED (restype)
5695 && (TYPE_PRECISION (restype)
5696 == TYPE_PRECISION (integer_type_node)))
5697 restype = unsigned_type_node;
5698 else
5699 restype = integer_type_node;
5701 DECL_RESULT (current_function_decl)
5702 = build_decl (RESULT_DECL, NULL_TREE, restype);
5704 /* If this fcn was already referenced via a block-scope `extern' decl
5705 (or an implicit decl), propagate certain information about the usage. */
5706 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5707 TREE_ADDRESSABLE (current_function_decl) = 1;
5709 immediate_size_expand = old_immediate_size_expand;
5711 start_fname_decls ();
5713 return 1;
5716 /* Store the parameter declarations into the current function declaration.
5717 This is called after parsing the parameter declarations, before
5718 digesting the body of the function.
5720 For an old-style definition, modify the function's type
5721 to specify at least the number of arguments. */
5723 void
5724 store_parm_decls (void)
5726 tree fndecl = current_function_decl;
5727 tree parm;
5729 /* This is either a chain of PARM_DECLs (if a prototype was used)
5730 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5731 tree specparms = current_function_parms;
5733 /* This is a list of types declared among parms in a prototype. */
5734 tree parmtags = current_function_parm_tags;
5736 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5737 tree parmdecls = getdecls ();
5739 /* This is a chain of any other decls that came in among the parm
5740 declarations. If a parm is declared with enum {foo, bar} x;
5741 then CONST_DECLs for foo and bar are put here. */
5742 tree nonparms = 0;
5744 /* The function containing FNDECL, if any. */
5745 tree context = decl_function_context (fndecl);
5747 /* Nonzero if this definition is written with a prototype. */
5748 int prototype = 0;
5750 bool saved_warn_shadow = warn_shadow;
5752 /* Don't re-emit shadow warnings. */
5753 warn_shadow = false;
5755 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5757 /* This case is when the function was defined with an ANSI prototype.
5758 The parms already have decls, so we need not do anything here
5759 except record them as in effect
5760 and complain if any redundant old-style parm decls were written. */
5762 tree next;
5763 tree others = 0;
5765 prototype = 1;
5767 if (parmdecls != 0)
5769 tree decl, link;
5771 error_with_decl (fndecl,
5772 "parm types given both in parmlist and separately");
5773 /* Get rid of the erroneous decls; don't keep them on
5774 the list of parms, since they might not be PARM_DECLs. */
5775 for (decl = current_binding_level->names;
5776 decl; decl = TREE_CHAIN (decl))
5777 if (DECL_NAME (decl))
5778 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = 0;
5779 for (link = current_binding_level->shadowed;
5780 link; link = TREE_CHAIN (link))
5781 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5782 current_binding_level->names = 0;
5783 current_binding_level->shadowed = 0;
5786 specparms = nreverse (specparms);
5787 for (parm = specparms; parm; parm = next)
5789 next = TREE_CHAIN (parm);
5790 if (TREE_CODE (parm) == PARM_DECL)
5792 if (DECL_NAME (parm) == 0)
5793 error_with_decl (parm, "parameter name omitted");
5794 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5795 && VOID_TYPE_P (TREE_TYPE (parm)))
5797 error_with_decl (parm, "parameter `%s' declared void");
5798 /* Change the type to error_mark_node so this parameter
5799 will be ignored by assign_parms. */
5800 TREE_TYPE (parm) = error_mark_node;
5802 pushdecl (parm);
5804 else
5806 /* If we find an enum constant or a type tag,
5807 put it aside for the moment. */
5808 TREE_CHAIN (parm) = 0;
5809 others = chainon (others, parm);
5813 /* Get the decls in their original chain order
5814 and record in the function. */
5815 DECL_ARGUMENTS (fndecl) = getdecls ();
5817 #if 0
5818 /* If this function takes a variable number of arguments,
5819 add a phony parameter to the end of the parm list,
5820 to represent the position of the first unnamed argument. */
5821 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
5822 != void_type_node)
5824 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
5825 /* Let's hope the address of the unnamed parm
5826 won't depend on its type. */
5827 TREE_TYPE (dummy) = integer_type_node;
5828 DECL_ARG_TYPE (dummy) = integer_type_node;
5829 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
5831 #endif
5833 /* Now pushdecl the enum constants. */
5834 for (parm = others; parm; parm = next)
5836 next = TREE_CHAIN (parm);
5837 if (DECL_NAME (parm) == 0)
5839 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5841 else if (TREE_CODE (parm) != PARM_DECL)
5842 pushdecl (parm);
5845 storetags (chainon (parmtags, gettags ()));
5847 else
5849 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5850 each with a parm name as the TREE_VALUE.
5852 PARMDECLS is a chain of declarations for parameters.
5853 Warning! It can also contain CONST_DECLs which are not parameters
5854 but are names of enumerators of any enum types
5855 declared among the parameters.
5857 First match each formal parameter name with its declaration.
5858 Associate decls with the names and store the decls
5859 into the TREE_PURPOSE slots. */
5861 /* We use DECL_WEAK as a flag to show which parameters have been
5862 seen already since it is not used on PARM_DECL or CONST_DECL. */
5863 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5864 DECL_WEAK (parm) = 0;
5866 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5868 tree tail, found = NULL;
5870 if (TREE_VALUE (parm) == 0)
5872 error_with_decl (fndecl,
5873 "parameter name missing from parameter list");
5874 TREE_PURPOSE (parm) = 0;
5875 continue;
5878 /* See if any of the parmdecls specifies this parm by name.
5879 Ignore any enumerator decls. */
5880 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5881 if (DECL_NAME (tail) == TREE_VALUE (parm)
5882 && TREE_CODE (tail) == PARM_DECL)
5884 found = tail;
5885 break;
5888 /* If declaration already marked, we have a duplicate name.
5889 Complain, and don't use this decl twice. */
5890 if (found && DECL_WEAK (found))
5892 error_with_decl (found, "multiple parameters named `%s'");
5893 found = 0;
5896 /* If the declaration says "void", complain and ignore it. */
5897 if (found && VOID_TYPE_P (TREE_TYPE (found)))
5899 error_with_decl (found, "parameter `%s' declared void");
5900 TREE_TYPE (found) = integer_type_node;
5901 DECL_ARG_TYPE (found) = integer_type_node;
5902 layout_decl (found, 0);
5905 /* If no declaration found, default to int. */
5906 if (!found)
5908 found = build_decl (PARM_DECL, TREE_VALUE (parm),
5909 integer_type_node);
5910 DECL_ARG_TYPE (found) = TREE_TYPE (found);
5911 DECL_SOURCE_LOCATION (found) = DECL_SOURCE_LOCATION (fndecl);
5912 if (flag_isoc99)
5913 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
5914 else if (extra_warnings)
5915 warning_with_decl (found, "type of `%s' defaults to `int'");
5916 pushdecl (found);
5919 TREE_PURPOSE (parm) = found;
5921 /* Mark this decl as "already found". */
5922 DECL_WEAK (found) = 1;
5925 /* Put anything which is on the parmdecls chain and which is
5926 not a PARM_DECL onto the list NONPARMS. (The types of
5927 non-parm things which might appear on the list include
5928 enumerators and NULL-named TYPE_DECL nodes.) Complain about
5929 any actual PARM_DECLs not matched with any names. */
5931 nonparms = 0;
5932 for (parm = parmdecls; parm;)
5934 tree next = TREE_CHAIN (parm);
5935 TREE_CHAIN (parm) = 0;
5937 if (TREE_CODE (parm) != PARM_DECL)
5938 nonparms = chainon (nonparms, parm);
5939 else
5941 /* Complain about args with incomplete types. */
5942 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5944 error_with_decl (parm, "parameter `%s' has incomplete type");
5945 TREE_TYPE (parm) = error_mark_node;
5948 if (! DECL_WEAK (parm))
5950 error_with_decl (parm,
5951 "declaration for parameter `%s' but no such parameter");
5952 /* Pretend the parameter was not missing.
5953 This gets us to a standard state and minimizes
5954 further error messages. */
5955 specparms
5956 = chainon (specparms,
5957 tree_cons (parm, NULL_TREE, NULL_TREE));
5961 parm = next;
5964 /* Chain the declarations together in the order of the list of
5965 names. Store that chain in the function decl, replacing the
5966 list of names. */
5967 parm = specparms;
5968 DECL_ARGUMENTS (fndecl) = 0;
5970 tree last;
5971 for (last = 0; parm; parm = TREE_CHAIN (parm))
5972 if (TREE_PURPOSE (parm))
5974 if (last == 0)
5975 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
5976 else
5977 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5978 last = TREE_PURPOSE (parm);
5979 TREE_CHAIN (last) = 0;
5983 /* If there was a previous prototype,
5984 set the DECL_ARG_TYPE of each argument according to
5985 the type previously specified, and report any mismatches. */
5987 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5989 tree type;
5990 for (parm = DECL_ARGUMENTS (fndecl),
5991 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5992 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5993 != void_type_node));
5994 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5996 if (parm == 0 || type == 0
5997 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5999 error ("number of arguments doesn't match prototype");
6000 error ("%Hprototype declaration",
6001 &current_function_prototype_locus);
6002 break;
6004 /* Type for passing arg must be consistent with that
6005 declared for the arg. ISO C says we take the unqualified
6006 type for parameters declared with qualified type. */
6007 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6008 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6009 COMPARE_STRICT))
6011 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6012 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6014 /* Adjust argument to match prototype. E.g. a previous
6015 `int foo(float);' prototype causes
6016 `int foo(x) float x; {...}' to be treated like
6017 `int foo(float x) {...}'. This is particularly
6018 useful for argument types like uid_t. */
6019 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6021 if (PROMOTE_PROTOTYPES
6022 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6023 && TYPE_PRECISION (TREE_TYPE (parm))
6024 < TYPE_PRECISION (integer_type_node))
6025 DECL_ARG_TYPE (parm) = integer_type_node;
6027 if (pedantic)
6029 pedwarn ("promoted argument `%s' doesn't match prototype",
6030 IDENTIFIER_POINTER (DECL_NAME (parm)));
6031 warning ("%Hprototype declaration",
6032 &current_function_prototype_locus);
6035 else
6037 error ("argument `%s' doesn't match prototype",
6038 IDENTIFIER_POINTER (DECL_NAME (parm)));
6039 error ("%Hprototype declaration",
6040 &current_function_prototype_locus);
6044 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6047 /* Otherwise, create a prototype that would match. */
6049 else
6051 tree actual = 0, last = 0, type;
6053 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6055 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6056 if (last)
6057 TREE_CHAIN (last) = type;
6058 else
6059 actual = type;
6060 last = type;
6062 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6063 if (last)
6064 TREE_CHAIN (last) = type;
6065 else
6066 actual = type;
6068 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6069 of the type of this function, but we need to avoid having this
6070 affect the types of other similarly-typed functions, so we must
6071 first force the generation of an identical (but separate) type
6072 node for the relevant function type. The new node we create
6073 will be a variant of the main variant of the original function
6074 type. */
6076 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6078 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6081 /* Now store the final chain of decls for the arguments
6082 as the decl-chain of the current lexical scope.
6083 Put the enumerators in as well, at the front so that
6084 DECL_ARGUMENTS is not modified. */
6086 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6089 /* Make sure the binding level for the top of the function body
6090 gets a BLOCK if there are any in the function.
6091 Otherwise, the dbx output is wrong. */
6093 keep_next_if_subblocks = 1;
6095 /* ??? This might be an improvement,
6096 but needs to be thought about some more. */
6097 #if 0
6098 keep_next_level_flag = 1;
6099 #endif
6101 /* Write a record describing this function definition to the prototypes
6102 file (if requested). */
6104 gen_aux_info_record (fndecl, 1, 0, prototype);
6106 /* Initialize the RTL code for the function. */
6107 init_function_start (fndecl);
6109 /* Begin the statement tree for this function. */
6110 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6112 /* If this is a nested function, save away the sizes of any
6113 variable-size types so that we can expand them when generating
6114 RTL. */
6115 if (context)
6117 tree t;
6119 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6120 = nreverse (get_pending_sizes ());
6121 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6123 t = TREE_CHAIN (t))
6124 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6127 /* This function is being processed in whole-function mode. */
6128 cfun->x_whole_function_mode_p = 1;
6130 /* Even though we're inside a function body, we still don't want to
6131 call expand_expr to calculate the size of a variable-sized array.
6132 We haven't necessarily assigned RTL to all variables yet, so it's
6133 not safe to try to expand expressions involving them. */
6134 immediate_size_expand = 0;
6135 cfun->x_dont_save_pending_sizes_p = 1;
6137 warn_shadow = saved_warn_shadow;
6140 /* Finish up a function declaration and compile that function
6141 all the way to assembler language output. The free the storage
6142 for the function definition.
6144 This is called after parsing the body of the function definition.
6146 NESTED is nonzero if the function being finished is nested in another.
6147 CAN_DEFER_P is nonzero if the function may be deferred. */
6149 void
6150 finish_function (int nested, int can_defer_p)
6152 tree fndecl = current_function_decl;
6154 /* When a function declaration is totally empty, e.g.
6155 void foo(void) { }
6156 (the argument list is irrelevant) the compstmt rule will not
6157 bother calling pushlevel/poplevel, which means we get here with
6158 the binding_level stack out of sync. Detect this situation by
6159 noticing that the current_binding_level is still as
6160 store_parm_decls left it, and do a dummy push/pop to get back to
6161 consistency. Note that the call to pushlevel does not actually
6162 push another binding level - see there for details. */
6163 if (current_binding_level->parm_flag && keep_next_if_subblocks)
6165 pushlevel (0);
6166 poplevel (1, 0, 1);
6169 #if 0
6170 /* This caused &foo to be of type ptr-to-const-function which then
6171 got a warning when stored in a ptr-to-function variable. */
6172 TREE_READONLY (fndecl) = 1;
6173 #endif
6175 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6177 /* Must mark the RESULT_DECL as being in this function. */
6179 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6181 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6183 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6184 != integer_type_node)
6186 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6187 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6188 if (! warn_main)
6189 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6191 else
6193 #ifdef DEFAULT_MAIN_RETURN
6194 /* Make it so that `main' always returns success by default. */
6195 DEFAULT_MAIN_RETURN;
6196 #else
6197 if (flag_isoc99)
6198 c_expand_return (integer_zero_node);
6199 #endif
6203 finish_fname_decls ();
6205 /* Tie off the statement tree for this function. */
6206 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6208 /* Complain if there's just no return statement. */
6209 if (warn_return_type
6210 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6211 && !current_function_returns_value && !current_function_returns_null
6212 /* Don't complain if we abort. */
6213 && !current_function_returns_abnormally
6214 /* Don't warn for main(). */
6215 && !MAIN_NAME_P (DECL_NAME (fndecl))
6216 /* Or if they didn't actually specify a return type. */
6217 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6218 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6219 inline function, as we might never be compiled separately. */
6220 && DECL_INLINE (fndecl))
6221 warning ("no return statement in function returning non-void");
6223 /* Clear out memory we no longer need. */
6224 free_after_parsing (cfun);
6225 /* Since we never call rest_of_compilation, we never clear
6226 CFUN. Do so explicitly. */
6227 free_after_compilation (cfun);
6228 cfun = NULL;
6230 if (flag_unit_at_a_time && can_defer_p)
6232 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6233 current_function_decl = NULL;
6234 return;
6237 if (! nested)
6239 /* Function is parsed.
6240 Generate RTL for the body of this function or defer
6241 it for later expansion. */
6242 int uninlinable = 1;
6244 /* There's no reason to do any of the work here if we're only doing
6245 semantic analysis; this code just generates RTL. */
6246 if (flag_syntax_only)
6248 current_function_decl = NULL;
6249 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6250 return;
6253 if (flag_inline_trees)
6255 /* First, cache whether the current function is inlinable. Some
6256 predicates depend on cfun and current_function_decl to
6257 function completely. */
6258 timevar_push (TV_INTEGRATION);
6259 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6261 if (can_defer_p
6262 /* We defer functions marked inline *even if* the function
6263 itself is not inlinable. This is because we don't yet
6264 know if the function will actually be used; we may be
6265 able to avoid emitting it entirely. */
6266 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6267 /* Save function tree for inlining. Should return 0 if the
6268 language does not support function deferring or the
6269 function could not be deferred. */
6270 && defer_fn (fndecl))
6272 /* Let the back-end know that this function exists. */
6273 (*debug_hooks->deferred_inline_function) (fndecl);
6274 timevar_pop (TV_INTEGRATION);
6275 current_function_decl = NULL;
6276 return;
6279 /* Then, inline any functions called in it. */
6280 optimize_inline_calls (fndecl);
6281 timevar_pop (TV_INTEGRATION);
6284 c_expand_body (fndecl);
6286 /* Keep the function body if it's needed for inlining or dumping. */
6287 if (uninlinable && !dump_enabled_p (TDI_all))
6289 /* Allow the body of the function to be garbage collected. */
6290 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6293 /* Let the error reporting routines know that we're outside a
6294 function. For a nested function, this value is used in
6295 c_pop_function_context and then reset via pop_function_context. */
6296 current_function_decl = NULL;
6300 /* Generate the RTL for a deferred function FNDECL. */
6302 void
6303 c_expand_deferred_function (tree fndecl)
6305 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6306 function was deferred, e.g. in duplicate_decls. */
6307 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6309 if (flag_inline_trees)
6311 timevar_push (TV_INTEGRATION);
6312 optimize_inline_calls (fndecl);
6313 timevar_pop (TV_INTEGRATION);
6315 c_expand_body (fndecl);
6316 current_function_decl = NULL;
6320 /* Called to move the SAVE_EXPRs for parameter declarations in a
6321 nested function into the nested function. DATA is really the
6322 nested FUNCTION_DECL. */
6324 static tree
6325 set_save_expr_context (tree *tp,
6326 int *walk_subtrees,
6327 void *data)
6329 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6330 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6331 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6332 circularity. */
6333 else if (DECL_P (*tp))
6334 *walk_subtrees = 0;
6336 return NULL_TREE;
6339 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6340 then we are already in the process of generating RTL for another
6341 function. If can_defer_p is zero, we won't attempt to defer the
6342 generation of RTL. */
6344 static void
6345 c_expand_body_1 (tree fndecl, int nested_p)
6347 timevar_push (TV_EXPAND);
6349 if (nested_p)
6351 /* Make sure that we will evaluate variable-sized types involved
6352 in our function's type. */
6353 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6354 /* Squirrel away our current state. */
6355 push_function_context ();
6358 /* Initialize the RTL code for the function. */
6359 current_function_decl = fndecl;
6360 input_location = DECL_SOURCE_LOCATION (fndecl);
6361 init_function_start (fndecl);
6363 /* This function is being processed in whole-function mode. */
6364 cfun->x_whole_function_mode_p = 1;
6366 /* Even though we're inside a function body, we still don't want to
6367 call expand_expr to calculate the size of a variable-sized array.
6368 We haven't necessarily assigned RTL to all variables yet, so it's
6369 not safe to try to expand expressions involving them. */
6370 immediate_size_expand = 0;
6371 cfun->x_dont_save_pending_sizes_p = 1;
6373 /* Set up parameters and prepare for return, for the function. */
6374 expand_function_start (fndecl, 0);
6376 /* If the function has a variably modified type, there may be
6377 SAVE_EXPRs in the parameter types. Their context must be set to
6378 refer to this function; they cannot be expanded in the containing
6379 function. */
6380 if (decl_function_context (fndecl)
6381 && variably_modified_type_p (TREE_TYPE (fndecl)))
6382 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6383 NULL);
6385 /* If this function is `main', emit a call to `__main'
6386 to run global initializers, etc. */
6387 if (DECL_NAME (fndecl)
6388 && MAIN_NAME_P (DECL_NAME (fndecl))
6389 && DECL_CONTEXT (fndecl) == NULL_TREE)
6390 expand_main_function ();
6392 /* Generate the RTL for this function. */
6393 expand_stmt (DECL_SAVED_TREE (fndecl));
6395 /* We hard-wired immediate_size_expand to zero above.
6396 expand_function_end will decrement this variable. So, we set the
6397 variable to one here, so that after the decrement it will remain
6398 zero. */
6399 immediate_size_expand = 1;
6401 /* Allow language dialects to perform special processing. */
6402 if (lang_expand_function_end)
6403 (*lang_expand_function_end) ();
6405 /* Generate rtl for function exit. */
6406 expand_function_end ();
6408 /* If this is a nested function, protect the local variables in the stack
6409 above us from being collected while we're compiling this function. */
6410 if (nested_p)
6411 ggc_push_context ();
6413 /* Run the optimizers and output the assembler code for this function. */
6414 rest_of_compilation (fndecl);
6416 /* Undo the GC context switch. */
6417 if (nested_p)
6418 ggc_pop_context ();
6420 /* With just -Wextra, complain only if function returns both with
6421 and without a value. */
6422 if (extra_warnings
6423 && current_function_returns_value
6424 && current_function_returns_null)
6425 warning ("this function may return with or without a value");
6427 /* If requested, warn about function definitions where the function will
6428 return a value (usually of some struct or union type) which itself will
6429 take up a lot of stack space. */
6431 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6433 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6435 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6436 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6437 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6438 larger_than_size))
6440 unsigned int size_as_int
6441 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6443 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6444 warning_with_decl (fndecl,
6445 "size of return value of `%s' is %u bytes",
6446 size_as_int);
6447 else
6448 warning_with_decl (fndecl,
6449 "size of return value of `%s' is larger than %d bytes",
6450 larger_than_size);
6454 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6455 && ! flag_inline_trees)
6457 /* Stop pointing to the local nodes about to be freed.
6458 But DECL_INITIAL must remain nonzero so we know this
6459 was an actual function definition.
6460 For a nested function, this is done in c_pop_function_context.
6461 If rest_of_compilation set this to 0, leave it 0. */
6462 if (DECL_INITIAL (fndecl) != 0)
6463 DECL_INITIAL (fndecl) = error_mark_node;
6465 DECL_ARGUMENTS (fndecl) = 0;
6468 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6470 if (targetm.have_ctors_dtors)
6471 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6472 DEFAULT_INIT_PRIORITY);
6473 else
6474 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6477 if (DECL_STATIC_DESTRUCTOR (fndecl))
6479 if (targetm.have_ctors_dtors)
6480 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6481 DEFAULT_INIT_PRIORITY);
6482 else
6483 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6486 if (nested_p)
6487 /* Return to the enclosing function. */
6488 pop_function_context ();
6489 timevar_pop (TV_EXPAND);
6492 /* Like c_expand_body_1 but only for unnested functions. */
6494 void
6495 c_expand_body (tree fndecl)
6497 c_expand_body_1 (fndecl, 0);
6500 /* Check the declarations given in a for-loop for satisfying the C99
6501 constraints. */
6502 void
6503 check_for_loop_decls (void)
6505 tree t;
6507 if (!flag_isoc99)
6509 /* If we get here, declarations have been used in a for loop without
6510 the C99 for loop scope. This doesn't make much sense, so don't
6511 allow it. */
6512 error ("`for' loop initial declaration used outside C99 mode");
6513 return;
6515 /* C99 subclause 6.8.5 paragraph 3:
6517 [#3] The declaration part of a for statement shall only
6518 declare identifiers for objects having storage class auto or
6519 register.
6521 It isn't clear whether, in this sentence, "identifiers" binds to
6522 "shall only declare" or to "objects" - that is, whether all identifiers
6523 declared must be identifiers for objects, or whether the restriction
6524 only applies to those that are. (A question on this in comp.std.c
6525 in November 2000 received no answer.) We implement the strictest
6526 interpretation, to avoid creating an extension which later causes
6527 problems. */
6529 for (t = gettags (); t; t = TREE_CHAIN (t))
6531 if (TREE_PURPOSE (t) != 0)
6533 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6535 if (code == RECORD_TYPE)
6536 error ("`struct %s' declared in `for' loop initial declaration",
6537 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6538 else if (code == UNION_TYPE)
6539 error ("`union %s' declared in `for' loop initial declaration",
6540 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6541 else
6542 error ("`enum %s' declared in `for' loop initial declaration",
6543 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6547 for (t = getdecls (); t; t = TREE_CHAIN (t))
6549 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6550 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6551 else if (TREE_STATIC (t))
6552 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6553 else if (DECL_EXTERNAL (t))
6554 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6558 /* Save and restore the variables in this file and elsewhere
6559 that keep track of the progress of compilation of the current function.
6560 Used for nested functions. */
6562 struct language_function GTY(())
6564 struct c_language_function base;
6565 tree named_labels;
6566 tree shadowed_labels;
6567 int returns_value;
6568 int returns_null;
6569 int returns_abnormally;
6570 int warn_about_return_type;
6571 int extern_inline;
6572 struct binding_level *binding_level;
6575 /* Save and reinitialize the variables
6576 used during compilation of a C function. */
6578 void
6579 c_push_function_context (struct function *f)
6581 struct language_function *p;
6582 p = ((struct language_function *)
6583 ggc_alloc (sizeof (struct language_function)));
6584 f->language = p;
6586 p->base.x_stmt_tree = c_stmt_tree;
6587 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6588 p->named_labels = named_labels;
6589 p->shadowed_labels = shadowed_labels;
6590 p->returns_value = current_function_returns_value;
6591 p->returns_null = current_function_returns_null;
6592 p->returns_abnormally = current_function_returns_abnormally;
6593 p->warn_about_return_type = warn_about_return_type;
6594 p->extern_inline = current_extern_inline;
6595 p->binding_level = current_binding_level;
6598 /* Restore the variables used during compilation of a C function. */
6600 void
6601 c_pop_function_context (struct function *f)
6603 struct language_function *p = f->language;
6604 tree link;
6606 /* Bring back all the labels that were shadowed. */
6607 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6608 if (DECL_NAME (TREE_VALUE (link)) != 0)
6609 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6610 = TREE_VALUE (link);
6612 if (DECL_SAVED_INSNS (current_function_decl) == 0
6613 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6615 /* Stop pointing to the local nodes about to be freed. */
6616 /* But DECL_INITIAL must remain nonzero so we know this
6617 was an actual function definition. */
6618 DECL_INITIAL (current_function_decl) = error_mark_node;
6619 DECL_ARGUMENTS (current_function_decl) = 0;
6622 c_stmt_tree = p->base.x_stmt_tree;
6623 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6624 named_labels = p->named_labels;
6625 shadowed_labels = p->shadowed_labels;
6626 current_function_returns_value = p->returns_value;
6627 current_function_returns_null = p->returns_null;
6628 current_function_returns_abnormally = p->returns_abnormally;
6629 warn_about_return_type = p->warn_about_return_type;
6630 current_extern_inline = p->extern_inline;
6631 current_binding_level = p->binding_level;
6633 f->language = NULL;
6636 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6638 void
6639 c_dup_lang_specific_decl (tree decl)
6641 struct lang_decl *ld;
6643 if (!DECL_LANG_SPECIFIC (decl))
6644 return;
6646 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6647 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6648 sizeof (struct lang_decl));
6649 DECL_LANG_SPECIFIC (decl) = ld;
6652 /* The functions below are required for functionality of doing
6653 function at once processing in the C front end. Currently these
6654 functions are not called from anywhere in the C front end, but as
6655 these changes continue, that will change. */
6657 /* Returns nonzero if the current statement is a full expression,
6658 i.e. temporaries created during that statement should be destroyed
6659 at the end of the statement. */
6662 stmts_are_full_exprs_p (void)
6664 return 0;
6667 /* Returns the stmt_tree (if any) to which statements are currently
6668 being added. If there is no active statement-tree, NULL is
6669 returned. */
6671 stmt_tree
6672 current_stmt_tree (void)
6674 return &c_stmt_tree;
6677 /* Returns the stack of SCOPE_STMTs for the current function. */
6679 tree *
6680 current_scope_stmt_stack (void)
6682 return &c_scope_stmt_stack;
6685 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6686 C. */
6689 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6691 return 0;
6694 /* Dummy function in place of callback used by C++. */
6696 void
6697 extract_interface_info (void)
6701 /* Return a new COMPOUND_STMT, after adding it to the current
6702 statement tree. */
6704 tree
6705 c_begin_compound_stmt (void)
6707 tree stmt;
6709 /* Create the COMPOUND_STMT. */
6710 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6712 return stmt;
6715 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6716 common code. */
6718 void
6719 c_expand_decl_stmt (tree t)
6721 tree decl = DECL_STMT_DECL (t);
6723 /* Expand nested functions. */
6724 if (TREE_CODE (decl) == FUNCTION_DECL
6725 && DECL_CONTEXT (decl) == current_function_decl
6726 && DECL_SAVED_TREE (decl))
6727 c_expand_body_1 (decl, 1);
6730 /* Return the global value of T as a symbol. */
6732 tree
6733 identifier_global_value (tree t)
6735 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6736 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6737 return decl;
6739 /* Shadowed by something else; find the true global value. */
6740 for (decl = global_binding_level->names; decl; decl = TREE_CHAIN (decl))
6741 if (DECL_NAME (decl) == t)
6742 return decl;
6744 /* Only local values for this decl. */
6745 return 0;
6748 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6749 otherwise the name is found in ridpointers from RID_INDEX. */
6751 void
6752 record_builtin_type (enum rid rid_index, const char *name, tree type)
6754 tree id;
6755 if (name == 0)
6756 id = ridpointers[(int) rid_index];
6757 else
6758 id = get_identifier (name);
6759 pushdecl (build_decl (TYPE_DECL, id, type));
6762 /* Build the void_list_node (void_type_node having been created). */
6763 tree
6764 build_void_list_node (void)
6766 tree t = build_tree_list (NULL_TREE, void_type_node);
6767 return t;
6770 /* Return something to represent absolute declarators containing a *.
6771 TARGET is the absolute declarator that the * contains.
6772 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6773 to apply to the pointer type, represented as identifiers, possible mixed
6774 with attributes.
6776 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6777 if attributes are present) and whose type is the modifier list. */
6779 tree
6780 make_pointer_declarator (tree type_quals_attrs, tree target)
6782 tree quals, attrs;
6783 tree itarget = target;
6784 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6785 if (attrs != NULL_TREE)
6786 itarget = tree_cons (attrs, target, NULL_TREE);
6787 return build1 (INDIRECT_REF, quals, itarget);
6790 /* Hash and equality functions for link_hash_table: key off
6791 DECL_ASSEMBLER_NAME. */
6793 static hashval_t
6794 link_hash_hash (const void *x_p)
6796 tree x = (tree)x_p;
6797 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6800 static int
6801 link_hash_eq (const void *x1_p, const void *x2_p)
6803 tree x1 = (tree)x1_p;
6804 tree x2 = (tree)x2_p;
6805 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6808 /* Propagate information between definitions and uses between multiple
6809 translation units in TU_LIST based on linkage rules. */
6811 void
6812 merge_translation_unit_decls (void)
6814 const tree tu_list = current_file_decl;
6815 tree tu;
6816 tree decl;
6817 htab_t link_hash_table;
6818 tree block;
6820 /* Create the BLOCK that poplevel would have created, but don't
6821 actually call poplevel since that's expensive. */
6822 block = make_node (BLOCK);
6823 BLOCK_VARS (block) = current_binding_level->names;
6824 TREE_USED (block) = 1;
6825 DECL_INITIAL (current_file_decl) = block;
6827 /* If only one translation unit seen, no copying necessary. */
6828 if (TREE_CHAIN (tu_list) == NULL_TREE)
6829 return;
6831 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6833 /* Enter any actual definitions into the hash table. */
6834 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6835 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6836 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6838 PTR *slot;
6839 slot = htab_find_slot (link_hash_table, decl, INSERT);
6841 /* If we've already got a definition, work out which one is
6842 the real one, put it into the hash table, and make the
6843 other one DECL_EXTERNAL. This is important to avoid
6844 putting out two definitions of the same symbol in the
6845 assembly output. */
6846 if (*slot != NULL)
6848 tree old_decl = (tree) *slot;
6850 /* If this is weak or common or whatever, suppress it
6851 in favour of the other definition. */
6852 if (DECL_WEAK (decl))
6853 DECL_EXTERNAL (decl) = 1;
6854 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6855 DECL_EXTERNAL (old_decl) = 1;
6856 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6857 DECL_EXTERNAL (decl) = 1;
6858 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6859 DECL_EXTERNAL (old_decl) = 1;
6861 if (DECL_EXTERNAL (decl))
6863 DECL_INITIAL (decl) = NULL_TREE;
6864 DECL_COMMON (decl) = 0;
6865 DECL_ONE_ONLY (decl) = 0;
6866 DECL_WEAK (decl) = 0;
6868 else if (DECL_EXTERNAL (old_decl))
6870 DECL_INITIAL (old_decl) = NULL_TREE;
6871 DECL_COMMON (old_decl) = 0;
6872 DECL_ONE_ONLY (old_decl) = 0;
6873 DECL_WEAK (old_decl) = 0;
6874 *slot = decl;
6876 else
6878 error_with_decl (decl, "redefinition of global `%s'");
6879 error_with_decl (old_decl, "`%s' previously defined here");
6882 else
6883 *slot = decl;
6886 /* Now insert the desired information from all the definitions
6887 into any plain declarations. */
6888 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6889 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6890 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6892 tree global_decl;
6893 global_decl = (tree) htab_find (link_hash_table, decl);
6895 if (! global_decl)
6896 continue;
6898 /* Print any appropriate error messages, and partially merge
6899 the decls. */
6900 (void) duplicate_decls (decl, global_decl, true, true);
6903 htab_delete (link_hash_table);
6906 /* Perform final processing on file-scope data. */
6908 void
6909 c_write_global_declarations(void)
6911 tree link;
6913 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6915 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6916 int len = list_length (globals);
6917 tree *vec = (tree *) xmalloc (sizeof (tree) * len);
6918 int i;
6919 tree decl;
6921 /* Process the decls in reverse order--earliest first.
6922 Put them into VEC from back to front, then take out from front. */
6924 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6925 vec[len - i - 1] = decl;
6927 wrapup_global_declarations (vec, len);
6929 check_global_declarations (vec, len);
6931 /* Clean up. */
6932 free (vec);
6936 /* Reset the parser's state in preparation for a new file. */
6938 void
6939 c_reset_state (void)
6941 tree link;
6942 tree file_scope_decl;
6944 /* Pop the global binding level. */
6945 if (current_binding_level != global_binding_level)
6946 current_binding_level = global_binding_level;
6947 file_scope_decl = current_file_decl;
6948 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6949 truly_local_externals = NULL_TREE;
6951 /* Start a new global binding level. */
6952 pushlevel (0);
6953 global_binding_level = current_binding_level;
6954 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6955 TREE_CHAIN (current_file_decl) = file_scope_decl;
6957 /* Reintroduce the global declarations. */
6958 for (link = builtin_decls; link; link = TREE_CHAIN (link))
6959 pushdecl (copy_node (link));
6962 #include "gt-c-decl.h"