OSDN Git Service

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