OSDN Git Service

* c-decl.c: Fix typos in several comments. Remove all
[pf3gnuchains/gcc-fork.git] / gcc / c-decl.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.  */
25
26 /* ??? not all decl nodes are given the most useful possible
27    line numbers.  For example, the CONST_DECLs for enum values.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "timevar.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "cgraph.h"
52 #include "hashtab.h"
53 #include "libfuncs.h"
54 #include "except.h"
55
56 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
57 enum decl_context
58 { NORMAL,                       /* Ordinary declaration */
59   FUNCDEF,                      /* Function definition */
60   PARM,                         /* Declaration of parm before function body */
61   FIELD,                        /* Declaration inside struct or union */
62   BITFIELD,                     /* Likewise but with specified width */
63   TYPENAME};                    /* Typename (inside cast or sizeof)  */
64
65 \f
66 /* Nonzero if we have seen an invalid cross reference
67    to a struct, union, or enum, but not yet printed the message.  */
68
69 tree pending_invalid_xref;
70 /* File and line to appear in the eventual error message.  */
71 location_t pending_invalid_xref_location;
72
73 /* While defining an enum type, this is 1 plus the last enumerator
74    constant value.  Note that will do not have to save this or `enum_overflow'
75    around nested function definition since such a definition could only
76    occur in an enum value expression and we don't use these variables in
77    that case.  */
78
79 static tree enum_next_value;
80
81 /* Nonzero means that there was overflow computing enum_next_value.  */
82
83 static int enum_overflow;
84
85 /* Parsing a function declarator leaves a list of parameter names
86    or a chain of parameter decls here.  */
87
88 static tree last_function_parms;
89
90 /* Parsing a function declarator leaves a chain of structure
91    and enum types declared in the parmlist here.  */
92
93 static tree last_function_parm_tags;
94
95 /* After parsing the declarator that starts a function definition,
96    `start_function' puts the list of parameter names or chain of decls here
97    for `store_parm_decls' to find.  */
98
99 static tree current_function_parms;
100
101 /* Similar, for last_function_parm_tags.  */
102
103 static tree current_function_parm_tags;
104
105 /* Similar, for the file and line that the prototype came from if this is
106    an old-style definition.  */
107
108 static location_t current_function_prototype_locus;
109
110 /* The current statement tree.  */
111
112 static GTY(()) struct stmt_tree_s c_stmt_tree;
113
114 /* The current scope statement stack.  */
115
116 static GTY(()) tree c_scope_stmt_stack;
117
118 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
119    that have names.  Here so we can clear out their names' definitions
120    at the end of the function.  */
121
122 static GTY(()) tree named_labels;
123
124 /* A list of LABEL_DECLs from outer contexts that are currently shadowed.  */
125
126 static GTY(()) tree shadowed_labels;
127
128 /* A list of external DECLs that appeared at block scope when there was
129    some other global meaning for that identifier.  */
130 static GTY(()) tree truly_local_externals;
131
132 /* A list of the builtin file-scope DECLs.  */
133
134 static GTY(()) tree builtin_decls;
135
136 /* A DECL for the current file-scope context.  */
137
138 static GTY(()) tree current_file_decl;
139
140 /* Set to 0 at beginning of a function definition, set to 1 if
141    a return statement that specifies a return value is seen.  */
142
143 int current_function_returns_value;
144
145 /* Set to 0 at beginning of a function definition, set to 1 if
146    a return statement with no argument is seen.  */
147
148 int current_function_returns_null;
149
150 /* Set to 0 at beginning of a function definition, set to 1 if
151    a call to a noreturn function is seen.  */
152
153 int current_function_returns_abnormally;
154
155 /* Set to nonzero by `grokdeclarator' for a function
156    whose return type is defaulted, if warnings for this are desired.  */
157
158 static int warn_about_return_type;
159
160 /* Nonzero when starting a function declared `extern inline'.  */
161
162 static int current_extern_inline;
163 \f
164 /* For each binding contour we allocate a binding_level structure
165  * which records the names defined in that contour.
166  * Contours include:
167  *  0) the global one
168  *  1) one for each function definition,
169  *     where internal declarations of the parameters appear.
170  *  2) one for each compound statement,
171  *     to record its declarations.
172  *
173  * The current meaning of a name can be found by searching the levels from
174  * the current one out to the global one.
175  */
176
177 struct binding_level GTY(())
178   {
179     /* A chain of _DECL nodes for all variables, constants, functions,
180        and typedef types.  These are in the reverse of the order supplied.
181      */
182     tree names;
183
184     /* A list of structure, union and enum definitions,
185      * for looking up tag names.
186      * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
187      * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
188      * or ENUMERAL_TYPE node.
189      */
190     tree tags;
191
192     /* For each level, a list of shadowed outer-level definitions
193        to be restored when this level is popped.
194        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
195        whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
196     tree shadowed;
197
198     /* For each level, a list of shadowed outer-level tag definitions
199        to be restored when this level is popped.
200        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
201        whose TREE_VALUE is its old definition (a kind of ..._TYPE node).  */
202     tree shadowed_tags;
203
204     /* For each level (except not the global one),
205        a chain of BLOCK nodes for all the levels
206        that were entered and exited one level down.  */
207     tree blocks;
208
209     /* The binding level which this one is contained in (inherits from).  */
210     struct binding_level *level_chain;
211
212     /* Nonzero if we are currently filling this level with parameter
213        declarations.  */
214     char parm_flag;
215
216     /* Nonzero if this is the outermost block scope of a function body.
217        This scope contains both the parameters and the local variables
218        declared in the outermost block.  */
219     char function_body;
220
221     /* Nonzero means make a BLOCK for this level regardless of all else.  */
222     char keep;
223
224     /* Nonzero means make a BLOCK if this level has any subblocks.  */
225     char keep_if_subblocks;
226
227     /* List of decls in `names' that have incomplete structure or
228        union types.  */
229     tree incomplete_list;
230
231     /* A list of decls giving the (reversed) specified order of parms,
232        not including any forward-decls in the parmlist.
233        This is so we can put the parms in proper order for assign_parms.  */
234     tree parm_order;
235   };
236
237 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
238
239 /* The binding level currently in effect.  */
240
241 static GTY(()) struct binding_level *current_binding_level;
242
243 /* A chain of binding_level structures awaiting reuse.  */
244
245 static GTY((deletable (""))) struct binding_level *free_binding_level;
246
247 /* The outermost binding level, for names of file scope.
248    This is created when the compiler is started and exists
249    through the entire run.  */
250
251 static GTY(()) struct binding_level *global_binding_level;
252
253 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
254
255 static int keep_next_level_flag;
256
257 /* Nonzero means make a BLOCK for the next level pushed
258    if it has subblocks.  */
259
260 static int keep_next_if_subblocks;
261
262 /* The chain of outer levels of label scopes.
263    This uses the same data structure used for binding levels,
264    but it works differently: each link in the chain records
265    saved values of named_labels and shadowed_labels for
266    a label binding level outside the current one.  */
267
268 static GTY(()) struct binding_level *label_level_chain;
269
270 /* Functions called automatically at the beginning and end of execution.  */
271
272 tree static_ctors, static_dtors;
273
274 /* Forward declarations.  */
275
276 static struct binding_level *make_binding_level (void);
277 static void pop_binding_level (struct binding_level **);
278 static int duplicate_decls (tree, tree, int, int);
279 static int redeclaration_error_message (tree, tree);
280 static void implicit_decl_warning (tree);
281 static void storedecls (tree);
282 static void storetags (tree);
283 static tree lookup_tag (enum tree_code, tree, int);
284 static tree lookup_name_current_level (tree);
285 static tree grokdeclarator (tree, tree, enum decl_context, int);
286 static tree grokparms (tree, int);
287 static void layout_array_type (tree);
288 static tree c_make_fname_decl (tree, int);
289 static void c_expand_body_1 (tree, int);
290 static tree any_external_decl (tree);
291 static void record_external_decl (tree);
292 static void warn_if_shadowing (tree, tree);
293 static void clone_underlying_type (tree);
294 static bool flexible_array_type_p (tree);
295 static hashval_t link_hash_hash (const void *);
296 static int link_hash_eq (const void *, const void *);
297 \f
298 /* States indicating how grokdeclarator() should handle declspecs marked
299    with __attribute__((deprecated)).  An object declared as
300    __attribute__((deprecated)) suppresses warnings of uses of other
301    deprecated items.  */
302
303 enum deprecated_states {
304   DEPRECATED_NORMAL,
305   DEPRECATED_SUPPRESS
306 };
307
308 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
309
310 void
311 c_print_identifier (FILE *file, tree node, int indent)
312 {
313   print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
314   print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
315   print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
316   if (C_IS_RESERVED_WORD (node))
317     {
318       tree rid = ridpointers[C_RID_CODE (node)];
319       indent_to (file, indent + 4);
320       fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
321                (void *) rid, IDENTIFIER_POINTER (rid));
322     }
323 }
324 \f
325 /* Hook called at end of compilation to assume 1 elt
326    for a top-level tentative array defn that wasn't complete before.  */
327
328 void
329 c_finish_incomplete_decl (tree decl)
330 {
331   if (TREE_CODE (decl) == VAR_DECL)
332     {
333       tree type = TREE_TYPE (decl);
334       if (type != error_mark_node
335           && TREE_CODE (type) == ARRAY_TYPE
336           && ! DECL_EXTERNAL (decl)
337           && TYPE_DOMAIN (type) == 0)
338         {
339           warning_with_decl (decl, "array `%s' assumed to have one element");
340
341           complete_array_type (type, NULL_TREE, 1);
342
343           layout_decl (decl, 0);
344         }
345     }
346 }
347 \f
348 /* Reuse or create a struct for this binding level.  */
349
350 static struct binding_level *
351 make_binding_level (void)
352 {
353   struct binding_level *result;
354   if (free_binding_level)
355     {
356       result = free_binding_level;
357       free_binding_level = result->level_chain;
358       memset (result, 0, sizeof(struct binding_level));
359     }
360   else
361     result = (struct binding_level *)
362       ggc_alloc_cleared (sizeof (struct binding_level));
363
364   return result;
365 }
366
367 /* Remove a binding level from a list and add it to the level chain.  */
368
369 static void
370 pop_binding_level (struct binding_level **lp)
371 {
372   struct binding_level *l = *lp;
373   *lp = l->level_chain;
374
375   memset (l, 0, sizeof (struct binding_level));
376   l->level_chain = free_binding_level;
377   free_binding_level = l;
378 }
379
380 /* Nonzero if we are currently in the global binding level.  */
381
382 int
383 global_bindings_p (void)
384 {
385   return current_binding_level == global_binding_level;
386 }
387
388 void
389 keep_next_level (void)
390 {
391   keep_next_level_flag = 1;
392 }
393
394 /* Nonzero if the current level needs to have a BLOCK made.  */
395
396 int
397 kept_level_p (void)
398 {
399   return ((current_binding_level->keep_if_subblocks
400            && current_binding_level->blocks != 0)
401           || current_binding_level->keep
402           || current_binding_level->names != 0
403           || current_binding_level->tags != 0);
404 }
405
406 /* Identify this binding level as a level of parameters.  */
407
408 void
409 declare_parm_level (void)
410 {
411   current_binding_level->parm_flag = 1;
412 }
413
414 /* Nonzero if currently making parm declarations.  */
415
416 int
417 in_parm_level_p (void)
418 {
419   return current_binding_level->parm_flag;
420 }
421
422 /* Enter a new binding level.  */
423
424 void
425 pushlevel (int dummy ATTRIBUTE_UNUSED)
426 {
427   /* If this is the top level of a function, make sure that
428      NAMED_LABELS is 0.  */
429
430   if (current_binding_level == global_binding_level)
431     named_labels = 0;
432
433   if (keep_next_if_subblocks)
434     {
435       /* This is the transition from the parameters to the top level
436          of the function body.  These are the same scope
437          (C99 6.2.1p4,6) so we do not push another binding level.
438
439          XXX Note kludge - keep_next_if_subblocks is set only by
440          store_parm_decls, which in turn is called when and only
441          when we are about to encounter the opening curly brace for
442          the function body.  */
443       current_binding_level->parm_flag = 0;
444       current_binding_level->function_body = 1;
445       current_binding_level->keep |= keep_next_level_flag;
446       current_binding_level->keep_if_subblocks = 1;
447
448       keep_next_level_flag = 0;
449       keep_next_if_subblocks = 0;
450     }
451   else
452     {
453       struct binding_level *newlevel = make_binding_level ();
454
455       newlevel->keep              = keep_next_level_flag;
456       newlevel->level_chain       = current_binding_level;
457       current_binding_level = newlevel;
458       keep_next_level_flag = 0;
459     }
460 }
461
462 /* Exit a binding level.
463    Pop the level off, and restore the state of the identifier-decl mappings
464    that were in effect when this level was entered.
465
466    If KEEP is nonzero, this level had explicit declarations, so
467    and create a "block" (a BLOCK node) for the level
468    to record its declarations and subblocks for symbol table output.
469
470    If FUNCTIONBODY is nonzero, this level is the body of a function,
471    so create a block as if KEEP were set and also clear out all
472    label names.
473
474    If REVERSE is nonzero, reverse the order of decls before putting
475    them into the BLOCK.  */
476
477 tree
478 poplevel (int keep, int reverse, int functionbody)
479 {
480   tree link;
481   tree block;
482   tree decl;
483   tree decls = current_binding_level->names;
484   tree tags = current_binding_level->tags;
485   tree subblocks = current_binding_level->blocks;
486
487   functionbody |= current_binding_level->function_body;
488   keep |= (current_binding_level->keep || functionbody
489            || (current_binding_level->keep_if_subblocks && subblocks != 0));
490
491   /* We used to warn about unused variables in expand_end_bindings,
492      i.e. while generating RTL.  But in function-at-a-time mode we may
493      choose to never expand a function at all (e.g. auto inlining), so
494      we do this explicitly now.
495      No warnings when the global scope is popped because the global
496      scope isn't popped for the last translation unit, so the warnings
497      are done in c_write_global_declaration.  */
498   if (current_binding_level != global_binding_level)
499     warn_about_unused_variables (decls);
500
501   /* Clear out the name-meanings declared on this level.
502      Propagate TREE_ADDRESSABLE from nested functions to their
503      containing functions.  */
504   for (link = decls; link; link = TREE_CHAIN (link))
505     {
506       if (DECL_NAME (link) != 0)
507         {
508           if (DECL_EXTERNAL (link) 
509               && current_binding_level != global_binding_level)
510             /* External decls stay in the symbol-value slot but are
511                inaccessible.  */
512             C_DECL_INVISIBLE (link) = 1;
513           else
514             IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
515         }
516
517       if (TREE_CODE (link) == FUNCTION_DECL
518           && ! TREE_ASM_WRITTEN (link)
519           && DECL_INITIAL (link) != 0
520           && TREE_ADDRESSABLE (link)
521           && DECL_ABSTRACT_ORIGIN (link) != 0
522           && DECL_ABSTRACT_ORIGIN (link) != link)
523         TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
524     }
525
526   /* Clear out the tag-meanings declared on this level.  */
527   for (link = tags; link; link = TREE_CHAIN (link))
528     if (TREE_PURPOSE (link))
529       IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
530
531   /* Restore all name-meanings of the outer levels
532      that were shadowed by this level.  */
533
534   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
535     IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
536
537   /* Restore all tag-meanings of the outer levels
538      that were shadowed by this level.  */
539
540   for (link = current_binding_level->shadowed_tags; link;
541        link = TREE_CHAIN (link))
542     IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
543
544   /* If this is the top level block of a function, remove all
545      PARM_DECLs from current_binding_level->names; they are already
546      stored in DECL_ARGUMENTS of cfun->decl in proper order, should
547      not be put in BLOCK_VARS, and furthermore reversing them will
548      cause trouble later.  They are all together at the end of the
549      list.  */
550   if (functionbody && decls)
551     {
552       if (TREE_CODE (decls) == PARM_DECL)
553         decls = 0;
554       else
555         {
556           link = decls;
557           while (TREE_CHAIN (link)
558                  && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
559             link = TREE_CHAIN (link);
560
561           TREE_CHAIN (link) = 0;
562         }
563     }
564
565   /* Get the decls in the order they were written.
566      Usually current_binding_level->names is in reverse order.
567      But parameter decls were previously put in forward order.  */
568
569   if (reverse)
570     decls = nreverse (decls);
571
572   /* If there were any declarations or structure tags in that level,
573      or if this level is a function body,
574      create a BLOCK to record them for the life of this function.  */
575
576   block = 0;
577   if (keep)
578     {
579       block = make_node (BLOCK);
580       BLOCK_VARS (block) = decls;
581       BLOCK_SUBBLOCKS (block) = subblocks;
582       TREE_USED (block) = 1;
583     }
584
585   /* In each subblock, record that this is its superior.  */
586
587   for (link = subblocks; link; link = TREE_CHAIN (link))
588     BLOCK_SUPERCONTEXT (link) = block;
589
590   /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
591      binding contour so that they point to the appropriate construct, i.e.
592      either to the current FUNCTION_DECL node, or else to the BLOCK node
593      we just constructed.
594
595      Note that for tagged types whose scope is just the formal parameter
596      list for some function type specification, we can't properly set
597      their TYPE_CONTEXTs here, because we don't have a pointer to the
598      appropriate FUNCTION_TYPE node readily available to us.  For those
599      cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
600      in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
601      node which will represent the "scope" for these "parameter list local"
602      tagged types.  */
603
604   decl = functionbody ? current_function_decl : block;
605   if (decl)
606     for (link = tags; link; link = TREE_CHAIN (link))
607       TYPE_CONTEXT (TREE_VALUE (link)) = decl;
608
609   /* If the level being exited is the top level of a function, check
610      over all the labels, and clear out the current (function local)
611      meanings of their names. Then add them to BLOCK_VARS.  */
612
613   if (functionbody)
614     {
615       for (link = named_labels; link; link = TREE_CHAIN (link))
616         {
617           tree label = TREE_VALUE (link);
618
619           if (DECL_INITIAL (label) == 0)
620             {
621               error_with_decl (label, "label `%s' used but not defined");
622               /* Avoid crashing later.  */
623               define_label (input_location, DECL_NAME (label));
624             }
625           else if (warn_unused_label && !TREE_USED (label))
626             warning_with_decl (label, "label `%s' defined but not used");
627           IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
628
629           /* Put the labels into the "variables" of the
630              top-level block, so debugger can see them.  */
631           TREE_CHAIN (label) = BLOCK_VARS (block);
632           BLOCK_VARS (block) = label;
633         }
634     }
635
636   /* Pop the current level, and free the structure for reuse.  */
637
638   pop_binding_level (&current_binding_level);
639
640   /* Dispose of the block that we just made inside some higher level.  */
641   if (functionbody)
642     DECL_INITIAL (current_function_decl) = block;
643   else if (block && current_binding_level)
644     current_binding_level->blocks
645       = chainon (current_binding_level->blocks, block);
646   /* If we did not make a block for the level just exited,
647      any blocks made for inner levels
648      (since they cannot be recorded as subblocks in that level)
649      must be carried forward so they will later become subblocks
650      of something else.  */
651   else if (! block && subblocks)
652     current_binding_level->blocks
653       = chainon (current_binding_level->blocks, subblocks);
654
655   return block;
656 }
657
658 /* Insert BLOCK at the end of the list of subblocks of the
659    current binding level.  This is used when a BIND_EXPR is expanded,
660    to handle the BLOCK node inside the BIND_EXPR.  */
661
662 void
663 insert_block (tree block)
664 {
665   TREE_USED (block) = 1;
666   current_binding_level->blocks
667     = chainon (current_binding_level->blocks, block);
668 }
669
670 /* Set the BLOCK node for the innermost scope (the one we are
671    currently in).  The RTL expansion machinery requires us to provide
672    this hook, but it is not useful in function-at-a-time mode.  */
673
674 void
675 set_block (tree block ATTRIBUTE_UNUSED)
676 {
677 }
678 \f
679 void
680 push_label_level (void)
681 {
682   struct binding_level *newlevel;
683
684   newlevel = make_binding_level ();
685
686   /* Add this level to the front of the chain (stack) of label levels.  */
687
688   newlevel->level_chain = label_level_chain;
689   label_level_chain = newlevel;
690
691   newlevel->names = named_labels;
692   newlevel->shadowed = shadowed_labels;
693   named_labels = 0;
694   shadowed_labels = 0;
695 }
696
697 void
698 pop_label_level (void)
699 {
700   struct binding_level *level = label_level_chain;
701   tree link, prev;
702
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;)
706     {
707       if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
708         {
709           if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
710             {
711               error_with_decl (TREE_VALUE (link),
712                                "label `%s' used but not defined");
713               /* Avoid crashing later.  */
714               define_label (input_location, DECL_NAME (TREE_VALUE (link)));
715             }
716           else if (warn_unused_label && !TREE_USED (TREE_VALUE (link)))
717             warning_with_decl (TREE_VALUE (link),
718                                "label `%s' defined but not used");
719           IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
720
721           /* Delete this element from the list.  */
722           link = TREE_CHAIN (link);
723           if (prev)
724             TREE_CHAIN (prev) = link;
725           else
726             named_labels = link;
727         }
728       else
729         {
730           prev = link;
731           link = TREE_CHAIN (link);
732         }
733     }
734
735   /* Bring back all the labels that were shadowed.  */
736   for (link = shadowed_labels; link; link = TREE_CHAIN (link))
737     if (DECL_NAME (TREE_VALUE (link)) != 0)
738       IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
739         = TREE_VALUE (link);
740
741   named_labels = chainon (named_labels, level->names);
742   shadowed_labels = level->shadowed;
743
744   /* Pop the current level, and free the structure for reuse.  */
745   pop_binding_level (&label_level_chain);
746 }
747 \f
748 /* Push a definition or a declaration of struct, union or enum tag "name".
749    "type" should be the type node.
750    We assume that the tag "name" is not already defined.
751
752    Note that the definition may really be just a forward reference.
753    In that case, the TYPE_SIZE will be zero.  */
754
755 void
756 pushtag (tree name, tree type)
757 {
758   struct binding_level *b = current_binding_level;
759
760   if (name)
761     {
762       /* Record the identifier as the type's name if it has none.  */
763
764       if (TYPE_NAME (type) == 0)
765         TYPE_NAME (type) = name;
766
767       if (IDENTIFIER_TAG_VALUE (name))
768         b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
769                                       b->shadowed_tags);
770       IDENTIFIER_TAG_VALUE (name) = type;
771     }
772
773   b->tags = tree_cons (name, type, b->tags);
774
775   /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
776      tagged type we just added to the current binding level.  This fake
777      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
778      to output a representation of a tagged type, and it also gives
779      us a convenient place to record the "scope start" address for the
780      tagged type.  */
781
782   TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
783
784   /* An approximation for now, so we can tell this is a function-scope tag.
785      This will be updated in poplevel.  */
786   TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
787 }
788 \f
789 /* Handle when a new declaration NEWDECL
790    has the same name as an old one OLDDECL
791    in the same binding contour.
792    Prints an error message if appropriate.
793
794    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
795    Otherwise, return 0.
796
797    When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
798    and OLDDECL is in an outer binding level and should thus not be changed.  */
799
800 static int
801 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
802                  int different_tu)
803 {
804   int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
805                         : COMPARE_STRICT);
806   int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
807                                comptype_flags);
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;
813
814   if (DECL_P (olddecl))
815     {
816       if (TREE_CODE (newdecl) == FUNCTION_DECL
817           && TREE_CODE (olddecl) == FUNCTION_DECL
818           && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
819         {
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)))
832             {
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");
837             }
838           else if (DECL_DECLARED_INLINE_P (olddecl)
839                    && DECL_UNINLINABLE (newdecl)
840                    && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
841             {
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");
846             }
847         }
848
849       DECL_ATTRIBUTES (newdecl)
850         = (*targetm.merge_decl_attributes) (olddecl, newdecl);
851     }
852
853   if (TREE_CODE (newtype) == ERROR_MARK
854       || TREE_CODE (oldtype) == ERROR_MARK)
855     types_match = 0;
856
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))
861     {
862       if (TREE_CODE (olddecl) == FUNCTION_DECL
863           && DECL_BUILT_IN (olddecl))
864         {
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))
869             {
870               if (warn_shadow)
871                 warning_with_decl (newdecl, "shadowing built-in function `%s'");
872             }
873           else
874             warning_with_decl (newdecl,
875                                "built-in function `%s' declared as non-function");
876         }
877       else
878         {
879           error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
880           error_with_decl (olddecl, "previous declaration of `%s'");
881         }
882
883       return 0;
884     }
885
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))
890     {
891       TREE_ASM_WRITTEN (olddecl) = 0;
892       return 1;
893     }
894
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.  */
899
900   if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
901     {
902       /* A function declaration for a built-in function.  */
903       if (!TREE_PUBLIC (newdecl))
904         {
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;
912         }
913       else if (!types_match)
914         {
915           /* Accept the return type of the new declaration if same modes.  */
916           tree oldreturntype = TREE_TYPE (oldtype);
917           tree newreturntype = TREE_TYPE (newtype);
918
919           if (TYPE_MODE (oldreturntype) == TYPE_MODE (newreturntype))
920             {
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));
928
929               types_match = comptypes (newtype, trytype, comptype_flags);
930               if (types_match)
931                 oldtype = trytype;
932             }
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)))))
941             {
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));
951
952               types_match = comptypes (newtype, trytype, comptype_flags);
953               if (types_match)
954                 oldtype = trytype;
955             }
956           if (! different_binding_level)
957             TREE_TYPE (olddecl) = oldtype;
958         }
959       else if (TYPE_ARG_TYPES (oldtype) == NULL
960                && TYPE_ARG_TYPES (newtype) != NULL)
961         {
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));
970
971           oldtype = trytype;
972           if (! different_binding_level)
973             TREE_TYPE (olddecl) = oldtype;
974         }
975       if (!types_match)
976         {
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;
980         }
981     }
982   else if (TREE_CODE (olddecl) == FUNCTION_DECL
983            && DECL_SOURCE_LINE (olddecl) == 0)
984     {
985       /* A function declaration for a predeclared function
986          that isn't actually built in.  */
987       if (!TREE_PUBLIC (newdecl))
988         {
989           /* If you declare it as static, the
990              default definition is overridden.  */
991           return 0;
992         }
993       else if (!types_match)
994         {
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);
999         }
1000     }
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)
1014                ||
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)))
1019     {
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;
1025
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;
1029     }
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))
1038     {
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;
1043     }
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), comptype_flags)
1052                  && TYPE_ARG_TYPES (newtype) == 0))
1053     {
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), comptype_flags)
1060           && ((TYPE_ARG_TYPES (oldtype) == 0
1061                && DECL_INITIAL (olddecl) == 0)
1062               ||
1063               (TYPE_ARG_TYPES (newtype) == 0
1064                && DECL_INITIAL (newdecl) == 0)))
1065         {
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))
1071             {
1072               tree type = TREE_VALUE (t);
1073
1074               if (TREE_CHAIN (t) == 0
1075                   && TYPE_MAIN_VARIANT (type) != void_type_node)
1076                 {
1077                   error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1078                   break;
1079                 }
1080
1081               if (c_type_promotes_to (type) != type)
1082                 {
1083                   error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1084                   break;
1085                 }
1086             }
1087         }
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'");
1092
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;
1099     }
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))
1103     {
1104       error_with_decl (newdecl, "thread-local declaration of `%s' follows non thread-local declaration");
1105       error_with_decl (olddecl, "previous declaration of `%s'");
1106     }
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))
1110     {
1111       error_with_decl (newdecl, "non thread-local declaration of `%s' follows thread-local declaration");
1112       error_with_decl (olddecl, "previous declaration of `%s'");
1113     }
1114   else
1115     {
1116       errmsg = redeclaration_error_message (newdecl, olddecl);
1117       if (errmsg)
1118         {
1119           switch (errmsg)
1120             {
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 ();
1132             }
1133
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;
1140         }
1141       else if (TREE_CODE (newdecl) == TYPE_DECL
1142                && (DECL_IN_SYSTEM_HEADER (olddecl)
1143                    || DECL_IN_SYSTEM_HEADER (newdecl)))
1144         {
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"));
1152         }
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)
1158         {
1159           tree type, parm;
1160           int nargs;
1161           /* Prototype decl follows defn w/o prototype.  */
1162
1163           for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1164                type = TYPE_ARG_TYPES (newtype),
1165                nargs = 1;
1166                ;
1167                parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1168             {
1169               if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1170                   && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1171                 {
1172                   warning_with_decl (newdecl, "prototype for `%s' follows");
1173                   warning_with_decl (olddecl, "non-prototype definition here");
1174                   break;
1175                 }
1176               if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1177                   || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1178                 {
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;
1184                 }
1185               /* Type for passing arg must be consistent
1186                  with that declared for the arg.  */
1187               if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1188                                comptype_flags))
1189                 {
1190                   error_with_decl (newdecl,
1191                                    "prototype for `%s' follows and argument %d doesn't match",
1192                                    nargs);
1193                   error_with_decl (olddecl, "non-prototype definition here");
1194                   errmsg = 1;
1195                   break;
1196                 }
1197             }
1198         }
1199       /* Warn about mismatches in various flags.  */
1200       else
1201         {
1202           /* Warn if function is now inline
1203              but was previously declared not inline and has been called.  */
1204           if (TREE_CODE (olddecl) == FUNCTION_DECL
1205               && ! DECL_DECLARED_INLINE_P (olddecl)
1206               && DECL_DECLARED_INLINE_P (newdecl)
1207               && TREE_USED (olddecl))
1208             warning_with_decl (newdecl,
1209                                "`%s' declared inline after being called");
1210           if (TREE_CODE (olddecl) == FUNCTION_DECL
1211               && ! DECL_DECLARED_INLINE_P (olddecl)
1212               && DECL_DECLARED_INLINE_P (newdecl)
1213               && DECL_INITIAL (olddecl) != 0)
1214             warning_with_decl (newdecl,
1215                                "`%s' declared inline after its definition");
1216
1217           /* If pedantic, warn when static declaration follows a non-static
1218              declaration.  Otherwise, do so only for functions.  */
1219           if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1220               && TREE_PUBLIC (olddecl)
1221               && !TREE_PUBLIC (newdecl))
1222             warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1223
1224           /* If warn_traditional, warn when a non-static function
1225              declaration follows a static one.  */
1226           if (warn_traditional && !in_system_header
1227               && TREE_CODE (olddecl) == FUNCTION_DECL
1228               && !TREE_PUBLIC (olddecl)
1229               && TREE_PUBLIC (newdecl))
1230             warning_with_decl (newdecl, "non-static declaration for `%s' follows static");
1231
1232           /* Warn when const declaration follows a non-const
1233              declaration, but not for functions.  */
1234           if (TREE_CODE (olddecl) != FUNCTION_DECL
1235               && !TREE_READONLY (olddecl)
1236               && TREE_READONLY (newdecl))
1237             warning_with_decl (newdecl, "const declaration for `%s' follows non-const");
1238           /* These bits are logically part of the type, for variables.
1239              But not for functions
1240              (where qualifiers are not valid ANSI anyway).  */
1241           else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1242               && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1243                   || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1244             pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1245         }
1246     }
1247
1248   /* Optionally warn about more than one declaration for the same name.  */
1249   if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1250       /* Don't warn about a function declaration
1251          followed by a definition.  */
1252       && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1253            && DECL_INITIAL (olddecl) == 0)
1254       /* Don't warn about extern decl followed by (tentative) definition.  */
1255       && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1256     {
1257       warning_with_decl (newdecl, "redundant redeclaration of `%s' in same scope");
1258       warning_with_decl (olddecl, "previous declaration of `%s'");
1259     }
1260
1261   /* Copy all the DECL_... slots specified in the new decl
1262      except for any that we copy here from the old type.
1263
1264      Past this point, we don't change OLDTYPE and NEWTYPE
1265      even if we change the types of NEWDECL and OLDDECL.  */
1266
1267   if (types_match)
1268     {
1269       /* When copying info to olddecl, we store into write_olddecl
1270          instead.  This allows us to avoid modifying olddecl when
1271          different_binding_level is true.  */
1272       tree write_olddecl = different_binding_level ? newdecl : olddecl;
1273
1274       /* Merge the data types specified in the two decls.  */
1275       if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1276         {
1277           if (different_binding_level)
1278             {
1279               if (TYPE_ARG_TYPES (oldtype) != 0
1280                   && TYPE_ARG_TYPES (newtype) == 0)
1281                 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1282               else
1283                 TREE_TYPE (newdecl)
1284                   = build_type_attribute_variant
1285                     (newtype,
1286                      merge_attributes (TYPE_ATTRIBUTES (newtype),
1287                                        TYPE_ATTRIBUTES (oldtype)));
1288             }
1289           else
1290             TREE_TYPE (newdecl)
1291               = TREE_TYPE (olddecl)
1292                 = common_type (newtype, oldtype);
1293         }
1294
1295       /* Lay the type out, unless already done.  */
1296       if (oldtype != TREE_TYPE (newdecl))
1297         {
1298           if (TREE_TYPE (newdecl) != error_mark_node)
1299             layout_type (TREE_TYPE (newdecl));
1300           if (TREE_CODE (newdecl) != FUNCTION_DECL
1301               && TREE_CODE (newdecl) != TYPE_DECL
1302               && TREE_CODE (newdecl) != CONST_DECL)
1303             layout_decl (newdecl, 0);
1304         }
1305       else
1306         {
1307           /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1308           DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1309           DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1310           DECL_MODE (newdecl) = DECL_MODE (olddecl);
1311           if (TREE_CODE (olddecl) != FUNCTION_DECL)
1312             if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1313               {
1314                 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1315                 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1316               }
1317         }
1318
1319       /* Keep the old rtl since we can safely use it.  */
1320       COPY_DECL_RTL (olddecl, newdecl);
1321
1322       /* Merge the type qualifiers.  */
1323       if (TREE_READONLY (newdecl))
1324         TREE_READONLY (write_olddecl) = 1;
1325
1326       if (TREE_THIS_VOLATILE (newdecl))
1327         {
1328           TREE_THIS_VOLATILE (write_olddecl) = 1;
1329           if (TREE_CODE (newdecl) == VAR_DECL
1330               /* If an automatic variable is re-declared in the same
1331                  function scope, but the old declaration was not
1332                  volatile, make_var_volatile() would crash because the
1333                  variable would have been assigned to a pseudo, not a
1334                  MEM.  Since this duplicate declaration is invalid
1335                  anyway, we just skip the call.  */
1336               && errmsg == 0)
1337             make_var_volatile (newdecl);
1338         }
1339
1340       /* Keep source location of definition rather than declaration.  */
1341       /* When called with different_binding_level set, keep the old
1342          information so that meaningful diagnostics can be given.  */
1343       if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1344           && ! different_binding_level)
1345         {
1346           DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1347           DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1348         }
1349
1350       /* Merge the unused-warning information.  */
1351       if (DECL_IN_SYSTEM_HEADER (olddecl))
1352         DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1353       else if (DECL_IN_SYSTEM_HEADER (newdecl))
1354         DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1355
1356       /* Merge the initialization information.  */
1357       /* When called with different_binding_level set, don't copy over
1358          DECL_INITIAL, so that we don't accidentally change function
1359          declarations into function definitions.  */
1360       if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1361         DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1362
1363       /* Merge the section attribute.
1364          We want to issue an error if the sections conflict but that must be
1365          done later in decl_attributes since we are called before attributes
1366          are assigned.  */
1367       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1368         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1369
1370       /* Copy the assembler name.
1371          Currently, it can only be defined in the prototype.  */
1372       COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1373
1374       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1375         {
1376           DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1377           DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1378           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1379           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1380             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1381           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1382           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1383           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1384           DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1385         }
1386     }
1387   /* If cannot merge, then use the new type and qualifiers,
1388      and don't preserve the old rtl.  */
1389   else if (! different_binding_level)
1390     {
1391       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1392       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1393       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1394       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1395     }
1396
1397   /* Merge the storage class information.  */
1398   merge_weak (newdecl, olddecl);
1399
1400   /* For functions, static overrides non-static.  */
1401   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1402     {
1403       TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1404       /* This is since we don't automatically
1405          copy the attributes of NEWDECL into OLDDECL.  */
1406       /* No need to worry about different_binding_level here because
1407          then TREE_PUBLIC (newdecl) was true.  */
1408       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1409       /* If this clears `static', clear it in the identifier too.  */
1410       if (! TREE_PUBLIC (olddecl))
1411         TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1412     }
1413   if (DECL_EXTERNAL (newdecl))
1414     {
1415       if (! different_binding_level || different_tu)
1416         {
1417           /* Don't mess with these flags on local externs; they remain
1418              external even if there's a declaration at file scope which
1419              isn't.  */
1420           TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1421           DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1422         }
1423       /* An extern decl does not override previous storage class.  */
1424       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1425       if (! DECL_EXTERNAL (newdecl))
1426         {
1427           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1428           /* If we have two non-EXTERNAL file-scope decls that are
1429              the same, only one of them should be written out.  */
1430           if (different_tu)
1431             TREE_ASM_WRITTEN (newdecl) = 1;
1432         }
1433     }
1434   else
1435     {
1436       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1437       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1438     }
1439
1440   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1441     {
1442       /* If we're redefining a function previously defined as extern
1443          inline, make sure we emit debug info for the inline before we
1444          throw it away, in case it was inlined into a function that hasn't
1445          been written out yet.  */
1446       if (new_is_definition && DECL_INITIAL (olddecl))
1447         {
1448           if (TREE_USED (olddecl))
1449             (*debug_hooks->outlining_inline_function) (olddecl);
1450
1451           /* The new defn must not be inline.  */
1452           DECL_INLINE (newdecl) = 0;
1453           DECL_UNINLINABLE (newdecl) = 1;
1454         }
1455       else
1456         {
1457           /* If either decl says `inline', this fn is inline,
1458              unless its definition was passed already.  */
1459           if (DECL_DECLARED_INLINE_P (newdecl)
1460               || DECL_DECLARED_INLINE_P (olddecl))
1461             DECL_DECLARED_INLINE_P (newdecl) = 1;
1462
1463           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1464             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1465         }
1466
1467       if (DECL_BUILT_IN (olddecl))
1468         {
1469           /* Get rid of any built-in function if new arg types don't match it
1470              or if we have a function definition.  */
1471           if (! types_match || new_is_definition)
1472             {
1473               if (! different_binding_level)
1474                 {
1475                   TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1476                   DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1477                 }
1478             }
1479           else
1480             {
1481               /* If redeclaring a builtin function, and not a definition,
1482                  it stays built in.  */
1483               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1484               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1485             }
1486         }
1487
1488       /* Also preserve various other info from the definition.  */
1489       if (! new_is_definition)
1490         {
1491           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1492           /* When called with different_binding_level set, don't copy over
1493              DECL_INITIAL, so that we don't accidentally change function
1494              declarations into function definitions.  */
1495           if (! different_binding_level)
1496             DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1497           DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1498           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1499           DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1500           DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1501
1502           /* Set DECL_INLINE on the declaration if we've got a body
1503              from which to instantiate.  */
1504           if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1505             {
1506               DECL_INLINE (newdecl) = 1;
1507               DECL_ABSTRACT_ORIGIN (newdecl)
1508                 = (different_binding_level
1509                    ? DECL_ORIGIN (olddecl)
1510                    : DECL_ABSTRACT_ORIGIN (olddecl));
1511             }
1512         }
1513       else
1514         {
1515           /* If a previous declaration said inline, mark the
1516              definition as inlinable.  */
1517           if (DECL_DECLARED_INLINE_P (newdecl)
1518               && ! DECL_UNINLINABLE (newdecl))
1519             DECL_INLINE (newdecl) = 1;
1520         }
1521     }
1522   if (different_binding_level)
1523     return 0;
1524
1525   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1526      But preserve OLDDECL's DECL_UID.  */
1527   {
1528     unsigned olddecl_uid = DECL_UID (olddecl);
1529
1530     memcpy ((char *) olddecl + sizeof (struct tree_common),
1531             (char *) newdecl + sizeof (struct tree_common),
1532             sizeof (struct tree_decl) - sizeof (struct tree_common));
1533     DECL_UID (olddecl) = olddecl_uid;
1534   }
1535
1536   /* NEWDECL contains the merged attribute lists.
1537      Update OLDDECL to be the same.  */
1538   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1539
1540   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1541      so that encode_section_info has a chance to look at the new decl
1542      flags and attributes.  */
1543   if (DECL_RTL_SET_P (olddecl)
1544       && (TREE_CODE (olddecl) == FUNCTION_DECL
1545           || (TREE_CODE (olddecl) == VAR_DECL
1546               && TREE_STATIC (olddecl))))
1547     make_decl_rtl (olddecl, NULL);
1548
1549   return 1;
1550 }
1551
1552 /* Return any external DECL associated with ID, whether or not it is
1553    currently in scope.  */
1554
1555 static tree
1556 any_external_decl (tree id)
1557 {
1558   tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1559   tree t;
1560
1561   if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1562     return 0;
1563   else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1564     return decl;
1565
1566   t = purpose_member (id, truly_local_externals);
1567   if (t)
1568     return TREE_VALUE (t);
1569
1570   return 0;
1571 }
1572
1573 /* Record an external decl DECL.  This only does something if a
1574    shadowing decl already exists.  */
1575 static void
1576 record_external_decl (tree decl)
1577 {
1578   tree name = DECL_NAME (decl);
1579   if (!IDENTIFIER_SYMBOL_VALUE (name))
1580     return;
1581
1582   truly_local_externals = tree_cons (name, decl, truly_local_externals);
1583 }
1584
1585 /* Check whether decl-node X shadows an existing declaration.
1586    OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1587    which might be a NULL_TREE.  */
1588 static void
1589 warn_if_shadowing (tree x, tree old)
1590 {
1591   const char *name;
1592
1593   /* Nothing to shadow?  */
1594   if (old == 0
1595       /* Shadow warnings not wanted?  */
1596       || !warn_shadow
1597       /* No shadow warnings for internally generated vars.  */
1598       || DECL_SOURCE_LINE (x) == 0
1599       /* No shadow warnings for vars made for inlining.  */
1600       || DECL_FROM_INLINE (x)
1601       /* Don't warn about the parm names in function declarator
1602          within a function declarator.
1603          It would be nice to avoid warning in any function
1604          declarator in a declaration, as opposed to a definition,
1605          but there is no way to tell it's not a definition.  */
1606       || (TREE_CODE (x) == PARM_DECL
1607           && current_binding_level->level_chain->parm_flag))
1608     return;
1609
1610   name = IDENTIFIER_POINTER (DECL_NAME (x));
1611
1612   if (TREE_CODE (old) == PARM_DECL)
1613     shadow_warning (SW_PARAM, name, old);
1614   else if (C_DECL_FILE_SCOPE (old))
1615     shadow_warning (SW_GLOBAL, name, old);
1616   else
1617     shadow_warning (SW_LOCAL, name, old);
1618 }
1619
1620
1621 /* Subroutine of pushdecl.
1622
1623    X is a TYPE_DECL for a typedef statement.  Create a brand new
1624    ..._TYPE node (which will be just a variant of the existing
1625    ..._TYPE node with identical properties) and then install X
1626    as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1627
1628    The whole point here is to end up with a situation where each
1629    and every ..._TYPE node the compiler creates will be uniquely
1630    associated with AT MOST one node representing a typedef name.
1631    This way, even though the compiler substitutes corresponding
1632    ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1633    early on, later parts of the compiler can always do the reverse
1634    translation and get back the corresponding typedef name.  For
1635    example, given:
1636
1637         typedef struct S MY_TYPE;
1638         MY_TYPE object;
1639
1640    Later parts of the compiler might only know that `object' was of
1641    type `struct S' if it were not for code just below.  With this
1642    code however, later parts of the compiler see something like:
1643
1644         struct S' == struct S
1645         typedef struct S' MY_TYPE;
1646         struct S' object;
1647
1648     And they can then deduce (from the node for type struct S') that
1649     the original object declaration was:
1650
1651                 MY_TYPE object;
1652
1653     Being able to do this is important for proper support of protoize,
1654     and also for generating precise symbolic debugging information
1655     which takes full account of the programmer's (typedef) vocabulary.
1656
1657     Obviously, we don't want to generate a duplicate ..._TYPE node if
1658     the TYPE_DECL node that we are now processing really represents a
1659     standard built-in type.
1660
1661     Since all standard types are effectively declared at line zero
1662     in the source file, we can easily check to see if we are working
1663     on a standard type by checking the current value of lineno.  */
1664
1665 static void
1666 clone_underlying_type (tree x)
1667 {
1668   if (DECL_SOURCE_LINE (x) == 0)
1669     {
1670       if (TYPE_NAME (TREE_TYPE (x)) == 0)
1671         TYPE_NAME (TREE_TYPE (x)) = x;
1672     }
1673   else if (TREE_TYPE (x) != error_mark_node
1674            && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1675     {
1676       tree tt = TREE_TYPE (x);
1677       DECL_ORIGINAL_TYPE (x) = tt;
1678       tt = build_type_copy (tt);
1679       TYPE_NAME (tt) = x;
1680       TREE_USED (tt) = TREE_USED (x);
1681       TREE_TYPE (x) = tt;
1682     }
1683 }
1684
1685 /* Record a decl-node X as belonging to the current lexical scope.
1686    Check for errors (such as an incompatible declaration for the same
1687    name already seen in the same scope).
1688
1689    Returns either X or an old decl for the same name.
1690    If an old decl is returned, it may have been smashed
1691    to agree with what X says.  */
1692
1693 tree
1694 pushdecl (tree x)
1695 {
1696   tree name = DECL_NAME (x);
1697   struct binding_level *scope = current_binding_level;
1698
1699 #ifdef ENABLE_CHECKING
1700   if (error_mark_node == 0)
1701     /* Called too early.  */
1702     abort ();
1703 #endif
1704
1705   /* Functions need the lang_decl data.  */
1706   if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1707     DECL_LANG_SPECIFIC (x) = (struct lang_decl *)
1708       ggc_alloc_cleared (sizeof (struct lang_decl));
1709
1710   /* A local extern declaration for a function doesn't constitute nesting.
1711      A local auto declaration does, since it's a forward decl
1712      for a nested function coming later.  */
1713   if (current_function_decl == NULL
1714       || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1715           && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1716     DECL_CONTEXT (x) = current_file_decl;
1717   else
1718     DECL_CONTEXT (x) = current_function_decl;
1719   
1720   if (name)
1721     {
1722       tree old;
1723
1724       if (warn_nested_externs
1725           && scope != global_binding_level
1726           && DECL_EXTERNAL (x)
1727           && !DECL_IN_SYSTEM_HEADER (x))
1728         warning ("nested extern declaration of `%s'",
1729                  IDENTIFIER_POINTER (name));
1730
1731       old = lookup_name_current_level (name);
1732       if (old && duplicate_decls (x, old, 0, false))
1733         return old;
1734       if (DECL_EXTERNAL (x) || scope == global_binding_level)
1735         {
1736           /* Find and check against a previous, not-in-scope, external
1737              decl for this identifier.  (C99 s???: If two declarations
1738              with external linkage, referring to the same object, have
1739              incompatible types, the behavior is undefined).  */
1740           tree ext = any_external_decl (name);
1741           if (ext)
1742             {
1743               if (duplicate_decls (x, ext, scope != global_binding_level, 
1744                                    false))
1745                 x = copy_node (ext);
1746             }
1747           else
1748             record_external_decl (x);
1749         }
1750
1751       if (TREE_CODE (x) == TYPE_DECL)
1752         clone_underlying_type (x);
1753
1754       /* If storing a local value, there may already be one
1755          (inherited).  If so, record it for restoration when this
1756          binding level ends.  Take care not to do this if we are
1757          replacing an older decl in the same binding level (i.e.
1758          duplicate_decls returned false, above).  */
1759       if (scope != global_binding_level
1760           && IDENTIFIER_SYMBOL_VALUE (name)
1761           && IDENTIFIER_SYMBOL_VALUE (name) != old)
1762         {
1763           warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1764           scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1765                                        scope->shadowed);
1766         }
1767
1768       /* Install the new declaration in the requested binding level.  */
1769       IDENTIFIER_SYMBOL_VALUE (name) = x;
1770       C_DECL_INVISIBLE (x) = 0;
1771
1772       /* Keep list of variables in this level with incomplete type.
1773          If the input is erroneous, we can have error_mark in the type
1774          slot (e.g. "f(void a, ...)") - that doesn't count as an
1775          incomplete type.  */
1776       if (TREE_TYPE (x) != error_mark_node
1777           && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1778         {
1779           tree element = TREE_TYPE (x);
1780
1781           while (TREE_CODE (element) == ARRAY_TYPE)
1782             element = TREE_TYPE (element);
1783           if (TREE_CODE (element) == RECORD_TYPE
1784               || TREE_CODE (element) == UNION_TYPE)
1785             scope->incomplete_list = tree_cons (NULL_TREE, x,
1786                                                 scope->incomplete_list);
1787         }
1788     }
1789
1790   /* Put decls on list in reverse order.
1791      We will reverse them later if necessary.  */
1792   TREE_CHAIN (x) = scope->names;
1793   scope->names = x;
1794
1795   return x;
1796 }
1797
1798 /* Record X as belonging to the global scope (C99 "file scope").
1799    This is used only internally by the Objective-C front end,
1800    and is limited to its needs.  It will hork if there is _any_
1801    visible binding for X (not just a global one).  */
1802 tree
1803 pushdecl_top_level (tree x)
1804 {
1805   tree name, old;
1806
1807   if (TREE_CODE (x) != VAR_DECL)
1808     abort ();
1809
1810   name = DECL_NAME (x);
1811   old = IDENTIFIER_SYMBOL_VALUE (name);
1812
1813   if (old)
1814     {
1815       if (DECL_CONTEXT (old))
1816         abort ();
1817
1818       if (!duplicate_decls (x, old, 0, false))
1819         abort ();
1820
1821       return old;
1822     }
1823
1824   DECL_CONTEXT (x) = current_file_decl;
1825   IDENTIFIER_SYMBOL_VALUE (name) = x;
1826   TREE_CHAIN (x) = global_binding_level->names;
1827   global_binding_level->names = x;
1828   return x;
1829 }
1830
1831 /* Record X as belonging to the outermost scope of the current
1832    function.  This is used only internally, by c_make_fname_decl and
1833    build_external_ref, and is limited to their needs.  The NAME is
1834    provided as a separate argument because build_external_ref wants to
1835    use error_mark_node for X.  For VAR_DECLs, duplicate_decls is not
1836    called; if there is any preexisting decl for this identifier, it is
1837    an ICE.  */
1838 tree
1839 pushdecl_function_level (tree x, tree name)
1840 {
1841   struct binding_level *scope;
1842
1843   scope = current_binding_level;
1844   while (scope->function_body == 0)
1845     scope = scope->level_chain;
1846   if (!scope)
1847     abort ();
1848
1849   if (x == error_mark_node)
1850     scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1851                                  scope->shadowed);
1852   else if (TREE_CODE (x) == VAR_DECL)
1853     {
1854       if (name != DECL_NAME (x))
1855         abort ();
1856       if (IDENTIFIER_SYMBOL_VALUE (name))
1857         abort ();
1858
1859       DECL_CONTEXT (x) = current_function_decl;
1860       TREE_CHAIN (x) = scope->names;
1861       scope->names = x;
1862     }
1863
1864   IDENTIFIER_SYMBOL_VALUE (name) = x;
1865   return x;
1866 }
1867 \f
1868 /* Generate an implicit declaration for identifier FUNCTIONID as a
1869    function of type int ().  */
1870
1871 tree
1872 implicitly_declare (tree functionid)
1873 {
1874   tree decl = any_external_decl (functionid);
1875
1876   if (decl && decl != error_mark_node)
1877     {
1878       /* Implicit declaration of a function already declared
1879          (somehow) in a different scope, or as a built-in.
1880          If this is the first time this has happened, warn;
1881          then recycle the old declaration.  */
1882       if (!C_DECL_IMPLICIT (decl))
1883         {
1884           implicit_decl_warning (DECL_NAME (decl));
1885           if (! C_DECL_FILE_SCOPE (decl))
1886             warning_with_decl (decl, "previous declaration of `%s'");
1887           C_DECL_IMPLICIT (decl) = 1;
1888         }
1889       /* If this function is global, then it must already be in the
1890          global binding level, so there's no need to push it again.  */
1891       if (current_binding_level == global_binding_level)
1892         return decl;
1893       /* If this is a local declaration, make a copy; we can't have
1894          the same DECL listed in two different binding levels.  */
1895       return pushdecl (copy_node (decl));
1896     }
1897
1898   /* Not seen before.  */
1899   decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1900   DECL_EXTERNAL (decl) = 1;
1901   TREE_PUBLIC (decl) = 1;
1902   C_DECL_IMPLICIT (decl) = 1;
1903   implicit_decl_warning (functionid);
1904
1905   /* ANSI standard says implicit declarations are in the innermost block.
1906      So we record the decl in the standard fashion.  */
1907   decl = pushdecl (decl);
1908
1909   /* No need to call objc_check_decl here - it's a function type.  */
1910   rest_of_decl_compilation (decl, NULL, 0, 0);
1911
1912   /* Write a record describing this implicit function declaration to the
1913      prototypes file (if requested).  */
1914
1915   gen_aux_info_record (decl, 0, 1, 0);
1916
1917   /* Possibly apply some default attributes to this implicit declaration.  */
1918   decl_attributes (&decl, NULL_TREE, 0);
1919
1920   return decl;
1921 }
1922
1923 static void
1924 implicit_decl_warning (tree id)
1925 {
1926   const char *name = IDENTIFIER_POINTER (id);
1927   if (mesg_implicit_function_declaration == 2)
1928     error ("implicit declaration of function `%s'", name);
1929   else if (mesg_implicit_function_declaration == 1)
1930     warning ("implicit declaration of function `%s'", name);
1931 }
1932
1933 /* Return zero if the declaration NEWDECL is valid
1934    when the declaration OLDDECL (assumed to be for the same name)
1935    has already been seen.
1936    Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1937    and 3 if it is a conflicting declaration.  */
1938
1939 static int
1940 redeclaration_error_message (tree newdecl, tree olddecl)
1941 {
1942   if (TREE_CODE (newdecl) == TYPE_DECL)
1943     {
1944       /* Do not complain about type redeclarations where at least one
1945          declaration was in a system header.  */
1946       if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1947         return 0;
1948       return 1;
1949     }
1950   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1951     {
1952       /* Declarations of functions can insist on internal linkage
1953          but they can't be inconsistent with internal linkage,
1954          so there can be no error on that account.
1955          However defining the same name twice is no good.  */
1956       if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1957           /* However, defining once as extern inline and a second
1958              time in another way is ok.  */
1959           && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1960                && ! (DECL_DECLARED_INLINE_P (newdecl)
1961                      && DECL_EXTERNAL (newdecl))))
1962         return 1;
1963       return 0;
1964     }
1965   else if (C_DECL_FILE_SCOPE (newdecl))
1966     {
1967       /* Objects declared at top level:  */
1968       /* If at least one is a reference, it's ok.  */
1969       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1970         return 0;
1971       /* Reject two definitions.  */
1972       if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1973         return 1;
1974       /* Now we have two tentative defs, or one tentative and one real def.  */
1975       /* Insist that the linkage match.  */
1976       if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1977         return 3;
1978       return 0;
1979     }
1980   else if (current_binding_level->parm_flag
1981            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1982     return 0;
1983   else
1984     {
1985       /* Newdecl has block scope.  If olddecl has block scope also, then
1986          reject two definitions, and reject a definition together with an
1987          external reference.  Otherwise, it is OK, because newdecl must
1988          be an extern reference to olddecl.  */
1989       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1990           && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1991         return 2;
1992       return 0;
1993     }
1994 }
1995 \f
1996 /* Get the LABEL_DECL corresponding to identifier ID as a label.
1997    Create one if none exists so far for the current function.
1998    This function is called for both label definitions and label references.  */
1999
2000 tree
2001 lookup_label (tree id)
2002 {
2003   tree decl = IDENTIFIER_LABEL_VALUE (id);
2004
2005   if (current_function_decl == 0)
2006     {
2007       error ("label %s referenced outside of any function",
2008              IDENTIFIER_POINTER (id));
2009       return 0;
2010     }
2011
2012   /* Use a label already defined or ref'd with this name.  */
2013   if (decl != 0)
2014     {
2015       /* But not if it is inherited and wasn't declared to be inheritable.  */
2016       if (DECL_CONTEXT (decl) != current_function_decl
2017           && ! C_DECLARED_LABEL_FLAG (decl))
2018         return shadow_label (id);
2019       return decl;
2020     }
2021
2022   decl = build_decl (LABEL_DECL, id, void_type_node);
2023
2024   /* A label not explicitly declared must be local to where it's ref'd.  */
2025   DECL_CONTEXT (decl) = current_function_decl;
2026
2027   DECL_MODE (decl) = VOIDmode;
2028
2029   /* Say where one reference is to the label,
2030      for the sake of the error if it is not defined.  */
2031   DECL_SOURCE_LOCATION (decl) = input_location;
2032
2033   IDENTIFIER_LABEL_VALUE (id) = decl;
2034
2035   named_labels = tree_cons (NULL_TREE, decl, named_labels);
2036
2037   return decl;
2038 }
2039
2040 /* Make a label named NAME in the current function,
2041    shadowing silently any that may be inherited from containing functions
2042    or containing scopes.
2043
2044    Note that valid use, if the label being shadowed
2045    comes from another scope in the same function,
2046    requires calling declare_nonlocal_label right away.  */
2047
2048 tree
2049 shadow_label (tree name)
2050 {
2051   tree decl = IDENTIFIER_LABEL_VALUE (name);
2052
2053   if (decl != 0)
2054     {
2055       tree dup;
2056
2057       /* Check to make sure that the label hasn't already been declared
2058          at this label scope */
2059       for (dup = named_labels; dup; dup = TREE_CHAIN (dup))
2060         if (TREE_VALUE (dup) == decl)
2061           {
2062             error ("duplicate label declaration `%s'",
2063                    IDENTIFIER_POINTER (name));
2064             error_with_decl (TREE_VALUE (dup),
2065                              "this is a previous declaration");
2066             /* Just use the previous declaration.  */
2067             return lookup_label (name);
2068           }
2069
2070       shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2071       IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2072     }
2073
2074   return lookup_label (name);
2075 }
2076
2077 /* Define a label, specifying the location in the source file.
2078    Return the LABEL_DECL node for the label, if the definition is valid.
2079    Otherwise return 0.  */
2080
2081 tree
2082 define_label (location_t location, tree name)
2083 {
2084   tree decl = lookup_label (name);
2085
2086   /* If label with this name is known from an outer context, shadow it.  */
2087   if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2088     {
2089       shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2090       IDENTIFIER_LABEL_VALUE (name) = 0;
2091       decl = lookup_label (name);
2092     }
2093
2094   if (warn_traditional && !in_system_header && lookup_name (name))
2095     warning ("%Htraditional C lacks a separate namespace for labels, "
2096              "identifier `%s' conflicts", &location, IDENTIFIER_POINTER (name));
2097
2098   if (DECL_INITIAL (decl) != 0)
2099     {
2100       error ("%Hduplicate label `%s'", &location, IDENTIFIER_POINTER (name));
2101       return 0;
2102     }
2103   else
2104     {
2105       /* Mark label as having been defined.  */
2106       DECL_INITIAL (decl) = error_mark_node;
2107       /* Say where in the source.  */
2108       DECL_SOURCE_LOCATION (decl) = location;
2109       return decl;
2110     }
2111 }
2112 \f
2113 /* Return the list of declarations of the current level.
2114    Note that this list is in reverse order unless/until
2115    you nreverse it; and when you do nreverse it, you must
2116    store the result back using `storedecls' or you will lose.  */
2117
2118 tree
2119 getdecls (void)
2120 {
2121   return current_binding_level->names;
2122 }
2123
2124 /* Return the list of type-tags (for structs, etc) of the current level.  */
2125
2126 tree
2127 gettags (void)
2128 {
2129   return current_binding_level->tags;
2130 }
2131
2132 /* Store the list of declarations of the current level.
2133    This is done for the parameter declarations of a function being defined,
2134    after they are modified in the light of any missing parameters.  */
2135
2136 static void
2137 storedecls (tree decls)
2138 {
2139   current_binding_level->names = decls;
2140 }
2141
2142 /* Similarly, store the list of tags of the current level.  */
2143
2144 static void
2145 storetags (tree tags)
2146 {
2147   current_binding_level->tags = tags;
2148 }
2149 \f
2150 /* Given NAME, an IDENTIFIER_NODE,
2151    return the structure (or union or enum) definition for that name.
2152    If THISLEVEL_ONLY is nonzero, searches only the current_binding_level.
2153    CODE says which kind of type the caller wants;
2154    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2155    If the wrong kind of type is found, an error is reported.  */
2156
2157 static tree
2158 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2159 {
2160   tree tag = IDENTIFIER_TAG_VALUE (name);
2161   int thislevel = 0;
2162
2163   if (!tag)
2164     return 0;
2165
2166   /* We only care about whether it's in this level if
2167      thislevel_only was set or it might be a type clash.  */
2168   if (thislevel_only || TREE_CODE (tag) != code)
2169     {
2170       if (current_binding_level == global_binding_level
2171           || purpose_member (name, current_binding_level->tags))
2172         thislevel = 1;
2173     }
2174
2175   if (thislevel_only && !thislevel)
2176     return 0;
2177
2178   if (TREE_CODE (tag) != code)
2179     {
2180       /* Definition isn't the kind we were looking for.  */
2181       pending_invalid_xref = name;
2182       pending_invalid_xref_location = input_location;
2183
2184       /* If in the same binding level as a declaration as a tag
2185          of a different type, this must not be allowed to
2186          shadow that tag, so give the error immediately.
2187          (For example, "struct foo; union foo;" is invalid.)  */
2188       if (thislevel)
2189         pending_xref_error ();
2190     }
2191   return tag;
2192 }
2193
2194 /* Print an error message now
2195    for a recent invalid struct, union or enum cross reference.
2196    We don't print them immediately because they are not invalid
2197    when used in the `struct foo;' construct for shadowing.  */
2198
2199 void
2200 pending_xref_error (void)
2201 {
2202   if (pending_invalid_xref != 0)
2203     error ("%H`%s' defined as wrong kind of tag",
2204            &pending_invalid_xref_location,
2205            IDENTIFIER_POINTER (pending_invalid_xref));
2206   pending_invalid_xref = 0;
2207 }
2208
2209 \f
2210 /* Look up NAME in the current binding level and its superiors
2211    in the namespace of variables, functions and typedefs.
2212    Return a ..._DECL node of some kind representing its definition,
2213    or return 0 if it is undefined.  */
2214
2215 tree
2216 lookup_name (tree name)
2217 {
2218   tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2219   if (decl == 0 || decl == error_mark_node)
2220     return decl;
2221   if (C_DECL_INVISIBLE (decl))
2222     return 0;
2223   return decl;
2224 }
2225
2226 /* Similar to `lookup_name' but look only at the current binding level.  */
2227
2228 static tree
2229 lookup_name_current_level (tree name)
2230 {
2231   tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2232
2233   if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2234     return 0;
2235
2236   if (current_binding_level == global_binding_level)
2237     return decl;
2238
2239   /* Scan the current scope for a decl with name NAME.  */
2240   if (chain_member (decl, current_binding_level->names))
2241     return decl;
2242
2243   return 0;
2244 }
2245 \f
2246 /* Create the predefined scalar types of C,
2247    and some nodes representing standard constants (0, 1, (void *) 0).
2248    Initialize the global binding level.
2249    Make definitions for built-in primitive functions.  */
2250
2251 void
2252 c_init_decl_processing (void)
2253 {
2254   tree endlink;
2255   tree ptr_ftype_void, ptr_ftype_ptr;
2256   location_t save_loc = input_location;
2257   
2258   /* Adds some ggc roots, and reserved words for c-parse.in.  */
2259   c_parse_init ();
2260
2261   current_function_decl = NULL;
2262   named_labels = NULL;
2263   current_binding_level = NULL_BINDING_LEVEL;
2264   free_binding_level = NULL_BINDING_LEVEL;
2265
2266   /* Make the binding_level structure for global names.  */
2267   pushlevel (0);
2268   global_binding_level = current_binding_level;
2269   /* Declarations from c_common_nodes_and_builtins must not be associated
2270      with this input file, lest we get differences between using and not
2271      using preprocessed headers.  */
2272   input_location.file = "<internal>";
2273   input_location.line = 0;
2274
2275   /* Make the DECL for the toplevel file scope.  */
2276   current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2277
2278   build_common_tree_nodes (flag_signed_char);
2279
2280   c_common_nodes_and_builtins ();
2281
2282   boolean_type_node = integer_type_node;
2283   boolean_true_node = integer_one_node;
2284   boolean_false_node = integer_zero_node;
2285
2286   c_bool_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
2287   TREE_SET_CODE (c_bool_type_node, BOOLEAN_TYPE);
2288   TYPE_MAX_VALUE (c_bool_type_node) = build_int_2 (1, 0);
2289   TREE_TYPE (TYPE_MAX_VALUE (c_bool_type_node)) = c_bool_type_node;
2290   TYPE_PRECISION (c_bool_type_node) = 1;
2291   pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2292                         c_bool_type_node));
2293   c_bool_false_node = build_int_2 (0, 0);
2294   TREE_TYPE (c_bool_false_node) = c_bool_type_node;
2295   c_bool_true_node = build_int_2 (1, 0);
2296   TREE_TYPE (c_bool_true_node) = c_bool_type_node;
2297
2298   endlink = void_list_node;
2299   ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2300   ptr_ftype_ptr
2301     = build_function_type (ptr_type_node,
2302                            tree_cons (NULL_TREE, ptr_type_node, endlink));
2303
2304   input_location = save_loc;
2305
2306   pedantic_lvalues = pedantic;
2307
2308   make_fname_decl = c_make_fname_decl;
2309   start_fname_decls ();
2310
2311   builtin_decls = global_binding_level->names;
2312 }
2313
2314 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2315    decl, NAME is the initialization string and TYPE_DEP indicates whether
2316    NAME depended on the type of the function.  As we don't yet implement
2317    delayed emission of static data, we mark the decl as emitted
2318    so it is not placed in the output.  Anything using it must therefore pull
2319    out the STRING_CST initializer directly.  This does mean that these names
2320    are string merging candidates, which is wrong for C99's __func__.  FIXME.  */
2321
2322 static tree
2323 c_make_fname_decl (tree id, int type_dep)
2324 {
2325   const char *name = fname_as_string (type_dep);
2326   tree decl, type, init;
2327   size_t length = strlen (name);
2328
2329   type =  build_array_type
2330           (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2331            build_index_type (size_int (length)));
2332
2333   decl = build_decl (VAR_DECL, id, type);
2334
2335   TREE_STATIC (decl) = 1;
2336   TREE_READONLY (decl) = 1;
2337   DECL_ARTIFICIAL (decl) = 1;
2338
2339   init = build_string (length + 1, name);
2340   TREE_TYPE (init) = type;
2341   DECL_INITIAL (decl) = init;
2342
2343   TREE_USED (decl) = 1;
2344
2345   if (current_function_decl)
2346     pushdecl_function_level (decl, DECL_NAME (decl));
2347
2348   finish_decl (decl, init, NULL_TREE);
2349
2350   return decl;
2351 }
2352
2353 /* Return a definition for a builtin function named NAME and whose data type
2354    is TYPE.  TYPE should be a function type with argument types.
2355    FUNCTION_CODE tells later passes how to compile calls to this function.
2356    See tree.h for its possible values.
2357
2358    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2359    the name to be called if we can't opencode the function.  If
2360    ATTRS is nonzero, use that for the function's attribute list.  */
2361
2362 tree
2363 builtin_function (const char *name, tree type, int function_code,
2364                   enum built_in_class class, const char *library_name,
2365                   tree attrs)
2366 {
2367   tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2368   DECL_EXTERNAL (decl) = 1;
2369   TREE_PUBLIC (decl) = 1;
2370   if (library_name)
2371     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2372   make_decl_rtl (decl, NULL);
2373   pushdecl (decl);
2374   DECL_BUILT_IN_CLASS (decl) = class;
2375   DECL_FUNCTION_CODE (decl) = function_code;
2376
2377   /* Warn if a function in the namespace for users
2378      is used without an occasion to consider it declared.  */
2379   if (name[0] != '_' || name[1] != '_')
2380     C_DECL_INVISIBLE (decl) = 1;
2381
2382   /* Possibly apply some default attributes to this built-in function.  */
2383   if (attrs)
2384     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2385   else
2386     decl_attributes (&decl, NULL_TREE, 0);
2387
2388   return decl;
2389 }
2390
2391 /* Apply default attributes to a function, if a system function with default
2392    attributes.  */
2393
2394 void
2395 c_insert_default_attributes (tree decl)
2396 {
2397   if (!TREE_PUBLIC (decl))
2398     return;
2399   c_common_insert_default_attributes (decl);
2400 }
2401 \f
2402 /* Called when a declaration is seen that contains no names to declare.
2403    If its type is a reference to a structure, union or enum inherited
2404    from a containing scope, shadow that tag name for the current scope
2405    with a forward reference.
2406    If its type defines a new named structure or union
2407    or defines an enum, it is valid but we need not do anything here.
2408    Otherwise, it is an error.  */
2409
2410 void
2411 shadow_tag (tree declspecs)
2412 {
2413   shadow_tag_warned (declspecs, 0);
2414 }
2415
2416 void
2417 shadow_tag_warned (tree declspecs, int warned)
2418
2419
2420      /* 1 => we have done a pedwarn.  2 => we have done a warning, but
2421         no pedwarn.  */
2422 {
2423   int found_tag = 0;
2424   tree link;
2425   tree specs, attrs;
2426
2427   pending_invalid_xref = 0;
2428
2429   /* Remove the attributes from declspecs, since they will confuse the
2430      following code.  */
2431   split_specs_attrs (declspecs, &specs, &attrs);
2432
2433   for (link = specs; link; link = TREE_CHAIN (link))
2434     {
2435       tree value = TREE_VALUE (link);
2436       enum tree_code code = TREE_CODE (value);
2437
2438       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2439         /* Used to test also that TYPE_SIZE (value) != 0.
2440            That caused warning for `struct foo;' at top level in the file.  */
2441         {
2442           tree name = TYPE_NAME (value);
2443           tree t;
2444
2445           found_tag++;
2446
2447           if (name == 0)
2448             {
2449               if (warned != 1 && code != ENUMERAL_TYPE)
2450                 /* Empty unnamed enum OK */
2451                 {
2452                   pedwarn ("unnamed struct/union that defines no instances");
2453                   warned = 1;
2454                 }
2455             }
2456           else
2457             {
2458               t = lookup_tag (code, name, 1);
2459
2460               if (t == 0)
2461                 {
2462                   t = make_node (code);
2463                   pushtag (name, t);
2464                 }
2465             }
2466         }
2467       else
2468         {
2469           if (!warned && ! in_system_header)
2470             {
2471               warning ("useless keyword or type name in empty declaration");
2472               warned = 2;
2473             }
2474         }
2475     }
2476
2477   if (found_tag > 1)
2478     error ("two types specified in one empty declaration");
2479
2480   if (warned != 1)
2481     {
2482       if (found_tag == 0)
2483         pedwarn ("empty declaration");
2484     }
2485 }
2486 \f
2487 /* Construct an array declarator.  EXPR is the expression inside [], or
2488    NULL_TREE.  QUALS are the type qualifiers inside the [] (to be applied
2489    to the pointer to which a parameter array is converted).  STATIC_P is
2490    nonzero if "static" is inside the [], zero otherwise.  VLA_UNSPEC_P
2491    is nonzero is the array is [*], a VLA of unspecified length which is
2492    nevertheless a complete type (not currently implemented by GCC),
2493    zero otherwise.  The declarator is constructed as an ARRAY_REF
2494    (to be decoded by grokdeclarator), whose operand 0 is what's on the
2495    left of the [] (filled by in set_array_declarator_type) and operand 1
2496    is the expression inside; whose TREE_TYPE is the type qualifiers and
2497    which has TREE_STATIC set if "static" is used.  */
2498
2499 tree
2500 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2501 {
2502   tree decl;
2503   decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2504   TREE_TYPE (decl) = quals;
2505   TREE_STATIC (decl) = (static_p ? 1 : 0);
2506   if (pedantic && !flag_isoc99)
2507     {
2508       if (static_p || quals != NULL_TREE)
2509         pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2510       if (vla_unspec_p)
2511         pedwarn ("ISO C90 does not support `[*]' array declarators");
2512     }
2513   if (vla_unspec_p)
2514     warning ("GCC does not yet properly implement `[*]' array declarators");
2515   return decl;
2516 }
2517
2518 /* Set the type of an array declarator.  DECL is the declarator, as
2519    constructed by build_array_declarator; TYPE is what appears on the left
2520    of the [] and goes in operand 0.  ABSTRACT_P is nonzero if it is an
2521    abstract declarator, zero otherwise; this is used to reject static and
2522    type qualifiers in abstract declarators, where they are not in the
2523    C99 grammar.  */
2524
2525 tree
2526 set_array_declarator_type (tree decl, tree type, int abstract_p)
2527 {
2528   TREE_OPERAND (decl, 0) = type;
2529   if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2530     error ("static or type qualifiers in abstract declarator");
2531   return decl;
2532 }
2533 \f
2534 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
2535
2536 tree
2537 groktypename (tree typename)
2538 {
2539   tree specs, attrs;
2540
2541   if (TREE_CODE (typename) != TREE_LIST)
2542     return typename;
2543
2544   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2545
2546   typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2547
2548   /* Apply attributes.  */
2549   decl_attributes (&typename, attrs, 0);
2550
2551   return typename;
2552 }
2553
2554 /* Return a PARM_DECL node for a given pair of specs and declarator.  */
2555
2556 tree
2557 groktypename_in_parm_context (tree typename)
2558 {
2559   if (TREE_CODE (typename) != TREE_LIST)
2560     return typename;
2561   return grokdeclarator (TREE_VALUE (typename),
2562                          TREE_PURPOSE (typename),
2563                          PARM, 0);
2564 }
2565
2566 /* Decode a declarator in an ordinary declaration or data definition.
2567    This is called as soon as the type information and variable name
2568    have been parsed, before parsing the initializer if any.
2569    Here we create the ..._DECL node, fill in its type,
2570    and put it on the list of decls for the current context.
2571    The ..._DECL node is returned as the value.
2572
2573    Exception: for arrays where the length is not specified,
2574    the type is left null, to be filled in by `finish_decl'.
2575
2576    Function definitions do not come here; they go to start_function
2577    instead.  However, external and forward declarations of functions
2578    do go through here.  Structure field declarations are done by
2579    grokfield and not through here.  */
2580
2581 tree
2582 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2583 {
2584   tree decl;
2585   tree tem;
2586
2587   /* An object declared as __attribute__((deprecated)) suppresses
2588      warnings of uses of other deprecated items.  */
2589   if (lookup_attribute ("deprecated", attributes))
2590     deprecated_state = DEPRECATED_SUPPRESS;
2591
2592   decl = grokdeclarator (declarator, declspecs,
2593                          NORMAL, initialized);
2594
2595   deprecated_state = DEPRECATED_NORMAL;
2596
2597   if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2598       && MAIN_NAME_P (DECL_NAME (decl)))
2599     warning_with_decl (decl, "`%s' is usually a function");
2600
2601   if (initialized)
2602     /* Is it valid for this decl to have an initializer at all?
2603        If not, set INITIALIZED to zero, which will indirectly
2604        tell `finish_decl' to ignore the initializer once it is parsed.  */
2605     switch (TREE_CODE (decl))
2606       {
2607       case TYPE_DECL:
2608         error ("typedef `%s' is initialized (use __typeof__ instead)",
2609                IDENTIFIER_POINTER (DECL_NAME (decl)));
2610         initialized = 0;
2611         break;
2612
2613       case FUNCTION_DECL:
2614         error ("function `%s' is initialized like a variable",
2615                IDENTIFIER_POINTER (DECL_NAME (decl)));
2616         initialized = 0;
2617         break;
2618
2619       case PARM_DECL:
2620         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
2621         error ("parameter `%s' is initialized",
2622                IDENTIFIER_POINTER (DECL_NAME (decl)));
2623         initialized = 0;
2624         break;
2625
2626       default:
2627         /* Don't allow initializations for incomplete types
2628            except for arrays which might be completed by the initialization.  */
2629
2630         /* This can happen if the array size is an undefined macro.  We already
2631            gave a warning, so we don't need another one.  */
2632         if (TREE_TYPE (decl) == error_mark_node)
2633           initialized = 0;
2634         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2635           {
2636             /* A complete type is ok if size is fixed.  */
2637
2638             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2639                 || C_DECL_VARIABLE_SIZE (decl))
2640               {
2641                 error ("variable-sized object may not be initialized");
2642                 initialized = 0;
2643               }
2644           }
2645         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2646           {
2647             error ("variable `%s' has initializer but incomplete type",
2648                    IDENTIFIER_POINTER (DECL_NAME (decl)));
2649             initialized = 0;
2650           }
2651         else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2652           {
2653             error ("elements of array `%s' have incomplete type",
2654                    IDENTIFIER_POINTER (DECL_NAME (decl)));
2655             initialized = 0;
2656           }
2657       }
2658
2659   if (initialized)
2660     {
2661       DECL_EXTERNAL (decl) = 0;
2662       if (current_binding_level == global_binding_level)
2663         TREE_STATIC (decl) = 1;
2664
2665       /* Tell `pushdecl' this is an initialized decl
2666          even though we don't yet have the initializer expression.
2667          Also tell `finish_decl' it may store the real initializer.  */
2668       DECL_INITIAL (decl) = error_mark_node;
2669     }
2670
2671   /* If this is a function declaration, write a record describing it to the
2672      prototypes file (if requested).  */
2673
2674   if (TREE_CODE (decl) == FUNCTION_DECL)
2675     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2676
2677   /* ANSI specifies that a tentative definition which is not merged with
2678      a non-tentative definition behaves exactly like a definition with an
2679      initializer equal to zero.  (Section 3.7.2)
2680
2681      -fno-common gives strict ANSI behavior, though this tends to break
2682      a large body of code that grew up without this rule.
2683
2684      Thread-local variables are never common, since there's no entrenched
2685      body of code to break, and it allows more efficient variable references
2686      in the presence of dynamic linking.  */
2687
2688   if (TREE_CODE (decl) == VAR_DECL
2689       && !initialized
2690       && TREE_PUBLIC (decl)
2691       && !DECL_THREAD_LOCAL (decl)
2692       && !flag_no_common)
2693     DECL_COMMON (decl) = 1;
2694
2695   /* Set attributes here so if duplicate decl, will have proper attributes.  */
2696   decl_attributes (&decl, attributes, 0);
2697
2698   /* If #pragma weak was used, mark the decl weak now.  */
2699   if (current_binding_level == global_binding_level)
2700     maybe_apply_pragma_weak (decl);
2701
2702   if (TREE_CODE (decl) == FUNCTION_DECL
2703       && DECL_DECLARED_INLINE_P (decl)
2704       && DECL_UNINLINABLE (decl)
2705       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2706     warning_with_decl (decl,
2707                        "inline function `%s' given attribute noinline");
2708
2709   /* Add this decl to the current binding level.
2710      TEM may equal DECL or it may be a previous decl of the same name.  */
2711   tem = pushdecl (decl);
2712
2713   /* For a local variable, define the RTL now.  */
2714   if (current_binding_level != global_binding_level
2715       /* But not if this is a duplicate decl
2716          and we preserved the rtl from the previous one
2717          (which may or may not happen).  */
2718       && !DECL_RTL_SET_P (tem)
2719       && C_DECL_FILE_SCOPE (tem))
2720     {
2721       if (TREE_TYPE (tem) != error_mark_node
2722           && COMPLETE_TYPE_P (TREE_TYPE (tem)))
2723         expand_decl (tem);
2724       else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2725                && DECL_INITIAL (tem) != 0)
2726         expand_decl (tem);
2727     }
2728
2729   return tem;
2730 }
2731
2732 /* Finish processing of a declaration;
2733    install its initial value.
2734    If the length of an array type is not known before,
2735    it must be determined now, from the initial value, or it is an error.  */
2736
2737 void
2738 finish_decl (tree decl, tree init, tree asmspec_tree)
2739 {
2740   tree type = TREE_TYPE (decl);
2741   int was_incomplete = (DECL_SIZE (decl) == 0);
2742   const char *asmspec = 0;
2743
2744   /* If a name was specified, get the string.  */
2745   if (current_binding_level == global_binding_level)
2746     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2747   if (asmspec_tree)
2748     asmspec = TREE_STRING_POINTER (asmspec_tree);
2749
2750   /* If `start_decl' didn't like having an initialization, ignore it now.  */
2751   if (init != 0 && DECL_INITIAL (decl) == 0)
2752     init = 0;
2753
2754   /* Don't crash if parm is initialized.  */
2755   if (TREE_CODE (decl) == PARM_DECL)
2756     init = 0;
2757
2758   if (init)
2759     store_init_value (decl, init);
2760
2761   /* Deduce size of array from initialization, if not already known */
2762   if (TREE_CODE (type) == ARRAY_TYPE
2763       && TYPE_DOMAIN (type) == 0
2764       && TREE_CODE (decl) != TYPE_DECL)
2765     {
2766       int do_default
2767         = (TREE_STATIC (decl)
2768            /* Even if pedantic, an external linkage array
2769               may have incomplete type at first.  */
2770            ? pedantic && !TREE_PUBLIC (decl)
2771            : !DECL_EXTERNAL (decl));
2772       int failure
2773         = complete_array_type (type, DECL_INITIAL (decl), do_default);
2774
2775       /* Get the completed type made by complete_array_type.  */
2776       type = TREE_TYPE (decl);
2777
2778       if (failure == 1)
2779         error_with_decl (decl, "initializer fails to determine size of `%s'");
2780
2781       else if (failure == 2)
2782         {
2783           if (do_default)
2784             error_with_decl (decl, "array size missing in `%s'");
2785           /* If a `static' var's size isn't known,
2786              make it extern as well as static, so it does not get
2787              allocated.
2788              If it is not `static', then do not mark extern;
2789              finish_incomplete_decl will give it a default size
2790              and it will get allocated.  */
2791           else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2792             DECL_EXTERNAL (decl) = 1;
2793         }
2794
2795       /* TYPE_MAX_VALUE is always one less than the number of elements
2796          in the array, because we start counting at zero.  Therefore,
2797          warn only if the value is less than zero.  */
2798       else if (pedantic && TYPE_DOMAIN (type) != 0
2799               && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2800         error_with_decl (decl, "zero or negative size array `%s'");
2801
2802       layout_decl (decl, 0);
2803     }
2804
2805   if (TREE_CODE (decl) == VAR_DECL)
2806     {
2807       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2808           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2809         layout_decl (decl, 0);
2810
2811       if (DECL_SIZE (decl) == 0
2812           /* Don't give an error if we already gave one earlier.  */
2813           && TREE_TYPE (decl) != error_mark_node
2814           && (TREE_STATIC (decl)
2815               ?
2816                 /* A static variable with an incomplete type
2817                    is an error if it is initialized.
2818                    Also if it is not file scope.
2819                    Otherwise, let it through, but if it is not `extern'
2820                    then it may cause an error message later.  */
2821                 (DECL_INITIAL (decl) != 0
2822                  || !C_DECL_FILE_SCOPE (decl))
2823               :
2824                 /* An automatic variable with an incomplete type
2825                    is an error.  */
2826                 !DECL_EXTERNAL (decl)))
2827         {
2828           error_with_decl (decl, "storage size of `%s' isn't known");
2829           TREE_TYPE (decl) = error_mark_node;
2830         }
2831
2832       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2833           && DECL_SIZE (decl) != 0)
2834         {
2835           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2836             constant_expression_warning (DECL_SIZE (decl));
2837           else
2838             error_with_decl (decl, "storage size of `%s' isn't constant");
2839         }
2840
2841       if (TREE_USED (type))
2842         TREE_USED (decl) = 1;
2843     }
2844
2845   /* If this is a function and an assembler name is specified, reset DECL_RTL
2846      so we can give it its new name.  Also, update built_in_decls if it
2847      was a normal built-in.  */
2848   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2849     {
2850       /* ASMSPEC is given, and not the name of a register.  Mark the
2851       name with a star so assemble_name won't munge it.  */
2852       char *starred = alloca (strlen (asmspec) + 2);
2853       starred[0] = '*';
2854       strcpy (starred + 1, asmspec);
2855
2856       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2857         {
2858           tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2859           SET_DECL_RTL (builtin, NULL_RTX);
2860           SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2861 #ifdef TARGET_MEM_FUNCTIONS
2862           if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2863             init_block_move_fn (starred);
2864           else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2865             init_block_clear_fn (starred);
2866 #else
2867           if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2868             init_block_move_fn (starred);
2869           else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2870             init_block_clear_fn (starred);
2871 #endif
2872         }
2873       SET_DECL_RTL (decl, NULL_RTX);
2874       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2875     }
2876
2877   /* Output the assembler code and/or RTL code for variables and functions,
2878      unless the type is an undefined structure or union.
2879      If not, it will get done when the type is completed.  */
2880
2881   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2882     {
2883       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
2884       if (c_dialect_objc ())
2885         objc_check_decl (decl);
2886
2887       if (C_DECL_FILE_SCOPE (decl))
2888         {
2889           if (DECL_INITIAL (decl) == NULL_TREE
2890               || DECL_INITIAL (decl) == error_mark_node)
2891             /* Don't output anything
2892                when a tentative file-scope definition is seen.
2893                But at end of compilation, do output code for them.  */
2894             DECL_DEFER_OUTPUT (decl) = 1;
2895           rest_of_decl_compilation (decl, asmspec, true, 0);
2896         }
2897       else
2898         {
2899           /* This is a local variable.  If there is an ASMSPEC, the
2900              user has requested that we handle it specially.  */
2901           if (asmspec)
2902             {
2903               /* In conjunction with an ASMSPEC, the `register'
2904                  keyword indicates that we should place the variable
2905                  in a particular register.  */
2906               if (DECL_REGISTER (decl))
2907                 DECL_C_HARD_REGISTER (decl) = 1;
2908
2909               /* If this is not a static variable, issue a warning.
2910                  It doesn't make any sense to give an ASMSPEC for an
2911                  ordinary, non-register local variable.  Historically,
2912                  GCC has accepted -- but ignored -- the ASMSPEC in
2913                  this case.  */
2914               if (TREE_CODE (decl) == VAR_DECL
2915                   && !DECL_REGISTER (decl)
2916                   && !TREE_STATIC (decl))
2917                 warning_with_decl (decl,
2918                                    "ignoring asm-specifier for non-static local variable `%s'");
2919               else
2920                 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2921             }
2922
2923           if (TREE_CODE (decl) != FUNCTION_DECL)
2924             add_decl_stmt (decl);
2925         }
2926
2927       if (!C_DECL_FILE_SCOPE (decl))
2928         {
2929           /* Recompute the RTL of a local array now
2930              if it used to be an incomplete type.  */
2931           if (was_incomplete
2932               && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2933             {
2934               /* If we used it already as memory, it must stay in memory.  */
2935               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2936               /* If it's still incomplete now, no init will save it.  */
2937               if (DECL_SIZE (decl) == 0)
2938                 DECL_INITIAL (decl) = 0;
2939             }
2940         }
2941     }
2942
2943   /* If this was marked 'used', be sure it will be output.  */
2944   if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2945     mark_referenced (DECL_ASSEMBLER_NAME (decl));
2946
2947   if (TREE_CODE (decl) == TYPE_DECL)
2948     {
2949       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
2950       if (c_dialect_objc ())
2951         objc_check_decl (decl);
2952       rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2953     }
2954
2955   /* At the end of a declaration, throw away any variable type sizes
2956      of types defined inside that declaration.  There is no use
2957      computing them in the following function definition.  */
2958   if (current_binding_level == global_binding_level)
2959     get_pending_sizes ();
2960
2961   /* Install a cleanup (aka destructor) if one was given.  */
2962   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2963     {
2964       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2965       if (attr)
2966         {
2967           static bool eh_initialized_p;
2968
2969           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2970           tree cleanup_decl = lookup_name (cleanup_id);
2971           tree cleanup;
2972
2973           /* Build "cleanup(&decl)" for the destructor.  */
2974           cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2975           cleanup = build_tree_list (NULL_TREE, cleanup);
2976           cleanup = build_function_call (cleanup_decl, cleanup);
2977
2978           /* Don't warn about decl unused; the cleanup uses it.  */
2979           TREE_USED (decl) = 1;
2980
2981           /* Initialize EH, if we've been told to do so.  */
2982           if (flag_exceptions && !eh_initialized_p)
2983             {
2984               eh_initialized_p = true;
2985               eh_personality_libfunc
2986                 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2987                                     ? "__gcc_personality_sj0"
2988                                     : "__gcc_personality_v0");
2989               using_eh_for_cleanups ();
2990             }
2991
2992           add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2993         }
2994     }
2995 }
2996
2997 /* Given a parsed parameter declaration,
2998    decode it into a PARM_DECL and push that on the current binding level.
2999    Also, for the sake of forward parm decls,
3000    record the given order of parms in `parm_order'.  */
3001
3002 void
3003 push_parm_decl (tree parm)
3004 {
3005   tree decl;
3006   int old_immediate_size_expand = immediate_size_expand;
3007   /* Don't try computing parm sizes now -- wait till fn is called.  */
3008   immediate_size_expand = 0;
3009
3010   decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3011                          TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3012   decl_attributes (&decl, TREE_VALUE (parm), 0);
3013
3014   decl = pushdecl (decl);
3015
3016   immediate_size_expand = old_immediate_size_expand;
3017
3018   current_binding_level->parm_order
3019     = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3020
3021   /* Add this decl to the current binding level.  */
3022   finish_decl (decl, NULL_TREE, NULL_TREE);
3023 }
3024
3025 /* Clear the given order of parms in `parm_order'.
3026    Used at start of parm list,
3027    and also at semicolon terminating forward decls.  */
3028
3029 void
3030 clear_parm_order (void)
3031 {
3032   current_binding_level->parm_order = NULL_TREE;
3033 }
3034 \f
3035 static GTY(()) int compound_literal_number;
3036
3037 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3038    literal, which may be an incomplete array type completed by the
3039    initializer; INIT is a CONSTRUCTOR that initializes the compound
3040    literal.  */
3041
3042 tree
3043 build_compound_literal (tree type, tree init)
3044 {
3045   /* We do not use start_decl here because we have a type, not a declarator;
3046      and do not use finish_decl because the decl should be stored inside
3047      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT.  */
3048   tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3049   tree complit;
3050   tree stmt;
3051   DECL_EXTERNAL (decl) = 0;
3052   TREE_PUBLIC (decl) = 0;
3053   TREE_STATIC (decl) = (current_binding_level == global_binding_level);
3054   DECL_CONTEXT (decl) = current_function_decl;
3055   TREE_USED (decl) = 1;
3056   TREE_TYPE (decl) = type;
3057   TREE_READONLY (decl) = TREE_READONLY (type);
3058   store_init_value (decl, init);
3059
3060   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3061     {
3062       int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3063       if (failure)
3064         abort ();
3065     }
3066
3067   type = TREE_TYPE (decl);
3068   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3069     return error_mark_node;
3070
3071   stmt = build_stmt (DECL_STMT, decl);
3072   complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3073   TREE_SIDE_EFFECTS (complit) = 1;
3074
3075   layout_decl (decl, 0);
3076
3077   if (TREE_STATIC (decl))
3078     {
3079       /* This decl needs a name for the assembler output.  We also need
3080          a unique suffix to be added to the name.  */
3081       char *name;
3082
3083       ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3084                                compound_literal_number);
3085       compound_literal_number++;
3086       DECL_NAME (decl) = get_identifier (name);
3087       DECL_DEFER_OUTPUT (decl) = 1;
3088       DECL_COMDAT (decl) = 1;
3089       DECL_ARTIFICIAL (decl) = 1;
3090       pushdecl (decl);
3091       rest_of_decl_compilation (decl, NULL, 1, 0);
3092     }
3093
3094   return complit;
3095 }
3096 \f
3097 /* Make TYPE a complete type based on INITIAL_VALUE.
3098    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3099    2 if there was no information (in which case assume 1 if DO_DEFAULT).  */
3100
3101 int
3102 complete_array_type (tree type, tree initial_value, int do_default)
3103 {
3104   tree maxindex = NULL_TREE;
3105   int value = 0;
3106
3107   if (initial_value)
3108     {
3109       /* Note MAXINDEX  is really the maximum index,
3110          one less than the size.  */
3111       if (TREE_CODE (initial_value) == STRING_CST)
3112         {
3113           int eltsize
3114             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3115           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3116                                    / eltsize) - 1, 0);
3117         }
3118       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3119         {
3120           tree elts = CONSTRUCTOR_ELTS (initial_value);
3121           maxindex = build_int_2 (-1, -1);
3122           for (; elts; elts = TREE_CHAIN (elts))
3123             {
3124               if (TREE_PURPOSE (elts))
3125                 maxindex = TREE_PURPOSE (elts);
3126               else
3127                 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3128                                         maxindex, integer_one_node));
3129             }
3130           maxindex = copy_node (maxindex);
3131         }
3132       else
3133         {
3134           /* Make an error message unless that happened already.  */
3135           if (initial_value != error_mark_node)
3136             value = 1;
3137
3138           /* Prevent further error messages.  */
3139           maxindex = build_int_2 (0, 0);
3140         }
3141     }
3142
3143   if (!maxindex)
3144     {
3145       if (do_default)
3146         maxindex = build_int_2 (0, 0);
3147       value = 2;
3148     }
3149
3150   if (maxindex)
3151     {
3152       TYPE_DOMAIN (type) = build_index_type (maxindex);
3153       if (!TREE_TYPE (maxindex))
3154         TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3155     }
3156
3157   /* Lay out the type now that we can get the real answer.  */
3158
3159   layout_type (type);
3160
3161   return value;
3162 }
3163 \f
3164 /* Determine whether TYPE is a structure with a flexible array member,
3165    or a union containing such a structure (possibly recursively).  */
3166
3167 static bool
3168 flexible_array_type_p (tree type)
3169 {
3170   tree x;
3171   switch (TREE_CODE (type))
3172     {
3173     case RECORD_TYPE:
3174       x = TYPE_FIELDS (type);
3175       if (x == NULL_TREE)
3176         return false;
3177       while (TREE_CHAIN (x) != NULL_TREE)
3178         x = TREE_CHAIN (x);
3179       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3180           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3181           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3182           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3183         return true;
3184       return false;
3185     case UNION_TYPE:
3186       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3187         {
3188           if (flexible_array_type_p (TREE_TYPE (x)))
3189             return true;
3190         }
3191       return false;
3192     default:
3193     return false;
3194   }
3195 }
3196 \f
3197 /* Given declspecs and a declarator,
3198    determine the name and type of the object declared
3199    and construct a ..._DECL node for it.
3200    (In one case we can return a ..._TYPE node instead.
3201     For invalid input we sometimes return 0.)
3202
3203    DECLSPECS is a chain of tree_list nodes whose value fields
3204     are the storage classes and type specifiers.
3205
3206    DECL_CONTEXT says which syntactic context this declaration is in:
3207      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3208      FUNCDEF for a function definition.  Like NORMAL but a few different
3209       error messages in each case.  Return value may be zero meaning
3210       this definition is too screwy to try to parse.
3211      PARM for a parameter declaration (either within a function prototype
3212       or before a function body).  Make a PARM_DECL, or return void_type_node.
3213      TYPENAME if for a typename (in a cast or sizeof).
3214       Don't make a DECL node; just return the ..._TYPE node.
3215      FIELD for a struct or union field; make a FIELD_DECL.
3216      BITFIELD for a field with specified width.
3217    INITIALIZED is 1 if the decl has an initializer.
3218
3219    In the TYPENAME case, DECLARATOR is really an absolute declarator.
3220    It may also be so in the PARM case, for a prototype where the
3221    argument type is specified but not the name.
3222
3223    This function is where the complicated C meanings of `static'
3224    and `extern' are interpreted.  */
3225
3226 static tree
3227 grokdeclarator (tree declarator, tree declspecs,
3228                 enum decl_context decl_context, int initialized)
3229 {
3230   int specbits = 0;
3231   tree spec;
3232   tree type = NULL_TREE;
3233   int longlong = 0;
3234   int constp;
3235   int restrictp;
3236   int volatilep;
3237   int type_quals = TYPE_UNQUALIFIED;
3238   int inlinep;
3239   int explicit_int = 0;
3240   int explicit_char = 0;
3241   int defaulted_int = 0;
3242   tree typedef_decl = 0;
3243   const char *name;
3244   tree typedef_type = 0;
3245   int funcdef_flag = 0;
3246   enum tree_code innermost_code = ERROR_MARK;
3247   int bitfield = 0;
3248   int size_varies = 0;
3249   tree decl_attr = NULL_TREE;
3250   tree array_ptr_quals = NULL_TREE;
3251   int array_parm_static = 0;
3252   tree returned_attrs = NULL_TREE;
3253
3254   if (decl_context == BITFIELD)
3255     bitfield = 1, decl_context = FIELD;
3256
3257   if (decl_context == FUNCDEF)
3258     funcdef_flag = 1, decl_context = NORMAL;
3259
3260   /* Look inside a declarator for the name being declared
3261      and get it as a string, for an error message.  */
3262   {
3263     tree decl = declarator;
3264     name = 0;
3265
3266     while (decl)
3267       switch (TREE_CODE (decl))
3268         {
3269         case ARRAY_REF:
3270         case INDIRECT_REF:
3271         case CALL_EXPR:
3272           innermost_code = TREE_CODE (decl);
3273           decl = TREE_OPERAND (decl, 0);
3274           break;
3275
3276         case TREE_LIST:
3277           decl = TREE_VALUE (decl);
3278           break;
3279
3280         case IDENTIFIER_NODE:
3281           name = IDENTIFIER_POINTER (decl);
3282           decl = 0;
3283           break;
3284
3285         default:
3286           abort ();
3287         }
3288     if (name == 0)
3289       name = "type name";
3290   }
3291
3292   /* A function definition's declarator must have the form of
3293      a function declarator.  */
3294
3295   if (funcdef_flag && innermost_code != CALL_EXPR)
3296     return 0;
3297
3298   /* Anything declared one level down from the top level
3299      must be one of the parameters of a function
3300      (because the body is at least two levels down).  */
3301
3302   /* If this looks like a function definition, make it one,
3303      even if it occurs where parms are expected.
3304      Then store_parm_decls will reject it and not use it as a parm.  */
3305   if (decl_context == NORMAL && !funcdef_flag
3306       && current_binding_level->parm_flag)
3307     decl_context = PARM;
3308
3309   /* Look through the decl specs and record which ones appear.
3310      Some typespecs are defined as built-in typenames.
3311      Others, the ones that are modifiers of other types,
3312      are represented by bits in SPECBITS: set the bits for
3313      the modifiers that appear.  Storage class keywords are also in SPECBITS.
3314
3315      If there is a typedef name or a type, store the type in TYPE.
3316      This includes builtin typedefs such as `int'.
3317
3318      Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3319      and did not come from a user typedef.
3320
3321      Set LONGLONG if `long' is mentioned twice.  */
3322
3323   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3324     {
3325       tree id = TREE_VALUE (spec);
3326
3327       /* If the entire declaration is itself tagged as deprecated then
3328          suppress reports of deprecated items.  */
3329       if (id && TREE_DEPRECATED (id))
3330         {
3331           if (deprecated_state != DEPRECATED_SUPPRESS)
3332             warn_deprecated_use (id);
3333         }
3334
3335       if (id == ridpointers[(int) RID_INT])
3336         explicit_int = 1;
3337       if (id == ridpointers[(int) RID_CHAR])
3338         explicit_char = 1;
3339
3340       if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3341         {
3342           enum rid i = C_RID_CODE (id);
3343           if ((int) i <= (int) RID_LAST_MODIFIER)
3344             {
3345               if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3346                 {
3347                   if (longlong)
3348                     error ("`long long long' is too long for GCC");
3349                   else
3350                     {
3351                       if (pedantic && !flag_isoc99 && ! in_system_header
3352                           && warn_long_long)
3353                         pedwarn ("ISO C90 does not support `long long'");
3354                       longlong = 1;
3355                     }
3356                 }
3357               else if (specbits & (1 << (int) i))
3358                 {
3359                   if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3360                     {
3361                       if (!flag_isoc99)
3362                         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3363                     }
3364                   else
3365                     error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3366                 }
3367
3368               /* Diagnose "__thread extern".  Recall that this list
3369                  is in the reverse order seen in the text.  */
3370               if (i == RID_THREAD
3371                   && (specbits & (1 << (int) RID_EXTERN
3372                                   | 1 << (int) RID_STATIC)))
3373                 {
3374                   if (specbits & 1 << (int) RID_EXTERN)
3375                     error ("`__thread' before `extern'");
3376                   else
3377                     error ("`__thread' before `static'");
3378                 }
3379
3380               specbits |= 1 << (int) i;
3381               goto found;
3382             }
3383         }
3384       if (type)
3385         error ("two or more data types in declaration of `%s'", name);
3386       /* Actual typedefs come to us as TYPE_DECL nodes.  */
3387       else if (TREE_CODE (id) == TYPE_DECL)
3388         {
3389           if (TREE_TYPE (id) == error_mark_node)
3390             ; /* Allow the type to default to int to avoid cascading errors.  */
3391           else
3392             {
3393               type = TREE_TYPE (id);
3394               decl_attr = DECL_ATTRIBUTES (id);
3395               typedef_decl = id;
3396             }
3397         }
3398       /* Built-in types come as identifiers.  */
3399       else if (TREE_CODE (id) == IDENTIFIER_NODE)
3400         {
3401           tree t = lookup_name (id);
3402           if (TREE_TYPE (t) == error_mark_node)
3403             ;
3404           else if (!t || TREE_CODE (t) != TYPE_DECL)
3405             error ("`%s' fails to be a typedef or built in type",
3406                    IDENTIFIER_POINTER (id));
3407           else
3408             {
3409               type = TREE_TYPE (t);
3410               typedef_decl = t;
3411             }
3412         }
3413       else if (TREE_CODE (id) != ERROR_MARK)
3414         type = id;
3415
3416     found:
3417       ;
3418     }
3419
3420   typedef_type = type;
3421   if (type)
3422     size_varies = C_TYPE_VARIABLE_SIZE (type);
3423
3424   /* No type at all: default to `int', and set DEFAULTED_INT
3425      because it was not a user-defined typedef.  */
3426
3427   if (type == 0)
3428     {
3429       if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3430                           | (1 << (int) RID_SIGNED)
3431                           | (1 << (int) RID_UNSIGNED)
3432                           | (1 << (int) RID_COMPLEX))))
3433           /* Don't warn about typedef foo = bar.  */
3434           && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3435           && ! in_system_header)
3436         {
3437           /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3438              and this is a function, or if -Wimplicit; prefer the former
3439              warning since it is more explicit.  */
3440           if ((warn_implicit_int || warn_return_type || flag_isoc99)
3441               && funcdef_flag)
3442             warn_about_return_type = 1;
3443           else if (warn_implicit_int || flag_isoc99)
3444             pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3445                          name);
3446         }
3447
3448       defaulted_int = 1;
3449       type = integer_type_node;
3450     }
3451
3452   /* Now process the modifiers that were specified
3453      and check for invalid combinations.  */
3454
3455   /* Long double is a special combination.  */
3456
3457   if ((specbits & 1 << (int) RID_LONG) && ! longlong
3458       && TYPE_MAIN_VARIANT (type) == double_type_node)
3459     {
3460       specbits &= ~(1 << (int) RID_LONG);
3461       type = long_double_type_node;
3462     }
3463
3464   /* Check all other uses of type modifiers.  */
3465
3466   if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3467                   | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3468     {
3469       int ok = 0;
3470
3471       if ((specbits & 1 << (int) RID_LONG)
3472           && (specbits & 1 << (int) RID_SHORT))
3473         error ("both long and short specified for `%s'", name);
3474       else if (((specbits & 1 << (int) RID_LONG)
3475                 || (specbits & 1 << (int) RID_SHORT))
3476                && explicit_char)
3477         error ("long or short specified with char for `%s'", name);
3478       else if (((specbits & 1 << (int) RID_LONG)
3479                 || (specbits & 1 << (int) RID_SHORT))
3480                && TREE_CODE (type) == REAL_TYPE)
3481         {
3482           static int already = 0;
3483
3484           error ("long or short specified with floating type for `%s'", name);
3485           if (! already && ! pedantic)
3486             {
3487               error ("the only valid combination is `long double'");
3488               already = 1;
3489             }
3490         }
3491       else if ((specbits & 1 << (int) RID_SIGNED)
3492                && (specbits & 1 << (int) RID_UNSIGNED))
3493         error ("both signed and unsigned specified for `%s'", name);
3494       else if (TREE_CODE (type) != INTEGER_TYPE)
3495         error ("long, short, signed or unsigned invalid for `%s'", name);
3496       else
3497         {
3498           ok = 1;
3499           if (!explicit_int && !defaulted_int && !explicit_char)
3500             {
3501               error ("long, short, signed or unsigned used invalidly for `%s'",
3502                      name);
3503               ok = 0;
3504             }
3505         }
3506
3507       /* Discard the type modifiers if they are invalid.  */
3508       if (! ok)
3509         {
3510           specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3511                         | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3512           longlong = 0;
3513         }
3514     }
3515
3516   if ((specbits & (1 << (int) RID_COMPLEX))
3517       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3518     {
3519       error ("complex invalid for `%s'", name);
3520       specbits &= ~(1 << (int) RID_COMPLEX);
3521     }
3522
3523   /* Decide whether an integer type is signed or not.
3524      Optionally treat bitfields as signed by default.  */
3525   if (specbits & 1 << (int) RID_UNSIGNED
3526       || (bitfield && ! flag_signed_bitfields
3527           && (explicit_int || defaulted_int || explicit_char
3528               /* A typedef for plain `int' without `signed'
3529                  can be controlled just like plain `int'.  */
3530               || ! (typedef_decl != 0
3531                     && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3532           && TREE_CODE (type) != ENUMERAL_TYPE
3533           && !(specbits & 1 << (int) RID_SIGNED)))
3534     {
3535       if (longlong)
3536         type = long_long_unsigned_type_node;
3537       else if (specbits & 1 << (int) RID_LONG)
3538         type = long_unsigned_type_node;
3539       else if (specbits & 1 << (int) RID_SHORT)
3540         type = short_unsigned_type_node;
3541       else if (type == char_type_node)
3542         type = unsigned_char_type_node;
3543       else if (typedef_decl)
3544         type = c_common_unsigned_type (type);
3545       else
3546         type = unsigned_type_node;
3547     }
3548   else if ((specbits & 1 << (int) RID_SIGNED)
3549            && type == char_type_node)
3550     type = signed_char_type_node;
3551   else if (longlong)
3552     type = long_long_integer_type_node;
3553   else if (specbits & 1 << (int) RID_LONG)
3554     type = long_integer_type_node;
3555   else if (specbits & 1 << (int) RID_SHORT)
3556     type = short_integer_type_node;
3557
3558   if (specbits & 1 << (int) RID_COMPLEX)
3559     {
3560       if (pedantic && !flag_isoc99)
3561         pedwarn ("ISO C90 does not support complex types");
3562       /* If we just have "complex", it is equivalent to
3563          "complex double", but if any modifiers at all are specified it is
3564          the complex form of TYPE.  E.g, "complex short" is
3565          "complex short int".  */
3566
3567       if (defaulted_int && ! longlong
3568           && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3569                             | (1 << (int) RID_SIGNED)
3570                             | (1 << (int) RID_UNSIGNED))))
3571         {
3572           if (pedantic)
3573             pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3574           type = complex_double_type_node;
3575         }
3576       else if (type == integer_type_node)
3577         {
3578           if (pedantic)
3579             pedwarn ("ISO C does not support complex integer types");
3580           type = complex_integer_type_node;
3581         }
3582       else if (type == float_type_node)
3583         type = complex_float_type_node;
3584       else if (type == double_type_node)
3585         type = complex_double_type_node;
3586       else if (type == long_double_type_node)
3587         type = complex_long_double_type_node;
3588       else
3589         {
3590           if (pedantic)
3591             pedwarn ("ISO C does not support complex integer types");
3592           type = build_complex_type (type);
3593         }
3594     }
3595
3596   /* Figure out the type qualifiers for the declaration.  There are
3597      two ways a declaration can become qualified.  One is something
3598      like `const int i' where the `const' is explicit.  Another is
3599      something like `typedef const int CI; CI i' where the type of the
3600      declaration contains the `const'.  */
3601   constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3602   restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3603   volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3604   inlinep = !! (specbits & (1 << (int) RID_INLINE));
3605   if (constp > 1 && ! flag_isoc99)
3606     pedwarn ("duplicate `const'");
3607   if (restrictp > 1 && ! flag_isoc99)
3608     pedwarn ("duplicate `restrict'");
3609   if (volatilep > 1 && ! flag_isoc99)
3610     pedwarn ("duplicate `volatile'");
3611   if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3612     type = TYPE_MAIN_VARIANT (type);
3613   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3614                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3615                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3616
3617   /* Warn if two storage classes are given. Default to `auto'.  */
3618
3619   {
3620     int nclasses = 0;
3621
3622     if (specbits & 1 << (int) RID_AUTO) nclasses++;
3623     if (specbits & 1 << (int) RID_STATIC) nclasses++;
3624     if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3625     if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3626     if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3627
3628     /* "static __thread" and "extern __thread" are allowed.  */
3629     if ((specbits & (1 << (int) RID_THREAD
3630                      | 1 << (int) RID_STATIC
3631                      | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3632       nclasses++;
3633
3634     /* Warn about storage classes that are invalid for certain
3635        kinds of declarations (parameters, typenames, etc.).  */
3636
3637     if (nclasses > 1)
3638       error ("multiple storage classes in declaration of `%s'", name);
3639     else if (funcdef_flag
3640              && (specbits
3641                  & ((1 << (int) RID_REGISTER)
3642                     | (1 << (int) RID_AUTO)
3643                     | (1 << (int) RID_TYPEDEF)
3644                     | (1 << (int) RID_THREAD))))
3645       {
3646         if (specbits & 1 << (int) RID_AUTO
3647             && (pedantic || current_binding_level == global_binding_level))
3648           pedwarn ("function definition declared `auto'");
3649         if (specbits & 1 << (int) RID_REGISTER)
3650           error ("function definition declared `register'");
3651         if (specbits & 1 << (int) RID_TYPEDEF)
3652           error ("function definition declared `typedef'");
3653         if (specbits & 1 << (int) RID_THREAD)
3654           error ("function definition declared `__thread'");
3655         specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3656                       | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3657       }
3658     else if (decl_context != NORMAL && nclasses > 0)
3659       {
3660         if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3661           ;
3662         else
3663           {
3664             switch (decl_context)
3665               {
3666               case FIELD:
3667                 error ("storage class specified for structure field `%s'",
3668                        name);
3669                 break;
3670               case PARM:
3671                 error ("storage class specified for parameter `%s'", name);
3672                 break;
3673               default:
3674                 error ("storage class specified for typename");
3675                 break;
3676               }
3677             specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3678                           | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3679                           | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3680           }
3681       }
3682     else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3683       {
3684         /* `extern' with initialization is invalid if not at top level.  */
3685         if (current_binding_level == global_binding_level)
3686           warning ("`%s' initialized and declared `extern'", name);
3687         else
3688           error ("`%s' has both `extern' and initializer", name);
3689       }
3690     else if (current_binding_level == global_binding_level)
3691       {
3692         if (specbits & 1 << (int) RID_AUTO)
3693           error ("top-level declaration of `%s' specifies `auto'", name);
3694       }
3695     else
3696       {
3697         if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3698           error ("nested function `%s' declared `extern'", name);
3699         else if ((specbits & (1 << (int) RID_THREAD
3700                                | 1 << (int) RID_EXTERN
3701                                | 1 << (int) RID_STATIC))
3702                  == (1 << (int) RID_THREAD))
3703           {
3704             error ("function-scope `%s' implicitly auto and declared `__thread'",
3705                    name);
3706             specbits &= ~(1 << (int) RID_THREAD);
3707           }
3708       }
3709   }
3710
3711   /* Now figure out the structure of the declarator proper.
3712      Descend through it, creating more complex types, until we reach
3713      the declared identifier (or NULL_TREE, in an absolute declarator).  */
3714
3715   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3716     {
3717       if (type == error_mark_node)
3718         {
3719           declarator = TREE_OPERAND (declarator, 0);
3720           continue;
3721         }
3722
3723       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3724          an INDIRECT_REF (for *...),
3725          a CALL_EXPR (for ...(...)),
3726          a TREE_LIST (for nested attributes),
3727          an identifier (for the name being declared)
3728          or a null pointer (for the place in an absolute declarator
3729          where the name was omitted).
3730          For the last two cases, we have just exited the loop.
3731
3732          At this point, TYPE is the type of elements of an array,
3733          or for a function to return, or for a pointer to point to.
3734          After this sequence of ifs, TYPE is the type of the
3735          array or function or pointer, and DECLARATOR has had its
3736          outermost layer removed.  */
3737
3738       if (array_ptr_quals != NULL_TREE || array_parm_static)
3739         {
3740           /* Only the innermost declarator (making a parameter be of
3741              array type which is converted to pointer type)
3742              may have static or type qualifiers.  */
3743           error ("static or type qualifiers in non-parameter array declarator");
3744           array_ptr_quals = NULL_TREE;
3745           array_parm_static = 0;
3746         }
3747
3748       if (TREE_CODE (declarator) == TREE_LIST)
3749         {
3750           /* We encode a declarator with embedded attributes using
3751              a TREE_LIST.  */
3752           tree attrs = TREE_PURPOSE (declarator);
3753           tree inner_decl;
3754           int attr_flags = 0;
3755           declarator = TREE_VALUE (declarator);
3756           inner_decl = declarator;
3757           while (inner_decl != NULL_TREE
3758                  && TREE_CODE (inner_decl) == TREE_LIST)
3759             inner_decl = TREE_VALUE (inner_decl);
3760           if (inner_decl == NULL_TREE
3761               || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3762             attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3763           else if (TREE_CODE (inner_decl) == CALL_EXPR)
3764             attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3765           else if (TREE_CODE (inner_decl) == ARRAY_REF)
3766             attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3767           returned_attrs = decl_attributes (&type,
3768                                             chainon (returned_attrs, attrs),
3769                                             attr_flags);
3770         }
3771       else if (TREE_CODE (declarator) == ARRAY_REF)
3772         {
3773           tree itype = NULL_TREE;
3774           tree size = TREE_OPERAND (declarator, 1);
3775           /* The index is a signed object `sizetype' bits wide.  */
3776           tree index_type = c_common_signed_type (sizetype);
3777
3778           array_ptr_quals = TREE_TYPE (declarator);
3779           array_parm_static = TREE_STATIC (declarator);
3780
3781           declarator = TREE_OPERAND (declarator, 0);
3782
3783           /* Check for some types that there cannot be arrays of.  */
3784
3785           if (VOID_TYPE_P (type))
3786             {
3787               error ("declaration of `%s' as array of voids", name);
3788               type = error_mark_node;
3789             }
3790
3791           if (TREE_CODE (type) == FUNCTION_TYPE)
3792             {
3793               error ("declaration of `%s' as array of functions", name);
3794               type = error_mark_node;
3795             }
3796
3797           if (pedantic && flexible_array_type_p (type))
3798             pedwarn ("invalid use of structure with flexible array member");
3799
3800           if (size == error_mark_node)
3801             type = error_mark_node;
3802
3803           if (type == error_mark_node)
3804             continue;
3805
3806           /* If size was specified, set ITYPE to a range-type for that size.
3807              Otherwise, ITYPE remains null.  finish_decl may figure it out
3808              from an initial value.  */
3809
3810           if (size)
3811             {
3812               /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
3813               STRIP_TYPE_NOPS (size);
3814
3815               if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3816                 {
3817                   error ("size of array `%s' has non-integer type", name);
3818                   size = integer_one_node;
3819                 }
3820
3821               if (pedantic && integer_zerop (size))
3822                 pedwarn ("ISO C forbids zero-size array `%s'", name);
3823
3824               if (TREE_CODE (size) == INTEGER_CST)
3825                 {
3826                   constant_expression_warning (size);
3827                   if (tree_int_cst_sgn (size) < 0)
3828                     {
3829                       error ("size of array `%s' is negative", name);
3830                       size = integer_one_node;
3831                     }
3832                 }
3833               else
3834                 {
3835                   /* Make sure the array size remains visibly nonconstant
3836                      even if it is (eg) a const variable with known value.  */
3837                   size_varies = 1;
3838
3839                   if (!flag_isoc99 && pedantic)
3840                     {
3841                       if (TREE_CONSTANT (size))
3842                         pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3843                                  name);
3844                       else
3845                         pedwarn ("ISO C90 forbids variable-size array `%s'",
3846                                  name);
3847                     }
3848                 }
3849
3850               if (integer_zerop (size))
3851                 {
3852                   /* A zero-length array cannot be represented with an
3853                      unsigned index type, which is what we'll get with
3854                      build_index_type.  Create an open-ended range instead.  */
3855                   itype = build_range_type (sizetype, size, NULL_TREE);
3856                 }
3857               else
3858                 {
3859                   /* Compute the maximum valid index, that is, size - 1.
3860                      Do the calculation in index_type, so that if it is
3861                      a variable the computations will be done in the
3862                      proper mode.  */
3863                   itype = fold (build (MINUS_EXPR, index_type,
3864                                        convert (index_type, size),
3865                                        convert (index_type, size_one_node)));
3866
3867                   /* If that overflowed, the array is too big.
3868                      ??? While a size of INT_MAX+1 technically shouldn't
3869                      cause an overflow (because we subtract 1), the overflow
3870                      is recorded during the conversion to index_type, before
3871                      the subtraction.  Handling this case seems like an
3872                      unnecessary complication.  */
3873                   if (TREE_OVERFLOW (itype))
3874                     {
3875                       error ("size of array `%s' is too large", name);
3876                       type = error_mark_node;
3877                       continue;
3878                     }
3879
3880                   if (size_varies)
3881                     {
3882                       /* We must be able to distinguish the
3883                          SAVE_EXPR_CONTEXT for the variably-sized type
3884                          so that we can set it correctly in
3885                          set_save_expr_context.  The convention is
3886                          that all SAVE_EXPRs that need to be reset
3887                          have NULL_TREE for their SAVE_EXPR_CONTEXT.  */
3888                       tree cfd = current_function_decl;
3889                       if (decl_context == PARM)
3890                         current_function_decl = NULL_TREE;
3891                       itype = variable_size (itype);
3892                       if (decl_context == PARM)
3893                         current_function_decl = cfd;
3894                     }
3895                   itype = build_index_type (itype);
3896                 }
3897             }
3898           else if (decl_context == FIELD)
3899             {
3900               if (pedantic && !flag_isoc99 && !in_system_header)
3901                 pedwarn ("ISO C90 does not support flexible array members");
3902
3903               /* ISO C99 Flexible array members are effectively identical
3904                  to GCC's zero-length array extension.  */
3905               itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3906             }
3907
3908           /* If pedantic, complain about arrays of incomplete types.  */
3909
3910           if (pedantic && !COMPLETE_TYPE_P (type))
3911             pedwarn ("array type has incomplete element type");
3912
3913           /* Build the array type itself, then merge any constancy or
3914              volatility into the target type.  We must do it in this order
3915              to ensure that the TYPE_MAIN_VARIANT field of the array type
3916              is set correctly.  */
3917
3918           type = build_array_type (type, itype);
3919           if (type_quals)
3920             type = c_build_qualified_type (type, type_quals);
3921
3922           if (size_varies)
3923             C_TYPE_VARIABLE_SIZE (type) = 1;
3924
3925           /* The GCC extension for zero-length arrays differs from
3926              ISO flexible array members in that sizeof yields zero.  */
3927           if (size && integer_zerop (size))
3928             {
3929               layout_type (type);
3930               TYPE_SIZE (type) = bitsize_zero_node;
3931               TYPE_SIZE_UNIT (type) = size_zero_node;
3932             }
3933           if (decl_context != PARM
3934               && (array_ptr_quals != NULL_TREE || array_parm_static))
3935             {
3936               error ("static or type qualifiers in non-parameter array declarator");
3937               array_ptr_quals = NULL_TREE;
3938               array_parm_static = 0;
3939             }
3940         }
3941       else if (TREE_CODE (declarator) == CALL_EXPR)
3942         {
3943           tree arg_types;
3944
3945           /* Declaring a function type.
3946              Make sure we have a valid type for the function to return.  */
3947           if (type == error_mark_node)
3948             continue;
3949
3950           size_varies = 0;
3951
3952           /* Warn about some types functions can't return.  */
3953
3954           if (TREE_CODE (type) == FUNCTION_TYPE)
3955             {
3956               error ("`%s' declared as function returning a function", name);
3957               type = integer_type_node;
3958             }
3959           if (TREE_CODE (type) == ARRAY_TYPE)
3960             {
3961               error ("`%s' declared as function returning an array", name);
3962               type = integer_type_node;
3963             }
3964
3965           /* Construct the function type and go to the next
3966              inner layer of declarator.  */
3967
3968           arg_types = grokparms (TREE_OPERAND (declarator, 1),
3969                                  funcdef_flag
3970                                  /* Say it's a definition
3971                                     only for the CALL_EXPR
3972                                     closest to the identifier.  */
3973                                  && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3974           /* Type qualifiers before the return type of the function
3975              qualify the return type, not the function type.  */
3976           if (type_quals)
3977             {
3978               /* Type qualifiers on a function return type are normally
3979                  permitted by the standard but have no effect, so give a
3980                  warning at -Wextra.  Qualifiers on a void return type have
3981                  meaning as a GNU extension, and are banned on function
3982                  definitions in ISO C.  FIXME: strictly we shouldn't
3983                  pedwarn for qualified void return types except on function
3984                  definitions, but not doing so could lead to the undesirable
3985                  state of a "volatile void" function return type not being
3986                  warned about, and a use of the function being compiled
3987                  with GNU semantics, with no diagnostics under -pedantic.  */
3988               if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3989                 pedwarn ("ISO C forbids qualified void function return type");
3990               else if (extra_warnings
3991                        && !(VOID_TYPE_P (type)
3992                             && type_quals == TYPE_QUAL_VOLATILE))
3993                 warning ("type qualifiers ignored on function return type");
3994
3995               type = c_build_qualified_type (type, type_quals);
3996             }
3997           type_quals = TYPE_UNQUALIFIED;
3998
3999           type = build_function_type (type, arg_types);
4000           declarator = TREE_OPERAND (declarator, 0);
4001
4002           /* Set the TYPE_CONTEXTs for each tagged type which is local to
4003              the formal parameter list of this FUNCTION_TYPE to point to
4004              the FUNCTION_TYPE node itself.  */
4005
4006           {
4007             tree link;
4008
4009             for (link = last_function_parm_tags;
4010                  link;
4011                  link = TREE_CHAIN (link))
4012               TYPE_CONTEXT (TREE_VALUE (link)) = type;
4013           }
4014         }
4015       else if (TREE_CODE (declarator) == INDIRECT_REF)
4016         {
4017           /* Merge any constancy or volatility into the target type
4018              for the pointer.  */
4019
4020           if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4021               && type_quals)
4022             pedwarn ("ISO C forbids qualified function types");
4023           if (type_quals)
4024             type = c_build_qualified_type (type, type_quals);
4025           type_quals = TYPE_UNQUALIFIED;
4026           size_varies = 0;
4027
4028           type = build_pointer_type (type);
4029
4030           /* Process a list of type modifier keywords
4031              (such as const or volatile) that were given inside the `*'.  */
4032
4033           if (TREE_TYPE (declarator))
4034             {
4035               tree typemodlist;
4036               int erred = 0;
4037
4038               constp = 0;
4039               volatilep = 0;
4040               restrictp = 0;
4041               for (typemodlist = TREE_TYPE (declarator); typemodlist;
4042                    typemodlist = TREE_CHAIN (typemodlist))
4043                 {
4044                   tree qualifier = TREE_VALUE (typemodlist);
4045
4046                   if (C_IS_RESERVED_WORD (qualifier))
4047                     {
4048                       if (C_RID_CODE (qualifier) == RID_CONST)
4049                         constp++;
4050                       else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4051                         volatilep++;
4052                       else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4053                         restrictp++;
4054                       else
4055                         erred++;
4056                     }
4057                   else
4058                     erred++;
4059                 }
4060
4061               if (erred)
4062                 error ("invalid type modifier within pointer declarator");
4063               if (constp > 1 && ! flag_isoc99)
4064                 pedwarn ("duplicate `const'");
4065               if (volatilep > 1 && ! flag_isoc99)
4066                 pedwarn ("duplicate `volatile'");
4067               if (restrictp > 1 && ! flag_isoc99)
4068                 pedwarn ("duplicate `restrict'");
4069
4070               type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4071                             | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4072                             | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4073             }
4074
4075           declarator = TREE_OPERAND (declarator, 0);
4076         }
4077       else
4078         abort ();
4079
4080     }
4081
4082   /* Now TYPE has the actual type.  */
4083
4084   /* Did array size calculations overflow?  */
4085
4086   if (TREE_CODE (type) == ARRAY_TYPE
4087       && COMPLETE_TYPE_P (type)
4088       && TREE_OVERFLOW (TYPE_SIZE (type)))
4089     {
4090       error ("size of array `%s' is too large", name);
4091       /* If we proceed with the array type as it is, we'll eventually
4092          crash in tree_low_cst().  */
4093       type = error_mark_node;
4094     }
4095
4096   /* If this is declaring a typedef name, return a TYPE_DECL.  */
4097
4098   if (specbits & (1 << (int) RID_TYPEDEF))
4099     {
4100       tree decl;
4101       /* Note that the grammar rejects storage classes
4102          in typenames, fields or parameters */
4103       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4104           && type_quals)
4105         pedwarn ("ISO C forbids qualified function types");
4106       if (type_quals)
4107         type = c_build_qualified_type (type, type_quals);
4108       decl = build_decl (TYPE_DECL, declarator, type);
4109       if ((specbits & (1 << (int) RID_SIGNED))
4110           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4111         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4112       decl_attributes (&decl, returned_attrs, 0);
4113       return decl;
4114     }
4115
4116   /* Detect the case of an array type of unspecified size
4117      which came, as such, direct from a typedef name.
4118      We must copy the type, so that each identifier gets
4119      a distinct type, so that each identifier's size can be
4120      controlled separately by its own initializer.  */
4121
4122   if (type != 0 && typedef_type != 0
4123       && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4124       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4125     {
4126       type = build_array_type (TREE_TYPE (type), 0);
4127       if (size_varies)
4128         C_TYPE_VARIABLE_SIZE (type) = 1;
4129     }
4130
4131   /* If this is a type name (such as, in a cast or sizeof),
4132      compute the type and return it now.  */
4133
4134   if (decl_context == TYPENAME)
4135     {
4136       /* Note that the grammar rejects storage classes
4137          in typenames, fields or parameters */
4138       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4139           && type_quals)
4140         pedwarn ("ISO C forbids const or volatile function types");
4141       if (type_quals)
4142         type = c_build_qualified_type (type, type_quals);
4143       decl_attributes (&type, returned_attrs, 0);
4144       return type;
4145     }
4146
4147   /* Aside from typedefs and type names (handle above),
4148      `void' at top level (not within pointer)
4149      is allowed only in public variables.
4150      We don't complain about parms either, but that is because
4151      a better error message can be made later.  */
4152
4153   if (VOID_TYPE_P (type) && decl_context != PARM
4154       && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4155             && ((specbits & (1 << (int) RID_EXTERN))
4156                 || (current_binding_level == global_binding_level
4157                     && !(specbits
4158                          & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4159     {
4160       error ("variable or field `%s' declared void", name);
4161       type = integer_type_node;
4162     }
4163
4164   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4165      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
4166
4167   {
4168     tree decl;
4169
4170     if (decl_context == PARM)
4171       {
4172         tree type_as_written;
4173         tree promoted_type;
4174
4175         /* A parameter declared as an array of T is really a pointer to T.
4176            One declared as a function is really a pointer to a function.  */
4177
4178         if (TREE_CODE (type) == ARRAY_TYPE)
4179           {
4180             /* Transfer const-ness of array into that of type pointed to.  */
4181             type = TREE_TYPE (type);
4182             if (type_quals)
4183               type = c_build_qualified_type (type, type_quals);
4184             type = build_pointer_type (type);
4185             type_quals = TYPE_UNQUALIFIED;
4186             if (array_ptr_quals)
4187               {
4188                 tree new_ptr_quals, new_ptr_attrs;
4189                 int erred = 0;
4190                 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4191                 /* We don't yet implement attributes in this context.  */
4192                 if (new_ptr_attrs != NULL_TREE)
4193                   warning ("attributes in parameter array declarator ignored");
4194
4195                 constp = 0;
4196                 volatilep = 0;
4197                 restrictp = 0;
4198                 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4199                   {
4200                     tree qualifier = TREE_VALUE (new_ptr_quals);
4201
4202                     if (C_IS_RESERVED_WORD (qualifier))
4203                       {
4204                         if (C_RID_CODE (qualifier) == RID_CONST)
4205                           constp++;
4206                         else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4207                           volatilep++;
4208                         else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4209                           restrictp++;
4210                         else
4211                           erred++;
4212                       }
4213                     else
4214                       erred++;
4215                   }
4216
4217                 if (erred)
4218                   error ("invalid type modifier within array declarator");
4219
4220                 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4221                               | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4222                               | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4223               }
4224             size_varies = 0;
4225           }
4226         else if (TREE_CODE (type) == FUNCTION_TYPE)
4227           {
4228             if (pedantic && type_quals)
4229               pedwarn ("ISO C forbids qualified function types");
4230             if (type_quals)
4231               type = c_build_qualified_type (type, type_quals);
4232             type = build_pointer_type (type);
4233             type_quals = TYPE_UNQUALIFIED;
4234           }
4235         else if (type_quals)
4236           type = c_build_qualified_type (type, type_quals);
4237
4238         type_as_written = type;
4239
4240         decl = build_decl (PARM_DECL, declarator, type);
4241         if (size_varies)
4242           C_DECL_VARIABLE_SIZE (decl) = 1;
4243
4244         /* Compute the type actually passed in the parmlist,
4245            for the case where there is no prototype.
4246            (For example, shorts and chars are passed as ints.)
4247            When there is a prototype, this is overridden later.  */
4248
4249         if (type == error_mark_node)
4250           promoted_type = type;
4251         else
4252           promoted_type = c_type_promotes_to (type);
4253
4254         DECL_ARG_TYPE (decl) = promoted_type;
4255         DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4256       }
4257     else if (decl_context == FIELD)
4258       {
4259         /* Structure field.  It may not be a function.  */
4260
4261         if (TREE_CODE (type) == FUNCTION_TYPE)
4262           {
4263             error ("field `%s' declared as a function", name);
4264             type = build_pointer_type (type);
4265           }
4266         else if (TREE_CODE (type) != ERROR_MARK
4267                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4268           {
4269             error ("field `%s' has incomplete type", name);
4270             type = error_mark_node;
4271           }
4272         /* Move type qualifiers down to element of an array.  */
4273         if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4274           {
4275             type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4276                                                              type_quals),
4277                                      TYPE_DOMAIN (type));
4278           }
4279         decl = build_decl (FIELD_DECL, declarator, type);
4280         DECL_NONADDRESSABLE_P (decl) = bitfield;
4281
4282         if (size_varies)
4283           C_DECL_VARIABLE_SIZE (decl) = 1;
4284       }
4285     else if (TREE_CODE (type) == FUNCTION_TYPE)
4286       {
4287         /* Every function declaration is "external"
4288            except for those which are inside a function body
4289            in which `auto' is used.
4290            That is a case not specified by ANSI C,
4291            and we use it for forward declarations for nested functions.  */
4292         int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4293                           || current_binding_level == global_binding_level);
4294
4295         if (specbits & (1 << (int) RID_AUTO)
4296             && (pedantic || current_binding_level == global_binding_level))
4297           pedwarn ("invalid storage class for function `%s'", name);
4298         if (specbits & (1 << (int) RID_REGISTER))
4299           error ("invalid storage class for function `%s'", name);
4300         if (specbits & (1 << (int) RID_THREAD))
4301           error ("invalid storage class for function `%s'", name);
4302         /* Function declaration not at top level.
4303            Storage classes other than `extern' are not allowed
4304            and `extern' makes no difference.  */
4305         if (current_binding_level != global_binding_level
4306             && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4307             && pedantic)
4308           pedwarn ("invalid storage class for function `%s'", name);
4309
4310         decl = build_decl (FUNCTION_DECL, declarator, type);
4311         decl = build_decl_attribute_variant (decl, decl_attr);
4312
4313         DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
4314           ggc_alloc_cleared (sizeof (struct lang_decl));
4315
4316         if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4317           pedwarn ("ISO C forbids qualified function types");
4318
4319         /* GNU C interprets a `volatile void' return type to indicate
4320            that the function does not return.  */
4321         if ((type_quals & TYPE_QUAL_VOLATILE)
4322             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4323           warning ("`noreturn' function returns non-void value");
4324
4325         if (extern_ref)
4326           DECL_EXTERNAL (decl) = 1;
4327         /* Record absence of global scope for `static' or `auto'.  */
4328         TREE_PUBLIC (decl)
4329           = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4330
4331         if (defaulted_int)
4332           C_FUNCTION_IMPLICIT_INT (decl) = 1;
4333
4334         /* Record presence of `inline', if it is reasonable.  */
4335         if (MAIN_NAME_P (declarator))
4336           {
4337             if (inlinep)
4338               warning ("cannot inline function `main'");
4339           }
4340         else if (inlinep)
4341           {
4342             /* Assume that otherwise the function can be inlined.  */
4343             DECL_DECLARED_INLINE_P (decl) = 1;
4344
4345             /* Do not mark bare declarations as DECL_INLINE.  Doing so
4346                in the presence of multiple declarations can result in
4347                the abstract origin pointing between the declarations,
4348                which will confuse dwarf2out.  */
4349             if (initialized)
4350               {
4351                 DECL_INLINE (decl) = 1;
4352                 if (specbits & (1 << (int) RID_EXTERN))
4353                   current_extern_inline = 1;
4354               }
4355           }
4356         /* If -finline-functions, assume it can be inlined.  This does
4357            two things: let the function be deferred until it is actually
4358            needed, and let dwarf2 know that the function is inlinable.  */
4359         else if (flag_inline_trees == 2 && initialized)
4360           {
4361             if (!DECL_INLINE (decl))
4362                 DID_INLINE_FUNC (decl) = 1;
4363             DECL_INLINE (decl) = 1;
4364             DECL_DECLARED_INLINE_P (decl) = 0;
4365           }
4366       }
4367     else
4368       {
4369         /* It's a variable.  */
4370         /* An uninitialized decl with `extern' is a reference.  */
4371         int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4372
4373         /* Move type qualifiers down to element of an array.  */
4374         if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4375           {
4376             int saved_align = TYPE_ALIGN(type);
4377             type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4378                                                              type_quals),
4379                                      TYPE_DOMAIN (type));
4380             TYPE_ALIGN (type) = saved_align;
4381           }
4382         else if (type_quals)
4383           type = c_build_qualified_type (type, type_quals);
4384
4385         /* It is invalid to create an `extern' declaration for a
4386            variable if there is a global declaration that is
4387            `static'.  */
4388         if (extern_ref && current_binding_level != global_binding_level)
4389           {
4390             tree global_decl;
4391
4392             global_decl = identifier_global_value (declarator);
4393             if (global_decl
4394                 && TREE_CODE (global_decl) == VAR_DECL
4395                 && !TREE_PUBLIC (global_decl))
4396               error ("variable previously declared `static' redeclared "
4397                      "`extern'");
4398           }
4399
4400         decl = build_decl (VAR_DECL, declarator, type);
4401         if (size_varies)
4402           C_DECL_VARIABLE_SIZE (decl) = 1;
4403
4404         if (inlinep)
4405           pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4406
4407         DECL_EXTERNAL (decl) = extern_ref;
4408
4409         /* At top level, the presence of a `static' or `register' storage
4410            class specifier, or the absence of all storage class specifiers
4411            makes this declaration a definition (perhaps tentative).  Also,
4412            the absence of both `static' and `register' makes it public.  */
4413         if (current_binding_level == global_binding_level)
4414           {
4415             TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4416                                                | (1 << (int) RID_REGISTER)));
4417             TREE_STATIC (decl) = !extern_ref;
4418           }
4419         /* Not at top level, only `static' makes a static definition.  */
4420         else
4421           {
4422             TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4423             TREE_PUBLIC (decl) = extern_ref;
4424           }
4425
4426         if (specbits & 1 << (int) RID_THREAD)
4427           {
4428             if (targetm.have_tls)
4429               DECL_THREAD_LOCAL (decl) = 1;
4430             else
4431               /* A mere warning is sure to result in improper semantics
4432                  at runtime.  Don't bother to allow this to compile.  */
4433               error ("thread-local storage not supported for this target");
4434           }
4435       }
4436
4437     /* Record `register' declaration for warnings on &
4438        and in case doing stupid register allocation.  */
4439
4440     if (specbits & (1 << (int) RID_REGISTER))
4441       DECL_REGISTER (decl) = 1;
4442
4443     /* Record constancy and volatility.  */
4444     c_apply_type_quals_to_decl (type_quals, decl);
4445
4446     /* If a type has volatile components, it should be stored in memory.
4447        Otherwise, the fact that those components are volatile
4448        will be ignored, and would even crash the compiler.  */
4449     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4450       c_mark_addressable (decl);
4451
4452     decl_attributes (&decl, returned_attrs, 0);
4453
4454     return decl;
4455   }
4456 }
4457 \f
4458 /* Decode the parameter-list info for a function type or function definition.
4459    The argument is the value returned by `get_parm_info' (or made in parse.y
4460    if there is an identifier list instead of a parameter decl list).
4461    These two functions are separate because when a function returns
4462    or receives functions then each is called multiple times but the order
4463    of calls is different.  The last call to `grokparms' is always the one
4464    that contains the formal parameter names of a function definition.
4465
4466    Store in `last_function_parms' a chain of the decls of parms.
4467    Also store in `last_function_parm_tags' a chain of the struct, union,
4468    and enum tags declared among the parms.
4469
4470    Return a list of arg types to use in the FUNCTION_TYPE for this function.
4471
4472    FUNCDEF_FLAG is nonzero for a function definition, 0 for
4473    a mere declaration.  A nonempty identifier-list gets an error message
4474    when FUNCDEF_FLAG is zero.  */
4475
4476 static tree
4477 grokparms (tree parms_info, int funcdef_flag)
4478 {
4479   tree first_parm = TREE_CHAIN (parms_info);
4480
4481   last_function_parms = TREE_PURPOSE (parms_info);
4482   last_function_parm_tags = TREE_VALUE (parms_info);
4483
4484   if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4485       && !in_system_header)
4486     warning ("function declaration isn't a prototype");
4487
4488   if (first_parm != 0
4489       && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4490     {
4491       if (! funcdef_flag)
4492         pedwarn ("parameter names (without types) in function declaration");
4493
4494       last_function_parms = first_parm;
4495       return 0;
4496     }
4497   else
4498     {
4499       tree parm;
4500       tree typelt;
4501       /* If the arg types are incomplete in a declaration,
4502          they must include undefined tags.
4503          These tags can never be defined in the scope of the declaration,
4504          so the types can never be completed,
4505          and no call can be compiled successfully.  */
4506       for (parm = last_function_parms, typelt = first_parm;
4507            parm;
4508            parm = TREE_CHAIN (parm))
4509         /* Skip over any enumeration constants declared here.  */
4510         if (TREE_CODE (parm) == PARM_DECL)
4511           {
4512             /* Barf if the parameter itself has an incomplete type.  */
4513             tree type = TREE_VALUE (typelt);
4514             if (type == error_mark_node)
4515               continue;
4516             if (!COMPLETE_TYPE_P (type))
4517               {
4518                 if (funcdef_flag && DECL_NAME (parm) != 0)
4519                   error ("parameter `%s' has incomplete type",
4520                          IDENTIFIER_POINTER (DECL_NAME (parm)));
4521                 else
4522                   warning ("parameter has incomplete type");
4523                 if (funcdef_flag)
4524                   {
4525                     TREE_VALUE (typelt) = error_mark_node;
4526                     TREE_TYPE (parm) = error_mark_node;
4527                   }
4528               }
4529             typelt = TREE_CHAIN (typelt);
4530           }
4531
4532       return first_parm;
4533     }
4534 }
4535
4536 /* Return a tree_list node with info on a parameter list just parsed.
4537    The TREE_PURPOSE is a chain of decls of those parms.
4538    The TREE_VALUE is a list of structure, union and enum tags defined.
4539    The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4540    This tree_list node is later fed to `grokparms'.
4541
4542    VOID_AT_END nonzero means append `void' to the end of the type-list.
4543    Zero means the parmlist ended with an ellipsis so don't append `void'.  */
4544
4545 tree
4546 get_parm_info (int void_at_end)
4547 {
4548   tree decl, t;
4549   tree types = 0;
4550   int erred = 0;
4551   tree tags = gettags ();
4552   tree parms = getdecls ();
4553   tree new_parms = 0;
4554   tree order = current_binding_level->parm_order;
4555
4556   /* Just `void' (and no ellipsis) is special.  There are really no parms.
4557      But if the `void' is qualified (by `const' or `volatile') or has a
4558      storage class specifier (`register'), then the behavior is undefined;
4559      by not counting it as the special case of `void' we will cause an
4560      error later.  Typedefs for `void' are OK (see DR#157).  */
4561   if (void_at_end && parms != 0
4562       && TREE_CHAIN (parms) == 0
4563       && VOID_TYPE_P (TREE_TYPE (parms))
4564       && ! TREE_THIS_VOLATILE (parms)
4565       && ! TREE_READONLY (parms)
4566       && ! DECL_REGISTER (parms)
4567       && DECL_NAME (parms) == 0)
4568     {
4569       parms = NULL_TREE;
4570       storedecls (NULL_TREE);
4571       return tree_cons (NULL_TREE, NULL_TREE,
4572                         tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4573     }
4574
4575   /* Extract enumerator values and other non-parms declared with the parms.
4576      Likewise any forward parm decls that didn't have real parm decls.  */
4577   for (decl = parms; decl;)
4578     {
4579       tree next = TREE_CHAIN (decl);
4580
4581       if (TREE_CODE (decl) != PARM_DECL)
4582         {
4583           TREE_CHAIN (decl) = new_parms;
4584           new_parms = decl;
4585         }
4586       else if (TREE_ASM_WRITTEN (decl))
4587         {
4588           error_with_decl (decl,
4589                            "parameter `%s' has just a forward declaration");
4590           TREE_CHAIN (decl) = new_parms;
4591           new_parms = decl;
4592         }
4593       decl = next;
4594     }
4595
4596   /* Put the parm decls back in the order they were in in the parm list.  */
4597   for (t = order; t; t = TREE_CHAIN (t))
4598     {
4599       if (TREE_CHAIN (t))
4600         TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4601       else
4602         TREE_CHAIN (TREE_VALUE (t)) = 0;
4603     }
4604
4605   new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4606                        new_parms);
4607
4608   /* Store the parmlist in the binding level since the old one
4609      is no longer a valid list.  (We have changed the chain pointers.)  */
4610   storedecls (new_parms);
4611
4612   for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4613     /* There may also be declarations for enumerators if an enumeration
4614        type is declared among the parms.  Ignore them here.  */
4615     if (TREE_CODE (decl) == PARM_DECL)
4616       {
4617         /* Since there is a prototype,
4618            args are passed in their declared types.  */
4619         tree type = TREE_TYPE (decl);
4620         DECL_ARG_TYPE (decl) = type;
4621         if (PROMOTE_PROTOTYPES
4622             && INTEGRAL_TYPE_P (type)
4623             && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4624           DECL_ARG_TYPE (decl) = integer_type_node;
4625
4626         types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4627         if (VOID_TYPE_P (TREE_VALUE (types)) && ! erred
4628             && DECL_NAME (decl) == 0)
4629           {
4630             error ("`void' in parameter list must be the entire list");
4631             erred = 1;
4632           }
4633       }
4634
4635   if (void_at_end)
4636     return tree_cons (new_parms, tags,
4637                       nreverse (tree_cons (NULL_TREE, void_type_node, types)));
4638
4639   return tree_cons (new_parms, tags, nreverse (types));
4640 }
4641
4642 /* At end of parameter list, warn about any struct, union or enum tags
4643    defined within.  Do so because these types cannot ever become complete.  */
4644
4645 void
4646 parmlist_tags_warning (void)
4647 {
4648   tree elt;
4649   static int already;
4650
4651   for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4652     {
4653       enum tree_code code = TREE_CODE (TREE_VALUE (elt));
4654       /* An anonymous union parm type is meaningful as a GNU extension.
4655          So don't warn for that.  */
4656       if (code == UNION_TYPE && TREE_PURPOSE (elt) == 0 && !pedantic)
4657         continue;
4658       if (TREE_PURPOSE (elt) != 0)
4659         {
4660           if (code == RECORD_TYPE)
4661             warning ("`struct %s' declared inside parameter list",
4662                      IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4663           else if (code == UNION_TYPE)
4664             warning ("`union %s' declared inside parameter list",
4665                      IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4666           else
4667             warning ("`enum %s' declared inside parameter list",
4668                      IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4669         }
4670       else
4671         {
4672           /* For translation these need to be separate warnings */
4673           if (code == RECORD_TYPE)
4674             warning ("anonymous struct declared inside parameter list");
4675           else if (code == UNION_TYPE)
4676             warning ("anonymous union declared inside parameter list");
4677           else
4678             warning ("anonymous enum declared inside parameter list");
4679         }
4680       if (! already)
4681         {
4682           warning ("its scope is only this definition or declaration, which is probably not what you want");
4683           already = 1;
4684         }
4685     }
4686 }
4687 \f
4688 /* Get the struct, enum or union (CODE says which) with tag NAME.
4689    Define the tag as a forward-reference if it is not defined.  */
4690
4691 tree
4692 xref_tag (enum tree_code code, tree name)
4693 {
4694   /* If a cross reference is requested, look up the type
4695      already defined for this tag and return it.  */
4696
4697   tree ref = lookup_tag (code, name, 0);
4698   /* If this is the right type of tag, return what we found.
4699      (This reference will be shadowed by shadow_tag later if appropriate.)
4700      If this is the wrong type of tag, do not return it.  If it was the
4701      wrong type in the same binding level, we will have had an error
4702      message already; if in a different binding level and declaring
4703      a name, pending_xref_error will give an error message; but if in a
4704      different binding level and not declaring a name, this tag should
4705      shadow the previous declaration of a different type of tag, and
4706      this would not work properly if we return the reference found.
4707      (For example, with "struct foo" in an outer scope, "union foo;"
4708      must shadow that tag with a new one of union type.)  */
4709   if (ref && TREE_CODE (ref) == code)
4710     return ref;
4711
4712   /* If no such tag is yet defined, create a forward-reference node
4713      and record it as the "definition".
4714      When a real declaration of this type is found,
4715      the forward-reference will be altered into a real type.  */
4716
4717   ref = make_node (code);
4718   if (code == ENUMERAL_TYPE)
4719     {
4720       /* Give the type a default layout like unsigned int
4721          to avoid crashing if it does not get defined.  */
4722       TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4723       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4724       TYPE_USER_ALIGN (ref) = 0;
4725       TREE_UNSIGNED (ref) = 1;
4726       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4727       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4728       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4729     }
4730
4731   pushtag (name, ref);
4732
4733   return ref;
4734 }
4735 \f
4736 /* Make sure that the tag NAME is defined *in the current binding level*
4737    at least as a forward reference.
4738    CODE says which kind of tag NAME ought to be.  */
4739
4740 tree
4741 start_struct (enum tree_code code, tree name)
4742 {
4743   /* If there is already a tag defined at this binding level
4744      (as a forward reference), just return it.  */
4745
4746   tree ref = 0;
4747
4748   if (name != 0)
4749     ref = lookup_tag (code, name, 1);
4750   if (ref && TREE_CODE (ref) == code)
4751     {
4752       if (TYPE_FIELDS (ref))
4753         {
4754           if (code == UNION_TYPE)
4755             error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4756           else
4757             error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4758         }
4759     }
4760   else
4761     {
4762       /* Otherwise create a forward-reference just so the tag is in scope.  */
4763
4764       ref = make_node (code);
4765       pushtag (name, ref);
4766     }
4767
4768   C_TYPE_BEING_DEFINED (ref) = 1;
4769   TYPE_PACKED (ref) = flag_pack_struct;
4770   return ref;
4771 }
4772
4773 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4774    of a structure component, returning a FIELD_DECL node.
4775    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4776
4777    This is done during the parsing of the struct declaration.
4778    The FIELD_DECL nodes are chained together and the lot of them
4779    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
4780
4781 tree
4782 grokfield (tree declarator, tree declspecs, tree width)
4783 {
4784   tree value;
4785
4786   if (declarator == NULL_TREE && width == NULL_TREE)
4787     {
4788       /* This is an unnamed decl.
4789
4790          If we have something of the form "union { list } ;" then this
4791          is the anonymous union extension.  Similarly for struct.
4792
4793          If this is something of the form "struct foo;", then
4794            If MS extensions are enabled, this is handled as an
4795              anonymous struct.
4796            Otherwise this is a forward declaration of a structure tag.
4797
4798          If this is something of the form "foo;" and foo is a TYPE_DECL, then
4799            If MS extensions are enabled and foo names a structure, then
4800              again this is an anonymous struct.
4801            Otherwise this is an error.
4802
4803          Oh what a horrid tangled web we weave.  I wonder if MS consciously
4804          took this from Plan 9 or if it was an accident of implementation
4805          that took root before someone noticed the bug...  */
4806
4807       tree type = TREE_VALUE (declspecs);
4808
4809       if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4810         type = TREE_TYPE (type);
4811       if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4812         {
4813           if (flag_ms_extensions)
4814             ; /* ok */
4815           else if (flag_iso)
4816             goto warn_unnamed_field;
4817           else if (TYPE_NAME (type) == NULL)
4818             ; /* ok */
4819           else
4820             goto warn_unnamed_field;
4821         }
4822       else
4823         {
4824         warn_unnamed_field:
4825           warning ("declaration does not declare anything");
4826           return NULL_TREE;
4827         }
4828     }
4829
4830   value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4831
4832   finish_decl (value, NULL_TREE, NULL_TREE);
4833   DECL_INITIAL (value) = width;
4834
4835   if (c_dialect_objc ())
4836     objc_check_decl (value);
4837   return value;
4838 }
4839 \f
4840 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
4841    the list such that this does not present a problem later.  */
4842
4843 static void
4844 detect_field_duplicates (tree fieldlist)
4845 {
4846   tree x, y;
4847   int timeout = 10;
4848
4849   /* First, see if there are more than "a few" fields.
4850      This is trivially true if there are zero or one fields.  */
4851   if (!fieldlist)
4852     return;
4853   x = TREE_CHAIN (fieldlist);
4854   if (!x)
4855     return;
4856   do {
4857     timeout--;
4858     x = TREE_CHAIN (x);
4859   } while (timeout > 0 && x);
4860
4861   /* If there were "few" fields, avoid the overhead of allocating
4862      a hash table.  Instead just do the nested traversal thing.  */
4863   if (timeout > 0)
4864     {
4865       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4866         if (DECL_NAME (x))
4867           {
4868             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4869               if (DECL_NAME (y) == DECL_NAME (x))
4870                 {
4871                   error_with_decl (x, "duplicate member `%s'");
4872                   DECL_NAME (x) = NULL_TREE;
4873                 }
4874           }
4875     }
4876   else
4877     {
4878       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4879       void **slot;
4880
4881       for (x = fieldlist; x ; x = TREE_CHAIN (x))
4882         if ((y = DECL_NAME (x)) != 0)
4883           {
4884             slot = htab_find_slot (htab, y, INSERT);
4885             if (*slot)
4886               {
4887                 error_with_decl (x, "duplicate member `%s'");
4888                 DECL_NAME (x) = NULL_TREE;
4889               }
4890             *slot = y;
4891           }
4892
4893       htab_delete (htab);
4894     }
4895 }
4896
4897 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4898    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4899    ATTRIBUTES are attributes to be applied to the structure.  */
4900
4901 tree
4902 finish_struct (tree t, tree fieldlist, tree attributes)
4903 {
4904   tree x;
4905   int toplevel = global_binding_level == current_binding_level;
4906   int saw_named_field;
4907
4908   /* If this type was previously laid out as a forward reference,
4909      make sure we lay it out again.  */
4910
4911   TYPE_SIZE (t) = 0;
4912
4913   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4914
4915   /* Nameless union parm types are useful as GCC extension.  */
4916   if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4917     /* Otherwise, warn about any struct or union def. in parmlist.  */
4918     if (in_parm_level_p ())
4919       {
4920         if (pedantic)
4921           pedwarn ("%s defined inside parms",
4922                    TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4923         else
4924           warning ("%s defined inside parms",
4925                    TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4926       }
4927
4928   if (pedantic)
4929     {
4930       for (x = fieldlist; x; x = TREE_CHAIN (x))
4931         if (DECL_NAME (x) != 0)
4932           break;
4933
4934       if (x == 0)
4935         pedwarn ("%s has no %s",
4936                  TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4937                  fieldlist ? _("named members") : _("members"));
4938     }
4939
4940   /* Install struct as DECL_CONTEXT of each field decl.
4941      Also process specified field sizes,m which is found in the DECL_INITIAL.
4942      Store 0 there, except for ": 0" fields (so we can find them
4943      and delete them, below).  */
4944
4945   saw_named_field = 0;
4946   for (x = fieldlist; x; x = TREE_CHAIN (x))
4947     {
4948       DECL_CONTEXT (x) = t;
4949       DECL_PACKED (x) |= TYPE_PACKED (t);
4950
4951       /* If any field is const, the structure type is pseudo-const.  */
4952       if (TREE_READONLY (x))
4953         C_TYPE_FIELDS_READONLY (t) = 1;
4954       else
4955         {
4956           /* A field that is pseudo-const makes the structure likewise.  */
4957           tree t1 = TREE_TYPE (x);
4958           while (TREE_CODE (t1) == ARRAY_TYPE)
4959             t1 = TREE_TYPE (t1);
4960           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4961               && C_TYPE_FIELDS_READONLY (t1))
4962             C_TYPE_FIELDS_READONLY (t) = 1;
4963         }
4964
4965       /* Any field that is volatile means variables of this type must be
4966          treated in some ways as volatile.  */
4967       if (TREE_THIS_VOLATILE (x))
4968         C_TYPE_FIELDS_VOLATILE (t) = 1;
4969
4970       /* Any field of nominal variable size implies structure is too.  */
4971       if (C_DECL_VARIABLE_SIZE (x))
4972         C_TYPE_VARIABLE_SIZE (t) = 1;
4973
4974       /* Detect invalid nested redefinition.  */
4975       if (TREE_TYPE (x) == t)
4976         error ("nested redefinition of `%s'",
4977                IDENTIFIER_POINTER (TYPE_NAME (t)));
4978
4979       /* Detect invalid bit-field size.  */
4980       if (DECL_INITIAL (x))
4981         STRIP_NOPS (DECL_INITIAL (x));
4982       if (DECL_INITIAL (x))
4983         {
4984           if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4985             constant_expression_warning (DECL_INITIAL (x));
4986           else
4987             {
4988               error_with_decl (x,
4989                                "bit-field `%s' width not an integer constant");
4990               DECL_INITIAL (x) = NULL;
4991             }
4992         }
4993
4994       /* Detect invalid bit-field type.  */
4995       if (DECL_INITIAL (x)
4996           && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4997           && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4998           && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4999         {
5000           error_with_decl (x, "bit-field `%s' has invalid type");
5001           DECL_INITIAL (x) = NULL;
5002         }
5003
5004       if (DECL_INITIAL (x) && pedantic
5005           && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5006           && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5007           && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != c_bool_type_node
5008           /* Accept an enum that's equivalent to int or unsigned int.  */
5009           && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5010                && (TYPE_PRECISION (TREE_TYPE (x))
5011                    == TYPE_PRECISION (integer_type_node))))
5012         pedwarn_with_decl (x, "bit-field `%s' type invalid in ISO C");
5013
5014       /* Detect and ignore out of range field width and process valid
5015          field widths.  */
5016       if (DECL_INITIAL (x))
5017         {
5018           int max_width
5019             = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == c_bool_type_node
5020                ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5021
5022           if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5023             error_with_decl (x, "negative width in bit-field `%s'");
5024           else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5025             pedwarn_with_decl (x, "width of `%s' exceeds its type");
5026           else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5027             error_with_decl (x, "zero width for bit-field `%s'");
5028           else
5029             {
5030               /* The test above has assured us that TREE_INT_CST_HIGH is 0.  */
5031               unsigned HOST_WIDE_INT width
5032                 = tree_low_cst (DECL_INITIAL (x), 1);
5033
5034               if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5035                   && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5036                                              TREE_UNSIGNED (TREE_TYPE (x)))
5037                       || (width
5038                           < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5039                                            TREE_UNSIGNED (TREE_TYPE (x))))))
5040                 warning_with_decl (x,
5041                                    "`%s' is narrower than values of its type");
5042
5043               DECL_SIZE (x) = bitsize_int (width);
5044               DECL_BIT_FIELD (x) = 1;
5045               SET_DECL_C_BIT_FIELD (x);
5046             }
5047         }
5048
5049       DECL_INITIAL (x) = 0;
5050
5051       /* Detect flexible array member in an invalid context.  */
5052       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5053           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5054           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5055           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5056         {
5057           if (TREE_CODE (t) == UNION_TYPE)
5058             error_with_decl (x, "flexible array member in union");
5059           else if (TREE_CHAIN (x) != NULL_TREE)
5060             error_with_decl (x, "flexible array member not at end of struct");
5061           else if (! saw_named_field)
5062             error_with_decl (x, "flexible array member in otherwise empty struct");
5063         }
5064
5065       if (pedantic && TREE_CODE (t) == RECORD_TYPE
5066           && flexible_array_type_p (TREE_TYPE (x)))
5067         pedwarn_with_decl (x, "invalid use of structure with flexible array member");
5068
5069       if (DECL_NAME (x))
5070         saw_named_field = 1;
5071     }
5072
5073   detect_field_duplicates (fieldlist);
5074
5075   /* Now we have the nearly final fieldlist.  Record it,
5076      then lay out the structure or union (including the fields).  */
5077
5078   TYPE_FIELDS (t) = fieldlist;
5079
5080   layout_type (t);
5081
5082   /* Delete all zero-width bit-fields from the fieldlist */
5083   {
5084     tree *fieldlistp = &fieldlist;
5085     while (*fieldlistp)
5086       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5087         *fieldlistp = TREE_CHAIN (*fieldlistp);
5088       else
5089         fieldlistp = &TREE_CHAIN (*fieldlistp);
5090   }
5091
5092   /* Now we have the truly final field list.
5093      Store it in this type and in the variants.  */
5094
5095   TYPE_FIELDS (t) = fieldlist;
5096
5097   /* If there are lots of fields, sort so we can look through them fast.
5098     We arbitrarily consider 16 or more elts to be "a lot".  */
5099
5100   {
5101     int len = 0;
5102
5103     for (x = fieldlist; x; x = TREE_CHAIN (x))
5104       {
5105         if (len > 15 || DECL_NAME (x) == NULL)
5106           break;
5107         len += 1;
5108       }
5109
5110     if (len > 15)
5111       {
5112         tree *field_array;
5113         struct lang_type *space;
5114         struct sorted_fields_type *space2;
5115         
5116         len += list_length (x);
5117   
5118         /* Use the same allocation policy here that make_node uses, to
5119           ensure that this lives as long as the rest of the struct decl.
5120           All decls in an inline function need to be saved.  */
5121   
5122         space = ggc_alloc (sizeof (struct lang_type));
5123         space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5124         
5125         len = 0;
5126         space->s = space2;
5127         field_array = &space2->elts[0];
5128         for (x = fieldlist; x; x = TREE_CHAIN (x))
5129           {
5130             field_array[len++] = x;
5131           
5132             /* if there is anonymous struct or union break out of the loop */
5133             if (DECL_NAME (x) == NULL)
5134               break;
5135           }
5136         /* found no anonymous struct/union add the TYPE_LANG_SPECIFIC. */
5137         if (x == NULL)
5138           {
5139             TYPE_LANG_SPECIFIC (t) = space;
5140             TYPE_LANG_SPECIFIC (t)->s->len = len;
5141             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5142             qsort (field_array, len, sizeof (tree), field_decl_cmp);
5143           }
5144       }
5145   }
5146   
5147   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5148     {
5149       TYPE_FIELDS (x) = TYPE_FIELDS (t);
5150       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5151       TYPE_ALIGN (x) = TYPE_ALIGN (t);
5152       TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5153     }
5154
5155   /* If this was supposed to be a transparent union, but we can't
5156      make it one, warn and turn off the flag.  */
5157   if (TREE_CODE (t) == UNION_TYPE
5158       && TYPE_TRANSPARENT_UNION (t)
5159       && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5160     {
5161       TYPE_TRANSPARENT_UNION (t) = 0;
5162       warning ("union cannot be made transparent");
5163     }
5164
5165   /* If this structure or union completes the type of any previous
5166      variable declaration, lay it out and output its rtl.  */
5167
5168   if (current_binding_level->incomplete_list != NULL_TREE)
5169     {
5170       tree prev = NULL_TREE;
5171
5172       for (x = current_binding_level->incomplete_list; x; x = TREE_CHAIN (x))
5173         {
5174           tree decl = TREE_VALUE (x);
5175
5176           if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5177               && TREE_CODE (decl) != TYPE_DECL)
5178             {
5179               layout_decl (decl, 0);
5180               /* This is a no-op in c-lang.c or something real in objc-act.c.  */
5181               if (c_dialect_objc ())
5182                 objc_check_decl (decl);
5183               rest_of_decl_compilation (decl, NULL, toplevel, 0);
5184               if (! toplevel)
5185                 expand_decl (decl);
5186               /* Unlink X from the incomplete list.  */
5187               if (prev)
5188                 TREE_CHAIN (prev) = TREE_CHAIN (x);
5189               else
5190                 current_binding_level->incomplete_list = TREE_CHAIN (x);
5191             }
5192           else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5193                    && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5194             {
5195               tree element = TREE_TYPE (decl);
5196               while (TREE_CODE (element) == ARRAY_TYPE)
5197                 element = TREE_TYPE (element);
5198               if (element == t)
5199                 {
5200                   layout_array_type (TREE_TYPE (decl));
5201                   if (TREE_CODE (decl) != TYPE_DECL)
5202                     {
5203                       layout_decl (decl, 0);
5204                       if (c_dialect_objc ())
5205                         objc_check_decl (decl);
5206                       rest_of_decl_compilation (decl, NULL, toplevel, 0);
5207                       if (! toplevel)
5208                         expand_decl (decl);
5209                     }
5210                   /* Unlink X from the incomplete list.  */
5211                   if (prev)
5212                     TREE_CHAIN (prev) = TREE_CHAIN (x);
5213                   else
5214                     current_binding_level->incomplete_list = TREE_CHAIN (x);
5215                 }
5216             }
5217         }
5218     }
5219
5220   /* Finish debugging output for this type.  */
5221   rest_of_type_compilation (t, toplevel);
5222
5223   return t;
5224 }
5225
5226 /* Lay out the type T, and its element type, and so on.  */
5227
5228 static void
5229 layout_array_type (tree t)
5230 {
5231   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5232     layout_array_type (TREE_TYPE (t));
5233   layout_type (t);
5234 }
5235 \f
5236 /* Begin compiling the definition of an enumeration type.
5237    NAME is its name (or null if anonymous).
5238    Returns the type object, as yet incomplete.
5239    Also records info about it so that build_enumerator
5240    may be used to declare the individual values as they are read.  */
5241
5242 tree
5243 start_enum (tree name)
5244 {
5245   tree enumtype = 0;
5246
5247   /* If this is the real definition for a previous forward reference,
5248      fill in the contents in the same object that used to be the
5249      forward reference.  */
5250
5251   if (name != 0)
5252     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5253
5254   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5255     {
5256       enumtype = make_node (ENUMERAL_TYPE);
5257       pushtag (name, enumtype);
5258     }
5259
5260   C_TYPE_BEING_DEFINED (enumtype) = 1;
5261
5262   if (TYPE_VALUES (enumtype) != 0)
5263     {
5264       /* This enum is a named one that has been declared already.  */
5265       error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5266
5267       /* Completely replace its old definition.
5268          The old enumerators remain defined, however.  */
5269       TYPE_VALUES (enumtype) = 0;
5270     }
5271
5272   enum_next_value = integer_zero_node;
5273   enum_overflow = 0;
5274
5275   if (flag_short_enums)
5276     TYPE_PACKED (enumtype) = 1;
5277
5278   return enumtype;
5279 }
5280
5281 /* After processing and defining all the values of an enumeration type,
5282    install their decls in the enumeration type and finish it off.
5283    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5284    and ATTRIBUTES are the specified attributes.
5285    Returns ENUMTYPE.  */
5286
5287 tree
5288 finish_enum (tree enumtype, tree values, tree attributes)
5289 {
5290   tree pair, tem;
5291   tree minnode = 0, maxnode = 0, enum_value_type;
5292   int precision, unsign;
5293   int toplevel = (global_binding_level == current_binding_level);
5294
5295   if (in_parm_level_p ())
5296     warning ("enum defined inside parms");
5297
5298   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5299
5300   /* Calculate the maximum value of any enumerator in this type.  */
5301
5302   if (values == error_mark_node)
5303     minnode = maxnode = integer_zero_node;
5304   else
5305     {
5306       minnode = maxnode = TREE_VALUE (values);
5307       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5308         {
5309           tree value = TREE_VALUE (pair);
5310           if (tree_int_cst_lt (maxnode, value))
5311             maxnode = value;
5312           if (tree_int_cst_lt (value, minnode))
5313             minnode = value;
5314         }
5315     }
5316
5317   /* Construct the final type of this enumeration.  It is the same
5318      as one of the integral types - the narrowest one that fits, except
5319      that normally we only go as narrow as int - and signed iff any of
5320      the values are negative.  */
5321   unsign = (tree_int_cst_sgn (minnode) >= 0);
5322   precision = MAX (min_precision (minnode, unsign),
5323                    min_precision (maxnode, unsign));
5324   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5325     {
5326       tree narrowest = c_common_type_for_size (precision, unsign);
5327       if (narrowest == 0)
5328         {
5329           warning ("enumeration values exceed range of largest integer");
5330           narrowest = long_long_integer_type_node;
5331         }
5332
5333       precision = TYPE_PRECISION (narrowest);
5334     }
5335   else
5336     precision = TYPE_PRECISION (integer_type_node);
5337
5338   if (precision == TYPE_PRECISION (integer_type_node))
5339     enum_value_type = c_common_type_for_size (precision, 0);
5340   else
5341     enum_value_type = enumtype;
5342
5343   TYPE_MIN_VALUE (enumtype) = minnode;
5344   TYPE_MAX_VALUE (enumtype) = maxnode;
5345   TYPE_PRECISION (enumtype) = precision;
5346   TREE_UNSIGNED (enumtype) = unsign;
5347   TYPE_SIZE (enumtype) = 0;
5348   layout_type (enumtype);
5349
5350   if (values != error_mark_node)
5351     {
5352       /* Change the type of the enumerators to be the enum type.  We
5353          need to do this irrespective of the size of the enum, for
5354          proper type checking.  Replace the DECL_INITIALs of the
5355          enumerators, and the value slots of the list, with copies
5356          that have the enum type; they cannot be modified in place
5357          because they may be shared (e.g.  integer_zero_node) Finally,
5358          change the purpose slots to point to the names of the decls.  */
5359       for (pair = values; pair; pair = TREE_CHAIN (pair))
5360         {
5361           tree enu = TREE_PURPOSE (pair);
5362
5363           TREE_TYPE (enu) = enumtype;
5364
5365           /* The ISO C Standard mandates enumerators to have type int,
5366              even though the underlying type of an enum type is
5367              unspecified.  Here we convert any enumerators that fit in
5368              an int to type int, to avoid promotions to unsigned types
5369              when comparing integers with enumerators that fit in the
5370              int range.  When -pedantic is given, build_enumerator()
5371              would have already taken care of those that don't fit.  */
5372           if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5373             DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5374           else
5375             DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5376
5377           TREE_PURPOSE (pair) = DECL_NAME (enu);
5378           TREE_VALUE (pair) = DECL_INITIAL (enu);
5379         }
5380
5381       TYPE_VALUES (enumtype) = values;
5382     }
5383
5384   /* Fix up all variant types of this enum type.  */
5385   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5386     {
5387       if (tem == enumtype)
5388         continue;
5389       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5390       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5391       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5392       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5393       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5394       TYPE_MODE (tem) = TYPE_MODE (enumtype);
5395       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5396       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5397       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5398       TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5399     }
5400
5401   /* Finish debugging output for this type.  */
5402   rest_of_type_compilation (enumtype, toplevel);
5403
5404   return enumtype;
5405 }
5406
5407 /* Build and install a CONST_DECL for one value of the
5408    current enumeration type (one that was begun with start_enum).
5409    Return a tree-list containing the CONST_DECL and its value.
5410    Assignment of sequential values by default is handled here.  */
5411
5412 tree
5413 build_enumerator (tree name, tree value)
5414 {
5415   tree decl, type;
5416
5417   /* Validate and default VALUE.  */
5418
5419   /* Remove no-op casts from the value.  */
5420   if (value)
5421     STRIP_TYPE_NOPS (value);
5422
5423   if (value != 0)
5424     {
5425       if (TREE_CODE (value) == INTEGER_CST)
5426         {
5427           value = default_conversion (value);
5428           constant_expression_warning (value);
5429         }
5430       else
5431         {
5432           error ("enumerator value for `%s' not integer constant",
5433                  IDENTIFIER_POINTER (name));
5434           value = 0;
5435         }
5436     }
5437
5438   /* Default based on previous value.  */
5439   /* It should no longer be possible to have NON_LVALUE_EXPR
5440      in the default.  */
5441   if (value == 0)
5442     {
5443       value = enum_next_value;
5444       if (enum_overflow)
5445         error ("overflow in enumeration values");
5446     }
5447
5448   if (pedantic && ! int_fits_type_p (value, integer_type_node))
5449     {
5450       pedwarn ("ISO C restricts enumerator values to range of `int'");
5451       value = convert (integer_type_node, value);
5452     }
5453
5454   /* Set basis for default for next value.  */
5455   enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5456   enum_overflow = tree_int_cst_lt (enum_next_value, value);
5457
5458   /* Now create a declaration for the enum value name.  */
5459
5460   type = TREE_TYPE (value);
5461   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5462                                       TYPE_PRECISION (integer_type_node)),
5463                                  (TYPE_PRECISION (type)
5464                                   >= TYPE_PRECISION (integer_type_node)
5465                                   && TREE_UNSIGNED (type)));
5466
5467   decl = build_decl (CONST_DECL, name, type);
5468   DECL_INITIAL (decl) = convert (type, value);
5469   pushdecl (decl);
5470
5471   return tree_cons (decl, value, NULL_TREE);
5472 }
5473
5474 \f
5475 /* Create the FUNCTION_DECL for a function definition.
5476    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5477    the declaration; they describe the function's name and the type it returns,
5478    but twisted together in a fashion that parallels the syntax of C.
5479
5480    This function creates a binding context for the function body
5481    as well as setting up the FUNCTION_DECL in current_function_decl.
5482
5483    Returns 1 on success.  If the DECLARATOR is not suitable for a function
5484    (it defines a datum instead), we return 0, which tells
5485    yyparse to report a parse error.  */
5486
5487 int
5488 start_function (tree declspecs, tree declarator, tree attributes)
5489 {
5490   tree decl1, old_decl;
5491   tree restype;
5492   int old_immediate_size_expand = immediate_size_expand;
5493
5494   current_function_returns_value = 0;  /* Assume, until we see it does.  */
5495   current_function_returns_null = 0;
5496   current_function_returns_abnormally = 0;
5497   warn_about_return_type = 0;
5498   current_extern_inline = 0;
5499   named_labels = 0;
5500   shadowed_labels = 0;
5501
5502   /* Don't expand any sizes in the return type of the function.  */
5503   immediate_size_expand = 0;
5504
5505   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5506
5507   /* If the declarator is not suitable for a function definition,
5508      cause a syntax error.  */
5509   if (decl1 == 0)
5510     {
5511       immediate_size_expand = old_immediate_size_expand;
5512       return 0;
5513     }
5514
5515   decl_attributes (&decl1, attributes, 0);
5516
5517   /* If #pragma weak was used, mark the decl weak now.  */
5518   if (current_binding_level == global_binding_level)
5519     maybe_apply_pragma_weak (decl1);
5520
5521   if (DECL_DECLARED_INLINE_P (decl1)
5522       && DECL_UNINLINABLE (decl1)
5523       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5524     warning_with_decl (decl1,
5525                        "inline function `%s' given attribute noinline");
5526
5527   announce_function (decl1);
5528
5529   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5530     {
5531       error ("return type is an incomplete type");
5532       /* Make it return void instead.  */
5533       TREE_TYPE (decl1)
5534         = build_function_type (void_type_node,
5535                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5536     }
5537
5538   if (warn_about_return_type)
5539     pedwarn_c99 ("return type defaults to `int'");
5540
5541   /* Save the parm names or decls from this function's declarator
5542      where store_parm_decls will find them.  */
5543   current_function_parms = last_function_parms;
5544   current_function_parm_tags = last_function_parm_tags;
5545
5546   /* Make the init_value nonzero so pushdecl knows this is not tentative.
5547      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
5548   DECL_INITIAL (decl1) = error_mark_node;
5549
5550   /* If this definition isn't a prototype and we had a prototype declaration
5551      before, copy the arg type info from that prototype.
5552      But not if what we had before was a builtin function.  */
5553   old_decl = lookup_name_current_level (DECL_NAME (decl1));
5554   if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5555       && !DECL_BUILT_IN (old_decl)
5556       && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5557           == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5558       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5559     {
5560       TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5561       current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5562     }
5563
5564   /* Optionally warn of old-fashioned def with no previous prototype.  */
5565   if (warn_strict_prototypes
5566       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5567       && C_DECL_ISNT_PROTOTYPE (old_decl))
5568     warning ("function declaration isn't a prototype");
5569   /* Optionally warn of any global def with no previous prototype.  */
5570   else if (warn_missing_prototypes
5571            && TREE_PUBLIC (decl1)
5572            && ! MAIN_NAME_P (DECL_NAME (decl1))
5573            && C_DECL_ISNT_PROTOTYPE (old_decl))
5574     warning_with_decl (decl1, "no previous prototype for `%s'");
5575   /* Optionally warn of any def with no previous prototype
5576      if the function has already been used.  */
5577   else if (warn_missing_prototypes
5578            && old_decl != 0 && TREE_USED (old_decl)
5579            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5580     warning_with_decl (decl1,
5581                        "`%s' was used with no prototype before its definition");
5582   /* Optionally warn of any global def with no previous declaration.  */
5583   else if (warn_missing_declarations
5584            && TREE_PUBLIC (decl1)
5585            && old_decl == 0
5586            && ! MAIN_NAME_P (DECL_NAME (decl1)))
5587     warning_with_decl (decl1, "no previous declaration for `%s'");
5588   /* Optionally warn of any def with no previous declaration
5589      if the function has already been used.  */
5590   else if (warn_missing_declarations
5591            && old_decl != 0 && TREE_USED (old_decl)
5592            && C_DECL_IMPLICIT (old_decl))
5593     warning_with_decl (decl1,
5594                        "`%s' was used with no declaration before its definition");
5595
5596   /* This is a definition, not a reference.
5597      So normally clear DECL_EXTERNAL.
5598      However, `extern inline' acts like a declaration
5599      except for defining how to inline.  So set DECL_EXTERNAL in that case.  */
5600   DECL_EXTERNAL (decl1) = current_extern_inline;
5601
5602   /* This function exists in static storage.
5603      (This does not mean `static' in the C sense!)  */
5604   TREE_STATIC (decl1) = 1;
5605
5606   /* A nested function is not global.  */
5607   if (current_function_decl != 0)
5608     TREE_PUBLIC (decl1) = 0;
5609
5610   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
5611   if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5612     {
5613       tree args;
5614       int argct = 0;
5615
5616       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5617           != integer_type_node)
5618         pedwarn_with_decl (decl1, "return type of `%s' is not `int'");
5619
5620       for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5621            args = TREE_CHAIN (args))
5622         {
5623           tree type = args ? TREE_VALUE (args) : 0;
5624
5625           if (type == void_type_node)
5626             break;
5627
5628           ++argct;
5629           switch (argct)
5630             {
5631             case 1:
5632               if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5633                 pedwarn_with_decl (decl1,
5634                                    "first argument of `%s' should be `int'");
5635               break;
5636
5637             case 2:
5638               if (TREE_CODE (type) != POINTER_TYPE
5639                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5640                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5641                       != char_type_node))
5642                 pedwarn_with_decl (decl1,
5643                                    "second argument of `%s' should be `char **'");
5644               break;
5645
5646             case 3:
5647               if (TREE_CODE (type) != POINTER_TYPE
5648                   || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5649                   || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5650                       != char_type_node))
5651                 pedwarn_with_decl (decl1,
5652                                    "third argument of `%s' should probably be `char **'");
5653               break;
5654             }
5655         }
5656
5657       /* It is intentional that this message does not mention the third
5658          argument because it's only mentioned in an appendix of the
5659          standard.  */
5660       if (argct > 0 && (argct < 2 || argct > 3))
5661         pedwarn_with_decl (decl1, "`%s' takes only zero or two arguments");
5662
5663       if (! TREE_PUBLIC (decl1))
5664         pedwarn_with_decl (decl1, "`%s' is normally a non-static function");
5665     }
5666
5667   /* Record the decl so that the function name is defined.
5668      If we already have a decl for this name, and it is a FUNCTION_DECL,
5669      use the old decl.  */
5670
5671   current_function_decl = pushdecl (decl1);
5672
5673   pushlevel (0);
5674   declare_parm_level ();
5675
5676   make_decl_rtl (current_function_decl, NULL);
5677
5678   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5679   /* Promote the value to int before returning it.  */
5680   if (c_promoting_integer_type_p (restype))
5681     {
5682       /* It retains unsignedness if not really getting wider.  */
5683       if (TREE_UNSIGNED (restype)
5684           && (TYPE_PRECISION (restype)
5685                   == TYPE_PRECISION (integer_type_node)))
5686         restype = unsigned_type_node;
5687       else
5688         restype = integer_type_node;
5689     }
5690   DECL_RESULT (current_function_decl)
5691     = build_decl (RESULT_DECL, NULL_TREE, restype);
5692
5693   /* If this fcn was already referenced via a block-scope `extern' decl
5694      (or an implicit decl), propagate certain information about the usage.  */
5695   if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5696     TREE_ADDRESSABLE (current_function_decl) = 1;
5697
5698   immediate_size_expand = old_immediate_size_expand;
5699
5700   start_fname_decls ();
5701
5702   return 1;
5703 }
5704 \f
5705 /* Store the parameter declarations into the current function declaration.
5706    This is called after parsing the parameter declarations, before
5707    digesting the body of the function.
5708
5709    For an old-style definition, modify the function's type
5710    to specify at least the number of arguments.  */
5711
5712 void
5713 store_parm_decls (void)
5714 {
5715   tree fndecl = current_function_decl;
5716   tree parm;
5717
5718   /* This is either a chain of PARM_DECLs (if a prototype was used)
5719      or a list of IDENTIFIER_NODEs (for an old-fashioned C definition).  */
5720   tree specparms = current_function_parms;
5721
5722   /* This is a list of types declared among parms in a prototype.  */
5723   tree parmtags = current_function_parm_tags;
5724
5725   /* This is a chain of PARM_DECLs from old-style parm declarations.  */
5726   tree parmdecls = getdecls ();
5727
5728   /* This is a chain of any other decls that came in among the parm
5729      declarations.  If a parm is declared with  enum {foo, bar} x;
5730      then CONST_DECLs for foo and bar are put here.  */
5731   tree nonparms = 0;
5732
5733   /* The function containing FNDECL, if any.  */
5734   tree context = decl_function_context (fndecl);
5735
5736   /* Nonzero if this definition is written with a prototype.  */
5737   int prototype = 0;
5738
5739   bool saved_warn_shadow = warn_shadow;
5740
5741   /* Don't re-emit shadow warnings.  */
5742   warn_shadow = false;
5743
5744   if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5745     {
5746       /* This case is when the function was defined with an ANSI prototype.
5747          The parms already have decls, so we need not do anything here
5748          except record them as in effect
5749          and complain if any redundant old-style parm decls were written.  */
5750
5751       tree next;
5752       tree others = 0;
5753
5754       prototype = 1;
5755
5756       if (parmdecls != 0)
5757         {
5758           tree decl, link;
5759
5760           error_with_decl (fndecl,
5761                            "parm types given both in parmlist and separately");
5762           /* Get rid of the erroneous decls; don't keep them on
5763              the list of parms, since they might not be PARM_DECLs.  */
5764           for (decl = current_binding_level->names;
5765                decl; decl = TREE_CHAIN (decl))
5766             if (DECL_NAME (decl))
5767               IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = 0;
5768           for (link = current_binding_level->shadowed;
5769                link; link = TREE_CHAIN (link))
5770             IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
5771           current_binding_level->names = 0;
5772           current_binding_level->shadowed = 0;
5773         }
5774
5775       specparms = nreverse (specparms);
5776       for (parm = specparms; parm; parm = next)
5777         {
5778           next = TREE_CHAIN (parm);
5779           if (TREE_CODE (parm) == PARM_DECL)
5780             {
5781               if (DECL_NAME (parm) == 0)
5782                 error_with_decl (parm, "parameter name omitted");
5783               else if (TREE_CODE (TREE_TYPE (parm)) != ERROR_MARK
5784                        && VOID_TYPE_P (TREE_TYPE (parm)))
5785                 {
5786                   error_with_decl (parm, "parameter `%s' declared void");
5787                   /* Change the type to error_mark_node so this parameter
5788                      will be ignored by assign_parms.  */
5789                   TREE_TYPE (parm) = error_mark_node;
5790                 }
5791               pushdecl (parm);
5792             }
5793           else
5794             {
5795               /* If we find an enum constant or a type tag,
5796                  put it aside for the moment.  */
5797               TREE_CHAIN (parm) = 0;
5798               others = chainon (others, parm);
5799             }
5800         }
5801
5802       /* Get the decls in their original chain order
5803          and record in the function.  */
5804       DECL_ARGUMENTS (fndecl) = getdecls ();
5805
5806       /* Now pushdecl the enum constants.  */
5807       for (parm = others; parm; parm = next)
5808         {
5809           next = TREE_CHAIN (parm);
5810           if (DECL_NAME (parm) == 0)
5811             ;
5812           else if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) == void_type_node)
5813             ;
5814           else if (TREE_CODE (parm) != PARM_DECL)
5815             pushdecl (parm);
5816         }
5817
5818       storetags (chainon (parmtags, gettags ()));
5819     }
5820   else
5821     {
5822       /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5823          each with a parm name as the TREE_VALUE.
5824
5825          PARMDECLS is a chain of declarations for parameters.
5826          Warning! It can also contain CONST_DECLs which are not parameters
5827          but are names of enumerators of any enum types
5828          declared among the parameters.
5829
5830          First match each formal parameter name with its declaration.
5831          Associate decls with the names and store the decls
5832          into the TREE_PURPOSE slots.  */
5833
5834       /* We use DECL_WEAK as a flag to show which parameters have been
5835          seen already since it is not used on PARM_DECL or CONST_DECL.  */
5836       for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5837         DECL_WEAK (parm) = 0;
5838
5839       for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5840         {
5841           tree tail, found = NULL;
5842
5843           if (TREE_VALUE (parm) == 0)
5844             {
5845               error_with_decl (fndecl,
5846                                "parameter name missing from parameter list");
5847               TREE_PURPOSE (parm) = 0;
5848               continue;
5849             }
5850
5851           /* See if any of the parmdecls specifies this parm by name.
5852              Ignore any enumerator decls.  */
5853           for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5854             if (DECL_NAME (tail) == TREE_VALUE (parm)
5855                 && TREE_CODE (tail) == PARM_DECL)
5856               {
5857                 found = tail;
5858                 break;
5859               }
5860
5861           /* If declaration already marked, we have a duplicate name.
5862              Complain, and don't use this decl twice.  */
5863           if (found && DECL_WEAK (found))
5864             {
5865               error_with_decl (found, "multiple parameters named `%s'");
5866               found = 0;
5867             }
5868
5869           /* If the declaration says "void", complain and ignore it.  */
5870           if (found && VOID_TYPE_P (TREE_TYPE (found)))
5871             {
5872               error_with_decl (found, "parameter `%s' declared void");
5873               TREE_TYPE (found) = integer_type_node;
5874               DECL_ARG_TYPE (found) = integer_type_node;
5875               layout_decl (found, 0);
5876             }
5877
5878           /* If no declaration found, default to int.  */
5879           if (!found)
5880             {
5881               found = build_decl (PARM_DECL, TREE_VALUE (parm),
5882                                   integer_type_node);
5883               DECL_ARG_TYPE (found) = TREE_TYPE (found);
5884               DECL_SOURCE_LOCATION (found) = DECL_SOURCE_LOCATION (fndecl);
5885               if (flag_isoc99)
5886                 pedwarn_with_decl (found, "type of `%s' defaults to `int'");
5887               else if (extra_warnings)
5888                 warning_with_decl (found, "type of `%s' defaults to `int'");
5889               pushdecl (found);
5890             }
5891
5892           TREE_PURPOSE (parm) = found;
5893
5894           /* Mark this decl as "already found".  */
5895           DECL_WEAK (found) = 1;
5896         }
5897
5898       /* Put anything which is on the parmdecls chain and which is
5899          not a PARM_DECL onto the list NONPARMS.  (The types of
5900          non-parm things which might appear on the list include
5901          enumerators and NULL-named TYPE_DECL nodes.) Complain about
5902          any actual PARM_DECLs not matched with any names.  */
5903
5904       nonparms = 0;
5905       for (parm = parmdecls; parm;)
5906         {
5907           tree next = TREE_CHAIN (parm);
5908           TREE_CHAIN (parm) = 0;
5909
5910           if (TREE_CODE (parm) != PARM_DECL)
5911             nonparms = chainon (nonparms, parm);
5912           else
5913             {
5914               /* Complain about args with incomplete types.  */
5915               if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5916                 {
5917                   error_with_decl (parm, "parameter `%s' has incomplete type");
5918                   TREE_TYPE (parm) = error_mark_node;
5919                 }
5920
5921               if (! DECL_WEAK (parm))
5922                 {
5923                   error_with_decl (parm,
5924                                    "declaration for parameter `%s' but no such parameter");
5925                   /* Pretend the parameter was not missing.
5926                      This gets us to a standard state and minimizes
5927                      further error messages.  */
5928                   specparms
5929                     = chainon (specparms,
5930                                tree_cons (parm, NULL_TREE, NULL_TREE));
5931                 }
5932             }
5933
5934           parm = next;
5935         }
5936
5937       /* Chain the declarations together in the order of the list of
5938          names.  Store that chain in the function decl, replacing the
5939          list of names.  */
5940       parm = specparms;
5941       DECL_ARGUMENTS (fndecl) = 0;
5942       {
5943         tree last;
5944         for (last = 0; parm; parm = TREE_CHAIN (parm))
5945           if (TREE_PURPOSE (parm))
5946             {
5947               if (last == 0)
5948                 DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
5949               else
5950                 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5951               last = TREE_PURPOSE (parm);
5952               TREE_CHAIN (last) = 0;
5953             }
5954       }
5955
5956       /* If there was a previous prototype,
5957          set the DECL_ARG_TYPE of each argument according to
5958          the type previously specified, and report any mismatches.  */
5959
5960       if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5961         {
5962           tree type;
5963           for (parm = DECL_ARGUMENTS (fndecl),
5964                type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5965                parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5966                                  != void_type_node));
5967                parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5968             {
5969               if (parm == 0 || type == 0
5970                   || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5971                 {
5972                   error ("number of arguments doesn't match prototype");
5973                   error ("%Hprototype declaration",
5974                          &current_function_prototype_locus);
5975                   break;
5976                 }
5977               /* Type for passing arg must be consistent with that
5978                  declared for the arg.  ISO C says we take the unqualified
5979                  type for parameters declared with qualified type.  */
5980               if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5981                                TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5982                                COMPARE_STRICT))
5983                 {
5984                   if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5985                       == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5986                     {
5987                       /* Adjust argument to match prototype.  E.g. a previous
5988                          `int foo(float);' prototype causes
5989                          `int foo(x) float x; {...}' to be treated like
5990                          `int foo(float x) {...}'.  This is particularly
5991                          useful for argument types like uid_t.  */
5992                       DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5993
5994                       if (PROMOTE_PROTOTYPES
5995                           && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5996                           && TYPE_PRECISION (TREE_TYPE (parm))
5997                           < TYPE_PRECISION (integer_type_node))
5998                         DECL_ARG_TYPE (parm) = integer_type_node;
5999
6000                       if (pedantic)
6001                         {
6002                           pedwarn ("promoted argument `%s' doesn't match prototype",
6003                                    IDENTIFIER_POINTER (DECL_NAME (parm)));
6004                           warning ("%Hprototype declaration",
6005                                    &current_function_prototype_locus);
6006                         }
6007                     }
6008                   else
6009                     {
6010                       error ("argument `%s' doesn't match prototype",
6011                              IDENTIFIER_POINTER (DECL_NAME (parm)));
6012                       error ("%Hprototype declaration",
6013                              &current_function_prototype_locus);
6014                     }
6015                 }
6016             }
6017           TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6018         }
6019
6020       /* Otherwise, create a prototype that would match.  */
6021
6022       else
6023         {
6024           tree actual = 0, last = 0, type;
6025
6026           for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6027             {
6028               type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6029               if (last)
6030                 TREE_CHAIN (last) = type;
6031               else
6032                 actual = type;
6033               last = type;
6034             }
6035           type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6036           if (last)
6037             TREE_CHAIN (last) = type;
6038           else
6039             actual = type;
6040
6041           /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6042              of the type of this function, but we need to avoid having this
6043              affect the types of other similarly-typed functions, so we must
6044              first force the generation of an identical (but separate) type
6045              node for the relevant function type.  The new node we create
6046              will be a variant of the main variant of the original function
6047              type.  */
6048
6049           TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6050
6051           TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6052         }
6053
6054       /* Now store the final chain of decls for the arguments
6055          as the decl-chain of the current lexical scope.
6056          Put the enumerators in as well, at the front so that
6057          DECL_ARGUMENTS is not modified.  */
6058
6059       storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
6060     }
6061
6062   /* Make sure the binding level for the top of the function body
6063      gets a BLOCK if there are any in the function.
6064      Otherwise, the dbx output is wrong.  */
6065
6066   keep_next_if_subblocks = 1;
6067
6068   /* Write a record describing this function definition to the prototypes
6069      file (if requested).  */
6070
6071   gen_aux_info_record (fndecl, 1, 0, prototype);
6072
6073   /* Initialize the RTL code for the function.  */
6074   init_function_start (fndecl);
6075
6076   /* Begin the statement tree for this function.  */
6077   begin_stmt_tree (&DECL_SAVED_TREE (current_function_decl));
6078
6079   /* If this is a nested function, save away the sizes of any
6080      variable-size types so that we can expand them when generating
6081      RTL.  */
6082   if (context)
6083     {
6084       tree t;
6085
6086       DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6087         = nreverse (get_pending_sizes ());
6088       for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6089            t;
6090            t = TREE_CHAIN (t))
6091         SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6092     }
6093
6094   /* This function is being processed in whole-function mode.  */
6095   cfun->x_whole_function_mode_p = 1;
6096
6097   /* Even though we're inside a function body, we still don't want to
6098      call expand_expr to calculate the size of a variable-sized array.
6099      We haven't necessarily assigned RTL to all variables yet, so it's
6100      not safe to try to expand expressions involving them.  */
6101   immediate_size_expand = 0;
6102   cfun->x_dont_save_pending_sizes_p = 1;
6103
6104   warn_shadow = saved_warn_shadow;
6105 }
6106 \f
6107 /* Finish up a function declaration and compile that function
6108    all the way to assembler language output.  The free the storage
6109    for the function definition.
6110
6111    This is called after parsing the body of the function definition.
6112
6113    NESTED is nonzero if the function being finished is nested in another.
6114    CAN_DEFER_P is nonzero if the function may be deferred.  */
6115
6116 void
6117 finish_function (int nested, int can_defer_p)
6118 {
6119   tree fndecl = current_function_decl;
6120
6121   /* When a function declaration is totally empty, e.g.
6122         void foo(void) { }
6123      (the argument list is irrelevant) the compstmt rule will not
6124      bother calling pushlevel/poplevel, which means we get here with
6125      the binding_level stack out of sync.  Detect this situation by
6126      noticing that the current_binding_level is still as
6127      store_parm_decls left it, and do a dummy push/pop to get back to
6128      consistency.  Note that the call to pushlevel does not actually
6129      push another binding level - see there for details.  */
6130   if (current_binding_level->parm_flag && keep_next_if_subblocks)
6131     {
6132       pushlevel (0);
6133       poplevel (1, 0, 1);
6134     }
6135
6136   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6137
6138   /* Must mark the RESULT_DECL as being in this function.  */
6139
6140   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6141
6142   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6143     {
6144       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6145           != integer_type_node)
6146         {
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'");
6151         }
6152       else
6153         {
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
6161         }
6162     }
6163
6164   finish_fname_decls ();
6165
6166   /* Tie off the statement tree for this function.  */
6167   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6168
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");
6183
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;
6190
6191   if (flag_unit_at_a_time && can_defer_p)
6192     {
6193       cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6194       current_function_decl = NULL;
6195       return;
6196     }
6197
6198   if (! nested)
6199     {
6200       /* Function is parsed.
6201          Generate RTL for the body of this function or defer
6202          it for later expansion.  */
6203       int uninlinable = 1;
6204
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)
6208         {
6209           current_function_decl = NULL;
6210           DECL_SAVED_TREE (fndecl) = NULL_TREE;
6211           return;
6212         }
6213
6214       if (flag_inline_trees)
6215         {
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);
6221
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))
6232             {
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;
6238             }
6239
6240           /* Then, inline any functions called in it.  */
6241           optimize_inline_calls (fndecl);
6242           timevar_pop (TV_INTEGRATION);
6243         }
6244
6245       c_expand_body (fndecl);
6246
6247       /* Keep the function body if it's needed for inlining or dumping.  */
6248       if (uninlinable && !dump_enabled_p (TDI_all))
6249         {
6250           /* Allow the body of the function to be garbage collected.  */
6251           DECL_SAVED_TREE (fndecl) = NULL_TREE;
6252         }
6253
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;
6258     }
6259 }
6260
6261 /* Generate the RTL for a deferred function FNDECL.  */
6262
6263 void
6264 c_expand_deferred_function (tree fndecl)
6265 {
6266   /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6267      function was deferred, e.g. in duplicate_decls.  */
6268   if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6269     {
6270       if (flag_inline_trees)
6271         {
6272           timevar_push (TV_INTEGRATION);
6273           optimize_inline_calls (fndecl);
6274           timevar_pop (TV_INTEGRATION);
6275         }
6276       c_expand_body (fndecl);
6277       current_function_decl = NULL;
6278     }
6279 }
6280
6281 /* Called to move the SAVE_EXPRs for parameter declarations in a
6282    nested function into the nested function.  DATA is really the
6283    nested FUNCTION_DECL.  */
6284
6285 static tree
6286 set_save_expr_context (tree *tp,
6287                        int *walk_subtrees,
6288                        void *data)
6289 {
6290   if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6291     SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6292   /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6293      circularity.  */
6294   else if (DECL_P (*tp))
6295     *walk_subtrees = 0;
6296
6297   return NULL_TREE;
6298 }
6299
6300 /* Generate the RTL for the body of FNDECL.  If NESTED_P is nonzero,
6301    then we are already in the process of generating RTL for another
6302    function.  If can_defer_p is zero, we won't attempt to defer the
6303    generation of RTL.  */
6304
6305 static void
6306 c_expand_body_1 (tree fndecl, int nested_p)
6307 {
6308   timevar_push (TV_EXPAND);
6309
6310   if (nested_p)
6311     {
6312       /* Make sure that we will evaluate variable-sized types involved
6313          in our function's type.  */
6314       expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6315       /* Squirrel away our current state.  */
6316       push_function_context ();
6317     }
6318
6319   /* Initialize the RTL code for the function.  */
6320   current_function_decl = fndecl;
6321   input_location = DECL_SOURCE_LOCATION (fndecl);
6322   init_function_start (fndecl);
6323
6324   /* This function is being processed in whole-function mode.  */
6325   cfun->x_whole_function_mode_p = 1;
6326
6327   /* Even though we're inside a function body, we still don't want to
6328      call expand_expr to calculate the size of a variable-sized array.
6329      We haven't necessarily assigned RTL to all variables yet, so it's
6330      not safe to try to expand expressions involving them.  */
6331   immediate_size_expand = 0;
6332   cfun->x_dont_save_pending_sizes_p = 1;
6333
6334   /* Set up parameters and prepare for return, for the function.  */
6335   expand_function_start (fndecl, 0);
6336
6337   /* If the function has a variably modified type, there may be
6338      SAVE_EXPRs in the parameter types.  Their context must be set to
6339      refer to this function; they cannot be expanded in the containing
6340      function.  */
6341   if (decl_function_context (fndecl)
6342       && variably_modified_type_p (TREE_TYPE (fndecl)))
6343     walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6344                NULL);
6345
6346   /* If this function is `main', emit a call to `__main'
6347      to run global initializers, etc.  */
6348   if (DECL_NAME (fndecl)
6349       && MAIN_NAME_P (DECL_NAME (fndecl))
6350       && DECL_CONTEXT (fndecl) == NULL_TREE)
6351     expand_main_function ();
6352
6353   /* Generate the RTL for this function.  */
6354   expand_stmt (DECL_SAVED_TREE (fndecl));
6355
6356   /* We hard-wired immediate_size_expand to zero above.
6357      expand_function_end will decrement this variable.  So, we set the
6358      variable to one here, so that after the decrement it will remain
6359      zero.  */
6360   immediate_size_expand = 1;
6361
6362   /* Allow language dialects to perform special processing.  */
6363   if (lang_expand_function_end)
6364     (*lang_expand_function_end) ();
6365
6366   /* Generate rtl for function exit.  */
6367   expand_function_end ();
6368
6369   /* If this is a nested function, protect the local variables in the stack
6370      above us from being collected while we're compiling this function.  */
6371   if (nested_p)
6372     ggc_push_context ();
6373
6374   /* Run the optimizers and output the assembler code for this function.  */
6375   rest_of_compilation (fndecl);
6376
6377   /* Undo the GC context switch.  */
6378   if (nested_p)
6379     ggc_pop_context ();
6380
6381   /* With just -Wextra, complain only if function returns both with
6382      and without a value.  */
6383   if (extra_warnings
6384       && current_function_returns_value
6385       && current_function_returns_null)
6386     warning ("this function may return with or without a value");
6387
6388   /* If requested, warn about function definitions where the function will
6389      return a value (usually of some struct or union type) which itself will
6390      take up a lot of stack space.  */
6391
6392   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6393     {
6394       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6395
6396       if (ret_type && TYPE_SIZE_UNIT (ret_type)
6397           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6398           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6399                                    larger_than_size))
6400         {
6401           unsigned int size_as_int
6402             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6403
6404           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6405             warning_with_decl (fndecl,
6406                                "size of return value of `%s' is %u bytes",
6407                                size_as_int);
6408           else
6409             warning_with_decl (fndecl,
6410                                "size of return value of `%s' is larger than %d bytes",
6411                                larger_than_size);
6412         }
6413     }
6414
6415   if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6416       && ! flag_inline_trees)
6417     {
6418       /* Stop pointing to the local nodes about to be freed.
6419          But DECL_INITIAL must remain nonzero so we know this
6420          was an actual function definition.
6421          For a nested function, this is done in c_pop_function_context.
6422          If rest_of_compilation set this to 0, leave it 0.  */
6423       if (DECL_INITIAL (fndecl) != 0)
6424         DECL_INITIAL (fndecl) = error_mark_node;
6425
6426       DECL_ARGUMENTS (fndecl) = 0;
6427     }
6428
6429   if (DECL_STATIC_CONSTRUCTOR (fndecl))
6430     {
6431       if (targetm.have_ctors_dtors)
6432         (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6433                                          DEFAULT_INIT_PRIORITY);
6434       else
6435         static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6436     }
6437
6438   if (DECL_STATIC_DESTRUCTOR (fndecl))
6439     {
6440       if (targetm.have_ctors_dtors)
6441         (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6442                                         DEFAULT_INIT_PRIORITY);
6443       else
6444         static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6445     }
6446
6447   if (nested_p)
6448     /* Return to the enclosing function.  */
6449     pop_function_context ();
6450   timevar_pop (TV_EXPAND);
6451 }
6452
6453 /* Like c_expand_body_1 but only for unnested functions.  */
6454
6455 void
6456 c_expand_body (tree fndecl)
6457 {
6458   c_expand_body_1 (fndecl, 0);
6459 }
6460 \f
6461 /* Check the declarations given in a for-loop for satisfying the C99
6462    constraints.  */
6463 void
6464 check_for_loop_decls (void)
6465 {
6466   tree t;
6467
6468   if (!flag_isoc99)
6469     {
6470       /* If we get here, declarations have been used in a for loop without
6471          the C99 for loop scope.  This doesn't make much sense, so don't
6472          allow it.  */
6473       error ("`for' loop initial declaration used outside C99 mode");
6474       return;
6475     }
6476   /* C99 subclause 6.8.5 paragraph 3:
6477
6478        [#3]  The  declaration  part  of  a for statement shall only
6479        declare identifiers for objects having storage class auto or
6480        register.
6481
6482      It isn't clear whether, in this sentence, "identifiers" binds to
6483      "shall only declare" or to "objects" - that is, whether all identifiers
6484      declared must be identifiers for objects, or whether the restriction
6485      only applies to those that are.  (A question on this in comp.std.c
6486      in November 2000 received no answer.)  We implement the strictest
6487      interpretation, to avoid creating an extension which later causes
6488      problems.  */
6489
6490   for (t = gettags (); t; t = TREE_CHAIN (t))
6491     {
6492       if (TREE_PURPOSE (t) != 0)
6493         {
6494           enum tree_code code = TREE_CODE (TREE_VALUE (t));
6495
6496           if (code == RECORD_TYPE)
6497             error ("`struct %s' declared in `for' loop initial declaration",
6498                    IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6499           else if (code == UNION_TYPE)
6500             error ("`union %s' declared in `for' loop initial declaration",
6501                    IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6502           else
6503             error ("`enum %s' declared in `for' loop initial declaration",
6504                    IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6505         }
6506     }
6507
6508   for (t = getdecls (); t; t = TREE_CHAIN (t))
6509     {
6510       if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6511         error_with_decl (t, "declaration of non-variable `%s' in `for' loop initial declaration");
6512       else if (TREE_STATIC (t))
6513         error_with_decl (t, "declaration of static variable `%s' in `for' loop initial declaration");
6514       else if (DECL_EXTERNAL (t))
6515         error_with_decl (t, "declaration of `extern' variable `%s' in `for' loop initial declaration");
6516     }
6517 }
6518 \f
6519 /* Save and restore the variables in this file and elsewhere
6520    that keep track of the progress of compilation of the current function.
6521    Used for nested functions.  */
6522
6523 struct language_function GTY(())
6524 {
6525   struct c_language_function base;
6526   tree named_labels;
6527   tree shadowed_labels;
6528   int returns_value;
6529   int returns_null;
6530   int returns_abnormally;
6531   int warn_about_return_type;
6532   int extern_inline;
6533   struct binding_level *binding_level;
6534 };
6535
6536 /* Save and reinitialize the variables
6537    used during compilation of a C function.  */
6538
6539 void
6540 c_push_function_context (struct function *f)
6541 {
6542   struct language_function *p;
6543   p = ((struct language_function *)
6544        ggc_alloc (sizeof (struct language_function)));
6545   f->language = p;
6546
6547   p->base.x_stmt_tree = c_stmt_tree;
6548   p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6549   p->named_labels = named_labels;
6550   p->shadowed_labels = shadowed_labels;
6551   p->returns_value = current_function_returns_value;
6552   p->returns_null = current_function_returns_null;
6553   p->returns_abnormally = current_function_returns_abnormally;
6554   p->warn_about_return_type = warn_about_return_type;
6555   p->extern_inline = current_extern_inline;
6556   p->binding_level = current_binding_level;
6557 }
6558
6559 /* Restore the variables used during compilation of a C function.  */
6560
6561 void
6562 c_pop_function_context (struct function *f)
6563 {
6564   struct language_function *p = f->language;
6565   tree link;
6566
6567   /* Bring back all the labels that were shadowed.  */
6568   for (link = shadowed_labels; link; link = TREE_CHAIN (link))
6569     if (DECL_NAME (TREE_VALUE (link)) != 0)
6570       IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
6571         = TREE_VALUE (link);
6572
6573   if (DECL_SAVED_INSNS (current_function_decl) == 0
6574       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6575     {
6576       /* Stop pointing to the local nodes about to be freed.  */
6577       /* But DECL_INITIAL must remain nonzero so we know this
6578          was an actual function definition.  */
6579       DECL_INITIAL (current_function_decl) = error_mark_node;
6580       DECL_ARGUMENTS (current_function_decl) = 0;
6581     }
6582
6583   c_stmt_tree = p->base.x_stmt_tree;
6584   c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6585   named_labels = p->named_labels;
6586   shadowed_labels = p->shadowed_labels;
6587   current_function_returns_value = p->returns_value;
6588   current_function_returns_null = p->returns_null;
6589   current_function_returns_abnormally = p->returns_abnormally;
6590   warn_about_return_type = p->warn_about_return_type;
6591   current_extern_inline = p->extern_inline;
6592   current_binding_level = p->binding_level;
6593
6594   f->language = NULL;
6595 }
6596
6597 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
6598
6599 void
6600 c_dup_lang_specific_decl (tree decl)
6601 {
6602   struct lang_decl *ld;
6603
6604   if (!DECL_LANG_SPECIFIC (decl))
6605     return;
6606
6607   ld = (struct lang_decl *) ggc_alloc (sizeof (struct lang_decl));
6608   memcpy ((char *) ld, (char *) DECL_LANG_SPECIFIC (decl),
6609           sizeof (struct lang_decl));
6610   DECL_LANG_SPECIFIC (decl) = ld;
6611 }
6612
6613 /* The functions below are required for functionality of doing
6614    function at once processing in the C front end. Currently these
6615    functions are not called from anywhere in the C front end, but as
6616    these changes continue, that will change.  */
6617
6618 /* Returns nonzero if the current statement is a full expression,
6619    i.e. temporaries created during that statement should be destroyed
6620    at the end of the statement.  */
6621
6622 int
6623 stmts_are_full_exprs_p (void)
6624 {
6625   return 0;
6626 }
6627
6628 /* Returns the stmt_tree (if any) to which statements are currently
6629    being added.  If there is no active statement-tree, NULL is
6630    returned.  */
6631
6632 stmt_tree
6633 current_stmt_tree (void)
6634 {
6635   return &c_stmt_tree;
6636 }
6637
6638 /* Returns the stack of SCOPE_STMTs for the current function.  */
6639
6640 tree *
6641 current_scope_stmt_stack (void)
6642 {
6643   return &c_scope_stmt_stack;
6644 }
6645
6646 /* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
6647    C.  */
6648
6649 int
6650 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6651 {
6652   return 0;
6653 }
6654
6655 /* Dummy function in place of callback used by C++.  */
6656
6657 void
6658 extract_interface_info (void)
6659 {
6660 }
6661
6662 /* Return a new COMPOUND_STMT, after adding it to the current
6663    statement tree.  */
6664
6665 tree
6666 c_begin_compound_stmt (void)
6667 {
6668   tree stmt;
6669
6670   /* Create the COMPOUND_STMT.  */
6671   stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6672
6673   return stmt;
6674 }
6675
6676 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6677    common code.  */
6678
6679 void
6680 c_expand_decl_stmt (tree t)
6681 {
6682   tree decl = DECL_STMT_DECL (t);
6683
6684   /* Expand nested functions.  */
6685   if (TREE_CODE (decl) == FUNCTION_DECL
6686       && DECL_CONTEXT (decl) == current_function_decl
6687       && DECL_SAVED_TREE (decl))
6688     c_expand_body_1 (decl, 1);
6689 }
6690
6691 /* Return the global value of T as a symbol.  */
6692
6693 tree
6694 identifier_global_value (tree t)
6695 {
6696   tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6697   if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6698     return decl;
6699
6700   /* Shadowed by something else; find the true global value.  */
6701   for (decl = global_binding_level->names; decl; decl = TREE_CHAIN (decl))
6702     if (DECL_NAME (decl) == t)
6703       return decl;
6704
6705   /* Only local values for this decl.  */
6706   return 0;
6707 }
6708
6709 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
6710    otherwise the name is found in ridpointers from RID_INDEX.  */
6711
6712 void
6713 record_builtin_type (enum rid rid_index, const char *name, tree type)
6714 {
6715   tree id;
6716   if (name == 0)
6717     id = ridpointers[(int) rid_index];
6718   else
6719     id = get_identifier (name);
6720   pushdecl (build_decl (TYPE_DECL, id, type));
6721 }
6722
6723 /* Build the void_list_node (void_type_node having been created).  */
6724 tree
6725 build_void_list_node (void)
6726 {
6727   tree t = build_tree_list (NULL_TREE, void_type_node);
6728   return t;
6729 }
6730
6731 /* Return something to represent absolute declarators containing a *.
6732    TARGET is the absolute declarator that the * contains.
6733    TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6734    to apply to the pointer type, represented as identifiers, possible mixed
6735    with attributes.
6736
6737    We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6738    if attributes are present) and whose type is the modifier list.  */
6739
6740 tree
6741 make_pointer_declarator (tree type_quals_attrs, tree target)
6742 {
6743   tree quals, attrs;
6744   tree itarget = target;
6745   split_specs_attrs (type_quals_attrs, &quals, &attrs);
6746   if (attrs != NULL_TREE)
6747     itarget = tree_cons (attrs, target, NULL_TREE);
6748   return build1 (INDIRECT_REF, quals, itarget);
6749 }
6750
6751 /* Hash and equality functions for link_hash_table: key off
6752    DECL_ASSEMBLER_NAME.  */
6753
6754 static hashval_t
6755 link_hash_hash (const void *x_p)
6756 {
6757   tree x = (tree)x_p;
6758   return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6759 }
6760
6761 static int
6762 link_hash_eq (const void *x1_p, const void *x2_p)
6763 {
6764   tree x1 = (tree)x1_p;
6765   tree x2 = (tree)x2_p;
6766   return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6767 }
6768
6769 /* Propagate information between definitions and uses between multiple
6770    translation units in TU_LIST based on linkage rules.  */
6771
6772 void
6773 merge_translation_unit_decls (void)
6774 {
6775   const tree tu_list = current_file_decl;
6776   tree tu;
6777   tree decl;
6778   htab_t link_hash_table;
6779   tree block;
6780   
6781   /* Create the BLOCK that poplevel would have created, but don't
6782      actually call poplevel since that's expensive.  */
6783   block = make_node (BLOCK);
6784   BLOCK_VARS (block) = current_binding_level->names;
6785   TREE_USED (block) = 1;
6786   DECL_INITIAL (current_file_decl) = block;
6787
6788   /* If only one translation unit seen, no copying necessary.  */
6789   if (TREE_CHAIN (tu_list) == NULL_TREE)
6790     return;
6791
6792   link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6793
6794   /* Enter any actual definitions into the hash table.  */
6795   for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6796     for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6797       if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6798         {
6799           PTR *slot;
6800           slot = htab_find_slot (link_hash_table, decl, INSERT);
6801
6802           /* If we've already got a definition, work out which one is
6803              the real one, put it into the hash table, and make the
6804              other one DECL_EXTERNAL.  This is important to avoid
6805              putting out two definitions of the same symbol in the
6806              assembly output.  */
6807           if (*slot != NULL)
6808             {
6809               tree old_decl = (tree) *slot;
6810
6811               /* If this is weak or common or whatever, suppress it
6812                  in favour of the other definition.  */
6813               if (DECL_WEAK (decl))
6814                 DECL_EXTERNAL (decl) = 1;
6815               else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6816                 DECL_EXTERNAL (old_decl) = 1;
6817               else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6818                 DECL_EXTERNAL (decl) = 1;
6819               else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6820                 DECL_EXTERNAL (old_decl) = 1;
6821               
6822               if (DECL_EXTERNAL (decl))
6823                 {
6824                   DECL_INITIAL (decl) = NULL_TREE;
6825                   DECL_COMMON (decl) = 0;
6826                   DECL_ONE_ONLY (decl) = 0;
6827                   DECL_WEAK (decl) = 0;
6828                 }
6829               else if (DECL_EXTERNAL (old_decl))
6830                 {
6831                   DECL_INITIAL (old_decl) = NULL_TREE;
6832                   DECL_COMMON (old_decl) = 0;
6833                   DECL_ONE_ONLY (old_decl) = 0;
6834                   DECL_WEAK (old_decl) = 0;
6835                   *slot = decl;
6836                 }
6837               else
6838                 {
6839                   error_with_decl (decl, "redefinition of global `%s'");
6840                   error_with_decl (old_decl, "`%s' previously defined here");
6841                 }
6842             }
6843           else
6844             *slot = decl;
6845         }
6846
6847   /* Now insert the desired information from all the definitions
6848      into any plain declarations.  */
6849   for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6850     for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6851       if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6852         {
6853           tree global_decl;
6854           global_decl = (tree) htab_find (link_hash_table, decl);
6855           
6856           if (! global_decl)
6857             continue;
6858           
6859           /* Print any appropriate error messages, and partially merge
6860              the decls.  */
6861           (void) duplicate_decls (decl, global_decl, true, true);
6862         }
6863
6864   htab_delete (link_hash_table);
6865 }
6866
6867 /* Perform final processing on file-scope data.  */
6868
6869 void
6870 c_write_global_declarations(void)
6871 {
6872   tree link;
6873   
6874   for (link = current_file_decl; link; link = TREE_CHAIN (link))
6875     {
6876       tree globals = BLOCK_VARS (DECL_INITIAL (link));
6877       int len = list_length (globals);
6878       tree *vec = (tree *) xmalloc (sizeof (tree) * len);
6879       int i;
6880       tree decl;
6881       
6882       /* Process the decls in reverse order--earliest first.
6883          Put them into VEC from back to front, then take out from front.  */
6884       
6885       for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6886         vec[len - i - 1] = decl;
6887       
6888       wrapup_global_declarations (vec, len);
6889       
6890       check_global_declarations (vec, len);
6891       
6892       /* Clean up.  */
6893       free (vec);
6894     }
6895 }
6896
6897 /* Reset the parser's state in preparation for a new file.  */
6898
6899 void
6900 c_reset_state (void)
6901 {
6902   tree link;
6903   tree file_scope_decl;
6904   
6905   /* Pop the global binding level.  */
6906   if (current_binding_level != global_binding_level)
6907       current_binding_level = global_binding_level;
6908   file_scope_decl = current_file_decl;
6909   DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6910   truly_local_externals = NULL_TREE;
6911
6912   /* Start a new global binding level.  */
6913   pushlevel (0);
6914   global_binding_level = current_binding_level;
6915   current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6916   TREE_CHAIN (current_file_decl) = file_scope_decl;
6917
6918   /* Reintroduce the global declarations.  */
6919   for (link = builtin_decls; link; link = TREE_CHAIN (link))
6920     pushdecl (copy_node (link));
6921 }
6922
6923 #include "gt-c-decl.h"