OSDN Git Service

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