OSDN Git Service

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