gcc:
[official-gcc.git] / gcc / c-decl.c
blobbd3b76140cbcbca72c5539c0ec33cd141ebb4818
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 of parameter decls here. */
88 static tree last_function_parms;
90 /* Parsing a function declarator leaves a chain of structure
91 and enum types declared in the parmlist here. */
93 static tree last_function_parm_tags;
95 /* After parsing the declarator that starts a function definition,
96 `start_function' puts the list of parameter names or chain of decls here
97 for `store_parm_decls' to find. */
99 static tree current_function_parms;
101 /* Similar, for last_function_parm_tags. */
103 static tree current_function_parm_tags;
105 /* Similar, for the file and line that the prototype came from if this is
106 an old-style definition. */
108 static location_t current_function_prototype_locus;
110 /* The current statement tree. */
112 static GTY(()) struct stmt_tree_s c_stmt_tree;
114 /* The current scope statement stack. */
116 static GTY(()) tree c_scope_stmt_stack;
118 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
119 that have names. Here so we can clear out their names' definitions
120 at the end of the function. */
122 static GTY(()) tree named_labels;
124 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
126 static GTY(()) tree shadowed_labels;
128 /* A list of external DECLs that appeared at block scope when there was
129 some other global meaning for that identifier. */
130 static GTY(()) tree truly_local_externals;
132 /* A list of the builtin file-scope DECLs. */
134 static GTY(()) tree builtin_decls;
136 /* A DECL for the current file-scope context. */
138 static GTY(()) tree current_file_decl;
140 /* Set to 0 at beginning of a function definition, set to 1 if
141 a return statement that specifies a return value is seen. */
143 int current_function_returns_value;
145 /* Set to 0 at beginning of a function definition, set to 1 if
146 a return statement with no argument is seen. */
148 int current_function_returns_null;
150 /* Set to 0 at beginning of a function definition, set to 1 if
151 a call to a noreturn function is seen. */
153 int current_function_returns_abnormally;
155 /* Set to nonzero by `grokdeclarator' for a function
156 whose return type is defaulted, if warnings for this are desired. */
158 static int warn_about_return_type;
160 /* Nonzero when starting a function declared `extern inline'. */
162 static int current_extern_inline;
164 /* For each binding contour we allocate a binding_level structure
165 * which records the names defined in that contour.
166 * Contours include:
167 * 0) the global one
168 * 1) one for each function definition,
169 * where internal declarations of the parameters appear.
170 * 2) one for each compound statement,
171 * to record its declarations.
173 * The current meaning of a name can be found by searching the levels from
174 * the current one out to the global one.
177 struct binding_level GTY(())
179 /* A chain of _DECL nodes for all variables, constants, functions,
180 and typedef types. These are in the reverse of the order supplied.
182 tree names;
184 /* A list of structure, union and enum definitions,
185 * for looking up tag names.
186 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
187 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
188 * or ENUMERAL_TYPE node.
190 tree tags;
192 /* For each level, a list of shadowed outer-level definitions
193 to be restored when this level is popped.
194 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
195 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
196 tree shadowed;
198 /* For each level, a list of shadowed outer-level tag definitions
199 to be restored when this level is popped.
200 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
201 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
202 tree shadowed_tags;
204 /* For each level (except not the global one),
205 a chain of BLOCK nodes for all the levels
206 that were entered and exited one level down. */
207 tree blocks;
209 /* The binding level which this one is contained in (inherits from). */
210 struct binding_level *level_chain;
212 /* Nonzero if we are currently filling this level with parameter
213 declarations. */
214 char parm_flag;
216 /* Nonzero if this is the outermost block scope of a function body.
217 This scope contains both the parameters and the local variables
218 declared in the outermost block. */
219 char function_body;
221 /* Nonzero means make a BLOCK for this level regardless of all else. */
222 char keep;
224 /* Nonzero means make a BLOCK if this level has any subblocks. */
225 char keep_if_subblocks;
227 /* List of decls in `names' that have incomplete structure or
228 union types. */
229 tree incomplete_list;
231 /* A list of decls giving the (reversed) specified order of parms,
232 not including any forward-decls in the parmlist.
233 This is so we can put the parms in proper order for assign_parms. */
234 tree parm_order;
237 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
239 /* The binding level currently in effect. */
241 static GTY(()) struct binding_level *current_binding_level;
243 /* A chain of binding_level structures awaiting reuse. */
245 static GTY((deletable (""))) struct binding_level *free_binding_level;
247 /* The outermost binding level, for names of file scope.
248 This is created when the compiler is started and exists
249 through the entire run. */
251 static GTY(()) struct binding_level *global_binding_level;
253 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
255 static int keep_next_level_flag;
257 /* Nonzero means make a BLOCK for the next level pushed
258 if it has subblocks. */
260 static int keep_next_if_subblocks;
262 /* The chain of outer levels of label scopes.
263 This uses the same data structure used for binding levels,
264 but it works differently: each link in the chain records
265 saved values of named_labels and shadowed_labels for
266 a label binding level outside the current one. */
268 static GTY(()) struct binding_level *label_level_chain;
270 /* Functions called automatically at the beginning and end of execution. */
272 tree static_ctors, static_dtors;
274 /* Forward declarations. */
276 static struct binding_level *make_binding_level (void);
277 static void pop_binding_level (struct binding_level **);
278 static int duplicate_decls (tree, tree, int, int);
279 static int redeclaration_error_message (tree, tree);
280 static void implicit_decl_warning (tree);
281 static void storedecls (tree);
282 static void storetags (tree);
283 static tree lookup_tag (enum tree_code, tree, int);
284 static tree lookup_name_current_level (tree);
285 static tree grokdeclarator (tree, tree, enum decl_context, int);
286 static tree grokparms (tree, int);
287 static void layout_array_type (tree);
288 static tree c_make_fname_decl (tree, int);
289 static void c_expand_body_1 (tree, int);
290 static tree any_external_decl (tree);
291 static void record_external_decl (tree);
292 static void warn_if_shadowing (tree, tree);
293 static void clone_underlying_type (tree);
294 static void pushdecl_function_level (tree, tree);
295 static bool flexible_array_type_p (tree);
296 static hashval_t link_hash_hash (const void *);
297 static int link_hash_eq (const void *, const void *);
299 /* States indicating how grokdeclarator() should handle declspecs marked
300 with __attribute__((deprecated)). An object declared as
301 __attribute__((deprecated)) suppresses warnings of uses of other
302 deprecated items. */
304 enum deprecated_states {
305 DEPRECATED_NORMAL,
306 DEPRECATED_SUPPRESS
309 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
311 void
312 c_print_identifier (FILE *file, tree node, int indent)
314 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
315 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
316 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
317 if (C_IS_RESERVED_WORD (node))
319 tree rid = ridpointers[C_RID_CODE (node)];
320 indent_to (file, indent + 4);
321 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
322 (void *) rid, IDENTIFIER_POINTER (rid));
326 /* Hook called at end of compilation to assume 1 elt
327 for a top-level tentative array defn that wasn't complete before. */
329 void
330 c_finish_incomplete_decl (tree decl)
332 if (TREE_CODE (decl) == VAR_DECL)
334 tree type = TREE_TYPE (decl);
335 if (type != error_mark_node
336 && TREE_CODE (type) == ARRAY_TYPE
337 && ! DECL_EXTERNAL (decl)
338 && TYPE_DOMAIN (type) == 0)
340 warning ("%Harray '%D' assumed to have one element",
341 &DECL_SOURCE_LOCATION (decl), decl);
343 complete_array_type (type, NULL_TREE, 1);
345 layout_decl (decl, 0);
350 /* Reuse or create a struct for this binding level. */
352 static struct binding_level *
353 make_binding_level (void)
355 struct binding_level *result;
356 if (free_binding_level)
358 result = free_binding_level;
359 free_binding_level = result->level_chain;
360 memset (result, 0, sizeof(struct binding_level));
362 else
363 result = ggc_alloc_cleared (sizeof (struct binding_level));
365 return result;
368 /* Remove a binding level from a list and add it to the level chain. */
370 static void
371 pop_binding_level (struct binding_level **lp)
373 struct binding_level *l = *lp;
374 *lp = l->level_chain;
376 memset (l, 0, sizeof (struct binding_level));
377 l->level_chain = free_binding_level;
378 free_binding_level = l;
381 /* Nonzero if we are currently in the global binding level. */
384 global_bindings_p (void)
386 return current_binding_level == global_binding_level;
389 void
390 keep_next_level (void)
392 keep_next_level_flag = 1;
395 /* Identify this binding level as a level of parameters. */
397 void
398 declare_parm_level (void)
400 current_binding_level->parm_flag = 1;
403 /* Nonzero if currently making parm declarations. */
406 in_parm_level_p (void)
408 return current_binding_level->parm_flag;
411 /* Enter a new binding level. */
413 void
414 pushlevel (int dummy ATTRIBUTE_UNUSED)
416 /* If this is the top level of a function, make sure that
417 NAMED_LABELS is 0. */
419 if (current_binding_level == global_binding_level)
420 named_labels = 0;
422 if (keep_next_if_subblocks)
424 /* This is the transition from the parameters to the top level
425 of the function body. These are the same scope
426 (C99 6.2.1p4,6) so we do not push another binding level.
428 XXX Note kludge - keep_next_if_subblocks is set only by
429 store_parm_decls, which in turn is called when and only
430 when we are about to encounter the opening curly brace for
431 the function body. */
432 current_binding_level->parm_flag = 0;
433 current_binding_level->function_body = 1;
434 current_binding_level->keep |= keep_next_level_flag;
435 current_binding_level->keep_if_subblocks = 1;
437 keep_next_level_flag = 0;
438 keep_next_if_subblocks = 0;
440 else
442 struct binding_level *newlevel = make_binding_level ();
444 newlevel->keep = keep_next_level_flag;
445 newlevel->level_chain = current_binding_level;
446 current_binding_level = newlevel;
447 keep_next_level_flag = 0;
451 /* Exit a binding level.
452 Pop the level off, and restore the state of the identifier-decl mappings
453 that were in effect when this level was entered.
455 If KEEP is nonzero, this level had explicit declarations, so
456 and create a "block" (a BLOCK node) for the level
457 to record its declarations and subblocks for symbol table output.
459 If FUNCTIONBODY is nonzero, this level is the body of a function,
460 so create a block as if KEEP were set and also clear out all
461 label names.
463 If REVERSE is nonzero, reverse the order of decls before putting
464 them into the BLOCK. */
466 tree
467 poplevel (int keep, int reverse, int functionbody)
469 tree link;
470 tree block;
471 tree decl;
472 tree decls = current_binding_level->names;
473 tree tags = current_binding_level->tags;
474 tree subblocks = current_binding_level->blocks;
476 functionbody |= current_binding_level->function_body;
478 if (keep == KEEP_MAYBE)
479 keep = (current_binding_level->names || current_binding_level->tags);
481 keep |= (current_binding_level->keep || functionbody
482 || (subblocks && current_binding_level->keep_if_subblocks));
484 /* We used to warn about unused variables in expand_end_bindings,
485 i.e. while generating RTL. But in function-at-a-time mode we may
486 choose to never expand a function at all (e.g. auto inlining), so
487 we do this explicitly now.
488 No warnings when the global scope is popped because the global
489 scope isn't popped for the last translation unit, so the warnings
490 are done in c_write_global_declaration. */
491 if (current_binding_level != global_binding_level)
492 warn_about_unused_variables (decls);
494 /* Clear out the name-meanings declared on this level.
495 Propagate TREE_ADDRESSABLE from nested functions to their
496 containing functions. */
497 for (link = decls; link; link = TREE_CHAIN (link))
499 if (DECL_NAME (link) != 0)
501 if (DECL_EXTERNAL (link)
502 && current_binding_level != global_binding_level)
503 /* External decls stay in the symbol-value slot but are
504 inaccessible. */
505 C_DECL_INVISIBLE (link) = 1;
506 else
507 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
510 if (TREE_CODE (link) == FUNCTION_DECL
511 && ! TREE_ASM_WRITTEN (link)
512 && DECL_INITIAL (link) != 0
513 && TREE_ADDRESSABLE (link)
514 && DECL_ABSTRACT_ORIGIN (link) != 0
515 && DECL_ABSTRACT_ORIGIN (link) != link)
516 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
519 /* Clear out the tag-meanings declared on this level. */
520 for (link = tags; link; link = TREE_CHAIN (link))
521 if (TREE_PURPOSE (link))
522 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
524 /* Restore all name-meanings of the outer levels
525 that were shadowed by this level. */
527 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
528 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
530 /* Restore all tag-meanings of the outer levels
531 that were shadowed by this level. */
533 for (link = current_binding_level->shadowed_tags; link;
534 link = TREE_CHAIN (link))
535 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
537 /* If this is the top level block of a function, remove all
538 PARM_DECLs from current_binding_level->names; they are already
539 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
540 not be put in BLOCK_VARS, and furthermore reversing them will
541 cause trouble later. They are all together at the end of the
542 list. */
543 if (functionbody && decls)
545 if (TREE_CODE (decls) == PARM_DECL)
546 decls = 0;
547 else
549 link = decls;
550 while (TREE_CHAIN (link)
551 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
552 link = TREE_CHAIN (link);
554 TREE_CHAIN (link) = 0;
558 /* Get the decls in the order they were written.
559 Usually current_binding_level->names is in reverse order.
560 But parameter decls were previously put in forward order. */
562 if (reverse)
563 decls = nreverse (decls);
565 /* If there were any declarations or structure tags in that level,
566 or if this level is a function body,
567 create a BLOCK to record them for the life of this function. */
569 block = 0;
570 if (keep)
572 block = make_node (BLOCK);
573 BLOCK_VARS (block) = decls;
574 BLOCK_SUBBLOCKS (block) = subblocks;
575 TREE_USED (block) = 1;
578 /* In each subblock, record that this is its superior. */
580 for (link = subblocks; link; link = TREE_CHAIN (link))
581 BLOCK_SUPERCONTEXT (link) = block;
583 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
584 binding contour so that they point to the appropriate construct, i.e.
585 either to the current FUNCTION_DECL node, or else to the BLOCK node
586 we just constructed.
588 Note that for tagged types whose scope is just the formal parameter
589 list for some function type specification, we can't properly set
590 their TYPE_CONTEXTs here, because we don't have a pointer to the
591 appropriate FUNCTION_TYPE node readily available to us. For those
592 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
593 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
594 node which will represent the "scope" for these "parameter list local"
595 tagged types. */
597 decl = functionbody ? current_function_decl : block;
598 if (decl)
599 for (link = tags; link; link = TREE_CHAIN (link))
600 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
602 /* If the level being exited is the top level of a function, check
603 over all the labels, and clear out the current (function local)
604 meanings of their names. Then add them to BLOCK_VARS. */
606 if (functionbody)
608 for (link = named_labels; link; link = TREE_CHAIN (link))
610 tree label = TREE_VALUE (link);
612 if (DECL_INITIAL (label) == 0)
614 error ("%Hlabel '%D' used but not defined",
615 &DECL_SOURCE_LOCATION (label), label);
616 /* Avoid crashing later. */
617 define_label (input_location, DECL_NAME (label));
619 else if (warn_unused_label && !TREE_USED (label))
620 warning ("%Hlabel '%D' defined but not used",
621 &DECL_SOURCE_LOCATION (label), label);
622 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
624 /* Put the labels into the "variables" of the
625 top-level block, so debugger can see them. */
626 TREE_CHAIN (label) = BLOCK_VARS (block);
627 BLOCK_VARS (block) = label;
631 /* Pop the current level, and free the structure for reuse. */
633 pop_binding_level (&current_binding_level);
635 /* Dispose of the block that we just made inside some higher level. */
636 if (functionbody)
637 DECL_INITIAL (current_function_decl) = block;
638 else if (block && current_binding_level)
639 current_binding_level->blocks
640 = chainon (current_binding_level->blocks, block);
641 /* If we did not make a block for the level just exited,
642 any blocks made for inner levels
643 (since they cannot be recorded as subblocks in that level)
644 must be carried forward so they will later become subblocks
645 of something else. */
646 else if (! block && subblocks)
647 current_binding_level->blocks
648 = chainon (current_binding_level->blocks, subblocks);
650 return block;
653 /* Insert BLOCK at the end of the list of subblocks of the
654 current binding level. This is used when a BIND_EXPR is expanded,
655 to handle the BLOCK node inside the BIND_EXPR. */
657 void
658 insert_block (tree block)
660 TREE_USED (block) = 1;
661 current_binding_level->blocks
662 = chainon (current_binding_level->blocks, block);
665 /* Set the BLOCK node for the innermost scope (the one we are
666 currently in). The RTL expansion machinery requires us to provide
667 this hook, but it is not useful in function-at-a-time mode. */
669 void
670 set_block (tree block ATTRIBUTE_UNUSED)
674 void
675 push_label_level (void)
677 struct binding_level *newlevel;
679 newlevel = make_binding_level ();
681 /* Add this level to the front of the chain (stack) of label levels. */
683 newlevel->level_chain = label_level_chain;
684 label_level_chain = newlevel;
686 newlevel->names = named_labels;
687 newlevel->shadowed = shadowed_labels;
688 named_labels = 0;
689 shadowed_labels = 0;
692 void
693 pop_label_level (void)
695 struct binding_level *level = label_level_chain;
696 tree link, prev;
698 /* Clear out the definitions of the declared labels in this level.
699 Leave in the list any ordinary, non-declared labels. */
700 for (link = named_labels, prev = 0; link;)
702 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
704 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
706 error ("%Hlabel '%D' used but not defined",
707 &DECL_SOURCE_LOCATION (TREE_VALUE (link)),
708 TREE_VALUE (link));
709 /* Avoid crashing later. */
710 define_label (input_location, DECL_NAME (TREE_VALUE (link)));
712 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
713 warning ("%Hlabel '%D' defined but not used",
714 &DECL_SOURCE_LOCATION (TREE_VALUE (link)),
715 TREE_VALUE (link));
716 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
718 /* Delete this element from the list. */
719 link = TREE_CHAIN (link);
720 if (prev)
721 TREE_CHAIN (prev) = link;
722 else
723 named_labels = link;
725 else
727 prev = link;
728 link = TREE_CHAIN (link);
732 /* Bring back all the labels that were shadowed. */
733 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
734 if (DECL_NAME (TREE_VALUE (link)) != 0)
735 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
736 = TREE_VALUE (link);
738 named_labels = chainon (named_labels, level->names);
739 shadowed_labels = level->shadowed;
741 /* Pop the current level, and free the structure for reuse. */
742 pop_binding_level (&label_level_chain);
745 /* Push a definition or a declaration of struct, union or enum tag "name".
746 "type" should be the type node.
747 We assume that the tag "name" is not already defined.
749 Note that the definition may really be just a forward reference.
750 In that case, the TYPE_SIZE will be zero. */
752 void
753 pushtag (tree name, tree type)
755 struct binding_level *b = current_binding_level;
757 if (name)
759 /* Record the identifier as the type's name if it has none. */
761 if (TYPE_NAME (type) == 0)
762 TYPE_NAME (type) = name;
764 if (IDENTIFIER_TAG_VALUE (name))
765 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
766 b->shadowed_tags);
767 IDENTIFIER_TAG_VALUE (name) = type;
770 b->tags = tree_cons (name, type, b->tags);
772 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
773 tagged type we just added to the current binding level. This fake
774 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
775 to output a representation of a tagged type, and it also gives
776 us a convenient place to record the "scope start" address for the
777 tagged type. */
779 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
781 /* An approximation for now, so we can tell this is a function-scope tag.
782 This will be updated in poplevel. */
783 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
786 /* Handle when a new declaration NEWDECL
787 has the same name as an old one OLDDECL
788 in the same binding contour.
789 Prints an error message if appropriate.
791 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
792 Otherwise, return 0.
794 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
795 and OLDDECL is in an outer binding level and should thus not be changed. */
797 static int
798 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
799 int different_tu)
801 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
802 : COMPARE_STRICT);
803 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
804 comptype_flags);
805 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
806 && DECL_INITIAL (newdecl) != 0);
807 tree oldtype = TREE_TYPE (olddecl);
808 tree newtype = TREE_TYPE (newdecl);
809 int errmsg = 0;
811 if (DECL_P (olddecl))
813 if (TREE_CODE (newdecl) == FUNCTION_DECL
814 && TREE_CODE (olddecl) == FUNCTION_DECL
815 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
817 if (DECL_DECLARED_INLINE_P (newdecl)
818 && DECL_UNINLINABLE (newdecl)
819 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
820 /* Already warned elsewhere. */;
821 else if (DECL_DECLARED_INLINE_P (olddecl)
822 && DECL_UNINLINABLE (olddecl)
823 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
824 /* Already warned. */;
825 else if (DECL_DECLARED_INLINE_P (newdecl)
826 && ! DECL_DECLARED_INLINE_P (olddecl)
827 && DECL_UNINLINABLE (olddecl)
828 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
830 warning ("%Hfunction '%D' redeclared as inline",
831 &DECL_SOURCE_LOCATION (newdecl), newdecl);
832 warning ("%Hprevious declaration of function '%D' "
833 "with attribute noinline",
834 &DECL_SOURCE_LOCATION (olddecl), olddecl);
836 else if (DECL_DECLARED_INLINE_P (olddecl)
837 && DECL_UNINLINABLE (newdecl)
838 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
840 warning ("%Hfunction '%D' redeclared with attribute noinline",
841 &DECL_SOURCE_LOCATION (newdecl), newdecl);
842 warning ("%Hprevious declaration of function '%D' was inline",
843 &DECL_SOURCE_LOCATION (olddecl), olddecl);
847 DECL_ATTRIBUTES (newdecl)
848 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
851 if (TREE_CODE (newtype) == ERROR_MARK
852 || TREE_CODE (oldtype) == ERROR_MARK)
853 types_match = 0;
855 /* New decl is completely inconsistent with the old one =>
856 tell caller to replace the old one.
857 This is always an error except in the case of shadowing a builtin. */
858 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
860 if (TREE_CODE (olddecl) == FUNCTION_DECL
861 && DECL_BUILT_IN (olddecl))
863 /* If you declare a built-in or predefined function name as static,
864 the old definition is overridden,
865 but optionally warn this was a bad choice of name. */
866 if (!TREE_PUBLIC (newdecl))
868 if (warn_shadow)
869 warning ("%Hshadowing built-in function '%D'",
870 &DECL_SOURCE_LOCATION (newdecl), newdecl);
872 else
873 warning ("%Hbuilt-in function '%D' declared as non-function",
874 &DECL_SOURCE_LOCATION (newdecl), newdecl);
876 else
878 error ("%H'%D' redeclared as different kind of symbol",
879 &DECL_SOURCE_LOCATION (newdecl), newdecl);
880 error ("%Hprevious declaration of '%D'",
881 &DECL_SOURCE_LOCATION (olddecl), olddecl);
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 ("%Hshadowing built-in function '%D'",
911 &DECL_SOURCE_LOCATION (newdecl), newdecl);
912 /* Discard the old built-in function. */
913 return 0;
915 else if (!types_match)
917 /* Accept the return type of the new declaration if same modes. */
918 tree oldreturntype = TREE_TYPE (oldtype);
919 tree newreturntype = TREE_TYPE (newtype);
921 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
923 /* Function types may be shared, so we can't just modify
924 the return type of olddecl's function type. */
925 tree trytype
926 = build_function_type (newreturntype,
927 TYPE_ARG_TYPES (oldtype));
928 trytype = build_type_attribute_variant (trytype,
929 TYPE_ATTRIBUTES (oldtype));
931 types_match = comptypes (newtype, trytype, comptype_flags);
932 if (types_match)
933 oldtype = trytype;
935 /* Accept harmless mismatch in first argument type also.
936 This is for the ffs and fprintf builtins. */
937 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
938 && TYPE_ARG_TYPES (oldtype) != 0
939 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
940 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
941 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
942 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
944 /* Function types may be shared, so we can't just modify
945 the return type of olddecl's function type. */
946 tree trytype
947 = build_function_type (TREE_TYPE (oldtype),
948 tree_cons (NULL_TREE,
949 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
950 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
951 trytype = build_type_attribute_variant (trytype,
952 TYPE_ATTRIBUTES (oldtype));
954 types_match = comptypes (newtype, trytype, comptype_flags);
955 if (types_match)
956 oldtype = trytype;
958 if (! different_binding_level)
959 TREE_TYPE (olddecl) = oldtype;
961 else if (TYPE_ARG_TYPES (oldtype) == NULL
962 && TYPE_ARG_TYPES (newtype) != NULL)
964 /* For bcmp, bzero, fputs the builtin type has arguments not
965 specified. Use the ones from the prototype so that type checking
966 is done for them. */
967 tree trytype
968 = build_function_type (TREE_TYPE (oldtype),
969 TYPE_ARG_TYPES (newtype));
970 trytype = build_type_attribute_variant (trytype,
971 TYPE_ATTRIBUTES (oldtype));
973 oldtype = trytype;
974 if (! different_binding_level)
975 TREE_TYPE (olddecl) = oldtype;
977 if (!types_match)
979 /* If types don't match for a built-in, throw away the built-in. */
980 warning ("%Hconflicting types for built-in function '%D'",
981 &DECL_SOURCE_LOCATION (newdecl), newdecl);
982 return 0;
985 else if (TREE_CODE (olddecl) == FUNCTION_DECL
986 && DECL_SOURCE_LINE (olddecl) == 0)
988 /* A function declaration for a predeclared function
989 that isn't actually built in. */
990 if (!TREE_PUBLIC (newdecl))
992 /* If you declare it as static, the
993 default definition is overridden. */
994 return 0;
996 else if (!types_match)
998 /* If the types don't match, preserve volatility indication.
999 Later on, we will discard everything else about the
1000 default declaration. */
1001 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1004 /* Permit char *foo () to match void *foo (...) if not pedantic,
1005 if one of them came from a system header file. */
1006 else if (!types_match
1007 && TREE_CODE (olddecl) == FUNCTION_DECL
1008 && TREE_CODE (newdecl) == FUNCTION_DECL
1009 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1010 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1011 && (DECL_IN_SYSTEM_HEADER (olddecl)
1012 || DECL_IN_SYSTEM_HEADER (newdecl))
1013 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1014 && TYPE_ARG_TYPES (oldtype) == 0
1015 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1016 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1018 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1019 && TYPE_ARG_TYPES (newtype) == 0
1020 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1021 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1023 if (pedantic)
1024 pedwarn ("%Hconflicting types for '%D'",
1025 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1026 /* Make sure we keep void * as ret type, not char *. */
1027 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1028 TREE_TYPE (newdecl) = newtype = oldtype;
1030 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1031 we will come back here again. */
1032 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1034 /* Permit void foo (...) to match int foo (...) if the latter is the
1035 definition and implicit int was used. See c-torture/compile/920625-2.c. */
1036 else if (!types_match && new_is_definition
1037 && TREE_CODE (olddecl) == FUNCTION_DECL
1038 && TREE_CODE (newdecl) == FUNCTION_DECL
1039 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1040 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1041 && C_FUNCTION_IMPLICIT_INT (newdecl))
1043 pedwarn ("%Hconflicting types for '%D'",
1044 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1045 /* Make sure we keep void as the return type. */
1046 TREE_TYPE (newdecl) = newtype = oldtype;
1047 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1049 else if (!types_match
1050 /* Permit char *foo (int, ...); followed by char *foo ();
1051 if not pedantic. */
1052 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1053 && ! pedantic
1054 /* Return types must still match. */
1055 && comptypes (TREE_TYPE (oldtype),
1056 TREE_TYPE (newtype), comptype_flags)
1057 && TYPE_ARG_TYPES (newtype) == 0))
1059 error ("%Hconflicting types for '%D'",
1060 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1061 /* Check for function type mismatch
1062 involving an empty arglist vs a nonempty one. */
1063 if (TREE_CODE (olddecl) == FUNCTION_DECL
1064 && comptypes (TREE_TYPE (oldtype),
1065 TREE_TYPE (newtype), comptype_flags)
1066 && ((TYPE_ARG_TYPES (oldtype) == 0
1067 && DECL_INITIAL (olddecl) == 0)
1069 (TYPE_ARG_TYPES (newtype) == 0
1070 && DECL_INITIAL (newdecl) == 0)))
1072 /* Classify the problem further. */
1073 tree t = TYPE_ARG_TYPES (oldtype);
1074 if (t == 0)
1075 t = TYPE_ARG_TYPES (newtype);
1076 for (; t; t = TREE_CHAIN (t))
1078 tree type = TREE_VALUE (t);
1080 if (TREE_CHAIN (t) == 0
1081 && TYPE_MAIN_VARIANT (type) != void_type_node)
1083 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1084 break;
1087 if (c_type_promotes_to (type) != type)
1089 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1090 break;
1094 if (C_DECL_IMPLICIT (olddecl))
1095 error ("%Hprevious implicit declaration of '%D'",
1096 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1097 else
1098 error ("%Hprevious declaration of '%D'",
1099 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1101 /* This is safer because the initializer might contain references
1102 to variables that were declared between olddecl and newdecl. This
1103 will make the initializer invalid for olddecl in case it gets
1104 assigned to olddecl below. */
1105 if (TREE_CODE (newdecl) == VAR_DECL)
1106 DECL_INITIAL (newdecl) = 0;
1108 /* TLS cannot follow non-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 ("%Hthread-local declaration of '%D' follows non thread-local "
1113 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1114 error ("%Hprevious declaration of '%D'",
1115 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1117 /* non-TLS declaration cannot follow TLS declaration. */
1118 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1119 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1121 error ("%Hnon thread-local declaration of '%D' follows "
1122 "thread-local declaration",
1123 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1124 error ("%Hprevious declaration of '%D'",
1125 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1127 else
1129 errmsg = redeclaration_error_message (newdecl, olddecl);
1130 if (errmsg)
1132 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1133 switch (errmsg)
1135 case 1:
1136 error ("%Hredefinition of '%D'", locus, newdecl);
1137 break;
1138 case 2:
1139 error ("%Hredeclaration of '%D'", locus, newdecl);
1140 break;
1141 case 3:
1142 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1143 break;
1144 default:
1145 abort ();
1148 locus = &DECL_SOURCE_LOCATION (olddecl);
1149 if (DECL_INITIAL (olddecl)
1150 && current_binding_level == global_binding_level)
1151 error ("%H'%D' previously defined here", locus, olddecl);
1152 else
1153 error ("%H'%D' previously declared here", locus, olddecl);
1154 return 0;
1156 else if (TREE_CODE (newdecl) == TYPE_DECL
1157 && (DECL_IN_SYSTEM_HEADER (olddecl)
1158 || DECL_IN_SYSTEM_HEADER (newdecl)))
1160 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1161 warning ("%Hredefinition of '%D'", locus, newdecl);
1162 locus = &DECL_SOURCE_LOCATION (olddecl);
1163 if (DECL_INITIAL (olddecl)
1164 && current_binding_level == global_binding_level)
1165 warning ("%H'%D' previously defined here", locus, olddecl);
1166 else
1167 warning ("%H'%D' previously declared here", locus, olddecl);
1169 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1170 && DECL_INITIAL (olddecl) != 0
1171 && TYPE_ARG_TYPES (oldtype) == 0
1172 && TYPE_ARG_TYPES (newtype) != 0
1173 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1175 tree type, parm;
1176 int nargs;
1177 /* Prototype decl follows defn w/o prototype. */
1179 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1180 type = TYPE_ARG_TYPES (newtype),
1181 nargs = 1;
1183 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1185 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1186 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1188 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1189 warning ("%Hprototype for '%D' follows", locus, newdecl);
1190 locus = &DECL_SOURCE_LOCATION (olddecl);
1191 warning ("%Hnon-prototype definition here", locus);
1192 break;
1194 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1195 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1197 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1198 error ("%Hprototype for '%D' follows and number of "
1199 "arguments doesn't match", locus, newdecl);
1200 locus = &DECL_SOURCE_LOCATION (olddecl);
1201 error ("%Hnon-prototype definition here", locus);
1202 errmsg = 1;
1203 break;
1205 /* Type for passing arg must be consistent
1206 with that declared for the arg. */
1207 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1208 comptype_flags))
1210 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1211 error ("%Hprototype for '%D' follows and argument %d "
1212 "doesn't match", locus, newdecl, nargs);
1213 locus = &DECL_SOURCE_LOCATION (olddecl);
1214 error ("%Hnon-prototype definition here", locus);
1215 errmsg = 1;
1216 break;
1220 /* Warn about mismatches in various flags. */
1221 else
1223 /* Warn if function is now inline
1224 but was previously declared not inline and has been called. */
1225 if (TREE_CODE (olddecl) == FUNCTION_DECL
1226 && ! DECL_DECLARED_INLINE_P (olddecl)
1227 && DECL_DECLARED_INLINE_P (newdecl)
1228 && TREE_USED (olddecl))
1229 warning ("%H'%D' declared inline after being called",
1230 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1231 if (TREE_CODE (olddecl) == FUNCTION_DECL
1232 && ! DECL_DECLARED_INLINE_P (olddecl)
1233 && DECL_DECLARED_INLINE_P (newdecl)
1234 && DECL_INITIAL (olddecl) != 0)
1235 warning ("%H'%D' declared inline after its definition",
1236 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1238 /* If pedantic, warn when static declaration follows a non-static
1239 declaration. Otherwise, do so only for functions. */
1240 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1241 && TREE_PUBLIC (olddecl)
1242 && !TREE_PUBLIC (newdecl))
1243 warning ("%Hstatic declaration for '%D' follows non-static",
1244 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1246 /* If warn_traditional, warn when a non-static function
1247 declaration follows a static one. */
1248 if (warn_traditional && !in_system_header
1249 && TREE_CODE (olddecl) == FUNCTION_DECL
1250 && !TREE_PUBLIC (olddecl)
1251 && TREE_PUBLIC (newdecl))
1252 warning ("%Hnon-static declaration for '%D' follows static",
1253 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1255 /* Warn when const declaration follows a non-const
1256 declaration, but not for functions. */
1257 if (TREE_CODE (olddecl) != FUNCTION_DECL
1258 && !TREE_READONLY (olddecl)
1259 && TREE_READONLY (newdecl))
1260 warning ("%Hconst declaration for '%D' follows non-const",
1261 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1262 /* These bits are logically part of the type, for variables.
1263 But not for functions
1264 (where qualifiers are not valid ANSI anyway). */
1265 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1266 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1267 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1268 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1269 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1273 /* Optionally warn about more than one declaration for the same name. */
1274 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1275 /* Don't warn about a function declaration
1276 followed by a definition. */
1277 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1278 && DECL_INITIAL (olddecl) == 0)
1279 /* Don't warn about extern decl followed by (tentative) definition. */
1280 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1282 warning ("%Hredundant redeclaration of '%D' in same scope",
1283 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1284 warning ("%Hprevious declaration of '%D'",
1285 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1288 /* Copy all the DECL_... slots specified in the new decl
1289 except for any that we copy here from the old type.
1291 Past this point, we don't change OLDTYPE and NEWTYPE
1292 even if we change the types of NEWDECL and OLDDECL. */
1294 if (types_match)
1296 /* When copying info to olddecl, we store into write_olddecl
1297 instead. This allows us to avoid modifying olddecl when
1298 different_binding_level is true. */
1299 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1301 /* Merge the data types specified in the two decls. */
1302 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1304 if (different_binding_level)
1306 if (TYPE_ARG_TYPES (oldtype) != 0
1307 && TYPE_ARG_TYPES (newtype) == 0)
1308 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1309 else
1310 TREE_TYPE (newdecl)
1311 = build_type_attribute_variant
1312 (newtype,
1313 merge_attributes (TYPE_ATTRIBUTES (newtype),
1314 TYPE_ATTRIBUTES (oldtype)));
1316 else
1317 TREE_TYPE (newdecl)
1318 = TREE_TYPE (olddecl)
1319 = common_type (newtype, oldtype);
1322 /* Lay the type out, unless already done. */
1323 if (oldtype != TREE_TYPE (newdecl))
1325 if (TREE_TYPE (newdecl) != error_mark_node)
1326 layout_type (TREE_TYPE (newdecl));
1327 if (TREE_CODE (newdecl) != FUNCTION_DECL
1328 && TREE_CODE (newdecl) != TYPE_DECL
1329 && TREE_CODE (newdecl) != CONST_DECL)
1330 layout_decl (newdecl, 0);
1332 else
1334 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1335 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1336 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1337 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1338 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1339 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1341 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1342 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1346 /* Keep the old rtl since we can safely use it. */
1347 COPY_DECL_RTL (olddecl, newdecl);
1349 /* Merge the type qualifiers. */
1350 if (TREE_READONLY (newdecl))
1351 TREE_READONLY (write_olddecl) = 1;
1353 if (TREE_THIS_VOLATILE (newdecl))
1355 TREE_THIS_VOLATILE (write_olddecl) = 1;
1356 if (TREE_CODE (newdecl) == VAR_DECL
1357 /* If an automatic variable is re-declared in the same
1358 function scope, but the old declaration was not
1359 volatile, make_var_volatile() would crash because the
1360 variable would have been assigned to a pseudo, not a
1361 MEM. Since this duplicate declaration is invalid
1362 anyway, we just skip the call. */
1363 && errmsg == 0)
1364 make_var_volatile (newdecl);
1367 /* Keep source location of definition rather than declaration. */
1368 /* When called with different_binding_level set, keep the old
1369 information so that meaningful diagnostics can be given. */
1370 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1371 && ! different_binding_level)
1373 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1374 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1377 /* Merge the unused-warning information. */
1378 if (DECL_IN_SYSTEM_HEADER (olddecl))
1379 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1380 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1381 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1383 /* Merge the initialization information. */
1384 /* When called with different_binding_level set, don't copy over
1385 DECL_INITIAL, so that we don't accidentally change function
1386 declarations into function definitions. */
1387 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1388 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1390 /* Merge the section attribute.
1391 We want to issue an error if the sections conflict but that must be
1392 done later in decl_attributes since we are called before attributes
1393 are assigned. */
1394 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1395 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1397 /* Copy the assembler name.
1398 Currently, it can only be defined in the prototype. */
1399 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1401 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1403 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1404 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1405 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1406 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1407 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1408 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1409 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1410 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1411 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1414 /* If cannot merge, then use the new type and qualifiers,
1415 and don't preserve the old rtl. */
1416 else if (! different_binding_level)
1418 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1419 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1420 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1421 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1424 /* Merge the storage class information. */
1425 merge_weak (newdecl, olddecl);
1427 /* For functions, static overrides non-static. */
1428 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1430 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1431 /* This is since we don't automatically
1432 copy the attributes of NEWDECL into OLDDECL. */
1433 /* No need to worry about different_binding_level here because
1434 then TREE_PUBLIC (newdecl) was true. */
1435 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1436 /* If this clears `static', clear it in the identifier too. */
1437 if (! TREE_PUBLIC (olddecl))
1438 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1440 if (DECL_EXTERNAL (newdecl))
1442 if (! different_binding_level || different_tu)
1444 /* Don't mess with these flags on local externs; they remain
1445 external even if there's a declaration at file scope which
1446 isn't. */
1447 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1448 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1450 /* An extern decl does not override previous storage class. */
1451 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1452 if (! DECL_EXTERNAL (newdecl))
1454 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1455 /* If we have two non-EXTERNAL file-scope decls that are
1456 the same, only one of them should be written out. */
1457 if (different_tu)
1458 TREE_ASM_WRITTEN (newdecl) = 1;
1461 else
1463 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1464 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1467 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1469 /* If we're redefining a function previously defined as extern
1470 inline, make sure we emit debug info for the inline before we
1471 throw it away, in case it was inlined into a function that hasn't
1472 been written out yet. */
1473 if (new_is_definition && DECL_INITIAL (olddecl))
1475 if (TREE_USED (olddecl))
1476 (*debug_hooks->outlining_inline_function) (olddecl);
1478 /* The new defn must not be inline. */
1479 DECL_INLINE (newdecl) = 0;
1480 DECL_UNINLINABLE (newdecl) = 1;
1482 else
1484 /* If either decl says `inline', this fn is inline,
1485 unless its definition was passed already. */
1486 if (DECL_DECLARED_INLINE_P (newdecl)
1487 || DECL_DECLARED_INLINE_P (olddecl))
1488 DECL_DECLARED_INLINE_P (newdecl) = 1;
1490 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1491 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1494 if (DECL_BUILT_IN (olddecl))
1496 /* Get rid of any built-in function if new arg types don't match it
1497 or if we have a function definition. */
1498 if (! types_match || new_is_definition)
1500 if (! different_binding_level)
1502 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1503 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1506 else
1508 /* If redeclaring a builtin function, and not a definition,
1509 it stays built in. */
1510 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1511 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1515 /* Also preserve various other info from the definition. */
1516 if (! new_is_definition)
1518 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1519 /* When called with different_binding_level set, don't copy over
1520 DECL_INITIAL, so that we don't accidentally change function
1521 declarations into function definitions. */
1522 if (! different_binding_level)
1523 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1524 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1525 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1526 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1527 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1529 /* Set DECL_INLINE on the declaration if we've got a body
1530 from which to instantiate. */
1531 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1533 DECL_INLINE (newdecl) = 1;
1534 DECL_ABSTRACT_ORIGIN (newdecl)
1535 = (different_binding_level
1536 ? DECL_ORIGIN (olddecl)
1537 : DECL_ABSTRACT_ORIGIN (olddecl));
1540 else
1542 /* If a previous declaration said inline, mark the
1543 definition as inlinable. */
1544 if (DECL_DECLARED_INLINE_P (newdecl)
1545 && ! DECL_UNINLINABLE (newdecl))
1546 DECL_INLINE (newdecl) = 1;
1549 if (different_binding_level)
1550 return 0;
1552 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1553 But preserve OLDDECL's DECL_UID. */
1555 unsigned olddecl_uid = DECL_UID (olddecl);
1557 memcpy ((char *) olddecl + sizeof (struct tree_common),
1558 (char *) newdecl + sizeof (struct tree_common),
1559 sizeof (struct tree_decl) - sizeof (struct tree_common));
1560 DECL_UID (olddecl) = olddecl_uid;
1563 /* NEWDECL contains the merged attribute lists.
1564 Update OLDDECL to be the same. */
1565 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1567 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1568 so that encode_section_info has a chance to look at the new decl
1569 flags and attributes. */
1570 if (DECL_RTL_SET_P (olddecl)
1571 && (TREE_CODE (olddecl) == FUNCTION_DECL
1572 || (TREE_CODE (olddecl) == VAR_DECL
1573 && TREE_STATIC (olddecl))))
1574 make_decl_rtl (olddecl, NULL);
1576 return 1;
1579 /* Return any external DECL associated with ID, whether or not it is
1580 currently in scope. */
1582 static tree
1583 any_external_decl (tree id)
1585 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1586 tree t;
1588 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1589 return 0;
1590 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1591 return decl;
1593 t = purpose_member (id, truly_local_externals);
1594 if (t)
1595 return TREE_VALUE (t);
1597 return 0;
1600 /* Record an external decl DECL. This only does something if a
1601 shadowing decl already exists. */
1602 static void
1603 record_external_decl (tree decl)
1605 tree name = DECL_NAME (decl);
1606 if (!IDENTIFIER_SYMBOL_VALUE (name))
1607 return;
1609 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1612 /* Check whether decl-node X shadows an existing declaration.
1613 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1614 which might be a NULL_TREE. */
1615 static void
1616 warn_if_shadowing (tree x, tree old)
1618 const char *name;
1620 /* Nothing to shadow? */
1621 if (old == 0
1622 /* Shadow warnings not wanted? */
1623 || !warn_shadow
1624 /* No shadow warnings for internally generated vars. */
1625 || DECL_SOURCE_LINE (x) == 0
1626 /* No shadow warnings for vars made for inlining. */
1627 || DECL_FROM_INLINE (x)
1628 /* Don't warn about the parm names in function declarator
1629 within a function declarator.
1630 It would be nice to avoid warning in any function
1631 declarator in a declaration, as opposed to a definition,
1632 but there is no way to tell it's not a definition. */
1633 || (TREE_CODE (x) == PARM_DECL
1634 && current_binding_level->level_chain->parm_flag))
1635 return;
1637 name = IDENTIFIER_POINTER (DECL_NAME (x));
1639 if (TREE_CODE (old) == PARM_DECL)
1640 shadow_warning (SW_PARAM, name, old);
1641 else if (C_DECL_FILE_SCOPE (old))
1642 shadow_warning (SW_GLOBAL, name, old);
1643 else
1644 shadow_warning (SW_LOCAL, name, old);
1648 /* Subroutine of pushdecl.
1650 X is a TYPE_DECL for a typedef statement. Create a brand new
1651 ..._TYPE node (which will be just a variant of the existing
1652 ..._TYPE node with identical properties) and then install X
1653 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1655 The whole point here is to end up with a situation where each
1656 and every ..._TYPE node the compiler creates will be uniquely
1657 associated with AT MOST one node representing a typedef name.
1658 This way, even though the compiler substitutes corresponding
1659 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1660 early on, later parts of the compiler can always do the reverse
1661 translation and get back the corresponding typedef name. For
1662 example, given:
1664 typedef struct S MY_TYPE;
1665 MY_TYPE object;
1667 Later parts of the compiler might only know that `object' was of
1668 type `struct S' if it were not for code just below. With this
1669 code however, later parts of the compiler see something like:
1671 struct S' == struct S
1672 typedef struct S' MY_TYPE;
1673 struct S' object;
1675 And they can then deduce (from the node for type struct S') that
1676 the original object declaration was:
1678 MY_TYPE object;
1680 Being able to do this is important for proper support of protoize,
1681 and also for generating precise symbolic debugging information
1682 which takes full account of the programmer's (typedef) vocabulary.
1684 Obviously, we don't want to generate a duplicate ..._TYPE node if
1685 the TYPE_DECL node that we are now processing really represents a
1686 standard built-in type.
1688 Since all standard types are effectively declared at line zero
1689 in the source file, we can easily check to see if we are working
1690 on a standard type by checking the current value of lineno. */
1692 static void
1693 clone_underlying_type (tree x)
1695 if (DECL_SOURCE_LINE (x) == 0)
1697 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1698 TYPE_NAME (TREE_TYPE (x)) = x;
1700 else if (TREE_TYPE (x) != error_mark_node
1701 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1703 tree tt = TREE_TYPE (x);
1704 DECL_ORIGINAL_TYPE (x) = tt;
1705 tt = build_type_copy (tt);
1706 TYPE_NAME (tt) = x;
1707 TREE_USED (tt) = TREE_USED (x);
1708 TREE_TYPE (x) = tt;
1712 /* Record a decl-node X as belonging to the current lexical scope.
1713 Check for errors (such as an incompatible declaration for the same
1714 name already seen in the same scope).
1716 Returns either X or an old decl for the same name.
1717 If an old decl is returned, it may have been smashed
1718 to agree with what X says. */
1720 tree
1721 pushdecl (tree x)
1723 tree name = DECL_NAME (x);
1724 struct binding_level *scope = current_binding_level;
1726 #ifdef ENABLE_CHECKING
1727 if (error_mark_node == 0)
1728 /* Called too early. */
1729 abort ();
1730 #endif
1732 /* Functions need the lang_decl data. */
1733 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1734 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1736 /* A local extern declaration for a function doesn't constitute nesting.
1737 A local auto declaration does, since it's a forward decl
1738 for a nested function coming later. */
1739 if (current_function_decl == NULL
1740 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1741 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1742 DECL_CONTEXT (x) = current_file_decl;
1743 else
1744 DECL_CONTEXT (x) = current_function_decl;
1746 if (name)
1748 tree old;
1750 if (warn_nested_externs
1751 && scope != global_binding_level
1752 && DECL_EXTERNAL (x)
1753 && !DECL_IN_SYSTEM_HEADER (x))
1754 warning ("nested extern declaration of `%s'",
1755 IDENTIFIER_POINTER (name));
1757 old = lookup_name_current_level (name);
1758 if (old && duplicate_decls (x, old, 0, false))
1759 return old;
1760 if (DECL_EXTERNAL (x) || scope == global_binding_level)
1762 /* Find and check against a previous, not-in-scope, external
1763 decl for this identifier. (C99 s???: If two declarations
1764 with external linkage, referring to the same object, have
1765 incompatible types, the behavior is undefined). */
1766 tree ext = any_external_decl (name);
1767 if (ext)
1769 if (duplicate_decls (x, ext, scope != global_binding_level,
1770 false))
1771 x = copy_node (ext);
1773 else
1774 record_external_decl (x);
1777 if (TREE_CODE (x) == TYPE_DECL)
1778 clone_underlying_type (x);
1780 /* If storing a local value, there may already be one
1781 (inherited). If so, record it for restoration when this
1782 binding level ends. Take care not to do this if we are
1783 replacing an older decl in the same binding level (i.e.
1784 duplicate_decls returned false, above). */
1785 if (scope != global_binding_level
1786 && IDENTIFIER_SYMBOL_VALUE (name)
1787 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1789 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1790 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1791 scope->shadowed);
1794 /* Install the new declaration in the requested binding level. */
1795 IDENTIFIER_SYMBOL_VALUE (name) = x;
1796 C_DECL_INVISIBLE (x) = 0;
1798 /* Keep list of variables in this level with incomplete type.
1799 If the input is erroneous, we can have error_mark in the type
1800 slot (e.g. "f(void a, ...)") - that doesn't count as an
1801 incomplete type. */
1802 if (TREE_TYPE (x) != error_mark_node
1803 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1805 tree element = TREE_TYPE (x);
1807 while (TREE_CODE (element) == ARRAY_TYPE)
1808 element = TREE_TYPE (element);
1809 if (TREE_CODE (element) == RECORD_TYPE
1810 || TREE_CODE (element) == UNION_TYPE)
1811 scope->incomplete_list = tree_cons (NULL_TREE, x,
1812 scope->incomplete_list);
1816 /* Put decls on list in reverse order.
1817 We will reverse them later if necessary. */
1818 TREE_CHAIN (x) = scope->names;
1819 scope->names = x;
1821 return x;
1824 /* Record X as belonging to the global scope (C99 "file scope").
1825 This is used only internally by the Objective-C front end,
1826 and is limited to its needs. It will hork if there is _any_
1827 visible binding for X (not just a global one). */
1828 tree
1829 pushdecl_top_level (tree x)
1831 tree name, old;
1833 if (TREE_CODE (x) != VAR_DECL)
1834 abort ();
1836 name = DECL_NAME (x);
1837 old = IDENTIFIER_SYMBOL_VALUE (name);
1839 if (old)
1841 if (DECL_CONTEXT (old))
1842 abort ();
1844 if (!duplicate_decls (x, old, 0, false))
1845 abort ();
1847 return old;
1850 DECL_CONTEXT (x) = current_file_decl;
1851 IDENTIFIER_SYMBOL_VALUE (name) = x;
1852 TREE_CHAIN (x) = global_binding_level->names;
1853 global_binding_level->names = x;
1854 return x;
1857 /* Record X as belonging to the outermost scope of the current
1858 function. This is used only internally, by c_make_fname_decl and
1859 undeclared_variable, and is limited to their needs. The NAME is
1860 provided as a separate argument because undeclared_variable wants to
1861 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1862 called; if there is any preexisting decl for this identifier, it is
1863 an ICE. */
1864 static void
1865 pushdecl_function_level (tree x, tree name)
1867 struct binding_level *scope;
1869 scope = current_binding_level;
1870 while (scope->function_body == 0)
1871 scope = scope->level_chain;
1872 if (!scope)
1873 abort ();
1875 if (x == error_mark_node)
1876 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1877 scope->shadowed);
1878 else if (TREE_CODE (x) == VAR_DECL)
1880 if (name != DECL_NAME (x))
1881 abort ();
1882 if (IDENTIFIER_SYMBOL_VALUE (name))
1883 abort ();
1885 DECL_CONTEXT (x) = current_function_decl;
1886 TREE_CHAIN (x) = scope->names;
1887 scope->names = x;
1890 IDENTIFIER_SYMBOL_VALUE (name) = x;
1893 /* Generate an implicit declaration for identifier FUNCTIONID as a
1894 function of type int (). */
1896 tree
1897 implicitly_declare (tree functionid)
1899 tree decl = any_external_decl (functionid);
1901 if (decl && decl != error_mark_node)
1903 /* Implicit declaration of a function already declared
1904 (somehow) in a different scope, or as a built-in.
1905 If this is the first time this has happened, warn;
1906 then recycle the old declaration. */
1907 if (!C_DECL_IMPLICIT (decl))
1909 implicit_decl_warning (DECL_NAME (decl));
1910 if (! C_DECL_FILE_SCOPE (decl))
1911 warning ("%Hprevious declaration of '%D'",
1912 &DECL_SOURCE_LOCATION (decl), decl);
1913 C_DECL_IMPLICIT (decl) = 1;
1915 /* If this function is global, then it must already be in the
1916 global binding level, so there's no need to push it again. */
1917 if (current_binding_level == global_binding_level)
1918 return decl;
1919 /* If this is a local declaration, make a copy; we can't have
1920 the same DECL listed in two different binding levels. */
1921 return pushdecl (copy_node (decl));
1924 /* Not seen before. */
1925 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1926 DECL_EXTERNAL (decl) = 1;
1927 TREE_PUBLIC (decl) = 1;
1928 C_DECL_IMPLICIT (decl) = 1;
1929 implicit_decl_warning (functionid);
1931 /* ANSI standard says implicit declarations are in the innermost block.
1932 So we record the decl in the standard fashion. */
1933 decl = pushdecl (decl);
1935 /* No need to call objc_check_decl here - it's a function type. */
1936 rest_of_decl_compilation (decl, NULL, 0, 0);
1938 /* Write a record describing this implicit function declaration to the
1939 prototypes file (if requested). */
1941 gen_aux_info_record (decl, 0, 1, 0);
1943 /* Possibly apply some default attributes to this implicit declaration. */
1944 decl_attributes (&decl, NULL_TREE, 0);
1946 return decl;
1949 static void
1950 implicit_decl_warning (tree id)
1952 const char *name = IDENTIFIER_POINTER (id);
1953 if (mesg_implicit_function_declaration == 2)
1954 error ("implicit declaration of function `%s'", name);
1955 else if (mesg_implicit_function_declaration == 1)
1956 warning ("implicit declaration of function `%s'", name);
1959 /* Return zero if the declaration NEWDECL is valid
1960 when the declaration OLDDECL (assumed to be for the same name)
1961 has already been seen.
1962 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1963 and 3 if it is a conflicting declaration. */
1965 static int
1966 redeclaration_error_message (tree newdecl, tree olddecl)
1968 if (TREE_CODE (newdecl) == TYPE_DECL)
1970 /* Do not complain about type redeclarations where at least one
1971 declaration was in a system header. */
1972 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1973 return 0;
1974 return 1;
1976 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1978 /* Declarations of functions can insist on internal linkage
1979 but they can't be inconsistent with internal linkage,
1980 so there can be no error on that account.
1981 However defining the same name twice is no good. */
1982 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1983 /* However, defining once as extern inline and a second
1984 time in another way is ok. */
1985 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1986 && ! (DECL_DECLARED_INLINE_P (newdecl)
1987 && DECL_EXTERNAL (newdecl))))
1988 return 1;
1989 return 0;
1991 else if (C_DECL_FILE_SCOPE (newdecl))
1993 /* Objects declared at top level: */
1994 /* If at least one is a reference, it's ok. */
1995 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1996 return 0;
1997 /* Reject two definitions. */
1998 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1999 return 1;
2000 /* Now we have two tentative defs, or one tentative and one real def. */
2001 /* Insist that the linkage match. */
2002 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
2003 return 3;
2004 return 0;
2006 else if (current_binding_level->parm_flag
2007 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2008 return 0;
2009 else
2011 /* Newdecl has block scope. If olddecl has block scope also, then
2012 reject two definitions, and reject a definition together with an
2013 external reference. Otherwise, it is OK, because newdecl must
2014 be an extern reference to olddecl. */
2015 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
2016 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
2017 return 2;
2018 return 0;
2022 /* Issue an error message for a reference to an undeclared variable
2023 ID, including a reference to a builtin outside of function-call
2024 context. Establish a binding of the identifier to error_mark_node
2025 in an appropriate scope, which will suppress further errors for the
2026 same identifier. */
2027 void
2028 undeclared_variable (tree id)
2030 static bool already = false;
2032 if (current_function_decl == 0)
2034 error ("`%s' undeclared here (not in a function)",
2035 IDENTIFIER_POINTER (id));
2036 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
2038 else
2040 error ("`%s' undeclared (first use in this function)",
2041 IDENTIFIER_POINTER (id));
2043 if (! already)
2045 error ("(Each undeclared identifier is reported only once");
2046 error ("for each function it appears in.)");
2047 already = true;
2050 pushdecl_function_level (error_mark_node, id);
2054 /* Get the LABEL_DECL corresponding to identifier ID as a label.
2055 Create one if none exists so far for the current function.
2056 This function is called for both label definitions and label references. */
2058 tree
2059 lookup_label (tree id)
2061 tree decl = IDENTIFIER_LABEL_VALUE (id);
2063 if (current_function_decl == 0)
2065 error ("label %s referenced outside of any function",
2066 IDENTIFIER_POINTER (id));
2067 return 0;
2070 /* Use a label already defined or ref'd with this name. */
2071 if (decl != 0)
2073 /* But not if it is inherited and wasn't declared to be inheritable. */
2074 if (DECL_CONTEXT (decl) != current_function_decl
2075 && ! C_DECLARED_LABEL_FLAG (decl))
2076 return shadow_label (id);
2077 return decl;
2080 decl = build_decl (LABEL_DECL, id, void_type_node);
2082 /* A label not explicitly declared must be local to where it's ref'd. */
2083 DECL_CONTEXT (decl) = current_function_decl;
2085 DECL_MODE (decl) = VOIDmode;
2087 /* Say where one reference is to the label,
2088 for the sake of the error if it is not defined. */
2089 DECL_SOURCE_LOCATION (decl) = input_location;
2091 IDENTIFIER_LABEL_VALUE (id) = decl;
2093 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2095 return decl;
2098 /* Make a label named NAME in the current function,
2099 shadowing silently any that may be inherited from containing functions
2100 or containing scopes.
2102 Note that valid use, if the label being shadowed
2103 comes from another scope in the same function,
2104 requires calling declare_nonlocal_label right away. */
2106 tree
2107 shadow_label (tree name)
2109 tree decl = IDENTIFIER_LABEL_VALUE (name);
2111 if (decl != 0)
2113 tree dup;
2115 /* Check to make sure that the label hasn't already been declared
2116 at this label scope */
2117 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2118 if (TREE_VALUE (dup) == decl)
2120 error ("duplicate label declaration '%E'", name);
2121 error ("%Hthis is a previous declaration",
2122 &DECL_SOURCE_LOCATION (TREE_VALUE (dup)));
2123 /* Just use the previous declaration. */
2124 return lookup_label (name);
2127 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2128 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2131 return lookup_label (name);
2134 /* Define a label, specifying the location in the source file.
2135 Return the LABEL_DECL node for the label, if the definition is valid.
2136 Otherwise return 0. */
2138 tree
2139 define_label (location_t location, tree name)
2141 tree decl = lookup_label (name);
2143 /* If label with this name is known from an outer context, shadow it. */
2144 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2146 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2147 IDENTIFIER_LABEL_VALUE (name) = 0;
2148 decl = lookup_label (name);
2151 if (warn_traditional && !in_system_header && lookup_name (name))
2152 warning ("%Htraditional C lacks a separate namespace for labels, "
2153 "identifier `%s' conflicts", &location, IDENTIFIER_POINTER (name));
2155 if (DECL_INITIAL (decl) != 0)
2157 error ("%Hduplicate label `%s'", &location, IDENTIFIER_POINTER (name));
2158 return 0;
2160 else
2162 /* Mark label as having been defined. */
2163 DECL_INITIAL (decl) = error_mark_node;
2164 /* Say where in the source. */
2165 DECL_SOURCE_LOCATION (decl) = location;
2166 return decl;
2170 /* Return the list of declarations of the current level.
2171 Note that this list is in reverse order unless/until
2172 you nreverse it; and when you do nreverse it, you must
2173 store the result back using `storedecls' or you will lose. */
2175 tree
2176 getdecls (void)
2178 return current_binding_level->names;
2181 /* Return the list of type-tags (for structs, etc) of the current level. */
2183 tree
2184 gettags (void)
2186 return current_binding_level->tags;
2189 /* Store the list of declarations of the current level.
2190 This is done for the parameter declarations of a function being defined,
2191 after they are modified in the light of any missing parameters. */
2193 static void
2194 storedecls (tree decls)
2196 current_binding_level->names = decls;
2199 /* Similarly, store the list of tags of the current level. */
2201 static void
2202 storetags (tree tags)
2204 current_binding_level->tags = tags;
2207 /* Given NAME, an IDENTIFIER_NODE,
2208 return the structure (or union or enum) definition for that name.
2209 If THISLEVEL_ONLY is nonzero, searches only the current_binding_level.
2210 CODE says which kind of type the caller wants;
2211 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2212 If the wrong kind of type is found, an error is reported. */
2214 static tree
2215 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2217 tree tag = IDENTIFIER_TAG_VALUE (name);
2218 int thislevel = 0;
2220 if (!tag)
2221 return 0;
2223 /* We only care about whether it's in this level if
2224 thislevel_only was set or it might be a type clash. */
2225 if (thislevel_only || TREE_CODE (tag) != code)
2227 if (current_binding_level == global_binding_level
2228 || purpose_member (name, current_binding_level->tags))
2229 thislevel = 1;
2232 if (thislevel_only && !thislevel)
2233 return 0;
2235 if (TREE_CODE (tag) != code)
2237 /* Definition isn't the kind we were looking for. */
2238 pending_invalid_xref = name;
2239 pending_invalid_xref_location = input_location;
2241 /* If in the same binding level as a declaration as a tag
2242 of a different type, this must not be allowed to
2243 shadow that tag, so give the error immediately.
2244 (For example, "struct foo; union foo;" is invalid.) */
2245 if (thislevel)
2246 pending_xref_error ();
2248 return tag;
2251 /* Print an error message now
2252 for a recent invalid struct, union or enum cross reference.
2253 We don't print them immediately because they are not invalid
2254 when used in the `struct foo;' construct for shadowing. */
2256 void
2257 pending_xref_error (void)
2259 if (pending_invalid_xref != 0)
2260 error ("%H`%s' defined as wrong kind of tag",
2261 &pending_invalid_xref_location,
2262 IDENTIFIER_POINTER (pending_invalid_xref));
2263 pending_invalid_xref = 0;
2267 /* Look up NAME in the current binding level and its superiors
2268 in the namespace of variables, functions and typedefs.
2269 Return a ..._DECL node of some kind representing its definition,
2270 or return 0 if it is undefined. */
2272 tree
2273 lookup_name (tree name)
2275 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2276 if (decl == 0 || decl == error_mark_node)
2277 return decl;
2278 if (C_DECL_INVISIBLE (decl))
2279 return 0;
2280 return decl;
2283 /* Similar to `lookup_name' but look only at the current binding level. */
2285 static tree
2286 lookup_name_current_level (tree name)
2288 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2290 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2291 return 0;
2293 if (current_binding_level == global_binding_level)
2294 return decl;
2296 /* Scan the current scope for a decl with name NAME. */
2297 if (chain_member (decl, current_binding_level->names))
2298 return decl;
2300 return 0;
2303 /* Create the predefined scalar types of C,
2304 and some nodes representing standard constants (0, 1, (void *) 0).
2305 Initialize the global binding level.
2306 Make definitions for built-in primitive functions. */
2308 void
2309 c_init_decl_processing (void)
2311 tree endlink;
2312 tree ptr_ftype_void, ptr_ftype_ptr;
2313 location_t save_loc = input_location;
2315 /* Adds some ggc roots, and reserved words for c-parse.in. */
2316 c_parse_init ();
2318 current_function_decl = NULL;
2319 named_labels = NULL;
2320 current_binding_level = NULL_BINDING_LEVEL;
2321 free_binding_level = NULL_BINDING_LEVEL;
2323 /* Make the binding_level structure for global names. */
2324 pushlevel (0);
2325 global_binding_level = current_binding_level;
2326 /* Declarations from c_common_nodes_and_builtins must not be associated
2327 with this input file, lest we get differences between using and not
2328 using preprocessed headers. */
2329 input_location.file = "<internal>";
2330 input_location.line = 0;
2332 /* Make the DECL for the toplevel file scope. */
2333 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2335 build_common_tree_nodes (flag_signed_char);
2337 c_common_nodes_and_builtins ();
2339 boolean_type_node = integer_type_node;
2340 boolean_true_node = integer_one_node;
2341 boolean_false_node = integer_zero_node;
2343 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2344 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2345 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2346 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2347 TYPE_PRECISION (c_bool_type_node) = 1;
2348 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2349 c_bool_type_node));
2350 c_bool_false_node = build_int_2 (0, 0);
2351 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2352 c_bool_true_node = build_int_2 (1, 0);
2353 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2355 endlink = void_list_node;
2356 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2357 ptr_ftype_ptr
2358 = build_function_type (ptr_type_node,
2359 tree_cons (NULL_TREE, ptr_type_node, endlink));
2361 input_location = save_loc;
2363 pedantic_lvalues = pedantic;
2365 make_fname_decl = c_make_fname_decl;
2366 start_fname_decls ();
2368 builtin_decls = global_binding_level->names;
2371 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2372 decl, NAME is the initialization string and TYPE_DEP indicates whether
2373 NAME depended on the type of the function. As we don't yet implement
2374 delayed emission of static data, we mark the decl as emitted
2375 so it is not placed in the output. Anything using it must therefore pull
2376 out the STRING_CST initializer directly. This does mean that these names
2377 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2379 static tree
2380 c_make_fname_decl (tree id, int type_dep)
2382 const char *name = fname_as_string (type_dep);
2383 tree decl, type, init;
2384 size_t length = strlen (name);
2386 type = build_array_type
2387 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2388 build_index_type (size_int (length)));
2390 decl = build_decl (VAR_DECL, id, type);
2392 TREE_STATIC (decl) = 1;
2393 TREE_READONLY (decl) = 1;
2394 DECL_ARTIFICIAL (decl) = 1;
2396 init = build_string (length + 1, name);
2397 TREE_TYPE (init) = type;
2398 DECL_INITIAL (decl) = init;
2400 TREE_USED (decl) = 1;
2402 if (current_function_decl)
2403 pushdecl_function_level (decl, DECL_NAME (decl));
2405 finish_decl (decl, init, NULL_TREE);
2407 return decl;
2410 /* Return a definition for a builtin function named NAME and whose data type
2411 is TYPE. TYPE should be a function type with argument types.
2412 FUNCTION_CODE tells later passes how to compile calls to this function.
2413 See tree.h for its possible values.
2415 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2416 the name to be called if we can't opencode the function. If
2417 ATTRS is nonzero, use that for the function's attribute list. */
2419 tree
2420 builtin_function (const char *name, tree type, int function_code,
2421 enum built_in_class class, const char *library_name,
2422 tree attrs)
2424 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2425 DECL_EXTERNAL (decl) = 1;
2426 TREE_PUBLIC (decl) = 1;
2427 if (library_name)
2428 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2429 make_decl_rtl (decl, NULL);
2430 pushdecl (decl);
2431 DECL_BUILT_IN_CLASS (decl) = class;
2432 DECL_FUNCTION_CODE (decl) = function_code;
2434 /* Warn if a function in the namespace for users
2435 is used without an occasion to consider it declared. */
2436 if (name[0] != '_' || name[1] != '_')
2437 C_DECL_INVISIBLE (decl) = 1;
2439 /* Possibly apply some default attributes to this built-in function. */
2440 if (attrs)
2441 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2442 else
2443 decl_attributes (&decl, NULL_TREE, 0);
2445 return decl;
2448 /* Apply default attributes to a function, if a system function with default
2449 attributes. */
2451 void
2452 c_insert_default_attributes (tree decl)
2454 if (!TREE_PUBLIC (decl))
2455 return;
2456 c_common_insert_default_attributes (decl);
2459 /* Called when a declaration is seen that contains no names to declare.
2460 If its type is a reference to a structure, union or enum inherited
2461 from a containing scope, shadow that tag name for the current scope
2462 with a forward reference.
2463 If its type defines a new named structure or union
2464 or defines an enum, it is valid but we need not do anything here.
2465 Otherwise, it is an error. */
2467 void
2468 shadow_tag (tree declspecs)
2470 shadow_tag_warned (declspecs, 0);
2473 void
2474 shadow_tag_warned (tree declspecs, int warned)
2477 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2478 no pedwarn. */
2480 int found_tag = 0;
2481 tree link;
2482 tree specs, attrs;
2484 pending_invalid_xref = 0;
2486 /* Remove the attributes from declspecs, since they will confuse the
2487 following code. */
2488 split_specs_attrs (declspecs, &specs, &attrs);
2490 for (link = specs; link; link = TREE_CHAIN (link))
2492 tree value = TREE_VALUE (link);
2493 enum tree_code code = TREE_CODE (value);
2495 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2496 /* Used to test also that TYPE_SIZE (value) != 0.
2497 That caused warning for `struct foo;' at top level in the file. */
2499 tree name = TYPE_NAME (value);
2500 tree t;
2502 found_tag++;
2504 if (name == 0)
2506 if (warned != 1 && code != ENUMERAL_TYPE)
2507 /* Empty unnamed enum OK */
2509 pedwarn ("unnamed struct/union that defines no instances");
2510 warned = 1;
2513 else
2515 t = lookup_tag (code, name, 1);
2517 if (t == 0)
2519 t = make_node (code);
2520 pushtag (name, t);
2524 else
2526 if (!warned && ! in_system_header)
2528 warning ("useless keyword or type name in empty declaration");
2529 warned = 2;
2534 if (found_tag > 1)
2535 error ("two types specified in one empty declaration");
2537 if (warned != 1)
2539 if (found_tag == 0)
2540 pedwarn ("empty declaration");
2544 /* Construct an array declarator. EXPR is the expression inside [], or
2545 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2546 to the pointer to which a parameter array is converted). STATIC_P is
2547 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2548 is nonzero is the array is [*], a VLA of unspecified length which is
2549 nevertheless a complete type (not currently implemented by GCC),
2550 zero otherwise. The declarator is constructed as an ARRAY_REF
2551 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2552 left of the [] (filled by in set_array_declarator_type) and operand 1
2553 is the expression inside; whose TREE_TYPE is the type qualifiers and
2554 which has TREE_STATIC set if "static" is used. */
2556 tree
2557 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2559 tree decl;
2560 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2561 TREE_TYPE (decl) = quals;
2562 TREE_STATIC (decl) = (static_p ? 1 : 0);
2563 if (pedantic && !flag_isoc99)
2565 if (static_p || quals != NULL_TREE)
2566 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2567 if (vla_unspec_p)
2568 pedwarn ("ISO C90 does not support `[*]' array declarators");
2570 if (vla_unspec_p)
2571 warning ("GCC does not yet properly implement `[*]' array declarators");
2572 return decl;
2575 /* Set the type of an array declarator. DECL is the declarator, as
2576 constructed by build_array_declarator; TYPE is what appears on the left
2577 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2578 abstract declarator, zero otherwise; this is used to reject static and
2579 type qualifiers in abstract declarators, where they are not in the
2580 C99 grammar. */
2582 tree
2583 set_array_declarator_type (tree decl, tree type, int abstract_p)
2585 TREE_OPERAND (decl, 0) = type;
2586 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2587 error ("static or type qualifiers in abstract declarator");
2588 return decl;
2591 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2593 tree
2594 groktypename (tree typename)
2596 tree specs, attrs;
2598 if (TREE_CODE (typename) != TREE_LIST)
2599 return typename;
2601 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2603 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2605 /* Apply attributes. */
2606 decl_attributes (&typename, attrs, 0);
2608 return typename;
2611 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2613 tree
2614 groktypename_in_parm_context (tree typename)
2616 if (TREE_CODE (typename) != TREE_LIST)
2617 return typename;
2618 return grokdeclarator (TREE_VALUE (typename),
2619 TREE_PURPOSE (typename),
2620 PARM, 0);
2623 /* Decode a declarator in an ordinary declaration or data definition.
2624 This is called as soon as the type information and variable name
2625 have been parsed, before parsing the initializer if any.
2626 Here we create the ..._DECL node, fill in its type,
2627 and put it on the list of decls for the current context.
2628 The ..._DECL node is returned as the value.
2630 Exception: for arrays where the length is not specified,
2631 the type is left null, to be filled in by `finish_decl'.
2633 Function definitions do not come here; they go to start_function
2634 instead. However, external and forward declarations of functions
2635 do go through here. Structure field declarations are done by
2636 grokfield and not through here. */
2638 tree
2639 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2641 tree decl;
2642 tree tem;
2644 /* An object declared as __attribute__((deprecated)) suppresses
2645 warnings of uses of other deprecated items. */
2646 if (lookup_attribute ("deprecated", attributes))
2647 deprecated_state = DEPRECATED_SUPPRESS;
2649 decl = grokdeclarator (declarator, declspecs,
2650 NORMAL, initialized);
2652 deprecated_state = DEPRECATED_NORMAL;
2654 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2655 && MAIN_NAME_P (DECL_NAME (decl)))
2656 warning ("%H'%D' is usually a function",
2657 &DECL_SOURCE_LOCATION (decl), decl);
2659 if (initialized)
2660 /* Is it valid for this decl to have an initializer at all?
2661 If not, set INITIALIZED to zero, which will indirectly
2662 tell `finish_decl' to ignore the initializer once it is parsed. */
2663 switch (TREE_CODE (decl))
2665 case TYPE_DECL:
2666 error ("typedef `%s' is initialized (use __typeof__ instead)",
2667 IDENTIFIER_POINTER (DECL_NAME (decl)));
2668 initialized = 0;
2669 break;
2671 case FUNCTION_DECL:
2672 error ("function `%s' is initialized like a variable",
2673 IDENTIFIER_POINTER (DECL_NAME (decl)));
2674 initialized = 0;
2675 break;
2677 case PARM_DECL:
2678 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2679 error ("parameter `%s' is initialized",
2680 IDENTIFIER_POINTER (DECL_NAME (decl)));
2681 initialized = 0;
2682 break;
2684 default:
2685 /* Don't allow initializations for incomplete types
2686 except for arrays which might be completed by the initialization. */
2688 /* This can happen if the array size is an undefined macro. We already
2689 gave a warning, so we don't need another one. */
2690 if (TREE_TYPE (decl) == error_mark_node)
2691 initialized = 0;
2692 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2694 /* A complete type is ok if size is fixed. */
2696 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2697 || C_DECL_VARIABLE_SIZE (decl))
2699 error ("variable-sized object may not be initialized");
2700 initialized = 0;
2703 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2705 error ("variable `%s' has initializer but incomplete type",
2706 IDENTIFIER_POINTER (DECL_NAME (decl)));
2707 initialized = 0;
2709 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2711 error ("elements of array `%s' have incomplete type",
2712 IDENTIFIER_POINTER (DECL_NAME (decl)));
2713 initialized = 0;
2717 if (initialized)
2719 DECL_EXTERNAL (decl) = 0;
2720 if (current_binding_level == global_binding_level)
2721 TREE_STATIC (decl) = 1;
2723 /* Tell `pushdecl' this is an initialized decl
2724 even though we don't yet have the initializer expression.
2725 Also tell `finish_decl' it may store the real initializer. */
2726 DECL_INITIAL (decl) = error_mark_node;
2729 /* If this is a function declaration, write a record describing it to the
2730 prototypes file (if requested). */
2732 if (TREE_CODE (decl) == FUNCTION_DECL)
2733 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2735 /* ANSI specifies that a tentative definition which is not merged with
2736 a non-tentative definition behaves exactly like a definition with an
2737 initializer equal to zero. (Section 3.7.2)
2739 -fno-common gives strict ANSI behavior, though this tends to break
2740 a large body of code that grew up without this rule.
2742 Thread-local variables are never common, since there's no entrenched
2743 body of code to break, and it allows more efficient variable references
2744 in the presence of dynamic linking. */
2746 if (TREE_CODE (decl) == VAR_DECL
2747 && !initialized
2748 && TREE_PUBLIC (decl)
2749 && !DECL_THREAD_LOCAL (decl)
2750 && !flag_no_common)
2751 DECL_COMMON (decl) = 1;
2753 /* Set attributes here so if duplicate decl, will have proper attributes. */
2754 decl_attributes (&decl, attributes, 0);
2756 /* If #pragma weak was used, mark the decl weak now. */
2757 if (current_binding_level == global_binding_level)
2758 maybe_apply_pragma_weak (decl);
2760 if (TREE_CODE (decl) == FUNCTION_DECL
2761 && DECL_DECLARED_INLINE_P (decl)
2762 && DECL_UNINLINABLE (decl)
2763 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2764 warning ("%Hinline function '%D' given attribute noinline",
2765 &DECL_SOURCE_LOCATION (decl), decl);
2767 /* Add this decl to the current binding level.
2768 TEM may equal DECL or it may be a previous decl of the same name. */
2769 tem = pushdecl (decl);
2771 /* For a local variable, define the RTL now. */
2772 if (current_binding_level != global_binding_level
2773 /* But not if this is a duplicate decl
2774 and we preserved the rtl from the previous one
2775 (which may or may not happen). */
2776 && !DECL_RTL_SET_P (tem)
2777 && C_DECL_FILE_SCOPE (tem))
2779 if (TREE_TYPE (tem) != error_mark_node
2780 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2781 expand_decl (tem);
2782 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2783 && DECL_INITIAL (tem) != 0)
2784 expand_decl (tem);
2787 return tem;
2790 /* Finish processing of a declaration;
2791 install its initial value.
2792 If the length of an array type is not known before,
2793 it must be determined now, from the initial value, or it is an error. */
2795 void
2796 finish_decl (tree decl, tree init, tree asmspec_tree)
2798 tree type = TREE_TYPE (decl);
2799 int was_incomplete = (DECL_SIZE (decl) == 0);
2800 const char *asmspec = 0;
2802 /* If a name was specified, get the string. */
2803 if (current_binding_level == global_binding_level)
2804 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2805 if (asmspec_tree)
2806 asmspec = TREE_STRING_POINTER (asmspec_tree);
2808 /* If `start_decl' didn't like having an initialization, ignore it now. */
2809 if (init != 0 && DECL_INITIAL (decl) == 0)
2810 init = 0;
2812 /* Don't crash if parm is initialized. */
2813 if (TREE_CODE (decl) == PARM_DECL)
2814 init = 0;
2816 if (init)
2817 store_init_value (decl, init);
2819 /* Deduce size of array from initialization, if not already known */
2820 if (TREE_CODE (type) == ARRAY_TYPE
2821 && TYPE_DOMAIN (type) == 0
2822 && TREE_CODE (decl) != TYPE_DECL)
2824 int do_default
2825 = (TREE_STATIC (decl)
2826 /* Even if pedantic, an external linkage array
2827 may have incomplete type at first. */
2828 ? pedantic && !TREE_PUBLIC (decl)
2829 : !DECL_EXTERNAL (decl));
2830 int failure
2831 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2833 /* Get the completed type made by complete_array_type. */
2834 type = TREE_TYPE (decl);
2836 if (failure == 1)
2837 error ("%Hinitializer fails to determine size of '%D'",
2838 &DECL_SOURCE_LOCATION (decl), decl);
2840 else if (failure == 2)
2842 if (do_default)
2843 error ("%Harray size missing in '%D'",
2844 &DECL_SOURCE_LOCATION (decl), decl);
2845 /* If a `static' var's size isn't known,
2846 make it extern as well as static, so it does not get
2847 allocated.
2848 If it is not `static', then do not mark extern;
2849 finish_incomplete_decl will give it a default size
2850 and it will get allocated. */
2851 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2852 DECL_EXTERNAL (decl) = 1;
2855 /* TYPE_MAX_VALUE is always one less than the number of elements
2856 in the array, because we start counting at zero. Therefore,
2857 warn only if the value is less than zero. */
2858 else if (pedantic && TYPE_DOMAIN (type) != 0
2859 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2860 error ("%Hzero or negative size array '%D'",
2861 &DECL_SOURCE_LOCATION (decl), decl);
2863 layout_decl (decl, 0);
2866 if (TREE_CODE (decl) == VAR_DECL)
2868 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2869 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2870 layout_decl (decl, 0);
2872 if (DECL_SIZE (decl) == 0
2873 /* Don't give an error if we already gave one earlier. */
2874 && TREE_TYPE (decl) != error_mark_node
2875 && (TREE_STATIC (decl)
2877 /* A static variable with an incomplete type
2878 is an error if it is initialized.
2879 Also if it is not file scope.
2880 Otherwise, let it through, but if it is not `extern'
2881 then it may cause an error message later. */
2882 (DECL_INITIAL (decl) != 0
2883 || !C_DECL_FILE_SCOPE (decl))
2885 /* An automatic variable with an incomplete type
2886 is an error. */
2887 !DECL_EXTERNAL (decl)))
2889 error ("%Hstorage size of '%D' isn't known",
2890 &DECL_SOURCE_LOCATION (decl), decl);
2891 TREE_TYPE (decl) = error_mark_node;
2894 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2895 && DECL_SIZE (decl) != 0)
2897 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2898 constant_expression_warning (DECL_SIZE (decl));
2899 else
2900 error ("%Hstorage size of '%D' isn't constant",
2901 &DECL_SOURCE_LOCATION (decl), decl);
2904 if (TREE_USED (type))
2905 TREE_USED (decl) = 1;
2908 /* If this is a function and an assembler name is specified, reset DECL_RTL
2909 so we can give it its new name. Also, update built_in_decls if it
2910 was a normal built-in. */
2911 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2913 /* ASMSPEC is given, and not the name of a register. Mark the
2914 name with a star so assemble_name won't munge it. */
2915 char *starred = alloca (strlen (asmspec) + 2);
2916 starred[0] = '*';
2917 strcpy (starred + 1, asmspec);
2919 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2921 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2922 SET_DECL_RTL (builtin, NULL_RTX);
2923 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2924 #ifdef TARGET_MEM_FUNCTIONS
2925 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2926 init_block_move_fn (starred);
2927 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2928 init_block_clear_fn (starred);
2929 #else
2930 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2931 init_block_move_fn (starred);
2932 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2933 init_block_clear_fn (starred);
2934 #endif
2936 SET_DECL_RTL (decl, NULL_RTX);
2937 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2940 /* Output the assembler code and/or RTL code for variables and functions,
2941 unless the type is an undefined structure or union.
2942 If not, it will get done when the type is completed. */
2944 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2946 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2947 if (c_dialect_objc ())
2948 objc_check_decl (decl);
2950 if (C_DECL_FILE_SCOPE (decl))
2952 if (DECL_INITIAL (decl) == NULL_TREE
2953 || DECL_INITIAL (decl) == error_mark_node)
2954 /* Don't output anything
2955 when a tentative file-scope definition is seen.
2956 But at end of compilation, do output code for them. */
2957 DECL_DEFER_OUTPUT (decl) = 1;
2958 rest_of_decl_compilation (decl, asmspec, true, 0);
2960 else
2962 /* This is a local variable. If there is an ASMSPEC, the
2963 user has requested that we handle it specially. */
2964 if (asmspec)
2966 /* In conjunction with an ASMSPEC, the `register'
2967 keyword indicates that we should place the variable
2968 in a particular register. */
2969 if (DECL_REGISTER (decl))
2970 DECL_C_HARD_REGISTER (decl) = 1;
2972 /* If this is not a static variable, issue a warning.
2973 It doesn't make any sense to give an ASMSPEC for an
2974 ordinary, non-register local variable. Historically,
2975 GCC has accepted -- but ignored -- the ASMSPEC in
2976 this case. */
2977 if (TREE_CODE (decl) == VAR_DECL
2978 && !DECL_REGISTER (decl)
2979 && !TREE_STATIC (decl))
2980 warning ("%Hignoring asm-specifier for non-static local "
2981 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2982 else
2983 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2986 if (TREE_CODE (decl) != FUNCTION_DECL)
2987 add_decl_stmt (decl);
2990 if (!C_DECL_FILE_SCOPE (decl))
2992 /* Recompute the RTL of a local array now
2993 if it used to be an incomplete type. */
2994 if (was_incomplete
2995 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2997 /* If we used it already as memory, it must stay in memory. */
2998 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2999 /* If it's still incomplete now, no init will save it. */
3000 if (DECL_SIZE (decl) == 0)
3001 DECL_INITIAL (decl) = 0;
3006 /* If this was marked 'used', be sure it will be output. */
3007 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3008 mark_referenced (DECL_ASSEMBLER_NAME (decl));
3010 if (TREE_CODE (decl) == TYPE_DECL)
3012 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3013 if (c_dialect_objc ())
3014 objc_check_decl (decl);
3015 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
3018 /* At the end of a declaration, throw away any variable type sizes
3019 of types defined inside that declaration. There is no use
3020 computing them in the following function definition. */
3021 if (current_binding_level == global_binding_level)
3022 get_pending_sizes ();
3024 /* Install a cleanup (aka destructor) if one was given. */
3025 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3027 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3028 if (attr)
3030 static bool eh_initialized_p;
3032 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3033 tree cleanup_decl = lookup_name (cleanup_id);
3034 tree cleanup;
3036 /* Build "cleanup(&decl)" for the destructor. */
3037 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3038 cleanup = build_tree_list (NULL_TREE, cleanup);
3039 cleanup = build_function_call (cleanup_decl, cleanup);
3041 /* Don't warn about decl unused; the cleanup uses it. */
3042 TREE_USED (decl) = 1;
3044 /* Initialize EH, if we've been told to do so. */
3045 if (flag_exceptions && !eh_initialized_p)
3047 eh_initialized_p = true;
3048 eh_personality_libfunc
3049 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3050 ? "__gcc_personality_sj0"
3051 : "__gcc_personality_v0");
3052 using_eh_for_cleanups ();
3055 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3060 /* Given a parsed parameter declaration,
3061 decode it into a PARM_DECL and push that on the current binding level.
3062 Also, for the sake of forward parm decls,
3063 record the given order of parms in `parm_order'. */
3065 void
3066 push_parm_decl (tree parm)
3068 tree decl;
3069 int old_immediate_size_expand = immediate_size_expand;
3070 /* Don't try computing parm sizes now -- wait till fn is called. */
3071 immediate_size_expand = 0;
3073 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3074 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3075 decl_attributes (&decl, TREE_VALUE (parm), 0);
3077 decl = pushdecl (decl);
3079 immediate_size_expand = old_immediate_size_expand;
3081 current_binding_level->parm_order
3082 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3084 /* Add this decl to the current binding level. */
3085 finish_decl (decl, NULL_TREE, NULL_TREE);
3088 /* Clear the given order of parms in `parm_order'.
3089 Used at start of parm list,
3090 and also at semicolon terminating forward decls. */
3092 void
3093 clear_parm_order (void)
3095 current_binding_level->parm_order = NULL_TREE;
3098 static GTY(()) int compound_literal_number;
3100 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3101 literal, which may be an incomplete array type completed by the
3102 initializer; INIT is a CONSTRUCTOR that initializes the compound
3103 literal. */
3105 tree
3106 build_compound_literal (tree type, tree init)
3108 /* We do not use start_decl here because we have a type, not a declarator;
3109 and do not use finish_decl because the decl should be stored inside
3110 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3111 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3112 tree complit;
3113 tree stmt;
3114 DECL_EXTERNAL (decl) = 0;
3115 TREE_PUBLIC (decl) = 0;
3116 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3117 DECL_CONTEXT (decl) = current_function_decl;
3118 TREE_USED (decl) = 1;
3119 TREE_TYPE (decl) = type;
3120 TREE_READONLY (decl) = TREE_READONLY (type);
3121 store_init_value (decl, init);
3123 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3125 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3126 if (failure)
3127 abort ();
3130 type = TREE_TYPE (decl);
3131 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3132 return error_mark_node;
3134 stmt = build_stmt (DECL_STMT, decl);
3135 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3136 TREE_SIDE_EFFECTS (complit) = 1;
3138 layout_decl (decl, 0);
3140 if (TREE_STATIC (decl))
3142 /* This decl needs a name for the assembler output. We also need
3143 a unique suffix to be added to the name. */
3144 char *name;
3146 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3147 compound_literal_number);
3148 compound_literal_number++;
3149 DECL_NAME (decl) = get_identifier (name);
3150 DECL_DEFER_OUTPUT (decl) = 1;
3151 DECL_COMDAT (decl) = 1;
3152 DECL_ARTIFICIAL (decl) = 1;
3153 pushdecl (decl);
3154 rest_of_decl_compilation (decl, NULL, 1, 0);
3157 return complit;
3160 /* Make TYPE a complete type based on INITIAL_VALUE.
3161 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3162 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3165 complete_array_type (tree type, tree initial_value, int do_default)
3167 tree maxindex = NULL_TREE;
3168 int value = 0;
3170 if (initial_value)
3172 /* Note MAXINDEX is really the maximum index,
3173 one less than the size. */
3174 if (TREE_CODE (initial_value) == STRING_CST)
3176 int eltsize
3177 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3178 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3179 / eltsize) - 1, 0);
3181 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3183 tree elts = CONSTRUCTOR_ELTS (initial_value);
3184 maxindex = build_int_2 (-1, -1);
3185 for (; elts; elts = TREE_CHAIN (elts))
3187 if (TREE_PURPOSE (elts))
3188 maxindex = TREE_PURPOSE (elts);
3189 else
3190 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3191 maxindex, integer_one_node));
3193 maxindex = copy_node (maxindex);
3195 else
3197 /* Make an error message unless that happened already. */
3198 if (initial_value != error_mark_node)
3199 value = 1;
3201 /* Prevent further error messages. */
3202 maxindex = build_int_2 (0, 0);
3206 if (!maxindex)
3208 if (do_default)
3209 maxindex = build_int_2 (0, 0);
3210 value = 2;
3213 if (maxindex)
3215 TYPE_DOMAIN (type) = build_index_type (maxindex);
3216 if (!TREE_TYPE (maxindex))
3217 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3220 /* Lay out the type now that we can get the real answer. */
3222 layout_type (type);
3224 return value;
3227 /* Determine whether TYPE is a structure with a flexible array member,
3228 or a union containing such a structure (possibly recursively). */
3230 static bool
3231 flexible_array_type_p (tree type)
3233 tree x;
3234 switch (TREE_CODE (type))
3236 case RECORD_TYPE:
3237 x = TYPE_FIELDS (type);
3238 if (x == NULL_TREE)
3239 return false;
3240 while (TREE_CHAIN (x) != NULL_TREE)
3241 x = TREE_CHAIN (x);
3242 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3243 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3244 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3245 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3246 return true;
3247 return false;
3248 case UNION_TYPE:
3249 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3251 if (flexible_array_type_p (TREE_TYPE (x)))
3252 return true;
3254 return false;
3255 default:
3256 return false;
3260 /* Given declspecs and a declarator,
3261 determine the name and type of the object declared
3262 and construct a ..._DECL node for it.
3263 (In one case we can return a ..._TYPE node instead.
3264 For invalid input we sometimes return 0.)
3266 DECLSPECS is a chain of tree_list nodes whose value fields
3267 are the storage classes and type specifiers.
3269 DECL_CONTEXT says which syntactic context this declaration is in:
3270 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3271 FUNCDEF for a function definition. Like NORMAL but a few different
3272 error messages in each case. Return value may be zero meaning
3273 this definition is too screwy to try to parse.
3274 PARM for a parameter declaration (either within a function prototype
3275 or before a function body). Make a PARM_DECL, or return void_type_node.
3276 TYPENAME if for a typename (in a cast or sizeof).
3277 Don't make a DECL node; just return the ..._TYPE node.
3278 FIELD for a struct or union field; make a FIELD_DECL.
3279 BITFIELD for a field with specified width.
3280 INITIALIZED is 1 if the decl has an initializer.
3282 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3283 It may also be so in the PARM case, for a prototype where the
3284 argument type is specified but not the name.
3286 This function is where the complicated C meanings of `static'
3287 and `extern' are interpreted. */
3289 static tree
3290 grokdeclarator (tree declarator, tree declspecs,
3291 enum decl_context decl_context, int initialized)
3293 int specbits = 0;
3294 tree spec;
3295 tree type = NULL_TREE;
3296 int longlong = 0;
3297 int constp;
3298 int restrictp;
3299 int volatilep;
3300 int type_quals = TYPE_UNQUALIFIED;
3301 int inlinep;
3302 int explicit_int = 0;
3303 int explicit_char = 0;
3304 int defaulted_int = 0;
3305 tree typedef_decl = 0;
3306 const char *name;
3307 tree typedef_type = 0;
3308 int funcdef_flag = 0;
3309 enum tree_code innermost_code = ERROR_MARK;
3310 int bitfield = 0;
3311 int size_varies = 0;
3312 tree decl_attr = NULL_TREE;
3313 tree array_ptr_quals = NULL_TREE;
3314 int array_parm_static = 0;
3315 tree returned_attrs = NULL_TREE;
3317 if (decl_context == BITFIELD)
3318 bitfield = 1, decl_context = FIELD;
3320 if (decl_context == FUNCDEF)
3321 funcdef_flag = 1, decl_context = NORMAL;
3323 /* Look inside a declarator for the name being declared
3324 and get it as a string, for an error message. */
3326 tree decl = declarator;
3327 name = 0;
3329 while (decl)
3330 switch (TREE_CODE (decl))
3332 case ARRAY_REF:
3333 case INDIRECT_REF:
3334 case CALL_EXPR:
3335 innermost_code = TREE_CODE (decl);
3336 decl = TREE_OPERAND (decl, 0);
3337 break;
3339 case TREE_LIST:
3340 decl = TREE_VALUE (decl);
3341 break;
3343 case IDENTIFIER_NODE:
3344 name = IDENTIFIER_POINTER (decl);
3345 decl = 0;
3346 break;
3348 default:
3349 abort ();
3351 if (name == 0)
3352 name = "type name";
3355 /* A function definition's declarator must have the form of
3356 a function declarator. */
3358 if (funcdef_flag && innermost_code != CALL_EXPR)
3359 return 0;
3361 /* Anything declared one level down from the top level
3362 must be one of the parameters of a function
3363 (because the body is at least two levels down). */
3365 /* If this looks like a function definition, make it one,
3366 even if it occurs where parms are expected.
3367 Then store_parm_decls will reject it and not use it as a parm. */
3368 if (decl_context == NORMAL && !funcdef_flag
3369 && current_binding_level->parm_flag)
3370 decl_context = PARM;
3372 /* Look through the decl specs and record which ones appear.
3373 Some typespecs are defined as built-in typenames.
3374 Others, the ones that are modifiers of other types,
3375 are represented by bits in SPECBITS: set the bits for
3376 the modifiers that appear. Storage class keywords are also in SPECBITS.
3378 If there is a typedef name or a type, store the type in TYPE.
3379 This includes builtin typedefs such as `int'.
3381 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3382 and did not come from a user typedef.
3384 Set LONGLONG if `long' is mentioned twice. */
3386 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3388 tree id = TREE_VALUE (spec);
3390 /* If the entire declaration is itself tagged as deprecated then
3391 suppress reports of deprecated items. */
3392 if (id && TREE_DEPRECATED (id))
3394 if (deprecated_state != DEPRECATED_SUPPRESS)
3395 warn_deprecated_use (id);
3398 if (id == ridpointers[(int) RID_INT])
3399 explicit_int = 1;
3400 if (id == ridpointers[(int) RID_CHAR])
3401 explicit_char = 1;
3403 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3405 enum rid i = C_RID_CODE (id);
3406 if ((int) i <= (int) RID_LAST_MODIFIER)
3408 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3410 if (longlong)
3411 error ("`long long long' is too long for GCC");
3412 else
3414 if (pedantic && !flag_isoc99 && ! in_system_header
3415 && warn_long_long)
3416 pedwarn ("ISO C90 does not support `long long'");
3417 longlong = 1;
3420 else if (specbits & (1 << (int) i))
3422 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3424 if (!flag_isoc99)
3425 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3427 else
3428 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3431 /* Diagnose "__thread extern". Recall that this list
3432 is in the reverse order seen in the text. */
3433 if (i == RID_THREAD
3434 && (specbits & (1 << (int) RID_EXTERN
3435 | 1 << (int) RID_STATIC)))
3437 if (specbits & 1 << (int) RID_EXTERN)
3438 error ("`__thread' before `extern'");
3439 else
3440 error ("`__thread' before `static'");
3443 specbits |= 1 << (int) i;
3444 goto found;
3447 if (type)
3448 error ("two or more data types in declaration of `%s'", name);
3449 /* Actual typedefs come to us as TYPE_DECL nodes. */
3450 else if (TREE_CODE (id) == TYPE_DECL)
3452 if (TREE_TYPE (id) == error_mark_node)
3453 ; /* Allow the type to default to int to avoid cascading errors. */
3454 else
3456 type = TREE_TYPE (id);
3457 decl_attr = DECL_ATTRIBUTES (id);
3458 typedef_decl = id;
3461 /* Built-in types come as identifiers. */
3462 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3464 tree t = lookup_name (id);
3465 if (TREE_TYPE (t) == error_mark_node)
3467 else if (!t || TREE_CODE (t) != TYPE_DECL)
3468 error ("`%s' fails to be a typedef or built in type",
3469 IDENTIFIER_POINTER (id));
3470 else
3472 type = TREE_TYPE (t);
3473 typedef_decl = t;
3476 else if (TREE_CODE (id) != ERROR_MARK)
3477 type = id;
3479 found:
3483 typedef_type = type;
3484 if (type)
3485 size_varies = C_TYPE_VARIABLE_SIZE (type);
3487 /* No type at all: default to `int', and set DEFAULTED_INT
3488 because it was not a user-defined typedef. */
3490 if (type == 0)
3492 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3493 | (1 << (int) RID_SIGNED)
3494 | (1 << (int) RID_UNSIGNED)
3495 | (1 << (int) RID_COMPLEX))))
3496 /* Don't warn about typedef foo = bar. */
3497 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3498 && ! in_system_header)
3500 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3501 and this is a function, or if -Wimplicit; prefer the former
3502 warning since it is more explicit. */
3503 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3504 && funcdef_flag)
3505 warn_about_return_type = 1;
3506 else if (warn_implicit_int || flag_isoc99)
3507 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3508 name);
3511 defaulted_int = 1;
3512 type = integer_type_node;
3515 /* Now process the modifiers that were specified
3516 and check for invalid combinations. */
3518 /* Long double is a special combination. */
3520 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3521 && TYPE_MAIN_VARIANT (type) == double_type_node)
3523 specbits &= ~(1 << (int) RID_LONG);
3524 type = long_double_type_node;
3527 /* Check all other uses of type modifiers. */
3529 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3530 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3532 int ok = 0;
3534 if ((specbits & 1 << (int) RID_LONG)
3535 && (specbits & 1 << (int) RID_SHORT))
3536 error ("both long and short specified for `%s'", name);
3537 else if (((specbits & 1 << (int) RID_LONG)
3538 || (specbits & 1 << (int) RID_SHORT))
3539 && explicit_char)
3540 error ("long or short specified with char for `%s'", name);
3541 else if (((specbits & 1 << (int) RID_LONG)
3542 || (specbits & 1 << (int) RID_SHORT))
3543 && TREE_CODE (type) == REAL_TYPE)
3545 static int already = 0;
3547 error ("long or short specified with floating type for `%s'", name);
3548 if (! already && ! pedantic)
3550 error ("the only valid combination is `long double'");
3551 already = 1;
3554 else if ((specbits & 1 << (int) RID_SIGNED)
3555 && (specbits & 1 << (int) RID_UNSIGNED))
3556 error ("both signed and unsigned specified for `%s'", name);
3557 else if (TREE_CODE (type) != INTEGER_TYPE)
3558 error ("long, short, signed or unsigned invalid for `%s'", name);
3559 else
3561 ok = 1;
3562 if (!explicit_int && !defaulted_int && !explicit_char)
3564 error ("long, short, signed or unsigned used invalidly for `%s'",
3565 name);
3566 ok = 0;
3570 /* Discard the type modifiers if they are invalid. */
3571 if (! ok)
3573 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3574 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3575 longlong = 0;
3579 if ((specbits & (1 << (int) RID_COMPLEX))
3580 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3582 error ("complex invalid for `%s'", name);
3583 specbits &= ~(1 << (int) RID_COMPLEX);
3586 /* Decide whether an integer type is signed or not.
3587 Optionally treat bitfields as signed by default. */
3588 if (specbits & 1 << (int) RID_UNSIGNED
3589 || (bitfield && ! flag_signed_bitfields
3590 && (explicit_int || defaulted_int || explicit_char
3591 /* A typedef for plain `int' without `signed'
3592 can be controlled just like plain `int'. */
3593 || ! (typedef_decl != 0
3594 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3595 && TREE_CODE (type) != ENUMERAL_TYPE
3596 && !(specbits & 1 << (int) RID_SIGNED)))
3598 if (longlong)
3599 type = long_long_unsigned_type_node;
3600 else if (specbits & 1 << (int) RID_LONG)
3601 type = long_unsigned_type_node;
3602 else if (specbits & 1 << (int) RID_SHORT)
3603 type = short_unsigned_type_node;
3604 else if (type == char_type_node)
3605 type = unsigned_char_type_node;
3606 else if (typedef_decl)
3607 type = c_common_unsigned_type (type);
3608 else
3609 type = unsigned_type_node;
3611 else if ((specbits & 1 << (int) RID_SIGNED)
3612 && type == char_type_node)
3613 type = signed_char_type_node;
3614 else if (longlong)
3615 type = long_long_integer_type_node;
3616 else if (specbits & 1 << (int) RID_LONG)
3617 type = long_integer_type_node;
3618 else if (specbits & 1 << (int) RID_SHORT)
3619 type = short_integer_type_node;
3621 if (specbits & 1 << (int) RID_COMPLEX)
3623 if (pedantic && !flag_isoc99)
3624 pedwarn ("ISO C90 does not support complex types");
3625 /* If we just have "complex", it is equivalent to
3626 "complex double", but if any modifiers at all are specified it is
3627 the complex form of TYPE. E.g, "complex short" is
3628 "complex short int". */
3630 if (defaulted_int && ! longlong
3631 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3632 | (1 << (int) RID_SIGNED)
3633 | (1 << (int) RID_UNSIGNED))))
3635 if (pedantic)
3636 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3637 type = complex_double_type_node;
3639 else if (type == integer_type_node)
3641 if (pedantic)
3642 pedwarn ("ISO C does not support complex integer types");
3643 type = complex_integer_type_node;
3645 else if (type == float_type_node)
3646 type = complex_float_type_node;
3647 else if (type == double_type_node)
3648 type = complex_double_type_node;
3649 else if (type == long_double_type_node)
3650 type = complex_long_double_type_node;
3651 else
3653 if (pedantic)
3654 pedwarn ("ISO C does not support complex integer types");
3655 type = build_complex_type (type);
3659 /* Figure out the type qualifiers for the declaration. There are
3660 two ways a declaration can become qualified. One is something
3661 like `const int i' where the `const' is explicit. Another is
3662 something like `typedef const int CI; CI i' where the type of the
3663 declaration contains the `const'. */
3664 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3665 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3666 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3667 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3668 if (constp > 1 && ! flag_isoc99)
3669 pedwarn ("duplicate `const'");
3670 if (restrictp > 1 && ! flag_isoc99)
3671 pedwarn ("duplicate `restrict'");
3672 if (volatilep > 1 && ! flag_isoc99)
3673 pedwarn ("duplicate `volatile'");
3674 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3675 type = TYPE_MAIN_VARIANT (type);
3676 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3677 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3678 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3680 /* Warn if two storage classes are given. Default to `auto'. */
3683 int nclasses = 0;
3685 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3686 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3687 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3688 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3689 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3691 /* "static __thread" and "extern __thread" are allowed. */
3692 if ((specbits & (1 << (int) RID_THREAD
3693 | 1 << (int) RID_STATIC
3694 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3695 nclasses++;
3697 /* Warn about storage classes that are invalid for certain
3698 kinds of declarations (parameters, typenames, etc.). */
3700 if (nclasses > 1)
3701 error ("multiple storage classes in declaration of `%s'", name);
3702 else if (funcdef_flag
3703 && (specbits
3704 & ((1 << (int) RID_REGISTER)
3705 | (1 << (int) RID_AUTO)
3706 | (1 << (int) RID_TYPEDEF)
3707 | (1 << (int) RID_THREAD))))
3709 if (specbits & 1 << (int) RID_AUTO
3710 && (pedantic || current_binding_level == global_binding_level))
3711 pedwarn ("function definition declared `auto'");
3712 if (specbits & 1 << (int) RID_REGISTER)
3713 error ("function definition declared `register'");
3714 if (specbits & 1 << (int) RID_TYPEDEF)
3715 error ("function definition declared `typedef'");
3716 if (specbits & 1 << (int) RID_THREAD)
3717 error ("function definition declared `__thread'");
3718 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3719 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3721 else if (decl_context != NORMAL && nclasses > 0)
3723 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3725 else
3727 switch (decl_context)
3729 case FIELD:
3730 error ("storage class specified for structure field `%s'",
3731 name);
3732 break;
3733 case PARM:
3734 error ("storage class specified for parameter `%s'", name);
3735 break;
3736 default:
3737 error ("storage class specified for typename");
3738 break;
3740 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3741 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3742 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3745 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3747 /* `extern' with initialization is invalid if not at top level. */
3748 if (current_binding_level == global_binding_level)
3749 warning ("`%s' initialized and declared `extern'", name);
3750 else
3751 error ("`%s' has both `extern' and initializer", name);
3753 else if (current_binding_level == global_binding_level)
3755 if (specbits & 1 << (int) RID_AUTO)
3756 error ("top-level declaration of `%s' specifies `auto'", name);
3758 else
3760 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3761 error ("nested function `%s' declared `extern'", name);
3762 else if ((specbits & (1 << (int) RID_THREAD
3763 | 1 << (int) RID_EXTERN
3764 | 1 << (int) RID_STATIC))
3765 == (1 << (int) RID_THREAD))
3767 error ("function-scope `%s' implicitly auto and declared `__thread'",
3768 name);
3769 specbits &= ~(1 << (int) RID_THREAD);
3774 /* Now figure out the structure of the declarator proper.
3775 Descend through it, creating more complex types, until we reach
3776 the declared identifier (or NULL_TREE, in an absolute declarator). */
3778 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3780 if (type == error_mark_node)
3782 declarator = TREE_OPERAND (declarator, 0);
3783 continue;
3786 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3787 an INDIRECT_REF (for *...),
3788 a CALL_EXPR (for ...(...)),
3789 a TREE_LIST (for nested attributes),
3790 an identifier (for the name being declared)
3791 or a null pointer (for the place in an absolute declarator
3792 where the name was omitted).
3793 For the last two cases, we have just exited the loop.
3795 At this point, TYPE is the type of elements of an array,
3796 or for a function to return, or for a pointer to point to.
3797 After this sequence of ifs, TYPE is the type of the
3798 array or function or pointer, and DECLARATOR has had its
3799 outermost layer removed. */
3801 if (array_ptr_quals != NULL_TREE || array_parm_static)
3803 /* Only the innermost declarator (making a parameter be of
3804 array type which is converted to pointer type)
3805 may have static or type qualifiers. */
3806 error ("static or type qualifiers in non-parameter array declarator");
3807 array_ptr_quals = NULL_TREE;
3808 array_parm_static = 0;
3811 if (TREE_CODE (declarator) == TREE_LIST)
3813 /* We encode a declarator with embedded attributes using
3814 a TREE_LIST. */
3815 tree attrs = TREE_PURPOSE (declarator);
3816 tree inner_decl;
3817 int attr_flags = 0;
3818 declarator = TREE_VALUE (declarator);
3819 inner_decl = declarator;
3820 while (inner_decl != NULL_TREE
3821 && TREE_CODE (inner_decl) == TREE_LIST)
3822 inner_decl = TREE_VALUE (inner_decl);
3823 if (inner_decl == NULL_TREE
3824 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3825 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3826 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3827 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3828 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3829 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3830 returned_attrs = decl_attributes (&type,
3831 chainon (returned_attrs, attrs),
3832 attr_flags);
3834 else if (TREE_CODE (declarator) == ARRAY_REF)
3836 tree itype = NULL_TREE;
3837 tree size = TREE_OPERAND (declarator, 1);
3838 /* The index is a signed object `sizetype' bits wide. */
3839 tree index_type = c_common_signed_type (sizetype);
3841 array_ptr_quals = TREE_TYPE (declarator);
3842 array_parm_static = TREE_STATIC (declarator);
3844 declarator = TREE_OPERAND (declarator, 0);
3846 /* Check for some types that there cannot be arrays of. */
3848 if (VOID_TYPE_P (type))
3850 error ("declaration of `%s' as array of voids", name);
3851 type = error_mark_node;
3854 if (TREE_CODE (type) == FUNCTION_TYPE)
3856 error ("declaration of `%s' as array of functions", name);
3857 type = error_mark_node;
3860 if (pedantic && flexible_array_type_p (type))
3861 pedwarn ("invalid use of structure with flexible array member");
3863 if (size == error_mark_node)
3864 type = error_mark_node;
3866 if (type == error_mark_node)
3867 continue;
3869 /* If size was specified, set ITYPE to a range-type for that size.
3870 Otherwise, ITYPE remains null. finish_decl may figure it out
3871 from an initial value. */
3873 if (size)
3875 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3876 STRIP_TYPE_NOPS (size);
3878 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3880 error ("size of array `%s' has non-integer type", name);
3881 size = integer_one_node;
3884 if (pedantic && integer_zerop (size))
3885 pedwarn ("ISO C forbids zero-size array `%s'", name);
3887 if (TREE_CODE (size) == INTEGER_CST)
3889 constant_expression_warning (size);
3890 if (tree_int_cst_sgn (size) < 0)
3892 error ("size of array `%s' is negative", name);
3893 size = integer_one_node;
3896 else
3898 /* Make sure the array size remains visibly nonconstant
3899 even if it is (eg) a const variable with known value. */
3900 size_varies = 1;
3902 if (!flag_isoc99 && pedantic)
3904 if (TREE_CONSTANT (size))
3905 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3906 name);
3907 else
3908 pedwarn ("ISO C90 forbids variable-size array `%s'",
3909 name);
3913 if (integer_zerop (size))
3915 /* A zero-length array cannot be represented with an
3916 unsigned index type, which is what we'll get with
3917 build_index_type. Create an open-ended range instead. */
3918 itype = build_range_type (sizetype, size, NULL_TREE);
3920 else
3922 /* Compute the maximum valid index, that is, size - 1.
3923 Do the calculation in index_type, so that if it is
3924 a variable the computations will be done in the
3925 proper mode. */
3926 itype = fold (build (MINUS_EXPR, index_type,
3927 convert (index_type, size),
3928 convert (index_type, size_one_node)));
3930 /* If that overflowed, the array is too big.
3931 ??? While a size of INT_MAX+1 technically shouldn't
3932 cause an overflow (because we subtract 1), the overflow
3933 is recorded during the conversion to index_type, before
3934 the subtraction. Handling this case seems like an
3935 unnecessary complication. */
3936 if (TREE_OVERFLOW (itype))
3938 error ("size of array `%s' is too large", name);
3939 type = error_mark_node;
3940 continue;
3943 if (size_varies)
3945 /* We must be able to distinguish the
3946 SAVE_EXPR_CONTEXT for the variably-sized type
3947 so that we can set it correctly in
3948 set_save_expr_context. The convention is
3949 that all SAVE_EXPRs that need to be reset
3950 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3951 tree cfd = current_function_decl;
3952 if (decl_context == PARM)
3953 current_function_decl = NULL_TREE;
3954 itype = variable_size (itype);
3955 if (decl_context == PARM)
3956 current_function_decl = cfd;
3958 itype = build_index_type (itype);
3961 else if (decl_context == FIELD)
3963 if (pedantic && !flag_isoc99 && !in_system_header)
3964 pedwarn ("ISO C90 does not support flexible array members");
3966 /* ISO C99 Flexible array members are effectively identical
3967 to GCC's zero-length array extension. */
3968 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3971 /* If pedantic, complain about arrays of incomplete types. */
3973 if (pedantic && !COMPLETE_TYPE_P (type))
3974 pedwarn ("array type has incomplete element type");
3976 /* Build the array type itself, then merge any constancy or
3977 volatility into the target type. We must do it in this order
3978 to ensure that the TYPE_MAIN_VARIANT field of the array type
3979 is set correctly. */
3981 type = build_array_type (type, itype);
3982 if (type_quals)
3983 type = c_build_qualified_type (type, type_quals);
3985 if (size_varies)
3986 C_TYPE_VARIABLE_SIZE (type) = 1;
3988 /* The GCC extension for zero-length arrays differs from
3989 ISO flexible array members in that sizeof yields zero. */
3990 if (size && integer_zerop (size))
3992 layout_type (type);
3993 TYPE_SIZE (type) = bitsize_zero_node;
3994 TYPE_SIZE_UNIT (type) = size_zero_node;
3996 if (decl_context != PARM
3997 && (array_ptr_quals != NULL_TREE || array_parm_static))
3999 error ("static or type qualifiers in non-parameter array declarator");
4000 array_ptr_quals = NULL_TREE;
4001 array_parm_static = 0;
4004 else if (TREE_CODE (declarator) == CALL_EXPR)
4006 tree arg_types;
4008 /* Declaring a function type.
4009 Make sure we have a valid type for the function to return. */
4010 if (type == error_mark_node)
4011 continue;
4013 size_varies = 0;
4015 /* Warn about some types functions can't return. */
4017 if (TREE_CODE (type) == FUNCTION_TYPE)
4019 error ("`%s' declared as function returning a function", name);
4020 type = integer_type_node;
4022 if (TREE_CODE (type) == ARRAY_TYPE)
4024 error ("`%s' declared as function returning an array", name);
4025 type = integer_type_node;
4028 /* Construct the function type and go to the next
4029 inner layer of declarator. */
4031 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4032 funcdef_flag
4033 /* Say it's a definition
4034 only for the CALL_EXPR
4035 closest to the identifier. */
4036 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4037 /* Type qualifiers before the return type of the function
4038 qualify the return type, not the function type. */
4039 if (type_quals)
4041 /* Type qualifiers on a function return type are normally
4042 permitted by the standard but have no effect, so give a
4043 warning at -Wextra. Qualifiers on a void return type have
4044 meaning as a GNU extension, and are banned on function
4045 definitions in ISO C. FIXME: strictly we shouldn't
4046 pedwarn for qualified void return types except on function
4047 definitions, but not doing so could lead to the undesirable
4048 state of a "volatile void" function return type not being
4049 warned about, and a use of the function being compiled
4050 with GNU semantics, with no diagnostics under -pedantic. */
4051 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4052 pedwarn ("ISO C forbids qualified void function return type");
4053 else if (extra_warnings
4054 && !(VOID_TYPE_P (type)
4055 && type_quals == TYPE_QUAL_VOLATILE))
4056 warning ("type qualifiers ignored on function return type");
4058 type = c_build_qualified_type (type, type_quals);
4060 type_quals = TYPE_UNQUALIFIED;
4062 type = build_function_type (type, arg_types);
4063 declarator = TREE_OPERAND (declarator, 0);
4065 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4066 the formal parameter list of this FUNCTION_TYPE to point to
4067 the FUNCTION_TYPE node itself. */
4070 tree link;
4072 for (link = last_function_parm_tags;
4073 link;
4074 link = TREE_CHAIN (link))
4075 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4078 else if (TREE_CODE (declarator) == INDIRECT_REF)
4080 /* Merge any constancy or volatility into the target type
4081 for the pointer. */
4083 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4084 && type_quals)
4085 pedwarn ("ISO C forbids qualified function types");
4086 if (type_quals)
4087 type = c_build_qualified_type (type, type_quals);
4088 type_quals = TYPE_UNQUALIFIED;
4089 size_varies = 0;
4091 type = build_pointer_type (type);
4093 /* Process a list of type modifier keywords
4094 (such as const or volatile) that were given inside the `*'. */
4096 if (TREE_TYPE (declarator))
4098 tree typemodlist;
4099 int erred = 0;
4101 constp = 0;
4102 volatilep = 0;
4103 restrictp = 0;
4104 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4105 typemodlist = TREE_CHAIN (typemodlist))
4107 tree qualifier = TREE_VALUE (typemodlist);
4109 if (C_IS_RESERVED_WORD (qualifier))
4111 if (C_RID_CODE (qualifier) == RID_CONST)
4112 constp++;
4113 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4114 volatilep++;
4115 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4116 restrictp++;
4117 else
4118 erred++;
4120 else
4121 erred++;
4124 if (erred)
4125 error ("invalid type modifier within pointer declarator");
4126 if (constp > 1 && ! flag_isoc99)
4127 pedwarn ("duplicate `const'");
4128 if (volatilep > 1 && ! flag_isoc99)
4129 pedwarn ("duplicate `volatile'");
4130 if (restrictp > 1 && ! flag_isoc99)
4131 pedwarn ("duplicate `restrict'");
4133 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4134 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4135 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4138 declarator = TREE_OPERAND (declarator, 0);
4140 else
4141 abort ();
4145 /* Now TYPE has the actual type. */
4147 /* Did array size calculations overflow? */
4149 if (TREE_CODE (type) == ARRAY_TYPE
4150 && COMPLETE_TYPE_P (type)
4151 && TREE_OVERFLOW (TYPE_SIZE (type)))
4153 error ("size of array `%s' is too large", name);
4154 /* If we proceed with the array type as it is, we'll eventually
4155 crash in tree_low_cst(). */
4156 type = error_mark_node;
4159 /* If this is declaring a typedef name, return a TYPE_DECL. */
4161 if (specbits & (1 << (int) RID_TYPEDEF))
4163 tree decl;
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 qualified function types");
4169 if (type_quals)
4170 type = c_build_qualified_type (type, type_quals);
4171 decl = build_decl (TYPE_DECL, declarator, type);
4172 if ((specbits & (1 << (int) RID_SIGNED))
4173 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4174 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4175 decl_attributes (&decl, returned_attrs, 0);
4176 return decl;
4179 /* Detect the case of an array type of unspecified size
4180 which came, as such, direct from a typedef name.
4181 We must copy the type, so that each identifier gets
4182 a distinct type, so that each identifier's size can be
4183 controlled separately by its own initializer. */
4185 if (type != 0 && typedef_type != 0
4186 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4187 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4189 type = build_array_type (TREE_TYPE (type), 0);
4190 if (size_varies)
4191 C_TYPE_VARIABLE_SIZE (type) = 1;
4194 /* If this is a type name (such as, in a cast or sizeof),
4195 compute the type and return it now. */
4197 if (decl_context == TYPENAME)
4199 /* Note that the grammar rejects storage classes
4200 in typenames, fields or parameters */
4201 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4202 && type_quals)
4203 pedwarn ("ISO C forbids const or volatile function types");
4204 if (type_quals)
4205 type = c_build_qualified_type (type, type_quals);
4206 decl_attributes (&type, returned_attrs, 0);
4207 return type;
4210 /* Aside from typedefs and type names (handle above),
4211 `void' at top level (not within pointer)
4212 is allowed only in public variables.
4213 We don't complain about parms either, but that is because
4214 a better error message can be made later. */
4216 if (VOID_TYPE_P (type) && decl_context != PARM
4217 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4218 && ((specbits & (1 << (int) RID_EXTERN))
4219 || (current_binding_level == global_binding_level
4220 && !(specbits
4221 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4223 error ("variable or field `%s' declared void", name);
4224 type = integer_type_node;
4227 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4228 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4231 tree decl;
4233 if (decl_context == PARM)
4235 tree type_as_written;
4236 tree promoted_type;
4238 /* A parameter declared as an array of T is really a pointer to T.
4239 One declared as a function is really a pointer to a function. */
4241 if (TREE_CODE (type) == ARRAY_TYPE)
4243 /* Transfer const-ness of array into that of type pointed to. */
4244 type = TREE_TYPE (type);
4245 if (type_quals)
4246 type = c_build_qualified_type (type, type_quals);
4247 type = build_pointer_type (type);
4248 type_quals = TYPE_UNQUALIFIED;
4249 if (array_ptr_quals)
4251 tree new_ptr_quals, new_ptr_attrs;
4252 int erred = 0;
4253 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4254 /* We don't yet implement attributes in this context. */
4255 if (new_ptr_attrs != NULL_TREE)
4256 warning ("attributes in parameter array declarator ignored");
4258 constp = 0;
4259 volatilep = 0;
4260 restrictp = 0;
4261 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4263 tree qualifier = TREE_VALUE (new_ptr_quals);
4265 if (C_IS_RESERVED_WORD (qualifier))
4267 if (C_RID_CODE (qualifier) == RID_CONST)
4268 constp++;
4269 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4270 volatilep++;
4271 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4272 restrictp++;
4273 else
4274 erred++;
4276 else
4277 erred++;
4280 if (erred)
4281 error ("invalid type modifier within array declarator");
4283 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4284 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4285 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4287 size_varies = 0;
4289 else if (TREE_CODE (type) == FUNCTION_TYPE)
4291 if (pedantic && type_quals)
4292 pedwarn ("ISO C forbids qualified function types");
4293 if (type_quals)
4294 type = c_build_qualified_type (type, type_quals);
4295 type = build_pointer_type (type);
4296 type_quals = TYPE_UNQUALIFIED;
4298 else if (type_quals)
4299 type = c_build_qualified_type (type, type_quals);
4301 type_as_written = type;
4303 decl = build_decl (PARM_DECL, declarator, type);
4304 if (size_varies)
4305 C_DECL_VARIABLE_SIZE (decl) = 1;
4307 /* Compute the type actually passed in the parmlist,
4308 for the case where there is no prototype.
4309 (For example, shorts and chars are passed as ints.)
4310 When there is a prototype, this is overridden later. */
4312 if (type == error_mark_node)
4313 promoted_type = type;
4314 else
4315 promoted_type = c_type_promotes_to (type);
4317 DECL_ARG_TYPE (decl) = promoted_type;
4318 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4320 else if (decl_context == FIELD)
4322 /* Structure field. It may not be a function. */
4324 if (TREE_CODE (type) == FUNCTION_TYPE)
4326 error ("field `%s' declared as a function", name);
4327 type = build_pointer_type (type);
4329 else if (TREE_CODE (type) != ERROR_MARK
4330 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4332 error ("field `%s' has incomplete type", name);
4333 type = error_mark_node;
4335 /* Move type qualifiers down to element of an array. */
4336 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4338 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4339 type_quals),
4340 TYPE_DOMAIN (type));
4342 decl = build_decl (FIELD_DECL, declarator, type);
4343 DECL_NONADDRESSABLE_P (decl) = bitfield;
4345 if (size_varies)
4346 C_DECL_VARIABLE_SIZE (decl) = 1;
4348 else if (TREE_CODE (type) == FUNCTION_TYPE)
4350 /* Every function declaration is "external"
4351 except for those which are inside a function body
4352 in which `auto' is used.
4353 That is a case not specified by ANSI C,
4354 and we use it for forward declarations for nested functions. */
4355 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4356 || current_binding_level == global_binding_level);
4358 if (specbits & (1 << (int) RID_AUTO)
4359 && (pedantic || current_binding_level == global_binding_level))
4360 pedwarn ("invalid storage class for function `%s'", name);
4361 if (specbits & (1 << (int) RID_REGISTER))
4362 error ("invalid storage class for function `%s'", name);
4363 if (specbits & (1 << (int) RID_THREAD))
4364 error ("invalid storage class for function `%s'", name);
4365 /* Function declaration not at top level.
4366 Storage classes other than `extern' are not allowed
4367 and `extern' makes no difference. */
4368 if (current_binding_level != global_binding_level
4369 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4370 && pedantic)
4371 pedwarn ("invalid storage class for function `%s'", name);
4373 decl = build_decl (FUNCTION_DECL, declarator, type);
4374 decl = build_decl_attribute_variant (decl, decl_attr);
4376 DECL_LANG_SPECIFIC (decl)
4377 = ggc_alloc_cleared (sizeof (struct lang_decl));
4379 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4380 pedwarn ("ISO C forbids qualified function types");
4382 /* GNU C interprets a `volatile void' return type to indicate
4383 that the function does not return. */
4384 if ((type_quals & TYPE_QUAL_VOLATILE)
4385 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4386 warning ("`noreturn' function returns non-void value");
4388 if (extern_ref)
4389 DECL_EXTERNAL (decl) = 1;
4390 /* Record absence of global scope for `static' or `auto'. */
4391 TREE_PUBLIC (decl)
4392 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4394 if (defaulted_int)
4395 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4397 /* Record presence of `inline', if it is reasonable. */
4398 if (MAIN_NAME_P (declarator))
4400 if (inlinep)
4401 warning ("cannot inline function `main'");
4403 else if (inlinep)
4405 /* Assume that otherwise the function can be inlined. */
4406 DECL_DECLARED_INLINE_P (decl) = 1;
4408 /* Do not mark bare declarations as DECL_INLINE. Doing so
4409 in the presence of multiple declarations can result in
4410 the abstract origin pointing between the declarations,
4411 which will confuse dwarf2out. */
4412 if (initialized)
4414 DECL_INLINE (decl) = 1;
4415 if (specbits & (1 << (int) RID_EXTERN))
4416 current_extern_inline = 1;
4419 /* If -finline-functions, assume it can be inlined. This does
4420 two things: let the function be deferred until it is actually
4421 needed, and let dwarf2 know that the function is inlinable. */
4422 else if (flag_inline_trees == 2 && initialized)
4424 if (!DECL_INLINE (decl))
4425 DID_INLINE_FUNC (decl) = 1;
4426 DECL_INLINE (decl) = 1;
4427 DECL_DECLARED_INLINE_P (decl) = 0;
4430 else
4432 /* It's a variable. */
4433 /* An uninitialized decl with `extern' is a reference. */
4434 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4436 /* Move type qualifiers down to element of an array. */
4437 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4439 int saved_align = TYPE_ALIGN(type);
4440 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4441 type_quals),
4442 TYPE_DOMAIN (type));
4443 TYPE_ALIGN (type) = saved_align;
4445 else if (type_quals)
4446 type = c_build_qualified_type (type, type_quals);
4448 /* It is invalid to create an `extern' declaration for a
4449 variable if there is a global declaration that is
4450 `static'. */
4451 if (extern_ref && current_binding_level != global_binding_level)
4453 tree global_decl;
4455 global_decl = identifier_global_value (declarator);
4456 if (global_decl
4457 && TREE_CODE (global_decl) == VAR_DECL
4458 && !TREE_PUBLIC (global_decl))
4459 error ("variable previously declared `static' redeclared "
4460 "`extern'");
4463 decl = build_decl (VAR_DECL, declarator, type);
4464 if (size_varies)
4465 C_DECL_VARIABLE_SIZE (decl) = 1;
4467 if (inlinep)
4468 pedwarn ("%Hvariable '%D' declared `inline'",
4469 &DECL_SOURCE_LOCATION (decl), decl);
4471 DECL_EXTERNAL (decl) = extern_ref;
4473 /* At top level, the presence of a `static' or `register' storage
4474 class specifier, or the absence of all storage class specifiers
4475 makes this declaration a definition (perhaps tentative). Also,
4476 the absence of both `static' and `register' makes it public. */
4477 if (current_binding_level == global_binding_level)
4479 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4480 | (1 << (int) RID_REGISTER)));
4481 TREE_STATIC (decl) = !extern_ref;
4483 /* Not at top level, only `static' makes a static definition. */
4484 else
4486 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4487 TREE_PUBLIC (decl) = extern_ref;
4490 if (specbits & 1 << (int) RID_THREAD)
4492 if (targetm.have_tls)
4493 DECL_THREAD_LOCAL (decl) = 1;
4494 else
4495 /* A mere warning is sure to result in improper semantics
4496 at runtime. Don't bother to allow this to compile. */
4497 error ("thread-local storage not supported for this target");
4501 /* Record `register' declaration for warnings on &
4502 and in case doing stupid register allocation. */
4504 if (specbits & (1 << (int) RID_REGISTER))
4505 DECL_REGISTER (decl) = 1;
4507 /* Record constancy and volatility. */
4508 c_apply_type_quals_to_decl (type_quals, decl);
4510 /* If a type has volatile components, it should be stored in memory.
4511 Otherwise, the fact that those components are volatile
4512 will be ignored, and would even crash the compiler. */
4513 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4514 c_mark_addressable (decl);
4516 decl_attributes (&decl, returned_attrs, 0);
4518 return decl;
4522 /* Decode the parameter-list info for a function type or function definition.
4523 The argument is the value returned by `get_parm_info' (or made in parse.y
4524 if there is an identifier list instead of a parameter decl list).
4525 These two functions are separate because when a function returns
4526 or receives functions then each is called multiple times but the order
4527 of calls is different. The last call to `grokparms' is always the one
4528 that contains the formal parameter names of a function definition.
4530 Store in `last_function_parms' a chain of the decls of parms.
4531 Also store in `last_function_parm_tags' a chain of the struct, union,
4532 and enum tags declared among the parms.
4534 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4536 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4537 a mere declaration. A nonempty identifier-list gets an error message
4538 when FUNCDEF_FLAG is zero. */
4540 static tree
4541 grokparms (tree parms_info, int funcdef_flag)
4543 tree first_parm = TREE_CHAIN (parms_info);
4545 last_function_parms = TREE_PURPOSE (parms_info);
4546 last_function_parm_tags = TREE_VALUE (parms_info);
4548 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4549 && !in_system_header)
4550 warning ("function declaration isn't a prototype");
4552 if (first_parm != 0
4553 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4555 if (! funcdef_flag)
4556 pedwarn ("parameter names (without types) in function declaration");
4558 last_function_parms = first_parm;
4559 return 0;
4561 else
4563 tree parm;
4564 tree typelt;
4565 /* If the arg types are incomplete in a declaration,
4566 they must include undefined tags.
4567 These tags can never be defined in the scope of the declaration,
4568 so the types can never be completed,
4569 and no call can be compiled successfully. */
4570 for (parm = last_function_parms, typelt = first_parm;
4571 parm;
4572 parm = TREE_CHAIN (parm))
4573 /* Skip over any enumeration constants declared here. */
4574 if (TREE_CODE (parm) == PARM_DECL)
4576 /* Barf if the parameter itself has an incomplete type. */
4577 tree type = TREE_VALUE (typelt);
4578 if (type == error_mark_node)
4579 continue;
4580 if (!COMPLETE_TYPE_P (type))
4582 if (funcdef_flag && DECL_NAME (parm) != 0)
4583 error ("parameter `%s' has incomplete type",
4584 IDENTIFIER_POINTER (DECL_NAME (parm)));
4585 else
4586 warning ("parameter has incomplete type");
4587 if (funcdef_flag)
4589 TREE_VALUE (typelt) = error_mark_node;
4590 TREE_TYPE (parm) = error_mark_node;
4593 typelt = TREE_CHAIN (typelt);
4596 return first_parm;
4600 /* Return a tree_list node with info on a parameter list just parsed.
4601 The TREE_PURPOSE is a chain of decls of those parms.
4602 The TREE_VALUE is a list of structure, union and enum tags defined.
4603 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4604 This tree_list node is later fed to `grokparms'.
4606 VOID_AT_END nonzero means append `void' to the end of the type-list.
4607 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4609 tree
4610 get_parm_info (int void_at_end)
4612 tree decl, t;
4613 tree types = 0;
4614 int erred = 0;
4615 tree tags = gettags ();
4616 tree parms = getdecls ();
4617 tree new_parms = 0;
4618 tree order = current_binding_level->parm_order;
4620 /* Just `void' (and no ellipsis) is special. There are really no parms.
4621 But if the `void' is qualified (by `const' or `volatile') or has a
4622 storage class specifier (`register'), then the behavior is undefined;
4623 by not counting it as the special case of `void' we will cause an
4624 error later. Typedefs for `void' are OK (see DR#157). */
4625 if (void_at_end && parms != 0
4626 && TREE_CHAIN (parms) == 0
4627 && VOID_TYPE_P (TREE_TYPE (parms))
4628 && ! TREE_THIS_VOLATILE (parms)
4629 && ! TREE_READONLY (parms)
4630 && ! DECL_REGISTER (parms)
4631 && DECL_NAME (parms) == 0)
4633 parms = NULL_TREE;
4634 storedecls (NULL_TREE);
4635 return tree_cons (NULL_TREE, NULL_TREE,
4636 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4639 /* Extract enumerator values and other non-parms declared with the parms.
4640 Likewise any forward parm decls that didn't have real parm decls. */
4641 for (decl = parms; decl;)
4643 tree next = TREE_CHAIN (decl);
4645 if (TREE_CODE (decl) != PARM_DECL)
4647 TREE_CHAIN (decl) = new_parms;
4648 new_parms = decl;
4650 else if (TREE_ASM_WRITTEN (decl))
4652 error ("%Hparameter '%D' has just a forward declaration",
4653 &DECL_SOURCE_LOCATION (decl), decl);
4654 TREE_CHAIN (decl) = new_parms;
4655 new_parms = decl;
4657 decl = next;
4660 /* Put the parm decls back in the order they were in in the parm list. */
4661 for (t = order; t; t = TREE_CHAIN (t))
4663 if (TREE_CHAIN (t))
4664 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4665 else
4666 TREE_CHAIN (TREE_VALUE (t)) = 0;
4669 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4670 new_parms);
4672 /* Store the parmlist in the binding level since the old one
4673 is no longer a valid list. (We have changed the chain pointers.) */
4674 storedecls (new_parms);
4676 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4677 /* There may also be declarations for enumerators if an enumeration
4678 type is declared among the parms. Ignore them here. */
4679 if (TREE_CODE (decl) == PARM_DECL)
4681 /* Since there is a prototype,
4682 args are passed in their declared types. */
4683 tree type = TREE_TYPE (decl);
4684 DECL_ARG_TYPE (decl) = type;
4685 if (PROMOTE_PROTOTYPES
4686 && INTEGRAL_TYPE_P (type)
4687 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4688 DECL_ARG_TYPE (decl) = integer_type_node;
4690 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4691 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4692 && DECL_NAME (decl) == 0)
4694 error ("`void' in parameter list must be the entire list");
4695 erred = 1;
4699 if (void_at_end)
4700 return tree_cons (new_parms, tags,
4701 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4703 return tree_cons (new_parms, tags, nreverse (types));
4706 /* At end of parameter list, warn about any struct, union or enum tags
4707 defined within. Do so because these types cannot ever become complete. */
4709 void
4710 parmlist_tags_warning (void)
4712 tree elt;
4713 static int already;
4715 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4717 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4718 /* An anonymous union parm type is meaningful as a GNU extension.
4719 So don't warn for that. */
4720 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4721 continue;
4722 if (TREE_PURPOSE (elt) != 0)
4724 if (code == RECORD_TYPE)
4725 warning ("`struct %s' declared inside parameter list",
4726 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4727 else if (code == UNION_TYPE)
4728 warning ("`union %s' declared inside parameter list",
4729 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4730 else
4731 warning ("`enum %s' declared inside parameter list",
4732 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4734 else
4736 /* For translation these need to be separate warnings */
4737 if (code == RECORD_TYPE)
4738 warning ("anonymous struct declared inside parameter list");
4739 else if (code == UNION_TYPE)
4740 warning ("anonymous union declared inside parameter list");
4741 else
4742 warning ("anonymous enum declared inside parameter list");
4744 if (! already)
4746 warning ("its scope is only this definition or declaration, which is probably not what you want");
4747 already = 1;
4752 /* Get the struct, enum or union (CODE says which) with tag NAME.
4753 Define the tag as a forward-reference if it is not defined. */
4755 tree
4756 xref_tag (enum tree_code code, tree name)
4758 /* If a cross reference is requested, look up the type
4759 already defined for this tag and return it. */
4761 tree ref = lookup_tag (code, name, 0);
4762 /* If this is the right type of tag, return what we found.
4763 (This reference will be shadowed by shadow_tag later if appropriate.)
4764 If this is the wrong type of tag, do not return it. If it was the
4765 wrong type in the same binding level, we will have had an error
4766 message already; if in a different binding level and declaring
4767 a name, pending_xref_error will give an error message; but if in a
4768 different binding level and not declaring a name, this tag should
4769 shadow the previous declaration of a different type of tag, and
4770 this would not work properly if we return the reference found.
4771 (For example, with "struct foo" in an outer scope, "union foo;"
4772 must shadow that tag with a new one of union type.) */
4773 if (ref && TREE_CODE (ref) == code)
4774 return ref;
4776 /* If no such tag is yet defined, create a forward-reference node
4777 and record it as the "definition".
4778 When a real declaration of this type is found,
4779 the forward-reference will be altered into a real type. */
4781 ref = make_node (code);
4782 if (code == ENUMERAL_TYPE)
4784 /* Give the type a default layout like unsigned int
4785 to avoid crashing if it does not get defined. */
4786 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4787 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4788 TYPE_USER_ALIGN (ref) = 0;
4789 TREE_UNSIGNED (ref) = 1;
4790 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4791 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4792 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4795 pushtag (name, ref);
4797 return ref;
4800 /* Make sure that the tag NAME is defined *in the current binding level*
4801 at least as a forward reference.
4802 CODE says which kind of tag NAME ought to be. */
4804 tree
4805 start_struct (enum tree_code code, tree name)
4807 /* If there is already a tag defined at this binding level
4808 (as a forward reference), just return it. */
4810 tree ref = 0;
4812 if (name != 0)
4813 ref = lookup_tag (code, name, 1);
4814 if (ref && TREE_CODE (ref) == code)
4816 if (TYPE_FIELDS (ref))
4818 if (code == UNION_TYPE)
4819 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4820 else
4821 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4824 else
4826 /* Otherwise create a forward-reference just so the tag is in scope. */
4828 ref = make_node (code);
4829 pushtag (name, ref);
4832 C_TYPE_BEING_DEFINED (ref) = 1;
4833 TYPE_PACKED (ref) = flag_pack_struct;
4834 return ref;
4837 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4838 of a structure component, returning a FIELD_DECL node.
4839 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4841 This is done during the parsing of the struct declaration.
4842 The FIELD_DECL nodes are chained together and the lot of them
4843 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4845 tree
4846 grokfield (tree declarator, tree declspecs, tree width)
4848 tree value;
4850 if (declarator == NULL_TREE && width == NULL_TREE)
4852 /* This is an unnamed decl.
4854 If we have something of the form "union { list } ;" then this
4855 is the anonymous union extension. Similarly for struct.
4857 If this is something of the form "struct foo;", then
4858 If MS extensions are enabled, this is handled as an
4859 anonymous struct.
4860 Otherwise this is a forward declaration of a structure tag.
4862 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4863 If MS extensions are enabled and foo names a structure, then
4864 again this is an anonymous struct.
4865 Otherwise this is an error.
4867 Oh what a horrid tangled web we weave. I wonder if MS consciously
4868 took this from Plan 9 or if it was an accident of implementation
4869 that took root before someone noticed the bug... */
4871 tree type = TREE_VALUE (declspecs);
4873 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4874 type = TREE_TYPE (type);
4875 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4877 if (flag_ms_extensions)
4878 ; /* ok */
4879 else if (flag_iso)
4880 goto warn_unnamed_field;
4881 else if (TYPE_NAME (type) == NULL)
4882 ; /* ok */
4883 else
4884 goto warn_unnamed_field;
4886 else
4888 warn_unnamed_field:
4889 warning ("declaration does not declare anything");
4890 return NULL_TREE;
4894 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4896 finish_decl (value, NULL_TREE, NULL_TREE);
4897 DECL_INITIAL (value) = width;
4899 if (c_dialect_objc ())
4900 objc_check_decl (value);
4901 return value;
4904 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4905 the list such that this does not present a problem later. */
4907 static void
4908 detect_field_duplicates (tree fieldlist)
4910 tree x, y;
4911 int timeout = 10;
4913 /* First, see if there are more than "a few" fields.
4914 This is trivially true if there are zero or one fields. */
4915 if (!fieldlist)
4916 return;
4917 x = TREE_CHAIN (fieldlist);
4918 if (!x)
4919 return;
4920 do {
4921 timeout--;
4922 x = TREE_CHAIN (x);
4923 } while (timeout > 0 && x);
4925 /* If there were "few" fields, avoid the overhead of allocating
4926 a hash table. Instead just do the nested traversal thing. */
4927 if (timeout > 0)
4929 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4930 if (DECL_NAME (x))
4932 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4933 if (DECL_NAME (y) == DECL_NAME (x))
4935 error ("%Hduplicate member '%D'",
4936 &DECL_SOURCE_LOCATION (x), x);
4937 DECL_NAME (x) = NULL_TREE;
4941 else
4943 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4944 void **slot;
4946 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4947 if ((y = DECL_NAME (x)) != 0)
4949 slot = htab_find_slot (htab, y, INSERT);
4950 if (*slot)
4952 error ("%Hduplicate member '%D'",
4953 &DECL_SOURCE_LOCATION (x), x);
4954 DECL_NAME (x) = NULL_TREE;
4956 *slot = y;
4959 htab_delete (htab);
4963 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4964 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4965 ATTRIBUTES are attributes to be applied to the structure. */
4967 tree
4968 finish_struct (tree t, tree fieldlist, tree attributes)
4970 tree x;
4971 int toplevel = global_binding_level == current_binding_level;
4972 int saw_named_field;
4974 /* If this type was previously laid out as a forward reference,
4975 make sure we lay it out again. */
4977 TYPE_SIZE (t) = 0;
4979 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4981 /* Nameless union parm types are useful as GCC extension. */
4982 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4983 /* Otherwise, warn about any struct or union def. in parmlist. */
4984 if (in_parm_level_p ())
4986 if (pedantic)
4987 pedwarn ("%s defined inside parms",
4988 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4989 else
4990 warning ("%s defined inside parms",
4991 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4994 if (pedantic)
4996 for (x = fieldlist; x; x = TREE_CHAIN (x))
4997 if (DECL_NAME (x) != 0)
4998 break;
5000 if (x == 0)
5001 pedwarn ("%s has no %s",
5002 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5003 fieldlist ? _("named members") : _("members"));
5006 /* Install struct as DECL_CONTEXT of each field decl.
5007 Also process specified field sizes,m which is found in the DECL_INITIAL.
5008 Store 0 there, except for ": 0" fields (so we can find them
5009 and delete them, below). */
5011 saw_named_field = 0;
5012 for (x = fieldlist; x; x = TREE_CHAIN (x))
5014 DECL_CONTEXT (x) = t;
5015 DECL_PACKED (x) |= TYPE_PACKED (t);
5017 /* If any field is const, the structure type is pseudo-const. */
5018 if (TREE_READONLY (x))
5019 C_TYPE_FIELDS_READONLY (t) = 1;
5020 else
5022 /* A field that is pseudo-const makes the structure likewise. */
5023 tree t1 = TREE_TYPE (x);
5024 while (TREE_CODE (t1) == ARRAY_TYPE)
5025 t1 = TREE_TYPE (t1);
5026 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5027 && C_TYPE_FIELDS_READONLY (t1))
5028 C_TYPE_FIELDS_READONLY (t) = 1;
5031 /* Any field that is volatile means variables of this type must be
5032 treated in some ways as volatile. */
5033 if (TREE_THIS_VOLATILE (x))
5034 C_TYPE_FIELDS_VOLATILE (t) = 1;
5036 /* Any field of nominal variable size implies structure is too. */
5037 if (C_DECL_VARIABLE_SIZE (x))
5038 C_TYPE_VARIABLE_SIZE (t) = 1;
5040 /* Detect invalid nested redefinition. */
5041 if (TREE_TYPE (x) == t)
5042 error ("nested redefinition of `%s'",
5043 IDENTIFIER_POINTER (TYPE_NAME (t)));
5045 /* Detect invalid bit-field size. */
5046 if (DECL_INITIAL (x))
5047 STRIP_NOPS (DECL_INITIAL (x));
5048 if (DECL_INITIAL (x))
5050 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5051 constant_expression_warning (DECL_INITIAL (x));
5052 else
5054 error ("%Hbit-field '%D' width not an integer constant",
5055 &DECL_SOURCE_LOCATION (x), x);
5056 DECL_INITIAL (x) = NULL;
5060 /* Detect invalid bit-field type. */
5061 if (DECL_INITIAL (x)
5062 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5063 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5064 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5066 error ("%Hbit-field '%D' has invalid type",
5067 &DECL_SOURCE_LOCATION (x), x);
5068 DECL_INITIAL (x) = NULL;
5071 if (DECL_INITIAL (x) && pedantic
5072 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5073 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5074 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5075 /* Accept an enum that's equivalent to int or unsigned int. */
5076 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5077 && (TYPE_PRECISION (TREE_TYPE (x))
5078 == TYPE_PRECISION (integer_type_node))))
5079 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
5080 &DECL_SOURCE_LOCATION (x), x);
5082 /* Detect and ignore out of range field width and process valid
5083 field widths. */
5084 if (DECL_INITIAL (x))
5086 int max_width
5087 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5088 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5090 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5091 error ("%Hnegative width in bit-field '%D'",
5092 &DECL_SOURCE_LOCATION (x), x);
5093 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5094 pedwarn ("%Hwidth of '%D' exceeds its type",
5095 &DECL_SOURCE_LOCATION (x), x);
5096 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5097 error ("%Hzero width for bit-field '%D'",
5098 &DECL_SOURCE_LOCATION (x), x);
5099 else
5101 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5102 unsigned HOST_WIDE_INT width
5103 = tree_low_cst (DECL_INITIAL (x), 1);
5105 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5106 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5107 TREE_UNSIGNED (TREE_TYPE (x)))
5108 || (width
5109 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5110 TREE_UNSIGNED (TREE_TYPE (x))))))
5111 warning ("%H'%D' is narrower than values of its type",
5112 &DECL_SOURCE_LOCATION (x), x);
5114 DECL_SIZE (x) = bitsize_int (width);
5115 DECL_BIT_FIELD (x) = 1;
5116 SET_DECL_C_BIT_FIELD (x);
5120 DECL_INITIAL (x) = 0;
5122 /* Detect flexible array member in an invalid context. */
5123 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5124 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5125 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5126 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5128 if (TREE_CODE (t) == UNION_TYPE)
5129 error ("%Hflexible array member in union",
5130 &DECL_SOURCE_LOCATION (x));
5131 else if (TREE_CHAIN (x) != NULL_TREE)
5132 error ("%Hflexible array member not at end of struct",
5133 &DECL_SOURCE_LOCATION (x));
5134 else if (! saw_named_field)
5135 error ("%Hflexible array member in otherwise empty struct",
5136 &DECL_SOURCE_LOCATION (x));
5139 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5140 && flexible_array_type_p (TREE_TYPE (x)))
5141 pedwarn ("%Hinvalid use of structure with flexible array member",
5142 &DECL_SOURCE_LOCATION (x));
5144 if (DECL_NAME (x))
5145 saw_named_field = 1;
5148 detect_field_duplicates (fieldlist);
5150 /* Now we have the nearly final fieldlist. Record it,
5151 then lay out the structure or union (including the fields). */
5153 TYPE_FIELDS (t) = fieldlist;
5155 layout_type (t);
5157 /* Delete all zero-width bit-fields from the fieldlist */
5159 tree *fieldlistp = &fieldlist;
5160 while (*fieldlistp)
5161 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5162 *fieldlistp = TREE_CHAIN (*fieldlistp);
5163 else
5164 fieldlistp = &TREE_CHAIN (*fieldlistp);
5167 /* Now we have the truly final field list.
5168 Store it in this type and in the variants. */
5170 TYPE_FIELDS (t) = fieldlist;
5172 /* If there are lots of fields, sort so we can look through them fast.
5173 We arbitrarily consider 16 or more elts to be "a lot". */
5176 int len = 0;
5178 for (x = fieldlist; x; x = TREE_CHAIN (x))
5180 if (len > 15 || DECL_NAME (x) == NULL)
5181 break;
5182 len += 1;
5185 if (len > 15)
5187 tree *field_array;
5188 struct lang_type *space;
5189 struct sorted_fields_type *space2;
5191 len += list_length (x);
5193 /* Use the same allocation policy here that make_node uses, to
5194 ensure that this lives as long as the rest of the struct decl.
5195 All decls in an inline function need to be saved. */
5197 space = ggc_alloc (sizeof (struct lang_type));
5198 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5200 len = 0;
5201 space->s = space2;
5202 field_array = &space2->elts[0];
5203 for (x = fieldlist; x; x = TREE_CHAIN (x))
5205 field_array[len++] = x;
5207 /* if there is anonymous struct or union break out of the loop */
5208 if (DECL_NAME (x) == NULL)
5209 break;
5211 /* found no anonymous struct/union add the TYPE_LANG_SPECIFIC. */
5212 if (x == NULL)
5214 TYPE_LANG_SPECIFIC (t) = space;
5215 TYPE_LANG_SPECIFIC (t)->s->len = len;
5216 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5217 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5222 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5224 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5225 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5226 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5227 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5230 /* If this was supposed to be a transparent union, but we can't
5231 make it one, warn and turn off the flag. */
5232 if (TREE_CODE (t) == UNION_TYPE
5233 && TYPE_TRANSPARENT_UNION (t)
5234 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5236 TYPE_TRANSPARENT_UNION (t) = 0;
5237 warning ("union cannot be made transparent");
5240 /* If this structure or union completes the type of any previous
5241 variable declaration, lay it out and output its rtl. */
5243 if (current_binding_level->incomplete_list != NULL_TREE)
5245 tree prev = NULL_TREE;
5247 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5249 tree decl = TREE_VALUE (x);
5251 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5252 && TREE_CODE (decl) != TYPE_DECL)
5254 layout_decl (decl, 0);
5255 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5256 if (c_dialect_objc ())
5257 objc_check_decl (decl);
5258 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5259 if (! toplevel)
5260 expand_decl (decl);
5261 /* Unlink X from the incomplete list. */
5262 if (prev)
5263 TREE_CHAIN (prev) = TREE_CHAIN (x);
5264 else
5265 current_binding_level->incomplete_list = TREE_CHAIN (x);
5267 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5268 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5270 tree element = TREE_TYPE (decl);
5271 while (TREE_CODE (element) == ARRAY_TYPE)
5272 element = TREE_TYPE (element);
5273 if (element == t)
5275 layout_array_type (TREE_TYPE (decl));
5276 if (TREE_CODE (decl) != TYPE_DECL)
5278 layout_decl (decl, 0);
5279 if (c_dialect_objc ())
5280 objc_check_decl (decl);
5281 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5282 if (! toplevel)
5283 expand_decl (decl);
5285 /* Unlink X from the incomplete list. */
5286 if (prev)
5287 TREE_CHAIN (prev) = TREE_CHAIN (x);
5288 else
5289 current_binding_level->incomplete_list = TREE_CHAIN (x);
5295 /* Finish debugging output for this type. */
5296 rest_of_type_compilation (t, toplevel);
5298 return t;
5301 /* Lay out the type T, and its element type, and so on. */
5303 static void
5304 layout_array_type (tree t)
5306 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5307 layout_array_type (TREE_TYPE (t));
5308 layout_type (t);
5311 /* Begin compiling the definition of an enumeration type.
5312 NAME is its name (or null if anonymous).
5313 Returns the type object, as yet incomplete.
5314 Also records info about it so that build_enumerator
5315 may be used to declare the individual values as they are read. */
5317 tree
5318 start_enum (tree name)
5320 tree enumtype = 0;
5322 /* If this is the real definition for a previous forward reference,
5323 fill in the contents in the same object that used to be the
5324 forward reference. */
5326 if (name != 0)
5327 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5329 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5331 enumtype = make_node (ENUMERAL_TYPE);
5332 pushtag (name, enumtype);
5335 C_TYPE_BEING_DEFINED (enumtype) = 1;
5337 if (TYPE_VALUES (enumtype) != 0)
5339 /* This enum is a named one that has been declared already. */
5340 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5342 /* Completely replace its old definition.
5343 The old enumerators remain defined, however. */
5344 TYPE_VALUES (enumtype) = 0;
5347 enum_next_value = integer_zero_node;
5348 enum_overflow = 0;
5350 if (flag_short_enums)
5351 TYPE_PACKED (enumtype) = 1;
5353 return enumtype;
5356 /* After processing and defining all the values of an enumeration type,
5357 install their decls in the enumeration type and finish it off.
5358 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5359 and ATTRIBUTES are the specified attributes.
5360 Returns ENUMTYPE. */
5362 tree
5363 finish_enum (tree enumtype, tree values, tree attributes)
5365 tree pair, tem;
5366 tree minnode = 0, maxnode = 0, enum_value_type;
5367 int precision, unsign;
5368 int toplevel = (global_binding_level == current_binding_level);
5370 if (in_parm_level_p ())
5371 warning ("enum defined inside parms");
5373 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5375 /* Calculate the maximum value of any enumerator in this type. */
5377 if (values == error_mark_node)
5378 minnode = maxnode = integer_zero_node;
5379 else
5381 minnode = maxnode = TREE_VALUE (values);
5382 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5384 tree value = TREE_VALUE (pair);
5385 if (tree_int_cst_lt (maxnode, value))
5386 maxnode = value;
5387 if (tree_int_cst_lt (value, minnode))
5388 minnode = value;
5392 /* Construct the final type of this enumeration. It is the same
5393 as one of the integral types - the narrowest one that fits, except
5394 that normally we only go as narrow as int - and signed iff any of
5395 the values are negative. */
5396 unsign = (tree_int_cst_sgn (minnode) >= 0);
5397 precision = MAX (min_precision (minnode, unsign),
5398 min_precision (maxnode, unsign));
5399 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5401 tree narrowest = c_common_type_for_size (precision, unsign);
5402 if (narrowest == 0)
5404 warning ("enumeration values exceed range of largest integer");
5405 narrowest = long_long_integer_type_node;
5408 precision = TYPE_PRECISION (narrowest);
5410 else
5411 precision = TYPE_PRECISION (integer_type_node);
5413 if (precision == TYPE_PRECISION (integer_type_node))
5414 enum_value_type = c_common_type_for_size (precision, 0);
5415 else
5416 enum_value_type = enumtype;
5418 TYPE_MIN_VALUE (enumtype) = minnode;
5419 TYPE_MAX_VALUE (enumtype) = maxnode;
5420 TYPE_PRECISION (enumtype) = precision;
5421 TREE_UNSIGNED (enumtype) = unsign;
5422 TYPE_SIZE (enumtype) = 0;
5423 layout_type (enumtype);
5425 if (values != error_mark_node)
5427 /* Change the type of the enumerators to be the enum type. We
5428 need to do this irrespective of the size of the enum, for
5429 proper type checking. Replace the DECL_INITIALs of the
5430 enumerators, and the value slots of the list, with copies
5431 that have the enum type; they cannot be modified in place
5432 because they may be shared (e.g. integer_zero_node) Finally,
5433 change the purpose slots to point to the names of the decls. */
5434 for (pair = values; pair; pair = TREE_CHAIN (pair))
5436 tree enu = TREE_PURPOSE (pair);
5438 TREE_TYPE (enu) = enumtype;
5440 /* The ISO C Standard mandates enumerators to have type int,
5441 even though the underlying type of an enum type is
5442 unspecified. Here we convert any enumerators that fit in
5443 an int to type int, to avoid promotions to unsigned types
5444 when comparing integers with enumerators that fit in the
5445 int range. When -pedantic is given, build_enumerator()
5446 would have already taken care of those that don't fit. */
5447 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5448 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5449 else
5450 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5452 TREE_PURPOSE (pair) = DECL_NAME (enu);
5453 TREE_VALUE (pair) = DECL_INITIAL (enu);
5456 TYPE_VALUES (enumtype) = values;
5459 /* Fix up all variant types of this enum type. */
5460 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5462 if (tem == enumtype)
5463 continue;
5464 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5465 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5466 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5467 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5468 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5469 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5470 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5471 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5472 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5473 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5476 /* Finish debugging output for this type. */
5477 rest_of_type_compilation (enumtype, toplevel);
5479 return enumtype;
5482 /* Build and install a CONST_DECL for one value of the
5483 current enumeration type (one that was begun with start_enum).
5484 Return a tree-list containing the CONST_DECL and its value.
5485 Assignment of sequential values by default is handled here. */
5487 tree
5488 build_enumerator (tree name, tree value)
5490 tree decl, type;
5492 /* Validate and default VALUE. */
5494 /* Remove no-op casts from the value. */
5495 if (value)
5496 STRIP_TYPE_NOPS (value);
5498 if (value != 0)
5500 if (TREE_CODE (value) == INTEGER_CST)
5502 value = default_conversion (value);
5503 constant_expression_warning (value);
5505 else
5507 error ("enumerator value for `%s' not integer constant",
5508 IDENTIFIER_POINTER (name));
5509 value = 0;
5513 /* Default based on previous value. */
5514 /* It should no longer be possible to have NON_LVALUE_EXPR
5515 in the default. */
5516 if (value == 0)
5518 value = enum_next_value;
5519 if (enum_overflow)
5520 error ("overflow in enumeration values");
5523 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5525 pedwarn ("ISO C restricts enumerator values to range of `int'");
5526 value = convert (integer_type_node, value);
5529 /* Set basis for default for next value. */
5530 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5531 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5533 /* Now create a declaration for the enum value name. */
5535 type = TREE_TYPE (value);
5536 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5537 TYPE_PRECISION (integer_type_node)),
5538 (TYPE_PRECISION (type)
5539 >= TYPE_PRECISION (integer_type_node)
5540 && TREE_UNSIGNED (type)));
5542 decl = build_decl (CONST_DECL, name, type);
5543 DECL_INITIAL (decl) = convert (type, value);
5544 pushdecl (decl);
5546 return tree_cons (decl, value, NULL_TREE);
5550 /* Create the FUNCTION_DECL for a function definition.
5551 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5552 the declaration; they describe the function's name and the type it returns,
5553 but twisted together in a fashion that parallels the syntax of C.
5555 This function creates a binding context for the function body
5556 as well as setting up the FUNCTION_DECL in current_function_decl.
5558 Returns 1 on success. If the DECLARATOR is not suitable for a function
5559 (it defines a datum instead), we return 0, which tells
5560 yyparse to report a parse error. */
5563 start_function (tree declspecs, tree declarator, tree attributes)
5565 tree decl1, old_decl;
5566 tree restype;
5567 int old_immediate_size_expand = immediate_size_expand;
5569 current_function_returns_value = 0; /* Assume, until we see it does. */
5570 current_function_returns_null = 0;
5571 current_function_returns_abnormally = 0;
5572 warn_about_return_type = 0;
5573 current_extern_inline = 0;
5574 named_labels = 0;
5575 shadowed_labels = 0;
5577 /* Don't expand any sizes in the return type of the function. */
5578 immediate_size_expand = 0;
5580 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5582 /* If the declarator is not suitable for a function definition,
5583 cause a syntax error. */
5584 if (decl1 == 0)
5586 immediate_size_expand = old_immediate_size_expand;
5587 return 0;
5590 decl_attributes (&decl1, attributes, 0);
5592 /* If #pragma weak was used, mark the decl weak now. */
5593 if (current_binding_level == global_binding_level)
5594 maybe_apply_pragma_weak (decl1);
5596 if (DECL_DECLARED_INLINE_P (decl1)
5597 && DECL_UNINLINABLE (decl1)
5598 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5599 warning ("%Hinline function '%D' given attribute noinline",
5600 &DECL_SOURCE_LOCATION (decl1), decl1);
5602 announce_function (decl1);
5604 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5606 error ("return type is an incomplete type");
5607 /* Make it return void instead. */
5608 TREE_TYPE (decl1)
5609 = build_function_type (void_type_node,
5610 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5613 if (warn_about_return_type)
5614 pedwarn_c99 ("return type defaults to `int'");
5616 /* Save the parm names or decls from this function's declarator
5617 where store_parm_decls will find them. */
5618 current_function_parms = last_function_parms;
5619 current_function_parm_tags = last_function_parm_tags;
5621 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5622 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5623 DECL_INITIAL (decl1) = error_mark_node;
5625 /* If this definition isn't a prototype and we had a prototype declaration
5626 before, copy the arg type info from that prototype.
5627 But not if what we had before was a builtin function. */
5628 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5629 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5630 && !DECL_BUILT_IN (old_decl)
5631 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5632 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5633 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5635 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5636 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5639 /* Optionally warn of old-fashioned def with no previous prototype. */
5640 if (warn_strict_prototypes
5641 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5642 && C_DECL_ISNT_PROTOTYPE (old_decl))
5643 warning ("function declaration isn't a prototype");
5644 /* Optionally warn of any global def with no previous prototype. */
5645 else if (warn_missing_prototypes
5646 && TREE_PUBLIC (decl1)
5647 && ! MAIN_NAME_P (DECL_NAME (decl1))
5648 && C_DECL_ISNT_PROTOTYPE (old_decl))
5649 warning ("%Hno previous prototype for '%D'",
5650 &DECL_SOURCE_LOCATION (decl1), decl1);
5651 /* Optionally warn of any def with no previous prototype
5652 if the function has already been used. */
5653 else if (warn_missing_prototypes
5654 && old_decl != 0 && TREE_USED (old_decl)
5655 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5656 warning ("%H'%D' was used with no prototype before its definition",
5657 &DECL_SOURCE_LOCATION (decl1), decl1);
5658 /* Optionally warn of any global def with no previous declaration. */
5659 else if (warn_missing_declarations
5660 && TREE_PUBLIC (decl1)
5661 && old_decl == 0
5662 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5663 warning ("%Hno previous declaration for '%D'",
5664 &DECL_SOURCE_LOCATION (decl1), decl1);
5665 /* Optionally warn of any def with no previous declaration
5666 if the function has already been used. */
5667 else if (warn_missing_declarations
5668 && old_decl != 0 && TREE_USED (old_decl)
5669 && C_DECL_IMPLICIT (old_decl))
5670 warning ("%H`%D' was used with no declaration before its definition",
5671 &DECL_SOURCE_LOCATION (decl1), decl1);
5673 /* This is a definition, not a reference.
5674 So normally clear DECL_EXTERNAL.
5675 However, `extern inline' acts like a declaration
5676 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5677 DECL_EXTERNAL (decl1) = current_extern_inline;
5679 /* This function exists in static storage.
5680 (This does not mean `static' in the C sense!) */
5681 TREE_STATIC (decl1) = 1;
5683 /* A nested function is not global. */
5684 if (current_function_decl != 0)
5685 TREE_PUBLIC (decl1) = 0;
5687 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5688 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5690 tree args;
5691 int argct = 0;
5692 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5694 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5695 != integer_type_node)
5696 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5698 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5699 args = TREE_CHAIN (args))
5701 tree type = args ? TREE_VALUE (args) : 0;
5703 if (type == void_type_node)
5704 break;
5706 ++argct;
5707 switch (argct)
5709 case 1:
5710 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5711 pedwarn ("%Hfirst argument of '%D' should be `int'",
5712 locus, decl1);
5713 break;
5715 case 2:
5716 if (TREE_CODE (type) != POINTER_TYPE
5717 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5718 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5719 != char_type_node))
5720 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5721 locus, decl1);
5722 break;
5724 case 3:
5725 if (TREE_CODE (type) != POINTER_TYPE
5726 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5727 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5728 != char_type_node))
5729 pedwarn ("%Hthird argument of '%D' should probably be "
5730 "'char **'", locus, decl1);
5731 break;
5735 /* It is intentional that this message does not mention the third
5736 argument because it's only mentioned in an appendix of the
5737 standard. */
5738 if (argct > 0 && (argct < 2 || argct > 3))
5739 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5741 if (! TREE_PUBLIC (decl1))
5742 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5745 /* Record the decl so that the function name is defined.
5746 If we already have a decl for this name, and it is a FUNCTION_DECL,
5747 use the old decl. */
5749 current_function_decl = pushdecl (decl1);
5751 pushlevel (0);
5752 declare_parm_level ();
5754 make_decl_rtl (current_function_decl, NULL);
5756 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5757 /* Promote the value to int before returning it. */
5758 if (c_promoting_integer_type_p (restype))
5760 /* It retains unsignedness if not really getting wider. */
5761 if (TREE_UNSIGNED (restype)
5762 && (TYPE_PRECISION (restype)
5763 == TYPE_PRECISION (integer_type_node)))
5764 restype = unsigned_type_node;
5765 else
5766 restype = integer_type_node;
5768 DECL_RESULT (current_function_decl)
5769 = build_decl (RESULT_DECL, NULL_TREE, restype);
5771 /* If this fcn was already referenced via a block-scope `extern' decl
5772 (or an implicit decl), propagate certain information about the usage. */
5773 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5774 TREE_ADDRESSABLE (current_function_decl) = 1;
5776 immediate_size_expand = old_immediate_size_expand;
5778 start_fname_decls ();
5780 return 1;
5783 /* Store the parameter declarations into the current function declaration.
5784 This is called after parsing the parameter declarations, before
5785 digesting the body of the function.
5787 For an old-style definition, modify the function's type
5788 to specify at least the number of arguments. */
5790 void
5791 store_parm_decls (void)
5793 tree fndecl = current_function_decl;
5794 tree parm;
5796 /* This is either a chain of PARM_DECLs (if a prototype was used)
5797 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5798 tree specparms = current_function_parms;
5800 /* This is a list of types declared among parms in a prototype. */
5801 tree parmtags = current_function_parm_tags;
5803 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5804 tree parmdecls = getdecls ();
5806 /* This is a chain of any other decls that came in among the parm
5807 declarations. If a parm is declared with enum {foo, bar} x;
5808 then CONST_DECLs for foo and bar are put here. */
5809 tree nonparms = 0;
5811 /* The function containing FNDECL, if any. */
5812 tree context = decl_function_context (fndecl);
5814 /* Nonzero if this definition is written with a prototype. */
5815 int prototype = 0;
5817 bool saved_warn_shadow = warn_shadow;
5819 /* Don't re-emit shadow warnings. */
5820 warn_shadow = false;
5822 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5824 /* This case is when the function was defined with an ANSI prototype.
5825 The parms already have decls, so we need not do anything here
5826 except record them as in effect
5827 and complain if any redundant old-style parm decls were written. */
5829 tree next;
5830 tree others = 0;
5832 prototype = 1;
5834 if (parmdecls != 0)
5836 tree decl, link;
5838 error ("%Hparm types given both in parmlist and separately",
5839 &DECL_SOURCE_LOCATION (fndecl));
5840 /* Get rid of the erroneous decls; don't keep them on
5841 the list of parms, since they might not be PARM_DECLs. */
5842 for (decl = current_binding_level->names;
5843 decl; decl = TREE_CHAIN (decl))
5844 if (DECL_NAME (decl))
5845 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = 0;
5846 for (link = current_binding_level->shadowed;
5847 link; link = TREE_CHAIN (link))
5848 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5849 current_binding_level->names = 0;
5850 current_binding_level->shadowed = 0;
5853 specparms = nreverse (specparms);
5854 for (parm = specparms; parm; parm = next)
5856 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5857 next = TREE_CHAIN (parm);
5858 if (TREE_CODE (parm) == PARM_DECL)
5860 if (DECL_NAME (parm) == 0)
5861 error ("%Hparameter name omitted", locus);
5862 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5863 && VOID_TYPE_P (TREE_TYPE (parm)))
5865 error ("%Hparameter '%D' declared void", locus, parm);
5866 /* Change the type to error_mark_node so this parameter
5867 will be ignored by assign_parms. */
5868 TREE_TYPE (parm) = error_mark_node;
5870 pushdecl (parm);
5872 else
5874 /* If we find an enum constant or a type tag,
5875 put it aside for the moment. */
5876 TREE_CHAIN (parm) = 0;
5877 others = chainon (others, parm);
5881 /* Get the decls in their original chain order
5882 and record in the function. */
5883 DECL_ARGUMENTS (fndecl) = getdecls ();
5885 /* Now pushdecl the enum constants. */
5886 for (parm = others; parm; parm = next)
5888 next = TREE_CHAIN (parm);
5889 if (DECL_NAME (parm) == 0)
5891 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5893 else if (TREE_CODE (parm) != PARM_DECL)
5894 pushdecl (parm);
5897 storetags (chainon (parmtags, gettags ()));
5899 else
5901 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5902 each with a parm name as the TREE_VALUE.
5904 PARMDECLS is a chain of declarations for parameters.
5905 Warning! It can also contain CONST_DECLs which are not parameters
5906 but are names of enumerators of any enum types
5907 declared among the parameters.
5909 First match each formal parameter name with its declaration.
5910 Associate decls with the names and store the decls
5911 into the TREE_PURPOSE slots. */
5913 /* We use DECL_WEAK as a flag to show which parameters have been
5914 seen already since it is not used on PARM_DECL or CONST_DECL. */
5915 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5916 DECL_WEAK (parm) = 0;
5918 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5920 tree tail, found = NULL;
5922 if (TREE_VALUE (parm) == 0)
5924 error ("%Hparameter name missing from parameter list",
5925 &DECL_SOURCE_LOCATION (fndecl));
5926 TREE_PURPOSE (parm) = 0;
5927 continue;
5930 /* See if any of the parmdecls specifies this parm by name.
5931 Ignore any enumerator decls. */
5932 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5933 if (DECL_NAME (tail) == TREE_VALUE (parm)
5934 && TREE_CODE (tail) == PARM_DECL)
5936 found = tail;
5937 break;
5940 /* If declaration already marked, we have a duplicate name.
5941 Complain, and don't use this decl twice. */
5942 if (found && DECL_WEAK (found))
5944 error ("%Hmultiple parameters named '%D'",
5945 &DECL_SOURCE_LOCATION (found), found);
5946 found = 0;
5949 /* If the declaration says "void", complain and ignore it. */
5950 if (found && VOID_TYPE_P (TREE_TYPE (found)))
5952 error ("%Hparameter '%D' declared void",
5953 &DECL_SOURCE_LOCATION (found), found);
5954 TREE_TYPE (found) = integer_type_node;
5955 DECL_ARG_TYPE (found) = integer_type_node;
5956 layout_decl (found, 0);
5959 /* If no declaration found, default to int. */
5960 if (!found)
5962 found = build_decl (PARM_DECL, TREE_VALUE (parm),
5963 integer_type_node);
5964 DECL_ARG_TYPE (found) = TREE_TYPE (found);
5965 DECL_SOURCE_LOCATION (found) = DECL_SOURCE_LOCATION (fndecl);
5966 if (flag_isoc99)
5967 pedwarn ("%Htype of '%D' defaults to `int'",
5968 &DECL_SOURCE_LOCATION (found), found);
5969 else if (extra_warnings)
5970 warning ("%Htype of '%D' defaults to `int'",
5971 &DECL_SOURCE_LOCATION (found), found);
5972 pushdecl (found);
5975 TREE_PURPOSE (parm) = found;
5977 /* Mark this decl as "already found". */
5978 DECL_WEAK (found) = 1;
5981 /* Put anything which is on the parmdecls chain and which is
5982 not a PARM_DECL onto the list NONPARMS. (The types of
5983 non-parm things which might appear on the list include
5984 enumerators and NULL-named TYPE_DECL nodes.) Complain about
5985 any actual PARM_DECLs not matched with any names. */
5987 nonparms = 0;
5988 for (parm = parmdecls; parm;)
5990 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5991 tree next = TREE_CHAIN (parm);
5992 TREE_CHAIN (parm) = 0;
5994 if (TREE_CODE (parm) != PARM_DECL)
5995 nonparms = chainon (nonparms, parm);
5996 else
5998 /* Complain about args with incomplete types. */
5999 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6001 error ("%Hparameter '%D' has incomplete type", locus, parm);
6002 TREE_TYPE (parm) = error_mark_node;
6005 if (! DECL_WEAK (parm))
6007 error ("%Hdeclaration for parameter '%D' but no such "
6008 "parameter", locus, parm);
6009 /* Pretend the parameter was not missing.
6010 This gets us to a standard state and minimizes
6011 further error messages. */
6012 specparms
6013 = chainon (specparms,
6014 tree_cons (parm, NULL_TREE, NULL_TREE));
6018 parm = next;
6021 /* Chain the declarations together in the order of the list of
6022 names. Store that chain in the function decl, replacing the
6023 list of names. */
6024 parm = specparms;
6025 DECL_ARGUMENTS (fndecl) = 0;
6027 tree last;
6028 for (last = 0; parm; parm = TREE_CHAIN (parm))
6029 if (TREE_PURPOSE (parm))
6031 if (last == 0)
6032 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
6033 else
6034 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6035 last = TREE_PURPOSE (parm);
6036 TREE_CHAIN (last) = 0;
6040 /* If there was a previous prototype,
6041 set the DECL_ARG_TYPE of each argument according to
6042 the type previously specified, and report any mismatches. */
6044 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6046 tree type;
6047 for (parm = DECL_ARGUMENTS (fndecl),
6048 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6049 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6050 != void_type_node));
6051 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6053 if (parm == 0 || type == 0
6054 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6056 error ("number of arguments doesn't match prototype");
6057 error ("%Hprototype declaration",
6058 &current_function_prototype_locus);
6059 break;
6061 /* Type for passing arg must be consistent with that
6062 declared for the arg. ISO C says we take the unqualified
6063 type for parameters declared with qualified type. */
6064 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6065 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6066 COMPARE_STRICT))
6068 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6069 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6071 /* Adjust argument to match prototype. E.g. a previous
6072 `int foo(float);' prototype causes
6073 `int foo(x) float x; {...}' to be treated like
6074 `int foo(float x) {...}'. This is particularly
6075 useful for argument types like uid_t. */
6076 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6078 if (PROMOTE_PROTOTYPES
6079 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6080 && TYPE_PRECISION (TREE_TYPE (parm))
6081 < TYPE_PRECISION (integer_type_node))
6082 DECL_ARG_TYPE (parm) = integer_type_node;
6084 if (pedantic)
6086 pedwarn ("promoted argument `%s' doesn't match prototype",
6087 IDENTIFIER_POINTER (DECL_NAME (parm)));
6088 warning ("%Hprototype declaration",
6089 &current_function_prototype_locus);
6092 else
6094 error ("argument `%s' doesn't match prototype",
6095 IDENTIFIER_POINTER (DECL_NAME (parm)));
6096 error ("%Hprototype declaration",
6097 &current_function_prototype_locus);
6101 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6104 /* Otherwise, create a prototype that would match. */
6106 else
6108 tree actual = 0, last = 0, type;
6110 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6112 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6113 if (last)
6114 TREE_CHAIN (last) = type;
6115 else
6116 actual = type;
6117 last = type;
6119 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6120 if (last)
6121 TREE_CHAIN (last) = type;
6122 else
6123 actual = type;
6125 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6126 of the type of this function, but we need to avoid having this
6127 affect the types of other similarly-typed functions, so we must
6128 first force the generation of an identical (but separate) type
6129 node for the relevant function type. The new node we create
6130 will be a variant of the main variant of the original function
6131 type. */
6133 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6135 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6138 /* Now store the final chain of decls for the arguments
6139 as the decl-chain of the current lexical scope.
6140 Put the enumerators in as well, at the front so that
6141 DECL_ARGUMENTS is not modified. */
6143 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6146 /* Make sure the binding level for the top of the function body
6147 gets a BLOCK if there are any in the function.
6148 Otherwise, the dbx output is wrong. */
6150 keep_next_if_subblocks = 1;
6152 /* Write a record describing this function definition to the prototypes
6153 file (if requested). */
6155 gen_aux_info_record (fndecl, 1, 0, prototype);
6157 /* Initialize the RTL code for the function. */
6158 init_function_start (fndecl);
6160 /* Begin the statement tree for this function. */
6161 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6163 /* If this is a nested function, save away the sizes of any
6164 variable-size types so that we can expand them when generating
6165 RTL. */
6166 if (context)
6168 tree t;
6170 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6171 = nreverse (get_pending_sizes ());
6172 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6174 t = TREE_CHAIN (t))
6175 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6178 /* This function is being processed in whole-function mode. */
6179 cfun->x_whole_function_mode_p = 1;
6181 /* Even though we're inside a function body, we still don't want to
6182 call expand_expr to calculate the size of a variable-sized array.
6183 We haven't necessarily assigned RTL to all variables yet, so it's
6184 not safe to try to expand expressions involving them. */
6185 immediate_size_expand = 0;
6186 cfun->x_dont_save_pending_sizes_p = 1;
6188 warn_shadow = saved_warn_shadow;
6191 /* Finish up a function declaration and compile that function
6192 all the way to assembler language output. The free the storage
6193 for the function definition.
6195 This is called after parsing the body of the function definition.
6197 NESTED is nonzero if the function being finished is nested in another.
6198 CAN_DEFER_P is nonzero if the function may be deferred. */
6200 void
6201 finish_function (int nested, int can_defer_p)
6203 tree fndecl = current_function_decl;
6205 /* When a function declaration is totally empty, e.g.
6206 void foo(void) { }
6207 (the argument list is irrelevant) the compstmt rule will not
6208 bother calling pushlevel/poplevel, which means we get here with
6209 the binding_level stack out of sync. Detect this situation by
6210 noticing that the current_binding_level is still as
6211 store_parm_decls left it, and do a dummy push/pop to get back to
6212 consistency. Note that the call to pushlevel does not actually
6213 push another binding level - see there for details. */
6214 if (current_binding_level->parm_flag && keep_next_if_subblocks)
6216 pushlevel (0);
6217 poplevel (1, 0, 1);
6220 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6222 /* Must mark the RESULT_DECL as being in this function. */
6224 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6226 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6228 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6229 != integer_type_node)
6231 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6232 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6233 if (!warn_main)
6234 pedwarn ("%Hreturn type of '%D' is not `int'",
6235 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6237 else
6239 #ifdef DEFAULT_MAIN_RETURN
6240 /* Make it so that `main' always returns success by default. */
6241 DEFAULT_MAIN_RETURN;
6242 #else
6243 if (flag_isoc99)
6244 c_expand_return (integer_zero_node);
6245 #endif
6249 finish_fname_decls ();
6251 /* Tie off the statement tree for this function. */
6252 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6254 /* Complain if there's just no return statement. */
6255 if (warn_return_type
6256 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6257 && !current_function_returns_value && !current_function_returns_null
6258 /* Don't complain if we abort. */
6259 && !current_function_returns_abnormally
6260 /* Don't warn for main(). */
6261 && !MAIN_NAME_P (DECL_NAME (fndecl))
6262 /* Or if they didn't actually specify a return type. */
6263 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6264 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6265 inline function, as we might never be compiled separately. */
6266 && DECL_INLINE (fndecl))
6267 warning ("no return statement in function returning non-void");
6269 /* Clear out memory we no longer need. */
6270 free_after_parsing (cfun);
6271 /* Since we never call rest_of_compilation, we never clear
6272 CFUN. Do so explicitly. */
6273 free_after_compilation (cfun);
6274 cfun = NULL;
6276 if (flag_unit_at_a_time && can_defer_p)
6278 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6279 current_function_decl = NULL;
6280 return;
6283 if (! nested)
6285 /* Function is parsed.
6286 Generate RTL for the body of this function or defer
6287 it for later expansion. */
6288 int uninlinable = 1;
6290 /* There's no reason to do any of the work here if we're only doing
6291 semantic analysis; this code just generates RTL. */
6292 if (flag_syntax_only)
6294 current_function_decl = NULL;
6295 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6296 return;
6299 if (flag_inline_trees)
6301 /* First, cache whether the current function is inlinable. Some
6302 predicates depend on cfun and current_function_decl to
6303 function completely. */
6304 timevar_push (TV_INTEGRATION);
6305 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6307 if (can_defer_p
6308 /* We defer functions marked inline *even if* the function
6309 itself is not inlinable. This is because we don't yet
6310 know if the function will actually be used; we may be
6311 able to avoid emitting it entirely. */
6312 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6313 /* Save function tree for inlining. Should return 0 if the
6314 language does not support function deferring or the
6315 function could not be deferred. */
6316 && defer_fn (fndecl))
6318 /* Let the back-end know that this function exists. */
6319 (*debug_hooks->deferred_inline_function) (fndecl);
6320 timevar_pop (TV_INTEGRATION);
6321 current_function_decl = NULL;
6322 return;
6325 /* Then, inline any functions called in it. */
6326 optimize_inline_calls (fndecl);
6327 timevar_pop (TV_INTEGRATION);
6330 c_expand_body (fndecl);
6332 /* Keep the function body if it's needed for inlining or dumping. */
6333 if (uninlinable && !dump_enabled_p (TDI_all))
6335 /* Allow the body of the function to be garbage collected. */
6336 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6339 /* Let the error reporting routines know that we're outside a
6340 function. For a nested function, this value is used in
6341 c_pop_function_context and then reset via pop_function_context. */
6342 current_function_decl = NULL;
6346 /* Generate the RTL for a deferred function FNDECL. */
6348 void
6349 c_expand_deferred_function (tree fndecl)
6351 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6352 function was deferred, e.g. in duplicate_decls. */
6353 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6355 if (flag_inline_trees)
6357 timevar_push (TV_INTEGRATION);
6358 optimize_inline_calls (fndecl);
6359 timevar_pop (TV_INTEGRATION);
6361 c_expand_body (fndecl);
6362 current_function_decl = NULL;
6366 /* Called to move the SAVE_EXPRs for parameter declarations in a
6367 nested function into the nested function. DATA is really the
6368 nested FUNCTION_DECL. */
6370 static tree
6371 set_save_expr_context (tree *tp,
6372 int *walk_subtrees,
6373 void *data)
6375 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6376 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6377 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6378 circularity. */
6379 else if (DECL_P (*tp))
6380 *walk_subtrees = 0;
6382 return NULL_TREE;
6385 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6386 then we are already in the process of generating RTL for another
6387 function. If can_defer_p is zero, we won't attempt to defer the
6388 generation of RTL. */
6390 static void
6391 c_expand_body_1 (tree fndecl, int nested_p)
6393 timevar_push (TV_EXPAND);
6395 if (nested_p)
6397 /* Make sure that we will evaluate variable-sized types involved
6398 in our function's type. */
6399 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6400 /* Squirrel away our current state. */
6401 push_function_context ();
6404 /* Initialize the RTL code for the function. */
6405 current_function_decl = fndecl;
6406 input_location = DECL_SOURCE_LOCATION (fndecl);
6407 init_function_start (fndecl);
6409 /* This function is being processed in whole-function mode. */
6410 cfun->x_whole_function_mode_p = 1;
6412 /* Even though we're inside a function body, we still don't want to
6413 call expand_expr to calculate the size of a variable-sized array.
6414 We haven't necessarily assigned RTL to all variables yet, so it's
6415 not safe to try to expand expressions involving them. */
6416 immediate_size_expand = 0;
6417 cfun->x_dont_save_pending_sizes_p = 1;
6419 /* Set up parameters and prepare for return, for the function. */
6420 expand_function_start (fndecl, 0);
6422 /* If the function has a variably modified type, there may be
6423 SAVE_EXPRs in the parameter types. Their context must be set to
6424 refer to this function; they cannot be expanded in the containing
6425 function. */
6426 if (decl_function_context (fndecl)
6427 && variably_modified_type_p (TREE_TYPE (fndecl)))
6428 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6429 NULL);
6431 /* If this function is `main', emit a call to `__main'
6432 to run global initializers, etc. */
6433 if (DECL_NAME (fndecl)
6434 && MAIN_NAME_P (DECL_NAME (fndecl))
6435 && DECL_CONTEXT (fndecl) == NULL_TREE)
6436 expand_main_function ();
6438 /* Generate the RTL for this function. */
6439 expand_stmt (DECL_SAVED_TREE (fndecl));
6441 /* We hard-wired immediate_size_expand to zero above.
6442 expand_function_end will decrement this variable. So, we set the
6443 variable to one here, so that after the decrement it will remain
6444 zero. */
6445 immediate_size_expand = 1;
6447 /* Allow language dialects to perform special processing. */
6448 if (lang_expand_function_end)
6449 (*lang_expand_function_end) ();
6451 /* Generate rtl for function exit. */
6452 expand_function_end ();
6454 /* If this is a nested function, protect the local variables in the stack
6455 above us from being collected while we're compiling this function. */
6456 if (nested_p)
6457 ggc_push_context ();
6459 /* Run the optimizers and output the assembler code for this function. */
6460 rest_of_compilation (fndecl);
6462 /* Undo the GC context switch. */
6463 if (nested_p)
6464 ggc_pop_context ();
6466 /* With just -Wextra, complain only if function returns both with
6467 and without a value. */
6468 if (extra_warnings
6469 && current_function_returns_value
6470 && current_function_returns_null)
6471 warning ("this function may return with or without a value");
6473 /* If requested, warn about function definitions where the function will
6474 return a value (usually of some struct or union type) which itself will
6475 take up a lot of stack space. */
6477 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6479 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6481 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6482 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6483 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6484 larger_than_size))
6486 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
6487 unsigned int size_as_int
6488 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6490 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6491 warning ("%Hsize of return value of '%D' is %u bytes",
6492 locus, fndecl, size_as_int);
6493 else
6494 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6495 locus, fndecl, larger_than_size);
6499 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6500 && ! flag_inline_trees)
6502 /* Stop pointing to the local nodes about to be freed.
6503 But DECL_INITIAL must remain nonzero so we know this
6504 was an actual function definition.
6505 For a nested function, this is done in c_pop_function_context.
6506 If rest_of_compilation set this to 0, leave it 0. */
6507 if (DECL_INITIAL (fndecl) != 0)
6508 DECL_INITIAL (fndecl) = error_mark_node;
6510 DECL_ARGUMENTS (fndecl) = 0;
6513 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6515 if (targetm.have_ctors_dtors)
6516 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6517 DEFAULT_INIT_PRIORITY);
6518 else
6519 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6522 if (DECL_STATIC_DESTRUCTOR (fndecl))
6524 if (targetm.have_ctors_dtors)
6525 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6526 DEFAULT_INIT_PRIORITY);
6527 else
6528 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6531 if (nested_p)
6532 /* Return to the enclosing function. */
6533 pop_function_context ();
6534 timevar_pop (TV_EXPAND);
6537 /* Like c_expand_body_1 but only for unnested functions. */
6539 void
6540 c_expand_body (tree fndecl)
6542 c_expand_body_1 (fndecl, 0);
6545 /* Check the declarations given in a for-loop for satisfying the C99
6546 constraints. */
6547 void
6548 check_for_loop_decls (void)
6550 tree t;
6552 if (!flag_isoc99)
6554 /* If we get here, declarations have been used in a for loop without
6555 the C99 for loop scope. This doesn't make much sense, so don't
6556 allow it. */
6557 error ("`for' loop initial declaration used outside C99 mode");
6558 return;
6560 /* C99 subclause 6.8.5 paragraph 3:
6562 [#3] The declaration part of a for statement shall only
6563 declare identifiers for objects having storage class auto or
6564 register.
6566 It isn't clear whether, in this sentence, "identifiers" binds to
6567 "shall only declare" or to "objects" - that is, whether all identifiers
6568 declared must be identifiers for objects, or whether the restriction
6569 only applies to those that are. (A question on this in comp.std.c
6570 in November 2000 received no answer.) We implement the strictest
6571 interpretation, to avoid creating an extension which later causes
6572 problems. */
6574 for (t = gettags (); t; t = TREE_CHAIN (t))
6576 if (TREE_PURPOSE (t) != 0)
6578 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6580 if (code == RECORD_TYPE)
6581 error ("`struct %s' declared in `for' loop initial declaration",
6582 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6583 else if (code == UNION_TYPE)
6584 error ("`union %s' declared in `for' loop initial declaration",
6585 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6586 else
6587 error ("`enum %s' declared in `for' loop initial declaration",
6588 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6592 for (t = getdecls (); t; t = TREE_CHAIN (t))
6594 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6595 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6596 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6597 "initial declaration", locus, t);
6598 else if (TREE_STATIC (t))
6599 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6600 "initial declaration", locus, t);
6601 else if (DECL_EXTERNAL (t))
6602 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6603 "initial declaration", locus, t);
6607 /* Save and restore the variables in this file and elsewhere
6608 that keep track of the progress of compilation of the current function.
6609 Used for nested functions. */
6611 struct language_function GTY(())
6613 struct c_language_function base;
6614 tree named_labels;
6615 tree shadowed_labels;
6616 int returns_value;
6617 int returns_null;
6618 int returns_abnormally;
6619 int warn_about_return_type;
6620 int extern_inline;
6621 struct binding_level *binding_level;
6624 /* Save and reinitialize the variables
6625 used during compilation of a C function. */
6627 void
6628 c_push_function_context (struct function *f)
6630 struct language_function *p;
6631 p = ggc_alloc (sizeof (struct language_function));
6632 f->language = p;
6634 p->base.x_stmt_tree = c_stmt_tree;
6635 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6636 p->named_labels = named_labels;
6637 p->shadowed_labels = shadowed_labels;
6638 p->returns_value = current_function_returns_value;
6639 p->returns_null = current_function_returns_null;
6640 p->returns_abnormally = current_function_returns_abnormally;
6641 p->warn_about_return_type = warn_about_return_type;
6642 p->extern_inline = current_extern_inline;
6643 p->binding_level = current_binding_level;
6646 /* Restore the variables used during compilation of a C function. */
6648 void
6649 c_pop_function_context (struct function *f)
6651 struct language_function *p = f->language;
6652 tree link;
6654 /* Bring back all the labels that were shadowed. */
6655 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6656 if (DECL_NAME (TREE_VALUE (link)) != 0)
6657 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6658 = TREE_VALUE (link);
6660 if (DECL_SAVED_INSNS (current_function_decl) == 0
6661 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6663 /* Stop pointing to the local nodes about to be freed. */
6664 /* But DECL_INITIAL must remain nonzero so we know this
6665 was an actual function definition. */
6666 DECL_INITIAL (current_function_decl) = error_mark_node;
6667 DECL_ARGUMENTS (current_function_decl) = 0;
6670 c_stmt_tree = p->base.x_stmt_tree;
6671 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6672 named_labels = p->named_labels;
6673 shadowed_labels = p->shadowed_labels;
6674 current_function_returns_value = p->returns_value;
6675 current_function_returns_null = p->returns_null;
6676 current_function_returns_abnormally = p->returns_abnormally;
6677 warn_about_return_type = p->warn_about_return_type;
6678 current_extern_inline = p->extern_inline;
6679 current_binding_level = p->binding_level;
6681 f->language = NULL;
6684 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6686 void
6687 c_dup_lang_specific_decl (tree decl)
6689 struct lang_decl *ld;
6691 if (!DECL_LANG_SPECIFIC (decl))
6692 return;
6694 ld = ggc_alloc (sizeof (struct lang_decl));
6695 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6696 DECL_LANG_SPECIFIC (decl) = ld;
6699 /* The functions below are required for functionality of doing
6700 function at once processing in the C front end. Currently these
6701 functions are not called from anywhere in the C front end, but as
6702 these changes continue, that will change. */
6704 /* Returns nonzero if the current statement is a full expression,
6705 i.e. temporaries created during that statement should be destroyed
6706 at the end of the statement. */
6709 stmts_are_full_exprs_p (void)
6711 return 0;
6714 /* Returns the stmt_tree (if any) to which statements are currently
6715 being added. If there is no active statement-tree, NULL is
6716 returned. */
6718 stmt_tree
6719 current_stmt_tree (void)
6721 return &c_stmt_tree;
6724 /* Returns the stack of SCOPE_STMTs for the current function. */
6726 tree *
6727 current_scope_stmt_stack (void)
6729 return &c_scope_stmt_stack;
6732 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6733 C. */
6736 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6738 return 0;
6741 /* Dummy function in place of callback used by C++. */
6743 void
6744 extract_interface_info (void)
6748 /* Return a new COMPOUND_STMT, after adding it to the current
6749 statement tree. */
6751 tree
6752 c_begin_compound_stmt (void)
6754 tree stmt;
6756 /* Create the COMPOUND_STMT. */
6757 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6759 return stmt;
6762 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6763 common code. */
6765 void
6766 c_expand_decl_stmt (tree t)
6768 tree decl = DECL_STMT_DECL (t);
6770 /* Expand nested functions. */
6771 if (TREE_CODE (decl) == FUNCTION_DECL
6772 && DECL_CONTEXT (decl) == current_function_decl
6773 && DECL_SAVED_TREE (decl))
6774 c_expand_body_1 (decl, 1);
6777 /* Return the global value of T as a symbol. */
6779 tree
6780 identifier_global_value (tree t)
6782 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6783 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6784 return decl;
6786 /* Shadowed by something else; find the true global value. */
6787 for (decl = global_binding_level->names; decl; decl = TREE_CHAIN (decl))
6788 if (DECL_NAME (decl) == t)
6789 return decl;
6791 /* Only local values for this decl. */
6792 return 0;
6795 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6796 otherwise the name is found in ridpointers from RID_INDEX. */
6798 void
6799 record_builtin_type (enum rid rid_index, const char *name, tree type)
6801 tree id;
6802 if (name == 0)
6803 id = ridpointers[(int) rid_index];
6804 else
6805 id = get_identifier (name);
6806 pushdecl (build_decl (TYPE_DECL, id, type));
6809 /* Build the void_list_node (void_type_node having been created). */
6810 tree
6811 build_void_list_node (void)
6813 tree t = build_tree_list (NULL_TREE, void_type_node);
6814 return t;
6817 /* Return something to represent absolute declarators containing a *.
6818 TARGET is the absolute declarator that the * contains.
6819 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6820 to apply to the pointer type, represented as identifiers, possible mixed
6821 with attributes.
6823 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6824 if attributes are present) and whose type is the modifier list. */
6826 tree
6827 make_pointer_declarator (tree type_quals_attrs, tree target)
6829 tree quals, attrs;
6830 tree itarget = target;
6831 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6832 if (attrs != NULL_TREE)
6833 itarget = tree_cons (attrs, target, NULL_TREE);
6834 return build1 (INDIRECT_REF, quals, itarget);
6837 /* Hash and equality functions for link_hash_table: key off
6838 DECL_ASSEMBLER_NAME. */
6840 static hashval_t
6841 link_hash_hash (const void *x_p)
6843 tree x = (tree)x_p;
6844 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6847 static int
6848 link_hash_eq (const void *x1_p, const void *x2_p)
6850 tree x1 = (tree)x1_p;
6851 tree x2 = (tree)x2_p;
6852 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6855 /* Propagate information between definitions and uses between multiple
6856 translation units in TU_LIST based on linkage rules. */
6858 void
6859 merge_translation_unit_decls (void)
6861 const tree tu_list = current_file_decl;
6862 tree tu;
6863 tree decl;
6864 htab_t link_hash_table;
6865 tree block;
6867 /* Create the BLOCK that poplevel would have created, but don't
6868 actually call poplevel since that's expensive. */
6869 block = make_node (BLOCK);
6870 BLOCK_VARS (block) = current_binding_level->names;
6871 TREE_USED (block) = 1;
6872 DECL_INITIAL (current_file_decl) = block;
6874 /* If only one translation unit seen, no copying necessary. */
6875 if (TREE_CHAIN (tu_list) == NULL_TREE)
6876 return;
6878 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6880 /* Enter any actual definitions into the hash table. */
6881 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6882 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6883 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6885 PTR *slot;
6886 slot = htab_find_slot (link_hash_table, decl, INSERT);
6888 /* If we've already got a definition, work out which one is
6889 the real one, put it into the hash table, and make the
6890 other one DECL_EXTERNAL. This is important to avoid
6891 putting out two definitions of the same symbol in the
6892 assembly output. */
6893 if (*slot != NULL)
6895 tree old_decl = (tree) *slot;
6897 /* If this is weak or common or whatever, suppress it
6898 in favour of the other definition. */
6899 if (DECL_WEAK (decl))
6900 DECL_EXTERNAL (decl) = 1;
6901 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6902 DECL_EXTERNAL (old_decl) = 1;
6903 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6904 DECL_EXTERNAL (decl) = 1;
6905 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6906 DECL_EXTERNAL (old_decl) = 1;
6908 if (DECL_EXTERNAL (decl))
6910 DECL_INITIAL (decl) = NULL_TREE;
6911 DECL_COMMON (decl) = 0;
6912 DECL_ONE_ONLY (decl) = 0;
6913 DECL_WEAK (decl) = 0;
6915 else if (DECL_EXTERNAL (old_decl))
6917 DECL_INITIAL (old_decl) = NULL_TREE;
6918 DECL_COMMON (old_decl) = 0;
6919 DECL_ONE_ONLY (old_decl) = 0;
6920 DECL_WEAK (old_decl) = 0;
6921 *slot = decl;
6923 else
6925 error ("%Hredefinition of global '%D'",
6926 &DECL_SOURCE_LOCATION (decl), decl);
6927 error ("%H'%D' previously defined here",
6928 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6931 else
6932 *slot = decl;
6935 /* Now insert the desired information from all the definitions
6936 into any plain declarations. */
6937 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6938 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6939 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6941 tree global_decl;
6942 global_decl = htab_find (link_hash_table, decl);
6944 if (! global_decl)
6945 continue;
6947 /* Print any appropriate error messages, and partially merge
6948 the decls. */
6949 (void) duplicate_decls (decl, global_decl, true, true);
6952 htab_delete (link_hash_table);
6955 /* Perform final processing on file-scope data. */
6957 void
6958 c_write_global_declarations(void)
6960 tree link;
6962 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6964 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6965 int len = list_length (globals);
6966 tree *vec = xmalloc (sizeof (tree) * len);
6967 int i;
6968 tree decl;
6970 /* Process the decls in reverse order--earliest first.
6971 Put them into VEC from back to front, then take out from front. */
6973 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6974 vec[len - i - 1] = decl;
6976 wrapup_global_declarations (vec, len);
6978 check_global_declarations (vec, len);
6980 /* Clean up. */
6981 free (vec);
6985 /* Reset the parser's state in preparation for a new file. */
6987 void
6988 c_reset_state (void)
6990 tree link;
6991 tree file_scope_decl;
6993 /* Pop the global binding level. */
6994 if (current_binding_level != global_binding_level)
6995 current_binding_level = global_binding_level;
6996 file_scope_decl = current_file_decl;
6997 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6998 truly_local_externals = NULL_TREE;
7000 /* Start a new global binding level. */
7001 pushlevel (0);
7002 global_binding_level = current_binding_level;
7003 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
7004 TREE_CHAIN (current_file_decl) = file_scope_decl;
7006 /* Reintroduce the global declarations. */
7007 for (link = builtin_decls; link; link = TREE_CHAIN (link))
7008 pushdecl (copy_node (link));
7011 #include "gt-c-decl.h"