OSDN Git Service

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