OSDN Git Service

* c-common.c (handle_mode_attribute): Use %qE for identifiers in
[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             error ("field %qE has incomplete type", name);
5025             type = error_mark_node;
5026           }
5027         type = c_build_qualified_type (type, type_quals);
5028         decl = build_decl (FIELD_DECL, declarator->u.id, type);
5029         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5030         DECL_NONADDRESSABLE_P (decl) = bitfield;
5031         if (bitfield && !declarator->u.id)
5032           TREE_NO_WARNING (decl) = 1;
5033
5034         if (size_varies)
5035           C_DECL_VARIABLE_SIZE (decl) = 1;
5036       }
5037     else if (TREE_CODE (type) == FUNCTION_TYPE)
5038       {
5039         if (storage_class == csc_register || threadp)
5040           {
5041             error ("invalid storage class for function %qE", name);
5042            }
5043         else if (current_scope != file_scope)
5044           {
5045             /* Function declaration not at file scope.  Storage
5046                classes other than `extern' are not allowed, C99
5047                6.7.1p5, and `extern' makes no difference.  However,
5048                GCC allows 'auto', perhaps with 'inline', to support
5049                nested functions.  */
5050             if (storage_class == csc_auto)
5051                 pedwarn (input_location, OPT_pedantic,
5052                          "invalid storage class for function %qE", name);
5053             else if (storage_class == csc_static)
5054               {
5055                 error ("invalid storage class for function %qE", name);
5056                 if (funcdef_flag)
5057                   storage_class = declspecs->storage_class = csc_none;
5058                 else
5059                   return 0;
5060               }
5061           }
5062
5063         decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5064         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5065         decl = build_decl_attribute_variant (decl, decl_attr);
5066
5067         if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5068           pedwarn (input_location, OPT_pedantic,
5069                    "ISO C forbids qualified function types");
5070
5071         /* GNU C interprets a volatile-qualified function type to indicate
5072            that the function does not return.  */
5073         if ((type_quals & TYPE_QUAL_VOLATILE)
5074             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5075           warning (0, "%<noreturn%> function returns non-void value");
5076
5077         /* Every function declaration is an external reference
5078            (DECL_EXTERNAL) except for those which are not at file
5079            scope and are explicitly declared "auto".  This is
5080            forbidden by standard C (C99 6.7.1p5) and is interpreted by
5081            GCC to signify a forward declaration of a nested function.  */
5082         if (storage_class == csc_auto && current_scope != file_scope)
5083           DECL_EXTERNAL (decl) = 0;
5084         /* In C99, a function which is declared 'inline' with 'extern'
5085            is not an external reference (which is confusing).  It
5086            means that the later definition of the function must be output
5087            in this file, C99 6.7.4p6.  In GNU C89, a function declared
5088            'extern inline' is an external reference.  */
5089         else if (declspecs->inline_p && storage_class != csc_static)
5090           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5091                                   == flag_gnu89_inline);
5092         else
5093           DECL_EXTERNAL (decl) = !initialized;
5094
5095         /* Record absence of global scope for `static' or `auto'.  */
5096         TREE_PUBLIC (decl)
5097           = !(storage_class == csc_static || storage_class == csc_auto);
5098
5099         /* For a function definition, record the argument information
5100            block where store_parm_decls will look for it.  */
5101         if (funcdef_flag)
5102           current_function_arg_info = arg_info;
5103
5104         if (declspecs->default_int_p)
5105           C_FUNCTION_IMPLICIT_INT (decl) = 1;
5106
5107         /* Record presence of `inline', if it is reasonable.  */
5108         if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5109           {
5110             if (declspecs->inline_p)
5111               pedwarn (input_location, 0, "cannot inline function %<main%>");
5112           }
5113         else if (declspecs->inline_p)
5114           /* Record that the function is declared `inline'.  */
5115           DECL_DECLARED_INLINE_P (decl) = 1;
5116       }
5117     else
5118       {
5119         /* It's a variable.  */
5120         /* An uninitialized decl with `extern' is a reference.  */
5121         int extern_ref = !initialized && storage_class == csc_extern;
5122
5123         type = c_build_qualified_type (type, type_quals);
5124
5125         /* C99 6.2.2p7: It is invalid (compile-time undefined
5126            behavior) to create an 'extern' declaration for a
5127            variable if there is a global declaration that is
5128            'static' and the global declaration is not visible.
5129            (If the static declaration _is_ currently visible,
5130            the 'extern' declaration is taken to refer to that decl.) */
5131         if (extern_ref && current_scope != file_scope)
5132           {
5133             tree global_decl  = identifier_global_value (declarator->u.id);
5134             tree visible_decl = lookup_name (declarator->u.id);
5135
5136             if (global_decl
5137                 && global_decl != visible_decl
5138                 && TREE_CODE (global_decl) == VAR_DECL
5139                 && !TREE_PUBLIC (global_decl))
5140               error ("variable previously declared %<static%> redeclared "
5141                      "%<extern%>");
5142           }
5143
5144         decl = build_decl (VAR_DECL, declarator->u.id, type);
5145         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5146         if (size_varies)
5147           C_DECL_VARIABLE_SIZE (decl) = 1;
5148
5149         if (declspecs->inline_p)
5150           pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
5151
5152         /* At file scope, an initialized extern declaration may follow
5153            a static declaration.  In that case, DECL_EXTERNAL will be
5154            reset later in start_decl.  */
5155         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5156
5157         /* At file scope, the presence of a `static' or `register' storage
5158            class specifier, or the absence of all storage class specifiers
5159            makes this declaration a definition (perhaps tentative).  Also,
5160            the absence of `static' makes it public.  */
5161         if (current_scope == file_scope)
5162           {
5163             TREE_PUBLIC (decl) = storage_class != csc_static;
5164             TREE_STATIC (decl) = !extern_ref;
5165           }
5166         /* Not at file scope, only `static' makes a static definition.  */
5167         else
5168           {
5169             TREE_STATIC (decl) = (storage_class == csc_static);
5170             TREE_PUBLIC (decl) = extern_ref;
5171           }
5172
5173         if (threadp)
5174           DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5175       }
5176
5177     if ((storage_class == csc_extern
5178          || (storage_class == csc_none
5179              && TREE_CODE (type) == FUNCTION_TYPE
5180              && !funcdef_flag))
5181         && variably_modified_type_p (type, NULL_TREE))
5182       {
5183         /* C99 6.7.5.2p2 */
5184         if (TREE_CODE (type) == FUNCTION_TYPE)
5185           error ("non-nested function with variably modified type");
5186         else
5187           error ("object with variably modified type must have no linkage");
5188       }
5189
5190     /* Record `register' declaration for warnings on &
5191        and in case doing stupid register allocation.  */
5192
5193     if (storage_class == csc_register)
5194       {
5195         C_DECL_REGISTER (decl) = 1;
5196         DECL_REGISTER (decl) = 1;
5197       }
5198
5199     /* Record constancy and volatility.  */
5200     c_apply_type_quals_to_decl (type_quals, decl);
5201
5202     /* If a type has volatile components, it should be stored in memory.
5203        Otherwise, the fact that those components are volatile
5204        will be ignored, and would even crash the compiler.
5205        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
5206     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5207         && (TREE_CODE (decl) == VAR_DECL ||  TREE_CODE (decl) == PARM_DECL
5208           || TREE_CODE (decl) == RESULT_DECL))
5209       {
5210         /* It is not an error for a structure with volatile fields to
5211            be declared register, but reset DECL_REGISTER since it
5212            cannot actually go in a register.  */
5213         int was_reg = C_DECL_REGISTER (decl);
5214         C_DECL_REGISTER (decl) = 0;
5215         DECL_REGISTER (decl) = 0;
5216         c_mark_addressable (decl);
5217         C_DECL_REGISTER (decl) = was_reg;
5218       }
5219
5220   /* This is the earliest point at which we might know the assembler
5221      name of a variable.  Thus, if it's known before this, die horribly.  */
5222     gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5223
5224     return decl;
5225   }
5226 }
5227 \f
5228 /* Decode the parameter-list info for a function type or function definition.
5229    The argument is the value returned by `get_parm_info' (or made in c-parse.c
5230    if there is an identifier list instead of a parameter decl list).
5231    These two functions are separate because when a function returns
5232    or receives functions then each is called multiple times but the order
5233    of calls is different.  The last call to `grokparms' is always the one
5234    that contains the formal parameter names of a function definition.
5235
5236    Return a list of arg types to use in the FUNCTION_TYPE for this function.
5237
5238    FUNCDEF_FLAG is true for a function definition, false for
5239    a mere declaration.  A nonempty identifier-list gets an error message
5240    when FUNCDEF_FLAG is false.  */
5241
5242 static tree
5243 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5244 {
5245   tree arg_types = arg_info->types;
5246
5247   if (funcdef_flag && arg_info->had_vla_unspec)
5248     {
5249       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
5250       /* C99 6.7.5.2p4 */
5251       error ("%<[*]%> not allowed in other than function prototype scope");
5252     }
5253
5254   if (arg_types == 0 && !funcdef_flag && !in_system_header)
5255     warning (OPT_Wstrict_prototypes,
5256              "function declaration isn%'t a prototype");
5257
5258   if (arg_types == error_mark_node)
5259     return 0;  /* don't set TYPE_ARG_TYPES in this case */
5260
5261   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5262     {
5263       if (!funcdef_flag)
5264         pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5265
5266       arg_info->parms = arg_info->types;
5267       arg_info->types = 0;
5268       return 0;
5269     }
5270   else
5271     {
5272       tree parm, type, typelt;
5273       unsigned int parmno;
5274
5275       /* If there is a parameter of incomplete type in a definition,
5276          this is an error.  In a declaration this is valid, and a
5277          struct or union type may be completed later, before any calls
5278          or definition of the function.  In the case where the tag was
5279          first declared within the parameter list, a warning has
5280          already been given.  If a parameter has void type, then
5281          however the function cannot be defined or called, so
5282          warn.  */
5283
5284       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5285            parm;
5286            parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5287         {
5288           type = TREE_VALUE (typelt);
5289           if (type == error_mark_node)
5290             continue;
5291
5292           if (!COMPLETE_TYPE_P (type))
5293             {
5294               if (funcdef_flag)
5295                 {
5296                   if (DECL_NAME (parm))
5297                     error ("parameter %u (%q+D) has incomplete type",
5298                            parmno, parm);
5299                   else
5300                     error ("%Jparameter %u has incomplete type",
5301                            parm, parmno);
5302
5303                   TREE_VALUE (typelt) = error_mark_node;
5304                   TREE_TYPE (parm) = error_mark_node;
5305                 }
5306               else if (VOID_TYPE_P (type))
5307                 {
5308                   if (DECL_NAME (parm))
5309                     warning (0, "parameter %u (%q+D) has void type",
5310                              parmno, parm);
5311                   else
5312                     warning (0, "%Jparameter %u has void type",
5313                              parm, parmno);
5314                 }
5315             }
5316
5317           if (DECL_NAME (parm) && TREE_USED (parm))
5318             warn_if_shadowing (parm);
5319         }
5320       return arg_types;
5321     }
5322 }
5323
5324 /* Take apart the current scope and return a c_arg_info structure with
5325    info on a parameter list just parsed.
5326
5327    This structure is later fed to 'grokparms' and 'store_parm_decls'.
5328
5329    ELLIPSIS being true means the argument list ended in '...' so don't
5330    append a sentinel (void_list_node) to the end of the type-list.  */
5331
5332 struct c_arg_info *
5333 get_parm_info (bool ellipsis)
5334 {
5335   struct c_binding *b = current_scope->bindings;
5336   struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5337                                         struct c_arg_info);
5338   tree parms    = 0;
5339   tree tags     = 0;
5340   tree types    = 0;
5341   tree others   = 0;
5342
5343   static bool explained_incomplete_types = false;
5344   bool gave_void_only_once_err = false;
5345
5346   arg_info->parms = 0;
5347   arg_info->tags = 0;
5348   arg_info->types = 0;
5349   arg_info->others = 0;
5350   arg_info->pending_sizes = 0;
5351   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5352
5353   /* The bindings in this scope must not get put into a block.
5354      We will take care of deleting the binding nodes.  */
5355   current_scope->bindings = 0;
5356
5357   /* This function is only called if there was *something* on the
5358      parameter list.  */
5359   gcc_assert (b);
5360
5361   /* A parameter list consisting solely of 'void' indicates that the
5362      function takes no arguments.  But if the 'void' is qualified
5363      (by 'const' or 'volatile'), or has a storage class specifier
5364      ('register'), then the behavior is undefined; issue an error.
5365      Typedefs for 'void' are OK (see DR#157).  */
5366   if (b->prev == 0                          /* one binding */
5367       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
5368       && !DECL_NAME (b->decl)               /* anonymous */
5369       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5370     {
5371       if (TREE_THIS_VOLATILE (b->decl)
5372           || TREE_READONLY (b->decl)
5373           || C_DECL_REGISTER (b->decl))
5374         error ("%<void%> as only parameter may not be qualified");
5375
5376       /* There cannot be an ellipsis.  */
5377       if (ellipsis)
5378         error ("%<void%> must be the only parameter");
5379
5380       arg_info->types = void_list_node;
5381       return arg_info;
5382     }
5383
5384   if (!ellipsis)
5385     types = void_list_node;
5386
5387   /* Break up the bindings list into parms, tags, types, and others;
5388      apply sanity checks; purge the name-to-decl bindings.  */
5389   while (b)
5390     {
5391       tree decl = b->decl;
5392       tree type = TREE_TYPE (decl);
5393       const char *keyword;
5394
5395       switch (TREE_CODE (decl))
5396         {
5397         case PARM_DECL:
5398           if (b->id)
5399             {
5400               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5401               I_SYMBOL_BINDING (b->id) = b->shadowed;
5402             }
5403
5404           /* Check for forward decls that never got their actual decl.  */
5405           if (TREE_ASM_WRITTEN (decl))
5406             error ("parameter %q+D has just a forward declaration", decl);
5407           /* Check for (..., void, ...) and issue an error.  */
5408           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5409             {
5410               if (!gave_void_only_once_err)
5411                 {
5412                   error ("%<void%> must be the only parameter");
5413                   gave_void_only_once_err = true;
5414                 }
5415             }
5416           else
5417             {
5418               /* Valid parameter, add it to the list.  */
5419               TREE_CHAIN (decl) = parms;
5420               parms = decl;
5421
5422               /* Since there is a prototype, args are passed in their
5423                  declared types.  The back end may override this later.  */
5424               DECL_ARG_TYPE (decl) = type;
5425               types = tree_cons (0, type, types);
5426             }
5427           break;
5428
5429         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5430         case UNION_TYPE:    keyword = "union"; goto tag;
5431         case RECORD_TYPE:   keyword = "struct"; goto tag;
5432         tag:
5433           /* Types may not have tag-names, in which case the type
5434              appears in the bindings list with b->id NULL.  */
5435           if (b->id)
5436             {
5437               gcc_assert (I_TAG_BINDING (b->id) == b);
5438               I_TAG_BINDING (b->id) = b->shadowed;
5439             }
5440
5441           /* Warn about any struct, union or enum tags defined in a
5442              parameter list.  The scope of such types is limited to
5443              the parameter list, which is rarely if ever desirable
5444              (it's impossible to call such a function with type-
5445              correct arguments).  An anonymous union parm type is
5446              meaningful as a GNU extension, so don't warn for that.  */
5447           if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5448             {
5449               if (b->id)
5450                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5451                 warning (0, "%<%s %E%> declared inside parameter list",
5452                          keyword, b->id);
5453               else
5454                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
5455                 warning (0, "anonymous %s declared inside parameter list",
5456                          keyword);
5457
5458               if (!explained_incomplete_types)
5459                 {
5460                   warning (0, "its scope is only this definition or declaration,"
5461                            " which is probably not what you want");
5462                   explained_incomplete_types = true;
5463                 }
5464             }
5465
5466           tags = tree_cons (b->id, decl, tags);
5467           break;
5468
5469         case CONST_DECL:
5470         case TYPE_DECL:
5471         case FUNCTION_DECL:
5472           /* CONST_DECLs appear here when we have an embedded enum,
5473              and TYPE_DECLs appear here when we have an embedded struct
5474              or union.  No warnings for this - we already warned about the
5475              type itself.  FUNCTION_DECLs appear when there is an implicit
5476              function declaration in the parameter list.  */
5477
5478           TREE_CHAIN (decl) = others;
5479           others = decl;
5480           /* fall through */
5481
5482         case ERROR_MARK:
5483           /* error_mark_node appears here when we have an undeclared
5484              variable.  Just throw it away.  */
5485           if (b->id)
5486             {
5487               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5488               I_SYMBOL_BINDING (b->id) = b->shadowed;
5489             }
5490           break;
5491
5492           /* Other things that might be encountered.  */
5493         case LABEL_DECL:
5494         case VAR_DECL:
5495         default:
5496           gcc_unreachable ();
5497         }
5498
5499       b = free_binding_and_advance (b);
5500     }
5501
5502   arg_info->parms = parms;
5503   arg_info->tags = tags;
5504   arg_info->types = types;
5505   arg_info->others = others;
5506   arg_info->pending_sizes = get_pending_sizes ();
5507   return arg_info;
5508 }
5509 \f
5510 /* Get the struct, enum or union (CODE says which) with tag NAME.
5511    Define the tag as a forward-reference if it is not defined.
5512    Return a c_typespec structure for the type specifier.  */
5513
5514 struct c_typespec
5515 parser_xref_tag (enum tree_code code, tree name)
5516 {
5517   struct c_typespec ret;
5518   tree ref;
5519
5520   ret.expr = NULL_TREE;
5521   ret.expr_const_operands = true;
5522
5523   /* If a cross reference is requested, look up the type
5524      already defined for this tag and return it.  */
5525
5526   ref = lookup_tag (code, name, 0);
5527   /* If this is the right type of tag, return what we found.
5528      (This reference will be shadowed by shadow_tag later if appropriate.)
5529      If this is the wrong type of tag, do not return it.  If it was the
5530      wrong type in the same scope, we will have had an error
5531      message already; if in a different scope and declaring
5532      a name, pending_xref_error will give an error message; but if in a
5533      different scope and not declaring a name, this tag should
5534      shadow the previous declaration of a different type of tag, and
5535      this would not work properly if we return the reference found.
5536      (For example, with "struct foo" in an outer scope, "union foo;"
5537      must shadow that tag with a new one of union type.)  */
5538   ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5539   if (ref && TREE_CODE (ref) == code)
5540     {
5541       ret.spec = ref;
5542       return ret;
5543     }
5544
5545   /* If no such tag is yet defined, create a forward-reference node
5546      and record it as the "definition".
5547      When a real declaration of this type is found,
5548      the forward-reference will be altered into a real type.  */
5549
5550   ref = make_node (code);
5551   if (code == ENUMERAL_TYPE)
5552     {
5553       /* Give the type a default layout like unsigned int
5554          to avoid crashing if it does not get defined.  */
5555       SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5556       TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5557       TYPE_USER_ALIGN (ref) = 0;
5558       TYPE_UNSIGNED (ref) = 1;
5559       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5560       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5561       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5562     }
5563
5564   pushtag (name, ref);
5565
5566   ret.spec = ref;
5567   return ret;
5568 }
5569
5570 /* Get the struct, enum or union (CODE says which) with tag NAME.
5571    Define the tag as a forward-reference if it is not defined.
5572    Return a tree for the type.  */
5573
5574 tree
5575 xref_tag (enum tree_code code, tree name)
5576 {
5577   return parser_xref_tag (code, name).spec;
5578 }
5579 \f
5580 /* Make sure that the tag NAME is defined *in the current scope*
5581    at least as a forward reference.
5582    CODE says which kind of tag NAME ought to be.  */
5583
5584 tree
5585 start_struct (enum tree_code code, tree name)
5586 {
5587   /* If there is already a tag defined at this scope
5588      (as a forward reference), just return it.  */
5589
5590   tree ref = 0;
5591
5592   if (name != 0)
5593     ref = lookup_tag (code, name, 1);
5594   if (ref && TREE_CODE (ref) == code)
5595     {
5596       if (TYPE_SIZE (ref))
5597         {
5598           if (code == UNION_TYPE)
5599             error ("redefinition of %<union %E%>", name);
5600           else
5601             error ("redefinition of %<struct %E%>", name);
5602           /* Don't create structures using a name already in use.  */
5603           ref = NULL_TREE;
5604         }
5605       else if (C_TYPE_BEING_DEFINED (ref))
5606         {
5607           if (code == UNION_TYPE)
5608             error ("nested redefinition of %<union %E%>", name);
5609           else
5610             error ("nested redefinition of %<struct %E%>", name);
5611           /* Don't create structures that contain themselves.  */
5612           ref = NULL_TREE;
5613         }
5614     }
5615
5616   /* Otherwise create a forward-reference just so the tag is in scope.  */
5617
5618   if (ref == NULL_TREE || TREE_CODE (ref) != code)
5619     {
5620       ref = make_node (code);
5621       pushtag (name, ref);
5622     }
5623
5624   C_TYPE_BEING_DEFINED (ref) = 1;
5625   TYPE_PACKED (ref) = flag_pack_struct;
5626   return ref;
5627 }
5628
5629 /* Process the specs, declarator and width (NULL if omitted)
5630    of a structure component, returning a FIELD_DECL node.
5631    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5632    DECL_ATTRS is as for grokdeclarator.
5633
5634    LOC is the location of the structure component.
5635
5636    This is done during the parsing of the struct declaration.
5637    The FIELD_DECL nodes are chained together and the lot of them
5638    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5639
5640 tree
5641 grokfield (location_t loc,
5642            struct c_declarator *declarator, struct c_declspecs *declspecs,
5643            tree width, tree *decl_attrs)
5644 {
5645   tree value;
5646
5647   if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5648       && width == NULL_TREE)
5649     {
5650       /* This is an unnamed decl.
5651
5652          If we have something of the form "union { list } ;" then this
5653          is the anonymous union extension.  Similarly for struct.
5654
5655          If this is something of the form "struct foo;", then
5656            If MS extensions are enabled, this is handled as an
5657              anonymous struct.
5658            Otherwise this is a forward declaration of a structure tag.
5659
5660          If this is something of the form "foo;" and foo is a TYPE_DECL, then
5661            If MS extensions are enabled and foo names a structure, then
5662              again this is an anonymous struct.
5663            Otherwise this is an error.
5664
5665          Oh what a horrid tangled web we weave.  I wonder if MS consciously
5666          took this from Plan 9 or if it was an accident of implementation
5667          that took root before someone noticed the bug...  */
5668
5669       tree type = declspecs->type;
5670       bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5671                       || TREE_CODE (type) == UNION_TYPE);
5672       bool ok = false;
5673
5674       if (type_ok
5675           && (flag_ms_extensions || !declspecs->typedef_p))
5676         {
5677           if (flag_ms_extensions)
5678             ok = true;
5679           else if (flag_iso)
5680             ok = false;
5681           else if (TYPE_NAME (type) == NULL)
5682             ok = true;
5683           else
5684             ok = false;
5685         }
5686       if (!ok)
5687         {
5688           pedwarn (loc, 0, "declaration does not declare anything");
5689           return NULL_TREE;
5690         }
5691       pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5692     }
5693
5694   value = grokdeclarator (declarator, declspecs, FIELD, false,
5695                           width ? &width : NULL, decl_attrs, NULL, NULL,
5696                           DEPRECATED_NORMAL);
5697
5698   finish_decl (value, NULL_TREE, NULL_TREE, NULL_TREE);
5699   DECL_INITIAL (value) = width;
5700
5701   return value;
5702 }
5703 \f
5704 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
5705    the list such that this does not present a problem later.  */
5706
5707 static void
5708 detect_field_duplicates (tree fieldlist)
5709 {
5710   tree x, y;
5711   int timeout = 10;
5712
5713   /* First, see if there are more than "a few" fields.
5714      This is trivially true if there are zero or one fields.  */
5715   if (!fieldlist)
5716     return;
5717   x = TREE_CHAIN (fieldlist);
5718   if (!x)
5719     return;
5720   do {
5721     timeout--;
5722     x = TREE_CHAIN (x);
5723   } while (timeout > 0 && x);
5724
5725   /* If there were "few" fields, avoid the overhead of allocating
5726      a hash table.  Instead just do the nested traversal thing.  */
5727   if (timeout > 0)
5728     {
5729       for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5730         if (DECL_NAME (x))
5731           {
5732             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5733               if (DECL_NAME (y) == DECL_NAME (x))
5734                 {
5735                   error ("duplicate member %q+D", x);
5736                   DECL_NAME (x) = NULL_TREE;
5737                 }
5738           }
5739     }
5740   else
5741     {
5742       htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5743       void **slot;
5744
5745       for (x = fieldlist; x ; x = TREE_CHAIN (x))
5746         if ((y = DECL_NAME (x)) != 0)
5747           {
5748             slot = htab_find_slot (htab, y, INSERT);
5749             if (*slot)
5750               {
5751                 error ("duplicate member %q+D", x);
5752                 DECL_NAME (x) = NULL_TREE;
5753               }
5754             *slot = y;
5755           }
5756
5757       htab_delete (htab);
5758     }
5759 }
5760
5761 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5762    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5763    ATTRIBUTES are attributes to be applied to the structure.  */
5764
5765 tree
5766 finish_struct (tree t, tree fieldlist, tree attributes)
5767 {
5768   tree x;
5769   bool toplevel = file_scope == current_scope;
5770   int saw_named_field;
5771
5772   /* If this type was previously laid out as a forward reference,
5773      make sure we lay it out again.  */
5774
5775   TYPE_SIZE (t) = 0;
5776
5777   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5778
5779   if (pedantic)
5780     {
5781       for (x = fieldlist; x; x = TREE_CHAIN (x))
5782         if (DECL_NAME (x) != 0)
5783           break;
5784
5785       if (x == 0)
5786         {
5787           if (TREE_CODE (t) == UNION_TYPE)
5788             {
5789               if (fieldlist)
5790                 pedwarn (input_location, OPT_pedantic, "union has no named members");
5791               else
5792                 pedwarn (input_location, OPT_pedantic, "union has no members");
5793             }
5794           else
5795             {
5796               if (fieldlist)
5797                 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5798               else
5799                 pedwarn (input_location, OPT_pedantic, "struct has no members");
5800             }
5801         }
5802     }
5803
5804   /* Install struct as DECL_CONTEXT of each field decl.
5805      Also process specified field sizes, found in the DECL_INITIAL,
5806      storing 0 there after the type has been changed to precision equal
5807      to its width, rather than the precision of the specified standard
5808      type.  (Correct layout requires the original type to have been preserved
5809      until now.)  */
5810
5811   saw_named_field = 0;
5812   for (x = fieldlist; x; x = TREE_CHAIN (x))
5813     {
5814       if (TREE_TYPE (x) == error_mark_node)
5815         continue;
5816
5817       DECL_CONTEXT (x) = t;
5818
5819       /* If any field is const, the structure type is pseudo-const.  */
5820       if (TREE_READONLY (x))
5821         C_TYPE_FIELDS_READONLY (t) = 1;
5822       else
5823         {
5824           /* A field that is pseudo-const makes the structure likewise.  */
5825           tree t1 = TREE_TYPE (x);
5826           while (TREE_CODE (t1) == ARRAY_TYPE)
5827             t1 = TREE_TYPE (t1);
5828           if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5829               && C_TYPE_FIELDS_READONLY (t1))
5830             C_TYPE_FIELDS_READONLY (t) = 1;
5831         }
5832
5833       /* Any field that is volatile means variables of this type must be
5834          treated in some ways as volatile.  */
5835       if (TREE_THIS_VOLATILE (x))
5836         C_TYPE_FIELDS_VOLATILE (t) = 1;
5837
5838       /* Any field of nominal variable size implies structure is too.  */
5839       if (C_DECL_VARIABLE_SIZE (x))
5840         C_TYPE_VARIABLE_SIZE (t) = 1;
5841
5842       if (DECL_INITIAL (x))
5843         {
5844           unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5845           DECL_SIZE (x) = bitsize_int (width);
5846           DECL_BIT_FIELD (x) = 1;
5847           SET_DECL_C_BIT_FIELD (x);
5848         }
5849
5850       if (TYPE_PACKED (t)
5851           && (DECL_BIT_FIELD (x)
5852               || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5853         DECL_PACKED (x) = 1;
5854
5855       /* Detect flexible array member in an invalid context.  */
5856       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5857           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5858           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5859           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5860         {
5861           if (TREE_CODE (t) == UNION_TYPE)
5862             {
5863               error ("%Jflexible array member in union", x);
5864               TREE_TYPE (x) = error_mark_node;
5865             }
5866           else if (TREE_CHAIN (x) != NULL_TREE)
5867             {
5868               error ("%Jflexible array member not at end of struct", x);
5869               TREE_TYPE (x) = error_mark_node;
5870             }
5871           else if (!saw_named_field)
5872             {
5873               error ("%Jflexible array member in otherwise empty struct", x);
5874               TREE_TYPE (x) = error_mark_node;
5875             }
5876         }
5877
5878       if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5879           && flexible_array_type_p (TREE_TYPE (x)))
5880         pedwarn (input_location, OPT_pedantic, 
5881                  "%Jinvalid use of structure with flexible array member", x);
5882
5883       if (DECL_NAME (x))
5884         saw_named_field = 1;
5885     }
5886
5887   detect_field_duplicates (fieldlist);
5888
5889   /* Now we have the nearly final fieldlist.  Record it,
5890      then lay out the structure or union (including the fields).  */
5891
5892   TYPE_FIELDS (t) = fieldlist;
5893
5894   layout_type (t);
5895
5896   /* Give bit-fields their proper types.  */
5897   {
5898     tree *fieldlistp = &fieldlist;
5899     while (*fieldlistp)
5900       if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5901           && TREE_TYPE (*fieldlistp) != error_mark_node)
5902         {
5903           unsigned HOST_WIDE_INT width
5904             = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5905           tree type = TREE_TYPE (*fieldlistp);
5906           if (width != TYPE_PRECISION (type))
5907             {
5908               TREE_TYPE (*fieldlistp)
5909                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5910               DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5911             }
5912           DECL_INITIAL (*fieldlistp) = 0;
5913         }
5914       else
5915         fieldlistp = &TREE_CHAIN (*fieldlistp);
5916   }
5917
5918   /* Now we have the truly final field list.
5919      Store it in this type and in the variants.  */
5920
5921   TYPE_FIELDS (t) = fieldlist;
5922
5923   /* If there are lots of fields, sort so we can look through them fast.
5924      We arbitrarily consider 16 or more elts to be "a lot".  */
5925
5926   {
5927     int len = 0;
5928
5929     for (x = fieldlist; x; x = TREE_CHAIN (x))
5930       {
5931         if (len > 15 || DECL_NAME (x) == NULL)
5932           break;
5933         len += 1;
5934       }
5935
5936     if (len > 15)
5937       {
5938         tree *field_array;
5939         struct lang_type *space;
5940         struct sorted_fields_type *space2;
5941
5942         len += list_length (x);
5943
5944         /* Use the same allocation policy here that make_node uses, to
5945           ensure that this lives as long as the rest of the struct decl.
5946           All decls in an inline function need to be saved.  */
5947
5948         space = GGC_CNEW (struct lang_type);
5949         space2 = GGC_NEWVAR (struct sorted_fields_type,
5950                              sizeof (struct sorted_fields_type) + len * sizeof (tree));
5951
5952         len = 0;
5953         space->s = space2;
5954         field_array = &space2->elts[0];
5955         for (x = fieldlist; x; x = TREE_CHAIN (x))
5956           {
5957             field_array[len++] = x;
5958
5959             /* If there is anonymous struct or union, break out of the loop.  */
5960             if (DECL_NAME (x) == NULL)
5961               break;
5962           }
5963         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5964         if (x == NULL)
5965           {
5966             TYPE_LANG_SPECIFIC (t) = space;
5967             TYPE_LANG_SPECIFIC (t)->s->len = len;
5968             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5969             qsort (field_array, len, sizeof (tree), field_decl_cmp);
5970           }
5971       }
5972   }
5973
5974   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5975     {
5976       TYPE_FIELDS (x) = TYPE_FIELDS (t);
5977       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5978       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5979       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5980       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5981     }
5982
5983   /* If this was supposed to be a transparent union, but we can't
5984      make it one, warn and turn off the flag.  */
5985   if (TREE_CODE (t) == UNION_TYPE
5986       && TYPE_TRANSPARENT_UNION (t)
5987       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5988     {
5989       TYPE_TRANSPARENT_UNION (t) = 0;
5990       warning (0, "union cannot be made transparent");
5991     }
5992
5993   /* If this structure or union completes the type of any previous
5994      variable declaration, lay it out and output its rtl.  */
5995   for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5996        x;
5997        x = TREE_CHAIN (x))
5998     {
5999       tree decl = TREE_VALUE (x);
6000       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6001         layout_array_type (TREE_TYPE (decl));
6002       if (TREE_CODE (decl) != TYPE_DECL)
6003         {
6004           layout_decl (decl, 0);
6005           if (c_dialect_objc ())
6006             objc_check_decl (decl);
6007           rest_of_decl_compilation (decl, toplevel, 0);
6008           if (!toplevel)
6009             expand_decl (decl);
6010         }
6011     }
6012   C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
6013
6014   /* Finish debugging output for this type.  */
6015   rest_of_type_compilation (t, toplevel);
6016
6017   /* If we're inside a function proper, i.e. not file-scope and not still
6018      parsing parameters, then arrange for the size of a variable sized type
6019      to be bound now.  */
6020   if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
6021     add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
6022
6023   return t;
6024 }
6025
6026 /* Lay out the type T, and its element type, and so on.  */
6027
6028 static void
6029 layout_array_type (tree t)
6030 {
6031   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6032     layout_array_type (TREE_TYPE (t));
6033   layout_type (t);
6034 }
6035 \f
6036 /* Begin compiling the definition of an enumeration type.
6037    NAME is its name (or null if anonymous).
6038    Returns the type object, as yet incomplete.
6039    Also records info about it so that build_enumerator
6040    may be used to declare the individual values as they are read.  */
6041
6042 tree
6043 start_enum (struct c_enum_contents *the_enum, tree name)
6044 {
6045   tree enumtype = 0;
6046
6047   /* If this is the real definition for a previous forward reference,
6048      fill in the contents in the same object that used to be the
6049      forward reference.  */
6050
6051   if (name != 0)
6052     enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
6053
6054   if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
6055     {
6056       enumtype = make_node (ENUMERAL_TYPE);
6057       pushtag (name, enumtype);
6058     }
6059
6060   if (C_TYPE_BEING_DEFINED (enumtype))
6061     error ("nested redefinition of %<enum %E%>", name);
6062
6063   C_TYPE_BEING_DEFINED (enumtype) = 1;
6064
6065   if (TYPE_VALUES (enumtype) != 0)
6066     {
6067       /* This enum is a named one that has been declared already.  */
6068       error ("redeclaration of %<enum %E%>", name);
6069
6070       /* Completely replace its old definition.
6071          The old enumerators remain defined, however.  */
6072       TYPE_VALUES (enumtype) = 0;
6073     }
6074
6075   the_enum->enum_next_value = integer_zero_node;
6076   the_enum->enum_overflow = 0;
6077
6078   if (flag_short_enums)
6079     TYPE_PACKED (enumtype) = 1;
6080
6081   return enumtype;
6082 }
6083
6084 /* After processing and defining all the values of an enumeration type,
6085    install their decls in the enumeration type and finish it off.
6086    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6087    and ATTRIBUTES are the specified attributes.
6088    Returns ENUMTYPE.  */
6089
6090 tree
6091 finish_enum (tree enumtype, tree values, tree attributes)
6092 {
6093   tree pair, tem;
6094   tree minnode = 0, maxnode = 0;
6095   int precision, unsign;
6096   bool toplevel = (file_scope == current_scope);
6097   struct lang_type *lt;
6098
6099   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6100
6101   /* Calculate the maximum value of any enumerator in this type.  */
6102
6103   if (values == error_mark_node)
6104     minnode = maxnode = integer_zero_node;
6105   else
6106     {
6107       minnode = maxnode = TREE_VALUE (values);
6108       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6109         {
6110           tree value = TREE_VALUE (pair);
6111           if (tree_int_cst_lt (maxnode, value))
6112             maxnode = value;
6113           if (tree_int_cst_lt (value, minnode))
6114             minnode = value;
6115         }
6116     }
6117
6118   /* Construct the final type of this enumeration.  It is the same
6119      as one of the integral types - the narrowest one that fits, except
6120      that normally we only go as narrow as int - and signed iff any of
6121      the values are negative.  */
6122   unsign = (tree_int_cst_sgn (minnode) >= 0);
6123   precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6124                    tree_int_cst_min_precision (maxnode, unsign));
6125
6126   if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6127     {
6128       tem = c_common_type_for_size (precision, unsign);
6129       if (tem == NULL)
6130         {
6131           warning (0, "enumeration values exceed range of largest integer");
6132           tem = long_long_integer_type_node;
6133         }
6134     }
6135   else
6136     tem = unsign ? unsigned_type_node : integer_type_node;
6137
6138   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6139   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6140   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6141   TYPE_SIZE (enumtype) = 0;
6142
6143   /* If the precision of the type was specific with an attribute and it
6144      was too small, give an error.  Otherwise, use it.  */
6145   if (TYPE_PRECISION (enumtype))
6146     {
6147       if (precision > TYPE_PRECISION (enumtype))
6148         error ("specified mode too small for enumeral values");
6149     }
6150   else
6151     TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6152
6153   layout_type (enumtype);
6154
6155   if (values != error_mark_node)
6156     {
6157       /* Change the type of the enumerators to be the enum type.  We
6158          need to do this irrespective of the size of the enum, for
6159          proper type checking.  Replace the DECL_INITIALs of the
6160          enumerators, and the value slots of the list, with copies
6161          that have the enum type; they cannot be modified in place
6162          because they may be shared (e.g.  integer_zero_node) Finally,
6163          change the purpose slots to point to the names of the decls.  */
6164       for (pair = values; pair; pair = TREE_CHAIN (pair))
6165         {
6166           tree enu = TREE_PURPOSE (pair);
6167           tree ini = DECL_INITIAL (enu);
6168
6169           TREE_TYPE (enu) = enumtype;
6170
6171           /* The ISO C Standard mandates enumerators to have type int,
6172              even though the underlying type of an enum type is
6173              unspecified.  However, GCC allows enumerators of any
6174              integer type as an extensions.  build_enumerator()
6175              converts any enumerators that fit in an int to type int,
6176              to avoid promotions to unsigned types when comparing
6177              integers with enumerators that fit in the int range.
6178              When -pedantic is given, build_enumerator() would have
6179              already warned about those that don't fit. Here we
6180              convert the rest to the enumerator type. */
6181           if (TREE_TYPE (ini) != integer_type_node)
6182             ini = convert (enumtype, ini);
6183
6184           DECL_INITIAL (enu) = ini;
6185           TREE_PURPOSE (pair) = DECL_NAME (enu);
6186           TREE_VALUE (pair) = ini;
6187         }
6188
6189       TYPE_VALUES (enumtype) = values;
6190     }
6191
6192   /* Record the min/max values so that we can warn about bit-field
6193      enumerations that are too small for the values.  */
6194   lt = GGC_CNEW (struct lang_type);
6195   lt->enum_min = minnode;
6196   lt->enum_max = maxnode;
6197   TYPE_LANG_SPECIFIC (enumtype) = lt;
6198
6199   /* Fix up all variant types of this enum type.  */
6200   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6201     {
6202       if (tem == enumtype)
6203         continue;
6204       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6205       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6206       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6207       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6208       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6209       SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6210       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6211       TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6212       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6213       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6214       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6215     }
6216
6217   /* Finish debugging output for this type.  */
6218   rest_of_type_compilation (enumtype, toplevel);
6219
6220   return enumtype;
6221 }
6222
6223 /* Build and install a CONST_DECL for one value of the
6224    current enumeration type (one that was begun with start_enum).
6225    Return a tree-list containing the CONST_DECL and its value.
6226    Assignment of sequential values by default is handled here.  */
6227
6228 tree
6229 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6230                   location_t value_loc)
6231 {
6232   tree decl, type;
6233
6234   /* Validate and default VALUE.  */
6235
6236   if (value != 0)
6237     {
6238       /* Don't issue more errors for error_mark_node (i.e. an
6239          undeclared identifier) - just ignore the value expression.  */
6240       if (value == error_mark_node)
6241         value = 0;
6242       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6243         {
6244           error ("enumerator value for %qE is not an integer constant", name);
6245           value = 0;
6246         }
6247       else
6248         {
6249           if (TREE_CODE (value) != INTEGER_CST)
6250             {
6251               value = c_fully_fold (value, false, NULL);
6252               if (TREE_CODE (value) == INTEGER_CST)
6253                 pedwarn (value_loc, OPT_pedantic,
6254                          "enumerator value for %qE is not an integer "
6255                          "constant expression", name);
6256             }
6257           if (TREE_CODE (value) != INTEGER_CST)
6258             {
6259               error ("enumerator value for %qE is not an integer constant",
6260                      name);
6261               value = 0;
6262             }
6263           else
6264             {
6265               value = default_conversion (value);
6266               constant_expression_warning (value);
6267             }
6268         }
6269     }
6270
6271   /* Default based on previous value.  */
6272   /* It should no longer be possible to have NON_LVALUE_EXPR
6273      in the default.  */
6274   if (value == 0)
6275     {
6276       value = the_enum->enum_next_value;
6277       if (the_enum->enum_overflow)
6278         error ("overflow in enumeration values");
6279     }
6280   /* Even though the underlying type of an enum is unspecified, the
6281      type of enumeration constants is explicitly defined as int
6282      (6.4.4.3/2 in the C99 Standard).  GCC allows any integer type as
6283      an extension.  */
6284   else if (!int_fits_type_p (value, integer_type_node))
6285     pedwarn (value_loc, OPT_pedantic, 
6286              "ISO C restricts enumerator values to range of %<int%>");
6287
6288   /* The ISO C Standard mandates enumerators to have type int, even
6289      though the underlying type of an enum type is unspecified.
6290      However, GCC allows enumerators of any integer type as an
6291      extensions.  Here we convert any enumerators that fit in an int
6292      to type int, to avoid promotions to unsigned types when comparing
6293      integers with enumerators that fit in the int range.  When
6294      -pedantic is given, we would have already warned about those that
6295      don't fit. We have to do this here rather than in finish_enum
6296      because this value may be used to define more enumerators.  */
6297   if (int_fits_type_p (value, integer_type_node))
6298     value = convert (integer_type_node, value);
6299
6300   /* Set basis for default for next value.  */
6301   the_enum->enum_next_value
6302     = build_binary_op
6303          (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6304          PLUS_EXPR, value, integer_one_node, 0);
6305   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6306
6307   /* Now create a declaration for the enum value name.  */
6308
6309   type = TREE_TYPE (value);
6310   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6311                                       TYPE_PRECISION (integer_type_node)),
6312                                  (TYPE_PRECISION (type)
6313                                   >= TYPE_PRECISION (integer_type_node)
6314                                   && TYPE_UNSIGNED (type)));
6315
6316   decl = build_decl (CONST_DECL, name, type);
6317   DECL_INITIAL (decl) = convert (type, value);
6318   pushdecl (decl);
6319
6320   return tree_cons (decl, value, NULL_TREE);
6321 }
6322
6323 \f
6324 /* Create the FUNCTION_DECL for a function definition.
6325    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6326    the declaration; they describe the function's name and the type it returns,
6327    but twisted together in a fashion that parallels the syntax of C.
6328
6329    This function creates a binding context for the function body
6330    as well as setting up the FUNCTION_DECL in current_function_decl.
6331
6332    Returns 1 on success.  If the DECLARATOR is not suitable for a function
6333    (it defines a datum instead), we return 0, which tells
6334    yyparse to report a parse error.  */
6335
6336 int
6337 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6338                 tree attributes)
6339 {
6340   tree decl1, old_decl;
6341   tree restype, resdecl;
6342   struct c_label_context_se *nstack_se;
6343   struct c_label_context_vm *nstack_vm;
6344
6345   current_function_returns_value = 0;  /* Assume, until we see it does.  */
6346   current_function_returns_null = 0;
6347   current_function_returns_abnormally = 0;
6348   warn_about_return_type = 0;
6349   c_switch_stack = NULL;
6350
6351   nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6352   nstack_se->labels_def = NULL;
6353   nstack_se->labels_used = NULL;
6354   nstack_se->next = label_context_stack_se;
6355   label_context_stack_se = nstack_se;
6356
6357   nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6358   nstack_vm->labels_def = NULL;
6359   nstack_vm->labels_used = NULL;
6360   nstack_vm->scope = 0;
6361   nstack_vm->next = label_context_stack_vm;
6362   label_context_stack_vm = nstack_vm;
6363
6364   /* Indicate no valid break/continue context by setting these variables
6365      to some non-null, non-label value.  We'll notice and emit the proper
6366      error message in c_finish_bc_stmt.  */
6367   c_break_label = c_cont_label = size_zero_node;
6368
6369   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6370                           &attributes, NULL, NULL, DEPRECATED_NORMAL);
6371
6372   /* If the declarator is not suitable for a function definition,
6373      cause a syntax error.  */
6374   if (decl1 == 0)
6375     {
6376       label_context_stack_se = label_context_stack_se->next;
6377       label_context_stack_vm = label_context_stack_vm->next;
6378       return 0;
6379     }
6380
6381   decl_attributes (&decl1, attributes, 0);
6382
6383   if (DECL_DECLARED_INLINE_P (decl1)
6384       && DECL_UNINLINABLE (decl1)
6385       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6386     warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6387              decl1);
6388
6389   /* Handle gnu_inline attribute.  */
6390   if (declspecs->inline_p
6391       && !flag_gnu89_inline
6392       && TREE_CODE (decl1) == FUNCTION_DECL
6393       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6394           || current_function_decl))
6395     {
6396       if (declspecs->storage_class != csc_static)
6397         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6398     }
6399
6400   announce_function (decl1);
6401
6402   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6403     {
6404       error ("return type is an incomplete type");
6405       /* Make it return void instead.  */
6406       TREE_TYPE (decl1)
6407         = build_function_type (void_type_node,
6408                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6409     }
6410
6411   if (warn_about_return_type)
6412     pedwarn_c99 (input_location, flag_isoc99 ? 0 
6413                  : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6414                  "return type defaults to %<int%>");
6415
6416   /* Make the init_value nonzero so pushdecl knows this is not tentative.
6417      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
6418   DECL_INITIAL (decl1) = error_mark_node;
6419
6420   /* If this definition isn't a prototype and we had a prototype declaration
6421      before, copy the arg type info from that prototype.  */
6422   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6423   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6424     old_decl = 0;
6425   current_function_prototype_locus = UNKNOWN_LOCATION;
6426   current_function_prototype_built_in = false;
6427   current_function_prototype_arg_types = NULL_TREE;
6428   if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6429     {
6430       if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6431           && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6432                         TREE_TYPE (TREE_TYPE (old_decl))))
6433         {
6434           TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6435                                               TREE_TYPE (decl1));
6436           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6437           current_function_prototype_built_in
6438             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6439           current_function_prototype_arg_types
6440             = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6441         }
6442       if (TREE_PUBLIC (decl1))
6443         {
6444           /* If there is an external prototype declaration of this
6445              function, record its location but do not copy information
6446              to this decl.  This may be an invisible declaration
6447              (built-in or in a scope which has finished) or simply
6448              have more refined argument types than any declaration
6449              found above.  */
6450           struct c_binding *b;
6451           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6452             if (B_IN_SCOPE (b, external_scope))
6453               break;
6454           if (b)
6455             {
6456               tree ext_decl, ext_type;
6457               ext_decl = b->decl;
6458               ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6459               if (TREE_CODE (ext_type) == FUNCTION_TYPE
6460                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6461                                 TREE_TYPE (ext_type)))
6462                 {
6463                   current_function_prototype_locus
6464                     = DECL_SOURCE_LOCATION (ext_decl);
6465                   current_function_prototype_built_in
6466                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6467                   current_function_prototype_arg_types
6468                     = TYPE_ARG_TYPES (ext_type);
6469                 }
6470             }
6471         }
6472     }
6473
6474   /* Optionally warn of old-fashioned def with no previous prototype.  */
6475   if (warn_strict_prototypes
6476       && old_decl != error_mark_node
6477       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6478       && C_DECL_ISNT_PROTOTYPE (old_decl))
6479     warning (OPT_Wstrict_prototypes,
6480              "function declaration isn%'t a prototype");
6481   /* Optionally warn of any global def with no previous prototype.  */
6482   else if (warn_missing_prototypes
6483            && old_decl != error_mark_node
6484            && TREE_PUBLIC (decl1)
6485            && !MAIN_NAME_P (DECL_NAME (decl1))
6486            && C_DECL_ISNT_PROTOTYPE (old_decl))
6487     warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6488   /* Optionally warn of any def with no previous prototype
6489      if the function has already been used.  */
6490   else if (warn_missing_prototypes
6491            && old_decl != 0
6492            && old_decl != error_mark_node
6493            && TREE_USED (old_decl)
6494            && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6495     warning (OPT_Wmissing_prototypes,
6496              "%q+D was used with no prototype before its definition", decl1);
6497   /* Optionally warn of any global def with no previous declaration.  */
6498   else if (warn_missing_declarations
6499            && TREE_PUBLIC (decl1)
6500            && old_decl == 0
6501            && !MAIN_NAME_P (DECL_NAME (decl1)))
6502     warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6503              decl1);
6504   /* Optionally warn of any def with no previous declaration
6505      if the function has already been used.  */
6506   else if (warn_missing_declarations
6507            && old_decl != 0
6508            && old_decl != error_mark_node
6509            && TREE_USED (old_decl)
6510            && C_DECL_IMPLICIT (old_decl))
6511     warning (OPT_Wmissing_declarations,
6512              "%q+D was used with no declaration before its definition", decl1);
6513
6514   /* This function exists in static storage.
6515      (This does not mean `static' in the C sense!)  */
6516   TREE_STATIC (decl1) = 1;
6517
6518   /* A nested function is not global.  */
6519   if (current_function_decl != 0)
6520     TREE_PUBLIC (decl1) = 0;
6521
6522   /* This is the earliest point at which we might know the assembler
6523      name of the function.  Thus, if it's set before this, die horribly.  */
6524   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6525
6526   /* If #pragma weak was used, mark the decl weak now.  */
6527   if (current_scope == file_scope)
6528     maybe_apply_pragma_weak (decl1);
6529
6530   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
6531   if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6532     {
6533       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6534           != integer_type_node)
6535         pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6536
6537       check_main_parameter_types (decl1);
6538
6539       if (!TREE_PUBLIC (decl1))
6540         pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6541     }
6542
6543   /* Record the decl so that the function name is defined.
6544      If we already have a decl for this name, and it is a FUNCTION_DECL,
6545      use the old decl.  */
6546
6547   current_function_decl = pushdecl (decl1);
6548
6549   push_scope ();
6550   declare_parm_level ();
6551
6552   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6553   resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6554   DECL_ARTIFICIAL (resdecl) = 1;
6555   DECL_IGNORED_P (resdecl) = 1;
6556   DECL_RESULT (current_function_decl) = resdecl;
6557
6558   start_fname_decls ();
6559
6560   return 1;
6561 }
6562 \f
6563 /* Subroutine of store_parm_decls which handles new-style function
6564    definitions (prototype format). The parms already have decls, so we
6565    need only record them as in effect and complain if any redundant
6566    old-style parm decls were written.  */
6567 static void
6568 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6569 {
6570   tree decl;
6571
6572   if (current_scope->bindings)
6573     {
6574       error ("%Jold-style parameter declarations in prototyped "
6575              "function definition", fndecl);
6576
6577       /* Get rid of the old-style declarations.  */
6578       pop_scope ();
6579       push_scope ();
6580     }
6581   /* Don't issue this warning for nested functions, and don't issue this
6582      warning if we got here because ARG_INFO_TYPES was error_mark_node
6583      (this happens when a function definition has just an ellipsis in
6584      its parameter list).  */
6585   else if (!in_system_header && !current_function_scope
6586            && arg_info->types != error_mark_node)
6587     warning (OPT_Wtraditional,
6588              "%Jtraditional C rejects ISO C style function definitions",
6589              fndecl);
6590
6591   /* Now make all the parameter declarations visible in the function body.
6592      We can bypass most of the grunt work of pushdecl.  */
6593   for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6594     {
6595       DECL_CONTEXT (decl) = current_function_decl;
6596       if (DECL_NAME (decl))
6597         {
6598           bind (DECL_NAME (decl), decl, current_scope,
6599                 /*invisible=*/false, /*nested=*/false,
6600                 UNKNOWN_LOCATION);
6601           if (!TREE_USED (decl))
6602             warn_if_shadowing (decl);
6603         }
6604       else
6605         error ("%Jparameter name omitted", decl);
6606     }
6607
6608   /* Record the parameter list in the function declaration.  */
6609   DECL_ARGUMENTS (fndecl) = arg_info->parms;
6610
6611   /* Now make all the ancillary declarations visible, likewise.  */
6612   for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6613     {
6614       DECL_CONTEXT (decl) = current_function_decl;
6615       if (DECL_NAME (decl))
6616         bind (DECL_NAME (decl), decl, current_scope,
6617               /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6618     }
6619
6620   /* And all the tag declarations.  */
6621   for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6622     if (TREE_PURPOSE (decl))
6623       bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6624             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6625 }
6626
6627 /* Subroutine of store_parm_decls which handles old-style function
6628    definitions (separate parameter list and declarations).  */
6629
6630 static void
6631 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6632 {
6633   struct c_binding *b;
6634   tree parm, decl, last;
6635   tree parmids = arg_info->parms;
6636   struct pointer_set_t *seen_args = pointer_set_create ();
6637
6638   if (!in_system_header)
6639     warning (OPT_Wold_style_definition, "%Jold-style function definition",
6640              fndecl);
6641
6642   /* Match each formal parameter name with its declaration.  Save each
6643      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6644   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6645     {
6646       if (TREE_VALUE (parm) == 0)
6647         {
6648           error ("%Jparameter name missing from parameter list", fndecl);
6649           TREE_PURPOSE (parm) = 0;
6650           continue;
6651         }
6652
6653       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6654       if (b && B_IN_CURRENT_SCOPE (b))
6655         {
6656           decl = b->decl;
6657           /* If we got something other than a PARM_DECL it is an error.  */
6658           if (TREE_CODE (decl) != PARM_DECL)
6659             error ("%q+D declared as a non-parameter", decl);
6660           /* If the declaration is already marked, we have a duplicate
6661              name.  Complain and ignore the duplicate.  */
6662           else if (pointer_set_contains (seen_args, decl))
6663             {
6664               error ("multiple parameters named %q+D", decl);
6665               TREE_PURPOSE (parm) = 0;
6666               continue;
6667             }
6668           /* If the declaration says "void", complain and turn it into
6669              an int.  */
6670           else if (VOID_TYPE_P (TREE_TYPE (decl)))
6671             {
6672               error ("parameter %q+D declared with void type", decl);
6673               TREE_TYPE (decl) = integer_type_node;
6674               DECL_ARG_TYPE (decl) = integer_type_node;
6675               layout_decl (decl, 0);
6676             }
6677           warn_if_shadowing (decl);
6678         }
6679       /* If no declaration found, default to int.  */
6680       else
6681         {
6682           decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6683           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6684           DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6685           pushdecl (decl);
6686           warn_if_shadowing (decl);
6687
6688           if (flag_isoc99)
6689             pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6690           else 
6691             warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6692         }
6693
6694       TREE_PURPOSE (parm) = decl;
6695       pointer_set_insert (seen_args, decl);
6696     }
6697
6698   /* Now examine the parms chain for incomplete declarations
6699      and declarations with no corresponding names.  */
6700
6701   for (b = current_scope->bindings; b; b = b->prev)
6702     {
6703       parm = b->decl;
6704       if (TREE_CODE (parm) != PARM_DECL)
6705         continue;
6706
6707       if (TREE_TYPE (parm) != error_mark_node
6708           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6709         {
6710           error ("parameter %q+D has incomplete type", parm);
6711           TREE_TYPE (parm) = error_mark_node;
6712         }
6713
6714       if (!pointer_set_contains (seen_args, parm))
6715         {
6716           error ("declaration for parameter %q+D but no such parameter", parm);
6717
6718           /* Pretend the parameter was not missing.
6719              This gets us to a standard state and minimizes
6720              further error messages.  */
6721           parmids = chainon (parmids, tree_cons (parm, 0, 0));
6722         }
6723     }
6724
6725   /* Chain the declarations together in the order of the list of
6726      names.  Store that chain in the function decl, replacing the
6727      list of names.  Update the current scope to match.  */
6728   DECL_ARGUMENTS (fndecl) = 0;
6729
6730   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6731     if (TREE_PURPOSE (parm))
6732       break;
6733   if (parm && TREE_PURPOSE (parm))
6734     {
6735       last = TREE_PURPOSE (parm);
6736       DECL_ARGUMENTS (fndecl) = last;
6737
6738       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6739         if (TREE_PURPOSE (parm))
6740           {
6741             TREE_CHAIN (last) = TREE_PURPOSE (parm);
6742             last = TREE_PURPOSE (parm);
6743           }
6744       TREE_CHAIN (last) = 0;
6745     }
6746
6747   pointer_set_destroy (seen_args);
6748
6749   /* If there was a previous prototype,
6750      set the DECL_ARG_TYPE of each argument according to
6751      the type previously specified, and report any mismatches.  */
6752
6753   if (current_function_prototype_arg_types)
6754     {
6755       tree type;
6756       for (parm = DECL_ARGUMENTS (fndecl),
6757              type = current_function_prototype_arg_types;
6758            parm || (type && TREE_VALUE (type) != error_mark_node
6759                    && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6760            parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6761         {
6762           if (parm == 0 || type == 0
6763               || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6764             {
6765               if (current_function_prototype_built_in)
6766                 warning (0, "number of arguments doesn%'t match "
6767                          "built-in prototype");
6768               else
6769                 {
6770                   error ("number of arguments doesn%'t match prototype");
6771                   error ("%Hprototype declaration",
6772                          &current_function_prototype_locus);
6773                 }
6774               break;
6775             }
6776           /* Type for passing arg must be consistent with that
6777              declared for the arg.  ISO C says we take the unqualified
6778              type for parameters declared with qualified type.  */
6779           if (TREE_TYPE (parm) != error_mark_node
6780               && TREE_TYPE (type) != error_mark_node
6781               && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6782                              TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6783             {
6784               if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6785                   == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6786                 {
6787                   /* Adjust argument to match prototype.  E.g. a previous
6788                      `int foo(float);' prototype causes
6789                      `int foo(x) float x; {...}' to be treated like
6790                      `int foo(float x) {...}'.  This is particularly
6791                      useful for argument types like uid_t.  */
6792                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6793
6794                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6795                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6796                       && TYPE_PRECISION (TREE_TYPE (parm))
6797                       < TYPE_PRECISION (integer_type_node))
6798                     DECL_ARG_TYPE (parm) = integer_type_node;
6799
6800                   /* ??? Is it possible to get here with a
6801                      built-in prototype or will it always have
6802                      been diagnosed as conflicting with an
6803                      old-style definition and discarded?  */
6804                   if (current_function_prototype_built_in)
6805                     warning (OPT_pedantic, "promoted argument %qD "
6806                              "doesn%'t match built-in prototype", parm);
6807                   else
6808                     {
6809                       pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6810                                "doesn%'t match prototype", parm);
6811                       pedwarn (current_function_prototype_locus, OPT_pedantic,
6812                                "prototype declaration");
6813                     }
6814                 }
6815               else
6816                 {
6817                   if (current_function_prototype_built_in)
6818                     warning (0, "argument %qD doesn%'t match "
6819                              "built-in prototype", parm);
6820                   else
6821                     {
6822                       error ("argument %qD doesn%'t match prototype", parm);
6823                       error ("%Hprototype declaration",
6824                              &current_function_prototype_locus);
6825                     }
6826                 }
6827             }
6828         }
6829       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6830     }
6831
6832   /* Otherwise, create a prototype that would match.  */
6833
6834   else
6835     {
6836       tree actual = 0, last = 0, type;
6837
6838       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6839         {
6840           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6841           if (last)
6842             TREE_CHAIN (last) = type;
6843           else
6844             actual = type;
6845           last = type;
6846         }
6847       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6848       if (last)
6849         TREE_CHAIN (last) = type;
6850       else
6851         actual = type;
6852
6853       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6854          of the type of this function, but we need to avoid having this
6855          affect the types of other similarly-typed functions, so we must
6856          first force the generation of an identical (but separate) type
6857          node for the relevant function type.  The new node we create
6858          will be a variant of the main variant of the original function
6859          type.  */
6860
6861       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6862
6863       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6864     }
6865 }
6866
6867 /* Store parameter declarations passed in ARG_INFO into the current
6868    function declaration.  */
6869
6870 void
6871 store_parm_decls_from (struct c_arg_info *arg_info)
6872 {
6873   current_function_arg_info = arg_info;
6874   store_parm_decls ();
6875 }
6876
6877 /* Store the parameter declarations into the current function declaration.
6878    This is called after parsing the parameter declarations, before
6879    digesting the body of the function.
6880
6881    For an old-style definition, construct a prototype out of the old-style
6882    parameter declarations and inject it into the function's type.  */
6883
6884 void
6885 store_parm_decls (void)
6886 {
6887   tree fndecl = current_function_decl;
6888   bool proto;
6889
6890   /* The argument information block for FNDECL.  */
6891   struct c_arg_info *arg_info = current_function_arg_info;
6892   current_function_arg_info = 0;
6893
6894   /* True if this definition is written with a prototype.  Note:
6895      despite C99 6.7.5.3p14, we can *not* treat an empty argument
6896      list in a function definition as equivalent to (void) -- an
6897      empty argument list specifies the function has no parameters,
6898      but only (void) sets up a prototype for future calls.  */
6899   proto = arg_info->types != 0;
6900
6901   if (proto)
6902     store_parm_decls_newstyle (fndecl, arg_info);
6903   else
6904     store_parm_decls_oldstyle (fndecl, arg_info);
6905
6906   /* The next call to push_scope will be a function body.  */
6907
6908   next_is_function_body = true;
6909
6910   /* Write a record describing this function definition to the prototypes
6911      file (if requested).  */
6912
6913   gen_aux_info_record (fndecl, 1, 0, proto);
6914
6915   /* Initialize the RTL code for the function.  */
6916   allocate_struct_function (fndecl, false);
6917
6918   /* Begin the statement tree for this function.  */
6919   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6920
6921   /* ??? Insert the contents of the pending sizes list into the function
6922      to be evaluated.  The only reason left to have this is
6923         void foo(int n, int array[n++])
6924      because we throw away the array type in favor of a pointer type, and
6925      thus won't naturally see the SAVE_EXPR containing the increment.  All
6926      other pending sizes would be handled by gimplify_parameters.  */
6927   {
6928     tree t;
6929     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6930       add_stmt (TREE_VALUE (t));
6931   }
6932
6933   /* Even though we're inside a function body, we still don't want to
6934      call expand_expr to calculate the size of a variable-sized array.
6935      We haven't necessarily assigned RTL to all variables yet, so it's
6936      not safe to try to expand expressions involving them.  */
6937   cfun->dont_save_pending_sizes_p = 1;
6938 }
6939 \f
6940 /* Emit diagnostics that require gimple input for detection.  Operate on
6941    FNDECL and all its nested functions.  */
6942
6943 static void
6944 c_gimple_diagnostics_recursively (tree fndecl)
6945 {
6946   struct cgraph_node *cgn;
6947   gimple_seq body = gimple_body (fndecl);
6948
6949   /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6950   c_warn_unused_result (body);
6951
6952   /* Notice when OpenMP structured block constraints are violated.  */
6953   if (flag_openmp)
6954     diagnose_omp_structured_block_errors (fndecl);
6955
6956   /* Finalize all nested functions now.  */
6957   cgn = cgraph_node (fndecl);
6958   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6959     c_gimple_diagnostics_recursively (cgn->decl);
6960 }
6961
6962 /* Finish up a function declaration and compile that function
6963    all the way to assembler language output.  The free the storage
6964    for the function definition.
6965
6966    This is called after parsing the body of the function definition.  */
6967
6968 void
6969 finish_function (void)
6970 {
6971   tree fndecl = current_function_decl;
6972
6973   label_context_stack_se = label_context_stack_se->next;
6974   label_context_stack_vm = label_context_stack_vm->next;
6975
6976   if (TREE_CODE (fndecl) == FUNCTION_DECL
6977       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6978     {
6979       tree args = DECL_ARGUMENTS (fndecl);
6980       for (; args; args = TREE_CHAIN (args))
6981         {
6982           tree type = TREE_TYPE (args);
6983           if (INTEGRAL_TYPE_P (type)
6984               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6985             DECL_ARG_TYPE (args) = integer_type_node;
6986         }
6987     }
6988
6989   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6990     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6991
6992   /* Must mark the RESULT_DECL as being in this function.  */
6993
6994   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6995     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6996
6997   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
6998       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6999       == integer_type_node && flag_isoc99)
7000     {
7001       tree stmt = c_finish_return (integer_zero_node, NULL_TREE);
7002       /* Hack.  We don't want the middle-end to warn that this return
7003          is unreachable, so we mark its location as special.  Using
7004          UNKNOWN_LOCATION has the problem that it gets clobbered in
7005          annotate_one_with_locus.  A cleaner solution might be to
7006          ensure ! should_carry_locus_p (stmt), but that needs a flag.
7007       */
7008       SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
7009     }
7010
7011   /* Tie off the statement tree for this function.  */
7012   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
7013
7014   finish_fname_decls ();
7015
7016   /* Complain if there's just no return statement.  */
7017   if (warn_return_type
7018       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
7019       && !current_function_returns_value && !current_function_returns_null
7020       /* Don't complain if we are no-return.  */
7021       && !current_function_returns_abnormally
7022       /* Don't warn for main().  */
7023       && !MAIN_NAME_P (DECL_NAME (fndecl))
7024       /* Or if they didn't actually specify a return type.  */
7025       && !C_FUNCTION_IMPLICIT_INT (fndecl)
7026       /* Normally, with -Wreturn-type, flow will complain, but we might
7027          optimize out static functions.  */
7028       && !TREE_PUBLIC (fndecl))
7029     {
7030       warning (OPT_Wreturn_type,
7031                "no return statement in function returning non-void");
7032       TREE_NO_WARNING (fndecl) = 1;
7033     }
7034
7035   /* Store the end of the function, so that we get good line number
7036      info for the epilogue.  */
7037   cfun->function_end_locus = input_location;
7038
7039   /* Finalize the ELF visibility for the function.  */
7040   c_determine_visibility (fndecl);
7041
7042   /* For GNU C extern inline functions disregard inline limits.  */
7043   if (DECL_EXTERNAL (fndecl) 
7044       && DECL_DECLARED_INLINE_P (fndecl))
7045     DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
7046
7047   /* Genericize before inlining.  Delay genericizing nested functions
7048      until their parent function is genericized.  Since finalizing
7049      requires GENERIC, delay that as well.  */
7050
7051   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
7052       && !undef_nested_function)
7053     {
7054       if (!decl_function_context (fndecl))
7055         {
7056           c_genericize (fndecl);
7057           c_gimple_diagnostics_recursively (fndecl);
7058
7059           /* ??? Objc emits functions after finalizing the compilation unit.
7060              This should be cleaned up later and this conditional removed.  */
7061           if (cgraph_global_info_ready)
7062             {
7063               cgraph_add_new_function (fndecl, false);
7064               return;
7065             }
7066
7067           cgraph_finalize_function (fndecl, false);
7068         }
7069       else
7070         {
7071           /* Register this function with cgraph just far enough to get it
7072             added to our parent's nested function list.  Handy, since the
7073             C front end doesn't have such a list.  */
7074           (void) cgraph_node (fndecl);
7075         }
7076     }
7077
7078   if (!decl_function_context (fndecl))
7079     undef_nested_function = false;
7080
7081   /* We're leaving the context of this function, so zap cfun.
7082      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7083      tree_rest_of_compilation.  */
7084   set_cfun (NULL);
7085   current_function_decl = NULL;
7086 }
7087 \f
7088 /* Check the declarations given in a for-loop for satisfying the C99
7089    constraints.  If exactly one such decl is found, return it.  */
7090
7091 tree
7092 check_for_loop_decls (void)
7093 {
7094   struct c_binding *b;
7095   tree one_decl = NULL_TREE;
7096   int n_decls = 0;
7097
7098   if (!flag_isoc99)
7099     {
7100       static bool hint = true;
7101       /* If we get here, declarations have been used in a for loop without
7102          the C99 for loop scope.  This doesn't make much sense, so don't
7103          allow it.  */
7104       error ("%<for%> loop initial declarations are only allowed in C99 mode");
7105       if (hint)
7106         {
7107           inform (input_location, 
7108                   "use option -std=c99 or -std=gnu99 to compile your code");
7109           hint = false;
7110         }
7111       return NULL_TREE;
7112     }
7113   /* C99 subclause 6.8.5 paragraph 3:
7114
7115        [#3]  The  declaration  part  of  a for statement shall only
7116        declare identifiers for objects having storage class auto or
7117        register.
7118
7119      It isn't clear whether, in this sentence, "identifiers" binds to
7120      "shall only declare" or to "objects" - that is, whether all identifiers
7121      declared must be identifiers for objects, or whether the restriction
7122      only applies to those that are.  (A question on this in comp.std.c
7123      in November 2000 received no answer.)  We implement the strictest
7124      interpretation, to avoid creating an extension which later causes
7125      problems.  */
7126
7127   for (b = current_scope->bindings; b; b = b->prev)
7128     {
7129       tree id = b->id;
7130       tree decl = b->decl;
7131
7132       if (!id)
7133         continue;
7134
7135       switch (TREE_CODE (decl))
7136         {
7137         case VAR_DECL:
7138           if (TREE_STATIC (decl))
7139             error ("declaration of static variable %q+D in %<for%> loop "
7140                    "initial declaration", decl);
7141           else if (DECL_EXTERNAL (decl))
7142             error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7143                    "initial declaration", decl);
7144           break;
7145
7146         case RECORD_TYPE:
7147           error ("%<struct %E%> declared in %<for%> loop initial declaration",
7148                  id);
7149           break;
7150         case UNION_TYPE:
7151           error ("%<union %E%> declared in %<for%> loop initial declaration",
7152                  id);
7153           break;
7154         case ENUMERAL_TYPE:
7155           error ("%<enum %E%> declared in %<for%> loop initial declaration",
7156                  id);
7157           break;
7158         default:
7159           error ("declaration of non-variable %q+D in %<for%> loop "
7160                  "initial declaration", decl);
7161         }
7162
7163       n_decls++;
7164       one_decl = decl;
7165     }
7166
7167   return n_decls == 1 ? one_decl : NULL_TREE;
7168 }
7169 \f
7170 /* Save and reinitialize the variables
7171    used during compilation of a C function.  */
7172
7173 void
7174 c_push_function_context (void)
7175 {
7176   struct language_function *p;
7177   p = GGC_NEW (struct language_function);
7178   cfun->language = p;
7179
7180   p->base.x_stmt_tree = c_stmt_tree;
7181   p->x_break_label = c_break_label;
7182   p->x_cont_label = c_cont_label;
7183   p->x_switch_stack = c_switch_stack;
7184   p->arg_info = current_function_arg_info;
7185   p->returns_value = current_function_returns_value;
7186   p->returns_null = current_function_returns_null;
7187   p->returns_abnormally = current_function_returns_abnormally;
7188   p->warn_about_return_type = warn_about_return_type;
7189
7190   push_function_context ();
7191 }
7192
7193 /* Restore the variables used during compilation of a C function.  */
7194
7195 void
7196 c_pop_function_context (void)
7197 {
7198   struct language_function *p;
7199
7200   pop_function_context ();
7201   p = cfun->language;
7202   cfun->language = NULL;
7203
7204   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7205       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7206     {
7207       /* Stop pointing to the local nodes about to be freed.  */
7208       /* But DECL_INITIAL must remain nonzero so we know this
7209          was an actual function definition.  */
7210       DECL_INITIAL (current_function_decl) = error_mark_node;
7211       DECL_ARGUMENTS (current_function_decl) = 0;
7212     }
7213
7214   c_stmt_tree = p->base.x_stmt_tree;
7215   c_break_label = p->x_break_label;
7216   c_cont_label = p->x_cont_label;
7217   c_switch_stack = p->x_switch_stack;
7218   current_function_arg_info = p->arg_info;
7219   current_function_returns_value = p->returns_value;
7220   current_function_returns_null = p->returns_null;
7221   current_function_returns_abnormally = p->returns_abnormally;
7222   warn_about_return_type = p->warn_about_return_type;
7223 }
7224
7225 /* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
7226
7227 void
7228 c_dup_lang_specific_decl (tree decl)
7229 {
7230   struct lang_decl *ld;
7231
7232   if (!DECL_LANG_SPECIFIC (decl))
7233     return;
7234
7235   ld = GGC_NEW (struct lang_decl);
7236   memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7237   DECL_LANG_SPECIFIC (decl) = ld;
7238 }
7239
7240 /* The functions below are required for functionality of doing
7241    function at once processing in the C front end. Currently these
7242    functions are not called from anywhere in the C front end, but as
7243    these changes continue, that will change.  */
7244
7245 /* Returns the stmt_tree (if any) to which statements are currently
7246    being added.  If there is no active statement-tree, NULL is
7247    returned.  */
7248
7249 stmt_tree
7250 current_stmt_tree (void)
7251 {
7252   return &c_stmt_tree;
7253 }
7254
7255 /* Return the global value of T as a symbol.  */
7256
7257 tree
7258 identifier_global_value (tree t)
7259 {
7260   struct c_binding *b;
7261
7262   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7263     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7264       return b->decl;
7265
7266   return 0;
7267 }
7268
7269 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
7270    otherwise the name is found in ridpointers from RID_INDEX.  */
7271
7272 void
7273 record_builtin_type (enum rid rid_index, const char *name, tree type)
7274 {
7275   tree id, decl;
7276   if (name == 0)
7277     id = ridpointers[(int) rid_index];
7278   else
7279     id = get_identifier (name);
7280   decl = build_decl (TYPE_DECL, id, type);
7281   pushdecl (decl);
7282   if (debug_hooks->type_decl)
7283     debug_hooks->type_decl (decl, false);
7284 }
7285
7286 /* Build the void_list_node (void_type_node having been created).  */
7287 tree
7288 build_void_list_node (void)
7289 {
7290   tree t = build_tree_list (NULL_TREE, void_type_node);
7291   return t;
7292 }
7293
7294 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
7295
7296 struct c_parm *
7297 build_c_parm (struct c_declspecs *specs, tree attrs,
7298               struct c_declarator *declarator)
7299 {
7300   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7301   ret->specs = specs;
7302   ret->attrs = attrs;
7303   ret->declarator = declarator;
7304   return ret;
7305 }
7306
7307 /* Return a declarator with nested attributes.  TARGET is the inner
7308    declarator to which these attributes apply.  ATTRS are the
7309    attributes.  */
7310
7311 struct c_declarator *
7312 build_attrs_declarator (tree attrs, struct c_declarator *target)
7313 {
7314   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7315   ret->kind = cdk_attrs;
7316   ret->declarator = target;
7317   ret->u.attrs = attrs;
7318   return ret;
7319 }
7320
7321 /* Return a declarator for a function with arguments specified by ARGS
7322    and return type specified by TARGET.  */
7323
7324 struct c_declarator *
7325 build_function_declarator (struct c_arg_info *args,
7326                            struct c_declarator *target)
7327 {
7328   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7329   ret->kind = cdk_function;
7330   ret->declarator = target;
7331   ret->u.arg_info = args;
7332   return ret;
7333 }
7334
7335 /* Return a declarator for the identifier IDENT (which may be
7336    NULL_TREE for an abstract declarator).  */
7337
7338 struct c_declarator *
7339 build_id_declarator (tree ident)
7340 {
7341   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7342   ret->kind = cdk_id;
7343   ret->declarator = 0;
7344   ret->u.id = ident;
7345   /* Default value - may get reset to a more precise location. */
7346   ret->id_loc = input_location;
7347   return ret;
7348 }
7349
7350 /* Return something to represent absolute declarators containing a *.
7351    TARGET is the absolute declarator that the * contains.
7352    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7353    to apply to the pointer type.  */
7354
7355 struct c_declarator *
7356 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7357                          struct c_declarator *target)
7358 {
7359   tree attrs;
7360   int quals = 0;
7361   struct c_declarator *itarget = target;
7362   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7363   if (type_quals_attrs)
7364     {
7365       attrs = type_quals_attrs->attrs;
7366       quals = quals_from_declspecs (type_quals_attrs);
7367       if (attrs != NULL_TREE)
7368         itarget = build_attrs_declarator (attrs, target);
7369     }
7370   ret->kind = cdk_pointer;
7371   ret->declarator = itarget;
7372   ret->u.pointer_quals = quals;
7373   return ret;
7374 }
7375
7376 /* Return a pointer to a structure for an empty list of declaration
7377    specifiers.  */
7378
7379 struct c_declspecs *
7380 build_null_declspecs (void)
7381 {
7382   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7383   ret->type = 0;
7384   ret->expr = 0;
7385   ret->decl_attr = 0;
7386   ret->attrs = 0;
7387   ret->typespec_word = cts_none;
7388   ret->storage_class = csc_none;
7389   ret->expr_const_operands = true;
7390   ret->declspecs_seen_p = false;
7391   ret->type_seen_p = false;
7392   ret->non_sc_seen_p = false;
7393   ret->typedef_p = false;
7394   ret->tag_defined_p = false;
7395   ret->explicit_signed_p = false;
7396   ret->deprecated_p = false;
7397   ret->default_int_p = false;
7398   ret->long_p = false;
7399   ret->long_long_p = false;
7400   ret->short_p = false;
7401   ret->signed_p = false;
7402   ret->unsigned_p = false;
7403   ret->complex_p = false;
7404   ret->inline_p = false;
7405   ret->thread_p = false;
7406   ret->const_p = false;
7407   ret->volatile_p = false;
7408   ret->restrict_p = false;
7409   ret->saturating_p = false;
7410   return ret;
7411 }
7412
7413 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7414    returning SPECS.  */
7415
7416 struct c_declspecs *
7417 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7418 {
7419   enum rid i;
7420   bool dupe = false;
7421   specs->non_sc_seen_p = true;
7422   specs->declspecs_seen_p = true;
7423   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7424               && C_IS_RESERVED_WORD (qual));
7425   i = C_RID_CODE (qual);
7426   switch (i)
7427     {
7428     case RID_CONST:
7429       dupe = specs->const_p;
7430       specs->const_p = true;
7431       break;
7432     case RID_VOLATILE:
7433       dupe = specs->volatile_p;
7434       specs->volatile_p = true;
7435       break;
7436     case RID_RESTRICT:
7437       dupe = specs->restrict_p;
7438       specs->restrict_p = true;
7439       break;
7440     default:
7441       gcc_unreachable ();
7442     }
7443   if (dupe && !flag_isoc99)
7444     pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7445   return specs;
7446 }
7447
7448 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7449    returning SPECS.  */
7450
7451 struct c_declspecs *
7452 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7453 {
7454   tree type = spec.spec;
7455   specs->non_sc_seen_p = true;
7456   specs->declspecs_seen_p = true;
7457   specs->type_seen_p = true;
7458   if (TREE_DEPRECATED (type))
7459     specs->deprecated_p = true;
7460
7461   /* Handle type specifier keywords.  */
7462   if (TREE_CODE (type) == IDENTIFIER_NODE
7463       && C_IS_RESERVED_WORD (type)
7464       && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7465     {
7466       enum rid i = C_RID_CODE (type);
7467       if (specs->type)
7468         {
7469           error ("two or more data types in declaration specifiers");
7470           return specs;
7471         }
7472       if ((int) i <= (int) RID_LAST_MODIFIER)
7473         {
7474           /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat".  */
7475           bool dupe = false;
7476           switch (i)
7477             {
7478             case RID_LONG:
7479               if (specs->long_long_p)
7480                 {
7481                   error ("%<long long long%> is too long for GCC");
7482                   break;
7483                 }
7484               if (specs->long_p)
7485                 {
7486                   if (specs->typespec_word == cts_double)
7487                     {
7488                       error ("both %<long long%> and %<double%> in "
7489                              "declaration specifiers");
7490                       break;
7491                     }
7492                   pedwarn_c90 (input_location, OPT_Wlong_long, 
7493                                "ISO C90 does not support %<long long%>");
7494                   specs->long_long_p = 1;
7495                   break;
7496                 }
7497               if (specs->short_p)
7498                 error ("both %<long%> and %<short%> in "
7499                        "declaration specifiers");
7500               else if (specs->typespec_word == cts_void)
7501                 error ("both %<long%> and %<void%> in "
7502                        "declaration specifiers");
7503               else if (specs->typespec_word == cts_bool)
7504                 error ("both %<long%> and %<_Bool%> in "
7505                        "declaration specifiers");
7506               else if (specs->typespec_word == cts_char)
7507                 error ("both %<long%> and %<char%> in "
7508                        "declaration specifiers");
7509               else if (specs->typespec_word == cts_float)
7510                 error ("both %<long%> and %<float%> in "
7511                        "declaration specifiers");
7512               else if (specs->typespec_word == cts_dfloat32)
7513                 error ("both %<long%> and %<_Decimal32%> in "
7514                        "declaration specifiers");
7515               else if (specs->typespec_word == cts_dfloat64)
7516                 error ("both %<long%> and %<_Decimal64%> in "
7517                        "declaration specifiers");
7518               else if (specs->typespec_word == cts_dfloat128)
7519                 error ("both %<long%> and %<_Decimal128%> in "
7520                        "declaration specifiers");
7521               else
7522                 specs->long_p = true;
7523               break;
7524             case RID_SHORT:
7525               dupe = specs->short_p;
7526               if (specs->long_p)
7527                 error ("both %<long%> and %<short%> in "
7528                        "declaration specifiers");
7529               else if (specs->typespec_word == cts_void)
7530                 error ("both %<short%> and %<void%> in "
7531                        "declaration specifiers");
7532               else if (specs->typespec_word == cts_bool)
7533                 error ("both %<short%> and %<_Bool%> in "
7534                        "declaration specifiers");
7535               else if (specs->typespec_word == cts_char)
7536                 error ("both %<short%> and %<char%> in "
7537                        "declaration specifiers");
7538               else if (specs->typespec_word == cts_float)
7539                 error ("both %<short%> and %<float%> in "
7540                        "declaration specifiers");
7541               else if (specs->typespec_word == cts_double)
7542                 error ("both %<short%> and %<double%> in "
7543                        "declaration specifiers");
7544               else if (specs->typespec_word == cts_dfloat32)
7545                 error ("both %<short%> and %<_Decimal32%> in "
7546                        "declaration specifiers");
7547               else if (specs->typespec_word == cts_dfloat64)
7548                 error ("both %<short%> and %<_Decimal64%> in "
7549                                         "declaration specifiers");
7550               else if (specs->typespec_word == cts_dfloat128)
7551                 error ("both %<short%> and %<_Decimal128%> in "
7552                        "declaration specifiers");
7553               else
7554                 specs->short_p = true;
7555               break;
7556             case RID_SIGNED:
7557               dupe = specs->signed_p;
7558               if (specs->unsigned_p)
7559                 error ("both %<signed%> and %<unsigned%> in "
7560                        "declaration specifiers");
7561               else if (specs->typespec_word == cts_void)
7562                 error ("both %<signed%> and %<void%> in "
7563                        "declaration specifiers");
7564               else if (specs->typespec_word == cts_bool)
7565                 error ("both %<signed%> and %<_Bool%> in "
7566                        "declaration specifiers");
7567               else if (specs->typespec_word == cts_float)
7568                 error ("both %<signed%> and %<float%> in "
7569                        "declaration specifiers");
7570               else if (specs->typespec_word == cts_double)
7571                 error ("both %<signed%> and %<double%> in "
7572                        "declaration specifiers");
7573               else if (specs->typespec_word == cts_dfloat32)
7574                 error ("both %<signed%> and %<_Decimal32%> in "
7575                        "declaration specifiers");
7576               else if (specs->typespec_word == cts_dfloat64)
7577                 error ("both %<signed%> and %<_Decimal64%> in "
7578                        "declaration specifiers");
7579               else if (specs->typespec_word == cts_dfloat128)
7580                 error ("both %<signed%> and %<_Decimal128%> in "
7581                        "declaration specifiers");
7582               else
7583                 specs->signed_p = true;
7584               break;
7585             case RID_UNSIGNED:
7586               dupe = specs->unsigned_p;
7587               if (specs->signed_p)
7588                 error ("both %<signed%> and %<unsigned%> in "
7589                        "declaration specifiers");
7590               else if (specs->typespec_word == cts_void)
7591                 error ("both %<unsigned%> and %<void%> in "
7592                        "declaration specifiers");
7593               else if (specs->typespec_word == cts_bool)
7594                 error ("both %<unsigned%> and %<_Bool%> in "
7595                        "declaration specifiers");
7596               else if (specs->typespec_word == cts_float)
7597                 error ("both %<unsigned%> and %<float%> in "
7598                        "declaration specifiers");
7599               else if (specs->typespec_word == cts_double)
7600                 error ("both %<unsigned%> and %<double%> in "
7601                        "declaration specifiers");
7602               else if (specs->typespec_word == cts_dfloat32)
7603                 error ("both %<unsigned%> and %<_Decimal32%> in "
7604                        "declaration specifiers");
7605               else if (specs->typespec_word == cts_dfloat64)
7606                 error ("both %<unsigned%> and %<_Decimal64%> in "
7607                        "declaration specifiers");
7608               else if (specs->typespec_word == cts_dfloat128)
7609                 error ("both %<unsigned%> and %<_Decimal128%> in "
7610                        "declaration specifiers");
7611               else
7612                 specs->unsigned_p = true;
7613               break;
7614             case RID_COMPLEX:
7615               dupe = specs->complex_p;
7616               if (!flag_isoc99 && !in_system_header)
7617                 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7618               if (specs->typespec_word == cts_void)
7619                 error ("both %<complex%> and %<void%> in "
7620                        "declaration specifiers");
7621               else if (specs->typespec_word == cts_bool)
7622                 error ("both %<complex%> and %<_Bool%> in "
7623                        "declaration specifiers");
7624               else if (specs->typespec_word == cts_dfloat32)
7625                 error ("both %<complex%> and %<_Decimal32%> in "
7626                        "declaration specifiers");
7627               else if (specs->typespec_word == cts_dfloat64)
7628                 error ("both %<complex%> and %<_Decimal64%> in "
7629                        "declaration specifiers");
7630               else if (specs->typespec_word == cts_dfloat128)
7631                 error ("both %<complex%> and %<_Decimal128%> in "
7632                        "declaration specifiers");
7633               else if (specs->typespec_word == cts_fract)
7634                 error ("both %<complex%> and %<_Fract%> in "
7635                        "declaration specifiers");
7636               else if (specs->typespec_word == cts_accum)
7637                 error ("both %<complex%> and %<_Accum%> in "
7638                        "declaration specifiers");
7639               else if (specs->saturating_p)
7640                 error ("both %<complex%> and %<_Sat%> in "
7641                        "declaration specifiers");
7642               else
7643                 specs->complex_p = true;
7644               break;
7645             case RID_SAT:
7646               dupe = specs->saturating_p;
7647               pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7648               if (specs->typespec_word == cts_void)
7649                 error ("both %<_Sat%> and %<void%> in "
7650                        "declaration specifiers");
7651               else if (specs->typespec_word == cts_bool)
7652                 error ("both %<_Sat%> and %<_Bool%> in "
7653                        "declaration specifiers");
7654               else if (specs->typespec_word == cts_char)
7655                 error ("both %<_Sat%> and %<char%> in "
7656                        "declaration specifiers");
7657               else if (specs->typespec_word == cts_int)
7658                 error ("both %<_Sat%> and %<int%> in "
7659                        "declaration specifiers");
7660               else if (specs->typespec_word == cts_float)
7661                 error ("both %<_Sat%> and %<float%> in "
7662                        "declaration specifiers");
7663               else if (specs->typespec_word == cts_double)
7664                 error ("both %<_Sat%> and %<double%> in "
7665                        "declaration specifiers");
7666               else if (specs->typespec_word == cts_dfloat32)
7667                 error ("both %<_Sat%> and %<_Decimal32%> in "
7668                        "declaration specifiers");
7669               else if (specs->typespec_word == cts_dfloat64)
7670                 error ("both %<_Sat%> and %<_Decimal64%> in "
7671                        "declaration specifiers");
7672               else if (specs->typespec_word == cts_dfloat128)
7673                 error ("both %<_Sat%> and %<_Decimal128%> in "
7674                        "declaration specifiers");
7675               else if (specs->complex_p)
7676                 error ("both %<_Sat%> and %<complex%> in "
7677                        "declaration specifiers");
7678               else
7679                 specs->saturating_p = true;
7680               break;
7681             default:
7682               gcc_unreachable ();
7683             }
7684
7685           if (dupe)
7686             error ("duplicate %qE", type);
7687
7688           return specs;
7689         }
7690       else
7691         {
7692           /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7693              "_Decimal64", "_Decimal128", "_Fract" or "_Accum".  */
7694           if (specs->typespec_word != cts_none)
7695             {
7696               error ("two or more data types in declaration specifiers");
7697               return specs;
7698             }
7699           switch (i)
7700             {
7701             case RID_VOID:
7702               if (specs->long_p)
7703                 error ("both %<long%> and %<void%> in "
7704                        "declaration specifiers");
7705               else if (specs->short_p)
7706                 error ("both %<short%> and %<void%> in "
7707                        "declaration specifiers");
7708               else if (specs->signed_p)
7709                 error ("both %<signed%> and %<void%> in "
7710                        "declaration specifiers");
7711               else if (specs->unsigned_p)
7712                 error ("both %<unsigned%> and %<void%> in "
7713                        "declaration specifiers");
7714               else if (specs->complex_p)
7715                 error ("both %<complex%> and %<void%> in "
7716                        "declaration specifiers");
7717               else if (specs->saturating_p)
7718                 error ("both %<_Sat%> and %<void%> in "
7719                        "declaration specifiers");
7720               else
7721                 specs->typespec_word = cts_void;
7722               return specs;
7723             case RID_BOOL:
7724               if (specs->long_p)
7725                 error ("both %<long%> and %<_Bool%> in "
7726                        "declaration specifiers");
7727               else if (specs->short_p)
7728                 error ("both %<short%> and %<_Bool%> in "
7729                        "declaration specifiers");
7730               else if (specs->signed_p)
7731                 error ("both %<signed%> and %<_Bool%> in "
7732                        "declaration specifiers");
7733               else if (specs->unsigned_p)
7734                 error ("both %<unsigned%> and %<_Bool%> in "
7735                        "declaration specifiers");
7736               else if (specs->complex_p)
7737                 error ("both %<complex%> and %<_Bool%> in "
7738                        "declaration specifiers");
7739               else if (specs->saturating_p)
7740                 error ("both %<_Sat%> and %<_Bool%> in "
7741                        "declaration specifiers");
7742               else
7743                 specs->typespec_word = cts_bool;
7744               return specs;
7745             case RID_CHAR:
7746               if (specs->long_p)
7747                 error ("both %<long%> and %<char%> in "
7748                        "declaration specifiers");
7749               else if (specs->short_p)
7750                 error ("both %<short%> and %<char%> in "
7751                        "declaration specifiers");
7752               else if (specs->saturating_p)
7753                 error ("both %<_Sat%> and %<char%> in "
7754                        "declaration specifiers");
7755               else
7756                 specs->typespec_word = cts_char;
7757               return specs;
7758             case RID_INT:
7759               if (specs->saturating_p)
7760                 error ("both %<_Sat%> and %<int%> in "
7761                        "declaration specifiers");
7762               else
7763                 specs->typespec_word = cts_int;
7764               return specs;
7765             case RID_FLOAT:
7766               if (specs->long_p)
7767                 error ("both %<long%> and %<float%> in "
7768                        "declaration specifiers");
7769               else if (specs->short_p)
7770                 error ("both %<short%> and %<float%> in "
7771                        "declaration specifiers");
7772               else if (specs->signed_p)
7773                 error ("both %<signed%> and %<float%> in "
7774                        "declaration specifiers");
7775               else if (specs->unsigned_p)
7776                 error ("both %<unsigned%> and %<float%> in "
7777                        "declaration specifiers");
7778               else if (specs->saturating_p)
7779                 error ("both %<_Sat%> and %<float%> in "
7780                        "declaration specifiers");
7781               else
7782                 specs->typespec_word = cts_float;
7783               return specs;
7784             case RID_DOUBLE:
7785               if (specs->long_long_p)
7786                 error ("both %<long long%> and %<double%> in "
7787                        "declaration specifiers");
7788               else if (specs->short_p)
7789                 error ("both %<short%> and %<double%> in "
7790                        "declaration specifiers");
7791               else if (specs->signed_p)
7792                 error ("both %<signed%> and %<double%> in "
7793                        "declaration specifiers");
7794               else if (specs->unsigned_p)
7795                 error ("both %<unsigned%> and %<double%> in "
7796                        "declaration specifiers");
7797               else if (specs->saturating_p)
7798                 error ("both %<_Sat%> and %<double%> in "
7799                        "declaration specifiers");
7800               else
7801                 specs->typespec_word = cts_double;
7802               return specs;
7803             case RID_DFLOAT32:
7804             case RID_DFLOAT64:
7805             case RID_DFLOAT128:
7806               { 
7807                 const char *str;
7808                 if (i == RID_DFLOAT32)
7809                   str = "_Decimal32";
7810                 else if (i == RID_DFLOAT64)
7811                   str = "_Decimal64";
7812                 else
7813                   str = "_Decimal128";
7814                 if (specs->long_long_p)
7815                   error ("both %<long long%> and %<%s%> in "
7816                          "declaration specifiers", str);
7817                 if (specs->long_p)
7818                   error ("both %<long%> and %<%s%> in "
7819                          "declaration specifiers", str);
7820                 else if (specs->short_p)
7821                   error ("both %<short%> and %<%s%> in "
7822                          "declaration specifiers", str);
7823                 else if (specs->signed_p)
7824                   error ("both %<signed%> and %<%s%> in "
7825                          "declaration specifiers", str);
7826                 else if (specs->unsigned_p)
7827                   error ("both %<unsigned%> and %<%s%> in "
7828                          "declaration specifiers", str);
7829                 else if (specs->complex_p)
7830                   error ("both %<complex%> and %<%s%> in "
7831                          "declaration specifiers", str);
7832                 else if (specs->saturating_p)
7833                   error ("both %<_Sat%> and %<%s%> in "
7834                          "declaration specifiers", str);
7835                 else if (i == RID_DFLOAT32)
7836                   specs->typespec_word = cts_dfloat32;
7837                 else if (i == RID_DFLOAT64)
7838                   specs->typespec_word = cts_dfloat64;
7839                 else
7840                   specs->typespec_word = cts_dfloat128;
7841               }
7842               if (!targetm.decimal_float_supported_p ())
7843                 error ("decimal floating point not supported for this target");
7844               pedwarn (input_location, OPT_pedantic, 
7845                        "ISO C does not support decimal floating point");
7846               return specs;
7847             case RID_FRACT:
7848             case RID_ACCUM:
7849               {
7850                 const char *str;
7851                 if (i == RID_FRACT)
7852                   str = "_Fract";
7853                 else
7854                   str = "_Accum";
7855                 if (specs->complex_p)
7856                   error ("both %<complex%> and %<%s%> in "
7857                          "declaration specifiers", str);
7858                 else if (i == RID_FRACT)
7859                     specs->typespec_word = cts_fract;
7860                 else
7861                     specs->typespec_word = cts_accum;
7862               }
7863               if (!targetm.fixed_point_supported_p ())
7864                 error ("fixed-point types not supported for this target");
7865               pedwarn (input_location, OPT_pedantic, 
7866                        "ISO C does not support fixed-point types");
7867               return specs;
7868             default:
7869               /* ObjC reserved word "id", handled below.  */
7870               break;
7871             }
7872         }
7873     }
7874
7875   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7876      form of ObjC type, cases such as "int" and "long" being handled
7877      above), a TYPE (struct, union, enum and typeof specifiers) or an
7878      ERROR_MARK.  In none of these cases may there have previously
7879      been any type specifiers.  */
7880   if (specs->type || specs->typespec_word != cts_none
7881       || specs->long_p || specs->short_p || specs->signed_p
7882       || specs->unsigned_p || specs->complex_p)
7883     error ("two or more data types in declaration specifiers");
7884   else if (TREE_CODE (type) == TYPE_DECL)
7885     {
7886       if (TREE_TYPE (type) == error_mark_node)
7887         ; /* Allow the type to default to int to avoid cascading errors.  */
7888       else
7889         {
7890           specs->type = TREE_TYPE (type);
7891           specs->decl_attr = DECL_ATTRIBUTES (type);
7892           specs->typedef_p = true;
7893           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7894         }
7895     }
7896   else if (TREE_CODE (type) == IDENTIFIER_NODE)
7897     {
7898       tree t = lookup_name (type);
7899       if (!t || TREE_CODE (t) != TYPE_DECL)
7900         error ("%qE fails to be a typedef or built in type", type);
7901       else if (TREE_TYPE (t) == error_mark_node)
7902         ;
7903       else
7904         specs->type = TREE_TYPE (t);
7905     }
7906   else if (TREE_CODE (type) != ERROR_MARK)
7907     {
7908       if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7909         specs->tag_defined_p = true;
7910       if (spec.kind == ctsk_typeof)
7911         {
7912           specs->typedef_p = true;
7913           if (spec.expr)
7914             {
7915               if (specs->expr)
7916                 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7917                                       specs->expr, spec.expr);
7918               else
7919                 specs->expr = spec.expr;
7920               specs->expr_const_operands &= spec.expr_const_operands;
7921             }
7922         }
7923       specs->type = type;
7924     }
7925
7926   return specs;
7927 }
7928
7929 /* Add the storage class specifier or function specifier SCSPEC to the
7930    declaration specifiers SPECS, returning SPECS.  */
7931
7932 struct c_declspecs *
7933 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7934 {
7935   enum rid i;
7936   enum c_storage_class n = csc_none;
7937   bool dupe = false;
7938   specs->declspecs_seen_p = true;
7939   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7940               && C_IS_RESERVED_WORD (scspec));
7941   i = C_RID_CODE (scspec);
7942   if (specs->non_sc_seen_p)
7943     warning (OPT_Wold_style_declaration, 
7944              "%qE is not at beginning of declaration", scspec);
7945   switch (i)
7946     {
7947     case RID_INLINE:
7948       /* C99 permits duplicate inline.  Although of doubtful utility,
7949          it seems simplest to permit it in gnu89 mode as well, as
7950          there is also little utility in maintaining this as a
7951          difference between gnu89 and C99 inline.  */
7952       dupe = false;
7953       specs->inline_p = true;
7954       break;
7955     case RID_THREAD:
7956       dupe = specs->thread_p;
7957       if (specs->storage_class == csc_auto)
7958         error ("%<__thread%> used with %<auto%>");
7959       else if (specs->storage_class == csc_register)
7960         error ("%<__thread%> used with %<register%>");
7961       else if (specs->storage_class == csc_typedef)
7962         error ("%<__thread%> used with %<typedef%>");
7963       else
7964         specs->thread_p = true;
7965       break;
7966     case RID_AUTO:
7967       n = csc_auto;
7968       break;
7969     case RID_EXTERN:
7970       n = csc_extern;
7971       /* Diagnose "__thread extern".  */
7972       if (specs->thread_p)
7973         error ("%<__thread%> before %<extern%>");
7974       break;
7975     case RID_REGISTER:
7976       n = csc_register;
7977       break;
7978     case RID_STATIC:
7979       n = csc_static;
7980       /* Diagnose "__thread static".  */
7981       if (specs->thread_p)
7982         error ("%<__thread%> before %<static%>");
7983       break;
7984     case RID_TYPEDEF:
7985       n = csc_typedef;
7986       break;
7987     default:
7988       gcc_unreachable ();
7989     }
7990   if (n != csc_none && n == specs->storage_class)
7991     dupe = true;
7992   if (dupe)
7993     error ("duplicate %qE", scspec);
7994   if (n != csc_none)
7995     {
7996       if (specs->storage_class != csc_none && n != specs->storage_class)
7997         {
7998           error ("multiple storage classes in declaration specifiers");
7999         }
8000       else
8001         {
8002           specs->storage_class = n;
8003           if (n != csc_extern && n != csc_static && specs->thread_p)
8004             {
8005               error ("%<__thread%> used with %qE", scspec);
8006               specs->thread_p = false;
8007             }
8008         }
8009     }
8010   return specs;
8011 }
8012
8013 /* Add the attributes ATTRS to the declaration specifiers SPECS,
8014    returning SPECS.  */
8015
8016 struct c_declspecs *
8017 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
8018 {
8019   specs->attrs = chainon (attrs, specs->attrs);
8020   specs->declspecs_seen_p = true;
8021   return specs;
8022 }
8023
8024 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
8025    specifiers with any other type specifier to determine the resulting
8026    type.  This is where ISO C checks on complex types are made, since
8027    "_Complex long" is a prefix of the valid ISO C type "_Complex long
8028    double".  */
8029
8030 struct c_declspecs *
8031 finish_declspecs (struct c_declspecs *specs)
8032 {
8033   /* If a type was specified as a whole, we have no modifiers and are
8034      done.  */
8035   if (specs->type != NULL_TREE)
8036     {
8037       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8038                   && !specs->signed_p && !specs->unsigned_p
8039                   && !specs->complex_p);
8040       return specs;
8041     }
8042
8043   /* If none of "void", "_Bool", "char", "int", "float" or "double"
8044      has been specified, treat it as "int" unless "_Complex" is
8045      present and there are no other specifiers.  If we just have
8046      "_Complex", it is equivalent to "_Complex double", but e.g.
8047      "_Complex short" is equivalent to "_Complex short int".  */
8048   if (specs->typespec_word == cts_none)
8049     {
8050       if (specs->saturating_p)
8051         {
8052           error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
8053           if (!targetm.fixed_point_supported_p ())
8054             error ("fixed-point types not supported for this target");
8055           specs->typespec_word = cts_fract;
8056         }
8057       else if (specs->long_p || specs->short_p
8058                || specs->signed_p || specs->unsigned_p)
8059         {
8060           specs->typespec_word = cts_int;
8061         }
8062       else if (specs->complex_p)
8063         {
8064           specs->typespec_word = cts_double;
8065           pedwarn (input_location, OPT_pedantic, 
8066                    "ISO C does not support plain %<complex%> meaning "
8067                    "%<double complex%>");
8068         }
8069       else
8070         {
8071           specs->typespec_word = cts_int;
8072           specs->default_int_p = true;
8073           /* We don't diagnose this here because grokdeclarator will
8074              give more specific diagnostics according to whether it is
8075              a function definition.  */
8076         }
8077     }
8078
8079   /* If "signed" was specified, record this to distinguish "int" and
8080      "signed int" in the case of a bit-field with
8081      -funsigned-bitfields.  */
8082   specs->explicit_signed_p = specs->signed_p;
8083
8084   /* Now compute the actual type.  */
8085   switch (specs->typespec_word)
8086     {
8087     case cts_void:
8088       gcc_assert (!specs->long_p && !specs->short_p
8089                   && !specs->signed_p && !specs->unsigned_p
8090                   && !specs->complex_p);
8091       specs->type = void_type_node;
8092       break;
8093     case cts_bool:
8094       gcc_assert (!specs->long_p && !specs->short_p
8095                   && !specs->signed_p && !specs->unsigned_p
8096                   && !specs->complex_p);
8097       specs->type = boolean_type_node;
8098       break;
8099     case cts_char:
8100       gcc_assert (!specs->long_p && !specs->short_p);
8101       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8102       if (specs->signed_p)
8103         specs->type = signed_char_type_node;
8104       else if (specs->unsigned_p)
8105         specs->type = unsigned_char_type_node;
8106       else
8107         specs->type = char_type_node;
8108       if (specs->complex_p)
8109         {
8110           pedwarn (input_location, OPT_pedantic, 
8111                    "ISO C does not support complex integer types");
8112           specs->type = build_complex_type (specs->type);
8113         }
8114       break;
8115     case cts_int:
8116       gcc_assert (!(specs->long_p && specs->short_p));
8117       gcc_assert (!(specs->signed_p && specs->unsigned_p));
8118       if (specs->long_long_p)
8119         specs->type = (specs->unsigned_p
8120                        ? long_long_unsigned_type_node
8121                        : long_long_integer_type_node);
8122       else if (specs->long_p)
8123         specs->type = (specs->unsigned_p
8124                        ? long_unsigned_type_node
8125                        : long_integer_type_node);
8126       else if (specs->short_p)
8127         specs->type = (specs->unsigned_p
8128                        ? short_unsigned_type_node
8129                        : short_integer_type_node);
8130       else
8131         specs->type = (specs->unsigned_p
8132                        ? unsigned_type_node
8133                        : integer_type_node);
8134       if (specs->complex_p)
8135         {
8136           pedwarn (input_location, OPT_pedantic, 
8137                    "ISO C does not support complex integer types");
8138           specs->type = build_complex_type (specs->type);
8139         }
8140       break;
8141     case cts_float:
8142       gcc_assert (!specs->long_p && !specs->short_p
8143                   && !specs->signed_p && !specs->unsigned_p);
8144       specs->type = (specs->complex_p
8145                      ? complex_float_type_node
8146                      : float_type_node);
8147       break;
8148     case cts_double:
8149       gcc_assert (!specs->long_long_p && !specs->short_p
8150                   && !specs->signed_p && !specs->unsigned_p);
8151       if (specs->long_p)
8152         {
8153           specs->type = (specs->complex_p
8154                          ? complex_long_double_type_node
8155                          : long_double_type_node);
8156         }
8157       else
8158         {
8159           specs->type = (specs->complex_p
8160                          ? complex_double_type_node
8161                          : double_type_node);
8162         }
8163       break;
8164     case cts_dfloat32:
8165     case cts_dfloat64:
8166     case cts_dfloat128:
8167       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8168                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8169       if (specs->typespec_word == cts_dfloat32)
8170         specs->type = dfloat32_type_node;
8171       else if (specs->typespec_word == cts_dfloat64)
8172         specs->type = dfloat64_type_node;
8173       else
8174         specs->type = dfloat128_type_node;
8175       break;
8176     case cts_fract:
8177       gcc_assert (!specs->complex_p);
8178       if (!targetm.fixed_point_supported_p ())
8179         specs->type = integer_type_node;
8180       else if (specs->saturating_p)
8181         {
8182           if (specs->long_long_p)
8183             specs->type = specs->unsigned_p
8184                           ? sat_unsigned_long_long_fract_type_node
8185                           : sat_long_long_fract_type_node;
8186           else if (specs->long_p)
8187             specs->type = specs->unsigned_p
8188                           ? sat_unsigned_long_fract_type_node
8189                           : sat_long_fract_type_node;
8190           else if (specs->short_p)
8191             specs->type = specs->unsigned_p
8192                           ? sat_unsigned_short_fract_type_node
8193                           : sat_short_fract_type_node;
8194           else
8195             specs->type = specs->unsigned_p
8196                           ? sat_unsigned_fract_type_node
8197                           : sat_fract_type_node;
8198         }
8199       else
8200         {
8201           if (specs->long_long_p)
8202             specs->type = specs->unsigned_p
8203                           ? unsigned_long_long_fract_type_node
8204                           : long_long_fract_type_node;
8205           else if (specs->long_p)
8206             specs->type = specs->unsigned_p
8207                           ? unsigned_long_fract_type_node
8208                           : long_fract_type_node;
8209           else if (specs->short_p)
8210             specs->type = specs->unsigned_p
8211                           ? unsigned_short_fract_type_node
8212                           : short_fract_type_node;
8213           else
8214             specs->type = specs->unsigned_p
8215                           ? unsigned_fract_type_node
8216                           : fract_type_node;
8217         }
8218       break;
8219     case cts_accum:
8220       gcc_assert (!specs->complex_p);
8221       if (!targetm.fixed_point_supported_p ())
8222         specs->type = integer_type_node;
8223       else if (specs->saturating_p)
8224         {
8225           if (specs->long_long_p)
8226             specs->type = specs->unsigned_p
8227                           ? sat_unsigned_long_long_accum_type_node
8228                           : sat_long_long_accum_type_node;
8229           else if (specs->long_p)
8230             specs->type = specs->unsigned_p
8231                           ? sat_unsigned_long_accum_type_node
8232                           : sat_long_accum_type_node;
8233           else if (specs->short_p)
8234             specs->type = specs->unsigned_p
8235                           ? sat_unsigned_short_accum_type_node
8236                           : sat_short_accum_type_node;
8237           else
8238             specs->type = specs->unsigned_p
8239                           ? sat_unsigned_accum_type_node
8240                           : sat_accum_type_node;
8241         }
8242       else
8243         {
8244           if (specs->long_long_p)
8245             specs->type = specs->unsigned_p
8246                           ? unsigned_long_long_accum_type_node
8247                           : long_long_accum_type_node;
8248           else if (specs->long_p)
8249             specs->type = specs->unsigned_p
8250                           ? unsigned_long_accum_type_node
8251                           : long_accum_type_node;
8252           else if (specs->short_p)
8253             specs->type = specs->unsigned_p
8254                           ? unsigned_short_accum_type_node
8255                           : short_accum_type_node;
8256           else
8257             specs->type = specs->unsigned_p
8258                           ? unsigned_accum_type_node
8259                           : accum_type_node;
8260         }
8261       break;
8262     default:
8263       gcc_unreachable ();
8264     }
8265
8266   return specs;
8267 }
8268
8269 /* A subroutine of c_write_global_declarations.  Perform final processing
8270    on one file scope's declarations (or the external scope's declarations),
8271    GLOBALS.  */
8272
8273 static void
8274 c_write_global_declarations_1 (tree globals)
8275 {
8276   tree decl;
8277   bool reconsider;
8278
8279   /* Process the decls in the order they were written.  */
8280   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8281     {
8282       /* Check for used but undefined static functions using the C
8283          standard's definition of "used", and set TREE_NO_WARNING so
8284          that check_global_declarations doesn't repeat the check.  */
8285       if (TREE_CODE (decl) == FUNCTION_DECL
8286           && DECL_INITIAL (decl) == 0
8287           && DECL_EXTERNAL (decl)
8288           && !TREE_PUBLIC (decl)
8289           && C_DECL_USED (decl))
8290         {
8291           pedwarn (input_location, 0, "%q+F used but never defined", decl);
8292           TREE_NO_WARNING (decl) = 1;
8293         }
8294
8295       wrapup_global_declaration_1 (decl);
8296     }
8297
8298   do
8299     {
8300       reconsider = false;
8301       for (decl = globals; decl; decl = TREE_CHAIN (decl))
8302         reconsider |= wrapup_global_declaration_2 (decl);
8303     }
8304   while (reconsider);
8305
8306   for (decl = globals; decl; decl = TREE_CHAIN (decl))
8307     check_global_declaration_1 (decl);
8308 }
8309
8310 /* A subroutine of c_write_global_declarations Emit debug information for each
8311    of the declarations in GLOBALS.  */
8312
8313 static void
8314 c_write_global_declarations_2 (tree globals)
8315 {
8316   tree decl;
8317
8318   for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8319     debug_hooks->global_decl (decl);
8320 }
8321
8322 /* Preserve the external declarations scope across a garbage collect.  */
8323 static GTY(()) tree ext_block;
8324
8325 void
8326 c_write_global_declarations (void)
8327 {
8328   tree t;
8329
8330   /* We don't want to do this if generating a PCH.  */
8331   if (pch_file)
8332     return;
8333
8334   /* Don't waste time on further processing if -fsyntax-only or we've
8335      encountered errors.  */
8336   if (flag_syntax_only || errorcount || sorrycount)
8337     return;
8338
8339   /* Close the external scope.  */
8340   ext_block = pop_scope ();
8341   external_scope = 0;
8342   gcc_assert (!current_scope);
8343
8344   if (ext_block)
8345     {
8346       tree tmp = BLOCK_VARS (ext_block);
8347       int flags;
8348       FILE * stream = dump_begin (TDI_tu, &flags);
8349       if (stream && tmp)
8350         {
8351           dump_node (tmp, flags & ~TDF_SLIM, stream);
8352           dump_end (TDI_tu, stream);
8353         }
8354     }
8355
8356   /* Process all file scopes in this compilation, and the external_scope,
8357      through wrapup_global_declarations and check_global_declarations.  */
8358   for (t = all_translation_units; t; t = TREE_CHAIN (t))
8359     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8360   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8361
8362   /* We're done parsing; proceed to optimize and emit assembly.
8363      FIXME: shouldn't be the front end's responsibility to call this.  */
8364   cgraph_optimize ();
8365
8366   /* After cgraph has had a chance to emit everything that's going to
8367      be emitted, output debug information for globals.  */
8368   if (errorcount == 0 && sorrycount == 0)
8369     {
8370       timevar_push (TV_SYMOUT);
8371       for (t = all_translation_units; t; t = TREE_CHAIN (t))
8372         c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8373       c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8374       timevar_pop (TV_SYMOUT);
8375     }
8376
8377   ext_block = NULL;
8378 }
8379
8380 #include "gt-c-decl.h"