OSDN Git Service

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