PR c/9177
[official-gcc.git] / gcc / c-decl.c
blobee987858421cc852ba19ec36440184d271b99293
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "timevar.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "cgraph.h"
53 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
54 enum decl_context
55 { NORMAL, /* Ordinary declaration */
56 FUNCDEF, /* Function definition */
57 PARM, /* Declaration of parm before function body */
58 FIELD, /* Declaration inside struct or union */
59 BITFIELD, /* Likewise but with specified width */
60 TYPENAME}; /* Typename (inside cast or sizeof) */
63 /* Nonzero if we have seen an invalid cross reference
64 to a struct, union, or enum, but not yet printed the message. */
66 tree pending_invalid_xref;
67 /* File and line to appear in the eventual error message. */
68 const char *pending_invalid_xref_file;
69 int pending_invalid_xref_line;
71 /* While defining an enum type, this is 1 plus the last enumerator
72 constant value. Note that will do not have to save this or `enum_overflow'
73 around nested function definition since such a definition could only
74 occur in an enum value expression and we don't use these variables in
75 that case. */
77 static tree enum_next_value;
79 /* Nonzero means that there was overflow computing enum_next_value. */
81 static int enum_overflow;
83 /* Parsing a function declarator leaves a list of parameter names
84 or a chain or parameter decls here. */
86 static tree last_function_parms;
88 /* Parsing a function declarator leaves here a chain of structure
89 and enum types declared in the parmlist. */
91 static tree last_function_parm_tags;
93 /* After parsing the declarator that starts a function definition,
94 `start_function' puts here the list of parameter names or chain of decls.
95 `store_parm_decls' finds it here. */
97 static tree current_function_parms;
99 /* Similar, for last_function_parm_tags. */
100 static tree current_function_parm_tags;
102 /* Similar, for the file and line that the prototype came from if this is
103 an old-style definition. */
104 static const char *current_function_prototype_file;
105 static int current_function_prototype_line;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* The current scope statement stack. */
113 static GTY(()) tree c_scope_stmt_stack;
115 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
116 that have names. Here so we can clear out their names' definitions
117 at the end of the function. */
119 static GTY(()) tree named_labels;
121 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
123 static GTY(()) tree shadowed_labels;
125 /* A list of external DECLs that appeared at block scope when there was
126 some other global meaning for that identifier. */
127 static GTY(()) tree truly_local_externals;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement that specifies a return value is seen. */
132 int current_function_returns_value;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a return statement with no argument is seen. */
137 int current_function_returns_null;
139 /* Set to 0 at beginning of a function definition, set to 1 if
140 a call to a noreturn function is seen. */
142 int current_function_returns_abnormally;
144 /* Set to nonzero by `grokdeclarator' for a function
145 whose return type is defaulted, if warnings for this are desired. */
147 static int warn_about_return_type;
149 /* Nonzero when starting a function declared `extern inline'. */
151 static int current_extern_inline;
153 /* For each binding contour we allocate a binding_level structure
154 * which records the names defined in that contour.
155 * Contours include:
156 * 0) the global one
157 * 1) one for each function definition,
158 * where internal declarations of the parameters appear.
159 * 2) one for each compound statement,
160 * to record its declarations.
162 * The current meaning of a name can be found by searching the levels from
163 * the current one out to the global one.
166 struct binding_level GTY(())
168 /* A chain of _DECL nodes for all variables, constants, functions,
169 and typedef types. These are in the reverse of the order supplied.
171 tree names;
173 /* A list of structure, union and enum definitions,
174 * for looking up tag names.
175 * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
176 * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
177 * or ENUMERAL_TYPE node.
179 tree tags;
181 /* For each level, a list of shadowed outer-level definitions
182 to be restored when this level is popped.
183 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
184 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
185 tree shadowed;
187 /* For each level, a list of shadowed outer-level tag definitions
188 to be restored when this level is popped.
189 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
190 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
191 tree shadowed_tags;
193 /* For each level (except not the global one),
194 a chain of BLOCK nodes for all the levels
195 that were entered and exited one level down. */
196 tree blocks;
198 /* The binding level which this one is contained in (inherits from). */
199 struct binding_level *level_chain;
201 /* Nonzero if we are currently filling this level with parameter
202 declarations. */
203 char parm_flag;
205 /* Nonzero if this is the outermost block scope of a function body.
206 This scope contains both the parameters and the local variables
207 declared in the outermost block. */
208 char function_body;
210 /* Nonzero means make a BLOCK for this level regardless of all else. */
211 char keep;
213 /* Nonzero means make a BLOCK if this level has any subblocks. */
214 char keep_if_subblocks;
216 /* List of decls in `names' that have incomplete structure or
217 union types. */
218 tree incomplete_list;
220 /* A list of decls giving the (reversed) specified order of parms,
221 not including any forward-decls in the parmlist.
222 This is so we can put the parms in proper order for assign_parms. */
223 tree parm_order;
226 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
228 /* The binding level currently in effect. */
230 static GTY(()) struct binding_level *current_binding_level;
232 /* A chain of binding_level structures awaiting reuse. */
234 static GTY((deletable (""))) struct binding_level *free_binding_level;
236 /* The outermost binding level, for names of file scope.
237 This is created when the compiler is started and exists
238 through the entire run. */
240 static GTY(()) struct binding_level *global_binding_level;
242 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
244 static int keep_next_level_flag;
246 /* Nonzero means make a BLOCK for the next level pushed
247 if it has subblocks. */
249 static int keep_next_if_subblocks;
251 /* The chain of outer levels of label scopes.
252 This uses the same data structure used for binding levels,
253 but it works differently: each link in the chain records
254 saved values of named_labels and shadowed_labels for
255 a label binding level outside the current one. */
257 static GTY(()) struct binding_level *label_level_chain;
259 /* Functions called automatically at the beginning and end of execution. */
261 tree static_ctors, static_dtors;
263 /* Forward declarations. */
265 static struct binding_level *make_binding_level PARAMS ((void));
266 static void pop_binding_level PARAMS ((struct binding_level **));
267 static int duplicate_decls PARAMS ((tree, tree, int));
268 static int redeclaration_error_message PARAMS ((tree, tree));
269 static void implicit_decl_warning PARAMS ((tree));
270 static void storedecls PARAMS ((tree));
271 static void storetags PARAMS ((tree));
272 static tree lookup_tag PARAMS ((enum tree_code, tree, int));
273 static tree lookup_name_current_level PARAMS ((tree));
274 static tree grokdeclarator PARAMS ((tree, tree, enum decl_context,
275 int));
276 static tree grokparms PARAMS ((tree, int));
277 static void layout_array_type PARAMS ((tree));
278 static tree c_make_fname_decl PARAMS ((tree, int));
279 static void c_expand_body_1 PARAMS ((tree, int));
280 static tree any_external_decl PARAMS ((tree));
281 static void record_external_decl PARAMS ((tree));
282 static void warn_if_shadowing PARAMS ((tree, tree));
283 static void clone_underlying_type PARAMS ((tree));
284 static bool flexible_array_type_p PARAMS ((tree));
286 /* States indicating how grokdeclarator() should handle declspecs marked
287 with __attribute__((deprecated)). An object declared as
288 __attribute__((deprecated)) suppresses warnings of uses of other
289 deprecated items. */
291 enum deprecated_states {
292 DEPRECATED_NORMAL,
293 DEPRECATED_SUPPRESS
296 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
298 void
299 c_print_identifier (file, node, indent)
300 FILE *file;
301 tree node;
302 int indent;
304 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
305 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
306 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
307 if (C_IS_RESERVED_WORD (node))
309 tree rid = ridpointers[C_RID_CODE (node)];
310 indent_to (file, indent + 4);
311 fprintf (file, "rid ");
312 fprintf (file, HOST_PTR_PRINTF, (void *)rid);
313 fprintf (file, " \"%s\"", IDENTIFIER_POINTER (rid));
317 /* Hook called at end of compilation to assume 1 elt
318 for a top-level tentative array defn that wasn't complete before. */
320 void
321 c_finish_incomplete_decl (decl)
322 tree decl;
324 if (TREE_CODE (decl) == VAR_DECL)
326 tree type = TREE_TYPE (decl);
327 if (type != error_mark_node
328 && TREE_CODE (type) == ARRAY_TYPE
329 && ! DECL_EXTERNAL (decl)
330 && TYPE_DOMAIN (type) == 0)
332 warning_with_decl (decl, "array `%s' assumed to have one element");
334 complete_array_type (type, NULL_TREE, 1);
336 layout_decl (decl, 0);
341 /* Reuse or create a struct for this binding level. */
343 static struct binding_level *
344 make_binding_level ()
346 struct binding_level *result;
347 if (free_binding_level)
349 result = free_binding_level;
350 free_binding_level = result->level_chain;
351 memset (result, 0, sizeof(struct binding_level));
353 else
354 result = (struct binding_level *)
355 ggc_alloc_cleared (sizeof (struct binding_level));
357 return result;
360 /* Remove a binding level from a list and add it to the level chain. */
362 static void
363 pop_binding_level (lp)
364 struct binding_level **lp;
366 struct binding_level *l = *lp;
367 *lp = l->level_chain;
369 memset (l, 0, sizeof (struct binding_level));
370 l->level_chain = free_binding_level;
371 free_binding_level = l;
374 /* Nonzero if we are currently in the global binding level. */
377 global_bindings_p ()
379 return current_binding_level == global_binding_level;
382 void
383 keep_next_level ()
385 keep_next_level_flag = 1;
388 /* Nonzero if the current level needs to have a BLOCK made. */
391 kept_level_p ()
393 return ((current_binding_level->keep_if_subblocks
394 && current_binding_level->blocks != 0)
395 || current_binding_level->keep
396 || current_binding_level->names != 0
397 || current_binding_level->tags != 0);
400 /* Identify this binding level as a level of parameters.
401 DEFINITION_FLAG is 1 for a definition, 0 for a declaration.
402 But it turns out there is no way to pass the right value for
403 DEFINITION_FLAG, so we ignore it. */
405 void
406 declare_parm_level (definition_flag)
407 int definition_flag ATTRIBUTE_UNUSED;
409 current_binding_level->parm_flag = 1;
412 /* Nonzero if currently making parm declarations. */
415 in_parm_level_p ()
417 return current_binding_level->parm_flag;
420 /* Enter a new binding level. */
422 void
423 pushlevel (dummy)
424 int dummy ATTRIBUTE_UNUSED;
426 /* If this is the top level of a function, make sure that
427 NAMED_LABELS is 0. */
429 if (current_binding_level == global_binding_level)
430 named_labels = 0;
432 if (keep_next_if_subblocks)
434 /* This is the transition from the parameters to the top level
435 of the function body. These are the same scope
436 (C99 6.2.1p4,6) so we do not push another binding level.
438 XXX Note kludge - keep_next_if_subblocks is set only by
439 store_parm_decls, which in turn is called when and only
440 when we are about to encounter the opening curly brace for
441 the function body. */
442 current_binding_level->parm_flag = 0;
443 current_binding_level->function_body = 1;
444 current_binding_level->keep |= keep_next_level_flag;
445 current_binding_level->keep_if_subblocks = 1;
447 keep_next_level_flag = 0;
448 keep_next_if_subblocks = 0;
450 else
452 struct binding_level *newlevel = make_binding_level ();
454 newlevel->keep = keep_next_level_flag;
455 newlevel->level_chain = current_binding_level;
456 current_binding_level = newlevel;
457 keep_next_level_flag = 0;
461 /* Exit a binding level.
462 Pop the level off, and restore the state of the identifier-decl mappings
463 that were in effect when this level was entered.
465 If KEEP is nonzero, this level had explicit declarations, so
466 and create a "block" (a BLOCK node) for the level
467 to record its declarations and subblocks for symbol table output.
469 If FUNCTIONBODY is nonzero, this level is the body of a function,
470 so create a block as if KEEP were set and also clear out all
471 label names.
473 If REVERSE is nonzero, reverse the order of decls before putting
474 them into the BLOCK. */
476 tree
477 poplevel (keep, reverse, functionbody)
478 int keep;
479 int reverse;
480 int functionbody;
482 tree link;
483 tree block;
484 tree decl;
485 tree decls = current_binding_level->names;
486 tree tags = current_binding_level->tags;
487 tree subblocks = current_binding_level->blocks;
489 functionbody |= current_binding_level->function_body;
490 keep |= (current_binding_level->keep || functionbody
491 || (current_binding_level->keep_if_subblocks && subblocks != 0));
493 /* We used to warn about unused variables in expand_end_bindings,
494 i.e. while generating RTL. But in function-at-a-time mode we may
495 choose to never expand a function at all (e.g. auto inlining), so
496 we do this explicitly now. */
497 warn_about_unused_variables (decls);
499 /* Clear out the name-meanings declared on this level.
500 Propagate TREE_ADDRESSABLE from nested functions to their
501 containing functions. */
502 for (link = decls; link; link = TREE_CHAIN (link))
504 if (DECL_NAME (link) != 0)
506 if (DECL_EXTERNAL (link))
507 /* External decls stay in the symbol-value slot but are
508 inaccessible. */
509 C_DECL_INVISIBLE (link) = 1;
510 else
511 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
514 if (TREE_CODE (link) == FUNCTION_DECL
515 && ! TREE_ASM_WRITTEN (link)
516 && DECL_INITIAL (link) != 0
517 && TREE_ADDRESSABLE (link)
518 && DECL_ABSTRACT_ORIGIN (link) != 0
519 && DECL_ABSTRACT_ORIGIN (link) != link)
520 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
523 /* Clear out the tag-meanings declared on this level. */
524 for (link = tags; link; link = TREE_CHAIN (link))
525 if (TREE_PURPOSE (link))
526 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
528 /* Restore all name-meanings of the outer levels
529 that were shadowed by this level. */
531 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
532 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
534 /* Restore all tag-meanings of the outer levels
535 that were shadowed by this level. */
537 for (link = current_binding_level->shadowed_tags; link;
538 link = TREE_CHAIN (link))
539 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
541 /* If this is the top level block of a function, remove all
542 PARM_DECLs from current_binding_level->names; they are already
543 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
544 not be put in BLOCK_VARS, and furthermore reversing them will
545 cause trouble later. They are all together at the end of the
546 list. */
547 if (functionbody && decls)
549 if (TREE_CODE (decls) == PARM_DECL)
550 decls = 0;
551 else
553 link = decls;
554 while (TREE_CHAIN (link)
555 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
556 link = TREE_CHAIN (link);
558 TREE_CHAIN (link) = 0;
562 /* Get the decls in the order they were written.
563 Usually current_binding_level->names is in reverse order.
564 But parameter decls were previously put in forward order. */
566 if (reverse)
567 decls = nreverse (decls);
569 /* If there were any declarations or structure tags in that level,
570 or if this level is a function body,
571 create a BLOCK to record them for the life of this function. */
573 block = 0;
574 if (keep)
576 block = make_node (BLOCK);
577 BLOCK_VARS (block) = decls;
578 BLOCK_SUBBLOCKS (block) = subblocks;
579 TREE_USED (block) = 1;
582 /* In each subblock, record that this is its superior. */
584 for (link = subblocks; link; link = TREE_CHAIN (link))
585 BLOCK_SUPERCONTEXT (link) = block;
587 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
588 binding contour so that they point to the appropriate construct, i.e.
589 either to the current FUNCTION_DECL node, or else to the BLOCK node
590 we just constructed.
592 Note that for tagged types whose scope is just the formal parameter
593 list for some function type specification, we can't properly set
594 their TYPE_CONTEXTs here, because we don't have a pointer to the
595 appropriate FUNCTION_TYPE node readily available to us. For those
596 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
597 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
598 node which will represent the "scope" for these "parameter list local"
599 tagged types. */
601 decl = functionbody ? current_function_decl : block;
602 if (decl)
603 for (link = tags; link; link = TREE_CHAIN (link))
604 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
606 /* If the level being exited is the top level of a function, check
607 over all the labels, and clear out the current (function local)
608 meanings of their names. Then add them to BLOCK_VARS. */
610 if (functionbody)
612 for (link = named_labels; link; link = TREE_CHAIN (link))
614 tree label = TREE_VALUE (link);
616 if (DECL_INITIAL (label) == 0)
618 error_with_decl (label, "label `%s' used but not defined");
619 /* Avoid crashing later. */
620 define_label (input_filename, lineno,
621 DECL_NAME (label));
623 else if (warn_unused_label && !TREE_USED (label))
624 warning_with_decl (label, "label `%s' defined but not used");
625 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
627 /* Put the labels into the "variables" of the
628 top-level block, so debugger can see them. */
629 TREE_CHAIN (label) = BLOCK_VARS (block);
630 BLOCK_VARS (block) = label;
634 /* Pop the current level, and free the structure for reuse. */
636 pop_binding_level (&current_binding_level);
638 /* Dispose of the block that we just made inside some higher level. */
639 if (functionbody)
640 DECL_INITIAL (current_function_decl) = block;
641 else if (block)
642 current_binding_level->blocks
643 = chainon (current_binding_level->blocks, block);
644 /* If we did not make a block for the level just exited,
645 any blocks made for inner levels
646 (since they cannot be recorded as subblocks in that level)
647 must be carried forward so they will later become subblocks
648 of something else. */
649 else if (subblocks)
650 current_binding_level->blocks
651 = chainon (current_binding_level->blocks, subblocks);
653 return block;
656 /* Insert BLOCK at the end of the list of subblocks of the
657 current binding level. This is used when a BIND_EXPR is expanded,
658 to handle the BLOCK node inside the BIND_EXPR. */
660 void
661 insert_block (block)
662 tree block;
664 TREE_USED (block) = 1;
665 current_binding_level->blocks
666 = chainon (current_binding_level->blocks, block);
669 /* Set the BLOCK node for the innermost scope (the one we are
670 currently in). The RTL expansion machinery requires us to provide
671 this hook, but it is not useful in function-at-a-time mode. */
673 void
674 set_block (block)
675 tree block ATTRIBUTE_UNUSED;
679 void
680 push_label_level ()
682 struct binding_level *newlevel;
684 newlevel = make_binding_level ();
686 /* Add this level to the front of the chain (stack) of label levels. */
688 newlevel->level_chain = label_level_chain;
689 label_level_chain = newlevel;
691 newlevel->names = named_labels;
692 newlevel->shadowed = shadowed_labels;
693 named_labels = 0;
694 shadowed_labels = 0;
697 void
698 pop_label_level ()
700 struct binding_level *level = label_level_chain;
701 tree link, prev;
703 /* Clear out the definitions of the declared labels in this level.
704 Leave in the list any ordinary, non-declared labels. */
705 for (link = named_labels, prev = 0; link;)
707 if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
709 if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
711 error_with_decl (TREE_VALUE (link),
712 "label `%s' used but not defined");
713 /* Avoid crashing later. */
714 define_label (input_filename, lineno,
715 DECL_NAME (TREE_VALUE (link)));
717 else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
718 warning_with_decl (TREE_VALUE (link),
719 "label `%s' defined but not used");
720 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
722 /* Delete this element from the list. */
723 link = TREE_CHAIN (link);
724 if (prev)
725 TREE_CHAIN (prev) = link;
726 else
727 named_labels = link;
729 else
731 prev = link;
732 link = TREE_CHAIN (link);
736 /* Bring back all the labels that were shadowed. */
737 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
738 if (DECL_NAME (TREE_VALUE (link)) != 0)
739 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
740 = TREE_VALUE (link);
742 named_labels = chainon (named_labels, level->names);
743 shadowed_labels = level->shadowed;
745 /* Pop the current level, and free the structure for reuse. */
746 pop_binding_level (&label_level_chain);
749 /* Push a definition or a declaration of struct, union or enum tag "name".
750 "type" should be the type node.
751 We assume that the tag "name" is not already defined.
753 Note that the definition may really be just a forward reference.
754 In that case, the TYPE_SIZE will be zero. */
756 void
757 pushtag (name, type)
758 tree name, type;
760 struct binding_level *b = current_binding_level;
762 if (name)
764 /* Record the identifier as the type's name if it has none. */
766 if (TYPE_NAME (type) == 0)
767 TYPE_NAME (type) = name;
769 if (IDENTIFIER_TAG_VALUE (name))
770 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
771 b->shadowed_tags);
772 IDENTIFIER_TAG_VALUE (name) = type;
775 b->tags = tree_cons (name, type, b->tags);
777 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
778 tagged type we just added to the current binding level. This fake
779 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
780 to output a representation of a tagged type, and it also gives
781 us a convenient place to record the "scope start" address for the
782 tagged type. */
784 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
786 /* An approximation for now, so we can tell this is a function-scope tag.
787 This will be updated in poplevel. */
788 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
791 /* Handle when a new declaration NEWDECL
792 has the same name as an old one OLDDECL
793 in the same binding contour.
794 Prints an error message if appropriate.
796 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
797 Otherwise, return 0.
799 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
800 and OLDDECL is in an outer binding level and should thus not be changed. */
802 static int
803 duplicate_decls (newdecl, olddecl, different_binding_level)
804 tree newdecl, olddecl;
805 int different_binding_level;
807 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
808 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
809 && DECL_INITIAL (newdecl) != 0);
810 tree oldtype = TREE_TYPE (olddecl);
811 tree newtype = TREE_TYPE (newdecl);
812 int errmsg = 0;
814 if (DECL_P (olddecl))
816 if (TREE_CODE (newdecl) == FUNCTION_DECL
817 && TREE_CODE (olddecl) == FUNCTION_DECL
818 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
820 if (DECL_DECLARED_INLINE_P (newdecl)
821 && DECL_UNINLINABLE (newdecl)
822 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
823 /* Already warned elsewhere. */;
824 else if (DECL_DECLARED_INLINE_P (olddecl)
825 && DECL_UNINLINABLE (olddecl)
826 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
827 /* Already warned. */;
828 else if (DECL_DECLARED_INLINE_P (newdecl)
829 && ! DECL_DECLARED_INLINE_P (olddecl)
830 && DECL_UNINLINABLE (olddecl)
831 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
833 warning_with_decl (newdecl,
834 "function `%s' redeclared as inline");
835 warning_with_decl (olddecl,
836 "previous declaration of function `%s' with attribute noinline");
838 else if (DECL_DECLARED_INLINE_P (olddecl)
839 && DECL_UNINLINABLE (newdecl)
840 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
842 warning_with_decl (newdecl,
843 "function `%s' redeclared with attribute noinline");
844 warning_with_decl (olddecl,
845 "previous declaration of function `%s' was inline");
849 DECL_ATTRIBUTES (newdecl)
850 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
853 if (TREE_CODE (newtype) == ERROR_MARK
854 || TREE_CODE (oldtype) == ERROR_MARK)
855 types_match = 0;
857 /* New decl is completely inconsistent with the old one =>
858 tell caller to replace the old one.
859 This is always an error except in the case of shadowing a builtin. */
860 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
862 if (TREE_CODE (olddecl) == FUNCTION_DECL
863 && DECL_BUILT_IN (olddecl))
865 /* If you declare a built-in or predefined function name as static,
866 the old definition is overridden,
867 but optionally warn this was a bad choice of name. */
868 if (!TREE_PUBLIC (newdecl))
870 if (warn_shadow)
871 warning_with_decl (newdecl, "shadowing built-in function `%s'");
873 else
874 warning_with_decl (newdecl,
875 "built-in function `%s' declared as non-function");
877 else
879 error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
880 error_with_decl (olddecl, "previous declaration of `%s'");
883 return 0;
886 /* For real parm decl following a forward decl, return 1 so old decl
887 will be reused. Only allow this to happen once. */
888 if (types_match && TREE_CODE (newdecl) == PARM_DECL
889 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
891 TREE_ASM_WRITTEN (olddecl) = 0;
892 return 1;
895 /* The new declaration is the same kind of object as the old one.
896 The declarations may partially match. Print warnings if they don't
897 match enough. Ultimately, copy most of the information from the new
898 decl to the old one, and keep using the old one. */
900 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
902 /* A function declaration for a built-in function. */
903 if (!TREE_PUBLIC (newdecl))
905 /* If you declare a built-in function name as static, the
906 built-in definition is overridden,
907 but optionally warn this was a bad choice of name. */
908 if (warn_shadow)
909 warning_with_decl (newdecl, "shadowing built-in function `%s'");
910 /* Discard the old built-in function. */
911 return 0;
913 else if (!types_match)
915 /* Accept the return type of the new declaration if same modes. */
916 tree oldreturntype = TREE_TYPE (oldtype);
917 tree newreturntype = TREE_TYPE (newtype);
919 if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
921 /* Function types may be shared, so we can't just modify
922 the return type of olddecl's function type. */
923 tree trytype
924 = build_function_type (newreturntype,
925 TYPE_ARG_TYPES (oldtype));
926 trytype = build_type_attribute_variant (trytype,
927 TYPE_ATTRIBUTES (oldtype));
929 types_match = comptypes (newtype, trytype);
930 if (types_match)
931 oldtype = trytype;
933 /* Accept harmless mismatch in first argument type also.
934 This is for the ffs and fprintf builtins. */
935 if (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0
936 && TYPE_ARG_TYPES (oldtype) != 0
937 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != 0
938 && TREE_VALUE (TYPE_ARG_TYPES (oldtype)) != 0
939 && (TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (newtype)))
940 == TYPE_MODE (TREE_VALUE (TYPE_ARG_TYPES (oldtype)))))
942 /* Function types may be shared, so we can't just modify
943 the return type of olddecl's function type. */
944 tree trytype
945 = build_function_type (TREE_TYPE (oldtype),
946 tree_cons (NULL_TREE,
947 TREE_VALUE (TYPE_ARG_TYPES (newtype)),
948 TREE_CHAIN (TYPE_ARG_TYPES (oldtype))));
949 trytype = build_type_attribute_variant (trytype,
950 TYPE_ATTRIBUTES (oldtype));
952 types_match = comptypes (newtype, trytype);
953 if (types_match)
954 oldtype = trytype;
956 if (! different_binding_level)
957 TREE_TYPE (olddecl) = oldtype;
959 else if (TYPE_ARG_TYPES (oldtype) == NULL
960 && TYPE_ARG_TYPES (newtype) != NULL)
962 /* For bcmp, bzero, fputs the builtin type has arguments not
963 specified. Use the ones from the prototype so that type checking
964 is done for them. */
965 tree trytype
966 = build_function_type (TREE_TYPE (oldtype),
967 TYPE_ARG_TYPES (newtype));
968 trytype = build_type_attribute_variant (trytype,
969 TYPE_ATTRIBUTES (oldtype));
971 oldtype = trytype;
972 if (! different_binding_level)
973 TREE_TYPE (olddecl) = oldtype;
975 if (!types_match)
977 /* If types don't match for a built-in, throw away the built-in. */
978 warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
979 return 0;
982 else if (TREE_CODE (olddecl) == FUNCTION_DECL
983 && DECL_SOURCE_LINE (olddecl) == 0)
985 /* A function declaration for a predeclared function
986 that isn't actually built in. */
987 if (!TREE_PUBLIC (newdecl))
989 /* If you declare it as static, the
990 default definition is overridden. */
991 return 0;
993 else if (!types_match)
995 /* If the types don't match, preserve volatility indication.
996 Later on, we will discard everything else about the
997 default declaration. */
998 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1001 /* Permit char *foo () to match void *foo (...) if not pedantic,
1002 if one of them came from a system header file. */
1003 else if (!types_match
1004 && TREE_CODE (olddecl) == FUNCTION_DECL
1005 && TREE_CODE (newdecl) == FUNCTION_DECL
1006 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
1007 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
1008 && (DECL_IN_SYSTEM_HEADER (olddecl)
1009 || DECL_IN_SYSTEM_HEADER (newdecl))
1010 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
1011 && TYPE_ARG_TYPES (oldtype) == 0
1012 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
1013 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
1015 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
1016 && TYPE_ARG_TYPES (newtype) == 0
1017 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
1018 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
1020 if (pedantic)
1021 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1022 /* Make sure we keep void * as ret type, not char *. */
1023 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
1024 TREE_TYPE (newdecl) = newtype = oldtype;
1026 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
1027 we will come back here again. */
1028 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1030 /* Permit void foo (...) to match int foo (...) if the latter is the
1031 definition and implicit int was used. See c-torture/compile/920625-2.c. */
1032 else if (!types_match && new_is_definition
1033 && TREE_CODE (olddecl) == FUNCTION_DECL
1034 && TREE_CODE (newdecl) == FUNCTION_DECL
1035 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1036 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1037 && C_FUNCTION_IMPLICIT_INT (newdecl))
1039 pedwarn_with_decl (newdecl, "conflicting types for `%s'");
1040 /* Make sure we keep void as the return type. */
1041 TREE_TYPE (newdecl) = newtype = oldtype;
1042 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1044 else if (!types_match
1045 /* Permit char *foo (int, ...); followed by char *foo ();
1046 if not pedantic. */
1047 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1048 && ! pedantic
1049 /* Return types must still match. */
1050 && comptypes (TREE_TYPE (oldtype),
1051 TREE_TYPE (newtype))
1052 && TYPE_ARG_TYPES (newtype) == 0))
1054 error_with_decl (newdecl, "conflicting types for `%s'");
1055 /* Check for function type mismatch
1056 involving an empty arglist vs a nonempty one. */
1057 if (TREE_CODE (olddecl) == FUNCTION_DECL
1058 && comptypes (TREE_TYPE (oldtype),
1059 TREE_TYPE (newtype))
1060 && ((TYPE_ARG_TYPES (oldtype) == 0
1061 && DECL_INITIAL (olddecl) == 0)
1063 (TYPE_ARG_TYPES (newtype) == 0
1064 && DECL_INITIAL (newdecl) == 0)))
1066 /* Classify the problem further. */
1067 tree t = TYPE_ARG_TYPES (oldtype);
1068 if (t == 0)
1069 t = TYPE_ARG_TYPES (newtype);
1070 for (; t; t = TREE_CHAIN (t))
1072 tree type = TREE_VALUE (t);
1074 if (TREE_CHAIN (t) == 0
1075 && TYPE_MAIN_VARIANT (type) != void_type_node)
1077 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1078 break;
1081 if (c_type_promotes_to (type) != type)
1083 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1084 break;
1088 if (C_DECL_IMPLICIT (olddecl))
1089 error_with_decl (olddecl, "previous implicit declaration of `%s'");
1090 else
1091 error_with_decl (olddecl, "previous declaration of `%s'");
1093 /* This is safer because the initializer might contain references
1094 to variables that were declared between olddecl and newdecl. This
1095 will make the initializer invalid for olddecl in case it gets
1096 assigned to olddecl below. */
1097 if (TREE_CODE (newdecl) == VAR_DECL)
1098 DECL_INITIAL (newdecl) = 0;
1100 /* TLS cannot follow non-TLS declaration. */
1101 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1102 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1104 error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1105 error_with_decl (olddecl, "previous declaration of `%s'");
1107 /* non-TLS declaration cannot follow TLS declaration. */
1108 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1109 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1111 error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1112 error_with_decl (olddecl, "previous declaration of `%s'");
1114 else
1116 errmsg = redeclaration_error_message (newdecl, olddecl);
1117 if (errmsg)
1119 switch (errmsg)
1121 case 1:
1122 error_with_decl (newdecl, "redefinition of `%s'");
1123 break;
1124 case 2:
1125 error_with_decl (newdecl, "redeclaration of `%s'");
1126 break;
1127 case 3:
1128 error_with_decl (newdecl, "conflicting declarations of `%s'");
1129 break;
1130 default:
1131 abort ();
1134 error_with_decl (olddecl,
1135 ((DECL_INITIAL (olddecl)
1136 && current_binding_level == global_binding_level)
1137 ? "`%s' previously defined here"
1138 : "`%s' previously declared here"));
1139 return 0;
1141 else if (TREE_CODE (newdecl) == TYPE_DECL
1142 && (DECL_IN_SYSTEM_HEADER (olddecl)
1143 || DECL_IN_SYSTEM_HEADER (newdecl)))
1145 warning_with_decl (newdecl, "redefinition of `%s'");
1146 warning_with_decl
1147 (olddecl,
1148 ((DECL_INITIAL (olddecl)
1149 && current_binding_level == global_binding_level)
1150 ? "`%s' previously defined here"
1151 : "`%s' previously declared here"));
1153 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1154 && DECL_INITIAL (olddecl) != 0
1155 && TYPE_ARG_TYPES (oldtype) == 0
1156 && TYPE_ARG_TYPES (newtype) != 0
1157 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1159 tree type, parm;
1160 int nargs;
1161 /* Prototype decl follows defn w/o prototype. */
1163 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1164 type = TYPE_ARG_TYPES (newtype),
1165 nargs = 1;
1167 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1169 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1170 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1172 warning_with_decl (newdecl, "prototype for `%s' follows");
1173 warning_with_decl (olddecl, "non-prototype definition here");
1174 break;
1176 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1177 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1179 error_with_decl (newdecl,
1180 "prototype for `%s' follows and number of arguments doesn't match");
1181 error_with_decl (olddecl, "non-prototype definition here");
1182 errmsg = 1;
1183 break;
1185 /* Type for passing arg must be consistent
1186 with that declared for the arg. */
1187 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type)))
1189 error_with_decl (newdecl,
1190 "prototype for `%s' follows and argument %d doesn't match",
1191 nargs);
1192 error_with_decl (olddecl, "non-prototype definition here");
1193 errmsg = 1;
1194 break;
1198 /* Warn about mismatches in various flags. */
1199 else
1201 /* Warn if function is now inline
1202 but was previously declared not inline and has been called. */
1203 if (TREE_CODE (olddecl) == FUNCTION_DECL
1204 && ! DECL_DECLARED_INLINE_P (olddecl)
1205 && DECL_DECLARED_INLINE_P (newdecl)
1206 && TREE_USED (olddecl))
1207 warning_with_decl (newdecl,
1208 "`%s' declared inline after being called");
1209 if (TREE_CODE (olddecl) == FUNCTION_DECL
1210 && ! DECL_DECLARED_INLINE_P (olddecl)
1211 && DECL_DECLARED_INLINE_P (newdecl)
1212 && DECL_INITIAL (olddecl) != 0)
1213 warning_with_decl (newdecl,
1214 "`%s' declared inline after its definition");
1216 /* If pedantic, warn when static declaration follows a non-static
1217 declaration. Otherwise, do so only for functions. */
1218 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1219 && TREE_PUBLIC (olddecl)
1220 && !TREE_PUBLIC (newdecl))
1221 warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1223 /* If warn_traditional, warn when a non-static function
1224 declaration follows a static one. */
1225 if (warn_traditional && !in_system_header
1226 && TREE_CODE (olddecl) == FUNCTION_DECL
1227 && !TREE_PUBLIC (olddecl)
1228 && TREE_PUBLIC (newdecl))
1229 warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1231 /* Warn when const declaration follows a non-const
1232 declaration, but not for functions. */
1233 if (TREE_CODE (olddecl) != FUNCTION_DECL
1234 && !TREE_READONLY (olddecl)
1235 && TREE_READONLY (newdecl))
1236 warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1237 /* These bits are logically part of the type, for variables.
1238 But not for functions
1239 (where qualifiers are not valid ANSI anyway). */
1240 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1241 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1242 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1243 pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1247 /* Optionally warn about more than one declaration for the same name. */
1248 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1249 /* Don't warn about a function declaration
1250 followed by a definition. */
1251 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1252 && DECL_INITIAL (olddecl) == 0)
1253 /* Don't warn about extern decl followed by (tentative) definition. */
1254 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1256 warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1257 warning_with_decl (olddecl, "previous declaration of `%s'");
1260 /* Copy all the DECL_... slots specified in the new decl
1261 except for any that we copy here from the old type.
1263 Past this point, we don't change OLDTYPE and NEWTYPE
1264 even if we change the types of NEWDECL and OLDDECL. */
1266 if (types_match)
1268 /* When copying info to olddecl, we store into write_olddecl
1269 instead. This allows us to avoid modifying olddecl when
1270 different_binding_level is true. */
1271 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1273 /* Merge the data types specified in the two decls. */
1274 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1276 if (different_binding_level)
1278 if (TYPE_ARG_TYPES (oldtype) != 0
1279 && TYPE_ARG_TYPES (newtype) == 0)
1280 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1281 else
1282 TREE_TYPE (newdecl)
1283 = build_type_attribute_variant
1284 (newtype,
1285 merge_attributes (TYPE_ATTRIBUTES (newtype),
1286 TYPE_ATTRIBUTES (oldtype)));
1288 else
1289 TREE_TYPE (newdecl)
1290 = TREE_TYPE (olddecl)
1291 = common_type (newtype, oldtype);
1294 /* Lay the type out, unless already done. */
1295 if (oldtype != TREE_TYPE (newdecl))
1297 if (TREE_TYPE (newdecl) != error_mark_node)
1298 layout_type (TREE_TYPE (newdecl));
1299 if (TREE_CODE (newdecl) != FUNCTION_DECL
1300 && TREE_CODE (newdecl) != TYPE_DECL
1301 && TREE_CODE (newdecl) != CONST_DECL)
1302 layout_decl (newdecl, 0);
1304 else
1306 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1307 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1308 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1309 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1310 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1311 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1313 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1314 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1318 /* Keep the old rtl since we can safely use it. */
1319 COPY_DECL_RTL (olddecl, newdecl);
1321 /* Merge the type qualifiers. */
1322 if (TREE_READONLY (newdecl))
1323 TREE_READONLY (write_olddecl) = 1;
1325 if (TREE_THIS_VOLATILE (newdecl))
1327 TREE_THIS_VOLATILE (write_olddecl) = 1;
1328 if (TREE_CODE (newdecl) == VAR_DECL
1329 /* If an automatic variable is re-declared in the same
1330 function scope, but the old declaration was not
1331 volatile, make_var_volatile() would crash because the
1332 variable would have been assigned to a pseudo, not a
1333 MEM. Since this duplicate declaration is invalid
1334 anyway, we just skip the call. */
1335 && errmsg == 0)
1336 make_var_volatile (newdecl);
1339 /* Keep source location of definition rather than declaration. */
1340 /* When called with different_binding_level set, keep the old
1341 information so that meaningful diagnostics can be given. */
1342 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1343 && ! different_binding_level)
1345 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1346 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1349 /* Merge the unused-warning information. */
1350 if (DECL_IN_SYSTEM_HEADER (olddecl))
1351 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1352 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1353 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1355 /* Merge the initialization information. */
1356 /* When called with different_binding_level set, don't copy over
1357 DECL_INITIAL, so that we don't accidentally change function
1358 declarations into function definitions. */
1359 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1360 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1362 /* Merge the section attribute.
1363 We want to issue an error if the sections conflict but that must be
1364 done later in decl_attributes since we are called before attributes
1365 are assigned. */
1366 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1367 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1369 /* Copy the assembler name.
1370 Currently, it can only be defined in the prototype. */
1371 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1373 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1375 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1376 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1377 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1378 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1379 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1380 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1381 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1382 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1383 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1386 /* If cannot merge, then use the new type and qualifiers,
1387 and don't preserve the old rtl. */
1388 else if (! different_binding_level)
1390 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1391 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1392 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1393 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1396 /* Merge the storage class information. */
1397 merge_weak (newdecl, olddecl);
1399 /* For functions, static overrides non-static. */
1400 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1402 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1403 /* This is since we don't automatically
1404 copy the attributes of NEWDECL into OLDDECL. */
1405 /* No need to worry about different_binding_level here because
1406 then TREE_PUBLIC (newdecl) was true. */
1407 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1408 /* If this clears `static', clear it in the identifier too. */
1409 if (! TREE_PUBLIC (olddecl))
1410 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1412 if (DECL_EXTERNAL (newdecl))
1414 if (! different_binding_level)
1416 /* Don't mess with these flags on local externs; they remain
1417 external even if there's a declaration at file scope which
1418 isn't. */
1419 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1420 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1422 /* An extern decl does not override previous storage class. */
1423 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1424 if (! DECL_EXTERNAL (newdecl))
1425 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1427 else
1429 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1430 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1433 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1435 /* If we're redefining a function previously defined as extern
1436 inline, make sure we emit debug info for the inline before we
1437 throw it away, in case it was inlined into a function that hasn't
1438 been written out yet. */
1439 if (new_is_definition && DECL_INITIAL (olddecl))
1441 if (TREE_USED (olddecl))
1442 (*debug_hooks->outlining_inline_function) (olddecl);
1444 /* The new defn must not be inline. */
1445 DECL_INLINE (newdecl) = 0;
1446 DECL_UNINLINABLE (newdecl) = 1;
1448 else
1450 /* If either decl says `inline', this fn is inline,
1451 unless its definition was passed already. */
1452 if (DECL_DECLARED_INLINE_P (newdecl)
1453 || DECL_DECLARED_INLINE_P (olddecl))
1454 DECL_DECLARED_INLINE_P (newdecl) = 1;
1456 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1457 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1460 if (DECL_BUILT_IN (olddecl))
1462 /* Get rid of any built-in function if new arg types don't match it
1463 or if we have a function definition. */
1464 if (! types_match || new_is_definition)
1466 if (! different_binding_level)
1468 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1469 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1472 else
1474 /* If redeclaring a builtin function, and not a definition,
1475 it stays built in. */
1476 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1477 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1481 /* Also preserve various other info from the definition. */
1482 if (! new_is_definition)
1484 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1485 /* When called with different_binding_level set, don't copy over
1486 DECL_INITIAL, so that we don't accidentally change function
1487 declarations into function definitions. */
1488 if (! different_binding_level)
1489 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1490 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1491 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1492 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
1493 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1495 /* Set DECL_INLINE on the declaration if we've got a body
1496 from which to instantiate. */
1497 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1499 DECL_INLINE (newdecl) = 1;
1500 DECL_ABSTRACT_ORIGIN (newdecl)
1501 = (different_binding_level
1502 ? DECL_ORIGIN (olddecl)
1503 : DECL_ABSTRACT_ORIGIN (olddecl));
1506 else
1508 /* If a previous declaration said inline, mark the
1509 definition as inlinable. */
1510 if (DECL_DECLARED_INLINE_P (newdecl)
1511 && ! DECL_UNINLINABLE (newdecl))
1512 DECL_INLINE (newdecl) = 1;
1515 if (different_binding_level)
1516 return 0;
1518 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1519 But preserve OLDDECL's DECL_UID. */
1521 unsigned olddecl_uid = DECL_UID (olddecl);
1523 memcpy ((char *) olddecl + sizeof (struct tree_common),
1524 (char *) newdecl + sizeof (struct tree_common),
1525 sizeof (struct tree_decl) - sizeof (struct tree_common));
1526 DECL_UID (olddecl) = olddecl_uid;
1529 /* NEWDECL contains the merged attribute lists.
1530 Update OLDDECL to be the same. */
1531 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1533 return 1;
1536 /* Return any external DECL associated with ID, whether or not it is
1537 currently in scope. */
1538 static tree
1539 any_external_decl (id)
1540 tree id;
1542 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1543 tree t;
1545 if (decl && TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1546 return decl;
1548 t = purpose_member (id, truly_local_externals);
1549 if (t)
1550 return TREE_VALUE (t);
1552 return 0;
1555 /* Record an external decl DECL. This only does something if a
1556 shadowing decl already exists. */
1557 static void
1558 record_external_decl (decl)
1559 tree decl;
1561 tree name = DECL_NAME (decl);
1562 if (!IDENTIFIER_SYMBOL_VALUE (name))
1563 return;
1565 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1568 /* Check whether decl-node X shadows an existing declaration.
1569 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1570 which might be a NULL_TREE. */
1571 static void
1572 warn_if_shadowing (x, old)
1573 tree x, old;
1575 const char *name;
1577 /* Nothing to shadow? */
1578 if (old == 0
1579 /* Shadow warnings not wanted? */
1580 || !warn_shadow
1581 /* No shadow warnings for internally generated vars. */
1582 || DECL_SOURCE_LINE (x) == 0
1583 /* No shadow warnings for vars made for inlining. */
1584 || DECL_FROM_INLINE (x)
1585 /* Don't warn about the parm names in function declarator
1586 within a function declarator.
1587 It would be nice to avoid warning in any function
1588 declarator in a declaration, as opposed to a definition,
1589 but there is no way to tell it's not a definition. */
1590 || (TREE_CODE (x) == PARM_DECL
1591 && current_binding_level->level_chain->parm_flag))
1592 return;
1594 name = IDENTIFIER_POINTER (DECL_NAME (x));
1596 if (TREE_CODE (old) == PARM_DECL)
1597 shadow_warning (SW_PARAM, name, old);
1598 else if (DECL_CONTEXT (old) == 0)
1599 shadow_warning (SW_GLOBAL, name, old);
1600 else
1601 shadow_warning (SW_LOCAL, name, old);
1605 /* Subroutine of pushdecl.
1607 X is a TYPE_DECL for a typedef statement. Create a brand new
1608 ..._TYPE node (which will be just a variant of the existing
1609 ..._TYPE node with identical properties) and then install X
1610 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1612 The whole point here is to end up with a situation where each
1613 and every ..._TYPE node the compiler creates will be uniquely
1614 associated with AT MOST one node representing a typedef name.
1615 This way, even though the compiler substitutes corresponding
1616 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1617 early on, later parts of the compiler can always do the reverse
1618 translation and get back the corresponding typedef name. For
1619 example, given:
1621 typedef struct S MY_TYPE;
1622 MY_TYPE object;
1624 Later parts of the compiler might only know that `object' was of
1625 type `struct S' if it were not for code just below. With this
1626 code however, later parts of the compiler see something like:
1628 struct S' == struct S
1629 typedef struct S' MY_TYPE;
1630 struct S' object;
1632 And they can then deduce (from the node for type struct S') that
1633 the original object declaration was:
1635 MY_TYPE object;
1637 Being able to do this is important for proper support of protoize,
1638 and also for generating precise symbolic debugging information
1639 which takes full account of the programmer's (typedef) vocabulary.
1641 Obviously, we don't want to generate a duplicate ..._TYPE node if
1642 the TYPE_DECL node that we are now processing really represents a
1643 standard built-in type.
1645 Since all standard types are effectively declared at line zero
1646 in the source file, we can easily check to see if we are working
1647 on a standard type by checking the current value of lineno. */
1649 static void
1650 clone_underlying_type (x)
1651 tree x;
1653 if (DECL_SOURCE_LINE (x) == 0)
1655 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1656 TYPE_NAME (TREE_TYPE (x)) = x;
1658 else if (TREE_TYPE (x) != error_mark_node
1659 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1661 tree tt = TREE_TYPE (x);
1662 DECL_ORIGINAL_TYPE (x) = tt;
1663 tt = build_type_copy (tt);
1664 TYPE_NAME (tt) = x;
1665 TREE_USED (tt) = TREE_USED (x);
1666 TREE_TYPE (x) = tt;
1670 /* Record a decl-node X as belonging to the current lexical scope.
1671 Check for errors (such as an incompatible declaration for the same
1672 name already seen in the same scope).
1674 Returns either X or an old decl for the same name.
1675 If an old decl is returned, it may have been smashed
1676 to agree with what X says. */
1678 tree
1679 pushdecl (x)
1680 tree x;
1682 tree name = DECL_NAME (x);
1683 struct binding_level *scope = current_binding_level;
1685 #ifdef ENABLE_CHECKING
1686 if (error_mark_node == 0)
1687 /* Called too early. */
1688 abort ();
1689 #endif
1691 /* Functions need the lang_decl data. */
1692 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1693 DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1694 ggc_alloc_cleared (sizeof (struct lang_decl));
1696 /* A local extern declaration for a function doesn't constitute nesting.
1697 A local auto declaration does, since it's a forward decl
1698 for a nested function coming later. */
1699 if ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1700 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x))
1701 DECL_CONTEXT (x) = 0;
1702 else
1703 DECL_CONTEXT (x) = current_function_decl;
1705 if (name)
1707 tree old;
1709 if (warn_nested_externs
1710 && scope != global_binding_level
1711 && DECL_EXTERNAL (x)
1712 && !DECL_IN_SYSTEM_HEADER (x))
1713 warning ("nested extern declaration of `%s'",
1714 IDENTIFIER_POINTER (name));
1716 old = lookup_name_current_level (name);
1717 if (old && duplicate_decls (x, old, 0))
1718 return old;
1719 if (DECL_EXTERNAL (x) || scope == global_binding_level)
1721 /* Find and check against a previous, not-in-scope, external
1722 decl for this identifier. (C99 s???: If two declarations
1723 with external linkage, referring to the same object, have
1724 incompatible types, the behavior is undefined). */
1725 tree ext = any_external_decl (name);
1726 if (ext)
1728 if (duplicate_decls (x, ext, scope != global_binding_level))
1729 x = copy_node (ext);
1731 else
1732 record_external_decl (x);
1735 if (TREE_CODE (x) == TYPE_DECL)
1736 clone_underlying_type (x);
1738 /* If storing a local value, there may already be one
1739 (inherited). If so, record it for restoration when this
1740 binding level ends. Take care not to do this if we are
1741 replacing an older decl in the same binding level (i.e.
1742 duplicate_decls returned false, above). */
1743 if (scope != global_binding_level
1744 && IDENTIFIER_SYMBOL_VALUE (name)
1745 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1747 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1748 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1749 scope->shadowed);
1752 /* Install the new declaration in the requested binding level. */
1753 IDENTIFIER_SYMBOL_VALUE (name) = x;
1754 C_DECL_INVISIBLE (x) = 0;
1756 /* Keep list of variables in this level with incomplete type.
1757 If the input is erroneous, we can have error_mark in the type
1758 slot (e.g. "f(void a, ...)") - that doesn't count as an
1759 incomplete type. */
1760 if (TREE_TYPE (x) != error_mark_node
1761 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1763 tree element = TREE_TYPE (x);
1765 while (TREE_CODE (element) == ARRAY_TYPE)
1766 element = TREE_TYPE (element);
1767 if (TREE_CODE (element) == RECORD_TYPE
1768 || TREE_CODE (element) == UNION_TYPE)
1769 scope->incomplete_list = tree_cons (NULL_TREE, x,
1770 scope->incomplete_list);
1774 /* Put decls on list in reverse order.
1775 We will reverse them later if necessary. */
1776 TREE_CHAIN (x) = scope->names;
1777 scope->names = x;
1779 return x;
1782 /* Record X as belonging to the global scope (C99 "file scope").
1783 This is used only internally by the Objective C front end,
1784 and is limited to its needs. It will hork if there is _any_
1785 visible binding for X (not just a global one). */
1786 tree
1787 pushdecl_top_level (x)
1788 tree x;
1790 tree name, old;
1792 if (TREE_CODE (x) != VAR_DECL)
1793 abort ();
1795 name = DECL_NAME (x);
1796 old = IDENTIFIER_SYMBOL_VALUE (name);
1798 if (old)
1800 if (DECL_CONTEXT (old))
1801 abort ();
1803 if (!duplicate_decls (x, old, 0))
1804 abort ();
1806 return old;
1809 DECL_CONTEXT (x) = 0;
1810 IDENTIFIER_SYMBOL_VALUE (name) = x;
1811 TREE_CHAIN (x) = global_binding_level->names;
1812 global_binding_level->names = x;
1813 return x;
1816 /* Record X as belonging to the outermost scope of the current
1817 function. This is used only internally, by c_make_fname_decl and
1818 build_external_ref, and is limited to their needs. The NAME is
1819 provided as a separate argument because build_external_ref wants to
1820 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1821 called; if there is any preexisting decl for this identifier, it is
1822 an ICE. */
1823 tree
1824 pushdecl_function_level (x, name)
1825 tree x;
1826 tree name;
1828 struct binding_level *scope;
1830 scope = current_binding_level;
1831 while (scope->function_body == 0)
1832 scope = scope->level_chain;
1833 if (!scope)
1834 abort ();
1836 if (x == error_mark_node)
1837 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1838 scope->shadowed);
1839 else if (TREE_CODE (x) == VAR_DECL)
1841 if (name != DECL_NAME (x))
1842 abort ();
1843 if (IDENTIFIER_SYMBOL_VALUE (name))
1844 abort ();
1846 DECL_CONTEXT (x) = current_function_decl;
1847 TREE_CHAIN (x) = scope->names;
1848 scope->names = x;
1851 IDENTIFIER_SYMBOL_VALUE (name) = x;
1852 return x;
1855 /* Generate an implicit declaration for identifier FUNCTIONID as a
1856 function of type int (). */
1858 tree
1859 implicitly_declare (functionid)
1860 tree functionid;
1862 tree decl = any_external_decl (functionid);
1864 if (decl && decl != error_mark_node)
1866 /* Implicit declaration of a function already declared
1867 (somehow) in a different scope, or as a built-in.
1868 If this is the first time this has happened, warn;
1869 then recycle the old declaration. */
1870 if (!C_DECL_IMPLICIT (decl))
1872 implicit_decl_warning (DECL_NAME (decl));
1873 if (DECL_CONTEXT (decl))
1874 warning_with_decl (decl, "previous declaration of `%s'");
1875 C_DECL_IMPLICIT (decl) = 1;
1877 /* If this function is global, then it must already be in the
1878 global binding level, so there's no need to push it again. */
1879 if (current_binding_level == global_binding_level)
1880 return decl;
1881 /* If this is a local declaration, make a copy; we can't have
1882 the same DECL listed in two different binding levels. */
1883 return pushdecl (copy_node (decl));
1886 /* Not seen before. */
1887 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1888 DECL_EXTERNAL (decl) = 1;
1889 TREE_PUBLIC (decl) = 1;
1890 C_DECL_IMPLICIT (decl) = 1;
1891 implicit_decl_warning (functionid);
1893 /* ANSI standard says implicit declarations are in the innermost block.
1894 So we record the decl in the standard fashion. */
1895 decl = pushdecl (decl);
1897 /* No need to call objc_check_decl here - it's a function type. */
1898 rest_of_decl_compilation (decl, NULL, 0, 0);
1900 /* Write a record describing this implicit function declaration to the
1901 prototypes file (if requested). */
1903 gen_aux_info_record (decl, 0, 1, 0);
1905 /* Possibly apply some default attributes to this implicit declaration. */
1906 decl_attributes (&decl, NULL_TREE, 0);
1908 return decl;
1911 static void
1912 implicit_decl_warning (id)
1913 tree id;
1915 const char *name = IDENTIFIER_POINTER (id);
1916 if (mesg_implicit_function_declaration == 2)
1917 error ("implicit declaration of function `%s'", name);
1918 else if (mesg_implicit_function_declaration == 1)
1919 warning ("implicit declaration of function `%s'", name);
1922 /* Return zero if the declaration NEWDECL is valid
1923 when the declaration OLDDECL (assumed to be for the same name)
1924 has already been seen.
1925 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1926 and 3 if it is a conflicting declaration. */
1928 static int
1929 redeclaration_error_message (newdecl, olddecl)
1930 tree newdecl, olddecl;
1932 if (TREE_CODE (newdecl) == TYPE_DECL)
1934 /* Do not complain about type redeclarations where at least one
1935 declaration was in a system header. */
1936 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1937 return 0;
1938 return 1;
1940 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1942 /* Declarations of functions can insist on internal linkage
1943 but they can't be inconsistent with internal linkage,
1944 so there can be no error on that account.
1945 However defining the same name twice is no good. */
1946 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1947 /* However, defining once as extern inline and a second
1948 time in another way is ok. */
1949 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1950 && ! (DECL_DECLARED_INLINE_P (newdecl)
1951 && DECL_EXTERNAL (newdecl))))
1952 return 1;
1953 return 0;
1955 else if (DECL_CONTEXT (newdecl) == NULL_TREE)
1957 /* Objects declared at top level: */
1958 /* If at least one is a reference, it's ok. */
1959 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1960 return 0;
1961 /* Reject two definitions. */
1962 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1963 return 1;
1964 /* Now we have two tentative defs, or one tentative and one real def. */
1965 /* Insist that the linkage match. */
1966 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1967 return 3;
1968 return 0;
1970 else if (current_binding_level->parm_flag
1971 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1972 return 0;
1973 else
1975 /* Newdecl has block scope. If olddecl has block scope also, then
1976 reject two definitions, and reject a definition together with an
1977 external reference. Otherwise, it is OK, because newdecl must
1978 be an extern reference to olddecl. */
1979 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1980 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1981 return 2;
1982 return 0;
1986 /* Get the LABEL_DECL corresponding to identifier ID as a label.
1987 Create one if none exists so far for the current function.
1988 This function is called for both label definitions and label references. */
1990 tree
1991 lookup_label (id)
1992 tree id;
1994 tree decl = IDENTIFIER_LABEL_VALUE (id);
1996 if (current_function_decl == 0)
1998 error ("label %s referenced outside of any function",
1999 IDENTIFIER_POINTER (id));
2000 return 0;
2003 /* Use a label already defined or ref'd with this name. */
2004 if (decl != 0)
2006 /* But not if it is inherited and wasn't declared to be inheritable. */
2007 if (DECL_CONTEXT (decl) != current_function_decl
2008 && ! C_DECLARED_LABEL_FLAG (decl))
2009 return shadow_label (id);
2010 return decl;
2013 decl = build_decl (LABEL_DECL, id, void_type_node);
2015 /* A label not explicitly declared must be local to where it's ref'd. */
2016 DECL_CONTEXT (decl) = current_function_decl;
2018 DECL_MODE (decl) = VOIDmode;
2020 /* Say where one reference is to the label,
2021 for the sake of the error if it is not defined. */
2022 DECL_SOURCE_LINE (decl) = lineno;
2023 DECL_SOURCE_FILE (decl) = input_filename;
2025 IDENTIFIER_LABEL_VALUE (id) = decl;
2027 named_labels = tree_cons (NULL_TREE, decl, named_labels);
2029 return decl;
2032 /* Make a label named NAME in the current function,
2033 shadowing silently any that may be inherited from containing functions
2034 or containing scopes.
2036 Note that valid use, if the label being shadowed
2037 comes from another scope in the same function,
2038 requires calling declare_nonlocal_label right away. */
2040 tree
2041 shadow_label (name)
2042 tree name;
2044 tree decl = IDENTIFIER_LABEL_VALUE (name);
2046 if (decl != 0)
2048 tree dup;
2050 /* Check to make sure that the label hasn't already been declared
2051 at this label scope */
2052 for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2053 if (TREE_VALUE (dup) == decl)
2055 error ("duplicate label declaration `%s'",
2056 IDENTIFIER_POINTER (name));
2057 error_with_decl (TREE_VALUE (dup),
2058 "this is a previous declaration");
2059 /* Just use the previous declaration. */
2060 return lookup_label (name);
2063 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2064 IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2067 return lookup_label (name);
2070 /* Define a label, specifying the location in the source file.
2071 Return the LABEL_DECL node for the label, if the definition is valid.
2072 Otherwise return 0. */
2074 tree
2075 define_label (filename, line, name)
2076 const char *filename;
2077 int line;
2078 tree name;
2080 tree decl = lookup_label (name);
2082 /* If label with this name is known from an outer context, shadow it. */
2083 if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2085 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2086 IDENTIFIER_LABEL_VALUE (name) = 0;
2087 decl = lookup_label (name);
2090 if (warn_traditional && !in_system_header && lookup_name (name))
2091 warning_with_file_and_line (filename, line,
2092 "traditional C lacks a separate namespace for labels, identifier `%s' conflicts",
2093 IDENTIFIER_POINTER (name));
2095 if (DECL_INITIAL (decl) != 0)
2097 error_with_file_and_line (filename, line, "duplicate label `%s'",
2098 IDENTIFIER_POINTER (name));
2099 return 0;
2101 else
2103 /* Mark label as having been defined. */
2104 DECL_INITIAL (decl) = error_mark_node;
2105 /* Say where in the source. */
2106 DECL_SOURCE_FILE (decl) = filename;
2107 DECL_SOURCE_LINE (decl) = line;
2108 return decl;
2112 /* Return the list of declarations of the current level.
2113 Note that this list is in reverse order unless/until
2114 you nreverse it; and when you do nreverse it, you must
2115 store the result back using `storedecls' or you will lose. */
2117 tree
2118 getdecls ()
2120 return current_binding_level->names;
2123 /* Return the list of type-tags (for structs, etc) of the current level. */
2125 tree
2126 gettags ()
2128 return current_binding_level->tags;
2131 /* Store the list of declarations of the current level.
2132 This is done for the parameter declarations of a function being defined,
2133 after they are modified in the light of any missing parameters. */
2135 static void
2136 storedecls (decls)
2137 tree decls;
2139 current_binding_level->names = decls;
2142 /* Similarly, store the list of tags of the current level. */
2144 static void
2145 storetags (tags)
2146 tree tags;
2148 current_binding_level->tags = tags;
2151 /* Given NAME, an IDENTIFIER_NODE,
2152 return the structure (or union or enum) definition for that name.
2153 If THISLEVEL_ONLY is nonzero, searches only the current_binding_level.
2154 CODE says which kind of type the caller wants;
2155 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2156 If the wrong kind of type is found, an error is reported. */
2158 static tree
2159 lookup_tag (code, name, thislevel_only)
2160 enum tree_code code;
2161 tree name;
2162 int thislevel_only;
2164 tree tag = IDENTIFIER_TAG_VALUE (name);
2165 int thislevel = 0;
2167 if (!tag)
2168 return 0;
2170 /* We only care about whether it's in this level if
2171 thislevel_only was set or it might be a type clash. */
2172 if (thislevel_only || TREE_CODE (tag) != code)
2174 if (current_binding_level == global_binding_level
2175 || purpose_member (name, current_binding_level->tags))
2176 thislevel = 1;
2179 if (thislevel_only && !thislevel)
2180 return 0;
2182 if (TREE_CODE (tag) != code)
2184 /* Definition isn't the kind we were looking for. */
2185 pending_invalid_xref = name;
2186 pending_invalid_xref_file = input_filename;
2187 pending_invalid_xref_line = lineno;
2189 /* If in the same binding level as a declaration as a tag
2190 of a different type, this must not be allowed to
2191 shadow that tag, so give the error immediately.
2192 (For example, "struct foo; union foo;" is invalid.) */
2193 if (thislevel)
2194 pending_xref_error ();
2196 return tag;
2199 /* Print an error message now
2200 for a recent invalid struct, union or enum cross reference.
2201 We don't print them immediately because they are not invalid
2202 when used in the `struct foo;' construct for shadowing. */
2204 void
2205 pending_xref_error ()
2207 if (pending_invalid_xref != 0)
2208 error_with_file_and_line (pending_invalid_xref_file,
2209 pending_invalid_xref_line,
2210 "`%s' defined as wrong kind of tag",
2211 IDENTIFIER_POINTER (pending_invalid_xref));
2212 pending_invalid_xref = 0;
2216 /* Look up NAME in the current binding level and its superiors
2217 in the namespace of variables, functions and typedefs.
2218 Return a ..._DECL node of some kind representing its definition,
2219 or return 0 if it is undefined. */
2221 tree
2222 lookup_name (name)
2223 tree name;
2225 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2226 if (decl == 0 || decl == error_mark_node)
2227 return decl;
2228 if (C_DECL_INVISIBLE (decl))
2229 return 0;
2230 return decl;
2233 /* Similar to `lookup_name' but look only at the current binding level. */
2235 static tree
2236 lookup_name_current_level (name)
2237 tree name;
2239 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2241 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2242 return 0;
2244 if (current_binding_level == global_binding_level)
2245 return decl;
2247 /* Scan the current scope for a decl with name NAME. */
2248 if (chain_member (decl, current_binding_level->names))
2249 return decl;
2251 return 0;
2254 /* Create the predefined scalar types of C,
2255 and some nodes representing standard constants (0, 1, (void *) 0).
2256 Initialize the global binding level.
2257 Make definitions for built-in primitive functions. */
2259 void
2260 c_init_decl_processing ()
2262 tree endlink;
2263 tree ptr_ftype_void, ptr_ftype_ptr;
2265 /* Adds some ggc roots, and reserved words for c-parse.in. */
2266 c_parse_init ();
2268 current_function_decl = NULL;
2269 named_labels = NULL;
2270 current_binding_level = NULL_BINDING_LEVEL;
2271 free_binding_level = NULL_BINDING_LEVEL;
2273 /* Make the binding_level structure for global names. */
2274 pushlevel (0);
2275 global_binding_level = current_binding_level;
2277 build_common_tree_nodes (flag_signed_char);
2279 c_common_nodes_and_builtins ();
2281 boolean_type_node = integer_type_node;
2282 boolean_true_node = integer_one_node;
2283 boolean_false_node = integer_zero_node;
2285 c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2286 TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2287 TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2288 TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2289 TYPE_PRECISION (c_bool_type_node) = 1;
2290 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2291 c_bool_type_node));
2292 c_bool_false_node = build_int_2 (0, 0);
2293 TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2294 c_bool_true_node = build_int_2 (1, 0);
2295 TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2297 endlink = void_list_node;
2298 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2299 ptr_ftype_ptr
2300 = build_function_type (ptr_type_node,
2301 tree_cons (NULL_TREE, ptr_type_node, endlink));
2303 pedantic_lvalues = pedantic;
2305 make_fname_decl = c_make_fname_decl;
2306 start_fname_decls ();
2309 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2310 decl, NAME is the initialization string and TYPE_DEP indicates whether
2311 NAME depended on the type of the function. As we don't yet implement
2312 delayed emission of static data, we mark the decl as emitted
2313 so it is not placed in the output. Anything using it must therefore pull
2314 out the STRING_CST initializer directly. This does mean that these names
2315 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2317 static tree
2318 c_make_fname_decl (id, type_dep)
2319 tree id;
2320 int type_dep;
2322 const char *name = fname_as_string (type_dep);
2323 tree decl, type, init;
2324 size_t length = strlen (name);
2326 type = build_array_type
2327 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2328 build_index_type (size_int (length)));
2330 decl = build_decl (VAR_DECL, id, type);
2332 TREE_STATIC (decl) = 1;
2333 TREE_READONLY (decl) = 1;
2334 DECL_ARTIFICIAL (decl) = 1;
2336 init = build_string (length + 1, name);
2337 TREE_TYPE (init) = type;
2338 DECL_INITIAL (decl) = init;
2340 TREE_USED (decl) = 1;
2342 if (current_function_decl)
2343 pushdecl_function_level (decl, DECL_NAME (decl));
2345 finish_decl (decl, init, NULL_TREE);
2347 return decl;
2350 /* Return a definition for a builtin function named NAME and whose data type
2351 is TYPE. TYPE should be a function type with argument types.
2352 FUNCTION_CODE tells later passes how to compile calls to this function.
2353 See tree.h for its possible values.
2355 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2356 the name to be called if we can't opencode the function. If
2357 ATTRS is nonzero, use that for the function's attribute list. */
2359 tree
2360 builtin_function (name, type, function_code, class, library_name, attrs)
2361 const char *name;
2362 tree type;
2363 int function_code;
2364 enum built_in_class class;
2365 const char *library_name;
2366 tree attrs;
2368 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2369 DECL_EXTERNAL (decl) = 1;
2370 TREE_PUBLIC (decl) = 1;
2371 if (library_name)
2372 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2373 make_decl_rtl (decl, NULL);
2374 pushdecl (decl);
2375 DECL_BUILT_IN_CLASS (decl) = class;
2376 DECL_FUNCTION_CODE (decl) = function_code;
2378 /* Warn if a function in the namespace for users
2379 is used without an occasion to consider it declared. */
2380 if (name[0] != '_' || name[1] != '_')
2381 C_DECL_INVISIBLE (decl) = 1;
2383 /* Possibly apply some default attributes to this built-in function. */
2384 if (attrs)
2385 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2386 else
2387 decl_attributes (&decl, NULL_TREE, 0);
2389 return decl;
2392 /* Apply default attributes to a function, if a system function with default
2393 attributes. */
2395 void
2396 c_insert_default_attributes (decl)
2397 tree decl;
2399 if (!TREE_PUBLIC (decl))
2400 return;
2401 c_common_insert_default_attributes (decl);
2404 /* Called when a declaration is seen that contains no names to declare.
2405 If its type is a reference to a structure, union or enum inherited
2406 from a containing scope, shadow that tag name for the current scope
2407 with a forward reference.
2408 If its type defines a new named structure or union
2409 or defines an enum, it is valid but we need not do anything here.
2410 Otherwise, it is an error. */
2412 void
2413 shadow_tag (declspecs)
2414 tree declspecs;
2416 shadow_tag_warned (declspecs, 0);
2419 void
2420 shadow_tag_warned (declspecs, warned)
2421 tree declspecs;
2422 int warned;
2423 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2424 no pedwarn. */
2426 int found_tag = 0;
2427 tree link;
2428 tree specs, attrs;
2430 pending_invalid_xref = 0;
2432 /* Remove the attributes from declspecs, since they will confuse the
2433 following code. */
2434 split_specs_attrs (declspecs, &specs, &attrs);
2436 for (link = specs; link; link = TREE_CHAIN (link))
2438 tree value = TREE_VALUE (link);
2439 enum tree_code code = TREE_CODE (value);
2441 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2442 /* Used to test also that TYPE_SIZE (value) != 0.
2443 That caused warning for `struct foo;' at top level in the file. */
2445 tree name = TYPE_NAME (value);
2446 tree t;
2448 found_tag++;
2450 if (name == 0)
2452 if (warned != 1 && code != ENUMERAL_TYPE)
2453 /* Empty unnamed enum OK */
2455 pedwarn ("unnamed struct/union that defines no instances");
2456 warned = 1;
2459 else
2461 t = lookup_tag (code, name, 1);
2463 if (t == 0)
2465 t = make_node (code);
2466 pushtag (name, t);
2470 else
2472 if (!warned && ! in_system_header)
2474 warning ("useless keyword or type name in empty declaration");
2475 warned = 2;
2480 if (found_tag > 1)
2481 error ("two types specified in one empty declaration");
2483 if (warned != 1)
2485 if (found_tag == 0)
2486 pedwarn ("empty declaration");
2490 /* Construct an array declarator. EXPR is the expression inside [], or
2491 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2492 to the pointer to which a parameter array is converted). STATIC_P is
2493 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2494 is nonzero is the array is [*], a VLA of unspecified length which is
2495 nevertheless a complete type (not currently implemented by GCC),
2496 zero otherwise. The declarator is constructed as an ARRAY_REF
2497 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2498 left of the [] (filled by in set_array_declarator_type) and operand 1
2499 is the expression inside; whose TREE_TYPE is the type qualifiers and
2500 which has TREE_STATIC set if "static" is used. */
2502 tree
2503 build_array_declarator (expr, quals, static_p, vla_unspec_p)
2504 tree expr;
2505 tree quals;
2506 int static_p;
2507 int vla_unspec_p;
2509 tree decl;
2510 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2511 TREE_TYPE (decl) = quals;
2512 TREE_STATIC (decl) = (static_p ? 1 : 0);
2513 if (pedantic && !flag_isoc99)
2515 if (static_p || quals != NULL_TREE)
2516 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2517 if (vla_unspec_p)
2518 pedwarn ("ISO C90 does not support `[*]' array declarators");
2520 if (vla_unspec_p)
2521 warning ("GCC does not yet properly implement `[*]' array declarators");
2522 return decl;
2525 /* Set the type of an array declarator. DECL is the declarator, as
2526 constructed by build_array_declarator; TYPE is what appears on the left
2527 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2528 abstract declarator, zero otherwise; this is used to reject static and
2529 type qualifiers in abstract declarators, where they are not in the
2530 C99 grammar. */
2532 tree
2533 set_array_declarator_type (decl, type, abstract_p)
2534 tree decl;
2535 tree type;
2536 int abstract_p;
2538 TREE_OPERAND (decl, 0) = type;
2539 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2540 error ("static or type qualifiers in abstract declarator");
2541 return decl;
2544 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2546 tree
2547 groktypename (typename)
2548 tree typename;
2550 tree specs, attrs;
2552 if (TREE_CODE (typename) != TREE_LIST)
2553 return typename;
2555 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2557 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2559 /* Apply attributes. */
2560 decl_attributes (&typename, attrs, 0);
2562 return typename;
2565 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2567 tree
2568 groktypename_in_parm_context (typename)
2569 tree typename;
2571 if (TREE_CODE (typename) != TREE_LIST)
2572 return typename;
2573 return grokdeclarator (TREE_VALUE (typename),
2574 TREE_PURPOSE (typename),
2575 PARM, 0);
2578 /* Decode a declarator in an ordinary declaration or data definition.
2579 This is called as soon as the type information and variable name
2580 have been parsed, before parsing the initializer if any.
2581 Here we create the ..._DECL node, fill in its type,
2582 and put it on the list of decls for the current context.
2583 The ..._DECL node is returned as the value.
2585 Exception: for arrays where the length is not specified,
2586 the type is left null, to be filled in by `finish_decl'.
2588 Function definitions do not come here; they go to start_function
2589 instead. However, external and forward declarations of functions
2590 do go through here. Structure field declarations are done by
2591 grokfield and not through here. */
2593 tree
2594 start_decl (declarator, declspecs, initialized, attributes)
2595 tree declarator, declspecs;
2596 int initialized;
2597 tree attributes;
2599 tree decl;
2600 tree tem;
2602 /* An object declared as __attribute__((deprecated)) suppresses
2603 warnings of uses of other deprecated items. */
2604 if (lookup_attribute ("deprecated", attributes))
2605 deprecated_state = DEPRECATED_SUPPRESS;
2607 decl = grokdeclarator (declarator, declspecs,
2608 NORMAL, initialized);
2610 deprecated_state = DEPRECATED_NORMAL;
2612 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2613 && MAIN_NAME_P (DECL_NAME (decl)))
2614 warning_with_decl (decl, "`%s' is usually a function");
2616 if (initialized)
2617 /* Is it valid for this decl to have an initializer at all?
2618 If not, set INITIALIZED to zero, which will indirectly
2619 tell `finish_decl' to ignore the initializer once it is parsed. */
2620 switch (TREE_CODE (decl))
2622 case TYPE_DECL:
2623 error ("typedef `%s' is initialized (use __typeof__ instead)",
2624 IDENTIFIER_POINTER (DECL_NAME (decl)));
2625 initialized = 0;
2626 break;
2628 case FUNCTION_DECL:
2629 error ("function `%s' is initialized like a variable",
2630 IDENTIFIER_POINTER (DECL_NAME (decl)));
2631 initialized = 0;
2632 break;
2634 case PARM_DECL:
2635 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2636 error ("parameter `%s' is initialized",
2637 IDENTIFIER_POINTER (DECL_NAME (decl)));
2638 initialized = 0;
2639 break;
2641 default:
2642 /* Don't allow initializations for incomplete types
2643 except for arrays which might be completed by the initialization. */
2645 /* This can happen if the array size is an undefined macro. We already
2646 gave a warning, so we don't need another one. */
2647 if (TREE_TYPE (decl) == error_mark_node)
2648 initialized = 0;
2649 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2651 /* A complete type is ok if size is fixed. */
2653 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2654 || C_DECL_VARIABLE_SIZE (decl))
2656 error ("variable-sized object may not be initialized");
2657 initialized = 0;
2660 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2662 error ("variable `%s' has initializer but incomplete type",
2663 IDENTIFIER_POINTER (DECL_NAME (decl)));
2664 initialized = 0;
2666 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2668 error ("elements of array `%s' have incomplete type",
2669 IDENTIFIER_POINTER (DECL_NAME (decl)));
2670 initialized = 0;
2674 if (initialized)
2676 #if 0
2677 /* Seems redundant with grokdeclarator. */
2678 if (current_binding_level != global_binding_level
2679 && DECL_EXTERNAL (decl)
2680 && TREE_CODE (decl) != FUNCTION_DECL)
2681 warning ("declaration of `%s' has `extern' and is initialized",
2682 IDENTIFIER_POINTER (DECL_NAME (decl)));
2683 #endif
2684 DECL_EXTERNAL (decl) = 0;
2685 if (current_binding_level == global_binding_level)
2686 TREE_STATIC (decl) = 1;
2688 /* Tell `pushdecl' this is an initialized decl
2689 even though we don't yet have the initializer expression.
2690 Also tell `finish_decl' it may store the real initializer. */
2691 DECL_INITIAL (decl) = error_mark_node;
2694 /* If this is a function declaration, write a record describing it to the
2695 prototypes file (if requested). */
2697 if (TREE_CODE (decl) == FUNCTION_DECL)
2698 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2700 /* ANSI specifies that a tentative definition which is not merged with
2701 a non-tentative definition behaves exactly like a definition with an
2702 initializer equal to zero. (Section 3.7.2)
2704 -fno-common gives strict ANSI behavior, though this tends to break
2705 a large body of code that grew up without this rule.
2707 Thread-local variables are never common, since there's no entrenched
2708 body of code to break, and it allows more efficient variable references
2709 in the presence of dynamic linking. */
2711 if (TREE_CODE (decl) == VAR_DECL
2712 && !initialized
2713 && TREE_PUBLIC (decl)
2714 && !DECL_THREAD_LOCAL (decl)
2715 && !flag_no_common)
2716 DECL_COMMON (decl) = 1;
2718 /* Set attributes here so if duplicate decl, will have proper attributes. */
2719 decl_attributes (&decl, attributes, 0);
2721 /* If #pragma weak was used, mark the decl weak now. */
2722 if (current_binding_level == global_binding_level)
2723 maybe_apply_pragma_weak (decl);
2725 if (TREE_CODE (decl) == FUNCTION_DECL
2726 && DECL_DECLARED_INLINE_P (decl)
2727 && DECL_UNINLINABLE (decl)
2728 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2729 warning_with_decl (decl,
2730 "inline function `%s' given attribute noinline");
2732 /* Add this decl to the current binding level.
2733 TEM may equal DECL or it may be a previous decl of the same name. */
2734 tem = pushdecl (decl);
2736 /* For a local variable, define the RTL now. */
2737 if (current_binding_level != global_binding_level
2738 /* But not if this is a duplicate decl
2739 and we preserved the rtl from the previous one
2740 (which may or may not happen). */
2741 && !DECL_RTL_SET_P (tem)
2742 && !DECL_CONTEXT (tem))
2744 if (TREE_TYPE (tem) != error_mark_node
2745 && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2746 expand_decl (tem);
2747 else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2748 && DECL_INITIAL (tem) != 0)
2749 expand_decl (tem);
2752 return tem;
2755 /* Finish processing of a declaration;
2756 install its initial value.
2757 If the length of an array type is not known before,
2758 it must be determined now, from the initial value, or it is an error. */
2760 void
2761 finish_decl (decl, init, asmspec_tree)
2762 tree decl, init;
2763 tree asmspec_tree;
2765 tree type = TREE_TYPE (decl);
2766 int was_incomplete = (DECL_SIZE (decl) == 0);
2767 const char *asmspec = 0;
2769 /* If a name was specified, get the string. */
2770 if (current_binding_level == global_binding_level)
2771 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2772 if (asmspec_tree)
2773 asmspec = TREE_STRING_POINTER (asmspec_tree);
2775 /* If `start_decl' didn't like having an initialization, ignore it now. */
2776 if (init != 0 && DECL_INITIAL (decl) == 0)
2777 init = 0;
2779 /* Don't crash if parm is initialized. */
2780 if (TREE_CODE (decl) == PARM_DECL)
2781 init = 0;
2783 if (init)
2784 store_init_value (decl, init);
2786 /* Deduce size of array from initialization, if not already known */
2787 if (TREE_CODE (type) == ARRAY_TYPE
2788 && TYPE_DOMAIN (type) == 0
2789 && TREE_CODE (decl) != TYPE_DECL)
2791 int do_default
2792 = (TREE_STATIC (decl)
2793 /* Even if pedantic, an external linkage array
2794 may have incomplete type at first. */
2795 ? pedantic && !TREE_PUBLIC (decl)
2796 : !DECL_EXTERNAL (decl));
2797 int failure
2798 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2800 /* Get the completed type made by complete_array_type. */
2801 type = TREE_TYPE (decl);
2803 if (failure == 1)
2804 error_with_decl (decl, "initializer fails to determine size of `%s'");
2806 else if (failure == 2)
2808 if (do_default)
2809 error_with_decl (decl, "array size missing in `%s'");
2810 /* If a `static' var's size isn't known,
2811 make it extern as well as static, so it does not get
2812 allocated.
2813 If it is not `static', then do not mark extern;
2814 finish_incomplete_decl will give it a default size
2815 and it will get allocated. */
2816 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2817 DECL_EXTERNAL (decl) = 1;
2820 /* TYPE_MAX_VALUE is always one less than the number of elements
2821 in the array, because we start counting at zero. Therefore,
2822 warn only if the value is less than zero. */
2823 else if (pedantic && TYPE_DOMAIN (type) != 0
2824 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2825 error_with_decl (decl, "zero or negative size array `%s'");
2827 layout_decl (decl, 0);
2830 if (TREE_CODE (decl) == VAR_DECL)
2832 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2833 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2834 layout_decl (decl, 0);
2836 if (DECL_SIZE (decl) == 0
2837 /* Don't give an error if we already gave one earlier. */
2838 && TREE_TYPE (decl) != error_mark_node
2839 && (TREE_STATIC (decl)
2841 /* A static variable with an incomplete type
2842 is an error if it is initialized.
2843 Also if it is not file scope.
2844 Otherwise, let it through, but if it is not `extern'
2845 then it may cause an error message later. */
2846 (DECL_INITIAL (decl) != 0
2847 || DECL_CONTEXT (decl) != 0)
2849 /* An automatic variable with an incomplete type
2850 is an error. */
2851 !DECL_EXTERNAL (decl)))
2853 error_with_decl (decl, "storage size of `%s' isn't known");
2854 TREE_TYPE (decl) = error_mark_node;
2857 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2858 && DECL_SIZE (decl) != 0)
2860 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2861 constant_expression_warning (DECL_SIZE (decl));
2862 else
2863 error_with_decl (decl, "storage size of `%s' isn't constant");
2866 if (TREE_USED (type))
2867 TREE_USED (decl) = 1;
2870 /* If this is a function and an assembler name is specified, it isn't
2871 builtin any more. Also reset DECL_RTL so we can give it its new
2872 name. */
2873 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2875 DECL_BUILT_IN_CLASS (decl) = NOT_BUILT_IN;
2876 SET_DECL_RTL (decl, NULL_RTX);
2877 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2880 /* Output the assembler code and/or RTL code for variables and functions,
2881 unless the type is an undefined structure or union.
2882 If not, it will get done when the type is completed. */
2884 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2886 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2887 if (flag_objc)
2888 objc_check_decl (decl);
2890 if (!DECL_CONTEXT (decl))
2892 if (DECL_INITIAL (decl) == NULL_TREE
2893 || DECL_INITIAL (decl) == error_mark_node)
2894 /* Don't output anything
2895 when a tentative file-scope definition is seen.
2896 But at end of compilation, do output code for them. */
2897 DECL_DEFER_OUTPUT (decl) = 1;
2898 rest_of_decl_compilation (decl, asmspec,
2899 (DECL_CONTEXT (decl) == 0
2900 || TREE_ASM_WRITTEN (decl)), 0);
2902 else
2904 /* This is a local variable. If there is an ASMSPEC, the
2905 user has requested that we handle it specially. */
2906 if (asmspec)
2908 /* In conjunction with an ASMSPEC, the `register'
2909 keyword indicates that we should place the variable
2910 in a particular register. */
2911 if (DECL_REGISTER (decl))
2912 DECL_C_HARD_REGISTER (decl) = 1;
2914 /* If this is not a static variable, issue a warning.
2915 It doesn't make any sense to give an ASMSPEC for an
2916 ordinary, non-register local variable. Historically,
2917 GCC has accepted -- but ignored -- the ASMSPEC in
2918 this case. */
2919 if (TREE_CODE (decl) == VAR_DECL
2920 && !DECL_REGISTER (decl)
2921 && !TREE_STATIC (decl))
2922 warning_with_decl (decl,
2923 "ignoring asm-specifier for non-static local variable `%s'");
2924 else
2925 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2928 if (TREE_CODE (decl) != FUNCTION_DECL)
2929 add_decl_stmt (decl);
2932 if (DECL_CONTEXT (decl) != 0)
2934 /* Recompute the RTL of a local array now
2935 if it used to be an incomplete type. */
2936 if (was_incomplete
2937 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2939 /* If we used it already as memory, it must stay in memory. */
2940 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2941 /* If it's still incomplete now, no init will save it. */
2942 if (DECL_SIZE (decl) == 0)
2943 DECL_INITIAL (decl) = 0;
2948 if (TREE_CODE (decl) == TYPE_DECL)
2950 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2951 if (flag_objc)
2952 objc_check_decl (decl);
2953 rest_of_decl_compilation (decl, NULL, DECL_CONTEXT (decl) == 0, 0);
2956 /* At the end of a declaration, throw away any variable type sizes
2957 of types defined inside that declaration. There is no use
2958 computing them in the following function definition. */
2959 if (current_binding_level == global_binding_level)
2960 get_pending_sizes ();
2963 /* Given a parsed parameter declaration,
2964 decode it into a PARM_DECL and push that on the current binding level.
2965 Also, for the sake of forward parm decls,
2966 record the given order of parms in `parm_order'. */
2968 void
2969 push_parm_decl (parm)
2970 tree parm;
2972 tree decl;
2973 int old_immediate_size_expand = immediate_size_expand;
2974 /* Don't try computing parm sizes now -- wait till fn is called. */
2975 immediate_size_expand = 0;
2977 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2978 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2979 decl_attributes (&decl, TREE_VALUE (parm), 0);
2981 #if 0
2982 if (DECL_NAME (decl))
2984 tree olddecl;
2985 olddecl = lookup_name (DECL_NAME (decl));
2986 if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
2987 pedwarn_with_decl (decl,
2988 "ISO C forbids parameter `%s' shadowing typedef");
2990 #endif
2992 decl = pushdecl (decl);
2994 immediate_size_expand = old_immediate_size_expand;
2996 current_binding_level->parm_order
2997 = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
2999 /* Add this decl to the current binding level. */
3000 finish_decl (decl, NULL_TREE, NULL_TREE);
3003 /* Clear the given order of parms in `parm_order'.
3004 Used at start of parm list,
3005 and also at semicolon terminating forward decls. */
3007 void
3008 clear_parm_order ()
3010 current_binding_level->parm_order = NULL_TREE;
3013 static GTY(()) int compound_literal_number;
3015 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3016 literal, which may be an incomplete array type completed by the
3017 initializer; INIT is a CONSTRUCTOR that initializes the compound
3018 literal. */
3020 tree
3021 build_compound_literal (type, init)
3022 tree type;
3023 tree init;
3025 /* We do not use start_decl here because we have a type, not a declarator;
3026 and do not use finish_decl because the decl should be stored inside
3027 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3028 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3029 tree complit;
3030 tree stmt;
3031 DECL_EXTERNAL (decl) = 0;
3032 TREE_PUBLIC (decl) = 0;
3033 TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3034 DECL_CONTEXT (decl) = current_function_decl;
3035 TREE_USED (decl) = 1;
3036 TREE_TYPE (decl) = type;
3037 TREE_READONLY (decl) = TREE_READONLY (type);
3038 store_init_value (decl, init);
3040 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3042 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3043 if (failure)
3044 abort ();
3047 type = TREE_TYPE (decl);
3048 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3049 return error_mark_node;
3051 stmt = build_stmt (DECL_STMT, decl);
3052 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3053 TREE_SIDE_EFFECTS (complit) = 1;
3055 layout_decl (decl, 0);
3057 if (TREE_STATIC (decl))
3059 /* This decl needs a name for the assembler output. We also need
3060 a unique suffix to be added to the name. */
3061 char *name;
3063 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3064 compound_literal_number);
3065 compound_literal_number++;
3066 DECL_NAME (decl) = get_identifier (name);
3067 DECL_DEFER_OUTPUT (decl) = 1;
3068 DECL_COMDAT (decl) = 1;
3069 DECL_ARTIFICIAL (decl) = 1;
3070 pushdecl (decl);
3071 rest_of_decl_compilation (decl, NULL, 1, 0);
3074 return complit;
3077 /* Make TYPE a complete type based on INITIAL_VALUE.
3078 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3079 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3082 complete_array_type (type, initial_value, do_default)
3083 tree type;
3084 tree initial_value;
3085 int do_default;
3087 tree maxindex = NULL_TREE;
3088 int value = 0;
3090 if (initial_value)
3092 /* Note MAXINDEX is really the maximum index,
3093 one less than the size. */
3094 if (TREE_CODE (initial_value) == STRING_CST)
3096 int eltsize
3097 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3098 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3099 / eltsize) - 1, 0);
3101 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3103 tree elts = CONSTRUCTOR_ELTS (initial_value);
3104 maxindex = build_int_2 (-1, -1);
3105 for (; elts; elts = TREE_CHAIN (elts))
3107 if (TREE_PURPOSE (elts))
3108 maxindex = TREE_PURPOSE (elts);
3109 else
3110 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3111 maxindex, integer_one_node));
3113 maxindex = copy_node (maxindex);
3115 else
3117 /* Make an error message unless that happened already. */
3118 if (initial_value != error_mark_node)
3119 value = 1;
3121 /* Prevent further error messages. */
3122 maxindex = build_int_2 (0, 0);
3126 if (!maxindex)
3128 if (do_default)
3129 maxindex = build_int_2 (0, 0);
3130 value = 2;
3133 if (maxindex)
3135 TYPE_DOMAIN (type) = build_index_type (maxindex);
3136 if (!TREE_TYPE (maxindex))
3137 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3140 /* Lay out the type now that we can get the real answer. */
3142 layout_type (type);
3144 return value;
3147 /* Determine whether TYPE is a structure with a flexible array member,
3148 or a union containing such a structure (possibly recursively). */
3150 static bool
3151 flexible_array_type_p (type)
3152 tree type;
3154 tree x;
3155 switch (TREE_CODE (type))
3157 case RECORD_TYPE:
3158 x = TYPE_FIELDS (type);
3159 if (x == NULL_TREE)
3160 return false;
3161 while (TREE_CHAIN (x) != NULL_TREE)
3162 x = TREE_CHAIN (x);
3163 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3164 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3165 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3166 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3167 return true;
3168 return false;
3169 case UNION_TYPE:
3170 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3172 if (flexible_array_type_p (TREE_TYPE (x)))
3173 return true;
3175 return false;
3176 default:
3177 return false;
3181 /* Given declspecs and a declarator,
3182 determine the name and type of the object declared
3183 and construct a ..._DECL node for it.
3184 (In one case we can return a ..._TYPE node instead.
3185 For invalid input we sometimes return 0.)
3187 DECLSPECS is a chain of tree_list nodes whose value fields
3188 are the storage classes and type specifiers.
3190 DECL_CONTEXT says which syntactic context this declaration is in:
3191 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3192 FUNCDEF for a function definition. Like NORMAL but a few different
3193 error messages in each case. Return value may be zero meaning
3194 this definition is too screwy to try to parse.
3195 PARM for a parameter declaration (either within a function prototype
3196 or before a function body). Make a PARM_DECL, or return void_type_node.
3197 TYPENAME if for a typename (in a cast or sizeof).
3198 Don't make a DECL node; just return the ..._TYPE node.
3199 FIELD for a struct or union field; make a FIELD_DECL.
3200 BITFIELD for a field with specified width.
3201 INITIALIZED is 1 if the decl has an initializer.
3203 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3204 It may also be so in the PARM case, for a prototype where the
3205 argument type is specified but not the name.
3207 This function is where the complicated C meanings of `static'
3208 and `extern' are interpreted. */
3210 static tree
3211 grokdeclarator (declarator, declspecs, decl_context, initialized)
3212 tree declspecs;
3213 tree declarator;
3214 enum decl_context decl_context;
3215 int initialized;
3217 int specbits = 0;
3218 tree spec;
3219 tree type = NULL_TREE;
3220 int longlong = 0;
3221 int constp;
3222 int restrictp;
3223 int volatilep;
3224 int type_quals = TYPE_UNQUALIFIED;
3225 int inlinep;
3226 int explicit_int = 0;
3227 int explicit_char = 0;
3228 int defaulted_int = 0;
3229 tree typedef_decl = 0;
3230 const char *name;
3231 tree typedef_type = 0;
3232 int funcdef_flag = 0;
3233 enum tree_code innermost_code = ERROR_MARK;
3234 int bitfield = 0;
3235 int size_varies = 0;
3236 tree decl_attr = NULL_TREE;
3237 tree array_ptr_quals = NULL_TREE;
3238 int array_parm_static = 0;
3239 tree returned_attrs = NULL_TREE;
3241 if (decl_context == BITFIELD)
3242 bitfield = 1, decl_context = FIELD;
3244 if (decl_context == FUNCDEF)
3245 funcdef_flag = 1, decl_context = NORMAL;
3247 /* Look inside a declarator for the name being declared
3248 and get it as a string, for an error message. */
3250 tree decl = declarator;
3251 name = 0;
3253 while (decl)
3254 switch (TREE_CODE (decl))
3256 case ARRAY_REF:
3257 case INDIRECT_REF:
3258 case CALL_EXPR:
3259 innermost_code = TREE_CODE (decl);
3260 decl = TREE_OPERAND (decl, 0);
3261 break;
3263 case TREE_LIST:
3264 decl = TREE_VALUE (decl);
3265 break;
3267 case IDENTIFIER_NODE:
3268 name = IDENTIFIER_POINTER (decl);
3269 decl = 0;
3270 break;
3272 default:
3273 abort ();
3275 if (name == 0)
3276 name = "type name";
3279 /* A function definition's declarator must have the form of
3280 a function declarator. */
3282 if (funcdef_flag && innermost_code != CALL_EXPR)
3283 return 0;
3285 /* Anything declared one level down from the top level
3286 must be one of the parameters of a function
3287 (because the body is at least two levels down). */
3289 /* If this looks like a function definition, make it one,
3290 even if it occurs where parms are expected.
3291 Then store_parm_decls will reject it and not use it as a parm. */
3292 if (decl_context == NORMAL && !funcdef_flag
3293 && current_binding_level->parm_flag)
3294 decl_context = PARM;
3296 /* Look through the decl specs and record which ones appear.
3297 Some typespecs are defined as built-in typenames.
3298 Others, the ones that are modifiers of other types,
3299 are represented by bits in SPECBITS: set the bits for
3300 the modifiers that appear. Storage class keywords are also in SPECBITS.
3302 If there is a typedef name or a type, store the type in TYPE.
3303 This includes builtin typedefs such as `int'.
3305 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3306 and did not come from a user typedef.
3308 Set LONGLONG if `long' is mentioned twice. */
3310 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3312 tree id = TREE_VALUE (spec);
3314 /* If the entire declaration is itself tagged as deprecated then
3315 suppress reports of deprecated items. */
3316 if (id && TREE_DEPRECATED (id))
3318 if (deprecated_state != DEPRECATED_SUPPRESS)
3319 warn_deprecated_use (id);
3322 if (id == ridpointers[(int) RID_INT])
3323 explicit_int = 1;
3324 if (id == ridpointers[(int) RID_CHAR])
3325 explicit_char = 1;
3327 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3329 enum rid i = C_RID_CODE (id);
3330 if ((int) i <= (int) RID_LAST_MODIFIER)
3332 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3334 if (longlong)
3335 error ("`long long long' is too long for GCC");
3336 else
3338 if (pedantic && !flag_isoc99 && ! in_system_header
3339 && warn_long_long)
3340 pedwarn ("ISO C90 does not support `long long'");
3341 longlong = 1;
3344 else if (specbits & (1 << (int) i))
3346 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3348 if (!flag_isoc99)
3349 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3351 else
3352 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3355 /* Diagnose "__thread extern". Recall that this list
3356 is in the reverse order seen in the text. */
3357 if (i == RID_THREAD
3358 && (specbits & (1 << (int) RID_EXTERN
3359 | 1 << (int) RID_STATIC)))
3361 if (specbits & 1 << (int) RID_EXTERN)
3362 error ("`__thread' before `extern'");
3363 else
3364 error ("`__thread' before `static'");
3367 specbits |= 1 << (int) i;
3368 goto found;
3371 if (type)
3372 error ("two or more data types in declaration of `%s'", name);
3373 /* Actual typedefs come to us as TYPE_DECL nodes. */
3374 else if (TREE_CODE (id) == TYPE_DECL)
3376 if (TREE_TYPE (id) == error_mark_node)
3377 ; /* Allow the type to default to int to avoid cascading errors. */
3378 else
3380 type = TREE_TYPE (id);
3381 decl_attr = DECL_ATTRIBUTES (id);
3382 typedef_decl = id;
3385 /* Built-in types come as identifiers. */
3386 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3388 tree t = lookup_name (id);
3389 if (TREE_TYPE (t) == error_mark_node)
3391 else if (!t || TREE_CODE (t) != TYPE_DECL)
3392 error ("`%s' fails to be a typedef or built in type",
3393 IDENTIFIER_POINTER (id));
3394 else
3396 type = TREE_TYPE (t);
3397 typedef_decl = t;
3400 else if (TREE_CODE (id) != ERROR_MARK)
3401 type = id;
3403 found:
3407 typedef_type = type;
3408 if (type)
3409 size_varies = C_TYPE_VARIABLE_SIZE (type);
3411 /* No type at all: default to `int', and set DEFAULTED_INT
3412 because it was not a user-defined typedef. */
3414 if (type == 0)
3416 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3417 | (1 << (int) RID_SIGNED)
3418 | (1 << (int) RID_UNSIGNED)
3419 | (1 << (int) RID_COMPLEX))))
3420 /* Don't warn about typedef foo = bar. */
3421 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3422 && ! in_system_header)
3424 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3425 and this is a function, or if -Wimplicit; prefer the former
3426 warning since it is more explicit. */
3427 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3428 && funcdef_flag)
3429 warn_about_return_type = 1;
3430 else if (warn_implicit_int || flag_isoc99)
3431 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3432 name);
3435 defaulted_int = 1;
3436 type = integer_type_node;
3439 /* Now process the modifiers that were specified
3440 and check for invalid combinations. */
3442 /* Long double is a special combination. */
3444 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3445 && TYPE_MAIN_VARIANT (type) == double_type_node)
3447 specbits &= ~(1 << (int) RID_LONG);
3448 type = long_double_type_node;
3451 /* Check all other uses of type modifiers. */
3453 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3454 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3456 int ok = 0;
3458 if ((specbits & 1 << (int) RID_LONG)
3459 && (specbits & 1 << (int) RID_SHORT))
3460 error ("both long and short specified for `%s'", name);
3461 else if (((specbits & 1 << (int) RID_LONG)
3462 || (specbits & 1 << (int) RID_SHORT))
3463 && explicit_char)
3464 error ("long or short specified with char for `%s'", name);
3465 else if (((specbits & 1 << (int) RID_LONG)
3466 || (specbits & 1 << (int) RID_SHORT))
3467 && TREE_CODE (type) == REAL_TYPE)
3469 static int already = 0;
3471 error ("long or short specified with floating type for `%s'", name);
3472 if (! already && ! pedantic)
3474 error ("the only valid combination is `long double'");
3475 already = 1;
3478 else if ((specbits & 1 << (int) RID_SIGNED)
3479 && (specbits & 1 << (int) RID_UNSIGNED))
3480 error ("both signed and unsigned specified for `%s'", name);
3481 else if (TREE_CODE (type) != INTEGER_TYPE)
3482 error ("long, short, signed or unsigned invalid for `%s'", name);
3483 else
3485 ok = 1;
3486 if (!explicit_int && !defaulted_int && !explicit_char)
3488 error ("long, short, signed or unsigned used invalidly for `%s'",
3489 name);
3490 ok = 0;
3494 /* Discard the type modifiers if they are invalid. */
3495 if (! ok)
3497 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3498 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3499 longlong = 0;
3503 if ((specbits & (1 << (int) RID_COMPLEX))
3504 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3506 error ("complex invalid for `%s'", name);
3507 specbits &= ~(1 << (int) RID_COMPLEX);
3510 /* Decide whether an integer type is signed or not.
3511 Optionally treat bitfields as signed by default. */
3512 if (specbits & 1 << (int) RID_UNSIGNED
3513 || (bitfield && ! flag_signed_bitfields
3514 && (explicit_int || defaulted_int || explicit_char
3515 /* A typedef for plain `int' without `signed'
3516 can be controlled just like plain `int'. */
3517 || ! (typedef_decl != 0
3518 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3519 && TREE_CODE (type) != ENUMERAL_TYPE
3520 && !(specbits & 1 << (int) RID_SIGNED)))
3522 if (longlong)
3523 type = long_long_unsigned_type_node;
3524 else if (specbits & 1 << (int) RID_LONG)
3525 type = long_unsigned_type_node;
3526 else if (specbits & 1 << (int) RID_SHORT)
3527 type = short_unsigned_type_node;
3528 else if (type == char_type_node)
3529 type = unsigned_char_type_node;
3530 else if (typedef_decl)
3531 type = c_common_unsigned_type (type);
3532 else
3533 type = unsigned_type_node;
3535 else if ((specbits & 1 << (int) RID_SIGNED)
3536 && type == char_type_node)
3537 type = signed_char_type_node;
3538 else if (longlong)
3539 type = long_long_integer_type_node;
3540 else if (specbits & 1 << (int) RID_LONG)
3541 type = long_integer_type_node;
3542 else if (specbits & 1 << (int) RID_SHORT)
3543 type = short_integer_type_node;
3545 if (specbits & 1 << (int) RID_COMPLEX)
3547 if (pedantic && !flag_isoc99)
3548 pedwarn ("ISO C90 does not support complex types");
3549 /* If we just have "complex", it is equivalent to
3550 "complex double", but if any modifiers at all are specified it is
3551 the complex form of TYPE. E.g, "complex short" is
3552 "complex short int". */
3554 if (defaulted_int && ! longlong
3555 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3556 | (1 << (int) RID_SIGNED)
3557 | (1 << (int) RID_UNSIGNED))))
3559 if (pedantic)
3560 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3561 type = complex_double_type_node;
3563 else if (type == integer_type_node)
3565 if (pedantic)
3566 pedwarn ("ISO C does not support complex integer types");
3567 type = complex_integer_type_node;
3569 else if (type == float_type_node)
3570 type = complex_float_type_node;
3571 else if (type == double_type_node)
3572 type = complex_double_type_node;
3573 else if (type == long_double_type_node)
3574 type = complex_long_double_type_node;
3575 else
3577 if (pedantic)
3578 pedwarn ("ISO C does not support complex integer types");
3579 type = build_complex_type (type);
3583 /* Figure out the type qualifiers for the declaration. There are
3584 two ways a declaration can become qualified. One is something
3585 like `const int i' where the `const' is explicit. Another is
3586 something like `typedef const int CI; CI i' where the type of the
3587 declaration contains the `const'. */
3588 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3589 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3590 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3591 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3592 if (constp > 1 && ! flag_isoc99)
3593 pedwarn ("duplicate `const'");
3594 if (restrictp > 1 && ! flag_isoc99)
3595 pedwarn ("duplicate `restrict'");
3596 if (volatilep > 1 && ! flag_isoc99)
3597 pedwarn ("duplicate `volatile'");
3598 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3599 type = TYPE_MAIN_VARIANT (type);
3600 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3601 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3602 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3604 /* Warn if two storage classes are given. Default to `auto'. */
3607 int nclasses = 0;
3609 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3610 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3611 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3612 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3613 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3615 /* "static __thread" and "extern __thread" are allowed. */
3616 if ((specbits & (1 << (int) RID_THREAD
3617 | 1 << (int) RID_STATIC
3618 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3619 nclasses++;
3621 /* Warn about storage classes that are invalid for certain
3622 kinds of declarations (parameters, typenames, etc.). */
3624 if (nclasses > 1)
3625 error ("multiple storage classes in declaration of `%s'", name);
3626 else if (funcdef_flag
3627 && (specbits
3628 & ((1 << (int) RID_REGISTER)
3629 | (1 << (int) RID_AUTO)
3630 | (1 << (int) RID_TYPEDEF)
3631 | (1 << (int) RID_THREAD))))
3633 if (specbits & 1 << (int) RID_AUTO
3634 && (pedantic || current_binding_level == global_binding_level))
3635 pedwarn ("function definition declared `auto'");
3636 if (specbits & 1 << (int) RID_REGISTER)
3637 error ("function definition declared `register'");
3638 if (specbits & 1 << (int) RID_TYPEDEF)
3639 error ("function definition declared `typedef'");
3640 if (specbits & 1 << (int) RID_THREAD)
3641 error ("function definition declared `__thread'");
3642 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3643 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3645 else if (decl_context != NORMAL && nclasses > 0)
3647 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3649 else
3651 switch (decl_context)
3653 case FIELD:
3654 error ("storage class specified for structure field `%s'",
3655 name);
3656 break;
3657 case PARM:
3658 error ("storage class specified for parameter `%s'", name);
3659 break;
3660 default:
3661 error ("storage class specified for typename");
3662 break;
3664 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3665 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3666 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3669 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3671 /* `extern' with initialization is invalid if not at top level. */
3672 if (current_binding_level == global_binding_level)
3673 warning ("`%s' initialized and declared `extern'", name);
3674 else
3675 error ("`%s' has both `extern' and initializer", name);
3677 else if (current_binding_level == global_binding_level)
3679 if (specbits & 1 << (int) RID_AUTO)
3680 error ("top-level declaration of `%s' specifies `auto'", name);
3682 else
3684 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3685 error ("nested function `%s' declared `extern'", name);
3686 else if ((specbits & (1 << (int) RID_THREAD
3687 | 1 << (int) RID_EXTERN
3688 | 1 << (int) RID_STATIC))
3689 == (1 << (int) RID_THREAD))
3691 error ("function-scope `%s' implicitly auto and declared `__thread'",
3692 name);
3693 specbits &= ~(1 << (int) RID_THREAD);
3698 /* Now figure out the structure of the declarator proper.
3699 Descend through it, creating more complex types, until we reach
3700 the declared identifier (or NULL_TREE, in an absolute declarator). */
3702 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3704 if (type == error_mark_node)
3706 declarator = TREE_OPERAND (declarator, 0);
3707 continue;
3710 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3711 an INDIRECT_REF (for *...),
3712 a CALL_EXPR (for ...(...)),
3713 a TREE_LIST (for nested attributes),
3714 an identifier (for the name being declared)
3715 or a null pointer (for the place in an absolute declarator
3716 where the name was omitted).
3717 For the last two cases, we have just exited the loop.
3719 At this point, TYPE is the type of elements of an array,
3720 or for a function to return, or for a pointer to point to.
3721 After this sequence of ifs, TYPE is the type of the
3722 array or function or pointer, and DECLARATOR has had its
3723 outermost layer removed. */
3725 if (array_ptr_quals != NULL_TREE || array_parm_static)
3727 /* Only the innermost declarator (making a parameter be of
3728 array type which is converted to pointer type)
3729 may have static or type qualifiers. */
3730 error ("static or type qualifiers in non-parameter array declarator");
3731 array_ptr_quals = NULL_TREE;
3732 array_parm_static = 0;
3735 if (TREE_CODE (declarator) == TREE_LIST)
3737 /* We encode a declarator with embedded attributes using
3738 a TREE_LIST. */
3739 tree attrs = TREE_PURPOSE (declarator);
3740 tree inner_decl;
3741 int attr_flags = 0;
3742 declarator = TREE_VALUE (declarator);
3743 inner_decl = declarator;
3744 while (inner_decl != NULL_TREE
3745 && TREE_CODE (inner_decl) == TREE_LIST)
3746 inner_decl = TREE_VALUE (inner_decl);
3747 if (inner_decl == NULL_TREE
3748 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3749 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3750 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3751 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3752 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3753 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3754 returned_attrs = decl_attributes (&type,
3755 chainon (returned_attrs, attrs),
3756 attr_flags);
3758 else if (TREE_CODE (declarator) == ARRAY_REF)
3760 tree itype = NULL_TREE;
3761 tree size = TREE_OPERAND (declarator, 1);
3762 /* The index is a signed object `sizetype' bits wide. */
3763 tree index_type = c_common_signed_type (sizetype);
3765 array_ptr_quals = TREE_TYPE (declarator);
3766 array_parm_static = TREE_STATIC (declarator);
3768 declarator = TREE_OPERAND (declarator, 0);
3770 /* Check for some types that there cannot be arrays of. */
3772 if (VOID_TYPE_P (type))
3774 error ("declaration of `%s' as array of voids", name);
3775 type = error_mark_node;
3778 if (TREE_CODE (type) == FUNCTION_TYPE)
3780 error ("declaration of `%s' as array of functions", name);
3781 type = error_mark_node;
3784 if (pedantic && flexible_array_type_p (type))
3785 pedwarn ("invalid use of structure with flexible array member");
3787 if (size == error_mark_node)
3788 type = error_mark_node;
3790 if (type == error_mark_node)
3791 continue;
3793 /* If size was specified, set ITYPE to a range-type for that size.
3794 Otherwise, ITYPE remains null. finish_decl may figure it out
3795 from an initial value. */
3797 if (size)
3799 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3800 STRIP_TYPE_NOPS (size);
3802 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3804 error ("size of array `%s' has non-integer type", name);
3805 size = integer_one_node;
3808 if (pedantic && integer_zerop (size))
3809 pedwarn ("ISO C forbids zero-size array `%s'", name);
3811 if (TREE_CODE (size) == INTEGER_CST)
3813 constant_expression_warning (size);
3814 if (tree_int_cst_sgn (size) < 0)
3816 error ("size of array `%s' is negative", name);
3817 size = integer_one_node;
3820 else
3822 /* Make sure the array size remains visibly nonconstant
3823 even if it is (eg) a const variable with known value. */
3824 size_varies = 1;
3826 if (!flag_isoc99 && pedantic)
3828 if (TREE_CONSTANT (size))
3829 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3830 name);
3831 else
3832 pedwarn ("ISO C90 forbids variable-size array `%s'",
3833 name);
3837 if (integer_zerop (size))
3839 /* A zero-length array cannot be represented with an
3840 unsigned index type, which is what we'll get with
3841 build_index_type. Create an open-ended range instead. */
3842 itype = build_range_type (sizetype, size, NULL_TREE);
3844 else
3846 /* Compute the maximum valid index, that is, size - 1.
3847 Do the calculation in index_type, so that if it is
3848 a variable the computations will be done in the
3849 proper mode. */
3850 itype = fold (build (MINUS_EXPR, index_type,
3851 convert (index_type, size),
3852 convert (index_type, size_one_node)));
3854 /* If that overflowed, the array is too big.
3855 ??? While a size of INT_MAX+1 technically shouldn't
3856 cause an overflow (because we subtract 1), the overflow
3857 is recorded during the conversion to index_type, before
3858 the subtraction. Handling this case seems like an
3859 unnecessary complication. */
3860 if (TREE_OVERFLOW (itype))
3862 error ("size of array `%s' is too large", name);
3863 type = error_mark_node;
3864 continue;
3867 if (size_varies)
3869 /* We must be able to distinguish the
3870 SAVE_EXPR_CONTEXT for the variably-sized type
3871 so that we can set it correctly in
3872 set_save_expr_context. The convention is
3873 that all SAVE_EXPRs that need to be reset
3874 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3875 tree cfd = current_function_decl;
3876 if (decl_context == PARM)
3877 current_function_decl = NULL_TREE;
3878 itype = variable_size (itype);
3879 if (decl_context == PARM)
3880 current_function_decl = cfd;
3882 itype = build_index_type (itype);
3885 else if (decl_context == FIELD)
3887 if (pedantic && !flag_isoc99 && !in_system_header)
3888 pedwarn ("ISO C90 does not support flexible array members");
3890 /* ISO C99 Flexible array members are effectively identical
3891 to GCC's zero-length array extension. */
3892 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3895 /* If pedantic, complain about arrays of incomplete types. */
3897 if (pedantic && !COMPLETE_TYPE_P (type))
3898 pedwarn ("array type has incomplete element type");
3900 #if 0
3901 /* We shouldn't have a function type here at all!
3902 Functions aren't allowed as array elements. */
3903 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3904 && (constp || volatilep))
3905 pedwarn ("ISO C forbids const or volatile function types");
3906 #endif
3908 /* Build the array type itself, then merge any constancy or
3909 volatility into the target type. We must do it in this order
3910 to ensure that the TYPE_MAIN_VARIANT field of the array type
3911 is set correctly. */
3913 type = build_array_type (type, itype);
3914 if (type_quals)
3915 type = c_build_qualified_type (type, type_quals);
3917 if (size_varies)
3918 C_TYPE_VARIABLE_SIZE (type) = 1;
3920 /* The GCC extension for zero-length arrays differs from
3921 ISO flexible array members in that sizeof yields zero. */
3922 if (size && integer_zerop (size))
3924 layout_type (type);
3925 TYPE_SIZE (type) = bitsize_zero_node;
3926 TYPE_SIZE_UNIT (type) = size_zero_node;
3928 if (decl_context != PARM
3929 && (array_ptr_quals != NULL_TREE || array_parm_static))
3931 error ("static or type qualifiers in non-parameter array declarator");
3932 array_ptr_quals = NULL_TREE;
3933 array_parm_static = 0;
3936 else if (TREE_CODE (declarator) == CALL_EXPR)
3938 tree arg_types;
3940 /* Declaring a function type.
3941 Make sure we have a valid type for the function to return. */
3942 if (type == error_mark_node)
3943 continue;
3945 size_varies = 0;
3947 /* Warn about some types functions can't return. */
3949 if (TREE_CODE (type) == FUNCTION_TYPE)
3951 error ("`%s' declared as function returning a function", name);
3952 type = integer_type_node;
3954 if (TREE_CODE (type) == ARRAY_TYPE)
3956 error ("`%s' declared as function returning an array", name);
3957 type = integer_type_node;
3960 /* Construct the function type and go to the next
3961 inner layer of declarator. */
3963 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3964 funcdef_flag
3965 /* Say it's a definition
3966 only for the CALL_EXPR
3967 closest to the identifier. */
3968 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3969 /* Type qualifiers before the return type of the function
3970 qualify the return type, not the function type. */
3971 if (type_quals)
3973 /* Type qualifiers on a function return type are normally
3974 permitted by the standard but have no effect, so give a
3975 warning at -Wextra. Qualifiers on a void return type have
3976 meaning as a GNU extension, and are banned on function
3977 definitions in ISO C. FIXME: strictly we shouldn't
3978 pedwarn for qualified void return types except on function
3979 definitions, but not doing so could lead to the undesirable
3980 state of a "volatile void" function return type not being
3981 warned about, and a use of the function being compiled
3982 with GNU semantics, with no diagnostics under -pedantic. */
3983 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3984 pedwarn ("ISO C forbids qualified void function return type");
3985 else if (extra_warnings
3986 && !(VOID_TYPE_P (type)
3987 && type_quals == TYPE_QUAL_VOLATILE))
3988 warning ("type qualifiers ignored on function return type");
3990 type = c_build_qualified_type (type, type_quals);
3992 type_quals = TYPE_UNQUALIFIED;
3994 type = build_function_type (type, arg_types);
3995 declarator = TREE_OPERAND (declarator, 0);
3997 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3998 the formal parameter list of this FUNCTION_TYPE to point to
3999 the FUNCTION_TYPE node itself. */
4002 tree link;
4004 for (link = last_function_parm_tags;
4005 link;
4006 link = TREE_CHAIN (link))
4007 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4010 else if (TREE_CODE (declarator) == INDIRECT_REF)
4012 /* Merge any constancy or volatility into the target type
4013 for the pointer. */
4015 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4016 && type_quals)
4017 pedwarn ("ISO C forbids qualified function types");
4018 if (type_quals)
4019 type = c_build_qualified_type (type, type_quals);
4020 type_quals = TYPE_UNQUALIFIED;
4021 size_varies = 0;
4023 type = build_pointer_type (type);
4025 /* Process a list of type modifier keywords
4026 (such as const or volatile) that were given inside the `*'. */
4028 if (TREE_TYPE (declarator))
4030 tree typemodlist;
4031 int erred = 0;
4033 constp = 0;
4034 volatilep = 0;
4035 restrictp = 0;
4036 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4037 typemodlist = TREE_CHAIN (typemodlist))
4039 tree qualifier = TREE_VALUE (typemodlist);
4041 if (C_IS_RESERVED_WORD (qualifier))
4043 if (C_RID_CODE (qualifier) == RID_CONST)
4044 constp++;
4045 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4046 volatilep++;
4047 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4048 restrictp++;
4049 else
4050 erred++;
4052 else
4053 erred++;
4056 if (erred)
4057 error ("invalid type modifier within pointer declarator");
4058 if (constp > 1 && ! flag_isoc99)
4059 pedwarn ("duplicate `const'");
4060 if (volatilep > 1 && ! flag_isoc99)
4061 pedwarn ("duplicate `volatile'");
4062 if (restrictp > 1 && ! flag_isoc99)
4063 pedwarn ("duplicate `restrict'");
4065 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4066 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4067 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4070 declarator = TREE_OPERAND (declarator, 0);
4072 else
4073 abort ();
4077 /* Now TYPE has the actual type. */
4079 /* Did array size calculations overflow? */
4081 if (TREE_CODE (type) == ARRAY_TYPE
4082 && COMPLETE_TYPE_P (type)
4083 && TREE_OVERFLOW (TYPE_SIZE (type)))
4085 error ("size of array `%s' is too large", name);
4086 /* If we proceed with the array type as it is, we'll eventually
4087 crash in tree_low_cst(). */
4088 type = error_mark_node;
4091 /* If this is declaring a typedef name, return a TYPE_DECL. */
4093 if (specbits & (1 << (int) RID_TYPEDEF))
4095 tree decl;
4096 /* Note that the grammar rejects storage classes
4097 in typenames, fields or parameters */
4098 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4099 && type_quals)
4100 pedwarn ("ISO C forbids qualified function types");
4101 if (type_quals)
4102 type = c_build_qualified_type (type, type_quals);
4103 decl = build_decl (TYPE_DECL, declarator, type);
4104 if ((specbits & (1 << (int) RID_SIGNED))
4105 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4106 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4107 decl_attributes (&decl, returned_attrs, 0);
4108 return decl;
4111 /* Detect the case of an array type of unspecified size
4112 which came, as such, direct from a typedef name.
4113 We must copy the type, so that each identifier gets
4114 a distinct type, so that each identifier's size can be
4115 controlled separately by its own initializer. */
4117 if (type != 0 && typedef_type != 0
4118 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4119 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4121 type = build_array_type (TREE_TYPE (type), 0);
4122 if (size_varies)
4123 C_TYPE_VARIABLE_SIZE (type) = 1;
4126 /* If this is a type name (such as, in a cast or sizeof),
4127 compute the type and return it now. */
4129 if (decl_context == TYPENAME)
4131 /* Note that the grammar rejects storage classes
4132 in typenames, fields or parameters */
4133 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4134 && type_quals)
4135 pedwarn ("ISO C forbids const or volatile function types");
4136 if (type_quals)
4137 type = c_build_qualified_type (type, type_quals);
4138 decl_attributes (&type, returned_attrs, 0);
4139 return type;
4142 /* Aside from typedefs and type names (handle above),
4143 `void' at top level (not within pointer)
4144 is allowed only in public variables.
4145 We don't complain about parms either, but that is because
4146 a better error message can be made later. */
4148 if (VOID_TYPE_P (type) && decl_context != PARM
4149 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4150 && ((specbits & (1 << (int) RID_EXTERN))
4151 || (current_binding_level == global_binding_level
4152 && !(specbits
4153 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4155 error ("variable or field `%s' declared void", name);
4156 type = integer_type_node;
4159 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4160 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4163 tree decl;
4165 if (decl_context == PARM)
4167 tree type_as_written;
4168 tree promoted_type;
4170 /* A parameter declared as an array of T is really a pointer to T.
4171 One declared as a function is really a pointer to a function. */
4173 if (TREE_CODE (type) == ARRAY_TYPE)
4175 /* Transfer const-ness of array into that of type pointed to. */
4176 type = TREE_TYPE (type);
4177 if (type_quals)
4178 type = c_build_qualified_type (type, type_quals);
4179 type = build_pointer_type (type);
4180 type_quals = TYPE_UNQUALIFIED;
4181 if (array_ptr_quals)
4183 tree new_ptr_quals, new_ptr_attrs;
4184 int erred = 0;
4185 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4186 /* We don't yet implement attributes in this context. */
4187 if (new_ptr_attrs != NULL_TREE)
4188 warning ("attributes in parameter array declarator ignored");
4190 constp = 0;
4191 volatilep = 0;
4192 restrictp = 0;
4193 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4195 tree qualifier = TREE_VALUE (new_ptr_quals);
4197 if (C_IS_RESERVED_WORD (qualifier))
4199 if (C_RID_CODE (qualifier) == RID_CONST)
4200 constp++;
4201 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4202 volatilep++;
4203 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4204 restrictp++;
4205 else
4206 erred++;
4208 else
4209 erred++;
4212 if (erred)
4213 error ("invalid type modifier within array declarator");
4215 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4216 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4217 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4219 size_varies = 0;
4221 else if (TREE_CODE (type) == FUNCTION_TYPE)
4223 if (pedantic && type_quals)
4224 pedwarn ("ISO C forbids qualified function types");
4225 if (type_quals)
4226 type = c_build_qualified_type (type, type_quals);
4227 type = build_pointer_type (type);
4228 type_quals = TYPE_UNQUALIFIED;
4230 else if (type_quals)
4231 type = c_build_qualified_type (type, type_quals);
4233 type_as_written = type;
4235 decl = build_decl (PARM_DECL, declarator, type);
4236 if (size_varies)
4237 C_DECL_VARIABLE_SIZE (decl) = 1;
4239 /* Compute the type actually passed in the parmlist,
4240 for the case where there is no prototype.
4241 (For example, shorts and chars are passed as ints.)
4242 When there is a prototype, this is overridden later. */
4244 if (type == error_mark_node)
4245 promoted_type = type;
4246 else
4247 promoted_type = c_type_promotes_to (type);
4249 DECL_ARG_TYPE (decl) = promoted_type;
4250 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4252 else if (decl_context == FIELD)
4254 /* Structure field. It may not be a function. */
4256 if (TREE_CODE (type) == FUNCTION_TYPE)
4258 error ("field `%s' declared as a function", name);
4259 type = build_pointer_type (type);
4261 else if (TREE_CODE (type) != ERROR_MARK
4262 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4264 error ("field `%s' has incomplete type", name);
4265 type = error_mark_node;
4267 /* Move type qualifiers down to element of an array. */
4268 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4270 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4271 type_quals),
4272 TYPE_DOMAIN (type));
4273 #if 0
4274 /* Leave the field const or volatile as well. */
4275 type_quals = TYPE_UNQUALIFIED;
4276 #endif
4278 decl = build_decl (FIELD_DECL, declarator, type);
4279 DECL_NONADDRESSABLE_P (decl) = bitfield;
4281 if (size_varies)
4282 C_DECL_VARIABLE_SIZE (decl) = 1;
4284 else if (TREE_CODE (type) == FUNCTION_TYPE)
4286 /* Every function declaration is "external"
4287 except for those which are inside a function body
4288 in which `auto' is used.
4289 That is a case not specified by ANSI C,
4290 and we use it for forward declarations for nested functions. */
4291 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4292 || current_binding_level == global_binding_level);
4294 if (specbits & (1 << (int) RID_AUTO)
4295 && (pedantic || current_binding_level == global_binding_level))
4296 pedwarn ("invalid storage class for function `%s'", name);
4297 if (specbits & (1 << (int) RID_REGISTER))
4298 error ("invalid storage class for function `%s'", name);
4299 if (specbits & (1 << (int) RID_THREAD))
4300 error ("invalid storage class for function `%s'", name);
4301 /* Function declaration not at top level.
4302 Storage classes other than `extern' are not allowed
4303 and `extern' makes no difference. */
4304 if (current_binding_level != global_binding_level
4305 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4306 && pedantic)
4307 pedwarn ("invalid storage class for function `%s'", name);
4309 decl = build_decl (FUNCTION_DECL, declarator, type);
4310 decl = build_decl_attribute_variant (decl, decl_attr);
4312 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4313 ggc_alloc_cleared (sizeof (struct lang_decl));
4315 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4316 pedwarn ("ISO C forbids qualified function types");
4318 /* GNU C interprets a `volatile void' return type to indicate
4319 that the function does not return. */
4320 if ((type_quals & TYPE_QUAL_VOLATILE)
4321 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4322 warning ("`noreturn' function returns non-void value");
4324 if (extern_ref)
4325 DECL_EXTERNAL (decl) = 1;
4326 /* Record absence of global scope for `static' or `auto'. */
4327 TREE_PUBLIC (decl)
4328 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4330 if (defaulted_int)
4331 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4333 /* Record presence of `inline', if it is reasonable. */
4334 if (MAIN_NAME_P (declarator))
4336 if (inlinep)
4337 warning ("cannot inline function `main'");
4339 else if (inlinep)
4341 /* Assume that otherwise the function can be inlined. */
4342 DECL_DECLARED_INLINE_P (decl) = 1;
4344 /* Do not mark bare declarations as DECL_INLINE. Doing so
4345 in the presence of multiple declarations can result in
4346 the abstract origin pointing between the declarations,
4347 which will confuse dwarf2out. */
4348 if (initialized)
4350 DECL_INLINE (decl) = 1;
4351 if (specbits & (1 << (int) RID_EXTERN))
4352 current_extern_inline = 1;
4355 /* If -finline-functions, assume it can be inlined. This does
4356 two things: let the function be deferred until it is actually
4357 needed, and let dwarf2 know that the function is inlinable. */
4358 else if (flag_inline_trees == 2 && initialized)
4360 if (!DECL_INLINE (decl))
4361 DID_INLINE_FUNC (decl) = 1;
4362 DECL_INLINE (decl) = 1;
4363 DECL_DECLARED_INLINE_P (decl) = 0;
4366 else
4368 /* It's a variable. */
4369 /* An uninitialized decl with `extern' is a reference. */
4370 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4372 /* Move type qualifiers down to element of an array. */
4373 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4375 int saved_align = TYPE_ALIGN(type);
4376 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4377 type_quals),
4378 TYPE_DOMAIN (type));
4379 TYPE_ALIGN (type) = saved_align;
4380 #if 0 /* Leave the variable const or volatile as well. */
4381 type_quals = TYPE_UNQUALIFIED;
4382 #endif
4384 else if (type_quals)
4385 type = c_build_qualified_type (type, type_quals);
4387 /* It is invalid to create an `extern' declaration for a
4388 variable if there is a global declaration that is
4389 `static'. */
4390 if (extern_ref && current_binding_level != global_binding_level)
4392 tree global_decl;
4394 global_decl = identifier_global_value (declarator);
4395 if (global_decl
4396 && TREE_CODE (global_decl) == VAR_DECL
4397 && !TREE_PUBLIC (global_decl))
4398 error ("variable previously declared `static' redeclared "
4399 "`extern'");
4402 decl = build_decl (VAR_DECL, declarator, type);
4403 if (size_varies)
4404 C_DECL_VARIABLE_SIZE (decl) = 1;
4406 if (inlinep)
4407 pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4409 DECL_EXTERNAL (decl) = extern_ref;
4411 /* At top level, the presence of a `static' or `register' storage
4412 class specifier, or the absence of all storage class specifiers
4413 makes this declaration a definition (perhaps tentative). Also,
4414 the absence of both `static' and `register' makes it public. */
4415 if (current_binding_level == global_binding_level)
4417 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4418 | (1 << (int) RID_REGISTER)));
4419 TREE_STATIC (decl) = !extern_ref;
4421 /* Not at top level, only `static' makes a static definition. */
4422 else
4424 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4425 TREE_PUBLIC (decl) = extern_ref;
4428 if (specbits & 1 << (int) RID_THREAD)
4430 if (targetm.have_tls)
4431 DECL_THREAD_LOCAL (decl) = 1;
4432 else
4433 /* A mere warning is sure to result in improper semantics
4434 at runtime. Don't bother to allow this to compile. */
4435 error ("thread-local storage not supported for this target");
4439 /* Record `register' declaration for warnings on &
4440 and in case doing stupid register allocation. */
4442 if (specbits & (1 << (int) RID_REGISTER))
4443 DECL_REGISTER (decl) = 1;
4445 /* Record constancy and volatility. */
4446 c_apply_type_quals_to_decl (type_quals, decl);
4448 /* If a type has volatile components, it should be stored in memory.
4449 Otherwise, the fact that those components are volatile
4450 will be ignored, and would even crash the compiler. */
4451 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4452 c_mark_addressable (decl);
4454 decl_attributes (&decl, returned_attrs, 0);
4456 return decl;
4460 /* Decode the parameter-list info for a function type or function definition.
4461 The argument is the value returned by `get_parm_info' (or made in parse.y
4462 if there is an identifier list instead of a parameter decl list).
4463 These two functions are separate because when a function returns
4464 or receives functions then each is called multiple times but the order
4465 of calls is different. The last call to `grokparms' is always the one
4466 that contains the formal parameter names of a function definition.
4468 Store in `last_function_parms' a chain of the decls of parms.
4469 Also store in `last_function_parm_tags' a chain of the struct, union,
4470 and enum tags declared among the parms.
4472 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4474 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4475 a mere declaration. A nonempty identifier-list gets an error message
4476 when FUNCDEF_FLAG is zero. */
4478 static tree
4479 grokparms (parms_info, funcdef_flag)
4480 tree parms_info;
4481 int funcdef_flag;
4483 tree first_parm = TREE_CHAIN (parms_info);
4485 last_function_parms = TREE_PURPOSE (parms_info);
4486 last_function_parm_tags = TREE_VALUE (parms_info);
4488 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4489 && !in_system_header)
4490 warning ("function declaration isn't a prototype");
4492 if (first_parm != 0
4493 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4495 if (! funcdef_flag)
4496 pedwarn ("parameter names (without types) in function declaration");
4498 last_function_parms = first_parm;
4499 return 0;
4501 else
4503 tree parm;
4504 tree typelt;
4505 /* We no longer test FUNCDEF_FLAG.
4506 If the arg types are incomplete in a declaration,
4507 they must include undefined tags.
4508 These tags can never be defined in the scope of the declaration,
4509 so the types can never be completed,
4510 and no call can be compiled successfully. */
4511 #if 0
4512 /* In a fcn definition, arg types must be complete. */
4513 if (funcdef_flag)
4514 #endif
4515 for (parm = last_function_parms, typelt = first_parm;
4516 parm;
4517 parm = TREE_CHAIN (parm))
4518 /* Skip over any enumeration constants declared here. */
4519 if (TREE_CODE (parm) == PARM_DECL)
4521 /* Barf if the parameter itself has an incomplete type. */
4522 tree type = TREE_VALUE (typelt);
4523 if (type == error_mark_node)
4524 continue;
4525 if (!COMPLETE_TYPE_P (type))
4527 if (funcdef_flag && DECL_NAME (parm) != 0)
4528 error ("parameter `%s' has incomplete type",
4529 IDENTIFIER_POINTER (DECL_NAME (parm)));
4530 else
4531 warning ("parameter has incomplete type");
4532 if (funcdef_flag)
4534 TREE_VALUE (typelt) = error_mark_node;
4535 TREE_TYPE (parm) = error_mark_node;
4538 #if 0
4539 /* This has been replaced by parm_tags_warning, which
4540 uses a more accurate criterion for what to warn
4541 about. */
4542 else
4544 /* Now warn if is a pointer to an incomplete type. */
4545 while (TREE_CODE (type) == POINTER_TYPE
4546 || TREE_CODE (type) == REFERENCE_TYPE)
4547 type = TREE_TYPE (type);
4548 type = TYPE_MAIN_VARIANT (type);
4549 if (!COMPLETE_TYPE_P (type))
4551 if (DECL_NAME (parm) != 0)
4552 warning ("parameter `%s' points to incomplete type",
4553 IDENTIFIER_POINTER (DECL_NAME (parm)));
4554 else
4555 warning ("parameter points to incomplete type");
4558 #endif
4559 typelt = TREE_CHAIN (typelt);
4562 return first_parm;
4566 /* Return a tree_list node with info on a parameter list just parsed.
4567 The TREE_PURPOSE is a chain of decls of those parms.
4568 The TREE_VALUE is a list of structure, union and enum tags defined.
4569 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4570 This tree_list node is later fed to `grokparms'.
4572 VOID_AT_END nonzero means append `void' to the end of the type-list.
4573 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4575 tree
4576 get_parm_info (void_at_end)
4577 int void_at_end;
4579 tree decl, t;
4580 tree types = 0;
4581 int erred = 0;
4582 tree tags = gettags ();
4583 tree parms = getdecls ();
4584 tree new_parms = 0;
4585 tree order = current_binding_level->parm_order;
4587 /* Just `void' (and no ellipsis) is special. There are really no parms.
4588 But if the `void' is qualified (by `const' or `volatile') or has a
4589 storage class specifier (`register'), then the behavior is undefined;
4590 by not counting it as the special case of `void' we will cause an
4591 error later. Typedefs for `void' are OK (see DR#157). */
4592 if (void_at_end && parms != 0
4593 && TREE_CHAIN (parms) == 0
4594 && VOID_TYPE_P (TREE_TYPE (parms))
4595 && ! TREE_THIS_VOLATILE (parms)
4596 && ! TREE_READONLY (parms)
4597 && ! DECL_REGISTER (parms)
4598 && DECL_NAME (parms) == 0)
4600 parms = NULL_TREE;
4601 storedecls (NULL_TREE);
4602 return tree_cons (NULL_TREE, NULL_TREE,
4603 tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4606 /* Extract enumerator values and other non-parms declared with the parms.
4607 Likewise any forward parm decls that didn't have real parm decls. */
4608 for (decl = parms; decl;)
4610 tree next = TREE_CHAIN (decl);
4612 if (TREE_CODE (decl) != PARM_DECL)
4614 TREE_CHAIN (decl) = new_parms;
4615 new_parms = decl;
4617 else if (TREE_ASM_WRITTEN (decl))
4619 error_with_decl (decl,
4620 "parameter `%s' has just a forward declaration");
4621 TREE_CHAIN (decl) = new_parms;
4622 new_parms = decl;
4624 decl = next;
4627 /* Put the parm decls back in the order they were in in the parm list. */
4628 for (t = order; t; t = TREE_CHAIN (t))
4630 if (TREE_CHAIN (t))
4631 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4632 else
4633 TREE_CHAIN (TREE_VALUE (t)) = 0;
4636 new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4637 new_parms);
4639 /* Store the parmlist in the binding level since the old one
4640 is no longer a valid list. (We have changed the chain pointers.) */
4641 storedecls (new_parms);
4643 for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4644 /* There may also be declarations for enumerators if an enumeration
4645 type is declared among the parms. Ignore them here. */
4646 if (TREE_CODE (decl) == PARM_DECL)
4648 /* Since there is a prototype,
4649 args are passed in their declared types. */
4650 tree type = TREE_TYPE (decl);
4651 DECL_ARG_TYPE (decl) = type;
4652 if (PROMOTE_PROTOTYPES
4653 && INTEGRAL_TYPE_P (type)
4654 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4655 DECL_ARG_TYPE (decl) = integer_type_node;
4657 types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4658 if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4659 && DECL_NAME (decl) == 0)
4661 error ("`void' in parameter list must be the entire list");
4662 erred = 1;
4666 if (void_at_end)
4667 return tree_cons (new_parms, tags,
4668 nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4670 return tree_cons (new_parms, tags, nreverse (types));
4673 /* At end of parameter list, warn about any struct, union or enum tags
4674 defined within. Do so because these types cannot ever become complete. */
4676 void
4677 parmlist_tags_warning ()
4679 tree elt;
4680 static int already;
4682 for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4684 enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4685 /* An anonymous union parm type is meaningful as a GNU extension.
4686 So don't warn for that. */
4687 if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4688 continue;
4689 if (TREE_PURPOSE (elt) != 0)
4691 if (code == RECORD_TYPE)
4692 warning ("`struct %s' declared inside parameter list",
4693 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4694 else if (code == UNION_TYPE)
4695 warning ("`union %s' declared inside parameter list",
4696 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4697 else
4698 warning ("`enum %s' declared inside parameter list",
4699 IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4701 else
4703 /* For translation these need to be separate warnings */
4704 if (code == RECORD_TYPE)
4705 warning ("anonymous struct declared inside parameter list");
4706 else if (code == UNION_TYPE)
4707 warning ("anonymous union declared inside parameter list");
4708 else
4709 warning ("anonymous enum declared inside parameter list");
4711 if (! already)
4713 warning ("its scope is only this definition or declaration, which is probably not what you want");
4714 already = 1;
4719 /* Get the struct, enum or union (CODE says which) with tag NAME.
4720 Define the tag as a forward-reference if it is not defined. */
4722 tree
4723 xref_tag (code, name)
4724 enum tree_code code;
4725 tree name;
4727 /* If a cross reference is requested, look up the type
4728 already defined for this tag and return it. */
4730 tree ref = lookup_tag (code, name, 0);
4731 /* If this is the right type of tag, return what we found.
4732 (This reference will be shadowed by shadow_tag later if appropriate.)
4733 If this is the wrong type of tag, do not return it. If it was the
4734 wrong type in the same binding level, we will have had an error
4735 message already; if in a different binding level and declaring
4736 a name, pending_xref_error will give an error message; but if in a
4737 different binding level and not declaring a name, this tag should
4738 shadow the previous declaration of a different type of tag, and
4739 this would not work properly if we return the reference found.
4740 (For example, with "struct foo" in an outer scope, "union foo;"
4741 must shadow that tag with a new one of union type.) */
4742 if (ref && TREE_CODE (ref) == code)
4743 return ref;
4745 /* If no such tag is yet defined, create a forward-reference node
4746 and record it as the "definition".
4747 When a real declaration of this type is found,
4748 the forward-reference will be altered into a real type. */
4750 ref = make_node (code);
4751 if (code == ENUMERAL_TYPE)
4753 /* Give the type a default layout like unsigned int
4754 to avoid crashing if it does not get defined. */
4755 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4756 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4757 TYPE_USER_ALIGN (ref) = 0;
4758 TREE_UNSIGNED (ref) = 1;
4759 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4760 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4761 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4764 pushtag (name, ref);
4766 return ref;
4769 /* Make sure that the tag NAME is defined *in the current binding level*
4770 at least as a forward reference.
4771 CODE says which kind of tag NAME ought to be. */
4773 tree
4774 start_struct (code, name)
4775 enum tree_code code;
4776 tree name;
4778 /* If there is already a tag defined at this binding level
4779 (as a forward reference), just return it. */
4781 tree ref = 0;
4783 if (name != 0)
4784 ref = lookup_tag (code, name, 1);
4785 if (ref && TREE_CODE (ref) == code)
4787 if (TYPE_FIELDS (ref))
4789 if (code == UNION_TYPE)
4790 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4791 else
4792 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4795 else
4797 /* Otherwise create a forward-reference just so the tag is in scope. */
4799 ref = make_node (code);
4800 pushtag (name, ref);
4803 C_TYPE_BEING_DEFINED (ref) = 1;
4804 TYPE_PACKED (ref) = flag_pack_struct;
4805 return ref;
4808 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4809 of a structure component, returning a FIELD_DECL node.
4810 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4812 This is done during the parsing of the struct declaration.
4813 The FIELD_DECL nodes are chained together and the lot of them
4814 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4816 tree
4817 grokfield (filename, line, declarator, declspecs, width)
4818 const char *filename ATTRIBUTE_UNUSED;
4819 int line ATTRIBUTE_UNUSED;
4820 tree declarator, declspecs, width;
4822 tree value;
4824 if (declarator == NULL_TREE && width == NULL_TREE)
4826 /* This is an unnamed decl.
4828 If we have something of the form "union { list } ;" then this
4829 is the anonymous union extension. Similarly for struct.
4831 If this is something of the form "struct foo;", then
4832 If MS extensions are enabled, this is handled as an
4833 anonymous struct.
4834 Otherwise this is a forward declaration of a structure tag.
4836 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4837 If MS extensions are enabled and foo names a structure, then
4838 again this is an anonymous struct.
4839 Otherwise this is an error.
4841 Oh what a horrid tangled web we weave. I wonder if MS consciously
4842 took this from Plan 9 or if it was an accident of implementation
4843 that took root before someone noticed the bug... */
4845 tree type = TREE_VALUE (declspecs);
4847 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4848 type = TREE_TYPE (type);
4849 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4851 if (flag_ms_extensions)
4852 ; /* ok */
4853 else if (flag_iso)
4854 goto warn_unnamed_field;
4855 else if (TYPE_NAME (type) == NULL)
4856 ; /* ok */
4857 else
4858 goto warn_unnamed_field;
4860 else
4862 warn_unnamed_field:
4863 warning ("declaration does not declare anything");
4864 return NULL_TREE;
4868 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4870 finish_decl (value, NULL_TREE, NULL_TREE);
4871 DECL_INITIAL (value) = width;
4873 if (flag_objc)
4874 objc_check_decl (value);
4875 return value;
4878 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4879 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4880 ATTRIBUTES are attributes to be applied to the structure. */
4882 tree
4883 finish_struct (t, fieldlist, attributes)
4884 tree t;
4885 tree fieldlist;
4886 tree attributes;
4888 tree x;
4889 int toplevel = global_binding_level == current_binding_level;
4890 int saw_named_field;
4892 /* If this type was previously laid out as a forward reference,
4893 make sure we lay it out again. */
4895 TYPE_SIZE (t) = 0;
4897 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4899 /* Nameless union parm types are useful as GCC extension. */
4900 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4901 /* Otherwise, warn about any struct or union def. in parmlist. */
4902 if (in_parm_level_p ())
4904 if (pedantic)
4905 pedwarn ("%s defined inside parms",
4906 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4907 else
4908 warning ("%s defined inside parms",
4909 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4912 if (pedantic)
4914 for (x = fieldlist; x; x = TREE_CHAIN (x))
4915 if (DECL_NAME (x) != 0)
4916 break;
4918 if (x == 0)
4919 pedwarn ("%s has no %s",
4920 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4921 fieldlist ? _("named members") : _("members"));
4924 /* Install struct as DECL_CONTEXT of each field decl.
4925 Also process specified field sizes,m which is found in the DECL_INITIAL.
4926 Store 0 there, except for ": 0" fields (so we can find them
4927 and delete them, below). */
4929 saw_named_field = 0;
4930 for (x = fieldlist; x; x = TREE_CHAIN (x))
4932 DECL_CONTEXT (x) = t;
4933 DECL_PACKED (x) |= TYPE_PACKED (t);
4935 /* If any field is const, the structure type is pseudo-const. */
4936 if (TREE_READONLY (x))
4937 C_TYPE_FIELDS_READONLY (t) = 1;
4938 else
4940 /* A field that is pseudo-const makes the structure likewise. */
4941 tree t1 = TREE_TYPE (x);
4942 while (TREE_CODE (t1) == ARRAY_TYPE)
4943 t1 = TREE_TYPE (t1);
4944 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4945 && C_TYPE_FIELDS_READONLY (t1))
4946 C_TYPE_FIELDS_READONLY (t) = 1;
4949 /* Any field that is volatile means variables of this type must be
4950 treated in some ways as volatile. */
4951 if (TREE_THIS_VOLATILE (x))
4952 C_TYPE_FIELDS_VOLATILE (t) = 1;
4954 /* Any field of nominal variable size implies structure is too. */
4955 if (C_DECL_VARIABLE_SIZE (x))
4956 C_TYPE_VARIABLE_SIZE (t) = 1;
4958 /* Detect invalid nested redefinition. */
4959 if (TREE_TYPE (x) == t)
4960 error ("nested redefinition of `%s'",
4961 IDENTIFIER_POINTER (TYPE_NAME (t)));
4963 /* Detect invalid bit-field size. */
4964 if (DECL_INITIAL (x))
4965 STRIP_NOPS (DECL_INITIAL (x));
4966 if (DECL_INITIAL (x))
4968 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4969 constant_expression_warning (DECL_INITIAL (x));
4970 else
4972 error_with_decl (x,
4973 "bit-field `%s' width not an integer constant");
4974 DECL_INITIAL (x) = NULL;
4978 /* Detect invalid bit-field type. */
4979 if (DECL_INITIAL (x)
4980 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4981 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4982 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4984 error_with_decl (x, "bit-field `%s' has invalid type");
4985 DECL_INITIAL (x) = NULL;
4988 if (DECL_INITIAL (x) && pedantic
4989 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4990 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4991 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
4992 /* Accept an enum that's equivalent to int or unsigned int. */
4993 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4994 && (TYPE_PRECISION (TREE_TYPE (x))
4995 == TYPE_PRECISION (integer_type_node))))
4996 pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
4998 /* Detect and ignore out of range field width and process valid
4999 field widths. */
5000 if (DECL_INITIAL (x))
5002 int max_width
5003 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5004 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5006 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5007 error_with_decl (x, "negative width in bit-field `%s'");
5008 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5009 pedwarn_with_decl (x, "width of `%s' exceeds its type");
5010 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5011 error_with_decl (x, "zero width for bit-field `%s'");
5012 else
5014 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5015 unsigned HOST_WIDE_INT width
5016 = tree_low_cst (DECL_INITIAL (x), 1);
5018 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5019 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5020 TREE_UNSIGNED (TREE_TYPE (x)))
5021 || (width
5022 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5023 TREE_UNSIGNED (TREE_TYPE (x))))))
5024 warning_with_decl (x,
5025 "`%s' is narrower than values of its type");
5027 DECL_SIZE (x) = bitsize_int (width);
5028 DECL_BIT_FIELD (x) = 1;
5029 SET_DECL_C_BIT_FIELD (x);
5033 DECL_INITIAL (x) = 0;
5035 /* Detect flexible array member in an invalid context. */
5036 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5037 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5038 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5039 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5041 if (TREE_CODE (t) == UNION_TYPE)
5042 error_with_decl (x, "flexible array member in union");
5043 else if (TREE_CHAIN (x) != NULL_TREE)
5044 error_with_decl (x, "flexible array member not at end of struct");
5045 else if (! saw_named_field)
5046 error_with_decl (x, "flexible array member in otherwise empty struct");
5049 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5050 && flexible_array_type_p (TREE_TYPE (x)))
5051 pedwarn_with_decl (x, "invalid use of structure with flexible array member");
5053 if (DECL_NAME (x))
5054 saw_named_field = 1;
5057 /* Delete all duplicate fields from the fieldlist */
5058 for (x = fieldlist; x && TREE_CHAIN (x);)
5059 /* Anonymous fields aren't duplicates. */
5060 if (DECL_NAME (TREE_CHAIN (x)) == 0)
5061 x = TREE_CHAIN (x);
5062 else
5064 tree y = fieldlist;
5066 while (1)
5068 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5069 break;
5070 if (y == x)
5071 break;
5072 y = TREE_CHAIN (y);
5074 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
5076 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
5077 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
5079 else
5080 x = TREE_CHAIN (x);
5083 /* Now we have the nearly final fieldlist. Record it,
5084 then lay out the structure or union (including the fields). */
5086 TYPE_FIELDS (t) = fieldlist;
5088 layout_type (t);
5090 /* Delete all zero-width bit-fields from the fieldlist */
5092 tree *fieldlistp = &fieldlist;
5093 while (*fieldlistp)
5094 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5095 *fieldlistp = TREE_CHAIN (*fieldlistp);
5096 else
5097 fieldlistp = &TREE_CHAIN (*fieldlistp);
5100 /* Now we have the truly final field list.
5101 Store it in this type and in the variants. */
5103 TYPE_FIELDS (t) = fieldlist;
5105 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5107 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5108 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5109 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5110 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5113 /* If this was supposed to be a transparent union, but we can't
5114 make it one, warn and turn off the flag. */
5115 if (TREE_CODE (t) == UNION_TYPE
5116 && TYPE_TRANSPARENT_UNION (t)
5117 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5119 TYPE_TRANSPARENT_UNION (t) = 0;
5120 warning ("union cannot be made transparent");
5123 /* If this structure or union completes the type of any previous
5124 variable declaration, lay it out and output its rtl. */
5126 if (current_binding_level->incomplete_list != NULL_TREE)
5128 tree prev = NULL_TREE;
5130 for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5132 tree decl = TREE_VALUE (x);
5134 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5135 && TREE_CODE (decl) != TYPE_DECL)
5137 layout_decl (decl, 0);
5138 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5139 if (flag_objc)
5140 objc_check_decl (decl);
5141 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5142 if (! toplevel)
5143 expand_decl (decl);
5144 /* Unlink X from the incomplete list. */
5145 if (prev)
5146 TREE_CHAIN (prev) = TREE_CHAIN (x);
5147 else
5148 current_binding_level->incomplete_list = TREE_CHAIN (x);
5150 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5151 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5153 tree element = TREE_TYPE (decl);
5154 while (TREE_CODE (element) == ARRAY_TYPE)
5155 element = TREE_TYPE (element);
5156 if (element == t)
5158 layout_array_type (TREE_TYPE (decl));
5159 if (TREE_CODE (decl) != TYPE_DECL)
5161 layout_decl (decl, 0);
5162 if (flag_objc)
5163 objc_check_decl (decl);
5164 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5165 if (! toplevel)
5166 expand_decl (decl);
5168 /* Unlink X from the incomplete list. */
5169 if (prev)
5170 TREE_CHAIN (prev) = TREE_CHAIN (x);
5171 else
5172 current_binding_level->incomplete_list = TREE_CHAIN (x);
5178 /* Finish debugging output for this type. */
5179 rest_of_type_compilation (t, toplevel);
5181 return t;
5184 /* Lay out the type T, and its element type, and so on. */
5186 static void
5187 layout_array_type (t)
5188 tree t;
5190 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5191 layout_array_type (TREE_TYPE (t));
5192 layout_type (t);
5195 /* Begin compiling the definition of an enumeration type.
5196 NAME is its name (or null if anonymous).
5197 Returns the type object, as yet incomplete.
5198 Also records info about it so that build_enumerator
5199 may be used to declare the individual values as they are read. */
5201 tree
5202 start_enum (name)
5203 tree name;
5205 tree enumtype = 0;
5207 /* If this is the real definition for a previous forward reference,
5208 fill in the contents in the same object that used to be the
5209 forward reference. */
5211 if (name != 0)
5212 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5214 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5216 enumtype = make_node (ENUMERAL_TYPE);
5217 pushtag (name, enumtype);
5220 C_TYPE_BEING_DEFINED (enumtype) = 1;
5222 if (TYPE_VALUES (enumtype) != 0)
5224 /* This enum is a named one that has been declared already. */
5225 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5227 /* Completely replace its old definition.
5228 The old enumerators remain defined, however. */
5229 TYPE_VALUES (enumtype) = 0;
5232 enum_next_value = integer_zero_node;
5233 enum_overflow = 0;
5235 if (flag_short_enums)
5236 TYPE_PACKED (enumtype) = 1;
5238 return enumtype;
5241 /* After processing and defining all the values of an enumeration type,
5242 install their decls in the enumeration type and finish it off.
5243 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5244 and ATTRIBUTES are the specified attributes.
5245 Returns ENUMTYPE. */
5247 tree
5248 finish_enum (enumtype, values, attributes)
5249 tree enumtype;
5250 tree values;
5251 tree attributes;
5253 tree pair, tem;
5254 tree minnode = 0, maxnode = 0, enum_value_type;
5255 int precision, unsign;
5256 int toplevel = (global_binding_level == current_binding_level);
5258 if (in_parm_level_p ())
5259 warning ("enum defined inside parms");
5261 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5263 /* Calculate the maximum value of any enumerator in this type. */
5265 if (values == error_mark_node)
5266 minnode = maxnode = integer_zero_node;
5267 else
5269 minnode = maxnode = TREE_VALUE (values);
5270 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5272 tree value = TREE_VALUE (pair);
5273 if (tree_int_cst_lt (maxnode, value))
5274 maxnode = value;
5275 if (tree_int_cst_lt (value, minnode))
5276 minnode = value;
5280 /* Construct the final type of this enumeration. It is the same
5281 as one of the integral types - the narrowest one that fits, except
5282 that normally we only go as narrow as int - and signed iff any of
5283 the values are negative. */
5284 unsign = (tree_int_cst_sgn (minnode) >= 0);
5285 precision = MAX (min_precision (minnode, unsign),
5286 min_precision (maxnode, unsign));
5287 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5289 tree narrowest = c_common_type_for_size (precision, unsign);
5290 if (narrowest == 0)
5292 warning ("enumeration values exceed range of largest integer");
5293 narrowest = long_long_integer_type_node;
5296 precision = TYPE_PRECISION (narrowest);
5298 else
5299 precision = TYPE_PRECISION (integer_type_node);
5301 if (precision == TYPE_PRECISION (integer_type_node))
5302 enum_value_type = c_common_type_for_size (precision, 0);
5303 else
5304 enum_value_type = enumtype;
5306 TYPE_MIN_VALUE (enumtype) = minnode;
5307 TYPE_MAX_VALUE (enumtype) = maxnode;
5308 TYPE_PRECISION (enumtype) = precision;
5309 TREE_UNSIGNED (enumtype) = unsign;
5310 TYPE_SIZE (enumtype) = 0;
5311 layout_type (enumtype);
5313 if (values != error_mark_node)
5315 /* Change the type of the enumerators to be the enum type. We
5316 need to do this irrespective of the size of the enum, for
5317 proper type checking. Replace the DECL_INITIALs of the
5318 enumerators, and the value slots of the list, with copies
5319 that have the enum type; they cannot be modified in place
5320 because they may be shared (e.g. integer_zero_node) Finally,
5321 change the purpose slots to point to the names of the decls. */
5322 for (pair = values; pair; pair = TREE_CHAIN (pair))
5324 tree enu = TREE_PURPOSE (pair);
5326 TREE_TYPE (enu) = enumtype;
5328 /* The ISO C Standard mandates enumerators to have type int,
5329 even though the underlying type of an enum type is
5330 unspecified. Here we convert any enumerators that fit in
5331 an int to type int, to avoid promotions to unsigned types
5332 when comparing integers with enumerators that fit in the
5333 int range. When -pedantic is given, build_enumerator()
5334 would have already taken care of those that don't fit. */
5335 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5336 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5337 else
5338 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5340 TREE_PURPOSE (pair) = DECL_NAME (enu);
5341 TREE_VALUE (pair) = DECL_INITIAL (enu);
5344 TYPE_VALUES (enumtype) = values;
5347 /* Fix up all variant types of this enum type. */
5348 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5350 if (tem == enumtype)
5351 continue;
5352 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5353 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5354 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5355 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5356 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5357 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5358 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5359 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5360 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5361 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5364 /* Finish debugging output for this type. */
5365 rest_of_type_compilation (enumtype, toplevel);
5367 return enumtype;
5370 /* Build and install a CONST_DECL for one value of the
5371 current enumeration type (one that was begun with start_enum).
5372 Return a tree-list containing the CONST_DECL and its value.
5373 Assignment of sequential values by default is handled here. */
5375 tree
5376 build_enumerator (name, value)
5377 tree name, value;
5379 tree decl, type;
5381 /* Validate and default VALUE. */
5383 /* Remove no-op casts from the value. */
5384 if (value)
5385 STRIP_TYPE_NOPS (value);
5387 if (value != 0)
5389 if (TREE_CODE (value) == INTEGER_CST)
5391 value = default_conversion (value);
5392 constant_expression_warning (value);
5394 else
5396 error ("enumerator value for `%s' not integer constant",
5397 IDENTIFIER_POINTER (name));
5398 value = 0;
5402 /* Default based on previous value. */
5403 /* It should no longer be possible to have NON_LVALUE_EXPR
5404 in the default. */
5405 if (value == 0)
5407 value = enum_next_value;
5408 if (enum_overflow)
5409 error ("overflow in enumeration values");
5412 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5414 pedwarn ("ISO C restricts enumerator values to range of `int'");
5415 value = convert (integer_type_node, value);
5418 /* Set basis for default for next value. */
5419 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5420 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5422 /* Now create a declaration for the enum value name. */
5424 type = TREE_TYPE (value);
5425 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5426 TYPE_PRECISION (integer_type_node)),
5427 (TYPE_PRECISION (type)
5428 >= TYPE_PRECISION (integer_type_node)
5429 && TREE_UNSIGNED (type)));
5431 decl = build_decl (CONST_DECL, name, type);
5432 DECL_INITIAL (decl) = convert (type, value);
5433 pushdecl (decl);
5435 return tree_cons (decl, value, NULL_TREE);
5439 /* Create the FUNCTION_DECL for a function definition.
5440 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5441 the declaration; they describe the function's name and the type it returns,
5442 but twisted together in a fashion that parallels the syntax of C.
5444 This function creates a binding context for the function body
5445 as well as setting up the FUNCTION_DECL in current_function_decl.
5447 Returns 1 on success. If the DECLARATOR is not suitable for a function
5448 (it defines a datum instead), we return 0, which tells
5449 yyparse to report a parse error. */
5452 start_function (declspecs, declarator, attributes)
5453 tree declarator, declspecs, attributes;
5455 tree decl1, old_decl;
5456 tree restype;
5457 int old_immediate_size_expand = immediate_size_expand;
5459 current_function_returns_value = 0; /* Assume, until we see it does. */
5460 current_function_returns_null = 0;
5461 current_function_returns_abnormally = 0;
5462 warn_about_return_type = 0;
5463 current_extern_inline = 0;
5464 named_labels = 0;
5465 shadowed_labels = 0;
5467 /* Don't expand any sizes in the return type of the function. */
5468 immediate_size_expand = 0;
5470 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5472 /* If the declarator is not suitable for a function definition,
5473 cause a syntax error. */
5474 if (decl1 == 0)
5476 immediate_size_expand = old_immediate_size_expand;
5477 return 0;
5480 decl_attributes (&decl1, attributes, 0);
5482 /* If #pragma weak was used, mark the decl weak now. */
5483 if (current_binding_level == global_binding_level)
5484 maybe_apply_pragma_weak (decl1);
5486 if (DECL_DECLARED_INLINE_P (decl1)
5487 && DECL_UNINLINABLE (decl1)
5488 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5489 warning_with_decl (decl1,
5490 "inline function `%s' given attribute noinline");
5492 announce_function (decl1);
5494 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5496 error ("return type is an incomplete type");
5497 /* Make it return void instead. */
5498 TREE_TYPE (decl1)
5499 = build_function_type (void_type_node,
5500 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5503 if (warn_about_return_type)
5504 pedwarn_c99 ("return type defaults to `int'");
5506 /* Save the parm names or decls from this function's declarator
5507 where store_parm_decls will find them. */
5508 current_function_parms = last_function_parms;
5509 current_function_parm_tags = last_function_parm_tags;
5511 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5512 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5513 DECL_INITIAL (decl1) = error_mark_node;
5515 /* If this definition isn't a prototype and we had a prototype declaration
5516 before, copy the arg type info from that prototype.
5517 But not if what we had before was a builtin function. */
5518 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5519 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5520 && !DECL_BUILT_IN (old_decl)
5521 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5522 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5523 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5525 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5526 current_function_prototype_file = DECL_SOURCE_FILE (old_decl);
5527 current_function_prototype_line = DECL_SOURCE_LINE (old_decl);
5530 /* Optionally warn of old-fashioned def with no previous prototype. */
5531 if (warn_strict_prototypes
5532 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5533 && C_DECL_ISNT_PROTOTYPE (old_decl))
5534 warning ("function declaration isn't a prototype");
5535 /* Optionally warn of any global def with no previous prototype. */
5536 else if (warn_missing_prototypes
5537 && TREE_PUBLIC (decl1)
5538 && ! MAIN_NAME_P (DECL_NAME (decl1))
5539 && C_DECL_ISNT_PROTOTYPE (old_decl))
5540 warning_with_decl (decl1, "no previous prototype for `%s'");
5541 /* Optionally warn of any def with no previous prototype
5542 if the function has already been used. */
5543 else if (warn_missing_prototypes
5544 && old_decl != 0 && TREE_USED (old_decl)
5545 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5546 warning_with_decl (decl1,
5547 "`%s' was used with no prototype before its definition");
5548 /* Optionally warn of any global def with no previous declaration. */
5549 else if (warn_missing_declarations
5550 && TREE_PUBLIC (decl1)
5551 && old_decl == 0
5552 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5553 warning_with_decl (decl1, "no previous declaration for `%s'");
5554 /* Optionally warn of any def with no previous declaration
5555 if the function has already been used. */
5556 else if (warn_missing_declarations
5557 && old_decl != 0 && TREE_USED (old_decl)
5558 && C_DECL_IMPLICIT (old_decl))
5559 warning_with_decl (decl1,
5560 "`%s' was used with no declaration before its definition");
5562 /* This is a definition, not a reference.
5563 So normally clear DECL_EXTERNAL.
5564 However, `extern inline' acts like a declaration
5565 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5566 DECL_EXTERNAL (decl1) = current_extern_inline;
5568 /* This function exists in static storage.
5569 (This does not mean `static' in the C sense!) */
5570 TREE_STATIC (decl1) = 1;
5572 /* A nested function is not global. */
5573 if (current_function_decl != 0)
5574 TREE_PUBLIC (decl1) = 0;
5576 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5577 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5579 tree args;
5580 int argct = 0;
5582 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5583 != integer_type_node)
5584 pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5586 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5587 args = TREE_CHAIN (args))
5589 tree type = args ? TREE_VALUE (args) : 0;
5591 if (type == void_type_node)
5592 break;
5594 ++argct;
5595 switch (argct)
5597 case 1:
5598 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5599 pedwarn_with_decl (decl1,
5600 "first argument of `%s' should be `int'");
5601 break;
5603 case 2:
5604 if (TREE_CODE (type) != POINTER_TYPE
5605 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5606 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5607 != char_type_node))
5608 pedwarn_with_decl (decl1,
5609 "second argument of `%s' should be `char **'");
5610 break;
5612 case 3:
5613 if (TREE_CODE (type) != POINTER_TYPE
5614 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5615 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5616 != char_type_node))
5617 pedwarn_with_decl (decl1,
5618 "third argument of `%s' should probably be `char **'");
5619 break;
5623 /* It is intentional that this message does not mention the third
5624 argument because it's only mentioned in an appendix of the
5625 standard. */
5626 if (argct > 0 && (argct < 2 || argct > 3))
5627 pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5629 if (! TREE_PUBLIC (decl1))
5630 pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5633 /* Record the decl so that the function name is defined.
5634 If we already have a decl for this name, and it is a FUNCTION_DECL,
5635 use the old decl. */
5637 current_function_decl = pushdecl (decl1);
5639 pushlevel (0);
5640 declare_parm_level (1);
5642 make_decl_rtl (current_function_decl, NULL);
5644 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5645 /* Promote the value to int before returning it. */
5646 if (c_promoting_integer_type_p (restype))
5648 /* It retains unsignedness if not really getting wider. */
5649 if (TREE_UNSIGNED (restype)
5650 && (TYPE_PRECISION (restype)
5651 == TYPE_PRECISION (integer_type_node)))
5652 restype = unsigned_type_node;
5653 else
5654 restype = integer_type_node;
5656 DECL_RESULT (current_function_decl)
5657 = build_decl (RESULT_DECL, NULL_TREE, restype);
5659 /* If this fcn was already referenced via a block-scope `extern' decl
5660 (or an implicit decl), propagate certain information about the usage. */
5661 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5662 TREE_ADDRESSABLE (current_function_decl) = 1;
5664 immediate_size_expand = old_immediate_size_expand;
5666 start_fname_decls ();
5668 return 1;
5671 /* Store the parameter declarations into the current function declaration.
5672 This is called after parsing the parameter declarations, before
5673 digesting the body of the function.
5675 For an old-style definition, modify the function's type
5676 to specify at least the number of arguments. */
5678 void
5679 store_parm_decls ()
5681 tree fndecl = current_function_decl;
5682 tree parm;
5684 /* This is either a chain of PARM_DECLs (if a prototype was used)
5685 or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5686 tree specparms = current_function_parms;
5688 /* This is a list of types declared among parms in a prototype. */
5689 tree parmtags = current_function_parm_tags;
5691 /* This is a chain of PARM_DECLs from old-style parm declarations. */
5692 tree parmdecls = getdecls ();
5694 /* This is a chain of any other decls that came in among the parm
5695 declarations. If a parm is declared with enum {foo, bar} x;
5696 then CONST_DECLs for foo and bar are put here. */
5697 tree nonparms = 0;
5699 /* The function containing FNDECL, if any. */
5700 tree context = decl_function_context (fndecl);
5702 /* Nonzero if this definition is written with a prototype. */
5703 int prototype = 0;
5705 int saved_warn_shadow = warn_shadow;
5707 /* Don't re-emit shadow warnings. */
5708 warn_shadow = 0;
5710 if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5712 /* This case is when the function was defined with an ANSI prototype.
5713 The parms already have decls, so we need not do anything here
5714 except record them as in effect
5715 and complain if any redundant old-style parm decls were written. */
5717 tree next;
5718 tree others = 0;
5720 prototype = 1;
5722 if (parmdecls != 0)
5724 tree decl, link;
5726 error_with_decl (fndecl,
5727 "parm types given both in parmlist and separately");
5728 /* Get rid of the erroneous decls; don't keep them on
5729 the list of parms, since they might not be PARM_DECLs. */
5730 for (decl = current_binding_level->names;
5731 decl; decl = TREE_CHAIN (decl))
5732 if (DECL_NAME (decl))
5733 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = 0;
5734 for (link = current_binding_level->shadowed;
5735 link; link = TREE_CHAIN (link))
5736 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5737 current_binding_level->names = 0;
5738 current_binding_level->shadowed = 0;
5741 specparms = nreverse (specparms);
5742 for (parm = specparms; parm; parm = next)
5744 next = TREE_CHAIN (parm);
5745 if (TREE_CODE (parm) == PARM_DECL)
5747 if (DECL_NAME (parm) == 0)
5748 error_with_decl (parm, "parameter name omitted");
5749 else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5750 && VOID_TYPE_P (TREE_TYPE (parm)))
5752 error_with_decl (parm, "parameter `%s' declared void");
5753 /* Change the type to error_mark_node so this parameter
5754 will be ignored by assign_parms. */
5755 TREE_TYPE (parm) = error_mark_node;
5757 pushdecl (parm);
5759 else
5761 /* If we find an enum constant or a type tag,
5762 put it aside for the moment. */
5763 TREE_CHAIN (parm) = 0;
5764 others = chainon (others, parm);
5768 /* Get the decls in their original chain order
5769 and record in the function. */
5770 DECL_ARGUMENTS (fndecl) = getdecls ();
5772 #if 0
5773 /* If this function takes a variable number of arguments,
5774 add a phony parameter to the end of the parm list,
5775 to represent the position of the first unnamed argument. */
5776 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
5777 != void_type_node)
5779 tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
5780 /* Let's hope the address of the unnamed parm
5781 won't depend on its type. */
5782 TREE_TYPE (dummy) = integer_type_node;
5783 DECL_ARG_TYPE (dummy) = integer_type_node;
5784 DECL_ARGUMENTS (fndecl) = chainon (DECL_ARGUMENTS (fndecl), dummy);
5786 #endif
5788 /* Now pushdecl the enum constants. */
5789 for (parm = others; parm; parm = next)
5791 next = TREE_CHAIN (parm);
5792 if (DECL_NAME (parm) == 0)
5794 else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5796 else if (TREE_CODE (parm) != PARM_DECL)
5797 pushdecl (parm);
5800 storetags (chainon (parmtags, gettags ()));
5802 else
5804 /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5805 each with a parm name as the TREE_VALUE.
5807 PARMDECLS is a chain of declarations for parameters.
5808 Warning! It can also contain CONST_DECLs which are not parameters
5809 but are names of enumerators of any enum types
5810 declared among the parameters.
5812 First match each formal parameter name with its declaration.
5813 Associate decls with the names and store the decls
5814 into the TREE_PURPOSE slots. */
5816 /* We use DECL_WEAK as a flag to show which parameters have been
5817 seen already since it is not used on PARM_DECL or CONST_DECL. */
5818 for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5819 DECL_WEAK (parm) = 0;
5821 for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5823 tree tail, found = NULL;
5825 if (TREE_VALUE (parm) == 0)
5827 error_with_decl (fndecl,
5828 "parameter name missing from parameter list");
5829 TREE_PURPOSE (parm) = 0;
5830 continue;
5833 /* See if any of the parmdecls specifies this parm by name.
5834 Ignore any enumerator decls. */
5835 for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5836 if (DECL_NAME (tail) == TREE_VALUE (parm)
5837 && TREE_CODE (tail) == PARM_DECL)
5839 found = tail;
5840 break;
5843 /* If declaration already marked, we have a duplicate name.
5844 Complain, and don't use this decl twice. */
5845 if (found && DECL_WEAK (found))
5847 error_with_decl (found, "multiple parameters named `%s'");
5848 found = 0;
5851 /* If the declaration says "void", complain and ignore it. */
5852 if (found && VOID_TYPE_P (TREE_TYPE (found)))
5854 error_with_decl (found, "parameter `%s' declared void");
5855 TREE_TYPE (found) = integer_type_node;
5856 DECL_ARG_TYPE (found) = integer_type_node;
5857 layout_decl (found, 0);
5860 /* If no declaration found, default to int. */
5861 if (!found)
5863 found = build_decl (PARM_DECL, TREE_VALUE (parm),
5864 integer_type_node);
5865 DECL_ARG_TYPE (found) = TREE_TYPE (found);
5866 DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
5867 DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
5868 if (flag_isoc99)
5869 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
5870 else if (extra_warnings)
5871 warning_with_decl (found, "type of `%s' defaults to `int'");
5872 pushdecl (found);
5875 TREE_PURPOSE (parm) = found;
5877 /* Mark this decl as "already found". */
5878 DECL_WEAK (found) = 1;
5881 /* Put anything which is on the parmdecls chain and which is
5882 not a PARM_DECL onto the list NONPARMS. (The types of
5883 non-parm things which might appear on the list include
5884 enumerators and NULL-named TYPE_DECL nodes.) Complain about
5885 any actual PARM_DECLs not matched with any names. */
5887 nonparms = 0;
5888 for (parm = parmdecls; parm;)
5890 tree next = TREE_CHAIN (parm);
5891 TREE_CHAIN (parm) = 0;
5893 if (TREE_CODE (parm) != PARM_DECL)
5894 nonparms = chainon (nonparms, parm);
5895 else
5897 /* Complain about args with incomplete types. */
5898 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5900 error_with_decl (parm, "parameter `%s' has incomplete type");
5901 TREE_TYPE (parm) = error_mark_node;
5904 if (! DECL_WEAK (parm))
5906 error_with_decl (parm,
5907 "declaration for parameter `%s' but no such parameter");
5908 /* Pretend the parameter was not missing.
5909 This gets us to a standard state and minimizes
5910 further error messages. */
5911 specparms
5912 = chainon (specparms,
5913 tree_cons (parm, NULL_TREE, NULL_TREE));
5917 parm = next;
5920 /* Chain the declarations together in the order of the list of
5921 names. Store that chain in the function decl, replacing the
5922 list of names. */
5923 parm = specparms;
5924 DECL_ARGUMENTS (fndecl) = 0;
5926 tree last;
5927 for (last = 0; parm; parm = TREE_CHAIN (parm))
5928 if (TREE_PURPOSE (parm))
5930 if (last == 0)
5931 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
5932 else
5933 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5934 last = TREE_PURPOSE (parm);
5935 TREE_CHAIN (last) = 0;
5939 /* If there was a previous prototype,
5940 set the DECL_ARG_TYPE of each argument according to
5941 the type previously specified, and report any mismatches. */
5943 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5945 tree type;
5946 for (parm = DECL_ARGUMENTS (fndecl),
5947 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5948 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5949 != void_type_node));
5950 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5952 if (parm == 0 || type == 0
5953 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5955 error ("number of arguments doesn't match prototype");
5956 error_with_file_and_line (current_function_prototype_file,
5957 current_function_prototype_line,
5958 "prototype declaration");
5959 break;
5961 /* Type for passing arg must be consistent with that
5962 declared for the arg. ISO C says we take the unqualified
5963 type for parameters declared with qualified type. */
5964 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5965 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
5967 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5968 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5970 /* Adjust argument to match prototype. E.g. a previous
5971 `int foo(float);' prototype causes
5972 `int foo(x) float x; {...}' to be treated like
5973 `int foo(float x) {...}'. This is particularly
5974 useful for argument types like uid_t. */
5975 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5977 if (PROMOTE_PROTOTYPES
5978 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5979 && TYPE_PRECISION (TREE_TYPE (parm))
5980 < TYPE_PRECISION (integer_type_node))
5981 DECL_ARG_TYPE (parm) = integer_type_node;
5983 if (pedantic)
5985 pedwarn ("promoted argument `%s' doesn't match prototype",
5986 IDENTIFIER_POINTER (DECL_NAME (parm)));
5987 warning_with_file_and_line
5988 (current_function_prototype_file,
5989 current_function_prototype_line,
5990 "prototype declaration");
5993 else
5995 error ("argument `%s' doesn't match prototype",
5996 IDENTIFIER_POINTER (DECL_NAME (parm)));
5997 error_with_file_and_line (current_function_prototype_file,
5998 current_function_prototype_line,
5999 "prototype declaration");
6003 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6006 /* Otherwise, create a prototype that would match. */
6008 else
6010 tree actual = 0, last = 0, type;
6012 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6014 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6015 if (last)
6016 TREE_CHAIN (last) = type;
6017 else
6018 actual = type;
6019 last = type;
6021 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6022 if (last)
6023 TREE_CHAIN (last) = type;
6024 else
6025 actual = type;
6027 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6028 of the type of this function, but we need to avoid having this
6029 affect the types of other similarly-typed functions, so we must
6030 first force the generation of an identical (but separate) type
6031 node for the relevant function type. The new node we create
6032 will be a variant of the main variant of the original function
6033 type. */
6035 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6037 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6040 /* Now store the final chain of decls for the arguments
6041 as the decl-chain of the current lexical scope.
6042 Put the enumerators in as well, at the front so that
6043 DECL_ARGUMENTS is not modified. */
6045 storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6048 /* Make sure the binding level for the top of the function body
6049 gets a BLOCK if there are any in the function.
6050 Otherwise, the dbx output is wrong. */
6052 keep_next_if_subblocks = 1;
6054 /* ??? This might be an improvement,
6055 but needs to be thought about some more. */
6056 #if 0
6057 keep_next_level_flag = 1;
6058 #endif
6060 /* Write a record describing this function definition to the prototypes
6061 file (if requested). */
6063 gen_aux_info_record (fndecl, 1, 0, prototype);
6065 /* Initialize the RTL code for the function. */
6066 init_function_start (fndecl, input_filename, lineno);
6068 /* Begin the statement tree for this function. */
6069 begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6071 /* If this is a nested function, save away the sizes of any
6072 variable-size types so that we can expand them when generating
6073 RTL. */
6074 if (context)
6076 tree t;
6078 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6079 = nreverse (get_pending_sizes ());
6080 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6082 t = TREE_CHAIN (t))
6083 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6086 /* This function is being processed in whole-function mode. */
6087 cfun->x_whole_function_mode_p = 1;
6089 /* Even though we're inside a function body, we still don't want to
6090 call expand_expr to calculate the size of a variable-sized array.
6091 We haven't necessarily assigned RTL to all variables yet, so it's
6092 not safe to try to expand expressions involving them. */
6093 immediate_size_expand = 0;
6094 cfun->x_dont_save_pending_sizes_p = 1;
6096 warn_shadow = saved_warn_shadow;
6099 /* Finish up a function declaration and compile that function
6100 all the way to assembler language output. The free the storage
6101 for the function definition.
6103 This is called after parsing the body of the function definition.
6105 NESTED is nonzero if the function being finished is nested in another.
6106 CAN_DEFER_P is nonzero if the function may be deferred. */
6108 void
6109 finish_function (nested, can_defer_p)
6110 int nested;
6111 int can_defer_p;
6113 tree fndecl = current_function_decl;
6115 /* When a function declaration is totally empty, e.g.
6116 void foo(void) { }
6117 (the argument list is irrelevant) the compstmt rule will not
6118 bother calling pushlevel/poplevel, which means we get here with
6119 the binding_level stack out of sync. Detect this situation by
6120 noticing that the current_binding_level is still as
6121 store_parm_decls left it, and do a dummy push/pop to get back to
6122 consistency. Note that the call to pushlevel does not actually
6123 push another binding level - see there for details. */
6124 if (current_binding_level->parm_flag && keep_next_if_subblocks)
6126 pushlevel (0);
6127 poplevel (1, 0, 1);
6130 #if 0
6131 /* This caused &foo to be of type ptr-to-const-function which then
6132 got a warning when stored in a ptr-to-function variable. */
6133 TREE_READONLY (fndecl) = 1;
6134 #endif
6136 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6138 /* Must mark the RESULT_DECL as being in this function. */
6140 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6142 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6144 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6145 != integer_type_node)
6147 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6148 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6149 if (! warn_main)
6150 pedwarn_with_decl (fndecl, "return type of `%s' is not `int'");
6152 else
6154 #ifdef DEFAULT_MAIN_RETURN
6155 /* Make it so that `main' always returns success by default. */
6156 DEFAULT_MAIN_RETURN;
6157 #else
6158 if (flag_isoc99)
6159 c_expand_return (integer_zero_node);
6160 #endif
6164 finish_fname_decls ();
6166 /* Tie off the statement tree for this function. */
6167 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6169 /* Complain if there's just no return statement. */
6170 if (warn_return_type
6171 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6172 && !current_function_returns_value && !current_function_returns_null
6173 /* Don't complain if we abort. */
6174 && !current_function_returns_abnormally
6175 /* Don't warn for main(). */
6176 && !MAIN_NAME_P (DECL_NAME (fndecl))
6177 /* Or if they didn't actually specify a return type. */
6178 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6179 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6180 inline function, as we might never be compiled separately. */
6181 && DECL_INLINE (fndecl))
6182 warning ("no return statement in function returning non-void");
6184 /* Clear out memory we no longer need. */
6185 free_after_parsing (cfun);
6186 /* Since we never call rest_of_compilation, we never clear
6187 CFUN. Do so explicitly. */
6188 free_after_compilation (cfun);
6189 cfun = NULL;
6191 if (flag_unit_at_a_time && can_defer_p)
6193 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6194 current_function_decl = NULL;
6195 return;
6198 if (! nested)
6200 /* Function is parsed.
6201 Generate RTL for the body of this function or defer
6202 it for later expansion. */
6203 int uninlinable = 1;
6205 /* There's no reason to do any of the work here if we're only doing
6206 semantic analysis; this code just generates RTL. */
6207 if (flag_syntax_only)
6209 current_function_decl = NULL;
6210 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6211 return;
6214 if (flag_inline_trees)
6216 /* First, cache whether the current function is inlinable. Some
6217 predicates depend on cfun and current_function_decl to
6218 function completely. */
6219 timevar_push (TV_INTEGRATION);
6220 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6222 if (can_defer_p
6223 /* We defer functions marked inline *even if* the function
6224 itself is not inlinable. This is because we don't yet
6225 know if the function will actually be used; we may be
6226 able to avoid emitting it entirely. */
6227 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6228 /* Save function tree for inlining. Should return 0 if the
6229 language does not support function deferring or the
6230 function could not be deferred. */
6231 && defer_fn (fndecl))
6233 /* Let the back-end know that this function exists. */
6234 (*debug_hooks->deferred_inline_function) (fndecl);
6235 timevar_pop (TV_INTEGRATION);
6236 current_function_decl = NULL;
6237 return;
6240 /* Then, inline any functions called in it. */
6241 optimize_inline_calls (fndecl);
6242 timevar_pop (TV_INTEGRATION);
6245 c_expand_body (fndecl);
6247 /* Keep the function body if it's needed for inlining or dumping. */
6248 if (uninlinable && !dump_enabled_p (TDI_all))
6250 /* Allow the body of the function to be garbage collected. */
6251 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6254 /* Let the error reporting routines know that we're outside a
6255 function. For a nested function, this value is used in
6256 c_pop_function_context and then reset via pop_function_context. */
6257 current_function_decl = NULL;
6261 /* Generate the RTL for a deferred function FNDECL. */
6263 void
6264 c_expand_deferred_function (fndecl)
6265 tree fndecl;
6267 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6268 function was deferred, e.g. in duplicate_decls. */
6269 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6271 if (flag_inline_trees)
6273 timevar_push (TV_INTEGRATION);
6274 optimize_inline_calls (fndecl);
6275 timevar_pop (TV_INTEGRATION);
6277 c_expand_body (fndecl);
6278 current_function_decl = NULL;
6282 /* Called to move the SAVE_EXPRs for parameter declarations in a
6283 nested function into the nested function. DATA is really the
6284 nested FUNCTION_DECL. */
6286 static tree
6287 set_save_expr_context (tree *tp,
6288 int *walk_subtrees,
6289 void *data)
6291 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6292 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6293 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6294 circularity. */
6295 else if (DECL_P (*tp))
6296 *walk_subtrees = 0;
6298 return NULL_TREE;
6301 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6302 then we are already in the process of generating RTL for another
6303 function. If can_defer_p is zero, we won't attempt to defer the
6304 generation of RTL. */
6306 static void
6307 c_expand_body_1 (fndecl, nested_p)
6308 tree fndecl;
6309 int nested_p;
6311 timevar_push (TV_EXPAND);
6313 if (nested_p)
6315 /* Make sure that we will evaluate variable-sized types involved
6316 in our function's type. */
6317 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6318 /* Squirrel away our current state. */
6319 push_function_context ();
6322 /* Initialize the RTL code for the function. */
6323 current_function_decl = fndecl;
6324 input_filename = DECL_SOURCE_FILE (fndecl);
6325 init_function_start (fndecl, input_filename, DECL_SOURCE_LINE (fndecl));
6326 lineno = DECL_SOURCE_LINE (fndecl);
6328 /* This function is being processed in whole-function mode. */
6329 cfun->x_whole_function_mode_p = 1;
6331 /* Even though we're inside a function body, we still don't want to
6332 call expand_expr to calculate the size of a variable-sized array.
6333 We haven't necessarily assigned RTL to all variables yet, so it's
6334 not safe to try to expand expressions involving them. */
6335 immediate_size_expand = 0;
6336 cfun->x_dont_save_pending_sizes_p = 1;
6338 /* Set up parameters and prepare for return, for the function. */
6339 expand_function_start (fndecl, 0);
6341 /* If the function has a variably modified type, there may be
6342 SAVE_EXPRs in the parameter types. Their context must be set to
6343 refer to this function; they cannot be expanded in the containing
6344 function. */
6345 if (decl_function_context (fndecl)
6346 && variably_modified_type_p (TREE_TYPE (fndecl)))
6347 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6348 NULL);
6350 /* If this function is `main', emit a call to `__main'
6351 to run global initializers, etc. */
6352 if (DECL_NAME (fndecl)
6353 && MAIN_NAME_P (DECL_NAME (fndecl))
6354 && DECL_CONTEXT (fndecl) == NULL_TREE)
6355 expand_main_function ();
6357 /* Generate the RTL for this function. */
6358 expand_stmt (DECL_SAVED_TREE (fndecl));
6360 /* We hard-wired immediate_size_expand to zero above.
6361 expand_function_end will decrement this variable. So, we set the
6362 variable to one here, so that after the decrement it will remain
6363 zero. */
6364 immediate_size_expand = 1;
6366 /* Allow language dialects to perform special processing. */
6367 if (lang_expand_function_end)
6368 (*lang_expand_function_end) ();
6370 /* Generate rtl for function exit. */
6371 expand_function_end (input_filename, lineno, 0);
6373 /* If this is a nested function, protect the local variables in the stack
6374 above us from being collected while we're compiling this function. */
6375 if (nested_p)
6376 ggc_push_context ();
6378 /* Run the optimizers and output the assembler code for this function. */
6379 rest_of_compilation (fndecl);
6381 /* Undo the GC context switch. */
6382 if (nested_p)
6383 ggc_pop_context ();
6385 /* With just -Wextra, complain only if function returns both with
6386 and without a value. */
6387 if (extra_warnings
6388 && current_function_returns_value
6389 && current_function_returns_null)
6390 warning ("this function may return with or without a value");
6392 /* If requested, warn about function definitions where the function will
6393 return a value (usually of some struct or union type) which itself will
6394 take up a lot of stack space. */
6396 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6398 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6400 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6401 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6402 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6403 larger_than_size))
6405 unsigned int size_as_int
6406 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6408 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6409 warning_with_decl (fndecl,
6410 "size of return value of `%s' is %u bytes",
6411 size_as_int);
6412 else
6413 warning_with_decl (fndecl,
6414 "size of return value of `%s' is larger than %d bytes",
6415 larger_than_size);
6419 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6420 && ! flag_inline_trees)
6422 /* Stop pointing to the local nodes about to be freed.
6423 But DECL_INITIAL must remain nonzero so we know this
6424 was an actual function definition.
6425 For a nested function, this is done in c_pop_function_context.
6426 If rest_of_compilation set this to 0, leave it 0. */
6427 if (DECL_INITIAL (fndecl) != 0)
6428 DECL_INITIAL (fndecl) = error_mark_node;
6430 DECL_ARGUMENTS (fndecl) = 0;
6433 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6435 if (targetm.have_ctors_dtors)
6436 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6437 DEFAULT_INIT_PRIORITY);
6438 else
6439 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6442 if (DECL_STATIC_DESTRUCTOR (fndecl))
6444 if (targetm.have_ctors_dtors)
6445 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6446 DEFAULT_INIT_PRIORITY);
6447 else
6448 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6451 if (nested_p)
6452 /* Return to the enclosing function. */
6453 pop_function_context ();
6454 timevar_pop (TV_EXPAND);
6457 /* Like c_expand_body_1 but only for unnested functions. */
6459 void
6460 c_expand_body (fndecl)
6461 tree fndecl;
6463 c_expand_body_1 (fndecl, 0);
6466 /* Check the declarations given in a for-loop for satisfying the C99
6467 constraints. */
6468 void
6469 check_for_loop_decls ()
6471 tree t;
6473 if (!flag_isoc99)
6475 /* If we get here, declarations have been used in a for loop without
6476 the C99 for loop scope. This doesn't make much sense, so don't
6477 allow it. */
6478 error ("`for' loop initial declaration used outside C99 mode");
6479 return;
6481 /* C99 subclause 6.8.5 paragraph 3:
6483 [#3] The declaration part of a for statement shall only
6484 declare identifiers for objects having storage class auto or
6485 register.
6487 It isn't clear whether, in this sentence, "identifiers" binds to
6488 "shall only declare" or to "objects" - that is, whether all identifiers
6489 declared must be identifiers for objects, or whether the restriction
6490 only applies to those that are. (A question on this in comp.std.c
6491 in November 2000 received no answer.) We implement the strictest
6492 interpretation, to avoid creating an extension which later causes
6493 problems. */
6495 for (t = gettags (); t; t = TREE_CHAIN (t))
6497 if (TREE_PURPOSE (t) != 0)
6499 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6501 if (code == RECORD_TYPE)
6502 error ("`struct %s' declared in `for' loop initial declaration",
6503 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6504 else if (code == UNION_TYPE)
6505 error ("`union %s' declared in `for' loop initial declaration",
6506 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6507 else
6508 error ("`enum %s' declared in `for' loop initial declaration",
6509 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6513 for (t = getdecls (); t; t = TREE_CHAIN (t))
6515 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6516 error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6517 else if (TREE_STATIC (t))
6518 error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6519 else if (DECL_EXTERNAL (t))
6520 error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6524 /* Save and restore the variables in this file and elsewhere
6525 that keep track of the progress of compilation of the current function.
6526 Used for nested functions. */
6528 struct language_function GTY(())
6530 struct c_language_function base;
6531 tree named_labels;
6532 tree shadowed_labels;
6533 int returns_value;
6534 int returns_null;
6535 int returns_abnormally;
6536 int warn_about_return_type;
6537 int extern_inline;
6538 struct binding_level *binding_level;
6541 /* Save and reinitialize the variables
6542 used during compilation of a C function. */
6544 void
6545 c_push_function_context (f)
6546 struct function *f;
6548 struct language_function *p;
6549 p = ((struct language_function *)
6550 ggc_alloc (sizeof (struct language_function)));
6551 f->language = p;
6553 p->base.x_stmt_tree = c_stmt_tree;
6554 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6555 p->named_labels = named_labels;
6556 p->shadowed_labels = shadowed_labels;
6557 p->returns_value = current_function_returns_value;
6558 p->returns_null = current_function_returns_null;
6559 p->returns_abnormally = current_function_returns_abnormally;
6560 p->warn_about_return_type = warn_about_return_type;
6561 p->extern_inline = current_extern_inline;
6562 p->binding_level = current_binding_level;
6565 /* Restore the variables used during compilation of a C function. */
6567 void
6568 c_pop_function_context (f)
6569 struct function *f;
6571 struct language_function *p = f->language;
6572 tree link;
6574 /* Bring back all the labels that were shadowed. */
6575 for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6576 if (DECL_NAME (TREE_VALUE (link)) != 0)
6577 IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6578 = TREE_VALUE (link);
6580 if (DECL_SAVED_INSNS (current_function_decl) == 0
6581 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6583 /* Stop pointing to the local nodes about to be freed. */
6584 /* But DECL_INITIAL must remain nonzero so we know this
6585 was an actual function definition. */
6586 DECL_INITIAL (current_function_decl) = error_mark_node;
6587 DECL_ARGUMENTS (current_function_decl) = 0;
6590 c_stmt_tree = p->base.x_stmt_tree;
6591 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6592 named_labels = p->named_labels;
6593 shadowed_labels = p->shadowed_labels;
6594 current_function_returns_value = p->returns_value;
6595 current_function_returns_null = p->returns_null;
6596 current_function_returns_abnormally = p->returns_abnormally;
6597 warn_about_return_type = p->warn_about_return_type;
6598 current_extern_inline = p->extern_inline;
6599 current_binding_level = p->binding_level;
6601 f->language = NULL;
6604 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6606 void
6607 c_dup_lang_specific_decl (decl)
6608 tree decl;
6610 struct lang_decl *ld;
6612 if (!DECL_LANG_SPECIFIC (decl))
6613 return;
6615 ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6616 memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6617 sizeof (struct lang_decl));
6618 DECL_LANG_SPECIFIC (decl) = ld;
6621 /* The functions below are required for functionality of doing
6622 function at once processing in the C front end. Currently these
6623 functions are not called from anywhere in the C front end, but as
6624 these changes continue, that will change. */
6626 /* Returns nonzero if the current statement is a full expression,
6627 i.e. temporaries created during that statement should be destroyed
6628 at the end of the statement. */
6631 stmts_are_full_exprs_p ()
6633 return 0;
6636 /* Returns the stmt_tree (if any) to which statements are currently
6637 being added. If there is no active statement-tree, NULL is
6638 returned. */
6640 stmt_tree
6641 current_stmt_tree ()
6643 return &c_stmt_tree;
6646 /* Returns the stack of SCOPE_STMTs for the current function. */
6648 tree *
6649 current_scope_stmt_stack ()
6651 return &c_scope_stmt_stack;
6654 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6655 C. */
6658 anon_aggr_type_p (node)
6659 tree node ATTRIBUTE_UNUSED;
6661 return 0;
6664 /* Dummy function in place of callback used by C++. */
6666 void
6667 extract_interface_info ()
6671 /* Return a new COMPOUND_STMT, after adding it to the current
6672 statement tree. */
6674 tree
6675 c_begin_compound_stmt ()
6677 tree stmt;
6679 /* Create the COMPOUND_STMT. */
6680 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6682 return stmt;
6685 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6686 common code. */
6688 void
6689 c_expand_decl_stmt (t)
6690 tree t;
6692 tree decl = DECL_STMT_DECL (t);
6694 /* Expand nested functions. */
6695 if (TREE_CODE (decl) == FUNCTION_DECL
6696 && DECL_CONTEXT (decl) == current_function_decl
6697 && DECL_SAVED_TREE (decl))
6698 c_expand_body_1 (decl, 1);
6701 /* Return the global value of T as a symbol. */
6703 tree
6704 identifier_global_value (t)
6705 tree t;
6707 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6708 if (decl == 0 || DECL_CONTEXT (decl) == 0)
6709 return decl;
6711 /* Shadowed by something else; find the true global value. */
6712 for (decl = global_binding_level->names; decl; decl = TREE_CHAIN (decl))
6713 if (DECL_NAME (decl) == t)
6714 return decl;
6716 /* Only local values for this decl. */
6717 return 0;
6720 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6721 otherwise the name is found in ridpointers from RID_INDEX. */
6723 void
6724 record_builtin_type (rid_index, name, type)
6725 enum rid rid_index;
6726 const char *name;
6727 tree type;
6729 tree id;
6730 if (name == 0)
6731 id = ridpointers[(int) rid_index];
6732 else
6733 id = get_identifier (name);
6734 pushdecl (build_decl (TYPE_DECL, id, type));
6737 /* Build the void_list_node (void_type_node having been created). */
6738 tree
6739 build_void_list_node ()
6741 tree t = build_tree_list (NULL_TREE, void_type_node);
6742 return t;
6745 /* Return something to represent absolute declarators containing a *.
6746 TARGET is the absolute declarator that the * contains.
6747 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6748 to apply to the pointer type, represented as identifiers, possible mixed
6749 with attributes.
6751 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6752 if attributes are present) and whose type is the modifier list. */
6754 tree
6755 make_pointer_declarator (type_quals_attrs, target)
6756 tree type_quals_attrs, target;
6758 tree quals, attrs;
6759 tree itarget = target;
6760 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6761 if (attrs != NULL_TREE)
6762 itarget = tree_cons (attrs, target, NULL_TREE);
6763 return build1 (INDIRECT_REF, quals, itarget);
6766 #include "gt-c-decl.h"