OSDN Git Service

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