OSDN Git Service

2009-05-07 Paolo Bonzini <bonzini@gnu.org>
[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, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "gimple.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
59 #include "cgraph.h"
60 #include "hashtab.h"
61 #include "libfuncs.h"
62 #include "except.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
65 #include "gimple.h"
66 #include "plugin.h"
67
68 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
69 enum decl_context
70 { NORMAL,                       /* Ordinary declaration */
71   FUNCDEF,                      /* Function definition */
72   PARM,                         /* Declaration of parm before function body */
73   FIELD,                        /* Declaration inside struct or union */
74   TYPENAME};                    /* Typename (inside cast or sizeof)  */
75
76 /* States indicating how grokdeclarator() should handle declspecs marked
77    with __attribute__((deprecated)).  An object declared as
78    __attribute__((deprecated)) suppresses warnings of uses of other
79    deprecated items.  */
80
81 enum deprecated_states {
82   DEPRECATED_NORMAL,
83   DEPRECATED_SUPPRESS
84 };
85
86 \f
87 /* Nonzero if we have seen an invalid cross reference
88    to a struct, union, or enum, but not yet printed the message.  */
89 tree pending_invalid_xref;
90
91 /* File and line to appear in the eventual error message.  */
92 location_t pending_invalid_xref_location;
93
94 /* True means we've initialized exception handling.  */
95 bool c_eh_initialized_p;
96
97 /* The file and line that the prototype came from if this is an
98    old-style definition; used for diagnostics in
99    store_parm_decls_oldstyle.  */
100
101 static location_t current_function_prototype_locus;
102
103 /* Whether this prototype was built-in.  */
104
105 static bool current_function_prototype_built_in;
106
107 /* The argument type information of this prototype.  */
108
109 static tree current_function_prototype_arg_types;
110
111 /* The argument information structure for the function currently being
112    defined.  */
113
114 static struct c_arg_info *current_function_arg_info;
115
116 /* The obstack on which parser and related data structures, which are
117    not live beyond their top-level declaration or definition, are
118    allocated.  */
119 struct obstack parser_obstack;
120
121 /* The current statement tree.  */
122
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
124
125 /* State saving variables.  */
126 tree c_break_label;
127 tree c_cont_label;
128
129 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
130    included in this invocation.  Note that the current translation
131    unit is not included in this list.  */
132
133 static GTY(()) tree all_translation_units;
134
135 /* A list of decls to be made automatically visible in each file scope.  */
136 static GTY(()) tree visible_builtins;
137
138 /* Set to 0 at beginning of a function definition, set to 1 if
139    a return statement that specifies a return value is seen.  */
140
141 int current_function_returns_value;
142
143 /* Set to 0 at beginning of a function definition, set to 1 if
144    a return statement with no argument is seen.  */
145
146 int current_function_returns_null;
147
148 /* Set to 0 at beginning of a function definition, set to 1 if
149    a call to a noreturn function is seen.  */
150
151 int current_function_returns_abnormally;
152
153 /* Set to nonzero by `grokdeclarator' for a function
154    whose return type is defaulted, if warnings for this are desired.  */
155
156 static int warn_about_return_type;
157
158 /* Nonzero when the current toplevel function contains a declaration
159    of a nested function which is never defined.  */
160
161 static bool undef_nested_function;
162
163 /* True means global_bindings_p should return false even if the scope stack
164    says we are in file scope.  */
165 bool c_override_global_bindings_to_false;
166
167 \f
168 /* Each c_binding structure describes one binding of an identifier to
169    a decl.  All the decls in a scope - irrespective of namespace - are
170    chained together by the ->prev field, which (as the name implies)
171    runs in reverse order.  All the decls in a given namespace bound to
172    a given identifier are chained by the ->shadowed field, which runs
173    from inner to outer scopes.
174
175    The ->decl field usually points to a DECL node, but there are two
176    exceptions.  In the namespace of type tags, the bound entity is a
177    RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
178    identifier is encountered, it is bound to error_mark_node to
179    suppress further errors about that identifier in the current
180    function.
181
182    The ->type field stores the type of the declaration in this scope;
183    if NULL, the type is the type of the ->decl field.  This is only of
184    relevance for objects with external or internal linkage which may
185    be redeclared in inner scopes, forming composite types that only
186    persist for the duration of those scopes.  In the external scope,
187    this stores the composite of all the types declared for this
188    object, visible or not.  The ->inner_comp field (used only at file
189    scope) stores whether an incomplete array type at file scope was
190    completed at an inner scope to an array size other than 1.
191
192    The depth field is copied from the scope structure that holds this
193    decl.  It is used to preserve the proper ordering of the ->shadowed
194    field (see bind()) and also for a handful of special-case checks.
195    Finally, the invisible bit is true for a decl which should be
196    ignored for purposes of normal name lookup, and the nested bit is
197    true for a decl that's been bound a second time in an inner scope;
198    in all such cases, the binding in the outer scope will have its
199    invisible bit true.  */
200
201 struct GTY((chain_next ("%h.prev"))) c_binding {
202   tree decl;                    /* the decl bound */
203   tree type;                    /* the type in this scope */
204   tree id;                      /* the identifier it's bound to */
205   struct c_binding *prev;       /* the previous decl in this scope */
206   struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
207   unsigned int depth : 28;      /* depth of this scope */
208   BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
209   BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
210   BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211   /* one free bit */
212   location_t locus;             /* location for nested bindings */
213 };
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
218
219 #define I_SYMBOL_BINDING(node) \
220   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222  (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
223
224 #define I_TAG_BINDING(node) \
225   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227  (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
228
229 #define I_LABEL_BINDING(node) \
230   (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232  (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
233
234 /* Each C symbol points to three linked lists of c_binding structures.
235    These describe the values of the identifier in the three different
236    namespaces defined by the language.  */
237
238 struct GTY(()) lang_identifier {
239   struct c_common_identifier common_id;
240   struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
241   struct c_binding *tag_binding;    /* struct/union/enum tags */
242   struct c_binding *label_binding;  /* labels */
243 };
244
245 /* Validate c-lang.c's assumptions.  */
246 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
247 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
248
249 /* The resulting tree type.  */
250
251 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
252        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))")))  lang_tree_node
253  {
254   union tree_node GTY ((tag ("0"),
255                         desc ("tree_node_structure (&%h)")))
256     generic;
257   struct lang_identifier GTY ((tag ("1"))) identifier;
258 };
259
260 /* Each c_scope structure describes the complete contents of one
261    scope.  Four scopes are distinguished specially: the innermost or
262    current scope, the innermost function scope, the file scope (always
263    the second to outermost) and the outermost or external scope.
264
265    Most declarations are recorded in the current scope.
266
267    All normal label declarations are recorded in the innermost
268    function scope, as are bindings of undeclared identifiers to
269    error_mark_node.  (GCC permits nested functions as an extension,
270    hence the 'innermost' qualifier.)  Explicitly declared labels
271    (using the __label__ extension) appear in the current scope.
272
273    Being in the file scope (current_scope == file_scope) causes
274    special behavior in several places below.  Also, under some
275    conditions the Objective-C front end records declarations in the
276    file scope even though that isn't the current scope.
277
278    All declarations with external linkage are recorded in the external
279    scope, even if they aren't visible there; this models the fact that
280    such declarations are visible to the entire program, and (with a
281    bit of cleverness, see pushdecl) allows diagnosis of some violations
282    of C99 6.2.2p7 and 6.2.7p2:
283
284      If, within the same translation unit, the same identifier appears
285      with both internal and external linkage, the behavior is
286      undefined.
287
288      All declarations that refer to the same object or function shall
289      have compatible type; otherwise, the behavior is undefined.
290
291    Initially only the built-in declarations, which describe compiler
292    intrinsic functions plus a subset of the standard library, are in
293    this scope.
294
295    The order of the blocks list matters, and it is frequently appended
296    to.  To avoid having to walk all the way to the end of the list on
297    each insertion, or reverse the list later, we maintain a pointer to
298    the last list entry.  (FIXME: It should be feasible to use a reversed
299    list here.)
300
301    The bindings list is strictly in reverse order of declarations;
302    pop_scope relies on this.  */
303
304
305 struct GTY((chain_next ("%h.outer"))) c_scope {
306   /* The scope containing this one.  */
307   struct c_scope *outer;
308
309   /* The next outermost function scope.  */
310   struct c_scope *outer_function;
311
312   /* All bindings in this scope.  */
313   struct c_binding *bindings;
314
315   /* For each scope (except the global one), a chain of BLOCK nodes
316      for all the scopes that were entered and exited one level down.  */
317   tree blocks;
318   tree blocks_last;
319
320   /* The depth of this scope.  Used to keep the ->shadowed chain of
321      bindings sorted innermost to outermost.  */
322   unsigned int depth : 28;
323
324   /* True if we are currently filling this scope with parameter
325      declarations.  */
326   BOOL_BITFIELD parm_flag : 1;
327
328   /* True if we saw [*] in this scope.  Used to give an error messages
329      if these appears in a function definition.  */
330   BOOL_BITFIELD had_vla_unspec : 1;
331
332   /* True if we already complained about forward parameter decls
333      in this scope.  This prevents double warnings on
334      foo (int a; int b; ...)  */
335   BOOL_BITFIELD warned_forward_parm_decls : 1;
336
337   /* True if this is the outermost block scope of a function body.
338      This scope contains the parameters, the local variables declared
339      in the outermost block, and all the labels (except those in
340      nested functions, or declared at block scope with __label__).  */
341   BOOL_BITFIELD function_body : 1;
342
343   /* True means make a BLOCK for this scope no matter what.  */
344   BOOL_BITFIELD keep : 1;
345 };
346
347 /* The scope currently in effect.  */
348
349 static GTY(()) struct c_scope *current_scope;
350
351 /* The innermost function scope.  Ordinary (not explicitly declared)
352    labels, bindings to error_mark_node, and the lazily-created
353    bindings of __func__ and its friends get this scope.  */
354
355 static GTY(()) struct c_scope *current_function_scope;
356
357 /* The C file scope.  This is reset for each input translation unit.  */
358
359 static GTY(()) struct c_scope *file_scope;
360
361 /* The outermost scope.  This is used for all declarations with
362    external linkage, and only these, hence the name.  */
363
364 static GTY(()) struct c_scope *external_scope;
365
366 /* A chain of c_scope structures awaiting reuse.  */
367
368 static GTY((deletable)) struct c_scope *scope_freelist;
369
370 /* A chain of c_binding structures awaiting reuse.  */
371
372 static GTY((deletable)) struct c_binding *binding_freelist;
373
374 /* Append VAR to LIST in scope SCOPE.  */
375 #define SCOPE_LIST_APPEND(scope, list, decl) do {       \
376   struct c_scope *s_ = (scope);                         \
377   tree d_ = (decl);                                     \
378   if (s_->list##_last)                                  \
379     BLOCK_CHAIN (s_->list##_last) = d_;                 \
380   else                                                  \
381     s_->list = d_;                                      \
382   s_->list##_last = d_;                                 \
383 } while (0)
384
385 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
386 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
387   struct c_scope *t_ = (tscope);                                \
388   struct c_scope *f_ = (fscope);                                \
389   if (t_->to##_last)                                            \
390     BLOCK_CHAIN (t_->to##_last) = f_->from;                     \
391   else                                                          \
392     t_->to = f_->from;                                          \
393   t_->to##_last = f_->from##_last;                              \
394 } while (0)
395
396 /* A c_inline_static structure stores details of a static identifier
397    referenced in a definition of a function that may be an inline
398    definition if no subsequent declaration of that function uses
399    "extern" or does not use "inline".  */
400
401 struct GTY((chain_next ("%h.next"))) c_inline_static {
402   /* The location for a diagnostic.  */
403   location_t location;
404
405   /* The function that may be an inline definition.  */
406   tree function;
407
408   /* The object or function referenced.  */
409   tree static_decl;
410
411   /* What sort of reference this is.  */
412   enum c_inline_static_type type;
413
414   /* The next such structure or NULL.  */
415   struct c_inline_static *next;
416 };
417
418 /* List of static identifiers used or referenced in functions that may
419    be inline definitions.  */
420 static GTY(()) struct c_inline_static *c_inline_statics;
421
422 /* True means unconditionally make a BLOCK for the next scope pushed.  */
423
424 static bool keep_next_level_flag;
425
426 /* True means the next call to push_scope will be the outermost scope
427    of a function body, so do not push a new scope, merely cease
428    expecting parameter decls.  */
429
430 static bool next_is_function_body;
431
432 /* Forward declarations.  */
433 static tree lookup_name_in_scope (tree, struct c_scope *);
434 static tree c_make_fname_decl (tree, int);
435 static tree grokdeclarator (const struct c_declarator *,
436                             struct c_declspecs *,
437                             enum decl_context, bool, tree *, tree *, tree *,
438                             bool *, enum deprecated_states);
439 static tree grokparms (struct c_arg_info *, bool);
440 static void layout_array_type (tree);
441 \f
442 /* T is a statement.  Add it to the statement-tree.  This is the
443    C/ObjC version--C++ has a slightly different version of this
444    function.  */
445
446 tree
447 add_stmt (tree t)
448 {
449   enum tree_code code = TREE_CODE (t);
450
451   if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
452     {
453       if (!EXPR_HAS_LOCATION (t))
454         SET_EXPR_LOCATION (t, input_location);
455     }
456
457   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
458     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
459
460   /* Add T to the statement-tree.  Non-side-effect statements need to be
461      recorded during statement expressions.  */
462   append_to_statement_list_force (t, &cur_stmt_list);
463
464   return t;
465 }
466 \f
467
468 void
469 c_print_identifier (FILE *file, tree node, int indent)
470 {
471   print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
472   print_node (file, "tag", I_TAG_DECL (node), indent + 4);
473   print_node (file, "label", I_LABEL_DECL (node), indent + 4);
474   if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
475     {
476       tree rid = ridpointers[C_RID_CODE (node)];
477       indent_to (file, indent + 4);
478       fprintf (file, "rid %p \"%s\"",
479                (void *) rid, IDENTIFIER_POINTER (rid));
480     }
481 }
482
483 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
484    which may be any of several kinds of DECL or TYPE or error_mark_node,
485    in the scope SCOPE.  */
486 static void
487 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
488       bool nested, location_t locus)
489 {
490   struct c_binding *b, **here;
491
492   if (binding_freelist)
493     {
494       b = binding_freelist;
495       binding_freelist = b->prev;
496     }
497   else
498     b = GGC_NEW (struct c_binding);
499
500   b->shadowed = 0;
501   b->decl = decl;
502   b->id = name;
503   b->depth = scope->depth;
504   b->invisible = invisible;
505   b->nested = nested;
506   b->inner_comp = 0;
507   b->locus = locus;
508
509   b->type = 0;
510
511   b->prev = scope->bindings;
512   scope->bindings = b;
513
514   if (!name)
515     return;
516
517   switch (TREE_CODE (decl))
518     {
519     case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
520     case ENUMERAL_TYPE:
521     case UNION_TYPE:
522     case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
523     case VAR_DECL:
524     case FUNCTION_DECL:
525     case TYPE_DECL:
526     case CONST_DECL:
527     case PARM_DECL:
528     case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
529
530     default:
531       gcc_unreachable ();
532     }
533
534   /* Locate the appropriate place in the chain of shadowed decls
535      to insert this binding.  Normally, scope == current_scope and
536      this does nothing.  */
537   while (*here && (*here)->depth > scope->depth)
538     here = &(*here)->shadowed;
539
540   b->shadowed = *here;
541   *here = b;
542 }
543
544 /* Clear the binding structure B, stick it on the binding_freelist,
545    and return the former value of b->prev.  This is used by pop_scope
546    and get_parm_info to iterate destructively over all the bindings
547    from a given scope.  */
548 static struct c_binding *
549 free_binding_and_advance (struct c_binding *b)
550 {
551   struct c_binding *prev = b->prev;
552
553   memset (b, 0, sizeof (struct c_binding));
554   b->prev = binding_freelist;
555   binding_freelist = b;
556
557   return prev;
558 }
559
560 \f
561 /* Hook called at end of compilation to assume 1 elt
562    for a file-scope tentative array defn that wasn't complete before.  */
563
564 void
565 c_finish_incomplete_decl (tree decl)
566 {
567   if (TREE_CODE (decl) == VAR_DECL)
568     {
569       tree type = TREE_TYPE (decl);
570       if (type != error_mark_node
571           && TREE_CODE (type) == ARRAY_TYPE
572           && !DECL_EXTERNAL (decl)
573           && TYPE_DOMAIN (type) == 0)
574         {
575           warning (0, "array %q+D assumed to have one element", decl);
576
577           complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
578
579           layout_decl (decl, 0);
580         }
581     }
582 }
583 \f
584 /* Record that inline function FUNC contains a reference (location
585    LOC) to static DECL (file-scope or function-local according to
586    TYPE).  */
587
588 void
589 record_inline_static (location_t loc, tree func, tree decl,
590                       enum c_inline_static_type type)
591 {
592   struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
593   csi->location = loc;
594   csi->function = func;
595   csi->static_decl = decl;
596   csi->type = type;
597   csi->next = c_inline_statics;
598   c_inline_statics = csi;
599 }
600
601 /* Check for references to static declarations in inline functions at
602    the end of the translation unit and diagnose them if the functions
603    are still inline definitions.  */
604
605 static void
606 check_inline_statics (void)
607 {
608   struct c_inline_static *csi;
609   for (csi = c_inline_statics; csi; csi = csi->next)
610     {
611       if (DECL_EXTERNAL (csi->function))
612         switch (csi->type)
613           {
614           case csi_internal:
615             pedwarn (csi->location, 0,
616                      "%qD is static but used in inline function %qD "
617                      "which is not static", csi->static_decl, csi->function);
618             break;
619           case csi_modifiable:
620             pedwarn (csi->location, 0,
621                      "%q+D is static but declared in inline function %qD "
622                      "which is not static", csi->static_decl, csi->function);
623             break;
624           default:
625             gcc_unreachable ();
626           }
627     }
628   c_inline_statics = NULL;
629 }
630 \f
631 /* The Objective-C front-end often needs to determine the current scope.  */
632
633 void *
634 objc_get_current_scope (void)
635 {
636   return current_scope;
637 }
638
639 /* The following function is used only by Objective-C.  It needs to live here
640    because it accesses the innards of c_scope.  */
641
642 void
643 objc_mark_locals_volatile (void *enclosing_blk)
644 {
645   struct c_scope *scope;
646   struct c_binding *b;
647
648   for (scope = current_scope;
649        scope && scope != enclosing_blk;
650        scope = scope->outer)
651     {
652       for (b = scope->bindings; b; b = b->prev)
653         objc_volatilize_decl (b->decl);
654
655       /* Do not climb up past the current function.  */
656       if (scope->function_body)
657         break;
658     }
659 }
660
661 /* Nonzero if we are currently in file scope.  */
662
663 int
664 global_bindings_p (void)
665 {
666   return (current_scope == file_scope && !c_override_global_bindings_to_false
667           ? -1
668           : 0);
669 }
670
671 void
672 keep_next_level (void)
673 {
674   keep_next_level_flag = true;
675 }
676
677 /* Identify this scope as currently being filled with parameters.  */
678
679 void
680 declare_parm_level (void)
681 {
682   current_scope->parm_flag = true;
683 }
684
685 void
686 push_scope (void)
687 {
688   if (next_is_function_body)
689     {
690       /* This is the transition from the parameters to the top level
691          of the function body.  These are the same scope
692          (C99 6.2.1p4,6) so we do not push another scope structure.
693          next_is_function_body is set only by store_parm_decls, which
694          in turn is called when and only when we are about to
695          encounter the opening curly brace for the function body.
696
697          The outermost block of a function always gets a BLOCK node,
698          because the debugging output routines expect that each
699          function has at least one BLOCK.  */
700       current_scope->parm_flag         = false;
701       current_scope->function_body     = true;
702       current_scope->keep              = true;
703       current_scope->outer_function    = current_function_scope;
704       current_function_scope           = current_scope;
705
706       keep_next_level_flag = false;
707       next_is_function_body = false;
708     }
709   else
710     {
711       struct c_scope *scope;
712       if (scope_freelist)
713         {
714           scope = scope_freelist;
715           scope_freelist = scope->outer;
716         }
717       else
718         scope = GGC_CNEW (struct c_scope);
719
720       scope->keep          = keep_next_level_flag;
721       scope->outer         = current_scope;
722       scope->depth         = current_scope ? (current_scope->depth + 1) : 0;
723
724       /* Check for scope depth overflow.  Unlikely (2^28 == 268,435,456) but
725          possible.  */
726       if (current_scope && scope->depth == 0)
727         {
728           scope->depth--;
729           sorry ("GCC supports only %u nested scopes", scope->depth);
730         }
731
732       current_scope        = scope;
733       keep_next_level_flag = false;
734     }
735 }
736
737 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT.  */
738
739 static void
740 set_type_context (tree type, tree context)
741 {
742   for (type = TYPE_MAIN_VARIANT (type); type;
743        type = TYPE_NEXT_VARIANT (type))
744     TYPE_CONTEXT (type) = context;
745 }
746
747 /* Exit a scope.  Restore the state of the identifier-decl mappings
748    that were in effect when this scope was entered.  Return a BLOCK
749    node containing all the DECLs in this scope that are of interest
750    to debug info generation.  */
751
752 tree
753 pop_scope (void)
754 {
755   struct c_scope *scope = current_scope;
756   tree block, context, p;
757   struct c_binding *b;
758
759   bool functionbody = scope->function_body;
760   bool keep = functionbody || scope->keep || scope->bindings;
761
762   c_end_vm_scope (scope->depth);
763
764   /* If appropriate, create a BLOCK to record the decls for the life
765      of this function.  */
766   block = 0;
767   if (keep)
768     {
769       block = make_node (BLOCK);
770       BLOCK_SUBBLOCKS (block) = scope->blocks;
771       TREE_USED (block) = 1;
772
773       /* In each subblock, record that this is its superior.  */
774       for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
775         BLOCK_SUPERCONTEXT (p) = block;
776
777       BLOCK_VARS (block) = 0;
778     }
779
780   /* The TYPE_CONTEXTs for all of the tagged types belonging to this
781      scope must be set so that they point to the appropriate
782      construct, i.e.  either to the current FUNCTION_DECL node, or
783      else to the BLOCK node we just constructed.
784
785      Note that for tagged types whose scope is just the formal
786      parameter list for some function type specification, we can't
787      properly set their TYPE_CONTEXTs here, because we don't have a
788      pointer to the appropriate FUNCTION_TYPE node readily available
789      to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
790      type nodes get set in `grokdeclarator' as soon as we have created
791      the FUNCTION_TYPE node which will represent the "scope" for these
792      "parameter list local" tagged types.  */
793   if (scope->function_body)
794     context = current_function_decl;
795   else if (scope == file_scope)
796     {
797       tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
798       TREE_CHAIN (file_decl) = all_translation_units;
799       all_translation_units = file_decl;
800       context = file_decl;
801     }
802   else
803     context = block;
804
805   /* Clear all bindings in this scope.  */
806   for (b = scope->bindings; b; b = free_binding_and_advance (b))
807     {
808       p = b->decl;
809       switch (TREE_CODE (p))
810         {
811         case LABEL_DECL:
812           /* Warnings for unused labels, errors for undefined labels.  */
813           if (TREE_USED (p) && !DECL_INITIAL (p))
814             {
815               error ("label %q+D used but not defined", p);
816               DECL_INITIAL (p) = error_mark_node;
817             }
818           else 
819             warn_for_unused_label (p);
820
821           /* Labels go in BLOCK_VARS.  */
822           TREE_CHAIN (p) = BLOCK_VARS (block);
823           BLOCK_VARS (block) = p;
824           gcc_assert (I_LABEL_BINDING (b->id) == b);
825           I_LABEL_BINDING (b->id) = b->shadowed;
826           break;
827
828         case ENUMERAL_TYPE:
829         case UNION_TYPE:
830         case RECORD_TYPE:
831           set_type_context (p, context);
832
833           /* Types may not have tag-names, in which case the type
834              appears in the bindings list with b->id NULL.  */
835           if (b->id)
836             {
837               gcc_assert (I_TAG_BINDING (b->id) == b);
838               I_TAG_BINDING (b->id) = b->shadowed;
839             }
840           break;
841
842         case FUNCTION_DECL:
843           /* Propagate TREE_ADDRESSABLE from nested functions to their
844              containing functions.  */
845           if (!TREE_ASM_WRITTEN (p)
846               && DECL_INITIAL (p) != 0
847               && TREE_ADDRESSABLE (p)
848               && DECL_ABSTRACT_ORIGIN (p) != 0
849               && DECL_ABSTRACT_ORIGIN (p) != p)
850             TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
851           if (!DECL_EXTERNAL (p)
852               && !DECL_INITIAL (p)
853               && scope != file_scope
854               && scope != external_scope)
855             {
856               error ("nested function %q+D declared but never defined", p);
857               undef_nested_function = true;
858             }
859           else if (DECL_DECLARED_INLINE_P (p)
860                    && TREE_PUBLIC (p)
861                    && !DECL_INITIAL (p))
862             {
863               /* C99 6.7.4p6: "a function with external linkage... declared
864                  with an inline function specifier ... shall also be defined
865                  in the same translation unit."  */
866               if (!flag_gnu89_inline)
867                 pedwarn (input_location, 0,
868                          "inline function %q+D declared but never defined", p);
869               DECL_EXTERNAL (p) = 1;
870             }
871
872           goto common_symbol;
873
874         case VAR_DECL:
875           /* Warnings for unused variables.  */
876           if (!TREE_USED (p)
877               && !TREE_NO_WARNING (p)
878               && !DECL_IN_SYSTEM_HEADER (p)
879               && DECL_NAME (p)
880               && !DECL_ARTIFICIAL (p)
881               && scope != file_scope
882               && scope != external_scope)
883             warning (OPT_Wunused_variable, "unused variable %q+D", p);
884
885           if (b->inner_comp)
886             {
887               error ("type of array %q+D completed incompatibly with"
888                      " implicit initialization", p);
889             }
890
891           /* Fall through.  */
892         case TYPE_DECL:
893         case CONST_DECL:
894         common_symbol:
895           /* All of these go in BLOCK_VARS, but only if this is the
896              binding in the home scope.  */
897           if (!b->nested)
898             {
899               TREE_CHAIN (p) = BLOCK_VARS (block);
900               BLOCK_VARS (block) = p;
901             }
902           else if (VAR_OR_FUNCTION_DECL_P (p))
903             {
904               /* For block local externs add a special
905                  DECL_EXTERNAL decl for debug info generation.  */
906               tree extp = copy_node (p);
907
908               DECL_EXTERNAL (extp) = 1;
909               TREE_STATIC (extp) = 0;
910               TREE_PUBLIC (extp) = 1;
911               DECL_INITIAL (extp) = NULL_TREE;
912               DECL_LANG_SPECIFIC (extp) = NULL;
913               DECL_CONTEXT (extp) = current_function_decl;
914               if (TREE_CODE (p) == FUNCTION_DECL)
915                 {
916                   DECL_RESULT (extp) = NULL_TREE;
917                   DECL_SAVED_TREE (extp) = NULL_TREE;
918                   DECL_STRUCT_FUNCTION (extp) = NULL;
919                 }
920               if (b->locus != UNKNOWN_LOCATION)
921                 DECL_SOURCE_LOCATION (extp) = b->locus;
922               TREE_CHAIN (extp) = BLOCK_VARS (block);
923               BLOCK_VARS (block) = extp;
924             }
925           /* If this is the file scope, and we are processing more
926              than one translation unit in this compilation, set
927              DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
928              This makes same_translation_unit_p work, and causes
929              static declarations to be given disambiguating suffixes.  */
930           if (scope == file_scope && num_in_fnames > 1)
931             {
932               DECL_CONTEXT (p) = context;
933               if (TREE_CODE (p) == TYPE_DECL)
934                 set_type_context (TREE_TYPE (p), context);
935             }
936
937           /* Fall through.  */
938           /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
939              already been put there by store_parm_decls.  Unused-
940              parameter warnings are handled by function.c.
941              error_mark_node obviously does not go in BLOCK_VARS and
942              does not get unused-variable warnings.  */
943         case PARM_DECL:
944         case ERROR_MARK:
945           /* It is possible for a decl not to have a name.  We get
946              here with b->id NULL in this case.  */
947           if (b->id)
948             {
949               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
950               I_SYMBOL_BINDING (b->id) = b->shadowed;
951               if (b->shadowed && b->shadowed->type)
952                 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
953             }
954           break;
955
956         default:
957           gcc_unreachable ();
958         }
959     }
960
961
962   /* Dispose of the block that we just made inside some higher level.  */
963   if ((scope->function_body || scope == file_scope) && context)
964     {
965       DECL_INITIAL (context) = block;
966       BLOCK_SUPERCONTEXT (block) = context;
967     }
968   else if (scope->outer)
969     {
970       if (block)
971         SCOPE_LIST_APPEND (scope->outer, blocks, block);
972       /* If we did not make a block for the scope just exited, any
973          blocks made for inner scopes must be carried forward so they
974          will later become subblocks of something else.  */
975       else if (scope->blocks)
976         SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
977     }
978
979   /* Pop the current scope, and free the structure for reuse.  */
980   current_scope = scope->outer;
981   if (scope->function_body)
982     current_function_scope = scope->outer_function;
983
984   memset (scope, 0, sizeof (struct c_scope));
985   scope->outer = scope_freelist;
986   scope_freelist = scope;
987
988   return block;
989 }
990
991 void
992 push_file_scope (void)
993 {
994   tree decl;
995
996   if (file_scope)
997     return;
998
999   push_scope ();
1000   file_scope = current_scope;
1001
1002   start_fname_decls ();
1003
1004   for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1005     bind (DECL_NAME (decl), decl, file_scope,
1006           /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1007 }
1008
1009 void
1010 pop_file_scope (void)
1011 {
1012   /* In case there were missing closebraces, get us back to the global
1013      binding level.  */
1014   while (current_scope != file_scope)
1015     pop_scope ();
1016
1017   /* __FUNCTION__ is defined at file scope ("").  This
1018      call may not be necessary as my tests indicate it
1019      still works without it.  */
1020   finish_fname_decls ();
1021
1022   check_inline_statics ();
1023
1024   /* This is the point to write out a PCH if we're doing that.
1025      In that case we do not want to do anything else.  */
1026   if (pch_file)
1027     {
1028       c_common_write_pch ();
1029       return;
1030     }
1031
1032   /* Pop off the file scope and close this translation unit.  */
1033   pop_scope ();
1034   file_scope = 0;
1035
1036   maybe_apply_pending_pragma_weaks ();
1037   cgraph_finalize_compilation_unit ();
1038 }
1039
1040 \f
1041 /* Push a definition or a declaration of struct, union or enum tag "name".
1042    "type" should be the type node.
1043    We assume that the tag "name" is not already defined.
1044
1045    Note that the definition may really be just a forward reference.
1046    In that case, the TYPE_SIZE will be zero.  */
1047
1048 static void
1049 pushtag (tree name, tree type)
1050 {
1051   /* Record the identifier as the type's name if it has none.  */
1052   if (name && !TYPE_NAME (type))
1053     TYPE_NAME (type) = name;
1054   bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false,
1055         UNKNOWN_LOCATION);
1056
1057   /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1058      tagged type we just added to the current scope.  This fake
1059      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1060      to output a representation of a tagged type, and it also gives
1061      us a convenient place to record the "scope start" address for the
1062      tagged type.  */
1063
1064   TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1065
1066   /* An approximation for now, so we can tell this is a function-scope tag.
1067      This will be updated in pop_scope.  */
1068   TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1069 }
1070 \f
1071 /* Subroutine of compare_decls.  Allow harmless mismatches in return
1072    and argument types provided that the type modes match.  This function
1073    return a unified type given a suitable match, and 0 otherwise.  */
1074
1075 static tree
1076 match_builtin_function_types (tree newtype, tree oldtype)
1077 {
1078   tree newrettype, oldrettype;
1079   tree newargs, oldargs;
1080   tree trytype, tryargs;
1081
1082   /* Accept the return type of the new declaration if same modes.  */
1083   oldrettype = TREE_TYPE (oldtype);
1084   newrettype = TREE_TYPE (newtype);
1085
1086   if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1087     return 0;
1088
1089   oldargs = TYPE_ARG_TYPES (oldtype);
1090   newargs = TYPE_ARG_TYPES (newtype);
1091   tryargs = newargs;
1092
1093   while (oldargs || newargs)
1094     {
1095       if (!oldargs
1096           || !newargs
1097           || !TREE_VALUE (oldargs)
1098           || !TREE_VALUE (newargs)
1099           || TYPE_MODE (TREE_VALUE (oldargs))
1100              != TYPE_MODE (TREE_VALUE (newargs)))
1101         return 0;
1102
1103       oldargs = TREE_CHAIN (oldargs);
1104       newargs = TREE_CHAIN (newargs);
1105     }
1106
1107   trytype = build_function_type (newrettype, tryargs);
1108   return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1109 }
1110
1111 /* Subroutine of diagnose_mismatched_decls.  Check for function type
1112    mismatch involving an empty arglist vs a nonempty one and give clearer
1113    diagnostics.  */
1114 static void
1115 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1116                            tree newtype, tree oldtype)
1117 {
1118   tree t;
1119
1120   if (TREE_CODE (olddecl) != FUNCTION_DECL
1121       || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1122       || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1123            ||
1124            (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1125     return;
1126
1127   t = TYPE_ARG_TYPES (oldtype);
1128   if (t == 0)
1129     t = TYPE_ARG_TYPES (newtype);
1130   for (; t; t = TREE_CHAIN (t))
1131     {
1132       tree type = TREE_VALUE (t);
1133
1134       if (TREE_CHAIN (t) == 0
1135           && TYPE_MAIN_VARIANT (type) != void_type_node)
1136         {
1137           inform (input_location, "a parameter list with an ellipsis can%'t match "
1138                   "an empty parameter name list declaration");
1139           break;
1140         }
1141
1142       if (c_type_promotes_to (type) != type)
1143         {
1144           inform (input_location, "an argument type that has a default promotion can%'t match "
1145                   "an empty parameter name list declaration");
1146           break;
1147         }
1148     }
1149 }
1150
1151 /* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
1152    old-style function definition, NEWDECL is a prototype declaration.
1153    Diagnose inconsistencies in the argument list.  Returns TRUE if
1154    the prototype is compatible, FALSE if not.  */
1155 static bool
1156 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1157 {
1158   tree newargs, oldargs;
1159   int i;
1160
1161 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1162
1163   oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1164   newargs = TYPE_ARG_TYPES (newtype);
1165   i = 1;
1166
1167   for (;;)
1168     {
1169       tree oldargtype = TREE_VALUE (oldargs);
1170       tree newargtype = TREE_VALUE (newargs);
1171
1172       if (oldargtype == error_mark_node || newargtype == error_mark_node)
1173         return false;
1174
1175       oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1176       newargtype = TYPE_MAIN_VARIANT (newargtype);
1177
1178       if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1179         break;
1180
1181       /* Reaching the end of just one list means the two decls don't
1182          agree on the number of arguments.  */
1183       if (END_OF_ARGLIST (oldargtype))
1184         {
1185           error ("prototype for %q+D declares more arguments "
1186                  "than previous old-style definition", newdecl);
1187           return false;
1188         }
1189       else if (END_OF_ARGLIST (newargtype))
1190         {
1191           error ("prototype for %q+D declares fewer arguments "
1192                  "than previous old-style definition", newdecl);
1193           return false;
1194         }
1195
1196       /* Type for passing arg must be consistent with that declared
1197          for the arg.  */
1198       else if (!comptypes (oldargtype, newargtype))
1199         {
1200           error ("prototype for %q+D declares argument %d"
1201                  " with incompatible type",
1202                  newdecl, i);
1203           return false;
1204         }
1205
1206       oldargs = TREE_CHAIN (oldargs);
1207       newargs = TREE_CHAIN (newargs);
1208       i++;
1209     }
1210
1211   /* If we get here, no errors were found, but do issue a warning
1212      for this poor-style construct.  */
1213   warning (0, "prototype for %q+D follows non-prototype definition",
1214            newdecl);
1215   return true;
1216 #undef END_OF_ARGLIST
1217 }
1218
1219 /* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
1220    first in a pair of mismatched declarations, using the diagnostic
1221    function DIAG.  */
1222 static void
1223 locate_old_decl (tree decl)
1224 {
1225   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1226     ;
1227   else if (DECL_INITIAL (decl))
1228     inform (input_location, "previous definition of %q+D was here", decl);
1229   else if (C_DECL_IMPLICIT (decl))
1230     inform (input_location, "previous implicit declaration of %q+D was here", decl);
1231   else
1232     inform (input_location, "previous declaration of %q+D was here", decl);
1233 }
1234
1235 /* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
1236    Returns true if the caller should proceed to merge the two, false
1237    if OLDDECL should simply be discarded.  As a side effect, issues
1238    all necessary diagnostics for invalid or poor-style combinations.
1239    If it returns true, writes the types of NEWDECL and OLDDECL to
1240    *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1241    TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
1242
1243 static bool
1244 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1245                            tree *newtypep, tree *oldtypep)
1246 {
1247   tree newtype, oldtype;
1248   bool pedwarned = false;
1249   bool warned = false;
1250   bool retval = true;
1251
1252 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL)  \
1253                                   && DECL_EXTERNAL (DECL))
1254
1255   /* If we have error_mark_node for either decl or type, just discard
1256      the previous decl - we're in an error cascade already.  */
1257   if (olddecl == error_mark_node || newdecl == error_mark_node)
1258     return false;
1259   *oldtypep = oldtype = TREE_TYPE (olddecl);
1260   *newtypep = newtype = TREE_TYPE (newdecl);
1261   if (oldtype == error_mark_node || newtype == error_mark_node)
1262     return false;
1263
1264   /* Two different categories of symbol altogether.  This is an error
1265      unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
1266   if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1267     {
1268       if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1269             && DECL_BUILT_IN (olddecl)
1270             && !C_DECL_DECLARED_BUILTIN (olddecl)))
1271         {
1272           error ("%q+D redeclared as different kind of symbol", newdecl);
1273           locate_old_decl (olddecl);
1274         }
1275       else if (TREE_PUBLIC (newdecl))
1276         warning (0, "built-in function %q+D declared as non-function",
1277                  newdecl);
1278       else
1279         warning (OPT_Wshadow, "declaration of %q+D shadows "
1280                  "a built-in function", newdecl);
1281       return false;
1282     }
1283
1284   /* Enumerators have no linkage, so may only be declared once in a
1285      given scope.  */
1286   if (TREE_CODE (olddecl) == CONST_DECL)
1287     {
1288       error ("redeclaration of enumerator %q+D", newdecl);
1289       locate_old_decl (olddecl);
1290       return false;
1291     }
1292
1293   if (!comptypes (oldtype, newtype))
1294     {
1295       if (TREE_CODE (olddecl) == FUNCTION_DECL
1296           && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1297         {
1298           /* Accept harmless mismatch in function types.
1299              This is for the ffs and fprintf builtins.  */
1300           tree trytype = match_builtin_function_types (newtype, oldtype);
1301
1302           if (trytype && comptypes (newtype, trytype))
1303             *oldtypep = oldtype = trytype;
1304           else
1305             {
1306               /* If types don't match for a built-in, throw away the
1307                  built-in.  No point in calling locate_old_decl here, it
1308                  won't print anything.  */
1309               warning (0, "conflicting types for built-in function %q+D",
1310                        newdecl);
1311               return false;
1312             }
1313         }
1314       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1315                && DECL_IS_BUILTIN (olddecl))
1316         {
1317           /* A conflicting function declaration for a predeclared
1318              function that isn't actually built in.  Objective C uses
1319              these.  The new declaration silently overrides everything
1320              but the volatility (i.e. noreturn) indication.  See also
1321              below.  FIXME: Make Objective C use normal builtins.  */
1322           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1323           return false;
1324         }
1325       /* Permit void foo (...) to match int foo (...) if the latter is
1326          the definition and implicit int was used.  See
1327          c-torture/compile/920625-2.c.  */
1328       else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1329                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1330                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1331                && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1332         {
1333           pedwarned = pedwarn (input_location, 0,
1334                                "conflicting types for %q+D", newdecl);
1335           /* Make sure we keep void as the return type.  */
1336           TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1337           C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1338         }
1339       /* Permit void foo (...) to match an earlier call to foo (...) with
1340          no declared type (thus, implicitly int).  */
1341       else if (TREE_CODE (newdecl) == FUNCTION_DECL
1342                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1343                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1344                && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1345         {
1346           pedwarned = pedwarn (input_location, 0,
1347                                "conflicting types for %q+D", newdecl);
1348           /* Make sure we keep void as the return type.  */
1349           TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1350         }
1351       else
1352         {
1353           if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1354             error ("conflicting type qualifiers for %q+D", newdecl);
1355           else
1356             error ("conflicting types for %q+D", newdecl);
1357           diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1358           locate_old_decl (olddecl);
1359           return false;
1360         }
1361     }
1362
1363   /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1364      but silently ignore the redeclaration if either is in a system
1365      header.  (Conflicting redeclarations were handled above.)  */
1366   if (TREE_CODE (newdecl) == TYPE_DECL)
1367     {
1368       if (DECL_IN_SYSTEM_HEADER (newdecl)
1369           || DECL_IN_SYSTEM_HEADER (olddecl)
1370           || TREE_NO_WARNING (newdecl)
1371           || TREE_NO_WARNING (olddecl))
1372         return true;  /* Allow OLDDECL to continue in use.  */
1373
1374       error ("redefinition of typedef %q+D", newdecl);
1375       locate_old_decl (olddecl);
1376       return false;
1377     }
1378
1379   /* Function declarations can either be 'static' or 'extern' (no
1380      qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1381      can never conflict with each other on account of linkage
1382      (6.2.2p4).  Multiple definitions are not allowed (6.9p3,5) but
1383      gnu89 mode permits two definitions if one is 'extern inline' and
1384      one is not.  The non- extern-inline definition supersedes the
1385      extern-inline definition.  */
1386
1387   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1388     {
1389       /* If you declare a built-in function name as static, or
1390          define the built-in with an old-style definition (so we
1391          can't validate the argument list) the built-in definition is
1392          overridden, but optionally warn this was a bad choice of name.  */
1393       if (DECL_BUILT_IN (olddecl)
1394           && !C_DECL_DECLARED_BUILTIN (olddecl)
1395           && (!TREE_PUBLIC (newdecl)
1396               || (DECL_INITIAL (newdecl)
1397                   && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1398         {
1399           warning (OPT_Wshadow, "declaration of %q+D shadows "
1400                    "a built-in function", newdecl);
1401           /* Discard the old built-in function.  */
1402           return false;
1403         }
1404
1405       if (DECL_INITIAL (newdecl))
1406         {
1407           if (DECL_INITIAL (olddecl))
1408             {
1409               /* If both decls are in the same TU and the new declaration
1410                  isn't overriding an extern inline reject the new decl.
1411                  In c99, no overriding is allowed in the same translation
1412                  unit.  */
1413               if ((!DECL_EXTERN_INLINE (olddecl)
1414                    || DECL_EXTERN_INLINE (newdecl)
1415                    || (!flag_gnu89_inline
1416                        && (!DECL_DECLARED_INLINE_P (olddecl)
1417                            || !lookup_attribute ("gnu_inline",
1418                                                  DECL_ATTRIBUTES (olddecl)))
1419                        && (!DECL_DECLARED_INLINE_P (newdecl)
1420                            || !lookup_attribute ("gnu_inline",
1421                                                  DECL_ATTRIBUTES (newdecl))))
1422                   )
1423                   && same_translation_unit_p (newdecl, olddecl))
1424                 {
1425                   error ("redefinition of %q+D", newdecl);
1426                   locate_old_decl (olddecl);
1427                   return false;
1428                 }
1429             }
1430         }
1431       /* If we have a prototype after an old-style function definition,
1432          the argument types must be checked specially.  */
1433       else if (DECL_INITIAL (olddecl)
1434                && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1435                && TYPE_ACTUAL_ARG_TYPES (oldtype)
1436                && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1437         {
1438           locate_old_decl (olddecl);
1439           return false;
1440         }
1441       /* A non-static declaration (even an "extern") followed by a
1442          static declaration is undefined behavior per C99 6.2.2p3-5,7.
1443          The same is true for a static forward declaration at block
1444          scope followed by a non-static declaration/definition at file
1445          scope.  Static followed by non-static at the same scope is
1446          not undefined behavior, and is the most convenient way to get
1447          some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
1448          the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1449          we do diagnose it if -Wtraditional.  */
1450       if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1451         {
1452           /* Two exceptions to the rule.  If olddecl is an extern
1453              inline, or a predeclared function that isn't actually
1454              built in, newdecl silently overrides olddecl.  The latter
1455              occur only in Objective C; see also above.  (FIXME: Make
1456              Objective C use normal builtins.)  */
1457           if (!DECL_IS_BUILTIN (olddecl)
1458               && !DECL_EXTERN_INLINE (olddecl))
1459             {
1460               error ("static declaration of %q+D follows "
1461                      "non-static declaration", newdecl);
1462               locate_old_decl (olddecl);
1463             }
1464           return false;
1465         }
1466       else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1467         {
1468           if (DECL_CONTEXT (olddecl))
1469             {
1470               error ("non-static declaration of %q+D follows "
1471                      "static declaration", newdecl);
1472               locate_old_decl (olddecl);
1473               return false;
1474             }
1475           else if (warn_traditional)
1476             {
1477               warned |= warning (OPT_Wtraditional, 
1478                                  "non-static declaration of %q+D "
1479                                  "follows static declaration", newdecl);
1480             }
1481         }
1482
1483       /* Make sure gnu_inline attribute is either not present, or
1484          present on all inline decls.  */
1485       if (DECL_DECLARED_INLINE_P (olddecl)
1486           && DECL_DECLARED_INLINE_P (newdecl))
1487         {
1488           bool newa = lookup_attribute ("gnu_inline",
1489                                         DECL_ATTRIBUTES (newdecl)) != NULL;
1490           bool olda = lookup_attribute ("gnu_inline",
1491                                         DECL_ATTRIBUTES (olddecl)) != NULL;
1492           if (newa != olda)
1493             {
1494               error ("%<gnu_inline%> attribute present on %q+D",
1495                      newa ? newdecl : olddecl);
1496               error ("%Jbut not here", newa ? olddecl : newdecl);
1497             }
1498         }
1499     }
1500   else if (TREE_CODE (newdecl) == VAR_DECL)
1501     {
1502       /* Only variables can be thread-local, and all declarations must
1503          agree on this property.  */
1504       if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1505         {
1506           /* Nothing to check.  Since OLDDECL is marked threadprivate
1507              and NEWDECL does not have a thread-local attribute, we
1508              will merge the threadprivate attribute into NEWDECL.  */
1509           ;
1510         }
1511       else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1512         {
1513           if (DECL_THREAD_LOCAL_P (newdecl))
1514             error ("thread-local declaration of %q+D follows "
1515                    "non-thread-local declaration", newdecl);
1516           else
1517             error ("non-thread-local declaration of %q+D follows "
1518                    "thread-local declaration", newdecl);
1519
1520           locate_old_decl (olddecl);
1521           return false;
1522         }
1523
1524       /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1525       if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1526         {
1527           error ("redefinition of %q+D", newdecl);
1528           locate_old_decl (olddecl);
1529           return false;
1530         }
1531
1532       /* Objects declared at file scope: if the first declaration had
1533          external linkage (even if it was an external reference) the
1534          second must have external linkage as well, or the behavior is
1535          undefined.  If the first declaration had internal linkage, then
1536          the second must too, or else be an external reference (in which
1537          case the composite declaration still has internal linkage).
1538          As for function declarations, we warn about the static-then-
1539          extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
1540       if (DECL_FILE_SCOPE_P (newdecl)
1541           && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1542         {
1543           if (DECL_EXTERNAL (newdecl))
1544             {
1545               if (!DECL_FILE_SCOPE_P (olddecl))
1546                 {
1547                   error ("extern declaration of %q+D follows "
1548                          "declaration with no linkage", newdecl);
1549                   locate_old_decl (olddecl);
1550                   return false;
1551                 }
1552               else if (warn_traditional)
1553                 {
1554                   warned |= warning (OPT_Wtraditional, 
1555                                      "non-static declaration of %q+D "
1556                                      "follows static declaration", newdecl);
1557                 }
1558             }
1559           else
1560             {
1561               if (TREE_PUBLIC (newdecl))
1562                 error ("non-static declaration of %q+D follows "
1563                        "static declaration", newdecl);
1564               else
1565                 error ("static declaration of %q+D follows "
1566                        "non-static declaration", newdecl);
1567
1568               locate_old_decl (olddecl);
1569               return false;
1570             }
1571         }
1572       /* Two objects with the same name declared at the same block
1573          scope must both be external references (6.7p3).  */
1574       else if (!DECL_FILE_SCOPE_P (newdecl))
1575         {
1576           if (DECL_EXTERNAL (newdecl))
1577             {
1578               /* Extern with initializer at block scope, which will
1579                  already have received an error.  */
1580             }
1581           else if (DECL_EXTERNAL (olddecl))
1582             {
1583               error ("declaration of %q+D with no linkage follows "
1584                      "extern declaration", newdecl);
1585               locate_old_decl (olddecl);
1586             }
1587           else
1588             {
1589               error ("redeclaration of %q+D with no linkage", newdecl);
1590               locate_old_decl (olddecl);
1591             }
1592
1593           return false;
1594         }
1595     }
1596
1597   /* warnings */
1598   /* All decls must agree on a visibility.  */
1599   if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1600       && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1601       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1602     {
1603       warned |= warning (0, "redeclaration of %q+D with different visibility "
1604                          "(old visibility preserved)", newdecl);
1605     }
1606
1607   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1608     {
1609       /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1610       if (DECL_DECLARED_INLINE_P (newdecl)
1611           && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1612         {
1613           warned |= warning (OPT_Wattributes, 
1614                              "inline declaration of %qD follows "
1615                              "declaration with attribute noinline", newdecl);
1616         }
1617       else if (DECL_DECLARED_INLINE_P (olddecl)
1618                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1619         {
1620           warned |= warning (OPT_Wattributes, 
1621                              "declaration of %q+D with attribute "
1622                              "noinline follows inline declaration ", newdecl);
1623         }
1624     }
1625   else /* PARM_DECL, VAR_DECL */
1626     {
1627       /* Redeclaration of a parameter is a constraint violation (this is
1628          not explicitly stated, but follows from C99 6.7p3 [no more than
1629          one declaration of the same identifier with no linkage in the
1630          same scope, except type tags] and 6.2.2p6 [parameters have no
1631          linkage]).  We must check for a forward parameter declaration,
1632          indicated by TREE_ASM_WRITTEN on the old declaration - this is
1633          an extension, the mandatory diagnostic for which is handled by
1634          mark_forward_parm_decls.  */
1635
1636       if (TREE_CODE (newdecl) == PARM_DECL
1637           && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1638         {
1639           error ("redefinition of parameter %q+D", newdecl);
1640           locate_old_decl (olddecl);
1641           return false;
1642         }
1643     }
1644
1645   /* Optional warning for completely redundant decls.  */
1646   if (!warned && !pedwarned
1647       && warn_redundant_decls
1648       /* Don't warn about a function declaration followed by a
1649          definition.  */
1650       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1651            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1652       /* Don't warn about redundant redeclarations of builtins.  */
1653       && !(TREE_CODE (newdecl) == FUNCTION_DECL
1654            && !DECL_BUILT_IN (newdecl)
1655            && DECL_BUILT_IN (olddecl)
1656            && !C_DECL_DECLARED_BUILTIN (olddecl))
1657       /* Don't warn about an extern followed by a definition.  */
1658       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1659       /* Don't warn about forward parameter decls.  */
1660       && !(TREE_CODE (newdecl) == PARM_DECL
1661            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1662       /* Don't warn about a variable definition following a declaration.  */
1663       && !(TREE_CODE (newdecl) == VAR_DECL
1664            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1665     {
1666       warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1667                         newdecl);
1668     }
1669
1670   /* Report location of previous decl/defn.  */
1671   if (warned || pedwarned)
1672     locate_old_decl (olddecl);
1673
1674 #undef DECL_EXTERN_INLINE
1675
1676   return retval;
1677 }
1678
1679 /* Subroutine of duplicate_decls.  NEWDECL has been found to be
1680    consistent with OLDDECL, but carries new information.  Merge the
1681    new information into OLDDECL.  This function issues no
1682    diagnostics.  */
1683
1684 static void
1685 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1686 {
1687   bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1688                             && DECL_INITIAL (newdecl) != 0);
1689   bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1690                            && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1691   bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1692                            && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1693   bool extern_changed = false;
1694
1695   /* For real parm decl following a forward decl, rechain the old decl
1696      in its new location and clear TREE_ASM_WRITTEN (it's not a
1697      forward decl anymore).  */
1698   if (TREE_CODE (newdecl) == PARM_DECL
1699       && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1700     {
1701       struct c_binding *b, **here;
1702
1703       for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1704         if ((*here)->decl == olddecl)
1705           goto found;
1706       gcc_unreachable ();
1707
1708     found:
1709       b = *here;
1710       *here = b->prev;
1711       b->prev = current_scope->bindings;
1712       current_scope->bindings = b;
1713
1714       TREE_ASM_WRITTEN (olddecl) = 0;
1715     }
1716
1717   DECL_ATTRIBUTES (newdecl)
1718     = targetm.merge_decl_attributes (olddecl, newdecl);
1719
1720   /* Merge the data types specified in the two decls.  */
1721   TREE_TYPE (newdecl)
1722     = TREE_TYPE (olddecl)
1723     = composite_type (newtype, oldtype);
1724
1725   /* Lay the type out, unless already done.  */
1726   if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1727     {
1728       if (TREE_TYPE (newdecl) != error_mark_node)
1729         layout_type (TREE_TYPE (newdecl));
1730       if (TREE_CODE (newdecl) != FUNCTION_DECL
1731           && TREE_CODE (newdecl) != TYPE_DECL
1732           && TREE_CODE (newdecl) != CONST_DECL)
1733         layout_decl (newdecl, 0);
1734     }
1735   else
1736     {
1737       /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1738       DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1739       DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1740       DECL_MODE (newdecl) = DECL_MODE (olddecl);
1741       if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1742         {
1743           DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1744           DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1745         }
1746     }
1747
1748   /* Keep the old rtl since we can safely use it.  */
1749   if (HAS_RTL_P (olddecl))
1750     COPY_DECL_RTL (olddecl, newdecl);
1751
1752   /* Merge the type qualifiers.  */
1753   if (TREE_READONLY (newdecl))
1754     TREE_READONLY (olddecl) = 1;
1755
1756   if (TREE_THIS_VOLATILE (newdecl))
1757     TREE_THIS_VOLATILE (olddecl) = 1;
1758
1759   /* Merge deprecatedness.  */
1760   if (TREE_DEPRECATED (newdecl))
1761     TREE_DEPRECATED (olddecl) = 1;
1762
1763   /* If a decl is in a system header and the other isn't, keep the one on the
1764      system header. Otherwise, keep source location of definition rather than
1765      declaration and of prototype rather than non-prototype unless that
1766      prototype is built-in.  */
1767   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1768       && DECL_IN_SYSTEM_HEADER (olddecl)
1769       && !DECL_IN_SYSTEM_HEADER (newdecl) )
1770     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1771   else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1772            && DECL_IN_SYSTEM_HEADER (newdecl)
1773            && !DECL_IN_SYSTEM_HEADER (olddecl))
1774     DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1775   else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1776            || (old_is_prototype && !new_is_prototype
1777                && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1778     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1779
1780   /* Merge the initialization information.  */
1781    if (DECL_INITIAL (newdecl) == 0)
1782     DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1783
1784   /* Merge the threadprivate attribute.  */
1785   if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1786     {
1787       DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1788       C_DECL_THREADPRIVATE_P (newdecl) = 1;
1789     }
1790
1791   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1792     {
1793       /* Merge the section attribute.
1794          We want to issue an error if the sections conflict but that
1795          must be done later in decl_attributes since we are called
1796          before attributes are assigned.  */
1797       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1798         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1799
1800       /* Copy the assembler name.
1801          Currently, it can only be defined in the prototype.  */
1802       COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1803
1804       /* Use visibility of whichever declaration had it specified */
1805       if (DECL_VISIBILITY_SPECIFIED (olddecl))
1806         {
1807           DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1808           DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1809         }
1810
1811       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1812         {
1813           DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1814           DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1815           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1816           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1817             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1818           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1819           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1820           DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1821           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1822           DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1823           DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1824         }
1825
1826       /* Merge the storage class information.  */
1827       merge_weak (newdecl, olddecl);
1828
1829       /* For functions, static overrides non-static.  */
1830       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1831         {
1832           TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1833           /* This is since we don't automatically
1834              copy the attributes of NEWDECL into OLDDECL.  */
1835           TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1836           /* If this clears `static', clear it in the identifier too.  */
1837           if (!TREE_PUBLIC (olddecl))
1838             TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1839         }
1840     }
1841
1842   /* In c99, 'extern' declaration before (or after) 'inline' means this
1843      function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1844      is present.  */
1845   if (TREE_CODE (newdecl) == FUNCTION_DECL
1846       && !flag_gnu89_inline
1847       && (DECL_DECLARED_INLINE_P (newdecl)
1848           || DECL_DECLARED_INLINE_P (olddecl))
1849       && (!DECL_DECLARED_INLINE_P (newdecl)
1850           || !DECL_DECLARED_INLINE_P (olddecl)
1851           || !DECL_EXTERNAL (olddecl))
1852       && DECL_EXTERNAL (newdecl)
1853       && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1854       && !current_function_decl)
1855     DECL_EXTERNAL (newdecl) = 0;
1856
1857   if (DECL_EXTERNAL (newdecl))
1858     {
1859       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1860       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1861
1862       /* An extern decl does not override previous storage class.  */
1863       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1864       if (!DECL_EXTERNAL (newdecl))
1865         {
1866           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1867           DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1868         }
1869     }
1870   else
1871     {
1872       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1873       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1874     }
1875
1876   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1877     {
1878       /* If we're redefining a function previously defined as extern
1879          inline, make sure we emit debug info for the inline before we
1880          throw it away, in case it was inlined into a function that
1881          hasn't been written out yet.  */
1882       if (new_is_definition && DECL_INITIAL (olddecl))
1883         /* The new defn must not be inline.  */
1884         DECL_UNINLINABLE (newdecl) = 1;
1885       else
1886         {
1887           /* If either decl says `inline', this fn is inline, unless
1888              its definition was passed already.  */
1889           if (DECL_DECLARED_INLINE_P (newdecl)
1890               || DECL_DECLARED_INLINE_P (olddecl))
1891             DECL_DECLARED_INLINE_P (newdecl) = 1;
1892
1893           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1894             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1895
1896           DECL_DISREGARD_INLINE_LIMITS (newdecl)
1897             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1898             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1899                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1900         }
1901
1902       if (DECL_BUILT_IN (olddecl))
1903         {
1904           /* If redeclaring a builtin function, it stays built in.
1905              But it gets tagged as having been declared.  */
1906           DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1907           DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1908           C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1909           if (new_is_prototype)
1910             C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1911           else
1912             C_DECL_BUILTIN_PROTOTYPE (newdecl)
1913               = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1914         }
1915
1916       /* Preserve function specific target and optimization options */
1917       if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1918           && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1919         DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1920           = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1921
1922       if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1923           && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1924         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1925           = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1926
1927       /* Also preserve various other info from the definition.  */
1928       if (!new_is_definition)
1929         {
1930           tree t;
1931           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1932           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1933           DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1934           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1935           gimple_set_body (newdecl, gimple_body (olddecl));
1936           DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
1937           for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
1938             DECL_CONTEXT (t) = newdecl;
1939
1940           /* See if we've got a function to instantiate from.  */
1941           if (DECL_SAVED_TREE (olddecl))
1942             DECL_ABSTRACT_ORIGIN (newdecl)
1943               = DECL_ABSTRACT_ORIGIN (olddecl);
1944         }
1945     }
1946
1947    extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1948
1949    /* Merge the USED information.  */
1950    if (TREE_USED (olddecl))
1951      TREE_USED (newdecl) = 1;
1952    else if (TREE_USED (newdecl))
1953      TREE_USED (olddecl) = 1;
1954
1955   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1956      But preserve OLDDECL's DECL_UID and DECL_CONTEXT.  */
1957   {
1958     unsigned olddecl_uid = DECL_UID (olddecl);
1959     tree olddecl_context = DECL_CONTEXT (olddecl);
1960     tree olddecl_arguments = NULL;
1961     if (TREE_CODE (olddecl) == FUNCTION_DECL)
1962       olddecl_arguments = DECL_ARGUMENTS (olddecl);
1963
1964     memcpy ((char *) olddecl + sizeof (struct tree_common),
1965             (char *) newdecl + sizeof (struct tree_common),
1966             sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1967     switch (TREE_CODE (olddecl))
1968       {
1969       case FUNCTION_DECL:
1970         gimple_set_body (olddecl, gimple_body (newdecl));
1971         /* fall through */
1972
1973       case FIELD_DECL:
1974       case VAR_DECL:
1975       case PARM_DECL:
1976       case LABEL_DECL:
1977       case RESULT_DECL:
1978       case CONST_DECL:
1979       case TYPE_DECL:
1980         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1981                 (char *) newdecl + sizeof (struct tree_decl_common),
1982                 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1983         break;
1984
1985       default:
1986
1987         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1988                 (char *) newdecl + sizeof (struct tree_decl_common),
1989                 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1990       }
1991     DECL_UID (olddecl) = olddecl_uid;
1992     DECL_CONTEXT (olddecl) = olddecl_context;
1993     if (TREE_CODE (olddecl) == FUNCTION_DECL)
1994       DECL_ARGUMENTS (olddecl) = olddecl_arguments;
1995   }
1996
1997   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1998      so that encode_section_info has a chance to look at the new decl
1999      flags and attributes.  */
2000   if (DECL_RTL_SET_P (olddecl)
2001       && (TREE_CODE (olddecl) == FUNCTION_DECL
2002           || (TREE_CODE (olddecl) == VAR_DECL
2003               && TREE_STATIC (olddecl))))
2004     make_decl_rtl (olddecl);
2005
2006   /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2007      and the definition is coming from the old version, cgraph needs
2008      to be called again.  */
2009   if (extern_changed && !new_is_definition
2010       && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2011     cgraph_mark_if_needed (olddecl);
2012 }
2013
2014 /* Handle when a new declaration NEWDECL has the same name as an old
2015    one OLDDECL in the same binding contour.  Prints an error message
2016    if appropriate.
2017
2018    If safely possible, alter OLDDECL to look like NEWDECL, and return
2019    true.  Otherwise, return false.  */
2020
2021 static bool
2022 duplicate_decls (tree newdecl, tree olddecl)
2023 {
2024   tree newtype = NULL, oldtype = NULL;
2025
2026   if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2027     {
2028       /* Avoid `unused variable' and other warnings for OLDDECL.  */
2029       TREE_NO_WARNING (olddecl) = 1;
2030       return false;
2031     }
2032
2033   merge_decls (newdecl, olddecl, newtype, oldtype);
2034   return true;
2035 }
2036
2037 \f
2038 /* Check whether decl-node NEW_DECL shadows an existing declaration.  */
2039 static void
2040 warn_if_shadowing (tree new_decl)
2041 {
2042   struct c_binding *b;
2043
2044   /* Shadow warnings wanted?  */
2045   if (!warn_shadow
2046       /* No shadow warnings for internally generated vars.  */
2047       || DECL_IS_BUILTIN (new_decl)
2048       /* No shadow warnings for vars made for inlining.  */
2049       || DECL_FROM_INLINE (new_decl))
2050     return;
2051
2052   /* Is anything being shadowed?  Invisible decls do not count.  */
2053   for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2054     if (b->decl && b->decl != new_decl && !b->invisible)
2055       {
2056         tree old_decl = b->decl;
2057
2058         if (old_decl == error_mark_node)
2059           {
2060             warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2061                      "non-variable", new_decl);
2062             break;
2063           }
2064         else if (TREE_CODE (old_decl) == PARM_DECL)
2065           warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2066                    new_decl);
2067         else if (DECL_FILE_SCOPE_P (old_decl))
2068           warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2069                    "declaration", new_decl);
2070         else if (TREE_CODE (old_decl) == FUNCTION_DECL
2071                  && DECL_BUILT_IN (old_decl))
2072           {
2073             warning (OPT_Wshadow, "declaration of %q+D shadows "
2074                      "a built-in function", new_decl);
2075             break;
2076           }
2077         else
2078           warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2079                    new_decl);
2080
2081         warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2082
2083         break;
2084       }
2085 }
2086
2087 /* Record a decl-node X as belonging to the current lexical scope.
2088    Check for errors (such as an incompatible declaration for the same
2089    name already seen in the same scope).
2090
2091    Returns either X or an old decl for the same name.
2092    If an old decl is returned, it may have been smashed
2093    to agree with what X says.  */
2094
2095 tree
2096 pushdecl (tree x)
2097 {
2098   tree name = DECL_NAME (x);
2099   struct c_scope *scope = current_scope;
2100   struct c_binding *b;
2101   bool nested = false;
2102   location_t locus = DECL_SOURCE_LOCATION (x);
2103
2104   /* Must set DECL_CONTEXT for everything not at file scope or
2105      DECL_FILE_SCOPE_P won't work.  Local externs don't count
2106      unless they have initializers (which generate code).  */
2107   if (current_function_decl
2108       && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2109           || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2110     DECL_CONTEXT (x) = current_function_decl;
2111
2112   /* If this is of variably modified type, prevent jumping into its
2113      scope.  */
2114   if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2115       && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2116     c_begin_vm_scope (scope->depth);
2117
2118   /* Anonymous decls are just inserted in the scope.  */
2119   if (!name)
2120     {
2121       bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2122             locus);
2123       return x;
2124     }
2125
2126   /* First, see if there is another declaration with the same name in
2127      the current scope.  If there is, duplicate_decls may do all the
2128      work for us.  If duplicate_decls returns false, that indicates
2129      two incompatible decls in the same scope; we are to silently
2130      replace the old one (duplicate_decls has issued all appropriate
2131      diagnostics).  In particular, we should not consider possible
2132      duplicates in the external scope, or shadowing.  */
2133   b = I_SYMBOL_BINDING (name);
2134   if (b && B_IN_SCOPE (b, scope))
2135     {
2136       struct c_binding *b_ext, *b_use;
2137       tree type = TREE_TYPE (x);
2138       tree visdecl = b->decl;
2139       tree vistype = TREE_TYPE (visdecl);
2140       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2141           && COMPLETE_TYPE_P (TREE_TYPE (x)))
2142         b->inner_comp = false;
2143       b_use = b;
2144       b_ext = b;
2145       /* If this is an external linkage declaration, we should check
2146          for compatibility with the type in the external scope before
2147          setting the type at this scope based on the visible
2148          information only.  */
2149       if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2150         {
2151           while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2152             b_ext = b_ext->shadowed;
2153           if (b_ext)
2154             {
2155               b_use = b_ext;
2156               if (b_use->type)
2157                 TREE_TYPE (b_use->decl) = b_use->type;
2158             }
2159         }
2160       if (duplicate_decls (x, b_use->decl))
2161         {
2162           if (b_use != b)
2163             {
2164               /* Save the updated type in the external scope and
2165                  restore the proper type for this scope.  */
2166               tree thistype;
2167               if (comptypes (vistype, type))
2168                 thistype = composite_type (vistype, type);
2169               else
2170                 thistype = TREE_TYPE (b_use->decl);
2171               b_use->type = TREE_TYPE (b_use->decl);
2172               if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2173                   && DECL_BUILT_IN (b_use->decl))
2174                 thistype
2175                   = build_type_attribute_variant (thistype,
2176                                                   TYPE_ATTRIBUTES
2177                                                   (b_use->type));
2178               TREE_TYPE (b_use->decl) = thistype;
2179             }
2180           return b_use->decl;
2181         }
2182       else
2183         goto skip_external_and_shadow_checks;
2184     }
2185
2186   /* All declarations with external linkage, and all external
2187      references, go in the external scope, no matter what scope is
2188      current.  However, the binding in that scope is ignored for
2189      purposes of normal name lookup.  A separate binding structure is
2190      created in the requested scope; this governs the normal
2191      visibility of the symbol.
2192
2193      The binding in the externals scope is used exclusively for
2194      detecting duplicate declarations of the same object, no matter
2195      what scope they are in; this is what we do here.  (C99 6.2.7p2:
2196      All declarations that refer to the same object or function shall
2197      have compatible type; otherwise, the behavior is undefined.)  */
2198   if (DECL_EXTERNAL (x) || scope == file_scope)
2199     {
2200       tree type = TREE_TYPE (x);
2201       tree vistype = 0;
2202       tree visdecl = 0;
2203       bool type_saved = false;
2204       if (b && !B_IN_EXTERNAL_SCOPE (b)
2205           && (TREE_CODE (b->decl) == FUNCTION_DECL
2206               || TREE_CODE (b->decl) == VAR_DECL)
2207           && DECL_FILE_SCOPE_P (b->decl))
2208         {
2209           visdecl = b->decl;
2210           vistype = TREE_TYPE (visdecl);
2211         }
2212       if (scope != file_scope
2213           && !DECL_IN_SYSTEM_HEADER (x))
2214         warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2215
2216       while (b && !B_IN_EXTERNAL_SCOPE (b))
2217         {
2218           /* If this decl might be modified, save its type.  This is
2219              done here rather than when the decl is first bound
2220              because the type may change after first binding, through
2221              being completed or through attributes being added.  If we
2222              encounter multiple such decls, only the first should have
2223              its type saved; the others will already have had their
2224              proper types saved and the types will not have changed as
2225              their scopes will not have been re-entered.  */
2226           if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2227             {
2228               b->type = TREE_TYPE (b->decl);
2229               type_saved = true;
2230             }
2231           if (B_IN_FILE_SCOPE (b)
2232               && TREE_CODE (b->decl) == VAR_DECL
2233               && TREE_STATIC (b->decl)
2234               && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2235               && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2236               && TREE_CODE (type) == ARRAY_TYPE
2237               && TYPE_DOMAIN (type)
2238               && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2239               && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2240             {
2241               /* Array type completed in inner scope, which should be
2242                  diagnosed if the completion does not have size 1 and
2243                  it does not get completed in the file scope.  */
2244               b->inner_comp = true;
2245             }
2246           b = b->shadowed;
2247         }
2248
2249       /* If a matching external declaration has been found, set its
2250          type to the composite of all the types of that declaration.
2251          After the consistency checks, it will be reset to the
2252          composite of the visible types only.  */
2253       if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2254           && b->type)
2255         TREE_TYPE (b->decl) = b->type;
2256
2257       /* The point of the same_translation_unit_p check here is,
2258          we want to detect a duplicate decl for a construct like
2259          foo() { extern bar(); } ... static bar();  but not if
2260          they are in different translation units.  In any case,
2261          the static does not go in the externals scope.  */
2262       if (b
2263           && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2264           && duplicate_decls (x, b->decl))
2265         {
2266           tree thistype;
2267           if (vistype)
2268             {
2269               if (comptypes (vistype, type))
2270                 thistype = composite_type (vistype, type);
2271               else
2272                 thistype = TREE_TYPE (b->decl);
2273             }
2274           else
2275             thistype = type;
2276           b->type = TREE_TYPE (b->decl);
2277           if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2278             thistype
2279               = build_type_attribute_variant (thistype,
2280                                               TYPE_ATTRIBUTES (b->type));
2281           TREE_TYPE (b->decl) = thistype;
2282           bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2283                 locus);
2284           return b->decl;
2285         }
2286       else if (TREE_PUBLIC (x))
2287         {
2288           if (visdecl && !b && duplicate_decls (x, visdecl))
2289             {
2290               /* An external declaration at block scope referring to a
2291                  visible entity with internal linkage.  The composite
2292                  type will already be correct for this scope, so we
2293                  just need to fall through to make the declaration in
2294                  this scope.  */
2295               nested = true;
2296               x = visdecl;
2297             }
2298           else
2299             {
2300               bind (name, x, external_scope, /*invisible=*/true,
2301                     /*nested=*/false, locus);
2302               nested = true;
2303             }
2304         }
2305     }
2306
2307   if (TREE_CODE (x) != PARM_DECL)
2308     warn_if_shadowing (x);
2309
2310  skip_external_and_shadow_checks:
2311   if (TREE_CODE (x) == TYPE_DECL)
2312     set_underlying_type (x);
2313
2314   bind (name, x, scope, /*invisible=*/false, nested, locus);
2315
2316   /* If x's type is incomplete because it's based on a
2317      structure or union which has not yet been fully declared,
2318      attach it to that structure or union type, so we can go
2319      back and complete the variable declaration later, if the
2320      structure or union gets fully declared.
2321
2322      If the input is erroneous, we can have error_mark in the type
2323      slot (e.g. "f(void a, ...)") - that doesn't count as an
2324      incomplete type.  */
2325   if (TREE_TYPE (x) != error_mark_node
2326       && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2327     {
2328       tree element = TREE_TYPE (x);
2329
2330       while (TREE_CODE (element) == ARRAY_TYPE)
2331         element = TREE_TYPE (element);
2332       element = TYPE_MAIN_VARIANT (element);
2333
2334       if ((TREE_CODE (element) == RECORD_TYPE
2335            || TREE_CODE (element) == UNION_TYPE)
2336           && (TREE_CODE (x) != TYPE_DECL
2337               || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2338           && !COMPLETE_TYPE_P (element))
2339         C_TYPE_INCOMPLETE_VARS (element)
2340           = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2341     }
2342   return x;
2343 }
2344
2345 /* Record X as belonging to file scope.
2346    This is used only internally by the Objective-C front end,
2347    and is limited to its needs.  duplicate_decls is not called;
2348    if there is any preexisting decl for this identifier, it is an ICE.  */
2349
2350 tree
2351 pushdecl_top_level (tree x)
2352 {
2353   tree name;
2354   bool nested = false;
2355   gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2356
2357   name = DECL_NAME (x);
2358
2359  gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2360
2361   if (TREE_PUBLIC (x))
2362     {
2363       bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2364             UNKNOWN_LOCATION);
2365       nested = true;
2366     }
2367   if (file_scope)
2368     bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2369
2370   return x;
2371 }
2372 \f
2373 static void
2374 implicit_decl_warning (tree id, tree olddecl)
2375 {
2376   if (warn_implicit_function_declaration)
2377     {
2378       bool warned;
2379
2380       if (flag_isoc99)
2381         warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2382                           "implicit declaration of function %qE", id);
2383       else 
2384         warned = warning (OPT_Wimplicit_function_declaration, 
2385                           G_("implicit declaration of function %qE"), id);
2386       if (olddecl && warned)
2387         locate_old_decl (olddecl);
2388     }
2389 }
2390
2391 /* Generate an implicit declaration for identifier FUNCTIONID as a
2392    function of type int ().  */
2393
2394 tree
2395 implicitly_declare (tree functionid)
2396 {
2397   struct c_binding *b;
2398   tree decl = 0;
2399   tree asmspec_tree;
2400
2401   for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2402     {
2403       if (B_IN_SCOPE (b, external_scope))
2404         {
2405           decl = b->decl;
2406           break;
2407         }
2408     }
2409
2410   if (decl)
2411     {
2412       if (decl == error_mark_node)
2413         return decl;
2414
2415       /* FIXME: Objective-C has weird not-really-builtin functions
2416          which are supposed to be visible automatically.  They wind up
2417          in the external scope because they're pushed before the file
2418          scope gets created.  Catch this here and rebind them into the
2419          file scope.  */
2420       if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2421         {
2422           bind (functionid, decl, file_scope,
2423                 /*invisible=*/false, /*nested=*/true,
2424                 DECL_SOURCE_LOCATION (decl));
2425           return decl;
2426         }
2427       else
2428         {
2429           tree newtype = default_function_type;
2430           if (b->type)
2431             TREE_TYPE (decl) = b->type;
2432           /* Implicit declaration of a function already declared
2433              (somehow) in a different scope, or as a built-in.
2434              If this is the first time this has happened, warn;
2435              then recycle the old declaration but with the new type.  */
2436           if (!C_DECL_IMPLICIT (decl))
2437             {
2438               implicit_decl_warning (functionid, decl);
2439               C_DECL_IMPLICIT (decl) = 1;
2440             }
2441           if (DECL_BUILT_IN (decl))
2442             {
2443               newtype = build_type_attribute_variant (newtype,
2444                                                       TYPE_ATTRIBUTES
2445                                                       (TREE_TYPE (decl)));
2446               if (!comptypes (newtype, TREE_TYPE (decl)))
2447                 {
2448                   warning (0, "incompatible implicit declaration of built-in"
2449                            " function %qD", decl);
2450                   newtype = TREE_TYPE (decl);
2451                 }
2452             }
2453           else
2454             {
2455               if (!comptypes (newtype, TREE_TYPE (decl)))
2456                 {
2457                   error ("incompatible implicit declaration of function %qD",
2458                          decl);
2459                   locate_old_decl (decl);
2460                 }
2461             }
2462           b->type = TREE_TYPE (decl);
2463           TREE_TYPE (decl) = newtype;
2464           bind (functionid, decl, current_scope,
2465                 /*invisible=*/false, /*nested=*/true,
2466                 DECL_SOURCE_LOCATION (decl));
2467           return decl;
2468         }
2469     }
2470
2471   /* Not seen before.  */
2472   decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2473   DECL_EXTERNAL (decl) = 1;
2474   TREE_PUBLIC (decl) = 1;
2475   C_DECL_IMPLICIT (decl) = 1;
2476   implicit_decl_warning (functionid, 0);
2477   asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2478   if (asmspec_tree)
2479     set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2480
2481   /* C89 says implicit declarations are in the innermost block.
2482      So we record the decl in the standard fashion.  */
2483   decl = pushdecl (decl);
2484
2485   /* No need to call objc_check_decl here - it's a function type.  */
2486   rest_of_decl_compilation (decl, 0, 0);
2487
2488   /* Write a record describing this implicit function declaration
2489      to the prototypes file (if requested).  */
2490   gen_aux_info_record (decl, 0, 1, 0);
2491
2492   /* Possibly apply some default attributes to this implicit declaration.  */
2493   decl_attributes (&decl, NULL_TREE, 0);
2494
2495   return decl;
2496 }
2497
2498 /* Issue an error message for a reference to an undeclared variable
2499    ID, including a reference to a builtin outside of function-call
2500    context.  Establish a binding of the identifier to error_mark_node
2501    in an appropriate scope, which will suppress further errors for the
2502    same identifier.  The error message should be given location LOC.  */
2503 void
2504 undeclared_variable (tree id, location_t loc)
2505 {
2506   static bool already = false;
2507   struct c_scope *scope;
2508
2509   if (current_function_decl == 0)
2510     {
2511       error ("%H%qE undeclared here (not in a function)", &loc, id);
2512       scope = current_scope;
2513     }
2514   else
2515     {
2516       error ("%H%qE undeclared (first use in this function)", &loc, id);
2517
2518       if (!already)
2519         {
2520           error ("%H(Each undeclared identifier is reported only once", &loc);
2521           error ("%Hfor each function it appears in.)", &loc);
2522           already = true;
2523         }
2524
2525       /* If we are parsing old-style parameter decls, current_function_decl
2526          will be nonnull but current_function_scope will be null.  */
2527       scope = current_function_scope ? current_function_scope : current_scope;
2528     }
2529   bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2530         UNKNOWN_LOCATION);
2531 }
2532 \f
2533 /* Subroutine of lookup_label, declare_label, define_label: construct a
2534    LABEL_DECL with all the proper frills.  */
2535
2536 static tree
2537 make_label (tree name, location_t location)
2538 {
2539   tree label = build_decl (LABEL_DECL, name, void_type_node);
2540
2541   DECL_CONTEXT (label) = current_function_decl;
2542   DECL_MODE (label) = VOIDmode;
2543   DECL_SOURCE_LOCATION (label) = location;
2544
2545   return label;
2546 }
2547
2548 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2549    Create one if none exists so far for the current function.
2550    This is called when a label is used in a goto expression or
2551    has its address taken.  */
2552
2553 tree
2554 lookup_label (tree name)
2555 {
2556   tree label;
2557
2558   if (current_function_decl == 0)
2559     {
2560       error ("label %qE referenced outside of any function", name);
2561       return 0;
2562     }
2563
2564   /* Use a label already defined or ref'd with this name, but not if
2565      it is inherited from a containing function and wasn't declared
2566      using __label__.  */
2567   label = I_LABEL_DECL (name);
2568   if (label && (DECL_CONTEXT (label) == current_function_decl
2569                 || C_DECLARED_LABEL_FLAG (label)))
2570     {
2571       /* If the label has only been declared, update its apparent
2572          location to point here, for better diagnostics if it
2573          turns out not to have been defined.  */
2574       if (!TREE_USED (label))
2575         DECL_SOURCE_LOCATION (label) = input_location;
2576       return label;
2577     }
2578
2579   /* No label binding for that identifier; make one.  */
2580   label = make_label (name, input_location);
2581
2582   /* Ordinary labels go in the current function scope.  */
2583   bind (name, label, current_function_scope,
2584         /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2585   return label;
2586 }
2587
2588 /* Make a label named NAME in the current function, shadowing silently
2589    any that may be inherited from containing functions or containing
2590    scopes.  This is called for __label__ declarations.  */
2591
2592 tree
2593 declare_label (tree name)
2594 {
2595   struct c_binding *b = I_LABEL_BINDING (name);
2596   tree label;
2597
2598   /* Check to make sure that the label hasn't already been declared
2599      at this scope */
2600   if (b && B_IN_CURRENT_SCOPE (b))
2601     {
2602       error ("duplicate label declaration %qE", name);
2603       locate_old_decl (b->decl);
2604
2605       /* Just use the previous declaration.  */
2606       return b->decl;
2607     }
2608
2609   label = make_label (name, input_location);
2610   C_DECLARED_LABEL_FLAG (label) = 1;
2611
2612   /* Declared labels go in the current scope.  */
2613   bind (name, label, current_scope,
2614         /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2615   return label;
2616 }
2617
2618 /* Define a label, specifying the location in the source file.
2619    Return the LABEL_DECL node for the label, if the definition is valid.
2620    Otherwise return 0.  */
2621
2622 tree
2623 define_label (location_t location, tree name)
2624 {
2625   /* Find any preexisting label with this name.  It is an error
2626      if that label has already been defined in this function, or
2627      if there is a containing function with a declared label with
2628      the same name.  */
2629   tree label = I_LABEL_DECL (name);
2630   struct c_label_list *nlist_se, *nlist_vm;
2631
2632   if (label
2633       && ((DECL_CONTEXT (label) == current_function_decl
2634            && DECL_INITIAL (label) != 0)
2635           || (DECL_CONTEXT (label) != current_function_decl
2636               && C_DECLARED_LABEL_FLAG (label))))
2637     {
2638       error ("%Hduplicate label %qD", &location, label);
2639       locate_old_decl (label);
2640       return 0;
2641     }
2642   else if (label && DECL_CONTEXT (label) == current_function_decl)
2643     {
2644       /* The label has been used or declared already in this function,
2645          but not defined.  Update its location to point to this
2646          definition.  */
2647       if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2648         error ("%Jjump into statement expression", label);
2649       if (C_DECL_UNDEFINABLE_VM (label))
2650         error ("%Jjump into scope of identifier with variably modified type",
2651                label);
2652       DECL_SOURCE_LOCATION (label) = location;
2653     }
2654   else
2655     {
2656       /* No label binding for that identifier; make one.  */
2657       label = make_label (name, location);
2658
2659       /* Ordinary labels go in the current function scope.  */
2660       bind (name, label, current_function_scope,
2661             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2662     }
2663
2664   if (!in_system_header && lookup_name (name))
2665     warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2666              "for labels, identifier %qE conflicts", &location, name);
2667
2668   nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2669   nlist_se->next = label_context_stack_se->labels_def;
2670   nlist_se->label = label;
2671   label_context_stack_se->labels_def = nlist_se;
2672
2673   nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2674   nlist_vm->next = label_context_stack_vm->labels_def;
2675   nlist_vm->label = label;
2676   label_context_stack_vm->labels_def = nlist_vm;
2677
2678   /* Mark label as having been defined.  */
2679   DECL_INITIAL (label) = error_mark_node;
2680   return label;
2681 }
2682 \f
2683 /* Given NAME, an IDENTIFIER_NODE,
2684    return the structure (or union or enum) definition for that name.
2685    If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2686    CODE says which kind of type the caller wants;
2687    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2688    If the wrong kind of type is found, an error is reported.  */
2689
2690 static tree
2691 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2692 {
2693   struct c_binding *b = I_TAG_BINDING (name);
2694   int thislevel = 0;
2695
2696   if (!b || !b->decl)
2697     return 0;
2698
2699   /* We only care about whether it's in this level if
2700      thislevel_only was set or it might be a type clash.  */
2701   if (thislevel_only || TREE_CODE (b->decl) != code)
2702     {
2703       /* For our purposes, a tag in the external scope is the same as
2704          a tag in the file scope.  (Primarily relevant to Objective-C
2705          and its builtin structure tags, which get pushed before the
2706          file scope is created.)  */
2707       if (B_IN_CURRENT_SCOPE (b)
2708           || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2709         thislevel = 1;
2710     }
2711
2712   if (thislevel_only && !thislevel)
2713     return 0;
2714
2715   if (TREE_CODE (b->decl) != code)
2716     {
2717       /* Definition isn't the kind we were looking for.  */
2718       pending_invalid_xref = name;
2719       pending_invalid_xref_location = input_location;
2720
2721       /* If in the same binding level as a declaration as a tag
2722          of a different type, this must not be allowed to
2723          shadow that tag, so give the error immediately.
2724          (For example, "struct foo; union foo;" is invalid.)  */
2725       if (thislevel)
2726         pending_xref_error ();
2727     }
2728   return b->decl;
2729 }
2730
2731 /* Print an error message now
2732    for a recent invalid struct, union or enum cross reference.
2733    We don't print them immediately because they are not invalid
2734    when used in the `struct foo;' construct for shadowing.  */
2735
2736 void
2737 pending_xref_error (void)
2738 {
2739   if (pending_invalid_xref != 0)
2740     error ("%H%qE defined as wrong kind of tag",
2741            &pending_invalid_xref_location, pending_invalid_xref);
2742   pending_invalid_xref = 0;
2743 }
2744
2745 \f
2746 /* Look up NAME in the current scope and its superiors
2747    in the namespace of variables, functions and typedefs.
2748    Return a ..._DECL node of some kind representing its definition,
2749    or return 0 if it is undefined.  */
2750
2751 tree
2752 lookup_name (tree name)
2753 {
2754   struct c_binding *b = I_SYMBOL_BINDING (name);
2755   if (b && !b->invisible)
2756     return b->decl;
2757   return 0;
2758 }
2759
2760 /* Similar to `lookup_name' but look only at the indicated scope.  */
2761
2762 static tree
2763 lookup_name_in_scope (tree name, struct c_scope *scope)
2764 {
2765   struct c_binding *b;
2766
2767   for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2768     if (B_IN_SCOPE (b, scope))
2769       return b->decl;
2770   return 0;
2771 }
2772 \f
2773 /* Create the predefined scalar types of C,
2774    and some nodes representing standard constants (0, 1, (void *) 0).
2775    Initialize the global scope.
2776    Make definitions for built-in primitive functions.  */
2777
2778 void
2779 c_init_decl_processing (void)
2780 {
2781   location_t save_loc = input_location;
2782
2783   /* Initialize reserved words for parser.  */
2784   c_parse_init ();
2785
2786   current_function_decl = 0;
2787
2788   gcc_obstack_init (&parser_obstack);
2789
2790   /* Make the externals scope.  */
2791   push_scope ();
2792   external_scope = current_scope;
2793
2794   /* Declarations from c_common_nodes_and_builtins must not be associated
2795      with this input file, lest we get differences between using and not
2796      using preprocessed headers.  */
2797   input_location = BUILTINS_LOCATION;
2798
2799   build_common_tree_nodes (flag_signed_char, false);
2800
2801   c_common_nodes_and_builtins ();
2802
2803   /* In C, comparisons and TRUTH_* expressions have type int.  */
2804   truthvalue_type_node = integer_type_node;
2805   truthvalue_true_node = integer_one_node;
2806   truthvalue_false_node = integer_zero_node;
2807
2808   /* Even in C99, which has a real boolean type.  */
2809   pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2810                         boolean_type_node));
2811
2812   input_location = save_loc;
2813
2814   pedantic_lvalues = true;
2815
2816   make_fname_decl = c_make_fname_decl;
2817   start_fname_decls ();
2818 }
2819
2820 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2821    decl, NAME is the initialization string and TYPE_DEP indicates whether
2822    NAME depended on the type of the function.  As we don't yet implement
2823    delayed emission of static data, we mark the decl as emitted
2824    so it is not placed in the output.  Anything using it must therefore pull
2825    out the STRING_CST initializer directly.  FIXME.  */
2826
2827 static tree
2828 c_make_fname_decl (tree id, int type_dep)
2829 {
2830   const char *name = fname_as_string (type_dep);
2831   tree decl, type, init;
2832   size_t length = strlen (name);
2833
2834   type = build_array_type (char_type_node,
2835                            build_index_type (size_int (length)));
2836   type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2837
2838   decl = build_decl (VAR_DECL, id, type);
2839
2840   TREE_STATIC (decl) = 1;
2841   TREE_READONLY (decl) = 1;
2842   DECL_ARTIFICIAL (decl) = 1;
2843
2844   init = build_string (length + 1, name);
2845   free (CONST_CAST (char *, name));
2846   TREE_TYPE (init) = type;
2847   DECL_INITIAL (decl) = init;
2848
2849   TREE_USED (decl) = 1;
2850
2851   if (current_function_decl
2852       /* For invalid programs like this:
2853         
2854          void foo()
2855          const char* p = __FUNCTION__;
2856         
2857          the __FUNCTION__ is believed to appear in K&R style function
2858          parameter declarator.  In that case we still don't have
2859          function_scope.  */
2860       && (!errorcount || current_function_scope))
2861     {
2862       DECL_CONTEXT (decl) = current_function_decl;
2863       bind (id, decl, current_function_scope,
2864             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2865     }
2866
2867   finish_decl (decl, init, NULL_TREE, NULL_TREE);
2868
2869   return decl;
2870 }
2871
2872 tree
2873 c_builtin_function (tree decl)
2874 {
2875   tree type = TREE_TYPE (decl);
2876   tree   id = DECL_NAME (decl);
2877
2878   const char *name = IDENTIFIER_POINTER (id);
2879   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2880
2881   /* Should never be called on a symbol with a preexisting meaning.  */
2882   gcc_assert (!I_SYMBOL_BINDING (id));
2883
2884   bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2885         UNKNOWN_LOCATION);
2886
2887   /* Builtins in the implementation namespace are made visible without
2888      needing to be explicitly declared.  See push_file_scope.  */
2889   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2890     {
2891       TREE_CHAIN (decl) = visible_builtins;
2892       visible_builtins = decl;
2893     }
2894
2895   return decl;
2896 }
2897
2898 tree
2899 c_builtin_function_ext_scope (tree decl)
2900 {
2901   tree type = TREE_TYPE (decl);
2902   tree   id = DECL_NAME (decl);
2903
2904   const char *name = IDENTIFIER_POINTER (id);
2905   C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2906
2907   /* Should never be called on a symbol with a preexisting meaning.  */
2908   gcc_assert (!I_SYMBOL_BINDING (id));
2909
2910   bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2911         UNKNOWN_LOCATION);
2912
2913   /* Builtins in the implementation namespace are made visible without
2914      needing to be explicitly declared.  See push_file_scope.  */
2915   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2916     {
2917       TREE_CHAIN (decl) = visible_builtins;
2918       visible_builtins = decl;
2919     }
2920
2921   return decl;
2922 }
2923 \f
2924 /* Called when a declaration is seen that contains no names to declare.
2925    If its type is a reference to a structure, union or enum inherited
2926    from a containing scope, shadow that tag name for the current scope
2927    with a forward reference.
2928    If its type defines a new named structure or union
2929    or defines an enum, it is valid but we need not do anything here.
2930    Otherwise, it is an error.  */
2931
2932 void
2933 shadow_tag (const struct c_declspecs *declspecs)
2934 {
2935   shadow_tag_warned (declspecs, 0);
2936 }
2937
2938 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2939    but no pedwarn.  */
2940 void
2941 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2942 {
2943   bool found_tag = false;
2944
2945   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2946     {
2947       tree value = declspecs->type;
2948       enum tree_code code = TREE_CODE (value);
2949
2950       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2951         /* Used to test also that TYPE_SIZE (value) != 0.
2952            That caused warning for `struct foo;' at top level in the file.  */
2953         {
2954           tree name = TYPE_NAME (value);
2955           tree t;
2956
2957           found_tag = true;
2958
2959           if (declspecs->restrict_p)
2960             {
2961               error ("invalid use of %<restrict%>");
2962               warned = 1;
2963             }
2964
2965           if (name == 0)
2966             {
2967               if (warned != 1 && code != ENUMERAL_TYPE)
2968                 /* Empty unnamed enum OK */
2969                 {
2970                   pedwarn (input_location, 0,
2971                            "unnamed struct/union that defines no instances");
2972                   warned = 1;
2973                 }
2974             }
2975           else if (!declspecs->tag_defined_p
2976                    && declspecs->storage_class != csc_none)
2977             {
2978               if (warned != 1)
2979                 pedwarn (input_location, 0,
2980                          "empty declaration with storage class specifier "
2981                          "does not redeclare tag");
2982               warned = 1;
2983               pending_xref_error ();
2984             }
2985           else if (!declspecs->tag_defined_p
2986                    && (declspecs->const_p
2987                        || declspecs->volatile_p
2988                        || declspecs->restrict_p))
2989             {
2990               if (warned != 1)
2991                 pedwarn (input_location, 0,
2992                          "empty declaration with type qualifier "
2993                           "does not redeclare tag");
2994               warned = 1;
2995               pending_xref_error ();
2996             }
2997           else
2998             {
2999               pending_invalid_xref = 0;
3000               t = lookup_tag (code, name, 1);
3001
3002               if (t == 0)
3003                 {
3004                   t = make_node (code);
3005                   pushtag (name, t);
3006                 }
3007             }
3008         }
3009       else
3010         {
3011           if (warned != 1 && !in_system_header)
3012             {
3013               pedwarn (input_location, 0,
3014                        "useless type name in empty declaration");
3015               warned = 1;
3016             }
3017         }
3018     }
3019   else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3020     {
3021       pedwarn (input_location, 0, "useless type name in empty declaration");
3022       warned = 1;
3023     }
3024
3025   pending_invalid_xref = 0;
3026
3027   if (declspecs->inline_p)
3028     {
3029       error ("%<inline%> in empty declaration");
3030       warned = 1;
3031     }
3032
3033   if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3034     {
3035       error ("%<auto%> in file-scope empty declaration");
3036       warned = 1;
3037     }
3038
3039   if (current_scope == file_scope && declspecs->storage_class == csc_register)
3040     {
3041       error ("%<register%> in file-scope empty declaration");
3042       warned = 1;
3043     }
3044
3045   if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3046     {
3047       warning (0, "useless storage class specifier in empty declaration");
3048       warned = 2;
3049     }
3050
3051   if (!warned && !in_system_header && declspecs->thread_p)
3052     {
3053       warning (0, "useless %<__thread%> in empty declaration");
3054       warned = 2;
3055     }
3056
3057   if (!warned && !in_system_header && (declspecs->const_p
3058                                        || declspecs->volatile_p
3059                                        || declspecs->restrict_p))
3060     {
3061       warning (0, "useless type qualifier in empty declaration");
3062       warned = 2;
3063     }
3064
3065   if (warned != 1)
3066     {
3067       if (!found_tag)
3068         pedwarn (input_location, 0, "empty declaration");
3069     }
3070 }
3071 \f
3072
3073 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3074    bits.  SPECS represents declaration specifiers that the grammar
3075    only permits to contain type qualifiers and attributes.  */
3076
3077 int
3078 quals_from_declspecs (const struct c_declspecs *specs)
3079 {
3080   int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3081                | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3082                | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3083   gcc_assert (!specs->type
3084               && !specs->decl_attr
3085               && specs->typespec_word == cts_none
3086               && specs->storage_class == csc_none
3087               && !specs->typedef_p
3088               && !specs->explicit_signed_p
3089               && !specs->deprecated_p
3090               && !specs->long_p
3091               && !specs->long_long_p
3092               && !specs->short_p
3093               && !specs->signed_p
3094               && !specs->unsigned_p
3095               && !specs->complex_p
3096               && !specs->inline_p
3097               && !specs->thread_p);
3098   return quals;
3099 }
3100
3101 /* Construct an array declarator.  EXPR is the expression inside [],
3102    or NULL_TREE.  QUALS are the type qualifiers inside the [] (to be
3103    applied to the pointer to which a parameter array is converted).
3104    STATIC_P is true if "static" is inside the [], false otherwise.
3105    VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3106    length which is nevertheless a complete type, false otherwise.  The
3107    field for the contained declarator is left to be filled in by
3108    set_array_declarator_inner.  */
3109
3110 struct c_declarator *
3111 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3112                         bool vla_unspec_p)
3113 {
3114   struct c_declarator *declarator = XOBNEW (&parser_obstack,
3115                                             struct c_declarator);
3116   declarator->kind = cdk_array;
3117   declarator->declarator = 0;
3118   declarator->u.array.dimen = expr;
3119   if (quals)
3120     {
3121       declarator->u.array.attrs = quals->attrs;
3122       declarator->u.array.quals = quals_from_declspecs (quals);
3123     }
3124   else
3125     {
3126       declarator->u.array.attrs = NULL_TREE;
3127       declarator->u.array.quals = 0;
3128     }
3129   declarator->u.array.static_p = static_p;
3130   declarator->u.array.vla_unspec_p = vla_unspec_p;
3131   if (!flag_isoc99)
3132     {
3133       if (static_p || quals != NULL)
3134         pedwarn (input_location, OPT_pedantic,
3135                  "ISO C90 does not support %<static%> or type "
3136                  "qualifiers in parameter array declarators");
3137       if (vla_unspec_p)
3138         pedwarn (input_location, OPT_pedantic,
3139                  "ISO C90 does not support %<[*]%> array declarators");
3140     }
3141   if (vla_unspec_p)
3142     {
3143       if (!current_scope->parm_flag)
3144         {
3145           /* C99 6.7.5.2p4 */
3146           error ("%<[*]%> not allowed in other than function prototype scope");
3147           declarator->u.array.vla_unspec_p = false;
3148           return NULL;
3149         }
3150       current_scope->had_vla_unspec = true;
3151     }
3152   return declarator;
3153 }
3154
3155 /* Set the contained declarator of an array declarator.  DECL is the
3156    declarator, as constructed by build_array_declarator; INNER is what
3157    appears on the left of the [].  */
3158
3159 struct c_declarator *
3160 set_array_declarator_inner (struct c_declarator *decl,
3161                             struct c_declarator *inner)
3162 {
3163   decl->declarator = inner;
3164   return decl;
3165 }
3166
3167 /* INIT is a constructor that forms DECL's initializer.  If the final
3168    element initializes a flexible array field, add the size of that
3169    initializer to DECL's size.  */
3170
3171 static void
3172 add_flexible_array_elts_to_size (tree decl, tree init)
3173 {
3174   tree elt, type;
3175
3176   if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3177     return;
3178
3179   elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3180   type = TREE_TYPE (elt);
3181   if (TREE_CODE (type) == ARRAY_TYPE
3182       && TYPE_SIZE (type) == NULL_TREE
3183       && TYPE_DOMAIN (type) != NULL_TREE
3184       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3185     {
3186       complete_array_type (&type, elt, false);
3187       DECL_SIZE (decl)
3188         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3189       DECL_SIZE_UNIT (decl)
3190         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3191     }
3192 }
3193 \f
3194 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3195    Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3196    before the type name, and set *EXPR_CONST_OPERANDS, if
3197    EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3198    appear in a constant expression.  */
3199
3200 tree
3201 groktypename (struct c_type_name *type_name, tree *expr,
3202               bool *expr_const_operands)
3203 {
3204   tree type;
3205   tree attrs = type_name->specs->attrs;
3206
3207   type_name->specs->attrs = NULL_TREE;
3208
3209   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3210                          false, NULL, &attrs, expr, expr_const_operands,
3211                          DEPRECATED_NORMAL);
3212
3213   /* Apply attributes.  */
3214   decl_attributes (&type, attrs, 0);
3215
3216   return type;
3217 }
3218
3219 /* Decode a declarator in an ordinary declaration or data definition.
3220    This is called as soon as the type information and variable name
3221    have been parsed, before parsing the initializer if any.
3222    Here we create the ..._DECL node, fill in its type,
3223    and put it on the list of decls for the current context.
3224    The ..._DECL node is returned as the value.
3225
3226    Exception: for arrays where the length is not specified,
3227    the type is left null, to be filled in by `finish_decl'.
3228
3229    Function definitions do not come here; they go to start_function
3230    instead.  However, external and forward declarations of functions
3231    do go through here.  Structure field declarations are done by
3232    grokfield and not through here.  */
3233
3234 tree
3235 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3236             bool initialized, tree attributes)
3237 {
3238   tree decl;
3239   tree tem;
3240   tree expr = NULL_TREE;
3241   enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3242
3243   /* An object declared as __attribute__((deprecated)) suppresses
3244      warnings of uses of other deprecated items.  */
3245   if (lookup_attribute ("deprecated", attributes))
3246     deprecated_state = DEPRECATED_SUPPRESS;
3247
3248   decl = grokdeclarator (declarator, declspecs,
3249                          NORMAL, initialized, NULL, &attributes, &expr, NULL,
3250                          deprecated_state);
3251   if (!decl)
3252     return 0;
3253
3254   if (expr)
3255     add_stmt (expr);
3256
3257   if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3258     warning (OPT_Wmain, "%q+D is usually a function", decl);
3259
3260   if (initialized)
3261     /* Is it valid for this decl to have an initializer at all?
3262        If not, set INITIALIZED to zero, which will indirectly
3263        tell 'finish_decl' to ignore the initializer once it is parsed.  */
3264     switch (TREE_CODE (decl))
3265       {
3266       case TYPE_DECL:
3267         error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3268         initialized = 0;
3269         break;
3270
3271       case FUNCTION_DECL:
3272         error ("function %qD is initialized like a variable", decl);
3273         initialized = 0;
3274         break;
3275
3276       case PARM_DECL:
3277         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3278         error ("parameter %qD is initialized", decl);
3279         initialized = 0;
3280         break;
3281
3282       default:
3283         /* Don't allow initializations for incomplete types except for
3284            arrays which might be completed by the initialization.  */
3285
3286         /* This can happen if the array size is an undefined macro.
3287            We already gave a warning, so we don't need another one.  */
3288         if (TREE_TYPE (decl) == error_mark_node)
3289           initialized = 0;
3290         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3291           {
3292             /* A complete type is ok if size is fixed.  */
3293
3294             if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3295                 || C_DECL_VARIABLE_SIZE (decl))
3296               {
3297                 error ("variable-sized object may not be initialized");
3298                 initialized = 0;
3299               }
3300           }
3301         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3302           {
3303             error ("variable %qD has initializer but incomplete type", decl);
3304             initialized = 0;
3305           }
3306         else if (C_DECL_VARIABLE_SIZE (decl))
3307           {
3308             /* Although C99 is unclear about whether incomplete arrays
3309                of VLAs themselves count as VLAs, it does not make
3310                sense to permit them to be initialized given that
3311                ordinary VLAs may not be initialized.  */
3312             error ("variable-sized object may not be initialized");
3313             initialized = 0;
3314           }
3315       }
3316
3317   if (initialized)
3318     {
3319       if (current_scope == file_scope)
3320         TREE_STATIC (decl) = 1;
3321
3322       /* Tell 'pushdecl' this is an initialized decl
3323          even though we don't yet have the initializer expression.
3324          Also tell 'finish_decl' it may store the real initializer.  */
3325       DECL_INITIAL (decl) = error_mark_node;
3326     }
3327
3328   /* If this is a function declaration, write a record describing it to the
3329      prototypes file (if requested).  */
3330
3331   if (TREE_CODE (decl) == FUNCTION_DECL)
3332     gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3333
3334   /* ANSI specifies that a tentative definition which is not merged with
3335      a non-tentative definition behaves exactly like a definition with an
3336      initializer equal to zero.  (Section 3.7.2)
3337
3338      -fno-common gives strict ANSI behavior, though this tends to break
3339      a large body of code that grew up without this rule.
3340
3341      Thread-local variables are never common, since there's no entrenched
3342      body of code to break, and it allows more efficient variable references
3343      in the presence of dynamic linking.  */
3344
3345   if (TREE_CODE (decl) == VAR_DECL
3346       && !initialized
3347       && TREE_PUBLIC (decl)
3348       && !DECL_THREAD_LOCAL_P (decl)
3349       && !flag_no_common)
3350     DECL_COMMON (decl) = 1;
3351
3352   /* Set attributes here so if duplicate decl, will have proper attributes.  */
3353   decl_attributes (&decl, attributes, 0);
3354
3355   /* Handle gnu_inline attribute.  */
3356   if (declspecs->inline_p
3357       && !flag_gnu89_inline
3358       && TREE_CODE (decl) == FUNCTION_DECL
3359       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3360           || current_function_decl))
3361     {
3362       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3363         ;
3364       else if (declspecs->storage_class != csc_static)
3365         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3366     }
3367
3368   if (TREE_CODE (decl) == FUNCTION_DECL
3369       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3370     {
3371       struct c_declarator *ce = declarator;
3372
3373       if (ce->kind == cdk_pointer)
3374         ce = declarator->declarator;
3375       if (ce->kind == cdk_function)
3376         {
3377           tree args = ce->u.arg_info->parms;
3378           for (; args; args = TREE_CHAIN (args))
3379             {
3380               tree type = TREE_TYPE (args);
3381               if (type && INTEGRAL_TYPE_P (type)
3382                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3383                 DECL_ARG_TYPE (args) = integer_type_node;
3384             }
3385         }
3386     }
3387
3388   if (TREE_CODE (decl) == FUNCTION_DECL
3389       && DECL_DECLARED_INLINE_P (decl)
3390       && DECL_UNINLINABLE (decl)
3391       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3392     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3393              decl);
3394
3395   /* C99 6.7.4p3: An inline definition of a function with external
3396      linkage shall not contain a definition of a modifiable object
3397      with static storage duration...  */
3398   if (TREE_CODE (decl) == VAR_DECL
3399       && current_scope != file_scope
3400       && TREE_STATIC (decl)
3401       && !TREE_READONLY (decl)
3402       && DECL_DECLARED_INLINE_P (current_function_decl)
3403       && DECL_EXTERNAL (current_function_decl))
3404     record_inline_static (input_location, current_function_decl,
3405                           decl, csi_modifiable);
3406
3407   /* Add this decl to the current scope.
3408      TEM may equal DECL or it may be a previous decl of the same name.  */
3409   tem = pushdecl (decl);
3410
3411   if (initialized && DECL_EXTERNAL (tem))
3412     {
3413       DECL_EXTERNAL (tem) = 0;
3414       TREE_STATIC (tem) = 1;
3415     }
3416
3417   return tem;
3418 }
3419
3420 /* Initialize EH if not initialized yet and exceptions are enabled.  */
3421
3422 void
3423 c_maybe_initialize_eh (void)
3424 {
3425   if (!flag_exceptions || c_eh_initialized_p)
3426     return;
3427
3428   c_eh_initialized_p = true;
3429   eh_personality_libfunc
3430     = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3431                         ? "__gcc_personality_sj0"
3432                         : "__gcc_personality_v0");
3433   default_init_unwind_resume_libfunc ();
3434   using_eh_for_cleanups ();
3435 }
3436
3437 /* Finish processing of a declaration;
3438    install its initial value.
3439    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3440    If the length of an array type is not known before,
3441    it must be determined now, from the initial value, or it is an error.  */
3442
3443 void
3444 finish_decl (tree decl, tree init, tree origtype, tree asmspec_tree)
3445 {
3446   tree type;
3447   int was_incomplete = (DECL_SIZE (decl) == 0);
3448   const char *asmspec = 0;
3449
3450   /* If a name was specified, get the string.  */
3451   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3452       && DECL_FILE_SCOPE_P (decl))
3453     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3454   if (asmspec_tree)
3455     asmspec = TREE_STRING_POINTER (asmspec_tree);
3456
3457   /* If `start_decl' didn't like having an initialization, ignore it now.  */
3458   if (init != 0 && DECL_INITIAL (decl) == 0)
3459     init = 0;
3460
3461   /* Don't crash if parm is initialized.  */
3462   if (TREE_CODE (decl) == PARM_DECL)
3463     init = 0;
3464
3465   if (init)
3466     store_init_value (decl, init, origtype);
3467
3468   if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3469                             || TREE_CODE (decl) == FUNCTION_DECL
3470                             || TREE_CODE (decl) == FIELD_DECL))
3471     objc_check_decl (decl);
3472
3473   type = TREE_TYPE (decl);
3474
3475   /* Deduce size of array from initialization, if not already known.  */
3476   if (TREE_CODE (type) == ARRAY_TYPE
3477       && TYPE_DOMAIN (type) == 0
3478       && TREE_CODE (decl) != TYPE_DECL)
3479     {
3480       bool do_default
3481         = (TREE_STATIC (decl)
3482            /* Even if pedantic, an external linkage array
3483               may have incomplete type at first.  */
3484            ? pedantic && !TREE_PUBLIC (decl)
3485            : !DECL_EXTERNAL (decl));
3486       int failure
3487         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3488                                do_default);
3489
3490       /* Get the completed type made by complete_array_type.  */
3491       type = TREE_TYPE (decl);
3492
3493       switch (failure)
3494         {
3495         case 1:
3496           error ("initializer fails to determine size of %q+D", decl);
3497           break;
3498
3499         case 2:
3500           if (do_default)
3501             error ("array size missing in %q+D", decl);
3502           /* If a `static' var's size isn't known,
3503              make it extern as well as static, so it does not get
3504              allocated.
3505              If it is not `static', then do not mark extern;
3506              finish_incomplete_decl will give it a default size
3507              and it will get allocated.  */
3508           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3509             DECL_EXTERNAL (decl) = 1;
3510           break;
3511
3512         case 3:
3513           error ("zero or negative size array %q+D", decl);
3514           break;
3515
3516         case 0:
3517           /* For global variables, update the copy of the type that
3518              exists in the binding.  */
3519           if (TREE_PUBLIC (decl))
3520             {
3521               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3522               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3523                 b_ext = b_ext->shadowed;
3524               if (b_ext)
3525                 {
3526                   if (b_ext->type)
3527                     b_ext->type = composite_type (b_ext->type, type);
3528                   else
3529                     b_ext->type = type;
3530                 }
3531             }
3532           break;
3533
3534         default:
3535           gcc_unreachable ();
3536         }
3537
3538       if (DECL_INITIAL (decl))
3539         TREE_TYPE (DECL_INITIAL (decl)) = type;
3540
3541       layout_decl (decl, 0);
3542     }
3543
3544   if (TREE_CODE (decl) == VAR_DECL)
3545     {
3546       if (init && TREE_CODE (init) == CONSTRUCTOR)
3547         add_flexible_array_elts_to_size (decl, init);
3548
3549       if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3550           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3551         layout_decl (decl, 0);
3552
3553       if (DECL_SIZE (decl) == 0
3554           /* Don't give an error if we already gave one earlier.  */
3555           && TREE_TYPE (decl) != error_mark_node
3556           && (TREE_STATIC (decl)
3557               /* A static variable with an incomplete type
3558                  is an error if it is initialized.
3559                  Also if it is not file scope.
3560                  Otherwise, let it through, but if it is not `extern'
3561                  then it may cause an error message later.  */
3562               ? (DECL_INITIAL (decl) != 0
3563                  || !DECL_FILE_SCOPE_P (decl))
3564               /* An automatic variable with an incomplete type
3565                  is an error.  */
3566               : !DECL_EXTERNAL (decl)))
3567          {
3568            error ("storage size of %q+D isn%'t known", decl);
3569            TREE_TYPE (decl) = error_mark_node;
3570          }
3571
3572       if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3573           && DECL_SIZE (decl) != 0)
3574         {
3575           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3576             constant_expression_warning (DECL_SIZE (decl));
3577           else
3578             {
3579               error ("storage size of %q+D isn%'t constant", decl);
3580               TREE_TYPE (decl) = error_mark_node;
3581             }
3582         }
3583
3584       if (TREE_USED (type))
3585         TREE_USED (decl) = 1;
3586     }
3587
3588   /* If this is a function and an assembler name is specified, reset DECL_RTL
3589      so we can give it its new name.  Also, update built_in_decls if it
3590      was a normal built-in.  */
3591   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3592     {
3593       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3594         set_builtin_user_assembler_name (decl, asmspec);
3595       set_user_assembler_name (decl, asmspec);
3596     }
3597
3598   /* If #pragma weak was used, mark the decl weak now.  */
3599   maybe_apply_pragma_weak (decl);
3600
3601   /* Output the assembler code and/or RTL code for variables and functions,
3602      unless the type is an undefined structure or union.
3603      If not, it will get done when the type is completed.  */
3604
3605   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3606     {
3607       /* Determine the ELF visibility.  */
3608       if (TREE_PUBLIC (decl))
3609         c_determine_visibility (decl);
3610
3611       /* This is a no-op in c-lang.c or something real in objc-act.c.  */
3612       if (c_dialect_objc ())
3613         objc_check_decl (decl);
3614
3615       if (asmspec)
3616         {
3617           /* If this is not a static variable, issue a warning.
3618              It doesn't make any sense to give an ASMSPEC for an
3619              ordinary, non-register local variable.  Historically,
3620              GCC has accepted -- but ignored -- the ASMSPEC in
3621              this case.  */
3622           if (!DECL_FILE_SCOPE_P (decl)
3623               && TREE_CODE (decl) == VAR_DECL
3624               && !C_DECL_REGISTER (decl)
3625               && !TREE_STATIC (decl))
3626             warning (0, "ignoring asm-specifier for non-static local "
3627                      "variable %q+D", decl);
3628           else
3629             set_user_assembler_name (decl, asmspec);
3630         }
3631
3632       if (DECL_FILE_SCOPE_P (decl))
3633         {
3634           if (DECL_INITIAL (decl) == NULL_TREE
3635               || DECL_INITIAL (decl) == error_mark_node)
3636             /* Don't output anything
3637                when a tentative file-scope definition is seen.
3638                But at end of compilation, do output code for them.  */
3639             DECL_DEFER_OUTPUT (decl) = 1;
3640           rest_of_decl_compilation (decl, true, 0);
3641         }
3642       else
3643         {
3644           /* In conjunction with an ASMSPEC, the `register'
3645              keyword indicates that we should place the variable
3646              in a particular register.  */
3647           if (asmspec && C_DECL_REGISTER (decl))
3648             {
3649               DECL_HARD_REGISTER (decl) = 1;
3650               /* This cannot be done for a structure with volatile
3651                  fields, on which DECL_REGISTER will have been
3652                  reset.  */
3653               if (!DECL_REGISTER (decl))
3654                 error ("cannot put object with volatile field into register");
3655             }
3656
3657           if (TREE_CODE (decl) != FUNCTION_DECL)
3658             {
3659               /* If we're building a variable sized type, and we might be
3660                  reachable other than via the top of the current binding
3661                  level, then create a new BIND_EXPR so that we deallocate
3662                  the object at the right time.  */
3663               /* Note that DECL_SIZE can be null due to errors.  */
3664               if (DECL_SIZE (decl)
3665                   && !TREE_CONSTANT (DECL_SIZE (decl))
3666                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3667                 {
3668                   tree bind;
3669                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3670                   TREE_SIDE_EFFECTS (bind) = 1;
3671                   add_stmt (bind);
3672                   BIND_EXPR_BODY (bind) = push_stmt_list ();
3673                 }
3674               add_stmt (build_stmt (DECL_EXPR, decl));
3675             }
3676         }
3677
3678
3679       if (!DECL_FILE_SCOPE_P (decl))
3680         {
3681           /* Recompute the RTL of a local array now
3682              if it used to be an incomplete type.  */
3683           if (was_incomplete
3684               && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3685             {
3686               /* If we used it already as memory, it must stay in memory.  */
3687               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3688               /* If it's still incomplete now, no init will save it.  */
3689               if (DECL_SIZE (decl) == 0)
3690                 DECL_INITIAL (decl) = 0;
3691             }
3692         }
3693     }
3694
3695   if (TREE_CODE (decl) == TYPE_DECL)
3696     {
3697       if (!DECL_FILE_SCOPE_P (decl)
3698           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3699         add_stmt (build_stmt (DECL_EXPR, decl));
3700
3701       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3702     }
3703
3704   /* At the end of a declaration, throw away any variable type sizes
3705      of types defined inside that declaration.  There is no use
3706      computing them in the following function definition.  */
3707   if (current_scope == file_scope)
3708     get_pending_sizes ();
3709
3710   /* Install a cleanup (aka destructor) if one was given.  */
3711   if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3712     {
3713       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3714       if (attr)
3715         {
3716           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3717           tree cleanup_decl = lookup_name (cleanup_id);
3718           tree cleanup;
3719           VEC(tree,gc) *vec;
3720
3721           /* Build "cleanup(&decl)" for the destructor.  */
3722           cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3723           vec = VEC_alloc (tree, gc, 1);
3724           VEC_quick_push (tree, vec, cleanup);
3725           cleanup = build_function_call_vec (cleanup_decl, vec, NULL);
3726           VEC_free (tree, gc, vec);
3727
3728           /* Don't warn about decl unused; the cleanup uses it.  */
3729           TREE_USED (decl) = 1;
3730           TREE_USED (cleanup_decl) = 1;
3731
3732           /* Initialize EH, if we've been told to do so.  */
3733           c_maybe_initialize_eh ();
3734
3735           push_cleanup (decl, cleanup, false);
3736         }
3737     }
3738 }
3739
3740 /* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3741
3742 tree
3743 grokparm (const struct c_parm *parm)
3744 {
3745   tree attrs = parm->attrs;
3746   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3747                               NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3748
3749   decl_attributes (&decl, attrs, 0);
3750
3751   return decl;
3752 }
3753
3754 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3755    and push that on the current scope.  */
3756
3757 void
3758 push_parm_decl (const struct c_parm *parm)
3759 {
3760   tree attrs = parm->attrs;
3761   tree decl;
3762
3763   decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3764                          &attrs, NULL, NULL, DEPRECATED_NORMAL);
3765   decl_attributes (&decl, attrs, 0);
3766
3767   decl = pushdecl (decl);
3768
3769   finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
3770 }
3771
3772 /* Mark all the parameter declarations to date as forward decls.
3773    Also diagnose use of this extension.  */
3774
3775 void
3776 mark_forward_parm_decls (void)
3777 {
3778   struct c_binding *b;
3779
3780   if (pedantic && !current_scope->warned_forward_parm_decls)
3781     {
3782       pedwarn (input_location, OPT_pedantic,
3783                "ISO C forbids forward parameter declarations");
3784       current_scope->warned_forward_parm_decls = true;
3785     }
3786
3787   for (b = current_scope->bindings; b; b = b->prev)
3788     if (TREE_CODE (b->decl) == PARM_DECL)
3789       TREE_ASM_WRITTEN (b->decl) = 1;
3790 }
3791 \f
3792 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3793    literal, which may be an incomplete array type completed by the
3794    initializer; INIT is a CONSTRUCTOR that initializes the compound
3795    literal.  NON_CONST is true if the initializers contain something
3796    that cannot occur in a constant expression.  */
3797
3798 tree
3799 build_compound_literal (tree type, tree init, bool non_const)
3800 {
3801   /* We do not use start_decl here because we have a type, not a declarator;
3802      and do not use finish_decl because the decl should be stored inside
3803      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3804   tree decl;
3805   tree complit;
3806   tree stmt;
3807
3808   if (type == error_mark_node)
3809     return error_mark_node;
3810
3811   decl = build_decl (VAR_DECL, NULL_TREE, type);
3812   DECL_EXTERNAL (decl) = 0;
3813   TREE_PUBLIC (decl) = 0;
3814   TREE_STATIC (decl) = (current_scope == file_scope);
3815   DECL_CONTEXT (decl) = current_function_decl;
3816   TREE_USED (decl) = 1;
3817   TREE_TYPE (decl) = type;
3818   TREE_READONLY (decl) = TYPE_READONLY (type);
3819   store_init_value (decl, init, NULL_TREE);
3820
3821   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3822     {
3823       int failure = complete_array_type (&TREE_TYPE (decl),
3824                                          DECL_INITIAL (decl), true);
3825       gcc_assert (!failure);
3826
3827       type = TREE_TYPE (decl);
3828       TREE_TYPE (DECL_INITIAL (decl)) = type;
3829     }
3830
3831   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3832     return error_mark_node;
3833
3834   stmt = build_stmt (DECL_EXPR, decl);
3835   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3836   TREE_SIDE_EFFECTS (complit) = 1;
3837
3838   layout_decl (decl, 0);
3839
3840   if (TREE_STATIC (decl))
3841     {
3842       /* This decl needs a name for the assembler output.  */
3843       set_compound_literal_name (decl);
3844       DECL_DEFER_OUTPUT (decl) = 1;
3845       DECL_COMDAT (decl) = 1;
3846       DECL_ARTIFICIAL (decl) = 1;
3847       DECL_IGNORED_P (decl) = 1;
3848       pushdecl (decl);
3849       rest_of_decl_compilation (decl, 1, 0);
3850     }
3851
3852   if (non_const)
3853     {
3854       complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3855       C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3856     }
3857
3858   return complit;
3859 }
3860 \f
3861 /* Determine whether TYPE is a structure with a flexible array member,
3862    or a union containing such a structure (possibly recursively).  */
3863
3864 static bool
3865 flexible_array_type_p (tree type)
3866 {
3867   tree x;
3868   switch (TREE_CODE (type))
3869     {
3870     case RECORD_TYPE:
3871       x = TYPE_FIELDS (type);
3872       if (x == NULL_TREE)
3873         return false;
3874       while (TREE_CHAIN (x) != NULL_TREE)
3875         x = TREE_CHAIN (x);
3876       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3877           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3878           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3879           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3880         return true;
3881       return false;
3882     case UNION_TYPE:
3883       for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3884         {
3885           if (flexible_array_type_p (TREE_TYPE (x)))
3886             return true;
3887         }
3888       return false;
3889     default:
3890     return false;
3891   }
3892 }
3893 \f
3894 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3895    replacing with appropriate values if they are invalid.  */
3896 static void
3897 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
3898 {
3899   tree type_mv;
3900   unsigned int max_width;
3901   unsigned HOST_WIDE_INT w;
3902   const char *name = (orig_name
3903                       ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
3904                       : _("<anonymous>"));
3905
3906   /* Detect and ignore out of range field width and process valid
3907      field widths.  */
3908   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3909       || TREE_CODE (*width) != INTEGER_CST)
3910     {
3911       error ("bit-field %qs width not an integer constant", name);
3912       *width = integer_one_node;
3913     }
3914   else
3915     {
3916       constant_expression_warning (*width);
3917       if (tree_int_cst_sgn (*width) < 0)
3918         {
3919           error ("negative width in bit-field %qs", name);
3920           *width = integer_one_node;
3921         }
3922       else if (integer_zerop (*width) && orig_name)
3923         {
3924           error ("zero width for bit-field %qs", name);
3925           *width = integer_one_node;
3926         }
3927     }
3928
3929   /* Detect invalid bit-field type.  */
3930   if (TREE_CODE (*type) != INTEGER_TYPE
3931       && TREE_CODE (*type) != BOOLEAN_TYPE
3932       && TREE_CODE (*type) != ENUMERAL_TYPE)
3933     {
3934       error ("bit-field %qs has invalid type", name);
3935       *type = unsigned_type_node;
3936     }
3937
3938   type_mv = TYPE_MAIN_VARIANT (*type);
3939   if (!in_system_header
3940       && type_mv != integer_type_node
3941       && type_mv != unsigned_type_node
3942       && type_mv != boolean_type_node)
3943     pedwarn (input_location, OPT_pedantic,
3944              "type of bit-field %qs is a GCC extension", name);
3945
3946   max_width = TYPE_PRECISION (*type);
3947
3948   if (0 < compare_tree_int (*width, max_width))
3949     {
3950       error ("width of %qs exceeds its type", name);
3951       w = max_width;
3952       *width = build_int_cst (NULL_TREE, w);
3953     }
3954   else
3955     w = tree_low_cst (*width, 1);
3956
3957   if (TREE_CODE (*type) == ENUMERAL_TYPE)
3958     {
3959       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3960       if (!lt
3961           || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3962           || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3963         warning (0, "%qs is narrower than values of its type", name);
3964     }
3965 }
3966
3967 \f
3968
3969 /* Print warning about variable length array if necessary.  */
3970
3971 static void
3972 warn_variable_length_array (tree name, tree size)
3973 {
3974   int const_size = TREE_CONSTANT (size);
3975
3976   if (!flag_isoc99 && pedantic && warn_vla != 0)
3977     {
3978       if (const_size)
3979         {
3980           if (name)
3981             pedwarn (input_location, OPT_Wvla,
3982                      "ISO C90 forbids array %qE whose size "
3983                      "can%'t be evaluated",
3984                      name);
3985           else
3986             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
3987                      "can%'t be evaluated");
3988         }
3989       else
3990         {
3991           if (name) 
3992             pedwarn (input_location, OPT_Wvla,
3993                      "ISO C90 forbids variable length array %qE",
3994                      name);
3995           else
3996             pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
3997         }
3998     }
3999   else if (warn_vla > 0)
4000     {
4001       if (const_size)
4002         {
4003           if (name)
4004             warning (OPT_Wvla,
4005                      "the size of array %qE can"
4006                      "%'t be evaluated", name);
4007           else
4008             warning (OPT_Wvla,
4009                      "the size of array can %'t be evaluated");
4010         }
4011       else
4012         {
4013           if (name)
4014             warning (OPT_Wvla,
4015                      "variable length array %qE is used",
4016                      name);
4017           else
4018             warning (OPT_Wvla,
4019                      "variable length array is used");
4020         }
4021     }
4022 }
4023
4024 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4025    serve as the actual size-expression for a type or decl.  This is
4026    like variable_size in stor-layout.c, but we make global_bindings_p
4027    return negative to avoid calls to that function from outside the
4028    front end resulting in errors at file scope, then call this version
4029    instead from front-end code.  */
4030
4031 static tree
4032 c_variable_size (tree size)
4033 {
4034   tree save;
4035
4036   if (TREE_CONSTANT (size))
4037     return size;
4038
4039   size = save_expr (size);
4040
4041   save = skip_simple_arithmetic (size);
4042
4043   if (cfun && cfun->dont_save_pending_sizes_p)
4044     return size;
4045
4046   if (!global_bindings_p ())
4047     put_pending_size (save);
4048
4049   return size;
4050 }
4051
4052 /* Given declspecs and a declarator,
4053    determine the name and type of the object declared
4054    and construct a ..._DECL node for it.
4055    (In one case we can return a ..._TYPE node instead.
4056     For invalid input we sometimes return 0.)
4057
4058    DECLSPECS is a c_declspecs structure for the declaration specifiers.
4059
4060    DECL_CONTEXT says which syntactic context this declaration is in:
4061      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4062      FUNCDEF for a function definition.  Like NORMAL but a few different
4063       error messages in each case.  Return value may be zero meaning
4064       this definition is too screwy to try to parse.
4065      PARM for a parameter declaration (either within a function prototype
4066       or before a function body).  Make a PARM_DECL, or return void_type_node.
4067      TYPENAME if for a typename (in a cast or sizeof).
4068       Don't make a DECL node; just return the ..._TYPE node.
4069      FIELD for a struct or union field; make a FIELD_DECL.
4070    INITIALIZED is true if the decl has an initializer.
4071    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4072    representing the width of the bit-field.
4073    DECL_ATTRS points to the list of attributes that should be added to this
4074      decl.  Any nested attributes that belong on the decl itself will be
4075      added to this list.
4076    If EXPR is not NULL, any expressions that need to be evaluated as
4077      part of evaluating variably modified types will be stored in *EXPR.
4078    If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4079      set to indicate whether operands in *EXPR can be used in constant
4080      expressions.
4081    DEPRECATED_STATE is a deprecated_states value indicating whether
4082    deprecation warnings should be suppressed.
4083
4084    In the TYPENAME case, DECLARATOR is really an absolute declarator.
4085    It may also be so in the PARM case, for a prototype where the
4086    argument type is specified but not the name.
4087
4088    This function is where the complicated C meanings of `static'
4089    and `extern' are interpreted.  */
4090
4091 static tree
4092 grokdeclarator (const struct c_declarator *declarator,
4093                 struct c_declspecs *declspecs,
4094                 enum decl_context decl_context, bool initialized, tree *width,
4095                 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4096                 enum deprecated_states deprecated_state)
4097 {
4098   tree type = declspecs->type;
4099   bool threadp = declspecs->thread_p;
4100   enum c_storage_class storage_class = declspecs->storage_class;
4101   int constp;
4102   int restrictp;
4103   int volatilep;
4104   int type_quals = TYPE_UNQUALIFIED;
4105   tree name = NULL_TREE;
4106   bool funcdef_flag = false;
4107   bool funcdef_syntax = false;
4108   int size_varies = 0;
4109   tree decl_attr = declspecs->decl_attr;
4110   int array_ptr_quals = TYPE_UNQUALIFIED;
4111   tree array_ptr_attrs = NULL_TREE;
4112   int array_parm_static = 0;
4113   bool array_parm_vla_unspec_p = false;
4114   tree returned_attrs = NULL_TREE;
4115   bool bitfield = width != NULL;
4116   tree element_type;
4117   struct c_arg_info *arg_info = 0;
4118   tree expr_dummy;
4119   bool expr_const_operands_dummy;
4120
4121   if (expr == NULL)
4122     expr = &expr_dummy;
4123   if (expr_const_operands == NULL)
4124     expr_const_operands = &expr_const_operands_dummy;
4125
4126   *expr = declspecs->expr;
4127   *expr_const_operands = declspecs->expr_const_operands;
4128
4129   if (decl_context == FUNCDEF)
4130     funcdef_flag = true, decl_context = NORMAL;
4131
4132   /* Look inside a declarator for the name being declared
4133      and get it as an IDENTIFIER_NODE, for an error message.  */
4134   {
4135     const struct c_declarator *decl = declarator;
4136
4137     while (decl)
4138       switch (decl->kind)
4139         {
4140         case cdk_function:
4141         case cdk_array:
4142         case cdk_pointer:
4143           funcdef_syntax = (decl->kind == cdk_function);
4144           decl = decl->declarator;
4145           break;
4146
4147         case cdk_attrs:
4148           decl = decl->declarator;
4149           break;
4150
4151         case cdk_id:
4152           if (decl->u.id)
4153             name = decl->u.id;
4154           decl = 0;
4155           break;
4156
4157         default:
4158           gcc_unreachable ();
4159         }
4160     if (name == 0)
4161       {
4162         gcc_assert (decl_context == PARM
4163                     || decl_context == TYPENAME
4164                     || (decl_context == FIELD
4165                         && declarator->kind == cdk_id));
4166         gcc_assert (!initialized);
4167       }
4168   }
4169
4170   /* A function definition's declarator must have the form of
4171      a function declarator.  */
4172
4173   if (funcdef_flag && !funcdef_syntax)
4174     return 0;
4175
4176   /* If this looks like a function definition, make it one,
4177      even if it occurs where parms are expected.
4178      Then store_parm_decls will reject it and not use it as a parm.  */
4179   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4180     decl_context = PARM;
4181
4182   if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4183     warn_deprecated_use (declspecs->type);
4184
4185   if ((decl_context == NORMAL || decl_context == FIELD)
4186       && current_scope == file_scope
4187       && variably_modified_type_p (type, NULL_TREE))
4188     {
4189       if (name)
4190         error ("variably modified %qE at file scope", name);
4191       else
4192         error ("variably modified field at file scope");
4193       type = integer_type_node;
4194     }
4195
4196   size_varies = C_TYPE_VARIABLE_SIZE (type);
4197
4198   /* Diagnose defaulting to "int".  */
4199
4200   if (declspecs->default_int_p && !in_system_header)
4201     {
4202       /* Issue a warning if this is an ISO C 99 program or if
4203          -Wreturn-type and this is a function, or if -Wimplicit;
4204          prefer the former warning since it is more explicit.  */
4205       if ((warn_implicit_int || warn_return_type || flag_isoc99)
4206           && funcdef_flag)
4207         warn_about_return_type = 1;
4208       else
4209         {
4210           if (name)
4211             pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int, 
4212                          "type defaults to %<int%> in declaration of %qE",
4213                          name);
4214           else
4215             pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int, 
4216                          "type defaults to %<int%> in type name");
4217         }
4218     }
4219
4220   /* Adjust the type if a bit-field is being declared,
4221      -funsigned-bitfields applied and the type is not explicitly
4222      "signed".  */
4223   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4224       && TREE_CODE (type) == INTEGER_TYPE)
4225     type = unsigned_type_for (type);
4226
4227   /* Figure out the type qualifiers for the declaration.  There are
4228      two ways a declaration can become qualified.  One is something
4229      like `const int i' where the `const' is explicit.  Another is
4230      something like `typedef const int CI; CI i' where the type of the
4231      declaration contains the `const'.  A third possibility is that
4232      there is a type qualifier on the element type of a typedefed
4233      array type, in which case we should extract that qualifier so
4234      that c_apply_type_quals_to_decl receives the full list of
4235      qualifiers to work with (C90 is not entirely clear about whether
4236      duplicate qualifiers should be diagnosed in this case, but it
4237      seems most appropriate to do so).  */
4238   element_type = strip_array_types (type);
4239   constp = declspecs->const_p + TYPE_READONLY (element_type);
4240   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4241   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4242   if (pedantic && !flag_isoc99)
4243     {
4244       if (constp > 1)
4245         pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4246       if (restrictp > 1)
4247         pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4248       if (volatilep > 1)
4249         pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4250     }
4251   if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4252     type = TYPE_MAIN_VARIANT (type);
4253   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4254                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4255                 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4256
4257   /* Warn about storage classes that are invalid for certain
4258      kinds of declarations (parameters, typenames, etc.).  */
4259
4260   if (funcdef_flag
4261       && (threadp
4262           || storage_class == csc_auto
4263           || storage_class == csc_register
4264           || storage_class == csc_typedef))
4265     {
4266       if (storage_class == csc_auto)
4267         pedwarn (input_location, 
4268                  (current_scope == file_scope) ? 0 : OPT_pedantic, 
4269                  "function definition declared %<auto%>");
4270       if (storage_class == csc_register)
4271         error ("function definition declared %<register%>");
4272       if (storage_class == csc_typedef)
4273         error ("function definition declared %<typedef%>");
4274       if (threadp)
4275         error ("function definition declared %<__thread%>");
4276       threadp = false;
4277       if (storage_class == csc_auto
4278           || storage_class == csc_register
4279           || storage_class == csc_typedef)
4280         storage_class = csc_none;
4281     }
4282   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4283     {
4284       if (decl_context == PARM && storage_class == csc_register)
4285         ;
4286       else
4287         {
4288           switch (decl_context)
4289             {
4290             case FIELD:
4291               if (name)
4292                 error ("storage class specified for structure field %qE",
4293                        name);
4294               else
4295                 error ("storage class specified for structure field");
4296               break;
4297             case PARM:
4298               if (name)
4299                 error ("storage class specified for parameter %qE", name);
4300               else
4301                 error ("storage class specified for unnamed parameter");
4302               break;
4303             default:
4304               error ("storage class specified for typename");
4305               break;
4306             }
4307           storage_class = csc_none;
4308           threadp = false;
4309         }
4310     }
4311   else if (storage_class == csc_extern
4312            && initialized
4313            && !funcdef_flag)
4314     {
4315       /* 'extern' with initialization is invalid if not at file scope.  */
4316        if (current_scope == file_scope)
4317          {
4318            /* It is fine to have 'extern const' when compiling at C
4319               and C++ intersection.  */
4320            if (!(warn_cxx_compat && constp))
4321              warning (0, "%qE initialized and declared %<extern%>", name);
4322          }
4323       else
4324         error ("%qE has both %<extern%> and initializer", name);
4325     }
4326   else if (current_scope == file_scope)
4327     {
4328       if (storage_class == csc_auto)
4329         error ("file-scope declaration of %qE specifies %<auto%>", name);
4330       if (pedantic && storage_class == csc_register)
4331         pedwarn (input_location, OPT_pedantic,
4332                  "file-scope declaration of %qE specifies %<register%>", name);
4333     }
4334   else
4335     {
4336       if (storage_class == csc_extern && funcdef_flag)
4337         error ("nested function %qE declared %<extern%>", name);
4338       else if (threadp && storage_class == csc_none)
4339         {
4340           error ("function-scope %qE implicitly auto and declared "
4341                  "%<__thread%>",
4342                  name);
4343           threadp = false;
4344         }
4345     }
4346
4347   /* Now figure out the structure of the declarator proper.
4348      Descend through it, creating more complex types, until we reach
4349      the declared identifier (or NULL_TREE, in an absolute declarator).
4350      At each stage we maintain an unqualified version of the type
4351      together with any qualifiers that should be applied to it with
4352      c_build_qualified_type; this way, array types including
4353      multidimensional array types are first built up in unqualified
4354      form and then the qualified form is created with
4355      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
4356
4357   while (declarator && declarator->kind != cdk_id)
4358     {
4359       if (type == error_mark_node)
4360         {
4361           declarator = declarator->declarator;
4362           continue;
4363         }
4364
4365       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4366          a cdk_pointer (for *...),
4367          a cdk_function (for ...(...)),
4368          a cdk_attrs (for nested attributes),
4369          or a cdk_id (for the name being declared
4370          or the place in an absolute declarator
4371          where the name was omitted).
4372          For the last case, we have just exited the loop.
4373
4374          At this point, TYPE is the type of elements of an array,
4375          or for a function to return, or for a pointer to point to.
4376          After this sequence of ifs, TYPE is the type of the
4377          array or function or pointer, and DECLARATOR has had its
4378          outermost layer removed.  */
4379
4380       if (array_ptr_quals != TYPE_UNQUALIFIED
4381           || array_ptr_attrs != NULL_TREE
4382           || array_parm_static)
4383         {
4384           /* Only the innermost declarator (making a parameter be of
4385              array type which is converted to pointer type)
4386              may have static or type qualifiers.  */
4387           error ("static or type qualifiers in non-parameter array declarator");
4388           array_ptr_quals = TYPE_UNQUALIFIED;
4389           array_ptr_attrs = NULL_TREE;
4390           array_parm_static = 0;
4391         }
4392
4393       switch (declarator->kind)
4394         {
4395         case cdk_attrs:
4396           {
4397             /* A declarator with embedded attributes.  */
4398             tree attrs = declarator->u.attrs;
4399             const struct c_declarator *inner_decl;
4400             int attr_flags = 0;
4401             declarator = declarator->declarator;
4402             inner_decl = declarator;
4403             while (inner_decl->kind == cdk_attrs)
4404               inner_decl = inner_decl->declarator;
4405             if (inner_decl->kind == cdk_id)
4406               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4407             else if (inner_decl->kind == cdk_function)
4408               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4409             else if (inner_decl->kind == cdk_array)
4410               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4411             returned_attrs = decl_attributes (&type,
4412                                               chainon (returned_attrs, attrs),
4413                                               attr_flags);
4414             break;
4415           }
4416         case cdk_array:
4417           {
4418             tree itype = NULL_TREE;
4419             tree size = declarator->u.array.dimen;
4420             /* The index is a signed object `sizetype' bits wide.  */
4421             tree index_type = c_common_signed_type (sizetype);
4422
4423             array_ptr_quals = declarator->u.array.quals;
4424             array_ptr_attrs = declarator->u.array.attrs;
4425             array_parm_static = declarator->u.array.static_p;
4426             array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4427
4428             declarator = declarator->declarator;
4429
4430             /* Check for some types that there cannot be arrays of.  */
4431
4432             if (VOID_TYPE_P (type))
4433               {
4434                 if (name)
4435                   error ("declaration of %qE as array of voids", name);
4436                 else
4437                   error ("declaration of type name as array of voids");
4438                 type = error_mark_node;
4439               }
4440
4441             if (TREE_CODE (type) == FUNCTION_TYPE)
4442               {
4443                 if (name)
4444                   error ("declaration of %qE as array of functions", name);
4445                 else
4446                   error ("declaration of type name as array of functions");
4447                 type = error_mark_node;
4448               }
4449
4450             if (pedantic && !in_system_header && flexible_array_type_p (type))
4451               pedwarn (input_location, OPT_pedantic,
4452                        "invalid use of structure with flexible array member");
4453
4454             if (size == error_mark_node)
4455               type = error_mark_node;
4456
4457             if (type == error_mark_node)
4458               continue;
4459
4460             /* If size was specified, set ITYPE to a range-type for
4461                that size.  Otherwise, ITYPE remains null.  finish_decl
4462                may figure it out from an initial value.  */
4463
4464             if (size)
4465               {
4466                 bool size_maybe_const = true;
4467                 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4468                                        && !TREE_OVERFLOW (size));
4469                 bool this_size_varies = false;
4470
4471                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4472                    lvalue.  */
4473                 STRIP_TYPE_NOPS (size);
4474
4475                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4476                   {
4477                     if (name)
4478                       error ("size of array %qE has non-integer type", name);
4479                     else
4480                       error ("size of unnamed array has non-integer type");
4481                     size = integer_one_node;
4482                   }
4483
4484                 size = c_fully_fold (size, false, &size_maybe_const);
4485
4486                 if (pedantic && size_maybe_const && integer_zerop (size))
4487                   {
4488                     if (name)
4489                       pedwarn (input_location, OPT_pedantic,
4490                                "ISO C forbids zero-size array %qE", name);
4491                     else
4492                       pedwarn (input_location, OPT_pedantic,
4493                                "ISO C forbids zero-size array");
4494                   }
4495
4496                 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4497                   {
4498                     constant_expression_warning (size);
4499                     if (tree_int_cst_sgn (size) < 0)
4500                       {
4501                         if (name)
4502                           error ("size of array %qE is negative", name);
4503                         else
4504                           error ("size of unnamed array is negative");
4505                         size = integer_one_node;
4506                       }
4507                     /* Handle a size folded to an integer constant but
4508                        not an integer constant expression.  */
4509                     if (!size_int_const)
4510                       {
4511                         /* If this is a file scope declaration of an
4512                            ordinary identifier, this is invalid code;
4513                            diagnosing it here and not subsequently
4514                            treating the type as variable-length avoids
4515                            more confusing diagnostics later.  */
4516                         if ((decl_context == NORMAL || decl_context == FIELD)
4517                             && current_scope == file_scope)
4518                           pedwarn (input_location, 0,
4519                                    "variably modified %qE at file scope",
4520                                    name);
4521                         else
4522                           this_size_varies = size_varies = 1;
4523                         warn_variable_length_array (name, size);
4524                       }
4525                   }
4526                 else if ((decl_context == NORMAL || decl_context == FIELD)
4527                          && current_scope == file_scope)
4528                   {
4529                     error ("variably modified %qE at file scope", name);
4530                     size = integer_one_node;
4531                   }
4532                 else
4533                   {
4534                     /* Make sure the array size remains visibly
4535                        nonconstant even if it is (eg) a const variable
4536                        with known value.  */
4537                     this_size_varies = size_varies = 1;
4538                     warn_variable_length_array (name, size);
4539                   }
4540
4541                 if (integer_zerop (size) && !this_size_varies)
4542                   {
4543                     /* A zero-length array cannot be represented with
4544                        an unsigned index type, which is what we'll
4545                        get with build_index_type.  Create an
4546                        open-ended range instead.  */
4547                     itype = build_range_type (sizetype, size, NULL_TREE);
4548                   }
4549                 else
4550                   {
4551                     /* Arrange for the SAVE_EXPR on the inside of the
4552                        MINUS_EXPR, which allows the -1 to get folded
4553                        with the +1 that happens when building TYPE_SIZE.  */
4554                     if (size_varies)
4555                       size = c_variable_size (size);
4556                     if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4557                       size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4558                                      integer_zero_node, size);
4559
4560                     /* Compute the maximum valid index, that is, size
4561                        - 1.  Do the calculation in index_type, so that
4562                        if it is a variable the computations will be
4563                        done in the proper mode.  */
4564                     itype = fold_build2 (MINUS_EXPR, index_type,
4565                                          convert (index_type, size),
4566                                          convert (index_type,
4567                                                   size_one_node));
4568
4569                     /* If that overflowed, the array is too big.  ???
4570                        While a size of INT_MAX+1 technically shouldn't
4571                        cause an overflow (because we subtract 1), the
4572                        overflow is recorded during the conversion to
4573                        index_type, before the subtraction.  Handling
4574                        this case seems like an unnecessary
4575                        complication.  */
4576                     if (TREE_CODE (itype) == INTEGER_CST
4577                         && TREE_OVERFLOW (itype))
4578                       {
4579                         if (name)
4580                           error ("size of array %qE is too large", name);
4581                         else
4582                           error ("size of unnamed array is too large");
4583                         type = error_mark_node;
4584                         continue;
4585                       }
4586
4587                     itype = build_index_type (itype);
4588                   }
4589                 if (this_size_varies)
4590                   {
4591                     if (*expr)
4592                       *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4593                                       *expr, size);
4594                     else
4595                       *expr = size;
4596                     *expr_const_operands &= size_maybe_const;
4597                   }
4598               }
4599             else if (decl_context == FIELD)
4600               {
4601                 bool flexible_array_member = false;
4602                 if (array_parm_vla_unspec_p)
4603                   /* Field names can in fact have function prototype
4604                      scope so [*] is disallowed here through making
4605                      the field variably modified, not through being
4606                      something other than a declaration with function
4607                      prototype scope.  */
4608                   size_varies = 1;
4609                 else
4610                   {
4611                     const struct c_declarator *t = declarator;
4612                     while (t->kind == cdk_attrs)
4613                       t = t->declarator;
4614                     flexible_array_member = (t->kind == cdk_id);
4615                   }
4616                 if (flexible_array_member
4617                     && pedantic && !flag_isoc99 && !in_system_header)
4618                   pedwarn (input_location, OPT_pedantic,
4619                            "ISO C90 does not support flexible array members");
4620
4621                 /* ISO C99 Flexible array members are effectively
4622                    identical to GCC's zero-length array extension.  */
4623                 if (flexible_array_member || array_parm_vla_unspec_p)
4624                   itype = build_range_type (sizetype, size_zero_node,
4625                                             NULL_TREE);
4626               }
4627             else if (decl_context == PARM)
4628               {
4629                 if (array_parm_vla_unspec_p)
4630                   {
4631                     itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4632                     size_varies = 1;
4633                   }
4634               }
4635             else if (decl_context == TYPENAME)
4636               {
4637                 if (array_parm_vla_unspec_p)
4638                   {
4639                     /* C99 6.7.5.2p4 */
4640                     warning (0, "%<[*]%> not in a declaration");
4641                     /* We use this to avoid messing up with incomplete
4642                        array types of the same type, that would
4643                        otherwise be modified below.  */
4644                     itype = build_range_type (sizetype, size_zero_node,
4645                                               NULL_TREE);
4646                     size_varies = 1;
4647                   }
4648               }
4649
4650              /* Complain about arrays of incomplete types.  */
4651             if (!COMPLETE_TYPE_P (type))
4652               {
4653                 error ("array type has incomplete element type");
4654                 type = error_mark_node;
4655               }
4656             else
4657             /* When itype is NULL, a shared incomplete array type is
4658                returned for all array of a given type.  Elsewhere we
4659                make sure we don't complete that type before copying
4660                it, but here we want to make sure we don't ever
4661                modify the shared type, so we gcc_assert (itype)
4662                below.  */
4663               type = build_array_type (type, itype);
4664
4665             if (type != error_mark_node)
4666               {
4667                 if (size_varies)
4668                   {
4669                     /* It is ok to modify type here even if itype is
4670                        NULL: if size_varies, we're in a
4671                        multi-dimensional array and the inner type has
4672                        variable size, so the enclosing shared array type
4673                        must too.  */
4674                     if (size && TREE_CODE (size) == INTEGER_CST)
4675                       type
4676                         = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4677                     C_TYPE_VARIABLE_SIZE (type) = 1;
4678                   }
4679
4680                 /* The GCC extension for zero-length arrays differs from
4681                    ISO flexible array members in that sizeof yields
4682                    zero.  */
4683                 if (size && integer_zerop (size))
4684                   {
4685                     gcc_assert (itype);
4686                     TYPE_SIZE (type) = bitsize_zero_node;
4687                     TYPE_SIZE_UNIT (type) = size_zero_node;
4688                   }
4689                 if (array_parm_vla_unspec_p)
4690                   {
4691                     gcc_assert (itype);
4692                     /* The type is complete.  C99 6.7.5.2p4  */
4693                     TYPE_SIZE (type) = bitsize_zero_node;
4694                     TYPE_SIZE_UNIT (type) = size_zero_node;
4695                   }
4696               }
4697
4698             if (decl_context != PARM
4699                 && (array_ptr_quals != TYPE_UNQUALIFIED
4700                     || array_ptr_attrs != NULL_TREE
4701                     || array_parm_static))
4702               {
4703                 error ("static or type qualifiers in non-parameter array declarator");
4704                 array_ptr_quals = TYPE_UNQUALIFIED;
4705                 array_ptr_attrs = NULL_TREE;
4706                 array_parm_static = 0;
4707               }
4708             break;
4709           }
4710         case cdk_function:
4711           {
4712             /* Say it's a definition only for the declarator closest
4713                to the identifier, apart possibly from some
4714                attributes.  */
4715             bool really_funcdef = false;
4716             tree arg_types;
4717             if (funcdef_flag)
4718               {
4719                 const struct c_declarator *t = declarator->declarator;
4720                 while (t->kind == cdk_attrs)
4721                   t = t->declarator;
4722                 really_funcdef = (t->kind == cdk_id);
4723               }
4724
4725             /* Declaring a function type.  Make sure we have a valid
4726                type for the function to return.  */
4727             if (type == error_mark_node)
4728               continue;
4729
4730             size_varies = 0;
4731
4732             /* Warn about some types functions can't return.  */
4733             if (TREE_CODE (type) == FUNCTION_TYPE)
4734               {
4735                 if (name)
4736                   error ("%qE declared as function returning a function",
4737                          name);
4738                 else
4739                   error ("type name declared as function "
4740                          "returning a function");
4741                 type = integer_type_node;
4742               }
4743             if (TREE_CODE (type) == ARRAY_TYPE)
4744               {
4745                 if (name)
4746                   error ("%qE declared as function returning an array", name);
4747                 else
4748                   error ("type name declared as function returning an array");
4749                 type = integer_type_node;
4750               }
4751
4752             /* Construct the function type and go to the next
4753                inner layer of declarator.  */
4754             arg_info = declarator->u.arg_info;
4755             arg_types = grokparms (arg_info, really_funcdef);
4756             if (really_funcdef)
4757               put_pending_sizes (arg_info->pending_sizes);
4758
4759             /* Type qualifiers before the return type of the function
4760                qualify the return type, not the function type.  */
4761             if (type_quals)
4762               {
4763                 /* Type qualifiers on a function return type are
4764                    normally permitted by the standard but have no
4765                    effect, so give a warning at -Wreturn-type.
4766                    Qualifiers on a void return type are banned on
4767                    function definitions in ISO C; GCC used to used
4768                    them for noreturn functions.  */
4769                 if (VOID_TYPE_P (type) && really_funcdef)
4770                   pedwarn (input_location, 0,
4771                            "function definition has qualified void return type");
4772                 else
4773                   warning (OPT_Wignored_qualifiers,
4774                            "type qualifiers ignored on function return type");
4775
4776                 type = c_build_qualified_type (type, type_quals);
4777               }
4778             type_quals = TYPE_UNQUALIFIED;
4779
4780             type = build_function_type (type, arg_types);
4781             declarator = declarator->declarator;
4782
4783             /* Set the TYPE_CONTEXTs for each tagged type which is local to
4784                the formal parameter list of this FUNCTION_TYPE to point to
4785                the FUNCTION_TYPE node itself.  */
4786             {
4787               tree link;
4788
4789               for (link = arg_info->tags;
4790                    link;
4791                    link = TREE_CHAIN (link))
4792                 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4793             }
4794             break;
4795           }
4796         case cdk_pointer:
4797           {
4798             /* Merge any constancy or volatility into the target type
4799                for the pointer.  */
4800
4801             if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4802                 && type_quals)
4803               pedwarn (input_location, OPT_pedantic,
4804                        "ISO C forbids qualified function types");
4805             if (type_quals)
4806               type = c_build_qualified_type (type, type_quals);
4807             size_varies = 0;
4808
4809             /* When the pointed-to type involves components of variable size,
4810                care must be taken to ensure that the size evaluation code is
4811                emitted early enough to dominate all the possible later uses
4812                and late enough for the variables on which it depends to have
4813                been assigned.
4814
4815                This is expected to happen automatically when the pointed-to
4816                type has a name/declaration of it's own, but special attention
4817                is required if the type is anonymous.
4818
4819                We handle the NORMAL and FIELD contexts here by attaching an
4820                artificial TYPE_DECL to such pointed-to type.  This forces the
4821                sizes evaluation at a safe point and ensures it is not deferred
4822                until e.g. within a deeper conditional context.
4823
4824                We expect nothing to be needed here for PARM or TYPENAME.
4825                Pushing a TYPE_DECL at this point for TYPENAME would actually
4826                be incorrect, as we might be in the middle of an expression
4827                with side effects on the pointed-to type size "arguments" prior
4828                to the pointer declaration point and the fake TYPE_DECL in the
4829                enclosing context would force the size evaluation prior to the
4830                side effects.  */
4831
4832             if (!TYPE_NAME (type)
4833                 && (decl_context == NORMAL || decl_context == FIELD)
4834                 && variably_modified_type_p (type, NULL_TREE))
4835               {
4836                 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4837                 DECL_ARTIFICIAL (decl) = 1;
4838                 pushdecl (decl);
4839                 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
4840                 TYPE_NAME (type) = decl;
4841               }
4842
4843             type = build_pointer_type (type);
4844
4845             /* Process type qualifiers (such as const or volatile)
4846                that were given inside the `*'.  */
4847             type_quals = declarator->u.pointer_quals;
4848
4849             declarator = declarator->declarator;
4850             break;
4851           }
4852         default:
4853           gcc_unreachable ();
4854         }
4855     }
4856   *decl_attrs = chainon (returned_attrs, *decl_attrs);
4857
4858   /* Now TYPE has the actual type, apart from any qualifiers in
4859      TYPE_QUALS.  */
4860
4861   /* Check the type and width of a bit-field.  */
4862   if (bitfield)
4863     check_bitfield_type_and_width (&type, width, name);
4864
4865   /* Did array size calculations overflow?  */
4866
4867   if (TREE_CODE (type) == ARRAY_TYPE
4868       && COMPLETE_TYPE_P (type)
4869       && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4870       && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4871     {
4872       if (name)
4873         error ("size of array %qE is too large", name);
4874       else
4875         error ("size of unnamed array is too large");
4876       /* If we proceed with the array type as it is, we'll eventually
4877          crash in tree_low_cst().  */
4878       type = error_mark_node;
4879     }
4880
4881   /* If this is declaring a typedef name, return a TYPE_DECL.  */
4882
4883   if (storage_class == csc_typedef)
4884     {
4885       tree decl;
4886       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4887           && type_quals)
4888         pedwarn (input_location, OPT_pedantic,
4889                  "ISO C forbids qualified function types");
4890       if (type_quals)
4891         type = c_build_qualified_type (type, type_quals);
4892       decl = build_decl (TYPE_DECL, declarator->u.id, type);
4893       DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4894       if (declspecs->explicit_signed_p)
4895         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4896       if (declspecs->inline_p)
4897         pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4898       return decl;
4899     }
4900
4901   /* If this is a type name (such as, in a cast or sizeof),
4902      compute the type and return it now.  */
4903
4904   if (decl_context == TYPENAME)
4905     {
4906       /* Note that the grammar rejects storage classes in typenames
4907          and fields.  */
4908       gcc_assert (storage_class == csc_none && !threadp
4909                   && !declspecs->inline_p);
4910       if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4911           && type_quals)
4912         pedwarn (input_location, OPT_pedantic,
4913                  "ISO C forbids const or volatile function types");
4914       if (type_quals)
4915         type = c_build_qualified_type (type, type_quals);
4916       return type;
4917     }
4918
4919   if (pedantic && decl_context == FIELD
4920       && variably_modified_type_p (type, NULL_TREE))
4921     {
4922       /* C99 6.7.2.1p8 */
4923       pedwarn (input_location, OPT_pedantic, 
4924                "a member of a structure or union cannot have a variably modified type");
4925     }
4926
4927   /* Aside from typedefs and type names (handle above),
4928      `void' at top level (not within pointer)
4929      is allowed only in public variables.
4930      We don't complain about parms either, but that is because
4931      a better error message can be made later.  */
4932
4933   if (VOID_TYPE_P (type) && decl_context != PARM
4934       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4935             && (storage_class == csc_extern
4936                 || (current_scope == file_scope
4937                     && !(storage_class == csc_static
4938                          || storage_class == csc_register)))))
4939     {
4940       error ("variable or field %qE declared void", name);
4941       type = integer_type_node;
4942     }
4943
4944   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4945      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
4946
4947   {
4948     tree decl;
4949
4950     if (decl_context == PARM)
4951       {
4952         tree promoted_type;
4953
4954         /* A parameter declared as an array of T is really a pointer to T.
4955            One declared as a function is really a pointer to a function.  */
4956
4957         if (TREE_CODE (type) == ARRAY_TYPE)
4958           {
4959             /* Transfer const-ness of array into that of type pointed to.  */
4960             type = TREE_TYPE (type);
4961             if (type_quals)
4962               type = c_build_qualified_type (type, type_quals);
4963             type = build_pointer_type (type);
4964             type_quals = array_ptr_quals;
4965             if (type_quals)
4966               type = c_build_qualified_type (type, type_quals);
4967
4968             /* We don't yet implement attributes in this context.  */
4969             if (array_ptr_attrs != NULL_TREE)
4970               warning (OPT_Wattributes,
4971                        "attributes in parameter array declarator ignored");
4972
4973             size_varies = 0;
4974           }
4975         else if (TREE_CODE (type) == FUNCTION_TYPE)
4976           {
4977             if (type_quals)
4978               pedwarn (input_location, OPT_pedantic,
4979                        "ISO C forbids qualified function types");
4980             if (type_quals)
4981               type = c_build_qualified_type (type, type_quals);
4982             type = build_pointer_type (type);
4983             type_quals = TYPE_UNQUALIFIED;
4984           }
4985         else if (type_quals)
4986           type = c_build_qualified_type (type, type_quals);
4987
4988         decl = build_decl (PARM_DECL, declarator->u.id, type);
4989         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4990         if (size_varies)
4991           C_DECL_VARIABLE_SIZE (decl) = 1;
4992
4993         /* Compute the type actually passed in the parmlist,
4994            for the case where there is no prototype.
4995            (For example, shorts and chars are passed as ints.)
4996            When there is a prototype, this is overridden later.  */
4997
4998         if (type == error_mark_node)
4999           promoted_type = type;
5000         else
5001           promoted_type = c_type_promotes_to (type);
5002
5003         DECL_ARG_TYPE (decl) = promoted_type;
5004         if (declspecs->inline_p)
5005           pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
5006       }
5007     else if (decl_context == FIELD)
5008       {
5009         /* Note that the grammar rejects storage classes in typenames
5010            and fields.  */
5011         gcc_assert (storage_class == csc_none && !threadp
5012                     && !declspecs->inline_p);
5013
5014         /* Structure field.  It may not be a function.  */
5015
5016         if (TREE_CODE (type) == FUNCTION_TYPE)
5017           {
5018             error ("field %qE declared as a function", name);
5019             type = build_pointer_type (type);
5020           }
5021         else if (TREE_CODE (type) != ERROR_MARK
5022                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5023           {
5024             if (name)
5025               error ("field %qE has incomplete type", name);
5026             else
5027               error ("unnamed field has incomplete type");
5028             type = error_mark_node;
5029           }
5030         type = c_build_qualified_type (type, type_quals);
5031         decl = build_decl (FIELD_DECL, declarator->u.id, type);
5032         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5033         DECL_NONADDRESSABLE_P (decl) = bitfield;
5034         if (bitfield && !declarator->u.id)
5035           TREE_NO_WARNING (decl) = 1;
5036
5037         if (size_varies)
5038           C_DECL_VARIABLE_SIZE (decl) = 1;
5039       }
5040     else if (TREE_CODE (type) == FUNCTION_TYPE)
5041       {
5042         if (storage_class == csc_register || threadp)
5043           {
5044             error ("invalid storage class for function %qE", name);
5045            }
5046         else if (current_scope != file_scope)
5047           {
5048             /* Function declaration not at file scope.  Storage
5049                classes other than `extern' are not allowed, C99
5050                6.7.1p5, and `extern' makes no difference.  However,
5051                GCC allows 'auto', perhaps with 'inline', to support
5052                nested functions.  */
5053             if (storage_class == csc_auto)
5054                 pedwarn (input_location, OPT_pedantic,
5055                          "invalid storage class for function %qE", name);
5056             else if (storage_class == csc_static)
5057               {
5058                 error ("invalid storage class for function %qE", name);
5059                 if (funcdef_flag)
5060                   storage_class = declspecs->storage_class = csc_none;
5061                 else
5062                   return 0;
5063               }
5064           }
5065
5066         decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5067         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5068         decl = build_decl_attribute_variant (decl, decl_attr);
5069
5070         if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5071           pedwarn (input_location, OPT_pedantic,
5072                    "ISO C forbids qualified function types");
5073
5074         /* GNU C interprets a volatile-qualified function type to indicate
5075            that the function does not return.  */
5076         if ((type_quals & TYPE_QUAL_VOLATILE)
5077             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5078           warning (0, "%<noreturn%> function returns non-void value");
5079
5080         /* Every function declaration is an external reference
5081            (DECL_EXTERNAL) except for those which are not at file
5082            scope and are explicitly declared "auto".  This is
5083            forbidden by standard C (C99 6.7.1p5) and is interpreted by
5084            GCC to signify a forward declaration of a nested function.  */
5085         if (storage_class == csc_auto && current_scope != file_scope)
5086           DECL_EXTERNAL (decl) = 0;
5087         /* In C99, a function which is declared 'inline' with 'extern'
5088            is not an external reference (which is confusing).  It
5089            means that the later definition of the function must be output
5090            in this file, C99 6.7.4p6.  In GNU C89, a function declared
5091            'extern inline' is an external reference.  */
5092         else if (declspecs->inline_p && storage_class != csc_static)
5093           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5094                                   == flag_gnu89_inline);
5095         else
5096           DECL_EXTERNAL (decl) = !initialized;
5097
5098         /* Record absence of global scope for `static' or `auto'.  */
5099         TREE_PUBLIC (decl)
5100           = !(storage_class == csc_static || storage_class == csc_auto);
5101
5102         /* For a function definition, record the argument information
5103            block where store_parm_decls will look for it.  */
5104         if (funcdef_flag)
5105           current_function_arg_info = arg_info;
5106
5107         if (declspecs->default_int_p)
5108           C_FUNCTION_IMPLICIT_INT (decl) = 1;
5109
5110         /* Record presence of `inline', if it is reasonable.  */
5111         if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5112           {
5113             if (declspecs->inline_p)
5114               pedwarn (input_location, 0, "cannot inline function %<main%>");
5115           }
5116         else if (declspecs->inline_p)
5117           /* Record that the function is declared `inline'.  */
5118           DECL_DECLARED_INLINE_P (decl) = 1;
5119       }
5120     else
5121       {
5122         /* It's a variable.  */
5123         /* An uninitialized decl with `extern' is a reference.  */
5124         int extern_ref = !initialized && storage_class == csc_extern;
5125
5126         type = c_build_qualified_type (type, type_quals);
5127
5128         /* C99 6.2.2p7: It is invalid (compile-time undefined
5129            behavior) to create an 'extern' declaration for a
5130            variable if there is a global declaration that is
5131            'static' and the global declaration is not visible.
5132            (If the static declaration _is_ currently visible,
5133            the 'extern' declaration is taken to refer to that decl.) */
5134         if (extern_ref && current_scope != file_scope)
5135           {
5136             tree global_decl  = identifier_global_value (declarator->u.id);
5137             tree visible_decl = lookup_name (declarator->u.id);
5138
5139             if (global_decl
5140                 && global_decl != visible_decl
5141                 && TREE_CODE (global_decl) == VAR_DECL
5142                 && !TREE_PUBLIC (global_decl))
5143               error ("variable previously declared %<static%> redeclared "
5144                      "%<extern%>");
5145           }
5146
5147         decl = build_decl (VAR_DECL, declarator->u.id, type);
5148         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5149         if (size_varies)
5150           C_DECL_VARIABLE_SIZE (decl) = 1;
5151
5152         if (declspecs->inline_p)
5153           pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
5154
5155         /* At file scope, an initialized extern declaration may follow
5156            a static declaration.  In that case, DECL_EXTERNAL will be
5157            reset later in start_decl.  */
5158         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5159
5160         /* At file scope, the presence of a `static' or `register' storage
5161            class specifier, or the absence of all storage class specifiers
5162            makes this declaration a definition (perhaps tentative).  Also,
5163            the absence of `static' makes it public.  */
5164         if (current_scope == file_scope)
5165           {
5166             TREE_PUBLIC (decl) = storage_class != csc_static;
5167             TREE_STATIC (decl) = !extern_ref;
5168           }
5169         /* Not at file scope, only `static' makes a static definition.  */
5170         else
5171           {
5172             TREE_STATIC (decl) = (storage_class == csc_static);
5173             TREE_PUBLIC (decl) = extern_ref;
5174           }
5175
5176         if (threadp)
5177           DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5178       }
5179
5180     if ((storage_class == csc_extern
5181          || (storage_class == csc_none
5182              && TREE_CODE (type) == FUNCTION_TYPE
5183              && !funcdef_flag))
5184         && variably_modified_type_p (type, NULL_TREE))
5185       {
5186         /* C99 6.7.5.2p2 */
5187         if (TREE_CODE (type) == FUNCTION_TYPE)
5188           error ("non-nested function with variably modified type");
5189         else
5190           error ("object with variably modified type must have no linkage");
5191       }
5192
5193     /* Record `register' declaration for warnings on &
5194        and in case doing stupid register allocation.  */
5195
5196     if (storage_class == csc_register)
5197       {
5198         C_DECL_REGISTER (decl) = 1;
5199         DECL_REGISTER (decl) = 1;
5200       }
5201
5202     /* Record constancy and volatility.  */
5203     c_apply_type_quals_to_decl (type_quals, decl);
5204
5205     /* If a type has volatile components, it should be stored in memory.
5206        Otherwise, the fact that those components are volatile
5207        will be ignored, and would even crash the compiler.
5208        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
5209     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5210         && (TREE_CODE (decl) == VAR_DECL ||  TREE_CODE (decl) == PARM_DECL
5211           || TREE_CODE (decl) == RESULT_DECL))
5212       {
5213         /* It is not an error for a structure with volatile fields to
5214            be declared register, but reset DECL_REGISTER since it
5215            cannot actually go in a register.  */
5216         int was_reg = C_DECL_REGISTER (decl);
5217         C_DECL_REGISTER (decl) = 0;
5218         DECL_REGISTER (decl) = 0;
5219         c_mark_addressable (decl);
5220         C_DECL_REGISTER (decl) = was_reg;
5221       }
5222
5223   /* This is the earliest point at which we might know the assembler
5224      name of a variable.  Thus, if it's known before this, die horribly.  */
5225     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5226
5227     return decl;
5228   }
5229 }
5230 \f
5231 /* Decode the parameter-list info for a function type or function definition.
5232    The argument is the value returned by `get_parm_info' (or made in c-parse.c
5233    if there is an identifier list instead of a parameter decl list).
5234    These two functions are separate because when a function returns
5235    or receives functions then each is called multiple times but the order
5236    of calls is different.  The last call to `grokparms' is always the one
5237    that contains the formal parameter names of a function definition.
5238
5239    Return a list of arg types to use in the FUNCTION_TYPE for this function.
5240
5241    FUNCDEF_FLAG is true for a function definition, false for
5242    a mere declaration.  A nonempty identifier-list gets an error message
5243    when FUNCDEF_FLAG is false.  */
5244
5245 static tree
5246 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5247 {
5248   tree arg_types = arg_info->types;
5249
5250   if (funcdef_flag && arg_info->had_vla_unspec)
5251     {
5252       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
5253       /* C99 6.7.5.2p4 */
5254       error ("%<[*]%> not allowed in other than function prototype scope");
5255     }
5256
5257   if (arg_types == 0 && !funcdef_flag && !in_system_header)
5258     warning (OPT_Wstrict_prototypes,
5259              "function declaration isn%'t a prototype");
5260
5261   if (arg_types == error_mark_node)
5262     return 0;  /* don't set TYPE_ARG_TYPES in this case */
5263
5264   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5265     {
5266       if (!funcdef_flag)
5267         pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5268
5269       arg_info->parms = arg_info->types;
5270       arg_info->types = 0;
5271       return 0;
5272     }
5273   else
5274     {
5275       tree parm, type, typelt;
5276       unsigned int parmno;
5277
5278       /* If there is a parameter of incomplete type in a definition,
5279          this is an error.  In a declaration this is valid, and a
5280          struct or union type may be completed later, before any calls
5281          or definition of the function.  In the case where the tag was
5282          first declared within the parameter list, a warning has
5283          already been given.  If a parameter has void type, then
5284          however the function cannot be defined or called, so
5285          warn.  */
5286
5287       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5288            parm;
5289            parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5290         {
5291           type = TREE_VALUE (typelt);
5292           if (type == error_mark_node)
5293             continue;
5294
5295           if (!COMPLETE_TYPE_P (type))
5296             {
5297               if (funcdef_flag)
5298                 {
5299                   if (DECL_NAME (parm))
5300                     error ("parameter %u (%q+D) has incomplete type",
5301                            parmno, parm);
5302                   else
5303                     error ("%Jparameter %u has incomplete type",
5304                            parm, parmno);
5305
5306                   TREE_VALUE (typelt) = error_mark_node;
5307                   TREE_TYPE (parm) = error_mark_node;
5308                 }
5309               else if (VOID_TYPE_P (type))
5310                 {
5311                   if (DECL_NAME (parm))
5312                     warning (0, "parameter %u (%q+D) has void type",
5313                              parmno, parm);
5314                   else
5315                     warning (0, "%Jparameter %u has void type",
5316                              parm, parmno);
5317                 }
5318             }
5319
5320           if (DECL_NAME (parm) && TREE_USED (parm))
5321             warn_if_shadowing (parm);
5322         }
5323       return arg_types;
5324     }
5325 }
5326
5327 /* Take apart the current scope and return a c_arg_info structure with
5328    info on a parameter list just parsed.
5329
5330    This structure is later fed to 'grokparms' and 'store_parm_decls'.
5331
5332    ELLIPSIS being true means the argument list ended in '...' so don't
5333    append a sentinel (void_list_node) to the end of the type-list.  */
5334
5335 struct c_arg_info *
5336 get_parm_info (bool ellipsis)
5337 {
5338   struct c_binding *b = current_scope->bindings;
5339   struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5340                                         struct c_arg_info);
5341   tree parms    = 0;
5342   tree tags     = 0;
5343   tree types    = 0;
5344   tree others   = 0;
5345
5346   static bool explained_incomplete_types = false;
5347   bool gave_void_only_once_err = false;
5348
5349   arg_info->parms = 0;
5350   arg_info->tags = 0;
5351   arg_info->types = 0;
5352   arg_info->others = 0;
5353   arg_info->pending_sizes = 0;
5354   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5355
5356   /* The bindings in this scope must not get put into a block.
5357      We will take care of deleting the binding nodes.  */
5358   current_scope->bindings = 0;
5359
5360   /* This function is only called if there was *something* on the
5361      parameter list.  */
5362   gcc_assert (b);
5363
5364   /* A parameter list consisting solely of 'void' indicates that the
5365      function takes no arguments.  But if the 'void' is qualified
5366      (by 'const' or 'volatile'), or has a storage class specifier
5367      ('register'), then the behavior is undefined; issue an error.
5368      Typedefs for 'void' are OK (see DR#157).  */
5369   if (b->prev == 0                          /* one binding */
5370       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
5371       && !DECL_NAME (b->decl)               /* anonymous */
5372       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5373     {
5374       if (TREE_THIS_VOLATILE (b->decl)
5375           || TREE_READONLY (b->decl)
5376           || C_DECL_REGISTER (b->decl))
5377         error ("%<void%> as only parameter may not be qualified");
5378
5379       /* There cannot be an ellipsis.  */
5380       if (ellipsis)
5381         error ("%<void%> must be the only parameter");
5382
5383       arg_info->types = void_list_node;
5384       return arg_info;
5385     }
5386
5387   if (!ellipsis)
5388     types = void_list_node;
5389
5390   /* Break up the bindings list into parms, tags, types, and others;
5391      apply sanity checks; purge the name-to-decl bindings.  */
5392   while (b)
5393     {
5394       tree decl = b->decl;
5395       tree type = TREE_TYPE (decl);
5396       const char *keyword;
5397
5398       switch (TREE_CODE (decl))
5399         {
5400         case PARM_DECL:
5401           if (b->id)
5402             {
5403               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5404               I_SYMBOL_BINDING (b->id) = b->shadowed;
5405             }
5406
5407           /* Check for forward decls that never got their actual decl.  */
5408           if (TREE_ASM_WRITTEN (decl))
5409             error ("parameter %q+D has just a forward declaration", decl);
5410           /* Check for (..., void, ...) and issue an error.  */
5411           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5412             {
5413               if (!gave_void_only_once_err)
5414                 {
5415                   error ("%<void%> must be the only parameter");
5416                   gave_void_only_once_err = true;
5417                 }
5418             }
5419           else
5420             {
5421               /* Valid parameter, add it to the list.  */
5422               TREE_CHAIN (decl) = parms;
5423               parms = decl;
5424
5425               /* Since there is a prototype, args are passed in their
5426                  declared types.  The back end may override this later.  */
5427               DECL_ARG_TYPE (decl) = type;
5428               types = tree_cons (0, type, types);
5429             }
5430           break;
5431
5432         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5433         case UNION_TYPE:    keyword = "union"; goto tag;
5434         case RECORD_TYPE:   keyword = "struct"; goto tag;
5435         tag:
5436           /* Types may not have tag-names, in which case the type
5437              appears in the bindings list with b->id NULL.  */
5438           if (b->id)
5439             {
5440               gcc_assert (I_TAG_BINDING (b->id) == b);
5441               I_TAG_BINDING (b->id) = b->shadowed;
5442             }
5443
5444           /* Warn about any struct, union or enum tags defined in a
5445              parameter list.  The scope of such types is limited to
5446              the parameter list, which is rarely if ever desirable
5447              (it's impossible to call such a function with type-
5448              correct arguments).  An anonymous union parm type is
5449              meaningful as a GNU extension, so don't warn for that.  */
5450           if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5451             {
5452               if (b->id)
5453                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5454                 warning (0, "%<%s %E%> declared inside parameter list",
5455                          keyword, b->id);
5456               else
5457                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5458                 warning (0, "anonymous %s declared inside parameter list",
5459                          keyword);
5460
5461               if (!explained_incomplete_types)
5462                 {
5463                   warning (0, "its scope is only this definition or declaration,"
5464                            " which is probably not what you want");
5465                   explained_incomplete_types = true;
5466                 }
5467             }
5468
5469           tags = tree_cons (b->id, decl, tags);
5470           break;
5471
5472         case CONST_DECL:
5473         case TYPE_DECL:
5474         case FUNCTION_DECL:
5475           /* CONST_DECLs appear here when we have an embedded enum,
5476              and TYPE_DECLs appear here when we have an embedded struct
5477              or union.  No warnings for this - we already warned about the
5478              type itself.  FUNCTION_DECLs appear when there is an implicit
5479              function declaration in the parameter list.  */
5480
5481           TREE_CHAIN (decl) = others;
5482           others = decl;
5483           /* fall through */
5484
5485         case ERROR_MARK:
5486           /* error_mark_node appears here when we have an undeclared
5487              variable.  Just throw it away.  */
5488           if (b->id)
5489             {
5490               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5491               I_SYMBOL_BINDING (b->id) = b->shadowed;
5492             }
5493           break;
5494
5495           /* Other things that might be encountered.  */
5496         case LABEL_DECL:
5497         case VAR_DECL:
5498         default:
5499           gcc_unreachable ();
5500         }
5501
5502       b = free_binding_and_advance (b);
5503     }
5504
5505   arg_info->parms = parms;
5506   arg_info->tags = tags;
5507   arg_info->types = types;
5508   arg_info->others = others;
5509   arg_info->pending_sizes = get_pending_sizes ();
5510   return arg_info;
5511 }
5512 \f
5513 /* Get the struct, enum or union (CODE says which) with tag NAME.
5514    Define the tag as a forward-reference if it is not defined.
5515    Return a c_typespec structure for the type specifier.  */
5516
5517 struct c_typespec
5518 parser_xref_tag (enum tree_code code, tree name)
5519 {
5520   struct c_typespec ret;
5521   tree ref;
5522
5523   ret.expr = NULL_TREE;
5524   ret.expr_const_operands = true;
5525
5526   /* If a cross reference is requested, look up the type
5527      already defined for this tag and return it.  */
5528
5529   ref = lookup_tag (code, name, 0);
5530   /* If this is the right type of tag, return what we found.
5531      (This reference will be shadowed by shadow_tag later if appropriate.)
5532      If this is the wrong type of tag, do not return it.  If it was the
5533      wrong type in the same scope, we will have had an error
5534      message already; if in a different scope and declaring
5535      a name, pending_xref_error will give an error message; but if in a
5536      different scope and not declaring a name, this tag should
5537      shadow the previous declaration of a different type of tag, and
5538      this would not work properly if we return the reference found.
5539      (For example, with "struct foo" in an outer scope, "union foo;"
5540      must shadow that tag with a new one of union type.)  */
5541   ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5542   if (ref && TREE_CODE (ref) == code)
5543     {
5544       ret.spec = ref;
5545       return ret;
5546     }
5547
5548   /* If no such tag is yet defined, create a forward-reference node
5549      and record it as the "definition".
5550      When a real declaration of this type is found,
5551      the forward-reference will be altered into a real type.  */
5552
5553   ref = make_node (code);
5554   if (code == ENUMERAL_TYPE)
5555     {
5556       /* Give the type a default layout like unsigned int
5557          to avoid crashing if it does not get defined.  */
5558       SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5559       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5560       TYPE_USER_ALIGN (ref) = 0;
5561       TYPE_UNSIGNED (ref) = 1;
5562       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5563       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5564       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5565     }
5566
5567   pushtag (name, ref);
5568
5569   ret.spec = ref;
5570   return ret;
5571 }
5572
5573 /* Get the struct, enum or union (CODE says which) with tag NAME.
5574    Define the tag as a forward-reference if it is not defined.
5575    Return a tree for the type.  */
5576
5577 tree
5578 xref_tag (enum tree_code code, tree name)
5579 {
5580   return parser_xref_tag (code, name).spec;
5581 }
5582 \f
5583 /* Make sure that the tag NAME is defined *in the current scope*
5584    at least as a forward reference.
5585    CODE says which kind of tag NAME ought to be.  */
5586
5587 tree
5588 start_struct (enum tree_code code, tree name)
5589 {
5590   /* If there is already a tag defined at this scope
5591      (as a forward reference), just return it.  */
5592
5593   tree ref = 0;
5594
5595   if (name != 0)
5596     ref = lookup_tag (code, name, 1);
5597   if (ref && TREE_CODE (ref) == code)
5598     {
5599       if (TYPE_SIZE (ref))
5600         {
5601           if (code == UNION_TYPE)
5602             error ("redefinition of %<union %E%>", name);
5603           else
5604             error ("redefinition of %<struct %E%>", name);
5605           /* Don't create structures using a name already in use.  */
5606           ref = NULL_TREE;
5607         }
5608       else if (C_TYPE_BEING_DEFINED (ref))
5609         {
5610           if (code == UNION_TYPE)
5611             error ("nested redefinition of %<union %E%>", name);
5612           else
5613             error ("nested redefinition of %<struct %E%>", name);
5614           /* Don't create structures that contain themselves.  */
5615           ref = NULL_TREE;
5616         }
5617     }
5618
5619   /* Otherwise create a forward-reference just so the tag is in scope.  */
5620
5621   if (ref == NULL_TREE || TREE_CODE (ref) != code)
5622     {
5623       ref = make_node (code);
5624       pushtag (name, ref);
5625     }
5626
5627   C_TYPE_BEING_DEFINED (ref) = 1;
5628   TYPE_PACKED (ref) = flag_pack_struct;
5629   return ref;
5630 }
5631
5632 /* Process the specs, declarator and width (NULL if omitted)
5633    of a structure component, returning a FIELD_DECL node.
5634    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5635    DECL_ATTRS is as for grokdeclarator.
5636
5637    LOC is the location of the structure component.
5638
5639    This is done during the parsing of the struct declaration.
5640    The FIELD_DECL nodes are chained together and the lot of them
5641    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5642
5643 tree
5644 grokfield (location_t loc,
5645            struct c_declarator *declarator, struct c_declspecs *declspecs,
5646            tree width, tree *decl_attrs)
5647 {
5648   tree value;
5649
5650   if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5651       && width == NULL_TREE)
5652     {
5653       /* This is an unnamed decl.
5654
5655          If we have something of the form "union { list } ;" then this
5656          is the anonymous union extension.  Similarly for struct.
5657
5658          If this is something of the form "struct foo;", then
5659            If MS extensions are enabled, this is handled as an
5660              anonymous struct.
5661            Otherwise this is a forward declaration of a structure tag.
5662
5663          If this is something of the form "foo;" and foo is a TYPE_DECL, then
5664            If MS extensions are enabled and foo names a structure, then
5665              again this is an anonymous struct.
5666            Otherwise this is an error.
5667
5668          Oh what a horrid tangled web we weave.  I wonder if MS consciously
5669          took this from Plan 9 or if it was an accident of implementation
5670          that took root before someone noticed the bug...  */
5671
5672       tree type = declspecs->type;
5673       bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5674                       || TREE_CODE (type) == UNION_TYPE);
5675       bool ok = false;
5676
5677       if (type_ok
5678           && (flag_ms_extensions || !declspecs->typedef_p))
5679         {
5680           if (flag_ms_extensions)
5681             ok = true;
5682           else if (flag_iso)
5683             ok = false;
5684           else if (TYPE_NAME (type) == NULL)
5685             ok = true;
5686           else
5687             ok = false;
5688         }
5689       if (!ok)
5690         {
5691           pedwarn (loc, 0, "declaration does not declare anything");
5692           return NULL_TREE;
5693         }
5694       pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5695     }
5696
5697   value = grokdeclarator (declarator, declspecs, FIELD, false,
5698                           width ? &width : NULL, decl_attrs, NULL, NULL,
5699                           DEPRECATED_NORMAL);
5700
5701   finish_decl (value, NULL_TREE, NULL_TREE, NULL_TREE);
5702   DECL_INITIAL (value) = width;
5703
5704   return value;
5705 }
5706 \f
5707 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
5708    the list such that this does not present a problem later.  */
5709
5710 static void
5711 detect_field_duplicates (tree fieldlist)
5712 {
5713   tree x, y;
5714   int timeout = 10;
5715
5716   /* First, see if there are more than "a few" fields.
5717      This is trivially true if there are zero or one fields.  */
5718   if (!fieldlist)
5719     return;
5720   x = TREE_CHAIN (fieldlist);
5721   if (!x)
5722     return;
5723   do {
5724     timeout--;
5725     x = TREE_CHAIN (x);
5726   } while (timeout > 0 && x);
5727
5728   /* If there were "few" fields, avoid the overhead of allocating
5729      a hash table.  Instead just do the nested traversal thing.  */
5730   if (timeout > 0)
5731     {
5732       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5733         if (DECL_NAME (x))
5734           {
5735             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5736               if (DECL_NAME (y) == DECL_NAME (x))
5737                 {
5738                   error ("duplicate member %q+D", x);
5739                   DECL_NAME (x) = NULL_TREE;
5740                 }
5741           }
5742     }
5743   else
5744     {
5745       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5746       void **slot;
5747
5748       for (x = fieldlist; x ; x = TREE_CHAIN (x))
5749         if ((y = DECL_NAME (x)) != 0)
5750           {
5751             slot = htab_find_slot (htab, y, INSERT);
5752             if (*slot)
5753               {
5754                 error ("duplicate member %q+D", x);
5755                 DECL_NAME (x) = NULL_TREE;
5756               }
5757             *slot = y;
5758           }
5759
5760       htab_delete (htab);
5761     }
5762 }
5763
5764 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5765    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5766    ATTRIBUTES are attributes to be applied to the structure.  */
5767
5768 tree
5769 finish_struct (tree t, tree fieldlist, tree attributes)
5770 {
5771   tree x;
5772   bool toplevel = file_scope == current_scope;
5773   int saw_named_field;
5774
5775   /* If this type was previously laid out as a forward reference,
5776      make sure we lay it out again.  */
5777
5778   TYPE_SIZE (t) = 0;
5779
5780   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5781
5782   if (pedantic)
5783     {
5784       for (x = fieldlist; x; x = TREE_CHAIN (x))
5785         if (DECL_NAME (x) != 0)
5786           break;
5787
5788       if (x == 0)
5789         {
5790           if (TREE_CODE (t) == UNION_TYPE)
5791             {
5792               if (fieldlist)
5793                 pedwarn (input_location, OPT_pedantic, "union has no named members");
5794               else
5795                 pedwarn (input_location, OPT_pedantic, "union has no members");
5796             }
5797           else
5798             {
5799               if (fieldlist)
5800                 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5801               else
5802                 pedwarn (input_location, OPT_pedantic, "struct has no members");
5803             }
5804         }
5805     }
5806
5807   /* Install struct as DECL_CONTEXT of each field decl.
5808      Also process specified field sizes, found in the DECL_INITIAL,
5809      storing 0 there after the type has been changed to precision equal
5810      to its width, rather than the precision of the specified standard
5811      type.  (Correct layout requires the original type to have been preserved
5812      until now.)  */
5813
5814   saw_named_field = 0;
5815   for (x = fieldlist; x; x = TREE_CHAIN (x))
5816     {
5817       if (TREE_TYPE (x) == error_mark_node)
5818         continue;
5819
5820       DECL_CONTEXT (x) = t;
5821
5822       /* If any field is const, the structure type is pseudo-const.  */
5823       if (TREE_READONLY (x))
5824         C_TYPE_FIELDS_READONLY (t) = 1;
5825       else
5826         {
5827           /* A field that is pseudo-const makes the structure likewise.  */
5828           tree t1 = TREE_TYPE (x);
5829           while (TREE_CODE (t1) == ARRAY_TYPE)
5830             t1 = TREE_TYPE (t1);
5831           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5832               && C_TYPE_FIELDS_READONLY (t1))
5833             C_TYPE_FIELDS_READONLY (t) = 1;
5834         }
5835
5836       /* Any field that is volatile means variables of this type must be
5837          treated in some ways as volatile.  */
5838       if (TREE_THIS_VOLATILE (x))
5839         C_TYPE_FIELDS_VOLATILE (t) = 1;
5840
5841       /* Any field of nominal variable size implies structure is too.  */
5842       if (C_DECL_VARIABLE_SIZE (x))
5843         C_TYPE_VARIABLE_SIZE (t) = 1;
5844
5845       if (DECL_INITIAL (x))
5846         {
5847           unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5848           DECL_SIZE (x) = bitsize_int (width);
5849           DECL_BIT_FIELD (x) = 1;
5850           SET_DECL_C_BIT_FIELD (x);
5851         }
5852
5853       if (TYPE_PACKED (t)
5854           && (DECL_BIT_FIELD (x)
5855               || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5856         DECL_PACKED (x) = 1;
5857
5858       /* Detect flexible array member in an invalid context.  */
5859       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5860           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5861           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5862           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5863         {
5864           if (TREE_CODE (t) == UNION_TYPE)
5865             {
5866               error ("%Jflexible array member in union", x);
5867               TREE_TYPE (x) = error_mark_node;
5868             }
5869           else if (TREE_CHAIN (x) != NULL_TREE)
5870             {
5871               error ("%Jflexible array member not at end of struct", x);
5872               TREE_TYPE (x) = error_mark_node;
5873             }
5874           else if (!saw_named_field)
5875             {
5876               error ("%Jflexible array member in otherwise empty struct", x);
5877               TREE_TYPE (x) = error_mark_node;
5878             }
5879         }
5880
5881       if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5882           && flexible_array_type_p (TREE_TYPE (x)))
5883         pedwarn (input_location, OPT_pedantic, 
5884                  "%Jinvalid use of structure with flexible array member", x);
5885
5886       if (DECL_NAME (x))
5887         saw_named_field = 1;
5888     }
5889
5890   detect_field_duplicates (fieldlist);
5891
5892   /* Now we have the nearly final fieldlist.  Record it,
5893      then lay out the structure or union (including the fields).  */
5894
5895   TYPE_FIELDS (t) = fieldlist;
5896
5897   layout_type (t);
5898
5899   /* Give bit-fields their proper types.  */
5900   {
5901     tree *fieldlistp = &fieldlist;
5902     while (*fieldlistp)
5903       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5904           && TREE_TYPE (*fieldlistp) != error_mark_node)
5905         {
5906           unsigned HOST_WIDE_INT width
5907             = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5908           tree type = TREE_TYPE (*fieldlistp);
5909           if (width != TYPE_PRECISION (type))
5910             {
5911               TREE_TYPE (*fieldlistp)
5912                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5913               DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5914             }
5915           DECL_INITIAL (*fieldlistp) = 0;
5916         }
5917       else
5918         fieldlistp = &TREE_CHAIN (*fieldlistp);
5919   }
5920
5921   /* Now we have the truly final field list.
5922      Store it in this type and in the variants.  */
5923
5924   TYPE_FIELDS (t) = fieldlist;
5925
5926   /* If there are lots of fields, sort so we can look through them fast.
5927      We arbitrarily consider 16 or more elts to be "a lot".  */
5928
5929   {
5930     int len = 0;
5931
5932     for (x = fieldlist; x; x = TREE_CHAIN (x))
5933       {
5934         if (len > 15 || DECL_NAME (x) == NULL)
5935           break;
5936         len += 1;
5937       }
5938
5939     if (len > 15)
5940       {
5941         tree *field_array;
5942         struct lang_type *space;
5943         struct sorted_fields_type *space2;
5944
5945         len += list_length (x);
5946
5947         /* Use the same allocation policy here that make_node uses, to
5948           ensure that this lives as long as the rest of the struct decl.
5949           All decls in an inline function need to be saved.  */
5950
5951         space = GGC_CNEW (struct lang_type);
5952         space2 = GGC_NEWVAR (struct sorted_fields_type,
5953                              sizeof (struct sorted_fields_type) + len * sizeof (tree));
5954
5955         len = 0;
5956         space->s = space2;
5957         field_array = &space2->elts[0];
5958         for (x = fieldlist; x; x = TREE_CHAIN (x))
5959           {
5960             field_array[len++] = x;
5961
5962             /* If there is anonymous struct or union, break out of the loop.  */
5963             if (DECL_NAME (x) == NULL)
5964               break;
5965           }
5966         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5967         if (x == NULL)
5968           {
5969             TYPE_LANG_SPECIFIC (t) = space;
5970             TYPE_LANG_SPECIFIC (t)->s->len = len;
5971             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5972             qsort (field_array, len, sizeof (tree), field_decl_cmp);
5973           }
5974       }
5975   }
5976
5977   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5978     {
5979       TYPE_FIELDS (x) = TYPE_FIELDS (t);
5980       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5981       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5982       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5983       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5984     }
5985
5986   /* If this was supposed to be a transparent union, but we can't
5987      make it one, warn and turn off the flag.  */
5988   if (TREE_CODE (t) == UNION_TYPE
5989       && TYPE_TRANSPARENT_UNION (t)
5990       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5991     {
5992       TYPE_TRANSPARENT_UNION (t) = 0;
5993       warning (0, "union cannot be made transparent");
5994     }
5995
5996   /* If this structure or union completes the type of any previous
5997      variable declaration, lay it out and output its rtl.  */
5998   for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5999        x;
6000        x = TREE_CHAIN (x))
6001     {
6002       tree decl = TREE_VALUE (x);
6003       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6004         layout_array_type (TREE_TYPE (decl));
6005       if (TREE_CODE (decl) != TYPE_DECL)
6006         {
6007           layout_decl (decl, 0);
6008           if (c_dialect_objc ())
6009             objc_check_decl (decl);
6010           rest_of_decl_compilation (decl, toplevel, 0);
6011           if (!toplevel)
6012             expand_decl (decl);
6013         }
6014     }
6015   C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6016
6017   /* Finish debugging output for this type.  */
6018   rest_of_type_compilation (t, toplevel);
6019
6020   /* If we're inside a function proper, i.e. not file-scope and not still
6021      parsing parameters, then arrange for the size of a variable sized type
6022      to be bound now.  */
6023   if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6024     add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6025
6026   return t;
6027 }
6028
6029 /* Lay out the type T, and its element type, and so on.  */
6030
6031 static void
6032 layout_array_type (tree t)
6033 {
6034   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6035     layout_array_type (TREE_TYPE (t));
6036   layout_type (t);
6037 }
6038 \f
6039 /* Begin compiling the definition of an enumeration type.
6040    NAME is its name (or null if anonymous).
6041    Returns the type object, as yet incomplete.
6042    Also records info about it so that build_enumerator
6043    may be used to declare the individual values as they are read.  */
6044
6045 tree
6046 start_enum (struct c_enum_contents *the_enum, tree name)
6047 {
6048   tree enumtype = 0;
6049
6050   /* If this is the real definition for a previous forward reference,
6051      fill in the contents in the same object that used to be the
6052      forward reference.  */
6053
6054   if (name != 0)
6055     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
6056
6057   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6058     {
6059       enumtype = make_node (ENUMERAL_TYPE);
6060       pushtag (name, enumtype);
6061     }
6062
6063   if (C_TYPE_BEING_DEFINED (enumtype))
6064     error ("nested redefinition of %<enum %E%>", name);
6065
6066   C_TYPE_BEING_DEFINED (enumtype) = 1;
6067
6068   if (TYPE_VALUES (enumtype) != 0)
6069     {
6070       /* This enum is a named one that has been declared already.  */
6071       error ("redeclaration of %<enum %E%>", name);
6072
6073       /* Completely replace its old definition.
6074          The old enumerators remain defined, however.  */
6075       TYPE_VALUES (enumtype) = 0;
6076     }
6077
6078   the_enum->enum_next_value = integer_zero_node;
6079   the_enum->enum_overflow = 0;
6080
6081   if (flag_short_enums)
6082     TYPE_PACKED (enumtype) = 1;
6083
6084   return enumtype;
6085 }
6086
6087 /* After processing and defining all the values of an enumeration type,
6088    install their decls in the enumeration type and finish it off.
6089    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6090    and ATTRIBUTES are the specified attributes.
6091    Returns ENUMTYPE.  */
6092
6093 tree
6094 finish_enum (tree enumtype, tree values, tree attributes)
6095 {
6096   tree pair, tem;
6097   tree minnode = 0, maxnode = 0;
6098   int precision, unsign;
6099   bool toplevel = (file_scope == current_scope);
6100   struct lang_type *lt;
6101
6102   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6103
6104   /* Calculate the maximum value of any enumerator in this type.  */
6105
6106   if (values == error_mark_node)
6107     minnode = maxnode = integer_zero_node;
6108   else
6109     {
6110       minnode = maxnode = TREE_VALUE (values);
6111       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6112         {
6113           tree value = TREE_VALUE (pair);
6114           if (tree_int_cst_lt (maxnode, value))
6115             maxnode = value;
6116           if (tree_int_cst_lt (value, minnode))
6117             minnode = value;
6118         }
6119     }
6120
6121   /* Construct the final type of this enumeration.  It is the same
6122      as one of the integral types - the narrowest one that fits, except
6123      that normally we only go as narrow as int - and signed iff any of
6124      the values are negative.  */
6125   unsign = (tree_int_cst_sgn (minnode) >= 0);
6126   precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6127                    tree_int_cst_min_precision (maxnode, unsign));
6128
6129   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6130     {
6131       tem = c_common_type_for_size (precision, unsign);
6132       if (tem == NULL)
6133         {
6134           warning (0, "enumeration values exceed range of largest integer");
6135           tem = long_long_integer_type_node;
6136         }
6137     }
6138   else
6139     tem = unsign ? unsigned_type_node : integer_type_node;
6140
6141   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6142   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6143   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6144   TYPE_SIZE (enumtype) = 0;
6145
6146   /* If the precision of the type was specific with an attribute and it
6147      was too small, give an error.  Otherwise, use it.  */
6148   if (TYPE_PRECISION (enumtype))
6149     {
6150       if (precision > TYPE_PRECISION (enumtype))
6151         error ("specified mode too small for enumeral values");
6152     }
6153   else
6154     TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6155
6156   layout_type (enumtype);
6157
6158   if (values != error_mark_node)
6159     {
6160       /* Change the type of the enumerators to be the enum type.  We
6161          need to do this irrespective of the size of the enum, for
6162          proper type checking.  Replace the DECL_INITIALs of the
6163          enumerators, and the value slots of the list, with copies
6164          that have the enum type; they cannot be modified in place
6165          because they may be shared (e.g.  integer_zero_node) Finally,
6166          change the purpose slots to point to the names of the decls.  */
6167       for (pair = values; pair; pair = TREE_CHAIN (pair))
6168         {
6169           tree enu = TREE_PURPOSE (pair);
6170           tree ini = DECL_INITIAL (enu);
6171
6172           TREE_TYPE (enu) = enumtype;
6173
6174           /* The ISO C Standard mandates enumerators to have type int,
6175              even though the underlying type of an enum type is
6176              unspecified.  However, GCC allows enumerators of any
6177              integer type as an extensions.  build_enumerator()
6178              converts any enumerators that fit in an int to type int,
6179              to avoid promotions to unsigned types when comparing
6180              integers with enumerators that fit in the int range.
6181              When -pedantic is given, build_enumerator() would have
6182              already warned about those that don't fit. Here we
6183              convert the rest to the enumerator type. */
6184           if (TREE_TYPE (ini) != integer_type_node)
6185             ini = convert (enumtype, ini);
6186
6187           DECL_INITIAL (enu) = ini;
6188           TREE_PURPOSE (pair) = DECL_NAME (enu);
6189           TREE_VALUE (pair) = ini;
6190         }
6191
6192       TYPE_VALUES (enumtype) = values;
6193     }
6194
6195   /* Record the min/max values so that we can warn about bit-field
6196      enumerations that are too small for the values.  */
6197   lt = GGC_CNEW (struct lang_type);
6198   lt->enum_min = minnode;
6199   lt->enum_max = maxnode;
6200   TYPE_LANG_SPECIFIC (enumtype) = lt;
6201
6202   /* Fix up all variant types of this enum type.  */
6203   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6204     {
6205       if (tem == enumtype)
6206         continue;
6207       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6208       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6209       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6210       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6211       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6212       SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6213       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6214       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6215       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6216       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6217       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6218     }
6219
6220   /* Finish debugging output for this type.  */
6221   rest_of_type_compilation (enumtype, toplevel);
6222
6223   return enumtype;
6224 }
6225
6226 /* Build and install a CONST_DECL for one value of the
6227    current enumeration type (one that was begun with start_enum).
6228    Return a tree-list containing the CONST_DECL and its value.
6229    Assignment of sequential values by default is handled here.  */
6230
6231 tree
6232 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6233                   location_t value_loc)
6234 {
6235   tree decl, type;
6236
6237   /* Validate and default VALUE.  */
6238
6239   if (value != 0)
6240     {
6241       /* Don't issue more errors for error_mark_node (i.e. an
6242          undeclared identifier) - just ignore the value expression.  */
6243       if (value == error_mark_node)
6244         value = 0;
6245       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6246         {
6247           error ("enumerator value for %qE is not an integer constant", name);
6248           value = 0;
6249         }
6250       else
6251         {
6252           if (TREE_CODE (value) != INTEGER_CST)
6253             {
6254               value = c_fully_fold (value, false, NULL);
6255               if (TREE_CODE (value) == INTEGER_CST)
6256                 pedwarn (value_loc, OPT_pedantic,
6257                          "enumerator value for %qE is not an integer "
6258                          "constant expression", name);
6259             }
6260           if (TREE_CODE (value) != INTEGER_CST)
6261             {
6262               error ("enumerator value for %qE is not an integer constant",
6263                      name);
6264               value = 0;
6265             }
6266           else
6267             {
6268               value = default_conversion (value);
6269               constant_expression_warning (value);
6270             }
6271         }
6272     }
6273
6274   /* Default based on previous value.  */
6275   /* It should no longer be possible to have NON_LVALUE_EXPR
6276      in the default.  */
6277   if (value == 0)
6278     {
6279       value = the_enum->enum_next_value;
6280       if (the_enum->enum_overflow)
6281         error ("overflow in enumeration values");
6282     }
6283   /* Even though the underlying type of an enum is unspecified, the
6284      type of enumeration constants is explicitly defined as int
6285      (6.4.4.3/2 in the C99 Standard).  GCC allows any integer type as
6286      an extension.  */
6287   else if (!int_fits_type_p (value, integer_type_node))
6288     pedwarn (value_loc, OPT_pedantic, 
6289              "ISO C restricts enumerator values to range of %<int%>");
6290
6291   /* The ISO C Standard mandates enumerators to have type int, even
6292      though the underlying type of an enum type is unspecified.
6293      However, GCC allows enumerators of any integer type as an
6294      extensions.  Here we convert any enumerators that fit in an int
6295      to type int, to avoid promotions to unsigned types when comparing
6296      integers with enumerators that fit in the int range.  When
6297      -pedantic is given, we would have already warned about those that
6298      don't fit. We have to do this here rather than in finish_enum
6299      because this value may be used to define more enumerators.  */
6300   if (int_fits_type_p (value, integer_type_node))
6301     value = convert (integer_type_node, value);
6302
6303   /* Set basis for default for next value.  */
6304   the_enum->enum_next_value
6305     = build_binary_op
6306          (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6307          PLUS_EXPR, value, integer_one_node, 0);
6308   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6309
6310   /* Now create a declaration for the enum value name.  */
6311
6312   type = TREE_TYPE (value);
6313   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6314                                       TYPE_PRECISION (integer_type_node)),
6315                                  (TYPE_PRECISION (type)
6316                                   >= TYPE_PRECISION (integer_type_node)
6317                                   && TYPE_UNSIGNED (type)));
6318
6319   decl = build_decl (CONST_DECL, name, type);
6320   DECL_INITIAL (decl) = convert (type, value);
6321   pushdecl (decl);
6322
6323   return tree_cons (decl, value, NULL_TREE);
6324 }
6325
6326 \f
6327 /* Create the FUNCTION_DECL for a function definition.
6328    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6329    the declaration; they describe the function's name and the type it returns,
6330    but twisted together in a fashion that parallels the syntax of C.
6331
6332    This function creates a binding context for the function body
6333    as well as setting up the FUNCTION_DECL in current_function_decl.
6334
6335    Returns 1 on success.  If the DECLARATOR is not suitable for a function
6336    (it defines a datum instead), we return 0, which tells
6337    yyparse to report a parse error.  */
6338
6339 int
6340 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6341                 tree attributes)
6342 {
6343   tree decl1, old_decl;
6344   tree restype, resdecl;
6345   struct c_label_context_se *nstack_se;
6346   struct c_label_context_vm *nstack_vm;
6347
6348   current_function_returns_value = 0;  /* Assume, until we see it does.  */
6349   current_function_returns_null = 0;
6350   current_function_returns_abnormally = 0;
6351   warn_about_return_type = 0;
6352   c_switch_stack = NULL;
6353
6354   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6355   nstack_se->labels_def = NULL;
6356   nstack_se->labels_used = NULL;
6357   nstack_se->next = label_context_stack_se;
6358   label_context_stack_se = nstack_se;
6359
6360   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6361   nstack_vm->labels_def = NULL;
6362   nstack_vm->labels_used = NULL;
6363   nstack_vm->scope = 0;
6364   nstack_vm->next = label_context_stack_vm;
6365   label_context_stack_vm = nstack_vm;
6366
6367   /* Indicate no valid break/continue context by setting these variables
6368      to some non-null, non-label value.  We'll notice and emit the proper
6369      error message in c_finish_bc_stmt.  */
6370   c_break_label = c_cont_label = size_zero_node;
6371
6372   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6373                           &attributes, NULL, NULL, DEPRECATED_NORMAL);
6374
6375   /* If the declarator is not suitable for a function definition,
6376      cause a syntax error.  */
6377   if (decl1 == 0)
6378     {
6379       label_context_stack_se = label_context_stack_se->next;
6380       label_context_stack_vm = label_context_stack_vm->next;
6381       return 0;
6382     }
6383
6384   decl_attributes (&decl1, attributes, 0);
6385
6386   if (DECL_DECLARED_INLINE_P (decl1)
6387       && DECL_UNINLINABLE (decl1)
6388       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6389     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6390              decl1);
6391
6392   /* Handle gnu_inline attribute.  */
6393   if (declspecs->inline_p
6394       && !flag_gnu89_inline
6395       && TREE_CODE (decl1) == FUNCTION_DECL
6396       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6397           || current_function_decl))
6398     {
6399       if (declspecs->storage_class != csc_static)
6400         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6401     }
6402
6403   announce_function (decl1);
6404
6405   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6406     {
6407       error ("return type is an incomplete type");
6408       /* Make it return void instead.  */
6409       TREE_TYPE (decl1)
6410         = build_function_type (void_type_node,
6411                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6412     }
6413
6414   if (warn_about_return_type)
6415     pedwarn_c99 (input_location, flag_isoc99 ? 0 
6416                  : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6417                  "return type defaults to %<int%>");
6418
6419   /* Make the init_value nonzero so pushdecl knows this is not tentative.
6420      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
6421   DECL_INITIAL (decl1) = error_mark_node;
6422
6423   /* If this definition isn't a prototype and we had a prototype declaration
6424      before, copy the arg type info from that prototype.  */
6425   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6426   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6427     old_decl = 0;
6428   current_function_prototype_locus = UNKNOWN_LOCATION;
6429   current_function_prototype_built_in = false;
6430   current_function_prototype_arg_types = NULL_TREE;
6431   if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6432     {
6433       if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6434           && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6435                         TREE_TYPE (TREE_TYPE (old_decl))))
6436         {
6437           TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6438                                               TREE_TYPE (decl1));
6439           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6440           current_function_prototype_built_in
6441             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6442           current_function_prototype_arg_types
6443             = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6444         }
6445       if (TREE_PUBLIC (decl1))
6446         {
6447           /* If there is an external prototype declaration of this
6448              function, record its location but do not copy information
6449              to this decl.  This may be an invisible declaration
6450              (built-in or in a scope which has finished) or simply
6451              have more refined argument types than any declaration
6452              found above.  */
6453           struct c_binding *b;
6454           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6455             if (B_IN_SCOPE (b, external_scope))
6456               break;
6457           if (b)
6458             {
6459               tree ext_decl, ext_type;
6460               ext_decl = b->decl;
6461               ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6462               if (TREE_CODE (ext_type) == FUNCTION_TYPE
6463                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6464                                 TREE_TYPE (ext_type)))
6465                 {
6466                   current_function_prototype_locus
6467                     = DECL_SOURCE_LOCATION (ext_decl);
6468                   current_function_prototype_built_in
6469                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6470                   current_function_prototype_arg_types
6471                     = TYPE_ARG_TYPES (ext_type);
6472                 }
6473             }
6474         }
6475     }
6476
6477   /* Optionally warn of old-fashioned def with no previous prototype.  */
6478   if (warn_strict_prototypes
6479       && old_decl != error_mark_node
6480       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6481       && C_DECL_ISNT_PROTOTYPE (old_decl))
6482     warning (OPT_Wstrict_prototypes,
6483              "function declaration isn%'t a prototype");
6484   /* Optionally warn of any global def with no previous prototype.  */
6485   else if (warn_missing_prototypes
6486            && old_decl != error_mark_node
6487            && TREE_PUBLIC (decl1)
6488            && !MAIN_NAME_P (DECL_NAME (decl1))
6489            && C_DECL_ISNT_PROTOTYPE (old_decl))
6490     warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6491   /* Optionally warn of any def with no previous prototype
6492      if the function has already been used.  */
6493   else if (warn_missing_prototypes
6494            && old_decl != 0
6495            && old_decl != error_mark_node
6496            && TREE_USED (old_decl)
6497            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6498     warning (OPT_Wmissing_prototypes,
6499              "%q+D was used with no prototype before its definition", decl1);
6500   /* Optionally warn of any global def with no previous declaration.  */
6501   else if (warn_missing_declarations
6502            && TREE_PUBLIC (decl1)
6503            && old_decl == 0
6504            && !MAIN_NAME_P (DECL_NAME (decl1)))
6505     warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6506              decl1);
6507   /* Optionally warn of any def with no previous declaration
6508      if the function has already been used.  */
6509   else if (warn_missing_declarations
6510            && old_decl != 0
6511            && old_decl != error_mark_node
6512            && TREE_USED (old_decl)
6513            && C_DECL_IMPLICIT (old_decl))
6514     warning (OPT_Wmissing_declarations,
6515              "%q+D was used with no declaration before its definition", decl1);
6516
6517   /* This function exists in static storage.
6518      (This does not mean `static' in the C sense!)  */
6519   TREE_STATIC (decl1) = 1;
6520
6521   /* A nested function is not global.  */
6522   if (current_function_decl != 0)
6523     TREE_PUBLIC (decl1) = 0;
6524
6525   /* This is the earliest point at which we might know the assembler
6526      name of the function.  Thus, if it's set before this, die horribly.  */
6527   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6528
6529   /* If #pragma weak was used, mark the decl weak now.  */
6530   if (current_scope == file_scope)
6531     maybe_apply_pragma_weak (decl1);
6532
6533   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
6534   if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6535     {
6536       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6537           != integer_type_node)
6538         pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6539
6540       check_main_parameter_types (decl1);
6541
6542       if (!TREE_PUBLIC (decl1))
6543         pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6544     }
6545
6546   /* Record the decl so that the function name is defined.
6547      If we already have a decl for this name, and it is a FUNCTION_DECL,
6548      use the old decl.  */
6549
6550   current_function_decl = pushdecl (decl1);
6551
6552   push_scope ();
6553   declare_parm_level ();
6554
6555   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6556   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6557   DECL_ARTIFICIAL (resdecl) = 1;
6558   DECL_IGNORED_P (resdecl) = 1;
6559   DECL_RESULT (current_function_decl) = resdecl;
6560
6561   start_fname_decls ();
6562
6563   return 1;
6564 }
6565 \f
6566 /* Subroutine of store_parm_decls which handles new-style function
6567    definitions (prototype format). The parms already have decls, so we
6568    need only record them as in effect and complain if any redundant
6569    old-style parm decls were written.  */
6570 static void
6571 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6572 {
6573   tree decl;
6574
6575   if (current_scope->bindings)
6576     {
6577       error ("%Jold-style parameter declarations in prototyped "
6578              "function definition", fndecl);
6579
6580       /* Get rid of the old-style declarations.  */
6581       pop_scope ();
6582       push_scope ();
6583     }
6584   /* Don't issue this warning for nested functions, and don't issue this
6585      warning if we got here because ARG_INFO_TYPES was error_mark_node
6586      (this happens when a function definition has just an ellipsis in
6587      its parameter list).  */
6588   else if (!in_system_header && !current_function_scope
6589            && arg_info->types != error_mark_node)
6590     warning (OPT_Wtraditional,
6591              "%Jtraditional C rejects ISO C style function definitions",
6592              fndecl);
6593
6594   /* Now make all the parameter declarations visible in the function body.
6595      We can bypass most of the grunt work of pushdecl.  */
6596   for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6597     {
6598       DECL_CONTEXT (decl) = current_function_decl;
6599       if (DECL_NAME (decl))
6600         {
6601           bind (DECL_NAME (decl), decl, current_scope,
6602                 /*invisible=*/false, /*nested=*/false,
6603                 UNKNOWN_LOCATION);
6604           if (!TREE_USED (decl))
6605             warn_if_shadowing (decl);
6606         }
6607       else
6608         error ("%Jparameter name omitted", decl);
6609     }
6610
6611   /* Record the parameter list in the function declaration.  */
6612   DECL_ARGUMENTS (fndecl) = arg_info->parms;
6613
6614   /* Now make all the ancillary declarations visible, likewise.  */
6615   for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6616     {
6617       DECL_CONTEXT (decl) = current_function_decl;
6618       if (DECL_NAME (decl))
6619         bind (DECL_NAME (decl), decl, current_scope,
6620               /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6621     }
6622
6623   /* And all the tag declarations.  */
6624   for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6625     if (TREE_PURPOSE (decl))
6626       bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6627             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6628 }
6629
6630 /* Subroutine of store_parm_decls which handles old-style function
6631    definitions (separate parameter list and declarations).  */
6632
6633 static void
6634 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6635 {
6636   struct c_binding *b;
6637   tree parm, decl, last;
6638   tree parmids = arg_info->parms;
6639   struct pointer_set_t *seen_args = pointer_set_create ();
6640
6641   if (!in_system_header)
6642     warning (OPT_Wold_style_definition, "%Jold-style function definition",
6643              fndecl);
6644
6645   /* Match each formal parameter name with its declaration.  Save each
6646      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6647   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6648     {
6649       if (TREE_VALUE (parm) == 0)
6650         {
6651           error ("%Jparameter name missing from parameter list", fndecl);
6652           TREE_PURPOSE (parm) = 0;
6653           continue;
6654         }
6655
6656       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6657       if (b && B_IN_CURRENT_SCOPE (b))
6658         {
6659           decl = b->decl;
6660           /* If we got something other than a PARM_DECL it is an error.  */
6661           if (TREE_CODE (decl) != PARM_DECL)
6662             error ("%q+D declared as a non-parameter", decl);
6663           /* If the declaration is already marked, we have a duplicate
6664              name.  Complain and ignore the duplicate.  */
6665           else if (pointer_set_contains (seen_args, decl))
6666             {
6667               error ("multiple parameters named %q+D", decl);
6668               TREE_PURPOSE (parm) = 0;
6669               continue;
6670             }
6671           /* If the declaration says "void", complain and turn it into
6672              an int.  */
6673           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6674             {
6675               error ("parameter %q+D declared with void type", decl);
6676               TREE_TYPE (decl) = integer_type_node;
6677               DECL_ARG_TYPE (decl) = integer_type_node;
6678               layout_decl (decl, 0);
6679             }
6680           warn_if_shadowing (decl);
6681         }
6682       /* If no declaration found, default to int.  */
6683       else
6684         {
6685           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6686           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6687           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6688           pushdecl (decl);
6689           warn_if_shadowing (decl);
6690
6691           if (flag_isoc99)
6692             pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6693           else 
6694             warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6695         }
6696
6697       TREE_PURPOSE (parm) = decl;
6698       pointer_set_insert (seen_args, decl);
6699     }
6700
6701   /* Now examine the parms chain for incomplete declarations
6702      and declarations with no corresponding names.  */
6703
6704   for (b = current_scope->bindings; b; b = b->prev)
6705     {
6706       parm = b->decl;
6707       if (TREE_CODE (parm) != PARM_DECL)
6708         continue;
6709
6710       if (TREE_TYPE (parm) != error_mark_node
6711           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6712         {
6713           error ("parameter %q+D has incomplete type", parm);
6714           TREE_TYPE (parm) = error_mark_node;
6715         }
6716
6717       if (!pointer_set_contains (seen_args, parm))
6718         {
6719           error ("declaration for parameter %q+D but no such parameter", parm);
6720
6721           /* Pretend the parameter was not missing.
6722              This gets us to a standard state and minimizes
6723              further error messages.  */
6724           parmids = chainon (parmids, tree_cons (parm, 0, 0));
6725         }
6726     }
6727
6728   /* Chain the declarations together in the order of the list of
6729      names.  Store that chain in the function decl, replacing the
6730      list of names.  Update the current scope to match.  */
6731   DECL_ARGUMENTS (fndecl) = 0;
6732
6733   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6734     if (TREE_PURPOSE (parm))
6735       break;
6736   if (parm && TREE_PURPOSE (parm))
6737     {
6738       last = TREE_PURPOSE (parm);
6739       DECL_ARGUMENTS (fndecl) = last;
6740
6741       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6742         if (TREE_PURPOSE (parm))
6743           {
6744             TREE_CHAIN (last) = TREE_PURPOSE (parm);
6745             last = TREE_PURPOSE (parm);
6746           }
6747       TREE_CHAIN (last) = 0;
6748     }
6749
6750   pointer_set_destroy (seen_args);
6751
6752   /* If there was a previous prototype,
6753      set the DECL_ARG_TYPE of each argument according to
6754      the type previously specified, and report any mismatches.  */
6755
6756   if (current_function_prototype_arg_types)
6757     {
6758       tree type;
6759       for (parm = DECL_ARGUMENTS (fndecl),
6760              type = current_function_prototype_arg_types;
6761            parm || (type && TREE_VALUE (type) != error_mark_node
6762                    && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6763            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6764         {
6765           if (parm == 0 || type == 0
6766               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6767             {
6768               if (current_function_prototype_built_in)
6769                 warning (0, "number of arguments doesn%'t match "
6770                          "built-in prototype");
6771               else
6772                 {
6773                   error ("number of arguments doesn%'t match prototype");
6774                   error ("%Hprototype declaration",
6775                          &current_function_prototype_locus);
6776                 }
6777               break;
6778             }
6779           /* Type for passing arg must be consistent with that
6780              declared for the arg.  ISO C says we take the unqualified
6781              type for parameters declared with qualified type.  */
6782           if (TREE_TYPE (parm) != error_mark_node
6783               && TREE_TYPE (type) != error_mark_node
6784               && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6785                              TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6786             {
6787               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6788                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6789                 {
6790                   /* Adjust argument to match prototype.  E.g. a previous
6791                      `int foo(float);' prototype causes
6792                      `int foo(x) float x; {...}' to be treated like
6793                      `int foo(float x) {...}'.  This is particularly
6794                      useful for argument types like uid_t.  */
6795                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6796
6797                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6798                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6799                       && TYPE_PRECISION (TREE_TYPE (parm))
6800                       < TYPE_PRECISION (integer_type_node))
6801                     DECL_ARG_TYPE (parm) = integer_type_node;
6802
6803                   /* ??? Is it possible to get here with a
6804                      built-in prototype or will it always have
6805                      been diagnosed as conflicting with an
6806                      old-style definition and discarded?  */
6807                   if (current_function_prototype_built_in)
6808                     warning (OPT_pedantic, "promoted argument %qD "
6809                              "doesn%'t match built-in prototype", parm);
6810                   else
6811                     {
6812                       pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6813                                "doesn%'t match prototype", parm);
6814                       pedwarn (current_function_prototype_locus, OPT_pedantic,
6815                                "prototype declaration");
6816                     }
6817                 }
6818               else
6819                 {
6820                   if (current_function_prototype_built_in)
6821                     warning (0, "argument %qD doesn%'t match "
6822                              "built-in prototype", parm);
6823                   else
6824                     {
6825                       error ("argument %qD doesn%'t match prototype", parm);
6826                       error ("%Hprototype declaration",
6827                              &current_function_prototype_locus);
6828                     }
6829                 }
6830             }
6831         }
6832       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6833     }
6834
6835   /* Otherwise, create a prototype that would match.  */
6836
6837   else
6838     {
6839       tree actual = 0, last = 0, type;
6840
6841       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6842         {
6843           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6844           if (last)
6845             TREE_CHAIN (last) = type;
6846           else
6847             actual = type;
6848           last = type;
6849         }
6850       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6851       if (last)
6852         TREE_CHAIN (last) = type;
6853       else
6854         actual = type;
6855
6856       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6857          of the type of this function, but we need to avoid having this
6858          affect the types of other similarly-typed functions, so we must
6859          first force the generation of an identical (but separate) type
6860          node for the relevant function type.  The new node we create
6861          will be a variant of the main variant of the original function
6862          type.  */
6863
6864       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6865
6866       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6867     }
6868 }
6869
6870 /* Store parameter declarations passed in ARG_INFO into the current
6871    function declaration.  */
6872
6873 void
6874 store_parm_decls_from (struct c_arg_info *arg_info)
6875 {
6876   current_function_arg_info = arg_info;
6877   store_parm_decls ();
6878 }
6879
6880 /* Store the parameter declarations into the current function declaration.
6881    This is called after parsing the parameter declarations, before
6882    digesting the body of the function.
6883
6884    For an old-style definition, construct a prototype out of the old-style
6885    parameter declarations and inject it into the function's type.  */
6886
6887 void
6888 store_parm_decls (void)
6889 {
6890   tree fndecl = current_function_decl;
6891   bool proto;
6892
6893   /* The argument information block for FNDECL.  */
6894   struct c_arg_info *arg_info = current_function_arg_info;
6895   current_function_arg_info = 0;
6896
6897   /* True if this definition is written with a prototype.  Note:
6898      despite C99 6.7.5.3p14, we can *not* treat an empty argument
6899      list in a function definition as equivalent to (void) -- an
6900      empty argument list specifies the function has no parameters,
6901      but only (void) sets up a prototype for future calls.  */
6902   proto = arg_info->types != 0;
6903
6904   if (proto)
6905     store_parm_decls_newstyle (fndecl, arg_info);
6906   else
6907     store_parm_decls_oldstyle (fndecl, arg_info);
6908
6909   /* The next call to push_scope will be a function body.  */
6910
6911   next_is_function_body = true;
6912
6913   /* Write a record describing this function definition to the prototypes
6914      file (if requested).  */
6915
6916   gen_aux_info_record (fndecl, 1, 0, proto);
6917
6918   /* Initialize the RTL code for the function.  */
6919   allocate_struct_function (fndecl, false);
6920
6921   /* Begin the statement tree for this function.  */
6922   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6923
6924   /* ??? Insert the contents of the pending sizes list into the function
6925      to be evaluated.  The only reason left to have this is
6926         void foo(int n, int array[n++])
6927      because we throw away the array type in favor of a pointer type, and
6928      thus won't naturally see the SAVE_EXPR containing the increment.  All
6929      other pending sizes would be handled by gimplify_parameters.  */
6930   {
6931     tree t;
6932     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6933       add_stmt (TREE_VALUE (t));
6934   }
6935
6936   /* Even though we're inside a function body, we still don't want to
6937      call expand_expr to calculate the size of a variable-sized array.
6938      We haven't necessarily assigned RTL to all variables yet, so it's
6939      not safe to try to expand expressions involving them.  */
6940   cfun->dont_save_pending_sizes_p = 1;
6941 }
6942 \f
6943 /* Emit diagnostics that require gimple input for detection.  Operate on
6944    FNDECL and all its nested functions.  */
6945
6946 static void
6947 c_gimple_diagnostics_recursively (tree fndecl)
6948 {
6949   struct cgraph_node *cgn;
6950   gimple_seq body = gimple_body (fndecl);
6951
6952   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6953   c_warn_unused_result (body);
6954
6955   /* Notice when OpenMP structured block constraints are violated.  */
6956   if (flag_openmp)
6957     diagnose_omp_structured_block_errors (fndecl);
6958
6959   /* Finalize all nested functions now.  */
6960   cgn = cgraph_node (fndecl);
6961   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6962     c_gimple_diagnostics_recursively (cgn->decl);
6963 }
6964
6965 /* Finish up a function declaration and compile that function
6966    all the way to assembler language output.  The free the storage
6967    for the function definition.
6968
6969    This is called after parsing the body of the function definition.  */
6970
6971 void
6972 finish_function (void)
6973 {
6974   tree fndecl = current_function_decl;
6975
6976   label_context_stack_se = label_context_stack_se->next;
6977   label_context_stack_vm = label_context_stack_vm->next;
6978
6979   if (TREE_CODE (fndecl) == FUNCTION_DECL
6980       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6981     {
6982       tree args = DECL_ARGUMENTS (fndecl);
6983       for (; args; args = TREE_CHAIN (args))
6984         {
6985           tree type = TREE_TYPE (args);
6986           if (INTEGRAL_TYPE_P (type)
6987               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6988             DECL_ARG_TYPE (args) = integer_type_node;
6989         }
6990     }
6991
6992   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6993     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6994
6995   /* Must mark the RESULT_DECL as being in this function.  */
6996
6997   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6998     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6999
7000   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
7001       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
7002       == integer_type_node && flag_isoc99)
7003     {
7004       tree stmt = c_finish_return (integer_zero_node, NULL_TREE);
7005       /* Hack.  We don't want the middle-end to warn that this return
7006          is unreachable, so we mark its location as special.  Using
7007          UNKNOWN_LOCATION has the problem that it gets clobbered in
7008          annotate_one_with_locus.  A cleaner solution might be to
7009          ensure ! should_carry_locus_p (stmt), but that needs a flag.
7010       */
7011       SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7012     }
7013
7014   /* Tie off the statement tree for this function.  */
7015   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7016
7017   finish_fname_decls ();
7018
7019   /* Complain if there's just no return statement.  */
7020   if (warn_return_type
7021       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7022       && !current_function_returns_value && !current_function_returns_null
7023       /* Don't complain if we are no-return.  */
7024       && !current_function_returns_abnormally
7025       /* Don't warn for main().  */
7026       && !MAIN_NAME_P (DECL_NAME (fndecl))
7027       /* Or if they didn't actually specify a return type.  */
7028       && !C_FUNCTION_IMPLICIT_INT (fndecl)
7029       /* Normally, with -Wreturn-type, flow will complain, but we might
7030          optimize out static functions.  */
7031       && !TREE_PUBLIC (fndecl))
7032     {
7033       warning (OPT_Wreturn_type,
7034                "no return statement in function returning non-void");
7035       TREE_NO_WARNING (fndecl) = 1;
7036     }
7037
7038   /* Store the end of the function, so that we get good line number
7039      info for the epilogue.  */
7040   cfun->function_end_locus = input_location;
7041
7042   /* Finalize the ELF visibility for the function.  */
7043   c_determine_visibility (fndecl);
7044
7045   /* For GNU C extern inline functions disregard inline limits.  */
7046   if (DECL_EXTERNAL (fndecl) 
7047       && DECL_DECLARED_INLINE_P (fndecl))
7048     DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
7049
7050   /* Genericize before inlining.  Delay genericizing nested functions
7051      until their parent function is genericized.  Since finalizing
7052      requires GENERIC, delay that as well.  */
7053
7054   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7055       && !undef_nested_function)
7056     {
7057       if (!decl_function_context (fndecl))
7058         {
7059           c_genericize (fndecl);
7060           c_gimple_diagnostics_recursively (fndecl);
7061
7062           /* ??? Objc emits functions after finalizing the compilation unit.
7063              This should be cleaned up later and this conditional removed.  */
7064           if (cgraph_global_info_ready)
7065             {
7066               cgraph_add_new_function (fndecl, false);
7067               return;
7068             }
7069
7070           cgraph_finalize_function (fndecl, false);
7071         }
7072       else
7073         {
7074           /* Register this function with cgraph just far enough to get it
7075             added to our parent's nested function list.  Handy, since the
7076             C front end doesn't have such a list.  */
7077           (void) cgraph_node (fndecl);
7078         }
7079     }
7080
7081   if (!decl_function_context (fndecl))
7082     undef_nested_function = false;
7083
7084   /* We're leaving the context of this function, so zap cfun.
7085      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7086      tree_rest_of_compilation.  */
7087   set_cfun (NULL);
7088   current_function_decl = NULL;
7089 }
7090 \f
7091 /* Check the declarations given in a for-loop for satisfying the C99
7092    constraints.  If exactly one such decl is found, return it.  */
7093
7094 tree
7095 check_for_loop_decls (void)
7096 {
7097   struct c_binding *b;
7098   tree one_decl = NULL_TREE;
7099   int n_decls = 0;
7100
7101   if (!flag_isoc99)
7102     {
7103       static bool hint = true;
7104       /* If we get here, declarations have been used in a for loop without
7105          the C99 for loop scope.  This doesn't make much sense, so don't
7106          allow it.  */
7107       error ("%<for%> loop initial declarations are only allowed in C99 mode");
7108       if (hint)
7109         {
7110           inform (input_location, 
7111                   "use option -std=c99 or -std=gnu99 to compile your code");
7112           hint = false;
7113         }
7114       return NULL_TREE;
7115     }
7116   /* C99 subclause 6.8.5 paragraph 3:
7117
7118        [#3]  The  declaration  part  of  a for statement shall only
7119        declare identifiers for objects having storage class auto or
7120        register.
7121
7122      It isn't clear whether, in this sentence, "identifiers" binds to
7123      "shall only declare" or to "objects" - that is, whether all identifiers
7124      declared must be identifiers for objects, or whether the restriction
7125      only applies to those that are.  (A question on this in comp.std.c
7126      in November 2000 received no answer.)  We implement the strictest
7127      interpretation, to avoid creating an extension which later causes
7128      problems.  */
7129
7130   for (b = current_scope->bindings; b; b = b->prev)
7131     {
7132       tree id = b->id;
7133       tree decl = b->decl;
7134
7135       if (!id)
7136         continue;
7137
7138       switch (TREE_CODE (decl))
7139         {
7140         case VAR_DECL:
7141           if (TREE_STATIC (decl))
7142             error ("declaration of static variable %q+D in %<for%> loop "
7143                    "initial declaration", decl);
7144           else if (DECL_EXTERNAL (decl))
7145             error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7146                    "initial declaration", decl);
7147           break;
7148
7149         case RECORD_TYPE:
7150           error ("%<struct %E%> declared in %<for%> loop initial declaration",
7151                  id);
7152           break;
7153         case UNION_TYPE:
7154           error ("%<union %E%> declared in %<for%> loop initial declaration",
7155                  id);
7156           break;
7157         case ENUMERAL_TYPE:
7158           error ("%<enum %E%> declared in %<for%> loop initial declaration",
7159                  id);
7160           break;
7161         default:
7162           error ("declaration of non-variable %q+D in %<for%> loop "
7163                  "initial declaration", decl);
7164         }
7165
7166       n_decls++;
7167       one_decl = decl;
7168     }
7169
7170   return n_decls == 1 ? one_decl : NULL_TREE;
7171 }
7172 \f
7173 /* Save and reinitialize the variables
7174    used during compilation of a C function.  */
7175
7176 void
7177 c_push_function_context (void)
7178 {
7179   struct language_function *p;
7180   p = GGC_NEW (struct language_function);
7181   cfun->language = p;
7182
7183   p->base.x_stmt_tree = c_stmt_tree;
7184   p->x_break_label = c_break_label;
7185   p->x_cont_label = c_cont_label;
7186   p->x_switch_stack = c_switch_stack;
7187   p->arg_info = current_function_arg_info;
7188   p->returns_value = current_function_returns_value;
7189   p->returns_null = current_function_returns_null;
7190   p->returns_abnormally = current_function_returns_abnormally;
7191   p->warn_about_return_type = warn_about_return_type;
7192
7193   push_function_context ();
7194 }
7195
7196 /* Restore the variables used during compilation of a C function.  */
7197
7198 void
7199 c_pop_function_context (void)
7200 {
7201   struct language_function *p;
7202
7203   pop_function_context ();
7204   p = cfun->language;
7205   cfun->language = NULL;
7206
7207   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7208       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7209     {
7210       /* Stop pointing to the local nodes about to be freed.  */
7211       /* But DECL_INITIAL must remain nonzero so we know this
7212          was an actual function definition.  */
7213       DECL_INITIAL (current_function_decl) = error_mark_node;
7214       DECL_ARGUMENTS (current_function_decl) = 0;
7215     }
7216
7217   c_stmt_tree = p->base.x_stmt_tree;
7218   c_break_label = p->x_break_label;
7219   c_cont_label = p->x_cont_label;
7220   c_switch_stack = p->x_switch_stack;
7221   current_function_arg_info = p->arg_info;
7222   current_function_returns_value = p->returns_value;
7223   current_function_returns_null = p->returns_null;
7224   current_function_returns_abnormally = p->returns_abnormally;
7225   warn_about_return_type = p->warn_about_return_type;
7226 }
7227
7228 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
7229
7230 void
7231 c_dup_lang_specific_decl (tree decl)
7232 {
7233   struct lang_decl *ld;
7234
7235   if (!DECL_LANG_SPECIFIC (decl))
7236     return;
7237
7238   ld = GGC_NEW (struct lang_decl);
7239   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7240   DECL_LANG_SPECIFIC (decl) = ld;
7241 }
7242
7243 /* The functions below are required for functionality of doing
7244    function at once processing in the C front end. Currently these
7245    functions are not called from anywhere in the C front end, but as
7246    these changes continue, that will change.  */
7247
7248 /* Returns the stmt_tree (if any) to which statements are currently
7249    being added.  If there is no active statement-tree, NULL is
7250    returned.  */
7251
7252 stmt_tree
7253 current_stmt_tree (void)
7254 {
7255   return &c_stmt_tree;
7256 }
7257
7258 /* Return the global value of T as a symbol.  */
7259
7260 tree
7261 identifier_global_value (tree t)
7262 {
7263   struct c_binding *b;
7264
7265   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7266     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7267       return b->decl;
7268
7269   return 0;
7270 }
7271
7272 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
7273    otherwise the name is found in ridpointers from RID_INDEX.  */
7274
7275 void
7276 record_builtin_type (enum rid rid_index, const char *name, tree type)
7277 {
7278   tree id, decl;
7279   if (name == 0)
7280     id = ridpointers[(int) rid_index];
7281   else
7282     id = get_identifier (name);
7283   decl = build_decl (TYPE_DECL, id, type);
7284   pushdecl (decl);
7285   if (debug_hooks->type_decl)
7286     debug_hooks->type_decl (decl, false);
7287 }
7288
7289 /* Build the void_list_node (void_type_node having been created).  */
7290 tree
7291 build_void_list_node (void)
7292 {
7293   tree t = build_tree_list (NULL_TREE, void_type_node);
7294   return t;
7295 }
7296
7297 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
7298
7299 struct c_parm *
7300 build_c_parm (struct c_declspecs *specs, tree attrs,
7301               struct c_declarator *declarator)
7302 {
7303   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7304   ret->specs = specs;
7305   ret->attrs = attrs;
7306   ret->declarator = declarator;
7307   return ret;
7308 }
7309
7310 /* Return a declarator with nested attributes.  TARGET is the inner
7311    declarator to which these attributes apply.  ATTRS are the
7312    attributes.  */
7313
7314 struct c_declarator *
7315 build_attrs_declarator (tree attrs, struct c_declarator *target)
7316 {
7317   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7318   ret->kind = cdk_attrs;
7319   ret->declarator = target;
7320   ret->u.attrs = attrs;
7321   return ret;
7322 }
7323
7324 /* Return a declarator for a function with arguments specified by ARGS
7325    and return type specified by TARGET.  */
7326
7327 struct c_declarator *
7328 build_function_declarator (struct c_arg_info *args,
7329                            struct c_declarator *target)
7330 {
7331   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7332   ret->kind = cdk_function;
7333   ret->declarator = target;
7334   ret->u.arg_info = args;
7335   return ret;
7336 }
7337
7338 /* Return a declarator for the identifier IDENT (which may be
7339    NULL_TREE for an abstract declarator).  */
7340
7341 struct c_declarator *
7342 build_id_declarator (tree ident)
7343 {
7344   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7345   ret->kind = cdk_id;
7346   ret->declarator = 0;
7347   ret->u.id = ident;
7348   /* Default value - may get reset to a more precise location. */
7349   ret->id_loc = input_location;
7350   return ret;
7351 }
7352
7353 /* Return something to represent absolute declarators containing a *.
7354    TARGET is the absolute declarator that the * contains.
7355    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7356    to apply to the pointer type.  */
7357
7358 struct c_declarator *
7359 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7360                          struct c_declarator *target)
7361 {
7362   tree attrs;
7363   int quals = 0;
7364   struct c_declarator *itarget = target;
7365   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7366   if (type_quals_attrs)
7367     {
7368       attrs = type_quals_attrs->attrs;
7369       quals = quals_from_declspecs (type_quals_attrs);
7370       if (attrs != NULL_TREE)
7371         itarget = build_attrs_declarator (attrs, target);
7372     }
7373   ret->kind = cdk_pointer;
7374   ret->declarator = itarget;
7375   ret->u.pointer_quals = quals;
7376   return ret;
7377 }
7378
7379 /* Return a pointer to a structure for an empty list of declaration
7380    specifiers.  */
7381
7382 struct c_declspecs *
7383 build_null_declspecs (void)
7384 {
7385   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7386   ret->type = 0;
7387   ret->expr = 0;
7388   ret->decl_attr = 0;
7389   ret->attrs = 0;
7390   ret->typespec_word = cts_none;
7391   ret->storage_class = csc_none;
7392   ret->expr_const_operands = true;
7393   ret->declspecs_seen_p = false;
7394   ret->type_seen_p = false;
7395   ret->non_sc_seen_p = false;
7396   ret->typedef_p = false;
7397   ret->tag_defined_p = false;
7398   ret->explicit_signed_p = false;
7399   ret->deprecated_p = false;
7400   ret->default_int_p = false;
7401   ret->long_p = false;
7402   ret->long_long_p = false;
7403   ret->short_p = false;
7404   ret->signed_p = false;
7405   ret->unsigned_p = false;
7406   ret->complex_p = false;
7407   ret->inline_p = false;
7408   ret->thread_p = false;
7409   ret->const_p = false;
7410   ret->volatile_p = false;
7411   ret->restrict_p = false;
7412   ret->saturating_p = false;
7413   return ret;
7414 }
7415
7416 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7417    returning SPECS.  */
7418
7419 struct c_declspecs *
7420 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7421 {
7422   enum rid i;
7423   bool dupe = false;
7424   specs->non_sc_seen_p = true;
7425   specs->declspecs_seen_p = true;
7426   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7427               && C_IS_RESERVED_WORD (qual));
7428   i = C_RID_CODE (qual);
7429   switch (i)
7430     {
7431     case RID_CONST:
7432       dupe = specs->const_p;
7433       specs->const_p = true;
7434       break;
7435     case RID_VOLATILE:
7436       dupe = specs->volatile_p;
7437       specs->volatile_p = true;
7438       break;
7439     case RID_RESTRICT:
7440       dupe = specs->restrict_p;
7441       specs->restrict_p = true;
7442       break;
7443     default:
7444       gcc_unreachable ();
7445     }
7446   if (dupe && !flag_isoc99)
7447     pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7448   return specs;
7449 }
7450
7451 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7452    returning SPECS.  */
7453
7454 struct c_declspecs *
7455 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7456 {
7457   tree type = spec.spec;
7458   specs->non_sc_seen_p = true;
7459   specs->declspecs_seen_p = true;
7460   specs->type_seen_p = true;
7461   if (TREE_DEPRECATED (type))
7462     specs->deprecated_p = true;
7463
7464   /* Handle type specifier keywords.  */
7465   if (TREE_CODE (type) == IDENTIFIER_NODE
7466       && C_IS_RESERVED_WORD (type)
7467       && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7468     {
7469       enum rid i = C_RID_CODE (type);
7470       if (specs->type)
7471         {
7472           error ("two or more data types in declaration specifiers");
7473           return specs;
7474         }
7475       if ((int) i <= (int) RID_LAST_MODIFIER)
7476         {
7477           /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat".  */
7478           bool dupe = false;
7479           switch (i)
7480             {
7481             case RID_LONG:
7482               if (specs->long_long_p)
7483                 {
7484                   error ("%<long long long%> is too long for GCC");
7485                   break;
7486                 }
7487               if (specs->long_p)
7488                 {
7489                   if (specs->typespec_word == cts_double)
7490                     {
7491                       error ("both %<long long%> and %<double%> in "
7492                              "declaration specifiers");
7493                       break;
7494                     }
7495                   pedwarn_c90 (input_location, OPT_Wlong_long, 
7496                                "ISO C90 does not support %<long long%>");
7497                   specs->long_long_p = 1;
7498                   break;
7499                 }
7500               if (specs->short_p)
7501                 error ("both %<long%> and %<short%> in "
7502                        "declaration specifiers");
7503               else if (specs->typespec_word == cts_void)
7504                 error ("both %<long%> and %<void%> in "
7505                        "declaration specifiers");
7506               else if (specs->typespec_word == cts_bool)
7507                 error ("both %<long%> and %<_Bool%> in "
7508                        "declaration specifiers");
7509               else if (specs->typespec_word == cts_char)
7510                 error ("both %<long%> and %<char%> in "
7511                        "declaration specifiers");
7512               else if (specs->typespec_word == cts_float)
7513                 error ("both %<long%> and %<float%> in "
7514                        "declaration specifiers");
7515               else if (specs->typespec_word == cts_dfloat32)
7516                 error ("both %<long%> and %<_Decimal32%> in "
7517                        "declaration specifiers");
7518               else if (specs->typespec_word == cts_dfloat64)
7519                 error ("both %<long%> and %<_Decimal64%> in "
7520                        "declaration specifiers");
7521               else if (specs->typespec_word == cts_dfloat128)
7522                 error ("both %<long%> and %<_Decimal128%> in "
7523                        "declaration specifiers");
7524               else
7525                 specs->long_p = true;
7526               break;
7527             case RID_SHORT:
7528               dupe = specs->short_p;
7529               if (specs->long_p)
7530                 error ("both %<long%> and %<short%> in "
7531                        "declaration specifiers");
7532               else if (specs->typespec_word == cts_void)
7533                 error ("both %<short%> and %<void%> in "
7534                        "declaration specifiers");
7535               else if (specs->typespec_word == cts_bool)
7536                 error ("both %<short%> and %<_Bool%> in "
7537                        "declaration specifiers");
7538               else if (specs->typespec_word == cts_char)
7539                 error ("both %<short%> and %<char%> in "
7540                        "declaration specifiers");
7541               else if (specs->typespec_word == cts_float)
7542                 error ("both %<short%> and %<float%> in "
7543                        "declaration specifiers");
7544               else if (specs->typespec_word == cts_double)
7545                 error ("both %<short%> and %<double%> in "
7546                        "declaration specifiers");
7547               else if (specs->typespec_word == cts_dfloat32)
7548                 error ("both %<short%> and %<_Decimal32%> in "
7549                        "declaration specifiers");
7550               else if (specs->typespec_word == cts_dfloat64)
7551                 error ("both %<short%> and %<_Decimal64%> in "
7552                                         "declaration specifiers");
7553               else if (specs->typespec_word == cts_dfloat128)
7554                 error ("both %<short%> and %<_Decimal128%> in "
7555                        "declaration specifiers");
7556               else
7557                 specs->short_p = true;
7558               break;
7559             case RID_SIGNED:
7560               dupe = specs->signed_p;
7561               if (specs->unsigned_p)
7562                 error ("both %<signed%> and %<unsigned%> in "
7563                        "declaration specifiers");
7564               else if (specs->typespec_word == cts_void)
7565                 error ("both %<signed%> and %<void%> in "
7566                        "declaration specifiers");
7567               else if (specs->typespec_word == cts_bool)
7568                 error ("both %<signed%> and %<_Bool%> in "
7569                        "declaration specifiers");
7570               else if (specs->typespec_word == cts_float)
7571                 error ("both %<signed%> and %<float%> in "
7572                        "declaration specifiers");
7573               else if (specs->typespec_word == cts_double)
7574                 error ("both %<signed%> and %<double%> in "
7575                        "declaration specifiers");
7576               else if (specs->typespec_word == cts_dfloat32)
7577                 error ("both %<signed%> and %<_Decimal32%> in "
7578                        "declaration specifiers");
7579               else if (specs->typespec_word == cts_dfloat64)
7580                 error ("both %<signed%> and %<_Decimal64%> in "
7581                        "declaration specifiers");
7582               else if (specs->typespec_word == cts_dfloat128)
7583                 error ("both %<signed%> and %<_Decimal128%> in "
7584                        "declaration specifiers");
7585               else
7586                 specs->signed_p = true;
7587               break;
7588             case RID_UNSIGNED:
7589               dupe = specs->unsigned_p;
7590               if (specs->signed_p)
7591                 error ("both %<signed%> and %<unsigned%> in "
7592                        "declaration specifiers");
7593               else if (specs->typespec_word == cts_void)
7594                 error ("both %<unsigned%> and %<void%> in "
7595                        "declaration specifiers");
7596               else if (specs->typespec_word == cts_bool)
7597                 error ("both %<unsigned%> and %<_Bool%> in "
7598                        "declaration specifiers");
7599               else if (specs->typespec_word == cts_float)
7600                 error ("both %<unsigned%> and %<float%> in "
7601                        "declaration specifiers");
7602               else if (specs->typespec_word == cts_double)
7603                 error ("both %<unsigned%> and %<double%> in "
7604                        "declaration specifiers");
7605               else if (specs->typespec_word == cts_dfloat32)
7606                 error ("both %<unsigned%> and %<_Decimal32%> in "
7607                        "declaration specifiers");
7608               else if (specs->typespec_word == cts_dfloat64)
7609                 error ("both %<unsigned%> and %<_Decimal64%> in "
7610                        "declaration specifiers");
7611               else if (specs->typespec_word == cts_dfloat128)
7612                 error ("both %<unsigned%> and %<_Decimal128%> in "
7613                        "declaration specifiers");
7614               else
7615                 specs->unsigned_p = true;
7616               break;
7617             case RID_COMPLEX:
7618               dupe = specs->complex_p;
7619               if (!flag_isoc99 && !in_system_header)
7620                 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7621               if (specs->typespec_word == cts_void)
7622                 error ("both %<complex%> and %<void%> in "
7623                        "declaration specifiers");
7624               else if (specs->typespec_word == cts_bool)
7625                 error ("both %<complex%> and %<_Bool%> in "
7626                        "declaration specifiers");
7627               else if (specs->typespec_word == cts_dfloat32)
7628                 error ("both %<complex%> and %<_Decimal32%> in "
7629                        "declaration specifiers");
7630               else if (specs->typespec_word == cts_dfloat64)
7631                 error ("both %<complex%> and %<_Decimal64%> in "
7632                        "declaration specifiers");
7633               else if (specs->typespec_word == cts_dfloat128)
7634                 error ("both %<complex%> and %<_Decimal128%> in "
7635                        "declaration specifiers");
7636               else if (specs->typespec_word == cts_fract)
7637                 error ("both %<complex%> and %<_Fract%> in "
7638                        "declaration specifiers");
7639               else if (specs->typespec_word == cts_accum)
7640                 error ("both %<complex%> and %<_Accum%> in "
7641                        "declaration specifiers");
7642               else if (specs->saturating_p)
7643                 error ("both %<complex%> and %<_Sat%> in "
7644                        "declaration specifiers");
7645               else
7646                 specs->complex_p = true;
7647               break;
7648             case RID_SAT:
7649               dupe = specs->saturating_p;
7650               pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7651               if (specs->typespec_word == cts_void)
7652                 error ("both %<_Sat%> and %<void%> in "
7653                        "declaration specifiers");
7654               else if (specs->typespec_word == cts_bool)
7655                 error ("both %<_Sat%> and %<_Bool%> in "
7656                        "declaration specifiers");
7657               else if (specs->typespec_word == cts_char)
7658                 error ("both %<_Sat%> and %<char%> in "
7659                        "declaration specifiers");
7660               else if (specs->typespec_word == cts_int)
7661                 error ("both %<_Sat%> and %<int%> in "
7662                        "declaration specifiers");
7663               else if (specs->typespec_word == cts_float)
7664                 error ("both %<_Sat%> and %<float%> in "
7665                        "declaration specifiers");
7666               else if (specs->typespec_word == cts_double)
7667                 error ("both %<_Sat%> and %<double%> in "
7668                        "declaration specifiers");
7669               else if (specs->typespec_word == cts_dfloat32)
7670                 error ("both %<_Sat%> and %<_Decimal32%> in "
7671                        "declaration specifiers");
7672               else if (specs->typespec_word == cts_dfloat64)
7673                 error ("both %<_Sat%> and %<_Decimal64%> in "
7674                        "declaration specifiers");
7675               else if (specs->typespec_word == cts_dfloat128)
7676                 error ("both %<_Sat%> and %<_Decimal128%> in "
7677                        "declaration specifiers");
7678               else if (specs->complex_p)
7679                 error ("both %<_Sat%> and %<complex%> in "
7680                        "declaration specifiers");
7681               else
7682                 specs->saturating_p = true;
7683               break;
7684             default:
7685               gcc_unreachable ();
7686             }
7687
7688           if (dupe)
7689             error ("duplicate %qE", type);
7690
7691           return specs;
7692         }
7693       else
7694         {
7695           /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7696              "_Decimal64", "_Decimal128", "_Fract" or "_Accum".  */
7697           if (specs->typespec_word != cts_none)
7698             {
7699               error ("two or more data types in declaration specifiers");
7700               return specs;
7701             }
7702           switch (i)
7703             {
7704             case RID_VOID:
7705               if (specs->long_p)
7706                 error ("both %<long%> and %<void%> in "
7707                        "declaration specifiers");
7708               else if (specs->short_p)
7709                 error ("both %<short%> and %<void%> in "
7710                        "declaration specifiers");
7711               else if (specs->signed_p)
7712                 error ("both %<signed%> and %<void%> in "
7713                        "declaration specifiers");
7714               else if (specs->unsigned_p)
7715                 error ("both %<unsigned%> and %<void%> in "
7716                        "declaration specifiers");
7717               else if (specs->complex_p)
7718                 error ("both %<complex%> and %<void%> in "
7719                        "declaration specifiers");
7720               else if (specs->saturating_p)
7721                 error ("both %<_Sat%> and %<void%> in "
7722                        "declaration specifiers");
7723               else
7724                 specs->typespec_word = cts_void;
7725               return specs;
7726             case RID_BOOL:
7727               if (specs->long_p)
7728                 error ("both %<long%> and %<_Bool%> in "
7729                        "declaration specifiers");
7730               else if (specs->short_p)
7731                 error ("both %<short%> and %<_Bool%> in "
7732                        "declaration specifiers");
7733               else if (specs->signed_p)
7734                 error ("both %<signed%> and %<_Bool%> in "
7735                        "declaration specifiers");
7736               else if (specs->unsigned_p)
7737                 error ("both %<unsigned%> and %<_Bool%> in "
7738                        "declaration specifiers");
7739               else if (specs->complex_p)
7740                 error ("both %<complex%> and %<_Bool%> in "
7741                        "declaration specifiers");
7742               else if (specs->saturating_p)
7743                 error ("both %<_Sat%> and %<_Bool%> in "
7744                        "declaration specifiers");
7745               else
7746                 specs->typespec_word = cts_bool;
7747               return specs;
7748             case RID_CHAR:
7749               if (specs->long_p)
7750                 error ("both %<long%> and %<char%> in "
7751                        "declaration specifiers");
7752               else if (specs->short_p)
7753                 error ("both %<short%> and %<char%> in "
7754                        "declaration specifiers");
7755               else if (specs->saturating_p)
7756                 error ("both %<_Sat%> and %<char%> in "
7757                        "declaration specifiers");
7758               else
7759                 specs->typespec_word = cts_char;
7760               return specs;
7761             case RID_INT:
7762               if (specs->saturating_p)
7763                 error ("both %<_Sat%> and %<int%> in "
7764                        "declaration specifiers");
7765               else
7766                 specs->typespec_word = cts_int;
7767               return specs;
7768             case RID_FLOAT:
7769               if (specs->long_p)
7770                 error ("both %<long%> and %<float%> in "
7771                        "declaration specifiers");
7772               else if (specs->short_p)
7773                 error ("both %<short%> and %<float%> in "
7774                        "declaration specifiers");
7775               else if (specs->signed_p)
7776                 error ("both %<signed%> and %<float%> in "
7777                        "declaration specifiers");
7778               else if (specs->unsigned_p)
7779                 error ("both %<unsigned%> and %<float%> in "
7780                        "declaration specifiers");
7781               else if (specs->saturating_p)
7782                 error ("both %<_Sat%> and %<float%> in "
7783                        "declaration specifiers");
7784               else
7785                 specs->typespec_word = cts_float;
7786               return specs;
7787             case RID_DOUBLE:
7788               if (specs->long_long_p)
7789                 error ("both %<long long%> and %<double%> in "
7790                        "declaration specifiers");
7791               else if (specs->short_p)
7792                 error ("both %<short%> and %<double%> in "
7793                        "declaration specifiers");
7794               else if (specs->signed_p)
7795                 error ("both %<signed%> and %<double%> in "
7796                        "declaration specifiers");
7797               else if (specs->unsigned_p)
7798                 error ("both %<unsigned%> and %<double%> in "
7799                        "declaration specifiers");
7800               else if (specs->saturating_p)
7801                 error ("both %<_Sat%> and %<double%> in "
7802                        "declaration specifiers");
7803               else
7804                 specs->typespec_word = cts_double;
7805               return specs;
7806             case RID_DFLOAT32:
7807             case RID_DFLOAT64:
7808             case RID_DFLOAT128:
7809               { 
7810                 const char *str;
7811                 if (i == RID_DFLOAT32)
7812                   str = "_Decimal32";
7813                 else if (i == RID_DFLOAT64)
7814                   str = "_Decimal64";
7815                 else
7816                   str = "_Decimal128";
7817                 if (specs->long_long_p)
7818                   error ("both %<long long%> and %<%s%> in "
7819                          "declaration specifiers", str);
7820                 if (specs->long_p)
7821                   error ("both %<long%> and %<%s%> in "
7822                          "declaration specifiers", str);
7823                 else if (specs->short_p)
7824                   error ("both %<short%> and %<%s%> in "
7825                          "declaration specifiers", str);
7826                 else if (specs->signed_p)
7827                   error ("both %<signed%> and %<%s%> in "
7828                          "declaration specifiers", str);
7829                 else if (specs->unsigned_p)
7830                   error ("both %<unsigned%> and %<%s%> in "
7831                          "declaration specifiers", str);
7832                 else if (specs->complex_p)
7833                   error ("both %<complex%> and %<%s%> in "
7834                          "declaration specifiers", str);
7835                 else if (specs->saturating_p)
7836                   error ("both %<_Sat%> and %<%s%> in "
7837                          "declaration specifiers", str);
7838                 else if (i == RID_DFLOAT32)
7839                   specs->typespec_word = cts_dfloat32;
7840                 else if (i == RID_DFLOAT64)
7841                   specs->typespec_word = cts_dfloat64;
7842                 else
7843                   specs->typespec_word = cts_dfloat128;
7844               }
7845               if (!targetm.decimal_float_supported_p ())
7846                 error ("decimal floating point not supported for this target");
7847               pedwarn (input_location, OPT_pedantic, 
7848                        "ISO C does not support decimal floating point");
7849               return specs;
7850             case RID_FRACT:
7851             case RID_ACCUM:
7852               {
7853                 const char *str;
7854                 if (i == RID_FRACT)
7855                   str = "_Fract";
7856                 else
7857                   str = "_Accum";
7858                 if (specs->complex_p)
7859                   error ("both %<complex%> and %<%s%> in "
7860                          "declaration specifiers", str);
7861                 else if (i == RID_FRACT)
7862                     specs->typespec_word = cts_fract;
7863                 else
7864                     specs->typespec_word = cts_accum;
7865               }
7866               if (!targetm.fixed_point_supported_p ())
7867                 error ("fixed-point types not supported for this target");
7868               pedwarn (input_location, OPT_pedantic, 
7869                        "ISO C does not support fixed-point types");
7870               return specs;
7871             default:
7872               /* ObjC reserved word "id", handled below.  */
7873               break;
7874             }
7875         }
7876     }
7877
7878   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7879      form of ObjC type, cases such as "int" and "long" being handled
7880      above), a TYPE (struct, union, enum and typeof specifiers) or an
7881      ERROR_MARK.  In none of these cases may there have previously
7882      been any type specifiers.  */
7883   if (specs->type || specs->typespec_word != cts_none
7884       || specs->long_p || specs->short_p || specs->signed_p
7885       || specs->unsigned_p || specs->complex_p)
7886     error ("two or more data types in declaration specifiers");
7887   else if (TREE_CODE (type) == TYPE_DECL)
7888     {
7889       if (TREE_TYPE (type) == error_mark_node)
7890         ; /* Allow the type to default to int to avoid cascading errors.  */
7891       else
7892         {
7893           specs->type = TREE_TYPE (type);
7894           specs->decl_attr = DECL_ATTRIBUTES (type);
7895           specs->typedef_p = true;
7896           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7897         }
7898     }
7899   else if (TREE_CODE (type) == IDENTIFIER_NODE)
7900     {
7901       tree t = lookup_name (type);
7902       if (!t || TREE_CODE (t) != TYPE_DECL)
7903         error ("%qE fails to be a typedef or built in type", type);
7904       else if (TREE_TYPE (t) == error_mark_node)
7905         ;
7906       else
7907         specs->type = TREE_TYPE (t);
7908     }
7909   else if (TREE_CODE (type) != ERROR_MARK)
7910     {
7911       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7912         specs->tag_defined_p = true;
7913       if (spec.kind == ctsk_typeof)
7914         {
7915           specs->typedef_p = true;
7916           if (spec.expr)
7917             {
7918               if (specs->expr)
7919                 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7920                                       specs->expr, spec.expr);
7921               else
7922                 specs->expr = spec.expr;
7923               specs->expr_const_operands &= spec.expr_const_operands;
7924             }
7925         }
7926       specs->type = type;
7927     }
7928
7929   return specs;
7930 }
7931
7932 /* Add the storage class specifier or function specifier SCSPEC to the
7933    declaration specifiers SPECS, returning SPECS.  */
7934
7935 struct c_declspecs *
7936 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7937 {
7938   enum rid i;
7939   enum c_storage_class n = csc_none;
7940   bool dupe = false;
7941   specs->declspecs_seen_p = true;
7942   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7943               && C_IS_RESERVED_WORD (scspec));
7944   i = C_RID_CODE (scspec);
7945   if (specs->non_sc_seen_p)
7946     warning (OPT_Wold_style_declaration, 
7947              "%qE is not at beginning of declaration", scspec);
7948   switch (i)
7949     {
7950     case RID_INLINE:
7951       /* C99 permits duplicate inline.  Although of doubtful utility,
7952          it seems simplest to permit it in gnu89 mode as well, as
7953          there is also little utility in maintaining this as a
7954          difference between gnu89 and C99 inline.  */
7955       dupe = false;
7956       specs->inline_p = true;
7957       break;
7958     case RID_THREAD:
7959       dupe = specs->thread_p;
7960       if (specs->storage_class == csc_auto)
7961         error ("%<__thread%> used with %<auto%>");
7962       else if (specs->storage_class == csc_register)
7963         error ("%<__thread%> used with %<register%>");
7964       else if (specs->storage_class == csc_typedef)
7965         error ("%<__thread%> used with %<typedef%>");
7966       else
7967         specs->thread_p = true;
7968       break;
7969     case RID_AUTO:
7970       n = csc_auto;
7971       break;
7972     case RID_EXTERN:
7973       n = csc_extern;
7974       /* Diagnose "__thread extern".  */
7975       if (specs->thread_p)
7976         error ("%<__thread%> before %<extern%>");
7977       break;
7978     case RID_REGISTER:
7979       n = csc_register;
7980       break;
7981     case RID_STATIC:
7982       n = csc_static;
7983       /* Diagnose "__thread static".  */
7984       if (specs->thread_p)
7985         error ("%<__thread%> before %<static%>");
7986       break;
7987     case RID_TYPEDEF:
7988       n = csc_typedef;
7989       break;
7990     default:
7991       gcc_unreachable ();
7992     }
7993   if (n != csc_none && n == specs->storage_class)
7994     dupe = true;
7995   if (dupe)
7996     error ("duplicate %qE", scspec);
7997   if (n != csc_none)
7998     {
7999       if (specs->storage_class != csc_none && n != specs->storage_class)
8000         {
8001           error ("multiple storage classes in declaration specifiers");
8002         }
8003       else
8004         {
8005           specs->storage_class = n;
8006           if (n != csc_extern && n != csc_static && specs->thread_p)
8007             {
8008               error ("%<__thread%> used with %qE", scspec);
8009               specs->thread_p = false;
8010             }
8011         }
8012     }
8013   return specs;
8014 }
8015
8016 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8017    returning SPECS.  */
8018
8019 struct c_declspecs *
8020 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8021 {
8022   specs->attrs = chainon (attrs, specs->attrs);
8023   specs->declspecs_seen_p = true;
8024   return specs;
8025 }
8026
8027 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8028    specifiers with any other type specifier to determine the resulting
8029    type.  This is where ISO C checks on complex types are made, since
8030    "_Complex long" is a prefix of the valid ISO C type "_Complex long
8031    double".  */
8032
8033 struct c_declspecs *
8034 finish_declspecs (struct c_declspecs *specs)
8035 {
8036   /* If a type was specified as a whole, we have no modifiers and are
8037      done.  */
8038   if (specs->type != NULL_TREE)
8039     {
8040       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8041                   && !specs->signed_p && !specs->unsigned_p
8042                   && !specs->complex_p);
8043       return specs;
8044     }
8045
8046   /* If none of "void", "_Bool", "char", "int", "float" or "double"
8047      has been specified, treat it as "int" unless "_Complex" is
8048      present and there are no other specifiers.  If we just have
8049      "_Complex", it is equivalent to "_Complex double", but e.g.
8050      "_Complex short" is equivalent to "_Complex short int".  */
8051   if (specs->typespec_word == cts_none)
8052     {
8053       if (specs->saturating_p)
8054         {
8055           error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
8056           if (!targetm.fixed_point_supported_p ())
8057             error ("fixed-point types not supported for this target");
8058           specs->typespec_word = cts_fract;
8059         }
8060       else if (specs->long_p || specs->short_p
8061                || specs->signed_p || specs->unsigned_p)
8062         {
8063           specs->typespec_word = cts_int;
8064         }
8065       else if (specs->complex_p)
8066         {
8067           specs->typespec_word = cts_double;
8068           pedwarn (input_location, OPT_pedantic, 
8069                    "ISO C does not support plain %<complex%> meaning "
8070                    "%<double complex%>");
8071         }
8072       else
8073         {
8074           specs->typespec_word = cts_int;
8075           specs->default_int_p = true;
8076           /* We don't diagnose this here because grokdeclarator will
8077              give more specific diagnostics according to whether it is
8078              a function definition.  */
8079         }
8080     }
8081
8082   /* If "signed" was specified, record this to distinguish "int" and
8083      "signed int" in the case of a bit-field with
8084      -funsigned-bitfields.  */
8085   specs->explicit_signed_p = specs->signed_p;
8086
8087   /* Now compute the actual type.  */
8088   switch (specs->typespec_word)
8089     {
8090     case cts_void:
8091       gcc_assert (!specs->long_p && !specs->short_p
8092                   && !specs->signed_p && !specs->unsigned_p
8093                   && !specs->complex_p);
8094       specs->type = void_type_node;
8095       break;
8096     case cts_bool:
8097       gcc_assert (!specs->long_p && !specs->short_p
8098                   && !specs->signed_p && !specs->unsigned_p
8099                   && !specs->complex_p);
8100       specs->type = boolean_type_node;
8101       break;
8102     case cts_char:
8103       gcc_assert (!specs->long_p && !specs->short_p);
8104       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8105       if (specs->signed_p)
8106         specs->type = signed_char_type_node;
8107       else if (specs->unsigned_p)
8108         specs->type = unsigned_char_type_node;
8109       else
8110         specs->type = char_type_node;
8111       if (specs->complex_p)
8112         {
8113           pedwarn (input_location, OPT_pedantic, 
8114                    "ISO C does not support complex integer types");
8115           specs->type = build_complex_type (specs->type);
8116         }
8117       break;
8118     case cts_int:
8119       gcc_assert (!(specs->long_p && specs->short_p));
8120       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8121       if (specs->long_long_p)
8122         specs->type = (specs->unsigned_p
8123                        ? long_long_unsigned_type_node
8124                        : long_long_integer_type_node);
8125       else if (specs->long_p)
8126         specs->type = (specs->unsigned_p
8127                        ? long_unsigned_type_node
8128                        : long_integer_type_node);
8129       else if (specs->short_p)
8130         specs->type = (specs->unsigned_p
8131                        ? short_unsigned_type_node
8132                        : short_integer_type_node);
8133       else
8134         specs->type = (specs->unsigned_p
8135                        ? unsigned_type_node
8136                        : integer_type_node);
8137       if (specs->complex_p)
8138         {
8139           pedwarn (input_location, OPT_pedantic, 
8140                    "ISO C does not support complex integer types");
8141           specs->type = build_complex_type (specs->type);
8142         }
8143       break;
8144     case cts_float:
8145       gcc_assert (!specs->long_p && !specs->short_p
8146                   && !specs->signed_p && !specs->unsigned_p);
8147       specs->type = (specs->complex_p
8148                      ? complex_float_type_node
8149                      : float_type_node);
8150       break;
8151     case cts_double:
8152       gcc_assert (!specs->long_long_p && !specs->short_p
8153                   && !specs->signed_p && !specs->unsigned_p);
8154       if (specs->long_p)
8155         {
8156           specs->type = (specs->complex_p
8157                          ? complex_long_double_type_node
8158                          : long_double_type_node);
8159         }
8160       else
8161         {
8162           specs->type = (specs->complex_p
8163                          ? complex_double_type_node
8164                          : double_type_node);
8165         }
8166       break;
8167     case cts_dfloat32:
8168     case cts_dfloat64:
8169     case cts_dfloat128:
8170       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8171                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8172       if (specs->typespec_word == cts_dfloat32)
8173         specs->type = dfloat32_type_node;
8174       else if (specs->typespec_word == cts_dfloat64)
8175         specs->type = dfloat64_type_node;
8176       else
8177         specs->type = dfloat128_type_node;
8178       break;
8179     case cts_fract:
8180       gcc_assert (!specs->complex_p);
8181       if (!targetm.fixed_point_supported_p ())
8182         specs->type = integer_type_node;
8183       else if (specs->saturating_p)
8184         {
8185           if (specs->long_long_p)
8186             specs->type = specs->unsigned_p
8187                           ? sat_unsigned_long_long_fract_type_node
8188                           : sat_long_long_fract_type_node;
8189           else if (specs->long_p)
8190             specs->type = specs->unsigned_p
8191                           ? sat_unsigned_long_fract_type_node
8192                           : sat_long_fract_type_node;
8193           else if (specs->short_p)
8194             specs->type = specs->unsigned_p
8195                           ? sat_unsigned_short_fract_type_node
8196                           : sat_short_fract_type_node;
8197           else
8198             specs->type = specs->unsigned_p
8199                           ? sat_unsigned_fract_type_node
8200                           : sat_fract_type_node;
8201         }
8202       else
8203         {
8204           if (specs->long_long_p)
8205             specs->type = specs->unsigned_p
8206                           ? unsigned_long_long_fract_type_node
8207                           : long_long_fract_type_node;
8208           else if (specs->long_p)
8209             specs->type = specs->unsigned_p
8210                           ? unsigned_long_fract_type_node
8211                           : long_fract_type_node;
8212           else if (specs->short_p)
8213             specs->type = specs->unsigned_p
8214                           ? unsigned_short_fract_type_node
8215                           : short_fract_type_node;
8216           else
8217             specs->type = specs->unsigned_p
8218                           ? unsigned_fract_type_node
8219                           : fract_type_node;
8220         }
8221       break;
8222     case cts_accum:
8223       gcc_assert (!specs->complex_p);
8224       if (!targetm.fixed_point_supported_p ())
8225         specs->type = integer_type_node;
8226       else if (specs->saturating_p)
8227         {
8228           if (specs->long_long_p)
8229             specs->type = specs->unsigned_p
8230                           ? sat_unsigned_long_long_accum_type_node
8231                           : sat_long_long_accum_type_node;
8232           else if (specs->long_p)
8233             specs->type = specs->unsigned_p
8234                           ? sat_unsigned_long_accum_type_node
8235                           : sat_long_accum_type_node;
8236           else if (specs->short_p)
8237             specs->type = specs->unsigned_p
8238                           ? sat_unsigned_short_accum_type_node
8239                           : sat_short_accum_type_node;
8240           else
8241             specs->type = specs->unsigned_p
8242                           ? sat_unsigned_accum_type_node
8243                           : sat_accum_type_node;
8244         }
8245       else
8246         {
8247           if (specs->long_long_p)
8248             specs->type = specs->unsigned_p
8249                           ? unsigned_long_long_accum_type_node
8250                           : long_long_accum_type_node;
8251           else if (specs->long_p)
8252             specs->type = specs->unsigned_p
8253                           ? unsigned_long_accum_type_node
8254                           : long_accum_type_node;
8255           else if (specs->short_p)
8256             specs->type = specs->unsigned_p
8257                           ? unsigned_short_accum_type_node
8258                           : short_accum_type_node;
8259           else
8260             specs->type = specs->unsigned_p
8261                           ? unsigned_accum_type_node
8262                           : accum_type_node;
8263         }
8264       break;
8265     default:
8266       gcc_unreachable ();
8267     }
8268
8269   return specs;
8270 }
8271
8272 /* A subroutine of c_write_global_declarations.  Perform final processing
8273    on one file scope's declarations (or the external scope's declarations),
8274    GLOBALS.  */
8275
8276 static void
8277 c_write_global_declarations_1 (tree globals)
8278 {
8279   tree decl;
8280   bool reconsider;
8281
8282   /* Process the decls in the order they were written.  */
8283   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8284     {
8285       /* Check for used but undefined static functions using the C
8286          standard's definition of "used", and set TREE_NO_WARNING so
8287          that check_global_declarations doesn't repeat the check.  */
8288       if (TREE_CODE (decl) == FUNCTION_DECL
8289           && DECL_INITIAL (decl) == 0
8290           && DECL_EXTERNAL (decl)
8291           && !TREE_PUBLIC (decl)
8292           && C_DECL_USED (decl))
8293         {
8294           pedwarn (input_location, 0, "%q+F used but never defined", decl);
8295           TREE_NO_WARNING (decl) = 1;
8296         }
8297
8298       wrapup_global_declaration_1 (decl);
8299     }
8300
8301   do
8302     {
8303       reconsider = false;
8304       for (decl = globals; decl; decl = TREE_CHAIN (decl))
8305         reconsider |= wrapup_global_declaration_2 (decl);
8306     }
8307   while (reconsider);
8308
8309   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8310     check_global_declaration_1 (decl);
8311 }
8312
8313 /* A subroutine of c_write_global_declarations Emit debug information for each
8314    of the declarations in GLOBALS.  */
8315
8316 static void
8317 c_write_global_declarations_2 (tree globals)
8318 {
8319   tree decl;
8320
8321   for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8322     debug_hooks->global_decl (decl);
8323 }
8324
8325 /* Preserve the external declarations scope across a garbage collect.  */
8326 static GTY(()) tree ext_block;
8327
8328 void
8329 c_write_global_declarations (void)
8330 {
8331   tree t;
8332
8333   /* We don't want to do this if generating a PCH.  */
8334   if (pch_file)
8335     return;
8336
8337   /* Don't waste time on further processing if -fsyntax-only or we've
8338      encountered errors.  */
8339   if (flag_syntax_only || errorcount || sorrycount)
8340     return;
8341
8342   /* Close the external scope.  */
8343   ext_block = pop_scope ();
8344   external_scope = 0;
8345   gcc_assert (!current_scope);
8346
8347   if (ext_block)
8348     {
8349       tree tmp = BLOCK_VARS (ext_block);
8350       int flags;
8351       FILE * stream = dump_begin (TDI_tu, &flags);
8352       if (stream && tmp)
8353         {
8354           dump_node (tmp, flags & ~TDF_SLIM, stream);
8355           dump_end (TDI_tu, stream);
8356         }
8357     }
8358
8359   /* Process all file scopes in this compilation, and the external_scope,
8360      through wrapup_global_declarations and check_global_declarations.  */
8361   for (t = all_translation_units; t; t = TREE_CHAIN (t))
8362     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8363   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8364
8365   /* We're done parsing; proceed to optimize and emit assembly.
8366      FIXME: shouldn't be the front end's responsibility to call this.  */
8367   cgraph_optimize ();
8368
8369   /* After cgraph has had a chance to emit everything that's going to
8370      be emitted, output debug information for globals.  */
8371   if (errorcount == 0 && sorrycount == 0)
8372     {
8373       timevar_push (TV_SYMOUT);
8374       for (t = all_translation_units; t; t = TREE_CHAIN (t))
8375         c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8376       c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8377       timevar_pop (TV_SYMOUT);
8378     }
8379
8380   ext_block = NULL;
8381 }
8382
8383 #include "gt-c-decl.h"