OSDN Git Service

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