OSDN Git Service

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