OSDN Git Service

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