c: C2x storage class specifiers in compound literals
[platform/upstream/gcc.git] / gcc / c / c-decl.cc
1 /* Process declarations and variables for C compiler.
2    Copyright (C) 1988-2022 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 /* Process declarations and symbol lookup for C front end.
21    Also constructs types; the standard scalar types at initialization,
22    and structure, union, array and enum types when they are declared.  */
23
24 /* ??? not all decl nodes are given the most useful possible
25    line numbers.  For example, the CONST_DECLs for enum values.  */
26
27 #include "config.h"
28 #define INCLUDE_STRING
29 #define INCLUDE_MEMORY
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "function.h"
34 #include "c-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "intl.h"
39 #include "print-tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "attribs.h"
43 #include "toplev.h"
44 #include "debug.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "c-family/c-ubsan.h"
48 #include "c-lang.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
51 #include "dumpfile.h"
52 #include "plugin.h"
53 #include "c-family/c-ada-spec.h"
54 #include "builtins.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
57 #include "asan.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.h"
60 #include "c-family/c-spellcheck.h"
61 #include "context.h"  /* For 'g'.  */
62 #include "omp-general.h"
63 #include "omp-offload.h"  /* For offload_vars.  */
64
65 #include "tree-pretty-print.h"
66
67 /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
68 enum decl_context
69 { NORMAL,                       /* Ordinary declaration */
70   FUNCDEF,                      /* Function definition */
71   PARM,                         /* Declaration of parm before function body */
72   FIELD,                        /* Declaration inside struct or union */
73   TYPENAME};                    /* Typename (inside cast or sizeof)  */
74
75 /* States indicating how grokdeclarator() should handle declspecs marked
76    with __attribute__((deprecated)) or __attribute__((unavailable)).
77    An object declared as __attribute__((unavailable)) should suppress
78    any reports of being declared  with unavailable or deprecated items.
79    An object declared as __attribute__((deprecated)) should suppress
80    warnings of uses of other deprecated items.  */
81
82 enum deprecated_states {
83   DEPRECATED_NORMAL,
84   DEPRECATED_SUPPRESS,
85   UNAVAILABLE_DEPRECATED_SUPPRESS
86 };
87
88 \f
89 /* Nonzero if we have seen an invalid cross reference
90    to a struct, union, or enum, but not yet printed the message.  */
91 tree pending_invalid_xref;
92
93 /* File and line to appear in the eventual error message.  */
94 location_t pending_invalid_xref_location;
95
96 /* The file and line that the prototype came from if this is an
97    old-style definition; used for diagnostics in
98    store_parm_decls_oldstyle.  */
99
100 static location_t current_function_prototype_locus;
101
102 /* Whether this prototype was built-in.  */
103
104 static bool current_function_prototype_built_in;
105
106 /* The argument type information of this prototype.  */
107
108 static tree current_function_prototype_arg_types;
109
110 /* The argument information structure for the function currently being
111    defined.  */
112
113 static struct c_arg_info *current_function_arg_info;
114
115 /* The obstack on which parser and related data structures, which are
116    not live beyond their top-level declaration or definition, are
117    allocated.  */
118 struct obstack parser_obstack;
119
120 /* The current statement tree.  */
121
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
123
124 /* Zero if we are not in an iteration or switch statement, otherwise
125    a bitmask.  See bitmask definitions in c-tree.h.  */
126 unsigned char in_statement;
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 /* Vector of implicit "omp declare target" attributes to be added into
157    the attribute lists.  */
158 vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
159
160 /* If non-zero, we are inside of
161    #pragma omp begin assumes ... #pragma omp end assumes region.  */
162 int current_omp_begin_assumes;
163 \f
164 /* Each c_binding structure describes one binding of an identifier to
165    a decl.  All the decls in a scope - irrespective of namespace - are
166    chained together by the ->prev field, which (as the name implies)
167    runs in reverse order.  All the decls in a given namespace bound to
168    a given identifier are chained by the ->shadowed field, which runs
169    from inner to outer scopes.
170
171    The ->decl field usually points to a DECL node, but there are two
172    exceptions.  In the namespace of type tags, the bound entity is a
173    RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
174    identifier is encountered, it is bound to error_mark_node to
175    suppress further errors about that identifier in the current
176    function.
177
178    The ->u.type field stores the type of the declaration in this scope;
179    if NULL, the type is the type of the ->decl field.  This is only of
180    relevance for objects with external or internal linkage which may
181    be redeclared in inner scopes, forming composite types that only
182    persist for the duration of those scopes.  In the external scope,
183    this stores the composite of all the types declared for this
184    object, visible or not.  The ->inner_comp field (used only at file
185    scope) stores whether an incomplete array type at file scope was
186    completed at an inner scope to an array size other than 1.
187
188    The ->u.label field is used for labels.  It points to a structure
189    which stores additional information used for warnings.
190
191    The depth field is copied from the scope structure that holds this
192    decl.  It is used to preserve the proper ordering of the ->shadowed
193    field (see bind()) and also for a handful of special-case checks.
194    Finally, the invisible bit is true for a decl which should be
195    ignored for purposes of normal name lookup, and the nested bit is
196    true for a decl that's been bound a second time in an inner scope;
197    in all such cases, the binding in the outer scope will have its
198    invisible bit true.  */
199
200 struct GTY((chain_next ("%h.prev"))) c_binding {
201   union GTY(()) {               /* first so GTY desc can use decl */
202     tree GTY((tag ("0"))) type; /* the type in this scope */
203     struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
204   } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
205   tree decl;                    /* the decl bound */
206   tree id;                      /* the identifier it's bound to */
207   struct c_binding *prev;       /* the previous decl in this scope */
208   struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
209   unsigned int depth : 28;      /* depth of this scope */
210   BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
211   BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
212   BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
213   BOOL_BITFIELD in_struct : 1;  /* currently defined as struct field */
214   location_t locus;             /* location for nested bindings */
215 };
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
220
221 /* Each C symbol points to three linked lists of c_binding structures.
222    These describe the values of the identifier in the three different
223    namespaces defined by the language.  */
224
225 struct GTY(()) lang_identifier {
226   struct c_common_identifier common_id;
227   struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
228   struct c_binding *tag_binding;    /* struct/union/enum tags */
229   struct c_binding *label_binding;  /* labels */
230 };
231
232 /* Validate c-lang.cc's assumptions.  */
233 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
234 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
235
236 /* The binding oracle; see c-tree.h.  */
237 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
238
239 /* This flag is set on an identifier if we have previously asked the
240    binding oracle for this identifier's symbol binding.  */
241 #define I_SYMBOL_CHECKED(node) \
242   (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
243
244 static inline struct c_binding* *
245 i_symbol_binding (tree node)
246 {
247   struct lang_identifier *lid
248     = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
249
250   if (lid->symbol_binding == NULL
251       && c_binding_oracle != NULL
252       && !I_SYMBOL_CHECKED (node))
253     {
254       /* Set the "checked" flag first, to avoid infinite recursion
255          when the binding oracle calls back into gcc.  */
256       I_SYMBOL_CHECKED (node) = 1;
257       c_binding_oracle (C_ORACLE_SYMBOL, node);
258     }
259
260   return &lid->symbol_binding;
261 }
262
263 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
264
265 #define I_SYMBOL_DECL(node) \
266  (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
267
268 /* This flag is set on an identifier if we have previously asked the
269    binding oracle for this identifier's tag binding.  */
270 #define I_TAG_CHECKED(node) \
271   (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
272
273 static inline struct c_binding **
274 i_tag_binding (tree node)
275 {
276   struct lang_identifier *lid
277     = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
278
279   if (lid->tag_binding == NULL
280       && c_binding_oracle != NULL
281       && !I_TAG_CHECKED (node))
282     {
283       /* Set the "checked" flag first, to avoid infinite recursion
284          when the binding oracle calls back into gcc.  */
285       I_TAG_CHECKED (node) = 1;
286       c_binding_oracle (C_ORACLE_TAG, node);
287     }
288
289   return &lid->tag_binding;
290 }
291
292 #define I_TAG_BINDING(node) (*i_tag_binding (node))
293
294 #define I_TAG_DECL(node) \
295  (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
296
297 /* This flag is set on an identifier if we have previously asked the
298    binding oracle for this identifier's label binding.  */
299 #define I_LABEL_CHECKED(node) \
300   (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
301
302 static inline struct c_binding **
303 i_label_binding (tree node)
304 {
305   struct lang_identifier *lid
306     = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
307
308   if (lid->label_binding == NULL
309       && c_binding_oracle != NULL
310       && !I_LABEL_CHECKED (node))
311     {
312       /* Set the "checked" flag first, to avoid infinite recursion
313          when the binding oracle calls back into gcc.  */
314       I_LABEL_CHECKED (node) = 1;
315       c_binding_oracle (C_ORACLE_LABEL, node);
316     }
317
318   return &lid->label_binding;
319 }
320
321 #define I_LABEL_BINDING(node) (*i_label_binding (node))
322
323 #define I_LABEL_DECL(node) \
324  (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
325
326 /* The resulting tree type.  */
327
328 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
329        chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
330  {
331   union tree_node GTY ((tag ("0"),
332                         desc ("tree_node_structure (&%h)")))
333     generic;
334   struct lang_identifier GTY ((tag ("1"))) identifier;
335 };
336
337 /* Track bindings and other things that matter for goto warnings.  For
338    efficiency, we do not gather all the decls at the point of
339    definition.  Instead, we point into the bindings structure.  As
340    scopes are popped, we update these structures and gather the decls
341    that matter at that time.  */
342
343 struct GTY(()) c_spot_bindings {
344   /* The currently open scope which holds bindings defined when the
345      label was defined or the goto statement was found.  */
346   struct c_scope *scope;
347   /* The bindings in the scope field which were defined at the point
348      of the label or goto.  This lets us look at older or newer
349      bindings in the scope, as appropriate.  */
350   struct c_binding *bindings_in_scope;
351   /* The number of statement expressions that have started since this
352      label or goto statement was defined.  This is zero if we are at
353      the same statement expression level.  It is positive if we are in
354      a statement expression started since this spot.  It is negative
355      if this spot was in a statement expression and we have left
356      it.  */
357   int stmt_exprs;
358   /* Whether we started in a statement expression but are no longer in
359      it.  This is set to true if stmt_exprs ever goes negative.  */
360   bool left_stmt_expr;
361 };
362
363 /* This structure is used to keep track of bindings seen when a goto
364    statement is defined.  This is only used if we see the goto
365    statement before we see the label.  */
366
367 struct GTY(()) c_goto_bindings {
368   /* The location of the goto statement.  */
369   location_t loc;
370   /* The bindings of the goto statement.  */
371   struct c_spot_bindings goto_bindings;
372 };
373
374 typedef struct c_goto_bindings *c_goto_bindings_p;
375
376 /* The additional information we keep track of for a label binding.
377    These fields are updated as scopes are popped.  */
378
379 struct GTY(()) c_label_vars {
380   /* The shadowed c_label_vars, when one label shadows another (which
381      can only happen using a __label__ declaration).  */
382   struct c_label_vars *shadowed;
383   /* The bindings when the label was defined.  */
384   struct c_spot_bindings label_bindings;
385   /* A list of decls that we care about: decls about which we should
386      warn if a goto branches to this label from later in the function.
387      Decls are added to this list as scopes are popped.  We only add
388      the decls that matter.  */
389   vec<tree, va_gc> *decls_in_scope;
390   /* A list of goto statements to this label.  This is only used for
391      goto statements seen before the label was defined, so that we can
392      issue appropriate warnings for them.  */
393   vec<c_goto_bindings_p, va_gc> *gotos;
394 };
395
396 /* Each c_scope structure describes the complete contents of one
397    scope.  Four scopes are distinguished specially: the innermost or
398    current scope, the innermost function scope, the file scope (always
399    the second to outermost) and the outermost or external scope.
400
401    Most declarations are recorded in the current scope.
402
403    All normal label declarations are recorded in the innermost
404    function scope, as are bindings of undeclared identifiers to
405    error_mark_node.  (GCC permits nested functions as an extension,
406    hence the 'innermost' qualifier.)  Explicitly declared labels
407    (using the __label__ extension) appear in the current scope.
408
409    Being in the file scope (current_scope == file_scope) causes
410    special behavior in several places below.  Also, under some
411    conditions the Objective-C front end records declarations in the
412    file scope even though that isn't the current scope.
413
414    All declarations with external linkage are recorded in the external
415    scope, even if they aren't visible there; this models the fact that
416    such declarations are visible to the entire program, and (with a
417    bit of cleverness, see pushdecl) allows diagnosis of some violations
418    of C99 6.2.2p7 and 6.2.7p2:
419
420      If, within the same translation unit, the same identifier appears
421      with both internal and external linkage, the behavior is
422      undefined.
423
424      All declarations that refer to the same object or function shall
425      have compatible type; otherwise, the behavior is undefined.
426
427    Initially only the built-in declarations, which describe compiler
428    intrinsic functions plus a subset of the standard library, are in
429    this scope.
430
431    The order of the blocks list matters, and it is frequently appended
432    to.  To avoid having to walk all the way to the end of the list on
433    each insertion, or reverse the list later, we maintain a pointer to
434    the last list entry.  (FIXME: It should be feasible to use a reversed
435    list here.)
436
437    The bindings list is strictly in reverse order of declarations;
438    pop_scope relies on this.  */
439
440
441 struct GTY((chain_next ("%h.outer"))) c_scope {
442   /* The scope containing this one.  */
443   struct c_scope *outer;
444
445   /* The next outermost function scope.  */
446   struct c_scope *outer_function;
447
448   /* All bindings in this scope.  */
449   struct c_binding *bindings;
450
451   /* For each scope (except the global one), a chain of BLOCK nodes
452      for all the scopes that were entered and exited one level down.  */
453   tree blocks;
454   tree blocks_last;
455
456   /* The depth of this scope.  Used to keep the ->shadowed chain of
457      bindings sorted innermost to outermost.  */
458   unsigned int depth : 28;
459
460   /* True if we are currently filling this scope with parameter
461      declarations.  */
462   BOOL_BITFIELD parm_flag : 1;
463
464   /* True if we saw [*] in this scope.  Used to give an error messages
465      if these appears in a function definition.  */
466   BOOL_BITFIELD had_vla_unspec : 1;
467
468   /* True if we already complained about forward parameter decls
469      in this scope.  This prevents double warnings on
470      foo (int a; int b; ...)  */
471   BOOL_BITFIELD warned_forward_parm_decls : 1;
472
473   /* True if this is the outermost block scope of a function body.
474      This scope contains the parameters, the local variables declared
475      in the outermost block, and all the labels (except those in
476      nested functions, or declared at block scope with __label__).  */
477   BOOL_BITFIELD function_body : 1;
478
479   /* True means make a BLOCK for this scope no matter what.  */
480   BOOL_BITFIELD keep : 1;
481
482   /* True means that an unsuffixed float constant is _Decimal64.  */
483   BOOL_BITFIELD float_const_decimal64 : 1;
484
485   /* True if this scope has any label bindings.  This is used to speed
486      up searching for labels when popping scopes, particularly since
487      labels are normally only found at function scope.  */
488   BOOL_BITFIELD has_label_bindings : 1;
489
490   /* True if we should issue a warning if a goto statement crosses any
491      of the bindings.  We still need to check the list of bindings to
492      find the specific ones we need to warn about.  This is true if
493      decl_jump_unsafe would return true for any of the bindings.  This
494      is used to avoid looping over all the bindings unnecessarily.  */
495   BOOL_BITFIELD has_jump_unsafe_decl : 1;
496 };
497
498 /* The scope currently in effect.  */
499
500 static GTY(()) struct c_scope *current_scope;
501
502 /* The innermost function scope.  Ordinary (not explicitly declared)
503    labels, bindings to error_mark_node, and the lazily-created
504    bindings of __func__ and its friends get this scope.  */
505
506 static GTY(()) struct c_scope *current_function_scope;
507
508 /* The C file scope.  This is reset for each input translation unit.  */
509
510 static GTY(()) struct c_scope *file_scope;
511
512 /* The outermost scope.  This is used for all declarations with
513    external linkage, and only these, hence the name.  */
514
515 static GTY(()) struct c_scope *external_scope;
516
517 /* A chain of c_scope structures awaiting reuse.  */
518
519 static GTY((deletable)) struct c_scope *scope_freelist;
520
521 /* A chain of c_binding structures awaiting reuse.  */
522
523 static GTY((deletable)) struct c_binding *binding_freelist;
524
525 /* Append VAR to LIST in scope SCOPE.  */
526 #define SCOPE_LIST_APPEND(scope, list, decl) do {       \
527   struct c_scope *s_ = (scope);                         \
528   tree d_ = (decl);                                     \
529   if (s_->list##_last)                                  \
530     BLOCK_CHAIN (s_->list##_last) = d_;                 \
531   else                                                  \
532     s_->list = d_;                                      \
533   s_->list##_last = d_;                                 \
534 } while (0)
535
536 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
537 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
538   struct c_scope *t_ = (tscope);                                \
539   struct c_scope *f_ = (fscope);                                \
540   if (t_->to##_last)                                            \
541     BLOCK_CHAIN (t_->to##_last) = f_->from;                     \
542   else                                                          \
543     t_->to = f_->from;                                          \
544   t_->to##_last = f_->from##_last;                              \
545 } while (0)
546
547 /* A c_inline_static structure stores details of a static identifier
548    referenced in a definition of a function that may be an inline
549    definition if no subsequent declaration of that function uses
550    "extern" or does not use "inline".  */
551
552 struct GTY((chain_next ("%h.next"))) c_inline_static {
553   /* The location for a diagnostic.  */
554   location_t location;
555
556   /* The function that may be an inline definition.  */
557   tree function;
558
559   /* The object or function referenced.  */
560   tree static_decl;
561
562   /* What sort of reference this is.  */
563   enum c_inline_static_type type;
564
565   /* The next such structure or NULL.  */
566   struct c_inline_static *next;
567 };
568
569 /* List of static identifiers used or referenced in functions that may
570    be inline definitions.  */
571 static GTY(()) struct c_inline_static *c_inline_statics;
572
573 /* True means unconditionally make a BLOCK for the next scope pushed.  */
574
575 static bool keep_next_level_flag;
576
577 /* True means the next call to push_scope will be the outermost scope
578    of a function body, so do not push a new scope, merely cease
579    expecting parameter decls.  */
580
581 static bool next_is_function_body;
582
583 /* A vector of pointers to c_binding structures.  */
584
585 typedef struct c_binding *c_binding_ptr;
586
587 /* Information that we keep for a struct or union while it is being
588    parsed.  */
589
590 class c_struct_parse_info
591 {
592 public:
593   /* If warn_cxx_compat, a list of types defined within this
594      struct.  */
595   auto_vec<tree> struct_types;
596   /* If warn_cxx_compat, a list of field names which have bindings,
597      and which are defined in this struct, but which are not defined
598      in any enclosing struct.  This is used to clear the in_struct
599      field of the c_bindings structure.  */
600   auto_vec<c_binding_ptr> fields;
601   /* If warn_cxx_compat, a list of typedef names used when defining
602      fields in this struct.  */
603   auto_vec<tree> typedefs_seen;
604 };
605
606 /* Information for the struct or union currently being parsed, or
607    NULL if not parsing a struct or union.  */
608 static class c_struct_parse_info *struct_parse_info;
609
610 /* Forward declarations.  */
611 static tree lookup_name_in_scope (tree, struct c_scope *);
612 static tree c_make_fname_decl (location_t, tree, int);
613 static tree grokdeclarator (const struct c_declarator *,
614                             struct c_declspecs *,
615                             enum decl_context, bool, tree *, tree *, tree *,
616                             bool *, enum deprecated_states);
617 static tree grokparms (struct c_arg_info *, bool);
618 static void layout_array_type (tree);
619 static void warn_defaults_to (location_t, int, const char *, ...)
620     ATTRIBUTE_GCC_DIAG(3,4);
621 static const char *header_for_builtin_fn (tree);
622 \f
623 /* T is a statement.  Add it to the statement-tree.  This is the
624    C/ObjC version--C++ has a slightly different version of this
625    function.  */
626
627 tree
628 add_stmt (tree t)
629 {
630   enum tree_code code = TREE_CODE (t);
631
632   if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
633     {
634       if (!EXPR_HAS_LOCATION (t))
635         SET_EXPR_LOCATION (t, input_location);
636     }
637
638   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
639     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
640
641   /* Add T to the statement-tree.  Non-side-effect statements need to be
642      recorded during statement expressions.  */
643   if (!building_stmt_list_p ())
644     push_stmt_list ();
645   append_to_statement_list_force (t, &cur_stmt_list);
646
647   return t;
648 }
649 \f
650 /* Build a pointer type using the default pointer mode.  */
651
652 static tree
653 c_build_pointer_type (tree to_type)
654 {
655   addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
656                                               : TYPE_ADDR_SPACE (to_type);
657   machine_mode pointer_mode;
658
659   if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
660     pointer_mode = targetm.addr_space.pointer_mode (as);
661   else
662     pointer_mode = c_default_pointer_mode;
663   return build_pointer_type_for_mode (to_type, pointer_mode, false);
664 }
665
666 \f
667 /* Return true if we will want to say something if a goto statement
668    crosses DECL.  */
669
670 static bool
671 decl_jump_unsafe (tree decl)
672 {
673   if (error_operand_p (decl))
674     return false;
675
676   /* Don't warn for compound literals.  If a goto statement crosses
677      their initialization, it should cross also all the places where
678      the complit is used or where the complit address might be saved
679      into some variable, so code after the label to which goto jumps
680      should not be able to refer to the compound literal.  */
681   if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
682     return false;
683
684   /* Always warn about crossing variably modified types.  */
685   if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
686       && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
687     return true;
688
689   /* Otherwise, only warn if -Wgoto-misses-init and this is an
690      initialized automatic decl.  */
691   if (warn_jump_misses_init
692       && VAR_P (decl)
693       && !TREE_STATIC (decl)
694       && DECL_INITIAL (decl) != NULL_TREE)
695     return true;
696
697   return false;
698 }
699 \f
700
701 void
702 c_print_identifier (FILE *file, tree node, int indent)
703 {
704   void (*save) (enum c_oracle_request, tree identifier);
705
706   /* Temporarily hide any binding oracle.  Without this, calls to
707      debug_tree from the debugger will end up calling into the oracle,
708      making for a confusing debug session.  As the oracle isn't needed
709      here for normal operation, it's simplest to suppress it.  */
710   save = c_binding_oracle;
711   c_binding_oracle = NULL;
712
713   print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
714   print_node (file, "tag", I_TAG_DECL (node), indent + 4);
715   print_node (file, "label", I_LABEL_DECL (node), indent + 4);
716   if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
717     {
718       tree rid = ridpointers[C_RID_CODE (node)];
719       indent_to (file, indent + 4);
720       fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
721                (void *) rid, IDENTIFIER_POINTER (rid));
722     }
723
724   c_binding_oracle = save;
725 }
726
727 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
728    which may be any of several kinds of DECL or TYPE or error_mark_node,
729    in the scope SCOPE.  */
730 static void
731 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
732       bool nested, location_t locus)
733 {
734   struct c_binding *b, **here;
735
736   if (binding_freelist)
737     {
738       b = binding_freelist;
739       binding_freelist = b->prev;
740     }
741   else
742     b = ggc_alloc<c_binding> ();
743
744   b->shadowed = 0;
745   b->decl = decl;
746   b->id = name;
747   b->depth = scope->depth;
748   b->invisible = invisible;
749   b->nested = nested;
750   b->inner_comp = 0;
751   b->in_struct = 0;
752   b->locus = locus;
753
754   b->u.type = NULL;
755
756   b->prev = scope->bindings;
757   scope->bindings = b;
758
759   if (decl_jump_unsafe (decl))
760     scope->has_jump_unsafe_decl = 1;
761
762   if (!name)
763     return;
764
765   switch (TREE_CODE (decl))
766     {
767     case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
768     case ENUMERAL_TYPE:
769     case UNION_TYPE:
770     case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
771     case VAR_DECL:
772     case FUNCTION_DECL:
773     case TYPE_DECL:
774     case CONST_DECL:
775     case PARM_DECL:
776     case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
777
778     default:
779       gcc_unreachable ();
780     }
781
782   /* Locate the appropriate place in the chain of shadowed decls
783      to insert this binding.  Normally, scope == current_scope and
784      this does nothing.  */
785   while (*here && (*here)->depth > scope->depth)
786     here = &(*here)->shadowed;
787
788   b->shadowed = *here;
789   *here = b;
790 }
791
792 /* Clear the binding structure B, stick it on the binding_freelist,
793    and return the former value of b->prev.  This is used by pop_scope
794    and get_parm_info to iterate destructively over all the bindings
795    from a given scope.  */
796 static struct c_binding *
797 free_binding_and_advance (struct c_binding *b)
798 {
799   struct c_binding *prev = b->prev;
800
801   memset (b, 0, sizeof (struct c_binding));
802   b->prev = binding_freelist;
803   binding_freelist = b;
804
805   return prev;
806 }
807
808 /* Bind a label.  Like bind, but skip fields which aren't used for
809    labels, and add the LABEL_VARS value.  */
810 static void
811 bind_label (tree name, tree label, struct c_scope *scope,
812             struct c_label_vars *label_vars)
813 {
814   struct c_binding *b;
815
816   bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
817         UNKNOWN_LOCATION);
818
819   scope->has_label_bindings = true;
820
821   b = scope->bindings;
822   gcc_assert (b->decl == label);
823   label_vars->shadowed = b->u.label;
824   b->u.label = label_vars;
825 }
826 \f
827 /* Hook called at end of compilation to assume 1 elt
828    for a file-scope tentative array defn that wasn't complete before.  */
829
830 void
831 c_finish_incomplete_decl (tree decl)
832 {
833   if (VAR_P (decl))
834     {
835       tree type = TREE_TYPE (decl);
836       if (type != error_mark_node
837           && TREE_CODE (type) == ARRAY_TYPE
838           && !DECL_EXTERNAL (decl)
839           && TYPE_DOMAIN (type) == NULL_TREE)
840         {
841           warning_at (DECL_SOURCE_LOCATION (decl),
842                       0, "array %q+D assumed to have one element", decl);
843
844           complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
845
846           relayout_decl (decl);
847         }
848     }
849 }
850 \f
851 /* Record that inline function FUNC contains a reference (location
852    LOC) to static DECL (file-scope or function-local according to
853    TYPE).  */
854
855 void
856 record_inline_static (location_t loc, tree func, tree decl,
857                       enum c_inline_static_type type)
858 {
859   c_inline_static *csi = ggc_alloc<c_inline_static> ();
860   csi->location = loc;
861   csi->function = func;
862   csi->static_decl = decl;
863   csi->type = type;
864   csi->next = c_inline_statics;
865   c_inline_statics = csi;
866 }
867
868 /* Check for references to static declarations in inline functions at
869    the end of the translation unit and diagnose them if the functions
870    are still inline definitions.  */
871
872 static void
873 check_inline_statics (void)
874 {
875   struct c_inline_static *csi;
876   for (csi = c_inline_statics; csi; csi = csi->next)
877     {
878       if (DECL_EXTERNAL (csi->function))
879         switch (csi->type)
880           {
881           case csi_internal:
882             pedwarn (csi->location, 0,
883                      "%qD is static but used in inline function %qD "
884                      "which is not static", csi->static_decl, csi->function);
885             break;
886           case csi_modifiable:
887             pedwarn (csi->location, 0,
888                      "%q+D is static but declared in inline function %qD "
889                      "which is not static", csi->static_decl, csi->function);
890             break;
891           default:
892             gcc_unreachable ();
893           }
894     }
895   c_inline_statics = NULL;
896 }
897 \f
898 /* Fill in a c_spot_bindings structure.  If DEFINING is true, set it
899    for the current state, otherwise set it to uninitialized.  */
900
901 static void
902 set_spot_bindings (struct c_spot_bindings *p, bool defining)
903 {
904   if (defining)
905     {
906       p->scope = current_scope;
907       p->bindings_in_scope = current_scope->bindings;
908     }
909   else
910     {
911       p->scope = NULL;
912       p->bindings_in_scope = NULL;
913     }
914   p->stmt_exprs = 0;
915   p->left_stmt_expr = false;
916 }
917
918 /* Update spot bindings P as we pop out of SCOPE.  Return true if we
919    should push decls for a label.  */
920
921 static bool
922 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
923 {
924   if (p->scope != scope)
925     {
926       /* This label or goto is defined in some other scope, or it is a
927          label which is not yet defined.  There is nothing to
928          update.  */
929       return false;
930     }
931
932   /* Adjust the spot bindings to refer to the bindings already defined
933      in the enclosing scope.  */
934   p->scope = scope->outer;
935   p->bindings_in_scope = p->scope->bindings;
936
937   return true;
938 }
939 \f
940 /* The Objective-C front-end often needs to determine the current scope.  */
941
942 void *
943 objc_get_current_scope (void)
944 {
945   return current_scope;
946 }
947
948 /* The following function is used only by Objective-C.  It needs to live here
949    because it accesses the innards of c_scope.  */
950
951 void
952 objc_mark_locals_volatile (void *enclosing_blk)
953 {
954   struct c_scope *scope;
955   struct c_binding *b;
956
957   for (scope = current_scope;
958        scope && scope != enclosing_blk;
959        scope = scope->outer)
960     {
961       for (b = scope->bindings; b; b = b->prev)
962         objc_volatilize_decl (b->decl);
963
964       /* Do not climb up past the current function.  */
965       if (scope->function_body)
966         break;
967     }
968 }
969
970 /* Return true if we are in the global binding level.  */
971
972 bool
973 global_bindings_p (void)
974 {
975   return current_scope == file_scope;
976 }
977
978 /* Return true if we're declaring parameters in an old-style function
979    declaration.  */
980
981 bool
982 old_style_parameter_scope (void)
983 {
984   /* If processing parameters and there is no function statement list, we
985    * have an old-style function declaration.  */
986   return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
987 }
988
989 void
990 keep_next_level (void)
991 {
992   keep_next_level_flag = true;
993 }
994
995 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON.  */
996
997 void
998 set_float_const_decimal64 (void)
999 {
1000   current_scope->float_const_decimal64 = true;
1001 }
1002
1003 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma.  */
1004
1005 void
1006 clear_float_const_decimal64 (void)
1007 {
1008   current_scope->float_const_decimal64 = false;
1009 }
1010
1011 /* Return nonzero if an unsuffixed float constant is _Decimal64.  */
1012
1013 bool
1014 float_const_decimal64_p (void)
1015 {
1016   return current_scope->float_const_decimal64;
1017 }
1018
1019 /* Identify this scope as currently being filled with parameters.  */
1020
1021 void
1022 declare_parm_level (void)
1023 {
1024   current_scope->parm_flag = true;
1025 }
1026
1027 void
1028 push_scope (void)
1029 {
1030   if (next_is_function_body)
1031     {
1032       /* This is the transition from the parameters to the top level
1033          of the function body.  These are the same scope
1034          (C99 6.2.1p4,6) so we do not push another scope structure.
1035          next_is_function_body is set only by store_parm_decls, which
1036          in turn is called when and only when we are about to
1037          encounter the opening curly brace for the function body.
1038
1039          The outermost block of a function always gets a BLOCK node,
1040          because the debugging output routines expect that each
1041          function has at least one BLOCK.  */
1042       current_scope->parm_flag         = false;
1043       current_scope->function_body     = true;
1044       current_scope->keep              = true;
1045       current_scope->outer_function    = current_function_scope;
1046       current_function_scope           = current_scope;
1047
1048       keep_next_level_flag = false;
1049       next_is_function_body = false;
1050
1051       /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes.  */
1052       if (current_scope->outer)
1053         current_scope->float_const_decimal64
1054           = current_scope->outer->float_const_decimal64;
1055       else
1056         current_scope->float_const_decimal64 = false;
1057     }
1058   else
1059     {
1060       struct c_scope *scope;
1061       if (scope_freelist)
1062         {
1063           scope = scope_freelist;
1064           scope_freelist = scope->outer;
1065         }
1066       else
1067         scope = ggc_cleared_alloc<c_scope> ();
1068
1069       /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes.  */
1070       if (current_scope)
1071         scope->float_const_decimal64 = current_scope->float_const_decimal64;
1072       else
1073         scope->float_const_decimal64 = false;
1074
1075       scope->keep          = keep_next_level_flag;
1076       scope->outer         = current_scope;
1077       scope->depth         = current_scope ? (current_scope->depth + 1) : 0;
1078
1079       /* Check for scope depth overflow.  Unlikely (2^28 == 268,435,456) but
1080          possible.  */
1081       if (current_scope && scope->depth == 0)
1082         {
1083           scope->depth--;
1084           sorry ("GCC supports only %u nested scopes", scope->depth);
1085         }
1086
1087       current_scope        = scope;
1088       keep_next_level_flag = false;
1089     }
1090 }
1091
1092 /* This is called when we are leaving SCOPE.  For each label defined
1093    in SCOPE, add any appropriate decls to its decls_in_scope fields.
1094    These are the decls whose initialization will be skipped by a goto
1095    later in the function.  */
1096
1097 static void
1098 update_label_decls (struct c_scope *scope)
1099 {
1100   struct c_scope *s;
1101
1102   s = scope;
1103   while (s != NULL)
1104     {
1105       if (s->has_label_bindings)
1106         {
1107           struct c_binding *b;
1108
1109           for (b = s->bindings; b != NULL; b = b->prev)
1110             {
1111               struct c_label_vars *label_vars;
1112               struct c_binding *b1;
1113               bool hjud;
1114               unsigned int ix;
1115               struct c_goto_bindings *g;
1116
1117               if (TREE_CODE (b->decl) != LABEL_DECL)
1118                 continue;
1119               label_vars = b->u.label;
1120
1121               b1 = label_vars->label_bindings.bindings_in_scope;
1122               if (label_vars->label_bindings.scope == NULL)
1123                 hjud = false;
1124               else
1125                 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1126               if (update_spot_bindings (scope, &label_vars->label_bindings))
1127                 {
1128                   /* This label is defined in this scope.  */
1129                   if (hjud)
1130                     {
1131                       for (; b1 != NULL; b1 = b1->prev)
1132                         {
1133                           /* A goto from later in the function to this
1134                              label will never see the initialization
1135                              of B1, if any.  Save it to issue a
1136                              warning if needed.  */
1137                           if (decl_jump_unsafe (b1->decl))
1138                             vec_safe_push(label_vars->decls_in_scope, b1->decl);
1139                         }
1140                     }
1141                 }
1142
1143               /* Update the bindings of any goto statements associated
1144                  with this label.  */
1145               FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1146                 update_spot_bindings (scope, &g->goto_bindings);
1147             }
1148         }
1149
1150       /* Don't search beyond the current function.  */
1151       if (s == current_function_scope)
1152         break;
1153
1154       s = s->outer;
1155     }
1156 }
1157
1158 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT.  */
1159
1160 static void
1161 set_type_context (tree type, tree context)
1162 {
1163   for (type = TYPE_MAIN_VARIANT (type); type;
1164        type = TYPE_NEXT_VARIANT (type))
1165     TYPE_CONTEXT (type) = context;
1166 }
1167
1168 /* Exit a scope.  Restore the state of the identifier-decl mappings
1169    that were in effect when this scope was entered.  Return a BLOCK
1170    node containing all the DECLs in this scope that are of interest
1171    to debug info generation.  */
1172
1173 tree
1174 pop_scope (void)
1175 {
1176   struct c_scope *scope = current_scope;
1177   tree block, context, p;
1178   struct c_binding *b;
1179
1180   bool functionbody = scope->function_body;
1181   bool keep = functionbody || scope->keep || scope->bindings;
1182
1183   update_label_decls (scope);
1184
1185   /* If appropriate, create a BLOCK to record the decls for the life
1186      of this function.  */
1187   block = NULL_TREE;
1188   if (keep)
1189     {
1190       block = make_node (BLOCK);
1191       BLOCK_SUBBLOCKS (block) = scope->blocks;
1192       TREE_USED (block) = 1;
1193
1194       /* In each subblock, record that this is its superior.  */
1195       for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1196         BLOCK_SUPERCONTEXT (p) = block;
1197
1198       BLOCK_VARS (block) = NULL_TREE;
1199     }
1200
1201   /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1202      scope must be set so that they point to the appropriate
1203      construct, i.e.  either to the current FUNCTION_DECL node, or
1204      else to the BLOCK node we just constructed.
1205
1206      Note that for tagged types whose scope is just the formal
1207      parameter list for some function type specification, we can't
1208      properly set their TYPE_CONTEXTs here, because we don't have a
1209      pointer to the appropriate FUNCTION_TYPE node readily available
1210      to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
1211      type nodes get set in `grokdeclarator' as soon as we have created
1212      the FUNCTION_TYPE node which will represent the "scope" for these
1213      "parameter list local" tagged types.  */
1214   if (scope->function_body)
1215     context = current_function_decl;
1216   else if (scope == file_scope)
1217     {
1218       tree file_decl
1219         = build_translation_unit_decl (get_identifier (main_input_filename));
1220       context = file_decl;
1221       debug_hooks->register_main_translation_unit (file_decl);
1222     }
1223   else
1224     context = block;
1225
1226   /* Clear all bindings in this scope.  */
1227   for (b = scope->bindings; b; b = free_binding_and_advance (b))
1228     {
1229       p = b->decl;
1230       switch (TREE_CODE (p))
1231         {
1232         case LABEL_DECL:
1233           /* Warnings for unused labels, errors for undefined labels.  */
1234           if (TREE_USED (p) && !DECL_INITIAL (p))
1235             {
1236               error ("label %q+D used but not defined", p);
1237               DECL_INITIAL (p) = error_mark_node;
1238             }
1239           else
1240             warn_for_unused_label (p);
1241
1242           /* Labels go in BLOCK_VARS.  */
1243           DECL_CHAIN (p) = BLOCK_VARS (block);
1244           BLOCK_VARS (block) = p;
1245           gcc_assert (I_LABEL_BINDING (b->id) == b);
1246           I_LABEL_BINDING (b->id) = b->shadowed;
1247
1248           /* Also pop back to the shadowed label_vars.  */
1249           release_tree_vector (b->u.label->decls_in_scope);
1250           b->u.label = b->u.label->shadowed;
1251           break;
1252
1253         case ENUMERAL_TYPE:
1254         case UNION_TYPE:
1255         case RECORD_TYPE:
1256           set_type_context (p, context);
1257
1258           /* Types may not have tag-names, in which case the type
1259              appears in the bindings list with b->id NULL.  */
1260           if (b->id)
1261             {
1262               gcc_assert (I_TAG_BINDING (b->id) == b);
1263               I_TAG_BINDING (b->id) = b->shadowed;
1264             }
1265           break;
1266
1267         case FUNCTION_DECL:
1268           /* Propagate TREE_ADDRESSABLE from nested functions to their
1269              containing functions.  */
1270           if (!TREE_ASM_WRITTEN (p)
1271               && DECL_INITIAL (p) != NULL_TREE
1272               && TREE_ADDRESSABLE (p)
1273               && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1274               && DECL_ABSTRACT_ORIGIN (p) != p)
1275             TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1276           if (!TREE_PUBLIC (p)
1277               && !DECL_INITIAL (p)
1278               && !b->nested
1279               && scope != file_scope
1280               && scope != external_scope)
1281             {
1282               error ("nested function %q+D declared but never defined", p);
1283               undef_nested_function = true;
1284             }
1285           else if (DECL_DECLARED_INLINE_P (p)
1286                    && TREE_PUBLIC (p)
1287                    && !DECL_INITIAL (p))
1288             {
1289               /* C99 6.7.4p6: "a function with external linkage... declared
1290                  with an inline function specifier ... shall also be defined
1291                  in the same translation unit."  */
1292               if (!flag_gnu89_inline
1293                   && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1294                   && scope == external_scope)
1295                 pedwarn (input_location, 0,
1296                          "inline function %q+D declared but never defined", p);
1297               DECL_EXTERNAL (p) = 1;
1298             }
1299
1300           goto common_symbol;
1301
1302         case VAR_DECL:
1303           /* Warnings for unused variables.  */
1304           if ((!TREE_USED (p) || !DECL_READ_P (p))
1305               && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1306               && !DECL_IN_SYSTEM_HEADER (p)
1307               && DECL_NAME (p)
1308               && !DECL_ARTIFICIAL (p)
1309               && scope != file_scope
1310               && scope != external_scope)
1311             {
1312               if (!TREE_USED (p))
1313                 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1314               else if (DECL_CONTEXT (p) == current_function_decl)
1315                 warning_at (DECL_SOURCE_LOCATION (p),
1316                             OPT_Wunused_but_set_variable,
1317                             "variable %qD set but not used", p);
1318             }
1319
1320           if (b->inner_comp)
1321             {
1322               error ("type of array %q+D completed incompatibly with"
1323                      " implicit initialization", p);
1324             }
1325
1326           /* Fall through.  */
1327         case TYPE_DECL:
1328         case CONST_DECL:
1329         common_symbol:
1330           /* All of these go in BLOCK_VARS, but only if this is the
1331              binding in the home scope.  */
1332           if (!b->nested)
1333             {
1334               DECL_CHAIN (p) = BLOCK_VARS (block);
1335               BLOCK_VARS (block) = p;
1336             }
1337           else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1338             {
1339               /* For block local externs add a special
1340                  DECL_EXTERNAL decl for debug info generation.  */
1341               tree extp = copy_node (p);
1342
1343               DECL_EXTERNAL (extp) = 1;
1344               TREE_STATIC (extp) = 0;
1345               TREE_PUBLIC (extp) = 1;
1346               DECL_INITIAL (extp) = NULL_TREE;
1347               DECL_LANG_SPECIFIC (extp) = NULL;
1348               DECL_CONTEXT (extp) = current_function_decl;
1349               if (TREE_CODE (p) == FUNCTION_DECL)
1350                 {
1351                   DECL_RESULT (extp) = NULL_TREE;
1352                   DECL_SAVED_TREE (extp) = NULL_TREE;
1353                   DECL_STRUCT_FUNCTION (extp) = NULL;
1354                 }
1355               if (b->locus != UNKNOWN_LOCATION)
1356                 DECL_SOURCE_LOCATION (extp) = b->locus;
1357               DECL_CHAIN (extp) = BLOCK_VARS (block);
1358               BLOCK_VARS (block) = extp;
1359             }
1360           /* If this is the file scope set DECL_CONTEXT of each decl to
1361              the TRANSLATION_UNIT_DECL.  This makes same_translation_unit_p
1362              work.  */
1363           if (scope == file_scope)
1364             {
1365               DECL_CONTEXT (p) = context;
1366               if (TREE_CODE (p) == TYPE_DECL
1367                   && TREE_TYPE (p) != error_mark_node)
1368                 set_type_context (TREE_TYPE (p), context);
1369             }
1370
1371           gcc_fallthrough ();
1372           /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1373              already been put there by store_parm_decls.  Unused-
1374              parameter warnings are handled by function.cc.
1375              error_mark_node obviously does not go in BLOCK_VARS and
1376              does not get unused-variable warnings.  */
1377         case PARM_DECL:
1378         case ERROR_MARK:
1379           /* It is possible for a decl not to have a name.  We get
1380              here with b->id NULL in this case.  */
1381           if (b->id)
1382             {
1383               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1384               I_SYMBOL_BINDING (b->id) = b->shadowed;
1385               if (b->shadowed && b->shadowed->u.type)
1386                 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1387             }
1388           break;
1389
1390         default:
1391           gcc_unreachable ();
1392         }
1393     }
1394
1395
1396   /* Dispose of the block that we just made inside some higher level.  */
1397   if ((scope->function_body || scope == file_scope) && context)
1398     {
1399       DECL_INITIAL (context) = block;
1400       BLOCK_SUPERCONTEXT (block) = context;
1401     }
1402   else if (scope->outer)
1403     {
1404       if (block)
1405         SCOPE_LIST_APPEND (scope->outer, blocks, block);
1406       /* If we did not make a block for the scope just exited, any
1407          blocks made for inner scopes must be carried forward so they
1408          will later become subblocks of something else.  */
1409       else if (scope->blocks)
1410         SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1411     }
1412
1413   /* Pop the current scope, and free the structure for reuse.  */
1414   current_scope = scope->outer;
1415   if (scope->function_body)
1416     current_function_scope = scope->outer_function;
1417
1418   memset (scope, 0, sizeof (struct c_scope));
1419   scope->outer = scope_freelist;
1420   scope_freelist = scope;
1421
1422   return block;
1423 }
1424
1425 void
1426 push_file_scope (void)
1427 {
1428   tree decl;
1429
1430   if (file_scope)
1431     return;
1432
1433   push_scope ();
1434   file_scope = current_scope;
1435
1436   start_fname_decls ();
1437
1438   for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1439     bind (DECL_NAME (decl), decl, file_scope,
1440           /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1441 }
1442
1443 void
1444 pop_file_scope (void)
1445 {
1446   /* In case there were missing closebraces, get us back to the global
1447      binding level.  */
1448   while (current_scope != file_scope)
1449     pop_scope ();
1450
1451   /* __FUNCTION__ is defined at file scope ("").  This
1452      call may not be necessary as my tests indicate it
1453      still works without it.  */
1454   finish_fname_decls ();
1455
1456   check_inline_statics ();
1457
1458   /* This is the point to write out a PCH if we're doing that.
1459      In that case we do not want to do anything else.  */
1460   if (pch_file)
1461     {
1462       c_common_write_pch ();
1463       /* Ensure even the callers don't try to finalize the CU.  */
1464       flag_syntax_only = 1;
1465       return;
1466     }
1467
1468   /* Pop off the file scope and close this translation unit.  */
1469   pop_scope ();
1470   file_scope = 0;
1471
1472   maybe_apply_pending_pragma_weaks ();
1473 }
1474 \f
1475 /* Adjust the bindings for the start of a statement expression.  */
1476
1477 void
1478 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1479 {
1480   struct c_scope *scope;
1481
1482   for (scope = current_scope; scope != NULL; scope = scope->outer)
1483     {
1484       struct c_binding *b;
1485
1486       if (!scope->has_label_bindings)
1487         continue;
1488
1489       for (b = scope->bindings; b != NULL; b = b->prev)
1490         {
1491           struct c_label_vars *label_vars;
1492           unsigned int ix;
1493           struct c_goto_bindings *g;
1494
1495           if (TREE_CODE (b->decl) != LABEL_DECL)
1496             continue;
1497           label_vars = b->u.label;
1498           ++label_vars->label_bindings.stmt_exprs;
1499           FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1500             ++g->goto_bindings.stmt_exprs;
1501         }
1502     }
1503
1504   if (switch_bindings != NULL)
1505     ++switch_bindings->stmt_exprs;
1506 }
1507
1508 /* Adjust the bindings for the end of a statement expression.  */
1509
1510 void
1511 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1512 {
1513   struct c_scope *scope;
1514
1515   for (scope = current_scope; scope != NULL; scope = scope->outer)
1516     {
1517       struct c_binding *b;
1518
1519       if (!scope->has_label_bindings)
1520         continue;
1521
1522       for (b = scope->bindings; b != NULL; b = b->prev)
1523         {
1524           struct c_label_vars *label_vars;
1525           unsigned int ix;
1526           struct c_goto_bindings *g;
1527
1528           if (TREE_CODE (b->decl) != LABEL_DECL)
1529             continue;
1530           label_vars = b->u.label;
1531           --label_vars->label_bindings.stmt_exprs;
1532           if (label_vars->label_bindings.stmt_exprs < 0)
1533             {
1534               label_vars->label_bindings.left_stmt_expr = true;
1535               label_vars->label_bindings.stmt_exprs = 0;
1536             }
1537           FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1538             {
1539               --g->goto_bindings.stmt_exprs;
1540               if (g->goto_bindings.stmt_exprs < 0)
1541                 {
1542                   g->goto_bindings.left_stmt_expr = true;
1543                   g->goto_bindings.stmt_exprs = 0;
1544                 }
1545             }
1546         }
1547     }
1548
1549   if (switch_bindings != NULL)
1550     {
1551       --switch_bindings->stmt_exprs;
1552       gcc_assert (switch_bindings->stmt_exprs >= 0);
1553     }
1554 }
1555 \f
1556 /* Push a definition or a declaration of struct, union or enum tag "name".
1557    "type" should be the type node.
1558    We assume that the tag "name" is not already defined, and has a location
1559    of LOC.
1560
1561    Note that the definition may really be just a forward reference.
1562    In that case, the TYPE_SIZE will be zero.  */
1563
1564 static void
1565 pushtag (location_t loc, tree name, tree type)
1566 {
1567   /* Record the identifier as the type's name if it has none.  */
1568   if (name && !TYPE_NAME (type))
1569     TYPE_NAME (type) = name;
1570   bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1571
1572   /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1573      tagged type we just added to the current scope.  This fake
1574      NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1575      to output a representation of a tagged type, and it also gives
1576      us a convenient place to record the "scope start" address for the
1577      tagged type.  */
1578
1579   TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1580                                                 TYPE_DECL, NULL_TREE, type));
1581
1582   /* An approximation for now, so we can tell this is a function-scope tag.
1583      This will be updated in pop_scope.  */
1584   TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1585
1586   if (warn_cxx_compat && name != NULL_TREE)
1587     {
1588       struct c_binding *b = I_SYMBOL_BINDING (name);
1589
1590       if (b != NULL
1591           && b->decl != NULL_TREE
1592           && TREE_CODE (b->decl) == TYPE_DECL
1593           && (B_IN_CURRENT_SCOPE (b)
1594               || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1595           && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1596               != TYPE_MAIN_VARIANT (type)))
1597         {
1598           auto_diagnostic_group d;
1599           if (warning_at (loc, OPT_Wc___compat,
1600                           ("using %qD as both a typedef and a tag is "
1601                            "invalid in C++"), b->decl)
1602               && b->locus != UNKNOWN_LOCATION)
1603             inform (b->locus, "originally defined here");
1604         }
1605     }
1606 }
1607
1608 /* An exported interface to pushtag.  This is used by the gdb plugin's
1609    binding oracle to introduce a new tag binding.  */
1610
1611 void
1612 c_pushtag (location_t loc, tree name, tree type)
1613 {
1614   pushtag (loc, name, type);
1615 }
1616
1617 /* An exported interface to bind a declaration.  LOC is the location
1618    to use.  DECL is the declaration to bind.  The decl's name is used
1619    to determine how it is bound.  If DECL is a VAR_DECL, then
1620    IS_GLOBAL determines whether the decl is put into the global (file
1621    and external) scope or the current function's scope; if DECL is not
1622    a VAR_DECL then it is always put into the file scope.  */
1623
1624 void
1625 c_bind (location_t loc, tree decl, bool is_global)
1626 {
1627   struct c_scope *scope;
1628   bool nested = false;
1629
1630   if (!VAR_P (decl) || current_function_scope == NULL)
1631     {
1632       /* Types and functions are always considered to be global.  */
1633       scope = file_scope;
1634       DECL_EXTERNAL (decl) = 1;
1635       TREE_PUBLIC (decl) = 1;
1636     }
1637   else if (is_global)
1638     {
1639       /* Also bind it into the external scope.  */
1640       bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1641       nested = true;
1642       scope = file_scope;
1643       DECL_EXTERNAL (decl) = 1;
1644       TREE_PUBLIC (decl) = 1;
1645     }
1646   else
1647     {
1648       DECL_CONTEXT (decl) = current_function_decl;
1649       TREE_PUBLIC (decl) = 0;
1650       scope = current_function_scope;
1651     }
1652
1653   bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1654 }
1655 \f
1656
1657 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1658    it is) seen in a declaration of a file I/O etc. built-in, corresponding
1659    to the builtin_structptr_types array.  Subsequent declarations of such
1660    built-ins are expected to refer to it rather than to fileptr_type_node,
1661    etc. which is just void* (or to any other type).
1662    Used only by match_builtin_function_types.  */
1663
1664 static const unsigned builtin_structptr_type_count
1665   = ARRAY_SIZE (builtin_structptr_types);
1666
1667 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1668
1669 /* Returns true if types T1 and T2 representing return types or types
1670    of function arguments are close enough to be considered interchangeable
1671    in redeclarations of built-in functions.  */
1672
1673 static bool
1674 types_close_enough_to_match (tree t1, tree t2)
1675 {
1676   return (TYPE_MODE (t1) == TYPE_MODE (t2)
1677           && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1678           && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1679 }
1680
1681 /* Subroutine of compare_decls.  Allow harmless mismatches in return
1682    and argument types provided that the type modes match.  Set *STRICT
1683    and *ARGNO to the expected argument type and number in case of
1684    an argument type mismatch or null and zero otherwise.  Return
1685    a unified type given a suitable match, and 0 otherwise.  */
1686
1687 static tree
1688 match_builtin_function_types (tree newtype, tree oldtype,
1689                               tree *strict, unsigned *argno)
1690 {
1691   *argno = 0;
1692   *strict = NULL_TREE;
1693
1694   /* Accept the return type of the new declaration if it has the same
1695      mode and if they're both pointers or if neither is.  */
1696   tree oldrettype = TREE_TYPE (oldtype);
1697   tree newrettype = TREE_TYPE (newtype);
1698
1699   if (!types_close_enough_to_match (oldrettype, newrettype))
1700     return NULL_TREE;
1701
1702   /* Check that the return types are compatible but don't fail if they
1703      are not (e.g., int vs long in ILP32) and just let the caller know.  */
1704   if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1705                   TYPE_MAIN_VARIANT (newrettype)))
1706     *strict = oldrettype;
1707
1708   tree oldargs = TYPE_ARG_TYPES (oldtype);
1709   tree newargs = TYPE_ARG_TYPES (newtype);
1710   tree tryargs = newargs;
1711
1712   const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1713   const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1714
1715   gcc_checking_assert (nlst == nbst);
1716
1717   for (unsigned i = 1; oldargs || newargs; ++i)
1718     {
1719       if (!oldargs
1720           || !newargs
1721           || !TREE_VALUE (oldargs)
1722           || !TREE_VALUE (newargs))
1723         return NULL_TREE;
1724
1725       tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1726       tree newtype = TREE_VALUE (newargs);
1727       if (newtype == error_mark_node)
1728        return NULL_TREE;
1729       newtype = TYPE_MAIN_VARIANT (newtype);
1730
1731       if (!types_close_enough_to_match (oldtype, newtype))
1732         return NULL_TREE;
1733
1734       unsigned j = nbst;
1735       if (POINTER_TYPE_P (oldtype))
1736         /* Iterate over well-known struct types like FILE (whose types
1737            aren't known to us) and compare the pointer to each to
1738            the pointer argument.  */
1739         for (j = 0; j < nbst; ++j)
1740           {
1741             if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1742               continue;
1743             /* Store the first FILE* etc. argument type (whatever it is), and
1744                expect any subsequent declarations of file I/O etc. built-ins
1745                to refer to it rather than to fileptr_type_node etc. which is
1746                just void* (or const void*).  */
1747             if (last_structptr_types[j])
1748               {
1749                 if (!comptypes (last_structptr_types[j], newtype))
1750                   {
1751                     *argno = i;
1752                     *strict = last_structptr_types[j];
1753                   }
1754               }
1755             else
1756               last_structptr_types[j] = newtype;
1757             break;
1758           }
1759
1760       if (j == nbst && !comptypes (oldtype, newtype))
1761         {
1762           if (POINTER_TYPE_P (oldtype))
1763             {
1764               /* For incompatible pointers, only reject differences in
1765                  the unqualified variants of the referenced types but
1766                  consider differences in qualifiers as benign (report
1767                  those to caller via *STRICT below).  */
1768               tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1769               tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1770               if (!comptypes (oldref, newref))
1771                 return NULL_TREE;
1772             }
1773
1774           if (!*strict)
1775             {
1776               *argno = i;
1777               *strict = oldtype;
1778             }
1779         }
1780
1781       oldargs = TREE_CHAIN (oldargs);
1782       newargs = TREE_CHAIN (newargs);
1783     }
1784
1785   tree trytype = build_function_type (newrettype, tryargs);
1786
1787   /* Allow declaration to change transaction_safe attribute.  */
1788   tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1789   tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1790   tree newattrs = TYPE_ATTRIBUTES (newtype);
1791   tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1792   if (oldtsafe && !newtsafe)
1793     oldattrs = remove_attribute ("transaction_safe", oldattrs);
1794   else if (newtsafe && !oldtsafe)
1795     oldattrs = tree_cons (get_identifier ("transaction_safe"),
1796                           NULL_TREE, oldattrs);
1797
1798   return build_type_attribute_variant (trytype, oldattrs);
1799 }
1800
1801 /* Subroutine of diagnose_mismatched_decls.  Check for function type
1802    mismatch involving an empty arglist vs a nonempty one and give clearer
1803    diagnostics.  */
1804 static void
1805 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1806                            tree newtype, tree oldtype)
1807 {
1808   tree t;
1809
1810   if (TREE_CODE (olddecl) != FUNCTION_DECL
1811       || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1812       || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1813            || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1814     return;
1815
1816   t = TYPE_ARG_TYPES (oldtype);
1817   if (t == NULL_TREE)
1818     t = TYPE_ARG_TYPES (newtype);
1819   for (; t; t = TREE_CHAIN (t))
1820     {
1821       tree type = TREE_VALUE (t);
1822
1823       if (TREE_CHAIN (t) == NULL_TREE
1824           && TYPE_MAIN_VARIANT (type) != void_type_node)
1825         {
1826           inform (input_location, "a parameter list with an ellipsis "
1827                   "cannot match an empty parameter name list declaration");
1828           break;
1829         }
1830
1831       if (c_type_promotes_to (type) != type)
1832         {
1833           inform (input_location, "an argument type that has a default "
1834                   "promotion cannot match an empty parameter name list "
1835                   "declaration");
1836           break;
1837         }
1838     }
1839 }
1840
1841 /* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
1842    old-style function definition, NEWDECL is a prototype declaration.
1843    Diagnose inconsistencies in the argument list.  Returns TRUE if
1844    the prototype is compatible, FALSE if not.  */
1845 static bool
1846 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1847 {
1848   tree newargs, oldargs;
1849   int i;
1850
1851 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1852
1853   oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1854   newargs = TYPE_ARG_TYPES (newtype);
1855   i = 1;
1856
1857   for (;;)
1858     {
1859       tree oldargtype = TREE_VALUE (oldargs);
1860       tree newargtype = TREE_VALUE (newargs);
1861
1862       if (oldargtype == error_mark_node || newargtype == error_mark_node)
1863         return false;
1864
1865       oldargtype = (TYPE_ATOMIC (oldargtype)
1866                     ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1867                                               TYPE_QUAL_ATOMIC)
1868                     : TYPE_MAIN_VARIANT (oldargtype));
1869       newargtype = (TYPE_ATOMIC (newargtype)
1870                     ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1871                                               TYPE_QUAL_ATOMIC)
1872                     : TYPE_MAIN_VARIANT (newargtype));
1873
1874       if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1875         break;
1876
1877       /* Reaching the end of just one list means the two decls don't
1878          agree on the number of arguments.  */
1879       if (END_OF_ARGLIST (oldargtype))
1880         {
1881           error ("prototype for %q+D declares more arguments "
1882                  "than previous old-style definition", newdecl);
1883           return false;
1884         }
1885       else if (END_OF_ARGLIST (newargtype))
1886         {
1887           error ("prototype for %q+D declares fewer arguments "
1888                  "than previous old-style definition", newdecl);
1889           return false;
1890         }
1891
1892       /* Type for passing arg must be consistent with that declared
1893          for the arg.  */
1894       else if (!comptypes (oldargtype, newargtype))
1895         {
1896           error ("prototype for %q+D declares argument %d"
1897                  " with incompatible type",
1898                  newdecl, i);
1899           return false;
1900         }
1901
1902       oldargs = TREE_CHAIN (oldargs);
1903       newargs = TREE_CHAIN (newargs);
1904       i++;
1905     }
1906
1907   /* If we get here, no errors were found, but do issue a warning
1908      for this poor-style construct.  */
1909   warning (0, "prototype for %q+D follows non-prototype definition",
1910            newdecl);
1911   return true;
1912 #undef END_OF_ARGLIST
1913 }
1914
1915 /* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
1916    first in a pair of mismatched declarations, using the diagnostic
1917    function DIAG.  */
1918 static void
1919 locate_old_decl (tree decl)
1920 {
1921   if (TREE_CODE (decl) == FUNCTION_DECL
1922       && fndecl_built_in_p (decl)
1923       && !C_DECL_DECLARED_BUILTIN (decl))
1924     ;
1925   else if (DECL_INITIAL (decl))
1926     inform (input_location,
1927             "previous definition of %q+D with type %qT",
1928             decl, TREE_TYPE (decl));
1929   else if (C_DECL_IMPLICIT (decl))
1930     inform (input_location,
1931             "previous implicit declaration of %q+D with type %qT",
1932             decl, TREE_TYPE (decl));
1933   else
1934     inform (input_location,
1935             "previous declaration of %q+D with type %qT",
1936             decl, TREE_TYPE (decl));
1937 }
1938
1939 /* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
1940    Returns true if the caller should proceed to merge the two, false
1941    if OLDDECL should simply be discarded.  As a side effect, issues
1942    all necessary diagnostics for invalid or poor-style combinations.
1943    If it returns true, writes the types of NEWDECL and OLDDECL to
1944    *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1945    TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
1946
1947 static bool
1948 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1949                            tree *newtypep, tree *oldtypep)
1950 {
1951   tree newtype, oldtype;
1952   bool retval = true;
1953
1954 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL)  \
1955                                   && DECL_EXTERNAL (DECL))
1956
1957   /* If we have error_mark_node for either decl or type, just discard
1958      the previous decl - we're in an error cascade already.  */
1959   if (olddecl == error_mark_node || newdecl == error_mark_node)
1960     return false;
1961   *oldtypep = oldtype = TREE_TYPE (olddecl);
1962   *newtypep = newtype = TREE_TYPE (newdecl);
1963   if (oldtype == error_mark_node || newtype == error_mark_node)
1964     return false;
1965
1966   /* Two different categories of symbol altogether.  This is an error
1967      unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
1968   if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1969     {
1970       if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1971             && fndecl_built_in_p (olddecl)
1972             && !C_DECL_DECLARED_BUILTIN (olddecl)))
1973         {
1974           auto_diagnostic_group d;
1975           error ("%q+D redeclared as different kind of symbol", newdecl);
1976           locate_old_decl (olddecl);
1977         }
1978       else if (TREE_PUBLIC (newdecl))
1979         warning (OPT_Wbuiltin_declaration_mismatch,
1980                  "built-in function %q+D declared as non-function",
1981                  newdecl);
1982       else
1983         warning (OPT_Wshadow, "declaration of %q+D shadows "
1984                  "a built-in function", newdecl);
1985       return false;
1986     }
1987
1988   /* Enumerators have no linkage, so may only be declared once in a
1989      given scope.  */
1990   if (TREE_CODE (olddecl) == CONST_DECL)
1991     {
1992       auto_diagnostic_group d;
1993       error ("redeclaration of enumerator %q+D", newdecl);
1994       locate_old_decl (olddecl);
1995       return false;
1996     }
1997
1998   bool pedwarned = false;
1999   bool warned = false;
2000   bool enum_and_int_p = false;
2001   auto_diagnostic_group d;
2002
2003   int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2004                                                    &enum_and_int_p);
2005   if (!comptypes_result)
2006     {
2007       if (TREE_CODE (olddecl) == FUNCTION_DECL
2008           && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2009           && !C_DECL_DECLARED_BUILTIN (olddecl))
2010         {
2011           /* Accept "harmless" mismatches in function types such
2012              as missing qualifiers or int vs long when they're the same
2013              size.  However, diagnose return and argument types that are
2014              incompatible according to language rules.  */
2015           tree mismatch_expect;
2016           unsigned mismatch_argno;
2017
2018           tree trytype = match_builtin_function_types (newtype, oldtype,
2019                                                        &mismatch_expect,
2020                                                        &mismatch_argno);
2021
2022           if (trytype && comptypes (newtype, trytype))
2023             *oldtypep = oldtype = trytype;
2024           else
2025             {
2026               /* If types don't match for a built-in, throw away the
2027                  built-in.  No point in calling locate_old_decl here, it
2028                  won't print anything.  */
2029               const char *header = header_for_builtin_fn (olddecl);
2030               location_t loc = DECL_SOURCE_LOCATION (newdecl);
2031               if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2032                               "conflicting types for built-in function %q+D; "
2033                               "expected %qT",
2034                               newdecl, oldtype)
2035                   && header)
2036                 {
2037                   /* Suggest the right header to include as the preferred
2038                      solution rather than the spelling of the declaration.  */
2039                   rich_location richloc (line_table, loc);
2040                   maybe_add_include_fixit (&richloc, header, true);
2041                   inform (&richloc,
2042                           "%qD is declared in header %qs", olddecl, header);
2043                 }
2044               return false;
2045             }
2046
2047           if (mismatch_expect && extra_warnings)
2048             {
2049               location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2050               bool warned = false;
2051               if (mismatch_argno)
2052                 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2053                                      "mismatch in argument %u type of built-in "
2054                                      "function %qD; expected %qT",
2055                                      mismatch_argno, newdecl, mismatch_expect);
2056               else
2057                 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2058                                      "mismatch in return type of built-in "
2059                                      "function %qD; expected %qT",
2060                                      newdecl, mismatch_expect);
2061               const char *header = header_for_builtin_fn (olddecl);
2062               if (warned && header)
2063                 {
2064                   rich_location richloc (line_table, newloc);
2065                   maybe_add_include_fixit (&richloc, header, true);
2066                   inform (&richloc,
2067                           "%qD is declared in header %qs", olddecl, header);
2068                 }
2069             }
2070         }
2071       else if (TREE_CODE (olddecl) == FUNCTION_DECL
2072                && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2073         {
2074           /* A conflicting function declaration for a predeclared
2075              function that isn't actually built in.  Objective C uses
2076              these.  The new declaration silently overrides everything
2077              but the volatility (i.e. noreturn) indication.  See also
2078              below.  FIXME: Make Objective C use normal builtins.  */
2079           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2080           return false;
2081         }
2082       /* Permit void foo (...) to match int foo (...) if the latter is
2083          the definition and implicit int was used.  See
2084          c-torture/compile/920625-2.c.  */
2085       else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2086                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2087                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2088                && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2089         {
2090           pedwarned = pedwarn (input_location, 0,
2091                                "conflicting types for %q+D", newdecl);
2092           /* Make sure we keep void as the return type.  */
2093           TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2094           C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2095         }
2096       /* Permit void foo (...) to match an earlier call to foo (...) with
2097          no declared type (thus, implicitly int).  */
2098       else if (TREE_CODE (newdecl) == FUNCTION_DECL
2099                && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2100                && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2101                && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2102         {
2103           pedwarned = pedwarn (input_location, 0,
2104                                "conflicting types for %q+D; have %qT",
2105                                newdecl, newtype);
2106           /* Make sure we keep void as the return type.  */
2107           TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2108         }
2109       else
2110         {
2111           int new_quals = TYPE_QUALS (newtype);
2112           int old_quals = TYPE_QUALS (oldtype);
2113
2114           if (new_quals != old_quals)
2115             {
2116               addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2117               addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2118               if (new_addr != old_addr)
2119                 {
2120                   if (ADDR_SPACE_GENERIC_P (new_addr))
2121                     error ("conflicting named address spaces (generic vs %s) "
2122                            "for %q+D",
2123                            c_addr_space_name (old_addr), newdecl);
2124                   else if (ADDR_SPACE_GENERIC_P (old_addr))
2125                     error ("conflicting named address spaces (%s vs generic) "
2126                            "for %q+D",
2127                            c_addr_space_name (new_addr), newdecl);
2128                   else
2129                     error ("conflicting named address spaces (%s vs %s) "
2130                            "for %q+D",
2131                            c_addr_space_name (new_addr),
2132                            c_addr_space_name (old_addr),
2133                            newdecl);
2134                 }
2135
2136               if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2137                   != CLEAR_QUAL_ADDR_SPACE (old_quals))
2138                 error ("conflicting type qualifiers for %q+D", newdecl);
2139             }
2140           else
2141             error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2142           diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2143           locate_old_decl (olddecl);
2144           return false;
2145         }
2146     }
2147   /* Warn about enum/integer type mismatches.  They are compatible types
2148      (C2X 6.7.2.2/5), but may pose portability problems.  */
2149   else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
2150     warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2151                          OPT_Wenum_int_mismatch,
2152                          "conflicting types for %q+D due to enum/integer "
2153                          "mismatch; have %qT", newdecl, newtype);
2154
2155   /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2156      but silently ignore the redeclaration if either is in a system
2157      header.  (Conflicting redeclarations were handled above.)  This
2158      is allowed for C11 if the types are the same, not just
2159      compatible.  */
2160   if (TREE_CODE (newdecl) == TYPE_DECL)
2161     {
2162       bool types_different = false;
2163
2164       comptypes_result
2165         = comptypes_check_different_types (oldtype, newtype, &types_different);
2166
2167       if (comptypes_result != 1 || types_different)
2168         {
2169           error ("redefinition of typedef %q+D with different type", newdecl);
2170           locate_old_decl (olddecl);
2171           return false;
2172         }
2173
2174       if (DECL_IN_SYSTEM_HEADER (newdecl)
2175           || DECL_IN_SYSTEM_HEADER (olddecl)
2176           || warning_suppressed_p (newdecl, OPT_Wpedantic)
2177           || warning_suppressed_p (olddecl, OPT_Wpedantic))
2178         return true;  /* Allow OLDDECL to continue in use.  */
2179
2180       if (variably_modified_type_p (newtype, NULL))
2181         {
2182           error ("redefinition of typedef %q+D with variably modified type",
2183                  newdecl);
2184           locate_old_decl (olddecl);
2185         }
2186       else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2187                             "redefinition of typedef %q+D", newdecl))
2188         locate_old_decl (olddecl);
2189
2190       return true;
2191     }
2192
2193   /* Function declarations can either be 'static' or 'extern' (no
2194      qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2195      can never conflict with each other on account of linkage
2196      (6.2.2p4).  Multiple definitions are not allowed (6.9p3,5) but
2197      gnu89 mode permits two definitions if one is 'extern inline' and
2198      one is not.  The non- extern-inline definition supersedes the
2199      extern-inline definition.  */
2200
2201   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2202     {
2203       /* If you declare a built-in function name as static, or
2204          define the built-in with an old-style definition (so we
2205          can't validate the argument list) the built-in definition is
2206          overridden, but optionally warn this was a bad choice of name.  */
2207       if (fndecl_built_in_p (olddecl)
2208           && !C_DECL_DECLARED_BUILTIN (olddecl))
2209         {
2210           if (!TREE_PUBLIC (newdecl)
2211               || (DECL_INITIAL (newdecl)
2212                   && !prototype_p (TREE_TYPE (newdecl))))
2213             {
2214               warning_at (DECL_SOURCE_LOCATION (newdecl),
2215                           OPT_Wshadow, "declaration of %qD shadows "
2216                           "a built-in function", newdecl);
2217               /* Discard the old built-in function.  */
2218               return false;
2219             }
2220
2221           if (!prototype_p (TREE_TYPE (newdecl)))
2222             {
2223               /* Set for built-ins that take no arguments.  */
2224               bool func_void_args = false;
2225               if (tree at = TYPE_ARG_TYPES (oldtype))
2226                 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2227
2228               if (extra_warnings && !func_void_args)
2229                 warning_at (DECL_SOURCE_LOCATION (newdecl),
2230                             OPT_Wbuiltin_declaration_mismatch,
2231                             "declaration of built-in function %qD without "
2232                             "a prototype; expected %qT",
2233                             newdecl, TREE_TYPE (olddecl));
2234             }
2235         }
2236
2237       if (DECL_INITIAL (newdecl))
2238         {
2239           if (DECL_INITIAL (olddecl))
2240             {
2241               /* If both decls are in the same TU and the new declaration
2242                  isn't overriding an extern inline reject the new decl.
2243                  In c99, no overriding is allowed in the same translation
2244                  unit.  */
2245               if ((!DECL_EXTERN_INLINE (olddecl)
2246                    || DECL_EXTERN_INLINE (newdecl)
2247                    || (!flag_gnu89_inline
2248                        && (!DECL_DECLARED_INLINE_P (olddecl)
2249                            || !lookup_attribute ("gnu_inline",
2250                                                  DECL_ATTRIBUTES (olddecl)))
2251                        && (!DECL_DECLARED_INLINE_P (newdecl)
2252                            || !lookup_attribute ("gnu_inline",
2253                                                  DECL_ATTRIBUTES (newdecl))))
2254                   )
2255                   && same_translation_unit_p (newdecl, olddecl))
2256                 {
2257                   auto_diagnostic_group d;
2258                   error ("redefinition of %q+D", newdecl);
2259                   locate_old_decl (olddecl);
2260                   return false;
2261                 }
2262             }
2263         }
2264       /* If we have a prototype after an old-style function definition,
2265          the argument types must be checked specially.  */
2266       else if (DECL_INITIAL (olddecl)
2267                && !prototype_p (oldtype) && prototype_p (newtype)
2268                && TYPE_ACTUAL_ARG_TYPES (oldtype))
2269         {
2270           auto_diagnostic_group d;
2271           if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2272             {
2273               locate_old_decl (olddecl);
2274               return false;
2275             }
2276         }
2277       /* A non-static declaration (even an "extern") followed by a
2278          static declaration is undefined behavior per C99 6.2.2p3-5,7.
2279          The same is true for a static forward declaration at block
2280          scope followed by a non-static declaration/definition at file
2281          scope.  Static followed by non-static at the same scope is
2282          not undefined behavior, and is the most convenient way to get
2283          some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
2284          the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2285          we do diagnose it if -Wtraditional.  */
2286       if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2287         {
2288           /* Two exceptions to the rule.  If olddecl is an extern
2289              inline, or a predeclared function that isn't actually
2290              built in, newdecl silently overrides olddecl.  The latter
2291              occur only in Objective C; see also above.  (FIXME: Make
2292              Objective C use normal builtins.)  */
2293           if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2294               && !DECL_EXTERN_INLINE (olddecl))
2295             {
2296               auto_diagnostic_group d;
2297               error ("static declaration of %q+D follows "
2298                      "non-static declaration", newdecl);
2299               locate_old_decl (olddecl);
2300             }
2301           return false;
2302         }
2303       else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2304         {
2305           if (DECL_CONTEXT (olddecl))
2306             {
2307               auto_diagnostic_group d;
2308               error ("non-static declaration of %q+D follows "
2309                      "static declaration", newdecl);
2310               locate_old_decl (olddecl);
2311               return false;
2312             }
2313           else if (warn_traditional)
2314             {
2315               warned |= warning (OPT_Wtraditional,
2316                                  "non-static declaration of %q+D "
2317                                  "follows static declaration", newdecl);
2318             }
2319         }
2320
2321       /* Make sure gnu_inline attribute is either not present, or
2322          present on all inline decls.  */
2323       if (DECL_DECLARED_INLINE_P (olddecl)
2324           && DECL_DECLARED_INLINE_P (newdecl))
2325         {
2326           bool newa = lookup_attribute ("gnu_inline",
2327                                         DECL_ATTRIBUTES (newdecl)) != NULL;
2328           bool olda = lookup_attribute ("gnu_inline",
2329                                         DECL_ATTRIBUTES (olddecl)) != NULL;
2330           if (newa != olda)
2331             {
2332               auto_diagnostic_group d;
2333               error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2334                         newa ? newdecl : olddecl);
2335               error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2336                         "but not here");
2337             }
2338         }
2339     }
2340   else if (VAR_P (newdecl))
2341     {
2342       /* Only variables can be thread-local, and all declarations must
2343          agree on this property.  */
2344       if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2345         {
2346           /* Nothing to check.  Since OLDDECL is marked threadprivate
2347              and NEWDECL does not have a thread-local attribute, we
2348              will merge the threadprivate attribute into NEWDECL.  */
2349           ;
2350         }
2351       else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2352         {
2353           auto_diagnostic_group d;
2354           if (DECL_THREAD_LOCAL_P (newdecl))
2355             error ("thread-local declaration of %q+D follows "
2356                    "non-thread-local declaration", newdecl);
2357           else
2358             error ("non-thread-local declaration of %q+D follows "
2359                    "thread-local declaration", newdecl);
2360
2361           locate_old_decl (olddecl);
2362           return false;
2363         }
2364
2365       /* Multiple initialized definitions are not allowed (6.9p3,5).  */
2366       if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2367         {
2368           auto_diagnostic_group d;
2369           error ("redefinition of %q+D", newdecl);
2370           locate_old_decl (olddecl);
2371           return false;
2372         }
2373
2374       /* Objects declared at file scope: if the first declaration had
2375          external linkage (even if it was an external reference) the
2376          second must have external linkage as well, or the behavior is
2377          undefined.  If the first declaration had internal linkage, then
2378          the second must too, or else be an external reference (in which
2379          case the composite declaration still has internal linkage).
2380          As for function declarations, we warn about the static-then-
2381          extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
2382       if (DECL_FILE_SCOPE_P (newdecl)
2383           && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2384         {
2385           if (DECL_EXTERNAL (newdecl))
2386             {
2387               if (!DECL_FILE_SCOPE_P (olddecl))
2388                 {
2389                   auto_diagnostic_group d;
2390                   error ("extern declaration of %q+D follows "
2391                          "declaration with no linkage", newdecl);
2392                   locate_old_decl (olddecl);
2393                   return false;
2394                 }
2395               else if (warn_traditional)
2396                 {
2397                   warned |= warning (OPT_Wtraditional,
2398                                      "non-static declaration of %q+D "
2399                                      "follows static declaration", newdecl);
2400                 }
2401             }
2402           else
2403             {
2404               auto_diagnostic_group d;
2405               if (TREE_PUBLIC (newdecl))
2406                 error ("non-static declaration of %q+D follows "
2407                        "static declaration", newdecl);
2408               else
2409                 error ("static declaration of %q+D follows "
2410                        "non-static declaration", newdecl);
2411
2412               locate_old_decl (olddecl);
2413               return false;
2414             }
2415         }
2416       /* Two objects with the same name declared at the same block
2417          scope must both be external references (6.7p3).  */
2418       else if (!DECL_FILE_SCOPE_P (newdecl))
2419         {
2420           if (DECL_EXTERNAL (newdecl))
2421             {
2422               /* Extern with initializer at block scope, which will
2423                  already have received an error.  */
2424             }
2425           else if (DECL_EXTERNAL (olddecl))
2426             {
2427               auto_diagnostic_group d;
2428               error ("declaration of %q+D with no linkage follows "
2429                      "extern declaration", newdecl);
2430               locate_old_decl (olddecl);
2431             }
2432           else
2433             {
2434               auto_diagnostic_group d;
2435               error ("redeclaration of %q+D with no linkage", newdecl);
2436               locate_old_decl (olddecl);
2437             }
2438
2439           return false;
2440         }
2441
2442       /* C++ does not permit a decl to appear multiple times at file
2443          scope.  */
2444       if (warn_cxx_compat
2445           && DECL_FILE_SCOPE_P (newdecl)
2446           && !DECL_EXTERNAL (newdecl)
2447           && !DECL_EXTERNAL (olddecl))
2448         warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2449                               OPT_Wc___compat,
2450                               ("duplicate declaration of %qD is "
2451                                "invalid in C++"),
2452                               newdecl);
2453     }
2454
2455   /* warnings */
2456   /* All decls must agree on a visibility.  */
2457   if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2458       && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2459       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2460     {
2461       warned |= warning (0, "redeclaration of %q+D with different visibility "
2462                          "(old visibility preserved)", newdecl);
2463     }
2464
2465   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2466     warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2467   else /* PARM_DECL, VAR_DECL */
2468     {
2469       /* Redeclaration of a parameter is a constraint violation (this is
2470          not explicitly stated, but follows from C99 6.7p3 [no more than
2471          one declaration of the same identifier with no linkage in the
2472          same scope, except type tags] and 6.2.2p6 [parameters have no
2473          linkage]).  We must check for a forward parameter declaration,
2474          indicated by TREE_ASM_WRITTEN on the old declaration - this is
2475          an extension, the mandatory diagnostic for which is handled by
2476          mark_forward_parm_decls.  */
2477
2478       if (TREE_CODE (newdecl) == PARM_DECL
2479           && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2480         {
2481           auto_diagnostic_group d;
2482           error ("redefinition of parameter %q+D", newdecl);
2483           locate_old_decl (olddecl);
2484           return false;
2485         }
2486     }
2487
2488   /* Optional warning for completely redundant decls.  */
2489   if (!warned && !pedwarned
2490       && warn_redundant_decls
2491       /* Don't warn about a function declaration followed by a
2492          definition.  */
2493       && !(TREE_CODE (newdecl) == FUNCTION_DECL
2494            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2495       /* Don't warn about redundant redeclarations of builtins.  */
2496       && !(TREE_CODE (newdecl) == FUNCTION_DECL
2497            && !fndecl_built_in_p (newdecl)
2498            && fndecl_built_in_p (olddecl)
2499            && !C_DECL_DECLARED_BUILTIN (olddecl))
2500       /* Don't warn about an extern followed by a definition.  */
2501       && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2502       /* Don't warn about forward parameter decls.  */
2503       && !(TREE_CODE (newdecl) == PARM_DECL
2504            && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2505       /* Don't warn about a variable definition following a declaration.  */
2506       && !(VAR_P (newdecl)
2507            && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2508     {
2509       warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2510                         newdecl);
2511     }
2512
2513   /* Report location of previous decl/defn.  */
2514   if (warned || pedwarned)
2515     locate_old_decl (olddecl);
2516
2517 #undef DECL_EXTERN_INLINE
2518
2519   return retval;
2520 }
2521
2522 /* Subroutine of duplicate_decls.  NEWDECL has been found to be
2523    consistent with OLDDECL, but carries new information.  Merge the
2524    new information into OLDDECL.  This function issues no
2525    diagnostics.  */
2526
2527 static void
2528 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2529 {
2530   bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2531                             && DECL_INITIAL (newdecl) != NULL_TREE);
2532   bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2533                            && prototype_p (TREE_TYPE (newdecl)));
2534   bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2535                            && prototype_p (TREE_TYPE (olddecl)));
2536
2537   /* For real parm decl following a forward decl, rechain the old decl
2538      in its new location and clear TREE_ASM_WRITTEN (it's not a
2539      forward decl anymore).  */
2540   if (TREE_CODE (newdecl) == PARM_DECL
2541       && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2542     {
2543       struct c_binding *b, **here;
2544
2545       for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2546         if ((*here)->decl == olddecl)
2547           goto found;
2548       gcc_unreachable ();
2549
2550     found:
2551       b = *here;
2552       *here = b->prev;
2553       b->prev = current_scope->bindings;
2554       current_scope->bindings = b;
2555
2556       TREE_ASM_WRITTEN (olddecl) = 0;
2557     }
2558
2559   DECL_ATTRIBUTES (newdecl)
2560     = targetm.merge_decl_attributes (olddecl, newdecl);
2561
2562   /* For typedefs use the old type, as the new type's DECL_NAME points
2563      at newdecl, which will be ggc_freed.  */
2564   if (TREE_CODE (newdecl) == TYPE_DECL)
2565     {
2566       /* But NEWTYPE might have an attribute, honor that.  */
2567       tree tem = newtype;
2568       newtype = oldtype;
2569
2570       if (TYPE_USER_ALIGN (tem))
2571         {
2572           if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2573             SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2574           TYPE_USER_ALIGN (newtype) = true;
2575         }
2576
2577       /* And remove the new type from the variants list.  */
2578       if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2579         {
2580           tree remove = TREE_TYPE (newdecl);
2581           if (TYPE_MAIN_VARIANT (remove) == remove)
2582             {
2583               gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2584               /* If remove is the main variant, no need to remove that
2585                  from the list.  One of the DECL_ORIGINAL_TYPE
2586                  variants, e.g. created for aligned attribute, might still
2587                  refer to the newdecl TYPE_DECL though, so remove that one
2588                  in that case.  */
2589               if (DECL_ORIGINAL_TYPE (newdecl)
2590                   && DECL_ORIGINAL_TYPE (newdecl) != remove)
2591                 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2592                      t; t = TYPE_MAIN_VARIANT (t))
2593                   if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2594                     {
2595                       TYPE_NEXT_VARIANT (t)
2596                         = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2597                       break;
2598                     }
2599             }       
2600           else
2601             for (tree t = TYPE_MAIN_VARIANT (remove); ;
2602                  t = TYPE_NEXT_VARIANT (t))
2603               if (TYPE_NEXT_VARIANT (t) == remove)
2604                 {
2605                   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2606                   break;
2607                 }
2608         }
2609     }
2610
2611   /* Merge the data types specified in the two decls.  */
2612   TREE_TYPE (newdecl)
2613     = TREE_TYPE (olddecl)
2614     = composite_type (newtype, oldtype);
2615
2616   /* Lay the type out, unless already done.  */
2617   if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2618     {
2619       if (TREE_TYPE (newdecl) != error_mark_node)
2620         layout_type (TREE_TYPE (newdecl));
2621       if (TREE_CODE (newdecl) != FUNCTION_DECL
2622           && TREE_CODE (newdecl) != TYPE_DECL
2623           && TREE_CODE (newdecl) != CONST_DECL)
2624         layout_decl (newdecl, 0);
2625     }
2626   else
2627     {
2628       /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
2629       DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2630       DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2631       SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2632       if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2633         {
2634           SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2635           DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2636         }
2637       else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2638                && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2639         DECL_USER_ALIGN (newdecl) = 1;
2640       if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2641           > DECL_WARN_IF_NOT_ALIGN (newdecl))
2642         SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2643                                     DECL_WARN_IF_NOT_ALIGN (olddecl));
2644     }
2645
2646   /* Keep the old rtl since we can safely use it.  */
2647   if (HAS_RTL_P (olddecl))
2648     COPY_DECL_RTL (olddecl, newdecl);
2649
2650   /* Merge the type qualifiers.  */
2651   if (TREE_READONLY (newdecl))
2652     TREE_READONLY (olddecl) = 1;
2653
2654   if (TREE_THIS_VOLATILE (newdecl))
2655     TREE_THIS_VOLATILE (olddecl) = 1;
2656
2657   /* Merge deprecatedness.  */
2658   if (TREE_DEPRECATED (newdecl))
2659     TREE_DEPRECATED (olddecl) = 1;
2660
2661   /* Merge unavailability.  */
2662   if (TREE_UNAVAILABLE (newdecl))
2663     TREE_UNAVAILABLE (olddecl) = 1;
2664
2665   /* If a decl is in a system header and the other isn't, keep the one on the
2666      system header. Otherwise, keep source location of definition rather than
2667      declaration and of prototype rather than non-prototype unless that
2668      prototype is built-in.  */
2669   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2670       && DECL_IN_SYSTEM_HEADER (olddecl)
2671       && !DECL_IN_SYSTEM_HEADER (newdecl) )
2672     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2673   else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2674            && DECL_IN_SYSTEM_HEADER (newdecl)
2675            && !DECL_IN_SYSTEM_HEADER (olddecl))
2676     DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2677   else if ((DECL_INITIAL (newdecl) == NULL_TREE
2678             && DECL_INITIAL (olddecl) != NULL_TREE)
2679            || (old_is_prototype && !new_is_prototype
2680                && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2681     DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2682
2683   /* Merge the initialization information.  */
2684    if (DECL_INITIAL (newdecl) == NULL_TREE)
2685     DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2686
2687   /* Merge the threadprivate attribute.  */
2688   if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2689     C_DECL_THREADPRIVATE_P (newdecl) = 1;
2690
2691   if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2692     {
2693       /* Copy the assembler name.
2694          Currently, it can only be defined in the prototype.  */
2695       COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2696
2697       /* Use visibility of whichever declaration had it specified */
2698       if (DECL_VISIBILITY_SPECIFIED (olddecl))
2699         {
2700           DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2701           DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2702         }
2703
2704       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2705         {
2706           DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2707           DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2708           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2709           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2710             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2711           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2712           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2713           if (DECL_IS_OPERATOR_NEW_P (olddecl))
2714             DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2715           if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2716             DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2717           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2718           DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2719           DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2720         }
2721
2722       /* Merge the storage class information.  */
2723       merge_weak (newdecl, olddecl);
2724
2725       /* For functions, static overrides non-static.  */
2726       if (TREE_CODE (newdecl) == FUNCTION_DECL)
2727         {
2728           TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2729           /* This is since we don't automatically
2730              copy the attributes of NEWDECL into OLDDECL.  */
2731           TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2732           /* If this clears `static', clear it in the identifier too.  */
2733           if (!TREE_PUBLIC (olddecl))
2734             TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2735         }
2736     }
2737
2738   /* In c99, 'extern' declaration before (or after) 'inline' means this
2739      function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2740      is present.  */
2741   if (TREE_CODE (newdecl) == FUNCTION_DECL
2742       && !flag_gnu89_inline
2743       && (DECL_DECLARED_INLINE_P (newdecl)
2744           || DECL_DECLARED_INLINE_P (olddecl))
2745       && (!DECL_DECLARED_INLINE_P (newdecl)
2746           || !DECL_DECLARED_INLINE_P (olddecl)
2747           || !DECL_EXTERNAL (olddecl))
2748       && DECL_EXTERNAL (newdecl)
2749       && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2750       && !current_function_decl)
2751     DECL_EXTERNAL (newdecl) = 0;
2752
2753   /* An inline definition following a static declaration is not
2754      DECL_EXTERNAL.  */
2755   if (new_is_definition
2756       && (DECL_DECLARED_INLINE_P (newdecl)
2757           || DECL_DECLARED_INLINE_P (olddecl))
2758       && !TREE_PUBLIC (olddecl))
2759     DECL_EXTERNAL (newdecl) = 0;
2760
2761   if (DECL_EXTERNAL (newdecl))
2762     {
2763       TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2764       DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2765
2766       /* An extern decl does not override previous storage class.  */
2767       TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2768       if (!DECL_EXTERNAL (newdecl))
2769         {
2770           DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2771           DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2772         }
2773     }
2774   else
2775     {
2776       TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2777       TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2778     }
2779
2780   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2781     {
2782       /* If we're redefining a function previously defined as extern
2783          inline, make sure we emit debug info for the inline before we
2784          throw it away, in case it was inlined into a function that
2785          hasn't been written out yet.  */
2786       if (new_is_definition && DECL_INITIAL (olddecl))
2787         /* The new defn must not be inline.  */
2788         DECL_UNINLINABLE (newdecl) = 1;
2789       else
2790         {
2791           /* If either decl says `inline', this fn is inline, unless
2792              its definition was passed already.  */
2793           if (DECL_DECLARED_INLINE_P (newdecl)
2794               || DECL_DECLARED_INLINE_P (olddecl))
2795             DECL_DECLARED_INLINE_P (newdecl) = 1;
2796
2797           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2798             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2799
2800           DECL_DISREGARD_INLINE_LIMITS (newdecl)
2801             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2802             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2803                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2804         }
2805
2806       if (fndecl_built_in_p (olddecl))
2807         {
2808           /* If redeclaring a builtin function, it stays built in.
2809              But it gets tagged as having been declared.  */
2810           copy_decl_built_in_function (newdecl, olddecl);
2811           C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2812           if (new_is_prototype)
2813             {
2814               C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2815               if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2816                 {
2817                   enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2818                   switch (fncode)
2819                     {
2820                       /* If a compatible prototype of these builtin functions
2821                          is seen, assume the runtime implements it with the
2822                          expected semantics.  */
2823                     case BUILT_IN_STPCPY:
2824                       if (builtin_decl_explicit_p (fncode))
2825                         set_builtin_decl_implicit_p (fncode, true);
2826                       break;
2827                     default:
2828                       if (builtin_decl_explicit_p (fncode))
2829                         set_builtin_decl_declared_p (fncode, true);
2830                       break;
2831                     }
2832
2833                   copy_attributes_to_builtin (newdecl);
2834                 }
2835             }
2836           else
2837             C_DECL_BUILTIN_PROTOTYPE (newdecl)
2838               = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2839         }
2840
2841       /* Preserve function specific target and optimization options */
2842       if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2843           && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2844         DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2845           = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2846
2847       if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2848           && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2849         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2850           = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2851
2852       /* Also preserve various other info from the definition.  */
2853       if (!new_is_definition)
2854         {
2855           tree t;
2856           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2857           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2858           DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2859           DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2860           DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2861           for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2862             DECL_CONTEXT (t) = newdecl;
2863
2864           /* See if we've got a function to instantiate from.  */
2865           if (DECL_SAVED_TREE (olddecl))
2866             DECL_ABSTRACT_ORIGIN (newdecl)
2867               = DECL_ABSTRACT_ORIGIN (olddecl);
2868         }
2869     }
2870
2871   /* Merge the USED information.  */
2872   if (TREE_USED (olddecl))
2873     TREE_USED (newdecl) = 1;
2874   else if (TREE_USED (newdecl))
2875     TREE_USED (olddecl) = 1;
2876   if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2877     DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2878   if (DECL_PRESERVE_P (olddecl))
2879     DECL_PRESERVE_P (newdecl) = 1;
2880   else if (DECL_PRESERVE_P (newdecl))
2881     DECL_PRESERVE_P (olddecl) = 1;
2882
2883   /* Merge DECL_COMMON */
2884   if (VAR_P (olddecl) && VAR_P (newdecl)
2885       && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2886       && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2887     DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2888
2889   /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2890      But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2891      DECL_ARGUMENTS (if appropriate).  */
2892   {
2893     unsigned olddecl_uid = DECL_UID (olddecl);
2894     tree olddecl_context = DECL_CONTEXT (olddecl);
2895     tree olddecl_arguments = NULL;
2896     if (TREE_CODE (olddecl) == FUNCTION_DECL)
2897       olddecl_arguments = DECL_ARGUMENTS (olddecl);
2898
2899     memcpy ((char *) olddecl + sizeof (struct tree_common),
2900             (char *) newdecl + sizeof (struct tree_common),
2901             sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2902     DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2903     switch (TREE_CODE (olddecl))
2904       {
2905       case FUNCTION_DECL:
2906       case VAR_DECL:
2907         {
2908           struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2909
2910           memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2911                   (char *) newdecl + sizeof (struct tree_decl_common),
2912                   tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2913           olddecl->decl_with_vis.symtab_node = snode;
2914
2915           if ((DECL_EXTERNAL (olddecl)
2916                || TREE_PUBLIC (olddecl)
2917                || TREE_STATIC (olddecl))
2918               && DECL_SECTION_NAME (newdecl) != NULL)
2919             set_decl_section_name (olddecl, newdecl);
2920
2921           /* This isn't quite correct for something like
2922                 int __thread x attribute ((tls_model ("local-exec")));
2923                 extern int __thread x;
2924              as we'll lose the "local-exec" model.  */
2925           if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2926             set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2927           break;
2928         }
2929
2930       case FIELD_DECL:
2931       case PARM_DECL:
2932       case LABEL_DECL:
2933       case RESULT_DECL:
2934       case CONST_DECL:
2935       case TYPE_DECL:
2936         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2937                 (char *) newdecl + sizeof (struct tree_decl_common),
2938                 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2939         break;
2940
2941       default:
2942
2943         memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2944                 (char *) newdecl + sizeof (struct tree_decl_common),
2945                 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2946       }
2947     DECL_UID (olddecl) = olddecl_uid;
2948     DECL_CONTEXT (olddecl) = olddecl_context;
2949     if (TREE_CODE (olddecl) == FUNCTION_DECL)
2950       DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2951   }
2952
2953   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2954      so that encode_section_info has a chance to look at the new decl
2955      flags and attributes.  */
2956   if (DECL_RTL_SET_P (olddecl)
2957       && (TREE_CODE (olddecl) == FUNCTION_DECL
2958           || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2959     make_decl_rtl (olddecl);
2960 }
2961
2962 /* Handle when a new declaration NEWDECL has the same name as an old
2963    one OLDDECL in the same binding contour.  Prints an error message
2964    if appropriate.
2965
2966    If safely possible, alter OLDDECL to look like NEWDECL, and return
2967    true.  Otherwise, return false.  */
2968
2969 static bool
2970 duplicate_decls (tree newdecl, tree olddecl)
2971 {
2972   tree newtype = NULL, oldtype = NULL;
2973
2974   if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2975     {
2976       /* Avoid `unused variable' and other warnings for OLDDECL.  */
2977       suppress_warning (olddecl, OPT_Wunused);
2978       /* If the types are completely different, poison them both with
2979          error_mark_node.  */
2980       if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
2981           && olddecl != error_mark_node
2982           && seen_error ())
2983         {
2984           if (TREE_CODE (olddecl) != FUNCTION_DECL)
2985             TREE_TYPE (olddecl) = error_mark_node;
2986           if (TREE_CODE (newdecl) != FUNCTION_DECL)
2987             TREE_TYPE (newdecl) = error_mark_node;
2988         }
2989       return false;
2990     }
2991
2992   merge_decls (newdecl, olddecl, newtype, oldtype);
2993
2994   /* The NEWDECL will no longer be needed.
2995
2996      Before releasing the node, be sure to remove function from symbol
2997      table that might have been inserted there to record comdat group.
2998      Be sure to however do not free DECL_STRUCT_FUNCTION because this
2999      structure is shared in between NEWDECL and OLDECL.  */
3000   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3001     DECL_STRUCT_FUNCTION (newdecl) = NULL;
3002   if (VAR_OR_FUNCTION_DECL_P (newdecl))
3003     {
3004       struct symtab_node *snode = symtab_node::get (newdecl);
3005       if (snode)
3006         snode->remove ();
3007     }
3008   ggc_free (newdecl);
3009   return true;
3010 }
3011
3012 \f
3013 /* Check whether decl-node NEW_DECL shadows an existing declaration.  */
3014 static void
3015 warn_if_shadowing (tree new_decl)
3016 {
3017   struct c_binding *b;
3018
3019   /* Shadow warnings wanted?  */
3020   if (!(warn_shadow
3021         || warn_shadow_local
3022         || warn_shadow_compatible_local)
3023       /* No shadow warnings for internally generated vars.  */
3024       || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3025     return;
3026
3027   /* Is anything being shadowed?  Invisible decls do not count.  */
3028   for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3029     if (b->decl && b->decl != new_decl && !b->invisible
3030         && (b->decl == error_mark_node
3031             || diagnostic_report_warnings_p (global_dc,
3032                                              DECL_SOURCE_LOCATION (b->decl))))
3033       {
3034         tree old_decl = b->decl;
3035
3036         if (old_decl == error_mark_node)
3037           {
3038             warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3039                      "non-variable", new_decl);
3040             break;
3041           }
3042
3043         bool warned = false;
3044         auto_diagnostic_group d;
3045         if (TREE_CODE (old_decl) == PARM_DECL)
3046           {
3047             enum opt_code warning_code;
3048
3049             /* If '-Wshadow=compatible-local' is specified without other
3050                -Wshadow= flags, we will warn only when the types of the
3051                shadowing variable (i.e. new_decl) and the shadowed variable
3052                (old_decl) are compatible.  */
3053             if (warn_shadow)
3054               warning_code = OPT_Wshadow;
3055             else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3056               warning_code = OPT_Wshadow_compatible_local;
3057             else
3058               warning_code = OPT_Wshadow_local;
3059             warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3060                                  "declaration of %qD shadows a parameter",
3061                                  new_decl);
3062           }
3063         else if (DECL_FILE_SCOPE_P (old_decl))
3064           {
3065             /* Do not warn if a variable shadows a function, unless
3066                the variable is a function or a pointer-to-function.  */
3067             if (TREE_CODE (old_decl) == FUNCTION_DECL
3068                 && TREE_CODE (new_decl) != FUNCTION_DECL
3069                 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3070                 continue;
3071
3072             warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3073                                  "declaration of %qD shadows a global "
3074                                  "declaration",
3075                                  new_decl);
3076           }
3077         else if (TREE_CODE (old_decl) == FUNCTION_DECL
3078                  && fndecl_built_in_p (old_decl))
3079           {
3080             warning (OPT_Wshadow, "declaration of %q+D shadows "
3081                      "a built-in function", new_decl);
3082             break;
3083           }
3084         else
3085           {
3086             enum opt_code warning_code;
3087
3088             /* If '-Wshadow=compatible-local' is specified without other
3089                -Wshadow= flags, we will warn only when the types of the
3090                shadowing variable (i.e. new_decl) and the shadowed variable
3091                (old_decl) are compatible.  */
3092             if (warn_shadow)
3093               warning_code = OPT_Wshadow;
3094             else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3095               warning_code = OPT_Wshadow_compatible_local;
3096             else
3097               warning_code = OPT_Wshadow_local;
3098             warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3099                                  "declaration of %qD shadows a previous local",
3100                                  new_decl);
3101           }
3102
3103         if (warned)
3104           inform (DECL_SOURCE_LOCATION (old_decl),
3105                   "shadowed declaration is here");
3106
3107         break;
3108       }
3109 }
3110
3111 /* Record a decl-node X as belonging to the current lexical scope.
3112    Check for errors (such as an incompatible declaration for the same
3113    name already seen in the same scope).
3114
3115    Returns either X or an old decl for the same name.
3116    If an old decl is returned, it may have been smashed
3117    to agree with what X says.  */
3118
3119 tree
3120 pushdecl (tree x)
3121 {
3122   tree name = DECL_NAME (x);
3123   struct c_scope *scope = current_scope;
3124   struct c_binding *b;
3125   bool nested = false;
3126   location_t locus = DECL_SOURCE_LOCATION (x);
3127
3128   /* Must set DECL_CONTEXT for everything not at file scope or
3129      DECL_FILE_SCOPE_P won't work.  Local externs don't count
3130      unless they have initializers (which generate code).  */
3131   if (current_function_decl
3132       && (!VAR_OR_FUNCTION_DECL_P (x)
3133           || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3134     DECL_CONTEXT (x) = current_function_decl;
3135
3136   /* Anonymous decls are just inserted in the scope.  */
3137   if (!name)
3138     {
3139       bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3140             locus);
3141       return x;
3142     }
3143
3144   /* First, see if there is another declaration with the same name in
3145      the current scope.  If there is, duplicate_decls may do all the
3146      work for us.  If duplicate_decls returns false, that indicates
3147      two incompatible decls in the same scope; we are to silently
3148      replace the old one (duplicate_decls has issued all appropriate
3149      diagnostics).  In particular, we should not consider possible
3150      duplicates in the external scope, or shadowing.  */
3151   b = I_SYMBOL_BINDING (name);
3152   if (b && B_IN_SCOPE (b, scope))
3153     {
3154       struct c_binding *b_ext, *b_use;
3155       tree type = TREE_TYPE (x);
3156       tree visdecl = b->decl;
3157       tree vistype = TREE_TYPE (visdecl);
3158       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3159           && COMPLETE_TYPE_P (TREE_TYPE (x)))
3160         b->inner_comp = false;
3161       b_use = b;
3162       b_ext = b;
3163       /* If this is an external linkage declaration, we should check
3164          for compatibility with the type in the external scope before
3165          setting the type at this scope based on the visible
3166          information only.  */
3167       if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3168         {
3169           while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3170             b_ext = b_ext->shadowed;
3171           if (b_ext)
3172             {
3173               b_use = b_ext;
3174               if (b_use->u.type)
3175                 TREE_TYPE (b_use->decl) = b_use->u.type;
3176             }
3177         }
3178       if (duplicate_decls (x, b_use->decl))
3179         {
3180           if (b_use != b)
3181             {
3182               /* Save the updated type in the external scope and
3183                  restore the proper type for this scope.  */
3184               tree thistype;
3185               if (comptypes (vistype, type))
3186                 thistype = composite_type (vistype, type);
3187               else
3188                 thistype = TREE_TYPE (b_use->decl);
3189               b_use->u.type = TREE_TYPE (b_use->decl);
3190               if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3191                   && fndecl_built_in_p (b_use->decl))
3192                 thistype
3193                   = build_type_attribute_variant (thistype,
3194                                                   TYPE_ATTRIBUTES
3195                                                   (b_use->u.type));
3196               TREE_TYPE (b_use->decl) = thistype;
3197             }
3198           return b_use->decl;
3199         }
3200       else
3201         goto skip_external_and_shadow_checks;
3202     }
3203
3204   /* All declarations with external linkage, and all external
3205      references, go in the external scope, no matter what scope is
3206      current.  However, the binding in that scope is ignored for
3207      purposes of normal name lookup.  A separate binding structure is
3208      created in the requested scope; this governs the normal
3209      visibility of the symbol.
3210
3211      The binding in the externals scope is used exclusively for
3212      detecting duplicate declarations of the same object, no matter
3213      what scope they are in; this is what we do here.  (C99 6.2.7p2:
3214      All declarations that refer to the same object or function shall
3215      have compatible type; otherwise, the behavior is undefined.)
3216      However, in Objective-C, we also want to detect declarations
3217      conflicting with those of the basic types.  */
3218   if ((DECL_EXTERNAL (x) || scope == file_scope)
3219       && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3220     {
3221       tree type = TREE_TYPE (x);
3222       tree vistype = NULL_TREE;
3223       tree visdecl = NULL_TREE;
3224       bool type_saved = false;
3225       if (b && !B_IN_EXTERNAL_SCOPE (b)
3226           && VAR_OR_FUNCTION_DECL_P (b->decl)
3227           && DECL_FILE_SCOPE_P (b->decl))
3228         {
3229           visdecl = b->decl;
3230           vistype = TREE_TYPE (visdecl);
3231         }
3232       if (scope != file_scope
3233           && !DECL_IN_SYSTEM_HEADER (x))
3234         warning_at (locus, OPT_Wnested_externs,
3235                     "nested extern declaration of %qD", x);
3236
3237       while (b && !B_IN_EXTERNAL_SCOPE (b))
3238         {
3239           /* If this decl might be modified, save its type.  This is
3240              done here rather than when the decl is first bound
3241              because the type may change after first binding, through
3242              being completed or through attributes being added.  If we
3243              encounter multiple such decls, only the first should have
3244              its type saved; the others will already have had their
3245              proper types saved and the types will not have changed as
3246              their scopes will not have been re-entered.  */
3247           if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3248             {
3249               b->u.type = TREE_TYPE (b->decl);
3250               type_saved = true;
3251             }
3252           if (B_IN_FILE_SCOPE (b)
3253               && VAR_P (b->decl)
3254               && TREE_STATIC (b->decl)
3255               && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3256               && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3257               && TREE_CODE (type) == ARRAY_TYPE
3258               && TYPE_DOMAIN (type)
3259               && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3260               && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3261             {
3262               /* Array type completed in inner scope, which should be
3263                  diagnosed if the completion does not have size 1 and
3264                  it does not get completed in the file scope.  */
3265               b->inner_comp = true;
3266             }
3267           b = b->shadowed;
3268         }
3269
3270       /* If a matching external declaration has been found, set its
3271          type to the composite of all the types of that declaration.
3272          After the consistency checks, it will be reset to the
3273          composite of the visible types only.  */
3274       if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3275           && b->u.type)
3276         TREE_TYPE (b->decl) = b->u.type;
3277
3278       /* The point of the same_translation_unit_p check here is,
3279          we want to detect a duplicate decl for a construct like
3280          foo() { extern bar(); } ... static bar();  but not if
3281          they are in different translation units.  In any case,
3282          the static does not go in the externals scope.  */
3283       if (b
3284           && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3285           && duplicate_decls (x, b->decl))
3286         {
3287           tree thistype;
3288           if (vistype)
3289             {
3290               if (comptypes (vistype, type))
3291                 thistype = composite_type (vistype, type);
3292               else
3293                 thistype = TREE_TYPE (b->decl);
3294             }
3295           else
3296             thistype = type;
3297           b->u.type = TREE_TYPE (b->decl);
3298           /* Propagate the type attributes to the decl.  */
3299           thistype
3300             = build_type_attribute_variant (thistype,
3301                                             TYPE_ATTRIBUTES (b->u.type));
3302           TREE_TYPE (b->decl) = thistype;
3303           bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3304                 locus);
3305           return b->decl;
3306         }
3307       else if (TREE_PUBLIC (x))
3308         {
3309           if (visdecl && !b && duplicate_decls (x, visdecl))
3310             {
3311               /* An external declaration at block scope referring to a
3312                  visible entity with internal linkage.  The composite
3313                  type will already be correct for this scope, so we
3314                  just need to fall through to make the declaration in
3315                  this scope.  */
3316               nested = true;
3317               x = visdecl;
3318             }
3319           else
3320             {
3321               bind (name, x, external_scope, /*invisible=*/true,
3322                     /*nested=*/false, locus);
3323               nested = true;
3324             }
3325         }
3326     }
3327
3328   if (TREE_CODE (x) != PARM_DECL)
3329     warn_if_shadowing (x);
3330
3331  skip_external_and_shadow_checks:
3332   if (TREE_CODE (x) == TYPE_DECL)
3333     {
3334       /* So this is a typedef, set its underlying type.  */
3335       set_underlying_type (x);
3336
3337       /* If X is a typedef defined in the current function, record it
3338          for the purpose of implementing the -Wunused-local-typedefs
3339          warning.  */
3340       record_locally_defined_typedef (x);
3341     }
3342
3343   bind (name, x, scope, /*invisible=*/false, nested, locus);
3344
3345   /* If x's type is incomplete because it's based on a
3346      structure or union which has not yet been fully declared,
3347      attach it to that structure or union type, so we can go
3348      back and complete the variable declaration later, if the
3349      structure or union gets fully declared.
3350
3351      If the input is erroneous, we can have error_mark in the type
3352      slot (e.g. "f(void a, ...)") - that doesn't count as an
3353      incomplete type.  */
3354   if (TREE_TYPE (x) != error_mark_node
3355       && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3356     {
3357       tree element = TREE_TYPE (x);
3358
3359       while (TREE_CODE (element) == ARRAY_TYPE)
3360         element = TREE_TYPE (element);
3361       element = TYPE_MAIN_VARIANT (element);
3362
3363       if ((RECORD_OR_UNION_TYPE_P (element)
3364            || TREE_CODE (element) == ENUMERAL_TYPE)
3365           && (TREE_CODE (x) != TYPE_DECL
3366               || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3367           && !COMPLETE_TYPE_P (element))
3368         C_TYPE_INCOMPLETE_VARS (element)
3369           = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3370     }
3371   return x;
3372 }
3373 \f
3374
3375 /* Issue a warning about implicit function declaration.  ID is the function
3376    identifier, OLDDECL is a declaration of the function in a different scope,
3377    or NULL_TREE.  */
3378
3379 static void
3380 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3381 {
3382   if (!warn_implicit_function_declaration)
3383     return;
3384
3385   bool warned;
3386   auto_diagnostic_group d;
3387   name_hint hint;
3388   if (!olddecl)
3389     hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3390
3391   if (flag_isoc99)
3392     {
3393       if (const char *suggestion = hint.suggestion ())
3394         {
3395           gcc_rich_location richloc (loc);
3396           richloc.add_fixit_replace (suggestion);
3397           warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3398                             "implicit declaration of function %qE;"
3399                             " did you mean %qs?",
3400                             id, suggestion);
3401         }
3402       else
3403         warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3404                           "implicit declaration of function %qE", id);
3405     }
3406   else if (const char *suggestion = hint.suggestion ())
3407     {
3408       gcc_rich_location richloc (loc);
3409       richloc.add_fixit_replace (suggestion);
3410       warned = warning_at
3411         (&richloc, OPT_Wimplicit_function_declaration,
3412          G_("implicit declaration of function %qE; did you mean %qs?"),
3413          id, suggestion);
3414     }
3415   else
3416     warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3417                          G_("implicit declaration of function %qE"), id);
3418
3419   if (warned)
3420     {
3421       /* Whether the olddecl is an undeclared builtin function.
3422          locate_old_decl will not generate a diagnostic for those,
3423          so in that case we want to look elsewhere.  */
3424       bool undeclared_builtin = (olddecl
3425                                  && TREE_CODE (olddecl) == FUNCTION_DECL
3426                                  && fndecl_built_in_p (olddecl)
3427                                  && !C_DECL_DECLARED_BUILTIN (olddecl));
3428       if (undeclared_builtin)
3429         {
3430           const char *header = header_for_builtin_fn (olddecl);
3431           if (header)
3432             {
3433               rich_location richloc (line_table, loc);
3434               maybe_add_include_fixit (&richloc, header, true);
3435               inform (&richloc,
3436                       "include %qs or provide a declaration of %qE",
3437                       header, id);
3438             }
3439         }
3440       else if (olddecl)
3441         locate_old_decl (olddecl);
3442     }
3443
3444   if (!warned)
3445     hint.suppress ();
3446 }
3447
3448 /* Return the name of the header file that declares built-in function
3449    FNDECL, or null if either we don't know or don't expect to see an
3450    explicit declaration.  */
3451
3452 static const char *
3453 header_for_builtin_fn (tree fndecl)
3454 {
3455   if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3456     return NULL;
3457
3458   switch (DECL_FUNCTION_CODE (fndecl))
3459     {
3460     CASE_FLT_FN (BUILT_IN_ACOS):
3461     CASE_FLT_FN (BUILT_IN_ACOSH):
3462     CASE_FLT_FN (BUILT_IN_ASIN):
3463     CASE_FLT_FN (BUILT_IN_ASINH):
3464     CASE_FLT_FN (BUILT_IN_ATAN):
3465     CASE_FLT_FN (BUILT_IN_ATANH):
3466     CASE_FLT_FN (BUILT_IN_ATAN2):
3467     CASE_FLT_FN (BUILT_IN_CBRT):
3468     CASE_FLT_FN (BUILT_IN_CEIL):
3469     CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3470     CASE_FLT_FN (BUILT_IN_COPYSIGN):
3471     CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3472     CASE_FLT_FN (BUILT_IN_COS):
3473     CASE_FLT_FN (BUILT_IN_COSH):
3474     CASE_FLT_FN (BUILT_IN_ERF):
3475     CASE_FLT_FN (BUILT_IN_ERFC):
3476     CASE_FLT_FN (BUILT_IN_EXP):
3477     CASE_FLT_FN (BUILT_IN_EXP2):
3478     CASE_FLT_FN (BUILT_IN_EXPM1):
3479     CASE_FLT_FN (BUILT_IN_FABS):
3480     CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3481     CASE_FLT_FN (BUILT_IN_FDIM):
3482     CASE_FLT_FN (BUILT_IN_FLOOR):
3483     CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3484     CASE_FLT_FN (BUILT_IN_FMA):
3485     CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3486     CASE_FLT_FN (BUILT_IN_FMAX):
3487     CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3488     CASE_FLT_FN (BUILT_IN_FMIN):
3489     CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3490     CASE_FLT_FN (BUILT_IN_FMOD):
3491     CASE_FLT_FN (BUILT_IN_FREXP):
3492     CASE_FLT_FN (BUILT_IN_HYPOT):
3493     CASE_FLT_FN (BUILT_IN_ILOGB):
3494     CASE_FLT_FN (BUILT_IN_LDEXP):
3495     CASE_FLT_FN (BUILT_IN_LGAMMA):
3496     CASE_FLT_FN (BUILT_IN_LLRINT):
3497     CASE_FLT_FN (BUILT_IN_LLROUND):
3498     CASE_FLT_FN (BUILT_IN_LOG):
3499     CASE_FLT_FN (BUILT_IN_LOG10):
3500     CASE_FLT_FN (BUILT_IN_LOG1P):
3501     CASE_FLT_FN (BUILT_IN_LOG2):
3502     CASE_FLT_FN (BUILT_IN_LOGB):
3503     CASE_FLT_FN (BUILT_IN_LRINT):
3504     CASE_FLT_FN (BUILT_IN_LROUND):
3505     CASE_FLT_FN (BUILT_IN_MODF):
3506     CASE_FLT_FN (BUILT_IN_NAN):
3507     CASE_FLT_FN (BUILT_IN_NEARBYINT):
3508     CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3509     CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3510     CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3511     CASE_FLT_FN (BUILT_IN_POW):
3512     CASE_FLT_FN (BUILT_IN_REMAINDER):
3513     CASE_FLT_FN (BUILT_IN_REMQUO):
3514     CASE_FLT_FN (BUILT_IN_RINT):
3515     CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3516     CASE_FLT_FN (BUILT_IN_ROUND):
3517     CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3518     CASE_FLT_FN (BUILT_IN_SCALBLN):
3519     CASE_FLT_FN (BUILT_IN_SCALBN):
3520     CASE_FLT_FN (BUILT_IN_SIN):
3521     CASE_FLT_FN (BUILT_IN_SINH):
3522     CASE_FLT_FN (BUILT_IN_SINCOS):
3523     CASE_FLT_FN (BUILT_IN_SQRT):
3524     CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3525     CASE_FLT_FN (BUILT_IN_TAN):
3526     CASE_FLT_FN (BUILT_IN_TANH):
3527     CASE_FLT_FN (BUILT_IN_TGAMMA):
3528     CASE_FLT_FN (BUILT_IN_TRUNC):
3529     CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3530     case BUILT_IN_ISINF:
3531     case BUILT_IN_ISNAN:
3532       return "<math.h>";
3533     CASE_FLT_FN (BUILT_IN_CABS):
3534     CASE_FLT_FN (BUILT_IN_CACOS):
3535     CASE_FLT_FN (BUILT_IN_CACOSH):
3536     CASE_FLT_FN (BUILT_IN_CARG):
3537     CASE_FLT_FN (BUILT_IN_CASIN):
3538     CASE_FLT_FN (BUILT_IN_CASINH):
3539     CASE_FLT_FN (BUILT_IN_CATAN):
3540     CASE_FLT_FN (BUILT_IN_CATANH):
3541     CASE_FLT_FN (BUILT_IN_CCOS):
3542     CASE_FLT_FN (BUILT_IN_CCOSH):
3543     CASE_FLT_FN (BUILT_IN_CEXP):
3544     CASE_FLT_FN (BUILT_IN_CIMAG):
3545     CASE_FLT_FN (BUILT_IN_CLOG):
3546     CASE_FLT_FN (BUILT_IN_CONJ):
3547     CASE_FLT_FN (BUILT_IN_CPOW):
3548     CASE_FLT_FN (BUILT_IN_CPROJ):
3549     CASE_FLT_FN (BUILT_IN_CREAL):
3550     CASE_FLT_FN (BUILT_IN_CSIN):
3551     CASE_FLT_FN (BUILT_IN_CSINH):
3552     CASE_FLT_FN (BUILT_IN_CSQRT):
3553     CASE_FLT_FN (BUILT_IN_CTAN):
3554     CASE_FLT_FN (BUILT_IN_CTANH):
3555       return "<complex.h>";
3556     case BUILT_IN_MEMCHR:
3557     case BUILT_IN_MEMCMP:
3558     case BUILT_IN_MEMCPY:
3559     case BUILT_IN_MEMMOVE:
3560     case BUILT_IN_MEMSET:
3561     case BUILT_IN_STRCAT:
3562     case BUILT_IN_STRCHR:
3563     case BUILT_IN_STRCMP:
3564     case BUILT_IN_STRCPY:
3565     case BUILT_IN_STRCSPN:
3566     case BUILT_IN_STRLEN:
3567     case BUILT_IN_STRNCAT:
3568     case BUILT_IN_STRNCMP:
3569     case BUILT_IN_STRNCPY:
3570     case BUILT_IN_STRPBRK:
3571     case BUILT_IN_STRRCHR:
3572     case BUILT_IN_STRSPN:
3573     case BUILT_IN_STRSTR:
3574       return "<string.h>";
3575     case BUILT_IN_FPRINTF:
3576     case BUILT_IN_PUTC:
3577     case BUILT_IN_FPUTC:
3578     case BUILT_IN_FPUTS:
3579     case BUILT_IN_FSCANF:
3580     case BUILT_IN_FWRITE:
3581     case BUILT_IN_PRINTF:
3582     case BUILT_IN_PUTCHAR:
3583     case BUILT_IN_PUTS:
3584     case BUILT_IN_SCANF:
3585     case BUILT_IN_SNPRINTF:
3586     case BUILT_IN_SPRINTF:
3587     case BUILT_IN_SSCANF:
3588     case BUILT_IN_VFPRINTF:
3589     case BUILT_IN_VFSCANF:
3590     case BUILT_IN_VPRINTF:
3591     case BUILT_IN_VSCANF:
3592     case BUILT_IN_VSNPRINTF:
3593     case BUILT_IN_VSPRINTF:
3594     case BUILT_IN_VSSCANF:
3595       return "<stdio.h>";
3596     case BUILT_IN_ISALNUM:
3597     case BUILT_IN_ISALPHA:
3598     case BUILT_IN_ISBLANK:
3599     case BUILT_IN_ISCNTRL:
3600     case BUILT_IN_ISDIGIT:
3601     case BUILT_IN_ISGRAPH:
3602     case BUILT_IN_ISLOWER:
3603     case BUILT_IN_ISPRINT:
3604     case BUILT_IN_ISPUNCT:
3605     case BUILT_IN_ISSPACE:
3606     case BUILT_IN_ISUPPER:
3607     case BUILT_IN_ISXDIGIT:
3608     case BUILT_IN_TOLOWER:
3609     case BUILT_IN_TOUPPER:
3610       return "<ctype.h>";
3611     case BUILT_IN_ISWALNUM:
3612     case BUILT_IN_ISWALPHA:
3613     case BUILT_IN_ISWBLANK:
3614     case BUILT_IN_ISWCNTRL:
3615     case BUILT_IN_ISWDIGIT:
3616     case BUILT_IN_ISWGRAPH:
3617     case BUILT_IN_ISWLOWER:
3618     case BUILT_IN_ISWPRINT:
3619     case BUILT_IN_ISWPUNCT:
3620     case BUILT_IN_ISWSPACE:
3621     case BUILT_IN_ISWUPPER:
3622     case BUILT_IN_ISWXDIGIT:
3623     case BUILT_IN_TOWLOWER:
3624     case BUILT_IN_TOWUPPER:
3625       return "<wctype.h>";
3626     case BUILT_IN_ABORT:
3627     case BUILT_IN_ABS:
3628     case BUILT_IN_CALLOC:
3629     case BUILT_IN_EXIT:
3630     case BUILT_IN_FREE:
3631     case BUILT_IN_LABS:
3632     case BUILT_IN_LLABS:
3633     case BUILT_IN_MALLOC:
3634     case BUILT_IN_REALLOC:
3635     case BUILT_IN__EXIT2:
3636     case BUILT_IN_ALIGNED_ALLOC:
3637       return "<stdlib.h>";
3638     case BUILT_IN_IMAXABS:
3639       return "<inttypes.h>";
3640     case BUILT_IN_STRFTIME:
3641       return "<time.h>";
3642     default:
3643       return NULL;
3644     }
3645 }
3646
3647 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3648    function of type int ().  */
3649
3650 tree
3651 implicitly_declare (location_t loc, tree functionid)
3652 {
3653   struct c_binding *b;
3654   tree decl = NULL_TREE;
3655   tree asmspec_tree;
3656
3657   for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3658     {
3659       if (B_IN_SCOPE (b, external_scope))
3660         {
3661           decl = b->decl;
3662           break;
3663         }
3664     }
3665
3666   if (decl)
3667     {
3668       if (TREE_CODE (decl) != FUNCTION_DECL)
3669         return decl;
3670
3671       /* FIXME: Objective-C has weird not-really-builtin functions
3672          which are supposed to be visible automatically.  They wind up
3673          in the external scope because they're pushed before the file
3674          scope gets created.  Catch this here and rebind them into the
3675          file scope.  */
3676       if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3677         {
3678           bind (functionid, decl, file_scope,
3679                 /*invisible=*/false, /*nested=*/true,
3680                 DECL_SOURCE_LOCATION (decl));
3681           return decl;
3682         }
3683       else
3684         {
3685           tree newtype = default_function_type;
3686           if (b->u.type)
3687             TREE_TYPE (decl) = b->u.type;
3688           /* Implicit declaration of a function already declared
3689              (somehow) in a different scope, or as a built-in.
3690              If this is the first time this has happened, warn;
3691              then recycle the old declaration but with the new type.  */
3692           if (!C_DECL_IMPLICIT (decl))
3693             {
3694               implicit_decl_warning (loc, functionid, decl);
3695               C_DECL_IMPLICIT (decl) = 1;
3696             }
3697           if (fndecl_built_in_p (decl))
3698             {
3699               newtype = build_type_attribute_variant (newtype,
3700                                                       TYPE_ATTRIBUTES
3701                                                       (TREE_TYPE (decl)));
3702               if (!comptypes (newtype, TREE_TYPE (decl)))
3703                 {
3704                   auto_diagnostic_group d;
3705                   bool warned = warning_at (loc,
3706                                             OPT_Wbuiltin_declaration_mismatch,
3707                                             "incompatible implicit "
3708                                             "declaration of built-in "
3709                                             "function %qD", decl);
3710                   /* See if we can hint which header to include.  */
3711                   const char *header = header_for_builtin_fn (decl);
3712                   if (header != NULL && warned)
3713                     {
3714                       rich_location richloc (line_table, loc);
3715                       maybe_add_include_fixit (&richloc, header, true);
3716                       inform (&richloc,
3717                               "include %qs or provide a declaration of %qD",
3718                               header, decl);
3719                     }
3720                   newtype = TREE_TYPE (decl);
3721                 }
3722             }
3723           else
3724             {
3725               if (!comptypes (newtype, TREE_TYPE (decl)))
3726                 {
3727                   auto_diagnostic_group d;
3728                   error_at (loc, "incompatible implicit declaration of "
3729                             "function %qD", decl);
3730                   locate_old_decl (decl);
3731                 }
3732             }
3733           b->u.type = TREE_TYPE (decl);
3734           TREE_TYPE (decl) = newtype;
3735           bind (functionid, decl, current_scope,
3736                 /*invisible=*/false, /*nested=*/true,
3737                 DECL_SOURCE_LOCATION (decl));
3738           return decl;
3739         }
3740     }
3741
3742   /* Not seen before.  */
3743   decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3744   DECL_EXTERNAL (decl) = 1;
3745   TREE_PUBLIC (decl) = 1;
3746   C_DECL_IMPLICIT (decl) = 1;
3747   implicit_decl_warning (loc, functionid, 0);
3748   asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3749   if (asmspec_tree)
3750     set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3751
3752   /* C89 says implicit declarations are in the innermost block.
3753      So we record the decl in the standard fashion.  */
3754   decl = pushdecl (decl);
3755
3756   /* No need to call objc_check_decl here - it's a function type.  */
3757   rest_of_decl_compilation (decl, 0, 0);
3758
3759   /* Write a record describing this implicit function declaration
3760      to the prototypes file (if requested).  */
3761   gen_aux_info_record (decl, 0, 1, 0);
3762
3763   /* Possibly apply some default attributes to this implicit declaration.  */
3764   decl_attributes (&decl, NULL_TREE, 0);
3765
3766   return decl;
3767 }
3768
3769 /* Issue an error message for a reference to an undeclared variable
3770    ID, including a reference to a builtin outside of function-call
3771    context.  Establish a binding of the identifier to error_mark_node
3772    in an appropriate scope, which will suppress further errors for the
3773    same identifier.  The error message should be given location LOC.  */
3774 void
3775 undeclared_variable (location_t loc, tree id)
3776 {
3777   static bool already = false;
3778   struct c_scope *scope;
3779
3780   auto_diagnostic_group d;
3781   if (current_function_decl == NULL_TREE)
3782     {
3783       name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3784       if (const char *suggestion = guessed_id.suggestion ())
3785         {
3786           gcc_rich_location richloc (loc);
3787           richloc.add_fixit_replace (suggestion);
3788           error_at (&richloc,
3789                     "%qE undeclared here (not in a function);"
3790                     " did you mean %qs?",
3791                     id, suggestion);
3792         }
3793       else
3794         error_at (loc, "%qE undeclared here (not in a function)", id);
3795       scope = current_scope;
3796     }
3797   else
3798     {
3799       if (!objc_diagnose_private_ivar (id))
3800         {
3801           name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3802           if (const char *suggestion = guessed_id.suggestion ())
3803             {
3804               gcc_rich_location richloc (loc);
3805               richloc.add_fixit_replace (suggestion);
3806               error_at (&richloc,
3807                         "%qE undeclared (first use in this function);"
3808                         " did you mean %qs?",
3809                         id, suggestion);
3810             }
3811           else
3812             error_at (loc, "%qE undeclared (first use in this function)", id);
3813         }
3814       if (!already)
3815         {
3816           inform (loc, "each undeclared identifier is reported only"
3817                   " once for each function it appears in");
3818           already = true;
3819         }
3820
3821       /* If we are parsing old-style parameter decls, current_function_decl
3822          will be nonnull but current_function_scope will be null.  */
3823       scope = current_function_scope ? current_function_scope : current_scope;
3824     }
3825   bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3826         UNKNOWN_LOCATION);
3827 }
3828 \f
3829 /* Subroutine of lookup_label, declare_label, define_label: construct a
3830    LABEL_DECL with all the proper frills.  Also create a struct
3831    c_label_vars initialized for the current scope.  */
3832
3833 static tree
3834 make_label (location_t location, tree name, bool defining,
3835             struct c_label_vars **p_label_vars)
3836 {
3837   tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3838   DECL_CONTEXT (label) = current_function_decl;
3839   SET_DECL_MODE (label, VOIDmode);
3840
3841   c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3842   label_vars->shadowed = NULL;
3843   set_spot_bindings (&label_vars->label_bindings, defining);
3844   label_vars->decls_in_scope = make_tree_vector ();
3845   label_vars->gotos = NULL;
3846   *p_label_vars = label_vars;
3847
3848   return label;
3849 }
3850
3851 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3852    Create one if none exists so far for the current function.
3853    This is called when a label is used in a goto expression or
3854    has its address taken.  */
3855
3856 tree
3857 lookup_label (tree name)
3858 {
3859   tree label;
3860   struct c_label_vars *label_vars;
3861
3862   if (current_function_scope == 0)
3863     {
3864       error ("label %qE referenced outside of any function", name);
3865       return NULL_TREE;
3866     }
3867
3868   /* Use a label already defined or ref'd with this name, but not if
3869      it is inherited from a containing function and wasn't declared
3870      using __label__.  */
3871   label = I_LABEL_DECL (name);
3872   if (label && (DECL_CONTEXT (label) == current_function_decl
3873                 || C_DECLARED_LABEL_FLAG (label)))
3874     {
3875       /* If the label has only been declared, update its apparent
3876          location to point here, for better diagnostics if it
3877          turns out not to have been defined.  */
3878       if (DECL_INITIAL (label) == NULL_TREE)
3879         DECL_SOURCE_LOCATION (label) = input_location;
3880       return label;
3881     }
3882
3883   /* No label binding for that identifier; make one.  */
3884   label = make_label (input_location, name, false, &label_vars);
3885
3886   /* Ordinary labels go in the current function scope.  */
3887   bind_label (name, label, current_function_scope, label_vars);
3888
3889   return label;
3890 }
3891
3892 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3893    to LABEL.  */
3894
3895 static void
3896 warn_about_goto (location_t goto_loc, tree label, tree decl)
3897 {
3898   auto_diagnostic_group d;
3899   if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3900     error_at (goto_loc,
3901               "jump into scope of identifier with variably modified type");
3902   else
3903     if (!warning_at (goto_loc, OPT_Wjump_misses_init,
3904                      "jump skips variable initialization"))
3905       return;
3906   inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3907   inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3908 }
3909
3910 /* Look up a label because of a goto statement.  This is like
3911    lookup_label, but also issues any appropriate warnings.  */
3912
3913 tree
3914 lookup_label_for_goto (location_t loc, tree name)
3915 {
3916   tree label;
3917   struct c_label_vars *label_vars;
3918   unsigned int ix;
3919   tree decl;
3920
3921   label = lookup_label (name);
3922   if (label == NULL_TREE)
3923     return NULL_TREE;
3924
3925   /* If we are jumping to a different function, we can't issue any
3926      useful warnings.  */
3927   if (DECL_CONTEXT (label) != current_function_decl)
3928     {
3929       gcc_assert (C_DECLARED_LABEL_FLAG (label));
3930       return label;
3931     }
3932
3933   label_vars = I_LABEL_BINDING (name)->u.label;
3934
3935   /* If the label has not yet been defined, then push this goto on a
3936      list for possible later warnings.  */
3937   if (label_vars->label_bindings.scope == NULL)
3938     {
3939       c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3940
3941       g->loc = loc;
3942       set_spot_bindings (&g->goto_bindings, true);
3943       vec_safe_push (label_vars->gotos, g);
3944       return label;
3945     }
3946
3947   /* If there are any decls in label_vars->decls_in_scope, then this
3948      goto has missed the declaration of the decl.  This happens for a
3949      case like
3950        int i = 1;
3951       lab:
3952        ...
3953        goto lab;
3954      Issue a warning or error.  */
3955   FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3956     warn_about_goto (loc, label, decl);
3957
3958   if (label_vars->label_bindings.left_stmt_expr)
3959     {
3960       auto_diagnostic_group d;
3961       error_at (loc, "jump into statement expression");
3962       inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3963     }
3964
3965   return label;
3966 }
3967
3968 /* Make a label named NAME in the current function, shadowing silently
3969    any that may be inherited from containing functions or containing
3970    scopes.  This is called for __label__ declarations.  */
3971
3972 tree
3973 declare_label (tree name)
3974 {
3975   struct c_binding *b = I_LABEL_BINDING (name);
3976   tree label;
3977   struct c_label_vars *label_vars;
3978
3979   /* Check to make sure that the label hasn't already been declared
3980      at this scope */
3981   if (b && B_IN_CURRENT_SCOPE (b))
3982     {
3983       auto_diagnostic_group d;
3984       error ("duplicate label declaration %qE", name);
3985       locate_old_decl (b->decl);
3986
3987       /* Just use the previous declaration.  */
3988       return b->decl;
3989     }
3990
3991   label = make_label (input_location, name, false, &label_vars);
3992   C_DECLARED_LABEL_FLAG (label) = 1;
3993
3994   /* Declared labels go in the current scope.  */
3995   bind_label (name, label, current_scope, label_vars);
3996
3997   return label;
3998 }
3999
4000 /* When we define a label, issue any appropriate warnings if there are
4001    any gotos earlier in the function which jump to this label.  */
4002
4003 static void
4004 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4005 {
4006   unsigned int ix;
4007   struct c_goto_bindings *g;
4008
4009   FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4010     {
4011       struct c_binding *b;
4012       struct c_scope *scope;
4013
4014       /* We have a goto to this label.  The goto is going forward.  In
4015          g->scope, the goto is going to skip any binding which was
4016          defined after g->bindings_in_scope.  */
4017       if (g->goto_bindings.scope->has_jump_unsafe_decl)
4018         {
4019           for (b = g->goto_bindings.scope->bindings;
4020                b != g->goto_bindings.bindings_in_scope;
4021                b = b->prev)
4022             {
4023               if (decl_jump_unsafe (b->decl))
4024                 warn_about_goto (g->loc, label, b->decl);
4025             }
4026         }
4027
4028       /* We also need to warn about decls defined in any scopes
4029          between the scope of the label and the scope of the goto.  */
4030       for (scope = label_vars->label_bindings.scope;
4031            scope != g->goto_bindings.scope;
4032            scope = scope->outer)
4033         {
4034           gcc_assert (scope != NULL);
4035           if (scope->has_jump_unsafe_decl)
4036             {
4037               if (scope == label_vars->label_bindings.scope)
4038                 b = label_vars->label_bindings.bindings_in_scope;
4039               else
4040                 b = scope->bindings;
4041               for (; b != NULL; b = b->prev)
4042                 {
4043                   if (decl_jump_unsafe (b->decl))
4044                     warn_about_goto (g->loc, label, b->decl);
4045                 }
4046             }
4047         }
4048
4049       if (g->goto_bindings.stmt_exprs > 0)
4050         {
4051           auto_diagnostic_group d;
4052           error_at (g->loc, "jump into statement expression");
4053           inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4054                   label);
4055         }
4056     }
4057
4058   /* Now that the label is defined, we will issue warnings about
4059      subsequent gotos to this label when we see them.  */
4060   vec_safe_truncate (label_vars->gotos, 0);
4061   label_vars->gotos = NULL;
4062 }
4063
4064 /* Define a label, specifying the location in the source file.
4065    Return the LABEL_DECL node for the label, if the definition is valid.
4066    Otherwise return NULL_TREE.  */
4067
4068 tree
4069 define_label (location_t location, tree name)
4070 {
4071   /* Find any preexisting label with this name.  It is an error
4072      if that label has already been defined in this function, or
4073      if there is a containing function with a declared label with
4074      the same name.  */
4075   tree label = I_LABEL_DECL (name);
4076
4077   if (label
4078       && ((DECL_CONTEXT (label) == current_function_decl
4079            && DECL_INITIAL (label) != NULL_TREE)
4080           || (DECL_CONTEXT (label) != current_function_decl
4081               && C_DECLARED_LABEL_FLAG (label))))
4082     {
4083       auto_diagnostic_group d;
4084       error_at (location, "duplicate label %qD", label);
4085       locate_old_decl (label);
4086       return NULL_TREE;
4087     }
4088   else if (label && DECL_CONTEXT (label) == current_function_decl)
4089     {
4090       struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4091
4092       /* The label has been used or declared already in this function,
4093          but not defined.  Update its location to point to this
4094          definition.  */
4095       DECL_SOURCE_LOCATION (label) = location;
4096       set_spot_bindings (&label_vars->label_bindings, true);
4097
4098       /* Issue warnings as required about any goto statements from
4099          earlier in the function.  */
4100       check_earlier_gotos (label, label_vars);
4101     }
4102   else
4103     {
4104       struct c_label_vars *label_vars;
4105
4106       /* No label binding for that identifier; make one.  */
4107       label = make_label (location, name, true, &label_vars);
4108
4109       /* Ordinary labels go in the current function scope.  */
4110       bind_label (name, label, current_function_scope, label_vars);
4111     }
4112
4113   if (!in_system_header_at (input_location) && lookup_name (name))
4114     warning_at (location, OPT_Wtraditional,
4115                 "traditional C lacks a separate namespace "
4116                 "for labels, identifier %qE conflicts", name);
4117
4118   /* Mark label as having been defined.  */
4119   DECL_INITIAL (label) = error_mark_node;
4120   return label;
4121 }
4122 \f
4123 /* Get the bindings for a new switch statement.  This is used to issue
4124    warnings as appropriate for jumps from the switch to case or
4125    default labels.  */
4126
4127 struct c_spot_bindings *
4128 c_get_switch_bindings (void)
4129 {
4130   struct c_spot_bindings *switch_bindings;
4131
4132   switch_bindings = XNEW (struct c_spot_bindings);
4133   set_spot_bindings (switch_bindings, true);
4134   return switch_bindings;
4135 }
4136
4137 void
4138 c_release_switch_bindings (struct c_spot_bindings *bindings)
4139 {
4140   gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4141   XDELETE (bindings);
4142 }
4143
4144 /* This is called at the point of a case or default label to issue
4145    warnings about decls as needed.  It returns true if it found an
4146    error, not just a warning.  */
4147
4148 bool
4149 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4150                               location_t switch_loc, location_t case_loc)
4151 {
4152   bool saw_error;
4153   struct c_scope *scope;
4154
4155   saw_error = false;
4156   for (scope = current_scope;
4157        scope != switch_bindings->scope;
4158        scope = scope->outer)
4159     {
4160       struct c_binding *b;
4161
4162       gcc_assert (scope != NULL);
4163
4164       if (!scope->has_jump_unsafe_decl)
4165         continue;
4166
4167       for (b = scope->bindings; b != NULL; b = b->prev)
4168         {
4169           if (decl_jump_unsafe (b->decl))
4170             {
4171               auto_diagnostic_group d;
4172               bool emitted;
4173               if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
4174                 {
4175                   saw_error = true;
4176                   error_at (case_loc,
4177                             ("switch jumps into scope of identifier with "
4178                              "variably modified type"));
4179                   emitted = true;
4180                 }
4181               else
4182                 emitted
4183                   = warning_at (case_loc, OPT_Wjump_misses_init,
4184                                 "switch jumps over variable initialization");
4185               if (emitted)
4186                 {
4187                   inform (switch_loc, "switch starts here");
4188                   inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4189                           b->decl);
4190                 }
4191             }
4192         }
4193     }
4194
4195   if (switch_bindings->stmt_exprs > 0)
4196     {
4197       saw_error = true;
4198       auto_diagnostic_group d;
4199       error_at (case_loc, "switch jumps into statement expression");
4200       inform (switch_loc, "switch starts here");
4201     }
4202
4203   return saw_error;
4204 }
4205 \f
4206 /* Given NAME, an IDENTIFIER_NODE,
4207    return the structure (or union or enum) definition for that name.
4208    If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4209    CODE says which kind of type the caller wants;
4210    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4211    If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4212    location where the tag was defined.
4213    If the wrong kind of type is found, an error is reported.  */
4214
4215 static tree
4216 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4217             location_t *ploc)
4218 {
4219   struct c_binding *b = I_TAG_BINDING (name);
4220   bool thislevel = false;
4221
4222   if (!b || !b->decl)
4223     return NULL_TREE;
4224
4225   /* We only care about whether it's in this level if
4226      thislevel_only was set or it might be a type clash.  */
4227   if (thislevel_only || TREE_CODE (b->decl) != code)
4228     {
4229       /* For our purposes, a tag in the external scope is the same as
4230          a tag in the file scope.  (Primarily relevant to Objective-C
4231          and its builtin structure tags, which get pushed before the
4232          file scope is created.)  */
4233       if (B_IN_CURRENT_SCOPE (b)
4234           || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4235         thislevel = true;
4236     }
4237
4238   if (thislevel_only && !thislevel)
4239     return NULL_TREE;
4240
4241   if (TREE_CODE (b->decl) != code)
4242     {
4243       /* Definition isn't the kind we were looking for.  */
4244       pending_invalid_xref = name;
4245       pending_invalid_xref_location = input_location;
4246
4247       /* If in the same binding level as a declaration as a tag
4248          of a different type, this must not be allowed to
4249          shadow that tag, so give the error immediately.
4250          (For example, "struct foo; union foo;" is invalid.)  */
4251       if (thislevel)
4252         pending_xref_error ();
4253     }
4254
4255   if (ploc != NULL)
4256     *ploc = b->locus;
4257
4258   return b->decl;
4259 }
4260
4261 /* Return true if a definition exists for NAME with code CODE.  */
4262
4263 bool
4264 tag_exists_p (enum tree_code code, tree name)
4265 {
4266   struct c_binding *b = I_TAG_BINDING (name);
4267
4268   if (b == NULL || b->decl == NULL_TREE)
4269     return false;
4270   return TREE_CODE (b->decl) == code;
4271 }
4272
4273 /* Print an error message now
4274    for a recent invalid struct, union or enum cross reference.
4275    We don't print them immediately because they are not invalid
4276    when used in the `struct foo;' construct for shadowing.  */
4277
4278 void
4279 pending_xref_error (void)
4280 {
4281   if (pending_invalid_xref != NULL_TREE)
4282     error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4283               pending_invalid_xref);
4284   pending_invalid_xref = NULL_TREE;
4285 }
4286
4287 \f
4288 /* Look up NAME in the current scope and its superiors
4289    in the namespace of variables, functions and typedefs.
4290    Return a ..._DECL node of some kind representing its definition,
4291    or return NULL_TREE if it is undefined.  */
4292
4293 tree
4294 lookup_name (tree name)
4295 {
4296   struct c_binding *b = I_SYMBOL_BINDING (name);
4297   if (b && !b->invisible)
4298     {
4299       maybe_record_typedef_use (b->decl);
4300       return b->decl;
4301     }
4302   return NULL_TREE;
4303 }
4304
4305 /* Similar to `lookup_name' but look only at the indicated scope.  */
4306
4307 static tree
4308 lookup_name_in_scope (tree name, struct c_scope *scope)
4309 {
4310   struct c_binding *b;
4311
4312   for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4313     if (B_IN_SCOPE (b, scope))
4314       return b->decl;
4315   return NULL_TREE;
4316 }
4317
4318 /* Look for the closest match for NAME within the currently valid
4319    scopes.
4320
4321    This finds the identifier with the lowest Levenshtein distance to
4322    NAME.  If there are multiple candidates with equal minimal distance,
4323    the first one found is returned.  Scopes are searched from innermost
4324    outwards, and within a scope in reverse order of declaration, thus
4325    benefiting candidates "near" to the current scope.
4326
4327    The function also looks for similar macro names to NAME, since a
4328    misspelled macro name will not be expanded, and hence looks like an
4329    identifier to the C frontend.
4330
4331    It also looks for start_typename keywords, to detect "singed" vs "signed"
4332    typos.
4333
4334    Use LOC for any deferred diagnostics.  */
4335
4336 name_hint
4337 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4338 {
4339   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4340
4341   /* First, try some well-known names in the C standard library, in case
4342      the user forgot a #include.  */
4343   const char *header_hint
4344     = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4345
4346   if (header_hint)
4347     return name_hint (NULL,
4348                       new suggest_missing_header (loc,
4349                                                   IDENTIFIER_POINTER (name),
4350                                                   header_hint));
4351
4352   /* Only suggest names reserved for the implementation if NAME begins
4353      with an underscore.  */
4354   bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4355
4356   best_match<tree, tree> bm (name);
4357
4358   /* Look within currently valid scopes.  */
4359   for (c_scope *scope = current_scope; scope; scope = scope->outer)
4360     for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4361       {
4362         if (!binding->id || binding->invisible)
4363           continue;
4364         if (binding->decl == error_mark_node)
4365           continue;
4366         /* Don't use bindings from implicitly declared functions,
4367            as they were likely misspellings themselves.  */
4368         if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4369           if (C_DECL_IMPLICIT (binding->decl))
4370             continue;
4371         /* Don't suggest names that are reserved for use by the
4372            implementation, unless NAME began with an underscore.  */
4373         if (!consider_implementation_names)
4374           {
4375             const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4376             if (name_reserved_for_implementation_p (suggestion_str))
4377               continue;
4378           }
4379         switch (kind)
4380           {
4381           case FUZZY_LOOKUP_TYPENAME:
4382             if (TREE_CODE (binding->decl) != TYPE_DECL)
4383               continue;
4384             break;
4385
4386           case FUZZY_LOOKUP_FUNCTION_NAME:
4387             if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4388               {
4389                 /* Allow function pointers.  */
4390                 if ((VAR_P (binding->decl)
4391                      || TREE_CODE (binding->decl) == PARM_DECL)
4392                     && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4393                     && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4394                         == FUNCTION_TYPE))
4395                   break;
4396                 continue;
4397               }
4398             break;
4399
4400           default:
4401             break;
4402           }
4403         bm.consider (binding->id);
4404       }
4405
4406   /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4407      as:
4408        x = SOME_OTHER_MACRO (y);
4409      then "SOME_OTHER_MACRO" will survive to the frontend and show up
4410      as a misspelled identifier.
4411
4412      Use the best distance so far so that a candidate is only set if
4413      a macro is better than anything so far.  This allows early rejection
4414      (without calculating the edit distance) of macro names that must have
4415      distance >= bm.get_best_distance (), and means that we only get a
4416      non-NULL result for best_macro_match if it's better than any of
4417      the identifiers already checked, which avoids needless creation
4418      of identifiers for macro hashnodes.  */
4419   best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4420   cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4421   /* If a macro is the closest so far to NAME, use it, creating an
4422      identifier tree node for it.  */
4423   if (best_macro)
4424     {
4425       const char *id = (const char *)best_macro->ident.str;
4426       tree macro_as_identifier
4427         = get_identifier_with_length (id, best_macro->ident.len);
4428       bm.set_best_so_far (macro_as_identifier,
4429                           bmm.get_best_distance (),
4430                           bmm.get_best_candidate_length ());
4431     }
4432
4433   /* Try the "start_typename" keywords to detect
4434      "singed" vs "signed" typos.  */
4435   if (kind == FUZZY_LOOKUP_TYPENAME)
4436     {
4437       for (unsigned i = 0; i < num_c_common_reswords; i++)
4438         {
4439           const c_common_resword *resword = &c_common_reswords[i];
4440           if (!c_keyword_starts_typename (resword->rid))
4441             continue;
4442           tree resword_identifier = ridpointers [resword->rid];
4443           if (!resword_identifier)
4444             continue;
4445           gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4446           bm.consider (resword_identifier);
4447         }
4448     }
4449
4450   tree best = bm.get_best_meaningful_candidate ();
4451   if (best)
4452     return name_hint (IDENTIFIER_POINTER (best), NULL);
4453   else
4454     return name_hint (NULL, NULL);
4455 }
4456
4457 \f
4458 /* Handle the standard [[nodiscard]] attribute.  */
4459
4460 static tree
4461 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4462                             int /*flags*/, bool *no_add_attrs)
4463 {
4464   if (TREE_CODE (*node) == FUNCTION_DECL)
4465     {
4466       if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4467         warning_at (DECL_SOURCE_LOCATION (*node),
4468                     OPT_Wattributes, "%qE attribute applied to %qD with void "
4469                     "return type", name, *node);
4470     }
4471   else if (RECORD_OR_UNION_TYPE_P (*node)
4472            || TREE_CODE (*node) == ENUMERAL_TYPE)
4473     /* OK */;
4474   else
4475     {
4476       pedwarn (input_location,
4477                OPT_Wattributes, "%qE attribute can only be applied to "
4478                "functions or to structure, union or enumeration types", name);
4479       *no_add_attrs = true;
4480     }
4481   return NULL_TREE;
4482 }
4483
4484 /* Handle the standard [[noreturn]] attribute.  */
4485
4486 static tree
4487 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4488                                int flags, bool *no_add_attrs)
4489 {
4490   /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4491      only applies to functions, not function pointers.  */
4492   if (TREE_CODE (*node) == FUNCTION_DECL)
4493     return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4494   else
4495     {
4496       pedwarn (input_location, OPT_Wattributes,
4497                "standard %qE attribute can only be applied to functions",
4498                name);
4499       *no_add_attrs = true;
4500       return NULL_TREE;
4501     }
4502 }
4503
4504 /* Table of supported standard (C2x) attributes.  */
4505 const struct attribute_spec std_attribute_table[] =
4506 {
4507   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4508        affects_type_identity, handler, exclude } */
4509   { "_Noreturn", 0, 0, false, false, false, false,
4510     handle_std_noreturn_attribute, NULL },
4511   { "deprecated", 0, 1, false, false, false, false,
4512     handle_deprecated_attribute, NULL },
4513   { "fallthrough", 0, 0, false, false, false, false,
4514     handle_fallthrough_attribute, NULL },
4515   { "maybe_unused", 0, 0, false, false, false, false,
4516     handle_unused_attribute, NULL },
4517   { "nodiscard", 0, 1, false, false, false, false,
4518     handle_nodiscard_attribute, NULL },
4519   { "noreturn", 0, 0, false, false, false, false,
4520     handle_std_noreturn_attribute, NULL },
4521   { NULL, 0, 0, false, false, false, false, NULL, NULL }
4522 };
4523
4524 /* Create the predefined scalar types of C,
4525    and some nodes representing standard constants (0, 1, (void *) 0).
4526    Initialize the global scope.
4527    Make definitions for built-in primitive functions.  */
4528
4529 void
4530 c_init_decl_processing (void)
4531 {
4532   location_t save_loc = input_location;
4533
4534   /* Initialize reserved words for parser.  */
4535   c_parse_init ();
4536
4537   register_scoped_attributes (std_attribute_table, NULL);
4538
4539   current_function_decl = NULL_TREE;
4540
4541   gcc_obstack_init (&parser_obstack);
4542
4543   /* Make the externals scope.  */
4544   push_scope ();
4545   external_scope = current_scope;
4546
4547   /* Declarations from c_common_nodes_and_builtins must not be associated
4548      with this input file, lest we get differences between using and not
4549      using preprocessed headers.  */
4550   input_location = BUILTINS_LOCATION;
4551
4552   c_common_nodes_and_builtins ();
4553
4554   /* In C, comparisons and TRUTH_* expressions have type int.  */
4555   truthvalue_type_node = integer_type_node;
4556   truthvalue_true_node = integer_one_node;
4557   truthvalue_false_node = integer_zero_node;
4558
4559   /* Even in C99, which has a real boolean type.  */
4560   pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4561                         boolean_type_node));
4562
4563   /* C-specific nullptr initialization.  */
4564   record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4565   /* The size and alignment of nullptr_t is the same as for a pointer to
4566      character type.  */
4567   SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4568
4569   input_location = save_loc;
4570
4571   make_fname_decl = c_make_fname_decl;
4572   start_fname_decls ();
4573 }
4574
4575 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4576    give the decl, NAME is the initialization string and TYPE_DEP
4577    indicates whether NAME depended on the type of the function.  As we
4578    don't yet implement delayed emission of static data, we mark the
4579    decl as emitted so it is not placed in the output.  Anything using
4580    it must therefore pull out the STRING_CST initializer directly.
4581    FIXME.  */
4582
4583 static tree
4584 c_make_fname_decl (location_t loc, tree id, int type_dep)
4585 {
4586   const char *name = fname_as_string (type_dep);
4587   tree decl, type, init;
4588   size_t length = strlen (name);
4589
4590   type = build_array_type (char_type_node,
4591                            build_index_type (size_int (length)));
4592   type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4593
4594   decl = build_decl (loc, VAR_DECL, id, type);
4595
4596   TREE_STATIC (decl) = 1;
4597   TREE_READONLY (decl) = 1;
4598   DECL_ARTIFICIAL (decl) = 1;
4599
4600   init = build_string (length + 1, name);
4601   free (CONST_CAST (char *, name));
4602   TREE_TYPE (init) = type;
4603   DECL_INITIAL (decl) = init;
4604
4605   TREE_USED (decl) = 1;
4606
4607   if (current_function_decl
4608       /* For invalid programs like this:
4609
4610          void foo()
4611          const char* p = __FUNCTION__;
4612
4613          the __FUNCTION__ is believed to appear in K&R style function
4614          parameter declarator.  In that case we still don't have
4615          function_scope.  */
4616       && current_function_scope)
4617     {
4618       DECL_CONTEXT (decl) = current_function_decl;
4619       bind (id, decl, current_function_scope,
4620             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4621     }
4622
4623   finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4624
4625   return decl;
4626 }
4627
4628 tree
4629 c_builtin_function (tree decl)
4630 {
4631   tree type = TREE_TYPE (decl);
4632   tree   id = DECL_NAME (decl);
4633
4634   const char *name = IDENTIFIER_POINTER (id);
4635   C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4636
4637   /* Should never be called on a symbol with a preexisting meaning.  */
4638   gcc_assert (!I_SYMBOL_BINDING (id));
4639
4640   bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4641         UNKNOWN_LOCATION);
4642
4643   /* Builtins in the implementation namespace are made visible without
4644      needing to be explicitly declared.  See push_file_scope.  */
4645   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4646     {
4647       DECL_CHAIN (decl) = visible_builtins;
4648       visible_builtins = decl;
4649     }
4650
4651   return decl;
4652 }
4653
4654 tree
4655 c_builtin_function_ext_scope (tree decl)
4656 {
4657   tree type = TREE_TYPE (decl);
4658   tree   id = DECL_NAME (decl);
4659
4660   const char *name = IDENTIFIER_POINTER (id);
4661   C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4662
4663   if (external_scope)
4664     bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4665           UNKNOWN_LOCATION);
4666
4667   /* Builtins in the implementation namespace are made visible without
4668      needing to be explicitly declared.  See push_file_scope.  */
4669   if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4670     {
4671       DECL_CHAIN (decl) = visible_builtins;
4672       visible_builtins = decl;
4673     }
4674
4675   return decl;
4676 }
4677
4678 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL.  */
4679
4680 tree
4681 c_simulate_builtin_function_decl (tree decl)
4682 {
4683   tree type = TREE_TYPE (decl);
4684   C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4685   return pushdecl (decl);
4686 }
4687
4688 /* Warn about attributes in a context where they are unused
4689    (attribute-declarations, except for the "fallthrough" case, and
4690    attributes on statements).  */
4691
4692 void
4693 c_warn_unused_attributes (tree attrs)
4694 {
4695   for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4696     if (get_attribute_namespace (t) == NULL_TREE)
4697       /* The specifications of standard attributes mean this is a
4698          constraint violation.  */
4699       pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4700                get_attribute_name (t));
4701     else if (!attribute_ignored_p (t))
4702       warning (OPT_Wattributes, "%qE attribute ignored",
4703                get_attribute_name (t));
4704 }
4705
4706 /* Warn for standard attributes being applied to a type that is not
4707    being defined, where that is a constraint violation, and return a
4708    list of attributes with them removed.  */
4709
4710 tree
4711 c_warn_type_attributes (tree attrs)
4712 {
4713   tree *attr_ptr = &attrs;
4714   while (*attr_ptr)
4715     if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4716       {
4717         pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4718                  get_attribute_name (*attr_ptr));
4719         *attr_ptr = TREE_CHAIN (*attr_ptr);
4720       }
4721     else
4722       attr_ptr = &TREE_CHAIN (*attr_ptr);
4723   return attrs;
4724 }
4725 \f
4726 /* Called when a declaration is seen that contains no names to declare.
4727    If its type is a reference to a structure, union or enum inherited
4728    from a containing scope, shadow that tag name for the current scope
4729    with a forward reference.
4730    If its type defines a new named structure or union
4731    or defines an enum, it is valid but we need not do anything here.
4732    Otherwise, it is an error.  */
4733
4734 void
4735 shadow_tag (const struct c_declspecs *declspecs)
4736 {
4737   shadow_tag_warned (declspecs, 0);
4738 }
4739
4740 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4741    but no pedwarn.  */
4742 void
4743 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4744 {
4745   bool found_tag = false;
4746
4747   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4748     {
4749       tree value = declspecs->type;
4750       enum tree_code code = TREE_CODE (value);
4751
4752       if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4753         /* Used to test also that TYPE_SIZE (value) != 0.
4754            That caused warning for `struct foo;' at top level in the file.  */
4755         {
4756           tree name = TYPE_NAME (value);
4757           tree t;
4758
4759           found_tag = true;
4760
4761           if (declspecs->restrict_p)
4762             {
4763               error ("invalid use of %<restrict%>");
4764               warned = 1;
4765             }
4766
4767           if (name == NULL_TREE)
4768             {
4769               if (warned != 1 && code != ENUMERAL_TYPE)
4770                 /* Empty unnamed enum OK */
4771                 {
4772                   pedwarn (input_location, 0,
4773                            "unnamed struct/union that defines no instances");
4774                   warned = 1;
4775                 }
4776             }
4777           else if (declspecs->typespec_kind != ctsk_tagdef
4778                    && declspecs->typespec_kind != ctsk_tagfirstref
4779                    && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4780                    && declspecs->storage_class != csc_none)
4781             {
4782               if (warned != 1)
4783                 pedwarn (input_location, 0,
4784                          "empty declaration with storage class specifier "
4785                          "does not redeclare tag");
4786               warned = 1;
4787               pending_xref_error ();
4788             }
4789           else if (declspecs->typespec_kind != ctsk_tagdef
4790                    && declspecs->typespec_kind != ctsk_tagfirstref
4791                    && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4792                    && (declspecs->const_p
4793                        || declspecs->volatile_p
4794                        || declspecs->atomic_p
4795                        || declspecs->restrict_p
4796                        || declspecs->address_space))
4797             {
4798               if (warned != 1)
4799                 pedwarn (input_location, 0,
4800                          "empty declaration with type qualifier "
4801                           "does not redeclare tag");
4802               warned = 1;
4803               pending_xref_error ();
4804             }
4805           else if (declspecs->typespec_kind != ctsk_tagdef
4806                    && declspecs->typespec_kind != ctsk_tagfirstref
4807                    && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4808                    && declspecs->alignas_p)
4809             {
4810               if (warned != 1)
4811                 pedwarn (input_location, 0,
4812                          "empty declaration with %<_Alignas%> "
4813                           "does not redeclare tag");
4814               warned = 1;
4815               pending_xref_error ();
4816             }
4817           else
4818             {
4819               pending_invalid_xref = NULL_TREE;
4820               t = lookup_tag (code, name, true, NULL);
4821
4822               if (t == NULL_TREE)
4823                 {
4824                   t = make_node (code);
4825                   pushtag (input_location, name, t);
4826                 }
4827             }
4828         }
4829       else
4830         {
4831           if (warned != 1 && !in_system_header_at (input_location))
4832             {
4833               pedwarn (input_location, 0,
4834                        "useless type name in empty declaration");
4835               warned = 1;
4836             }
4837         }
4838     }
4839   else if (warned != 1 && !in_system_header_at (input_location)
4840            && declspecs->typedef_p)
4841     {
4842       pedwarn (input_location, 0, "useless type name in empty declaration");
4843       warned = 1;
4844     }
4845
4846   pending_invalid_xref = NULL_TREE;
4847
4848   if (declspecs->inline_p)
4849     {
4850       error ("%<inline%> in empty declaration");
4851       warned = 1;
4852     }
4853
4854   if (declspecs->noreturn_p)
4855     {
4856       error ("%<_Noreturn%> in empty declaration");
4857       warned = 1;
4858     }
4859
4860   if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4861     {
4862       error ("%<auto%> in file-scope empty declaration");
4863       warned = 1;
4864     }
4865
4866   if (current_scope == file_scope && declspecs->storage_class == csc_register)
4867     {
4868       error ("%<register%> in file-scope empty declaration");
4869       warned = 1;
4870     }
4871
4872   if (!warned && !in_system_header_at (input_location)
4873       && declspecs->storage_class != csc_none)
4874     {
4875       warning (0, "useless storage class specifier in empty declaration");
4876       warned = 2;
4877     }
4878
4879   if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4880     {
4881       warning (0, "useless %qs in empty declaration",
4882                declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4883       warned = 2;
4884     }
4885
4886   if (!warned
4887       && !in_system_header_at (input_location)
4888       && (declspecs->const_p
4889           || declspecs->volatile_p
4890           || declspecs->atomic_p
4891           || declspecs->restrict_p
4892           || declspecs->address_space))
4893     {
4894       warning (0, "useless type qualifier in empty declaration");
4895       warned = 2;
4896     }
4897
4898   if (!warned && !in_system_header_at (input_location)
4899       && declspecs->alignas_p)
4900     {
4901       warning (0, "useless %<_Alignas%> in empty declaration");
4902       warned = 2;
4903     }
4904
4905   if (found_tag
4906       && warned == 2
4907       && (declspecs->typespec_kind == ctsk_tagref_attrs
4908           || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
4909     {
4910       /* Standard attributes after the "struct" or "union" keyword are
4911          only permitted when the contents of the type are defined, or
4912          in the form "struct-or-union attribute-specifier-sequence
4913          identifier;".  If the ';' was not present, attributes were
4914          diagnosed in the parser.  Here, ensure that any other useless
4915          elements of the declaration result in a pedwarn, not just a
4916          warning.  Forward declarations of enum types are not part of
4917          standard C, but handle them the same.  */
4918       pedwarn (input_location, 0,
4919                "invalid use of attributes in empty declaration");
4920       warned = 1;
4921     }
4922
4923   if (warned != 1)
4924     {
4925       if (declspecs->declspecs_seen_p
4926           && !declspecs->non_std_attrs_seen_p)
4927         /* An attribute declaration (but not a fallthrough attribute
4928            declaration, which was handled separately); warn if there
4929            are any attributes being ignored (but not if the attributes
4930            were empty).  */
4931         c_warn_unused_attributes (declspecs->attrs);
4932       else if (!found_tag)
4933         pedwarn (input_location, 0, "empty declaration");
4934     }
4935 }
4936 \f
4937
4938 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4939    bits.  SPECS represents declaration specifiers that the grammar
4940    only permits to contain type qualifiers and attributes.  */
4941
4942 int
4943 quals_from_declspecs (const struct c_declspecs *specs)
4944 {
4945   int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4946                | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4947                | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4948                | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4949                | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4950   gcc_assert (!specs->type
4951               && !specs->decl_attr
4952               && specs->typespec_word == cts_none
4953               && specs->storage_class == csc_none
4954               && !specs->typedef_p
4955               && !specs->explicit_signed_p
4956               && !specs->deprecated_p
4957               && !specs->unavailable_p
4958               && !specs->long_p
4959               && !specs->long_long_p
4960               && !specs->short_p
4961               && !specs->signed_p
4962               && !specs->unsigned_p
4963               && !specs->complex_p
4964               && !specs->inline_p
4965               && !specs->noreturn_p
4966               && !specs->thread_p);
4967   return quals;
4968 }
4969
4970 /* Construct an array declarator.  LOC is the location of the
4971    beginning of the array (usually the opening brace).  EXPR is the
4972    expression inside [], or NULL_TREE.  QUALS are the type qualifiers
4973    inside the [] (to be applied to the pointer to which a parameter
4974    array is converted).  STATIC_P is true if "static" is inside the
4975    [], false otherwise.  VLA_UNSPEC_P is true if the array is [*], a
4976    VLA of unspecified length which is nevertheless a complete type,
4977    false otherwise.  The field for the contained declarator is left to
4978    be filled in by set_array_declarator_inner.  */
4979
4980 struct c_declarator *
4981 build_array_declarator (location_t loc,
4982                         tree expr, struct c_declspecs *quals, bool static_p,
4983                         bool vla_unspec_p)
4984 {
4985   struct c_declarator *declarator = XOBNEW (&parser_obstack,
4986                                             struct c_declarator);
4987   declarator->id_loc = loc;
4988   declarator->kind = cdk_array;
4989   declarator->declarator = 0;
4990   declarator->u.array.dimen = expr;
4991   if (quals)
4992     {
4993       declarator->u.array.attrs = quals->attrs;
4994       declarator->u.array.quals = quals_from_declspecs (quals);
4995     }
4996   else
4997     {
4998       declarator->u.array.attrs = NULL_TREE;
4999       declarator->u.array.quals = 0;
5000     }
5001   declarator->u.array.static_p = static_p;
5002   declarator->u.array.vla_unspec_p = vla_unspec_p;
5003   if (static_p || quals != NULL)
5004     pedwarn_c90 (loc, OPT_Wpedantic,
5005                  "ISO C90 does not support %<static%> or type "
5006                  "qualifiers in parameter array declarators");
5007   if (vla_unspec_p)
5008     pedwarn_c90 (loc, OPT_Wpedantic,
5009                  "ISO C90 does not support %<[*]%> array declarators");
5010   if (vla_unspec_p)
5011     {
5012       if (!current_scope->parm_flag)
5013         {
5014           /* C99 6.7.5.2p4 */
5015           error_at (loc, "%<[*]%> not allowed in other than "
5016                     "function prototype scope");
5017           declarator->u.array.vla_unspec_p = false;
5018           return NULL;
5019         }
5020       current_scope->had_vla_unspec = true;
5021     }
5022   return declarator;
5023 }
5024
5025 /* Set the contained declarator of an array declarator.  DECL is the
5026    declarator, as constructed by build_array_declarator; INNER is what
5027    appears on the left of the [].  */
5028
5029 struct c_declarator *
5030 set_array_declarator_inner (struct c_declarator *decl,
5031                             struct c_declarator *inner)
5032 {
5033   decl->declarator = inner;
5034   return decl;
5035 }
5036
5037 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]".  */
5038 static bool
5039 flexible_array_member_type_p (const_tree type)
5040 {
5041   if (TREE_CODE (type) == ARRAY_TYPE
5042       && TYPE_SIZE (type) == NULL_TREE
5043       && TYPE_DOMAIN (type) != NULL_TREE
5044       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5045     return true;
5046
5047   return false;
5048 }
5049
5050 /* Determine whether TYPE is a one-element array type "[1]".  */
5051 static bool
5052 one_element_array_type_p (const_tree type)
5053 {
5054   if (TREE_CODE (type) != ARRAY_TYPE)
5055     return false;
5056   return integer_zerop (array_type_nelts (type));
5057 }
5058
5059 /* Determine whether TYPE is a zero-length array type "[0]".  */
5060 static bool
5061 zero_length_array_type_p (const_tree type)
5062 {
5063   if (TREE_CODE (type) == ARRAY_TYPE)
5064     if (tree type_size = TYPE_SIZE_UNIT (type))
5065       if ((integer_zerop (type_size))
5066            && TYPE_DOMAIN (type) != NULL_TREE
5067            && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5068         return true;
5069   return false;
5070 }
5071
5072 /* INIT is a constructor that forms DECL's initializer.  If the final
5073    element initializes a flexible array field, add the size of that
5074    initializer to DECL's size.  */
5075
5076 static void
5077 add_flexible_array_elts_to_size (tree decl, tree init)
5078 {
5079   tree elt, type;
5080
5081   if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5082     return;
5083
5084   elt = CONSTRUCTOR_ELTS (init)->last ().value;
5085   type = TREE_TYPE (elt);
5086   if (flexible_array_member_type_p (type))
5087     {
5088       complete_array_type (&type, elt, false);
5089       DECL_SIZE (decl)
5090         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5091       DECL_SIZE_UNIT (decl)
5092         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5093     }
5094 }
5095 \f
5096 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5097    Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5098    before the type name, and set *EXPR_CONST_OPERANDS, if
5099    EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5100    appear in a constant expression.  */
5101
5102 tree
5103 groktypename (struct c_type_name *type_name, tree *expr,
5104               bool *expr_const_operands)
5105 {
5106   tree type;
5107   tree attrs = type_name->specs->attrs;
5108
5109   type_name->specs->attrs = NULL_TREE;
5110
5111   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5112                          false, NULL, &attrs, expr, expr_const_operands,
5113                          DEPRECATED_NORMAL);
5114
5115   /* Apply attributes.  */
5116   attrs = c_warn_type_attributes (attrs);
5117   decl_attributes (&type, attrs, 0);
5118
5119   return type;
5120 }
5121
5122 /* Looks up the most recent pushed declaration corresponding to DECL.  */
5123
5124 static tree
5125 lookup_last_decl (tree decl)
5126 {
5127   tree last_decl = lookup_name (DECL_NAME (decl));
5128   if (!last_decl)
5129     last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5130   return last_decl;
5131 }
5132
5133 /* Wrapper for decl_attributes that adds some implicit attributes
5134    to VAR_DECLs or FUNCTION_DECLs.  */
5135
5136 static tree
5137 c_decl_attributes (tree *node, tree attributes, int flags)
5138 {
5139   /* Add implicit "omp declare target" attribute if requested.  */
5140   if (vec_safe_length (current_omp_declare_target_attribute)
5141       && ((VAR_P (*node) && is_global_var (*node))
5142           || TREE_CODE (*node) == FUNCTION_DECL))
5143     {
5144       if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5145         attributes = tree_cons (get_identifier ("omp declare target implicit"),
5146                                 NULL_TREE, attributes);
5147       else
5148         {
5149           attributes = tree_cons (get_identifier ("omp declare target"),
5150                                   NULL_TREE, attributes);
5151           attributes = tree_cons (get_identifier ("omp declare target block"),
5152                                   NULL_TREE, attributes);
5153         }
5154       if (TREE_CODE (*node) == FUNCTION_DECL)
5155         {
5156           int device_type
5157             = current_omp_declare_target_attribute->last ().device_type;
5158           device_type = MAX (device_type, 0);
5159           if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5160               && !lookup_attribute ("omp declare target host", attributes))
5161             attributes
5162               = tree_cons (get_identifier ("omp declare target host"),
5163                            NULL_TREE, attributes);
5164           if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5165               && !lookup_attribute ("omp declare target nohost", attributes))
5166             attributes
5167               = tree_cons (get_identifier ("omp declare target nohost"),
5168                            NULL_TREE, attributes);
5169         }
5170     }
5171
5172   /* Look up the current declaration with all the attributes merged
5173      so far so that attributes on the current declaration that's
5174      about to be pushed that conflict with the former can be detected,
5175      diagnosed, and rejected as appropriate.  */
5176   tree last_decl = lookup_last_decl (*node);
5177   return decl_attributes (node, attributes, flags, last_decl);
5178 }
5179
5180
5181 /* Decode a declarator in an ordinary declaration or data definition.
5182    This is called as soon as the type information and variable name
5183    have been parsed, before parsing the initializer if any.
5184    Here we create the ..._DECL node, fill in its type,
5185    and put it on the list of decls for the current context.
5186    When nonnull, set *LASTLOC to the location of the prior declaration
5187    of the same entity if one exists.
5188    The ..._DECL node is returned as the value.
5189
5190    Exception: for arrays where the length is not specified,
5191    the type is left null, to be filled in by `finish_decl'.
5192
5193    Function definitions do not come here; they go to start_function
5194    instead.  However, external and forward declarations of functions
5195    do go through here.  Structure field declarations are done by
5196    grokfield and not through here.  */
5197
5198 tree
5199 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5200             bool initialized, tree attributes, location_t *lastloc /* = NULL */)
5201 {
5202   tree decl;
5203   tree tem;
5204   tree expr = NULL_TREE;
5205   enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5206
5207   /* An object declared as __attribute__((unavailable)) suppresses
5208      warnings and errors from __attribute__((deprecated/unavailable))
5209      components.
5210      An object declared as __attribute__((deprecated)) suppresses
5211      warnings of uses of other deprecated items.  */
5212   if (lookup_attribute ("unavailable", attributes))
5213     deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5214   else if (lookup_attribute ("deprecated", attributes))
5215     deprecated_state = DEPRECATED_SUPPRESS;
5216
5217   decl = grokdeclarator (declarator, declspecs,
5218                          NORMAL, initialized, NULL, &attributes, &expr, NULL,
5219                          deprecated_state);
5220   if (!decl || decl == error_mark_node)
5221     return NULL_TREE;
5222
5223   if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5224     if (lastdecl != error_mark_node)
5225       *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5226
5227   if (expr)
5228     add_stmt (fold_convert (void_type_node, expr));
5229
5230   if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5231       && TREE_PUBLIC (decl))
5232     warning (OPT_Wmain, "%q+D is usually a function", decl);
5233
5234   if (initialized)
5235     /* Is it valid for this decl to have an initializer at all?
5236        If not, set INITIALIZED to zero, which will indirectly
5237        tell 'finish_decl' to ignore the initializer once it is parsed.  */
5238     switch (TREE_CODE (decl))
5239       {
5240       case TYPE_DECL:
5241         error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5242         initialized = false;
5243         break;
5244
5245       case FUNCTION_DECL:
5246         error ("function %qD is initialized like a variable", decl);
5247         initialized = false;
5248         break;
5249
5250       case PARM_DECL:
5251         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
5252         error ("parameter %qD is initialized", decl);
5253         initialized = false;
5254         break;
5255
5256       default:
5257         /* Don't allow initializations for incomplete types except for
5258            arrays which might be completed by the initialization.  */
5259
5260         /* This can happen if the array size is an undefined macro.
5261            We already gave a warning, so we don't need another one.  */
5262         if (TREE_TYPE (decl) == error_mark_node)
5263           initialized = false;
5264         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5265           {
5266             /* A complete type is ok if size is fixed.  If the size is
5267                variable, an empty initializer is OK and nonempty
5268                initializers will be diagnosed in the parser.  */
5269           }
5270         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5271           {
5272             error ("variable %qD has initializer but incomplete type", decl);
5273             initialized = false;
5274           }
5275       }
5276
5277   if (initialized)
5278     {
5279       if (current_scope == file_scope)
5280         TREE_STATIC (decl) = 1;
5281
5282       /* Tell 'pushdecl' this is an initialized decl
5283          even though we don't yet have the initializer expression.
5284          Also tell 'finish_decl' it may store the real initializer.  */
5285       DECL_INITIAL (decl) = error_mark_node;
5286     }
5287
5288   /* If this is a function declaration, write a record describing it to the
5289      prototypes file (if requested).  */
5290
5291   if (TREE_CODE (decl) == FUNCTION_DECL)
5292     gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5293
5294   /* ANSI specifies that a tentative definition which is not merged with
5295      a non-tentative definition behaves exactly like a definition with an
5296      initializer equal to zero.  (Section 3.7.2)
5297
5298      -fno-common gives strict ANSI behavior, though this tends to break
5299      a large body of code that grew up without this rule.
5300
5301      Thread-local variables are never common, since there's no entrenched
5302      body of code to break, and it allows more efficient variable references
5303      in the presence of dynamic linking.  */
5304
5305   if (VAR_P (decl)
5306       && !initialized
5307       && TREE_PUBLIC (decl)
5308       && !DECL_THREAD_LOCAL_P (decl)
5309       && !flag_no_common)
5310     DECL_COMMON (decl) = 1;
5311
5312   /* Set attributes here so if duplicate decl, will have proper attributes.  */
5313   c_decl_attributes (&decl, attributes, 0);
5314
5315   /* Handle gnu_inline attribute.  */
5316   if (declspecs->inline_p
5317       && !flag_gnu89_inline
5318       && TREE_CODE (decl) == FUNCTION_DECL
5319       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5320           || current_function_decl))
5321     {
5322       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5323         ;
5324       else if (declspecs->storage_class != csc_static)
5325         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5326     }
5327
5328   if (TREE_CODE (decl) == FUNCTION_DECL
5329       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5330     {
5331       struct c_declarator *ce = declarator;
5332
5333       if (ce->kind == cdk_pointer)
5334         ce = declarator->declarator;
5335       if (ce->kind == cdk_function)
5336         {
5337           tree args = ce->u.arg_info->parms;
5338           for (; args; args = DECL_CHAIN (args))
5339             {
5340               tree type = TREE_TYPE (args);
5341               if (type && INTEGRAL_TYPE_P (type)
5342                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5343                 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5344             }
5345         }
5346     }
5347
5348   if (TREE_CODE (decl) == FUNCTION_DECL
5349       && DECL_DECLARED_INLINE_P (decl)
5350       && DECL_UNINLINABLE (decl)
5351       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5352     warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5353              decl, "noinline");
5354
5355   /* C99 6.7.4p3: An inline definition of a function with external
5356      linkage shall not contain a definition of a modifiable object
5357      with static storage duration...  */
5358   if (VAR_P (decl)
5359       && current_scope != file_scope
5360       && TREE_STATIC (decl)
5361       && !TREE_READONLY (decl)
5362       && DECL_DECLARED_INLINE_P (current_function_decl)
5363       && DECL_EXTERNAL (current_function_decl))
5364     record_inline_static (input_location, current_function_decl,
5365                           decl, csi_modifiable);
5366
5367   if (c_dialect_objc ()
5368       && VAR_OR_FUNCTION_DECL_P (decl))
5369       objc_check_global_decl (decl);
5370
5371   /* Add this decl to the current scope.
5372      TEM may equal DECL or it may be a previous decl of the same name.  */
5373   tem = pushdecl (decl);
5374
5375   if (initialized && DECL_EXTERNAL (tem))
5376     {
5377       DECL_EXTERNAL (tem) = 0;
5378       TREE_STATIC (tem) = 1;
5379     }
5380
5381   return tem;
5382 }
5383
5384 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5385    DECL or the non-array element type if DECL is an uninitialized array.
5386    If that type has a const member, diagnose this. */
5387
5388 static void
5389 diagnose_uninitialized_cst_member (tree decl, tree type)
5390 {
5391   tree field;
5392   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5393     {
5394       tree field_type;
5395       if (TREE_CODE (field) != FIELD_DECL)
5396         continue;
5397       field_type = strip_array_types (TREE_TYPE (field));
5398
5399       if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5400         {
5401           auto_diagnostic_group d;
5402           if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5403                           "uninitialized const member in %qT is invalid in C++",
5404                           strip_array_types (TREE_TYPE (decl))))
5405             inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5406         }
5407
5408       if (RECORD_OR_UNION_TYPE_P (field_type))
5409         diagnose_uninitialized_cst_member (decl, field_type);
5410     }
5411 }
5412
5413 /* Finish processing of a declaration;
5414    install its initial value.
5415    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5416    If the length of an array type is not known before,
5417    it must be determined now, from the initial value, or it is an error.
5418
5419    INIT_LOC is the location of the initial value.  */
5420
5421 void
5422 finish_decl (tree decl, location_t init_loc, tree init,
5423              tree origtype, tree asmspec_tree)
5424 {
5425   tree type;
5426   bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5427   const char *asmspec = 0;
5428
5429   /* If a name was specified, get the string.  */
5430   if (VAR_OR_FUNCTION_DECL_P (decl)
5431       && DECL_FILE_SCOPE_P (decl))
5432     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5433   if (asmspec_tree)
5434     asmspec = TREE_STRING_POINTER (asmspec_tree);
5435
5436   if (VAR_P (decl)
5437       && TREE_STATIC (decl)
5438       && global_bindings_p ())
5439     /* So decl is a global variable. Record the types it uses
5440        so that we can decide later to emit debug info for them.  */
5441     record_types_used_by_current_var_decl (decl);
5442
5443   /* If `start_decl' didn't like having an initialization, ignore it now.  */
5444   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5445     init = NULL_TREE;
5446
5447   /* Don't crash if parm is initialized.  */
5448   if (TREE_CODE (decl) == PARM_DECL)
5449     init = NULL_TREE;
5450
5451   if (init)
5452     store_init_value (init_loc, decl, init, origtype);
5453
5454   if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5455                             || TREE_CODE (decl) == FIELD_DECL))
5456     objc_check_decl (decl);
5457
5458   type = TREE_TYPE (decl);
5459
5460   /* Deduce size of array from initialization, if not already known.
5461      This is only needed for an initialization in the current scope;
5462      it must not be done for a file-scope initialization of a
5463      declaration with external linkage, redeclared in an inner scope
5464      with the outer declaration shadowed in an intermediate scope.  */
5465   if (TREE_CODE (type) == ARRAY_TYPE
5466       && TYPE_DOMAIN (type) == NULL_TREE
5467       && TREE_CODE (decl) != TYPE_DECL
5468       && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5469     {
5470       bool do_default
5471         = (TREE_STATIC (decl)
5472            /* Even if pedantic, an external linkage array
5473               may have incomplete type at first.  */
5474            ? pedantic && !TREE_PUBLIC (decl)
5475            : !DECL_EXTERNAL (decl));
5476       int failure
5477         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5478                                do_default);
5479
5480       /* Get the completed type made by complete_array_type.  */
5481       type = TREE_TYPE (decl);
5482
5483       switch (failure)
5484         {
5485         case 1:
5486           error ("initializer fails to determine size of %q+D", decl);
5487           break;
5488
5489         case 2:
5490           if (do_default)
5491             error ("array size missing in %q+D", decl);
5492           break;
5493
5494         case 3:
5495           error ("zero or negative size array %q+D", decl);
5496           break;
5497
5498         case 0:
5499           /* For global variables, update the copy of the type that
5500              exists in the binding.  */
5501           if (TREE_PUBLIC (decl))
5502             {
5503               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5504               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5505                 b_ext = b_ext->shadowed;
5506               if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5507                 {
5508                   if (b_ext->u.type && comptypes (b_ext->u.type, type))
5509                     b_ext->u.type = composite_type (b_ext->u.type, type);
5510                   else
5511                     b_ext->u.type = type;
5512                 }
5513             }
5514           break;
5515
5516         default:
5517           gcc_unreachable ();
5518         }
5519
5520       if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5521         TREE_TYPE (DECL_INITIAL (decl)) = type;
5522
5523       relayout_decl (decl);
5524     }
5525
5526   /* Look for braced array initializers for character arrays and
5527      recursively convert them into STRING_CSTs.  */
5528   if (tree init = DECL_INITIAL (decl))
5529     DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5530
5531   if (VAR_P (decl))
5532     {
5533       if (init && TREE_CODE (init) == CONSTRUCTOR)
5534         add_flexible_array_elts_to_size (decl, init);
5535
5536       complete_flexible_array_elts (DECL_INITIAL (decl));
5537
5538       if (is_global_var (decl))
5539         {
5540           type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5541                                        ? TCTX_THREAD_STORAGE
5542                                        : TCTX_STATIC_STORAGE);
5543           if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5544             TREE_TYPE (decl) = error_mark_node;
5545         }
5546
5547       if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5548           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5549         layout_decl (decl, 0);
5550
5551       if (DECL_SIZE (decl) == NULL_TREE
5552           /* Don't give an error if we already gave one earlier.  */
5553           && TREE_TYPE (decl) != error_mark_node
5554           && (TREE_STATIC (decl)
5555               /* A static variable with an incomplete type
5556                  is an error if it is initialized.
5557                  Also if it is not file scope.
5558                  Otherwise, let it through, but if it is not `extern'
5559                  then it may cause an error message later.  */
5560               ? (DECL_INITIAL (decl) != NULL_TREE
5561                  || !DECL_FILE_SCOPE_P (decl))
5562               /* An automatic variable with an incomplete type
5563                  is an error.  */
5564               : !DECL_EXTERNAL (decl)))
5565          {
5566            error ("storage size of %q+D isn%'t known", decl);
5567            TREE_TYPE (decl) = error_mark_node;
5568          }
5569
5570       if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5571           || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5572           && DECL_SIZE (decl) == NULL_TREE
5573           && TREE_STATIC (decl))
5574         incomplete_record_decls.safe_push (decl);
5575
5576       if (is_global_var (decl)
5577           && DECL_SIZE (decl) != NULL_TREE
5578           && TREE_TYPE (decl) != error_mark_node)
5579         {
5580           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5581             constant_expression_warning (DECL_SIZE (decl));
5582           else
5583             {
5584               error ("storage size of %q+D isn%'t constant", decl);
5585               TREE_TYPE (decl) = error_mark_node;
5586             }
5587         }
5588
5589       if (TREE_USED (type))
5590         {
5591           TREE_USED (decl) = 1;
5592           DECL_READ_P (decl) = 1;
5593         }
5594     }
5595
5596   /* If this is a function and an assembler name is specified, reset DECL_RTL
5597      so we can give it its new name.  Also, update builtin_decl if it
5598      was a normal built-in.  */
5599   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5600     {
5601       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5602         set_builtin_user_assembler_name (decl, asmspec);
5603       set_user_assembler_name (decl, asmspec);
5604     }
5605
5606   /* If #pragma weak was used, mark the decl weak now.  */
5607   maybe_apply_pragma_weak (decl);
5608
5609   /* Output the assembler code and/or RTL code for variables and functions,
5610      unless the type is an undefined structure or union.
5611      If not, it will get done when the type is completed.  */
5612
5613   if (VAR_OR_FUNCTION_DECL_P (decl))
5614     {
5615       /* Determine the ELF visibility.  */
5616       if (TREE_PUBLIC (decl))
5617         c_determine_visibility (decl);
5618
5619       /* This is a no-op in c-lang.cc or something real in objc-act.cc.  */
5620       if (c_dialect_objc ())
5621         objc_check_decl (decl);
5622
5623       if (asmspec)
5624         {
5625           /* If this is not a static variable, issue a warning.
5626              It doesn't make any sense to give an ASMSPEC for an
5627              ordinary, non-register local variable.  Historically,
5628              GCC has accepted -- but ignored -- the ASMSPEC in
5629              this case.  */
5630           if (!DECL_FILE_SCOPE_P (decl)
5631               && VAR_P (decl)
5632               && !C_DECL_REGISTER (decl)
5633               && !TREE_STATIC (decl))
5634             warning (0, "ignoring %<asm%> specifier for non-static local "
5635                      "variable %q+D", decl);
5636           else
5637             set_user_assembler_name (decl, asmspec);
5638         }
5639
5640       if (DECL_FILE_SCOPE_P (decl))
5641         {
5642           if (DECL_INITIAL (decl) == NULL_TREE
5643               || DECL_INITIAL (decl) == error_mark_node)
5644             /* Don't output anything
5645                when a tentative file-scope definition is seen.
5646                But at end of compilation, do output code for them.  */
5647             DECL_DEFER_OUTPUT (decl) = 1;
5648           if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5649             DECL_HARD_REGISTER (decl) = 1;
5650           rest_of_decl_compilation (decl, true, 0);
5651
5652           if (TREE_CODE (decl) == FUNCTION_DECL)
5653             {
5654               tree parms = DECL_ARGUMENTS (decl);
5655               const bool builtin = fndecl_built_in_p (decl);
5656               if (tree access = build_attr_access_from_parms (parms, !builtin))
5657                 decl_attributes (&decl, access, 0);
5658             }
5659         }
5660       else
5661         {
5662           /* In conjunction with an ASMSPEC, the `register'
5663              keyword indicates that we should place the variable
5664              in a particular register.  */
5665           if (asmspec && C_DECL_REGISTER (decl))
5666             {
5667               DECL_HARD_REGISTER (decl) = 1;
5668               /* This cannot be done for a structure with volatile
5669                  fields, on which DECL_REGISTER will have been
5670                  reset.  */
5671               if (!DECL_REGISTER (decl))
5672                 error ("cannot put object with volatile field into register");
5673             }
5674
5675           if (TREE_CODE (decl) != FUNCTION_DECL)
5676             {
5677               /* If we're building a variable sized type, and we might be
5678                  reachable other than via the top of the current binding
5679                  level, then create a new BIND_EXPR so that we deallocate
5680                  the object at the right time.  */
5681               /* Note that DECL_SIZE can be null due to errors.  */
5682               if (DECL_SIZE (decl)
5683                   && !TREE_CONSTANT (DECL_SIZE (decl))
5684                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5685                 {
5686                   tree bind;
5687                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5688                   TREE_SIDE_EFFECTS (bind) = 1;
5689                   add_stmt (bind);
5690                   BIND_EXPR_BODY (bind) = push_stmt_list ();
5691                 }
5692               add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5693                                     DECL_EXPR, decl));
5694             }
5695         }
5696
5697
5698       if (!DECL_FILE_SCOPE_P (decl))
5699         {
5700           /* Recompute the RTL of a local array now
5701              if it used to be an incomplete type.  */
5702           if (was_incomplete && !is_global_var (decl))
5703             {
5704               /* If we used it already as memory, it must stay in memory.  */
5705               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5706               /* If it's still incomplete now, no init will save it.  */
5707               if (DECL_SIZE (decl) == NULL_TREE)
5708                 DECL_INITIAL (decl) = NULL_TREE;
5709             }
5710         }
5711     }
5712
5713   if (TREE_CODE (decl) == TYPE_DECL)
5714     {
5715       if (!DECL_FILE_SCOPE_P (decl)
5716           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5717         add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5718
5719       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5720     }
5721
5722   /* Install a cleanup (aka destructor) if one was given.  */
5723   if (VAR_P (decl) && !TREE_STATIC (decl))
5724     {
5725       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5726       if (attr)
5727         {
5728           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5729           tree cleanup_decl = lookup_name (cleanup_id);
5730           tree cleanup;
5731           vec<tree, va_gc> *v;
5732
5733           /* Build "cleanup(&decl)" for the destructor.  */
5734           cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5735           vec_alloc (v, 1);
5736           v->quick_push (cleanup);
5737           cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5738                                                vNULL, cleanup_decl, v, NULL);
5739           vec_free (v);
5740
5741           /* Don't warn about decl unused; the cleanup uses it.  */
5742           TREE_USED (decl) = 1;
5743           TREE_USED (cleanup_decl) = 1;
5744           DECL_READ_P (decl) = 1;
5745
5746           push_cleanup (decl, cleanup, false);
5747         }
5748     }
5749
5750   if (warn_cxx_compat
5751       && VAR_P (decl)
5752       && !DECL_EXTERNAL (decl)
5753       && DECL_INITIAL (decl) == NULL_TREE)
5754     {
5755       type = strip_array_types (type);
5756       if (TREE_READONLY (decl))
5757         warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5758                     "uninitialized %<const %D%> is invalid in C++", decl);
5759       else if (RECORD_OR_UNION_TYPE_P (type)
5760                && C_TYPE_FIELDS_READONLY (type))
5761         diagnose_uninitialized_cst_member (decl, type);
5762     }
5763
5764   if (flag_openmp
5765       && VAR_P (decl)
5766       && lookup_attribute ("omp declare target implicit",
5767                            DECL_ATTRIBUTES (decl)))
5768     {
5769       DECL_ATTRIBUTES (decl)
5770         = remove_attribute ("omp declare target implicit",
5771                             DECL_ATTRIBUTES (decl));
5772       if (!omp_mappable_type (TREE_TYPE (decl)))
5773         error ("%q+D in declare target directive does not have mappable type",
5774                decl);
5775       else if (!lookup_attribute ("omp declare target",
5776                                   DECL_ATTRIBUTES (decl))
5777                && !lookup_attribute ("omp declare target link",
5778                                      DECL_ATTRIBUTES (decl)))
5779         {
5780           DECL_ATTRIBUTES (decl)
5781             = tree_cons (get_identifier ("omp declare target"),
5782                          NULL_TREE, DECL_ATTRIBUTES (decl));
5783             symtab_node *node = symtab_node::get (decl);
5784             if (node != NULL)
5785               {
5786                 node->offloadable = 1;
5787                 if (ENABLE_OFFLOADING)
5788                   {
5789                     g->have_offload = true;
5790                     if (is_a <varpool_node *> (node))
5791                       vec_safe_push (offload_vars, decl);
5792                   }
5793               }
5794         }
5795     }
5796
5797   /* This is the last point we can lower alignment so give the target the
5798      chance to do so.  */
5799   if (VAR_P (decl)
5800       && !is_global_var (decl)
5801       && !DECL_HARD_REGISTER (decl))
5802     targetm.lower_local_decl_alignment (decl);
5803
5804   invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5805 }
5806
5807 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5808    EXPR is NULL or a pointer to an expression that needs to be
5809    evaluated for the side effects of array size expressions in the
5810    parameters.  */
5811
5812 tree
5813 grokparm (const struct c_parm *parm, tree *expr)
5814 {
5815   tree attrs = parm->attrs;
5816   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5817                               NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5818
5819   decl_attributes (&decl, attrs, 0);
5820
5821   return decl;
5822 }
5823
5824 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5825    described by PARM, concatenated onto attributes ATTRS.
5826    The spec consists of one dollar symbol for each specified variable
5827    bound, one asterisk for each unspecified variable bound, followed
5828    by at most one specification of the most significant bound of
5829    an ordinary array parameter.  For ordinary arrays the specification
5830    is either the constant bound itself, or the space character for
5831    an array with an unspecified bound (the [] form).  Finally, a chain
5832    of specified variable bounds is appended to the spec, starting with
5833    the most significant bound.  For example, the PARM T a[2][m][3][n]
5834    will produce __attribute__((arg spec ("[$$2]", m, n)).
5835    For T a typedef for an array with variable bounds, the bounds are
5836    included in the specification in the expected order.
5837    No "arg spec"  is created for parameters of pointer types, making
5838    a distinction between T(*)[N] (or, equivalently, T[][N]) and
5839    the T[M][N] form, all of which have the same type and are represented
5840    the same, but only the last of which gets an "arg spec" describing
5841    the most significant bound M.  */
5842
5843 static tree
5844 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5845 {
5846   /* The attribute specification string, minor bound first.  */
5847   std::string spec;
5848
5849   /* A list of VLA variable bounds, major first, or null if unspecified
5850      or not a VLA.  */
5851   tree vbchain = NULL_TREE;
5852   /* True for a pointer parameter.  */
5853   bool pointer = false;
5854   /* True for an ordinary array with an unpecified bound.  */
5855   bool nobound = false;
5856
5857   /* Create a string representation for the bounds of the array/VLA.  */
5858   for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
5859     {
5860       next = pd->declarator;
5861       while (next && next->kind == cdk_attrs)
5862         next = next->declarator;
5863
5864       /* Remember if a pointer has been seen to avoid storing the constant
5865          bound.  */
5866       if (pd->kind == cdk_pointer)
5867         pointer = true;
5868
5869       if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
5870           && (!next || next->kind == cdk_id))
5871         {
5872           /* Do nothing for the common case of a pointer.  The fact that
5873              the parameter is one can be deduced from the absence of
5874              an arg spec for it.  */
5875           return attrs;
5876         }
5877
5878       if (pd->kind == cdk_id)
5879         {
5880           if (pointer
5881               || !parm->specs->type
5882               || TREE_CODE (parm->specs->type) != ARRAY_TYPE
5883               || !TYPE_DOMAIN (parm->specs->type)
5884               || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
5885             continue;
5886
5887           tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
5888           if (!vbchain
5889               && TREE_CODE (max) == INTEGER_CST)
5890             {
5891               /* Extract the upper bound from a parameter of an array type
5892                  unless the parameter is an ordinary array of unspecified
5893                  bound in which case a next iteration of the loop will
5894                  exit.  */
5895               if (spec.empty () || spec.end ()[-1] != ' ')
5896                 {
5897                   if (!tree_fits_shwi_p (max))
5898                     continue;
5899
5900                   /* The upper bound is the value of the largest valid
5901                      index.  */
5902                   HOST_WIDE_INT n = tree_to_shwi (max) + 1;
5903                   char buf[40];
5904                   sprintf (buf, "%lu", (unsigned long)n);
5905                   spec += buf;
5906                 }
5907               continue;
5908             }
5909
5910           /* For a VLA typedef, create a list of its variable bounds and
5911              append it in the expected order to VBCHAIN.  */
5912           tree tpbnds = NULL_TREE;
5913           for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
5914                type = TREE_TYPE (type))
5915             {
5916               tree nelts = array_type_nelts (type);
5917               if (error_operand_p (nelts))
5918                 return attrs;
5919               if (TREE_CODE (nelts) != INTEGER_CST)
5920                 {
5921                   /* Each variable VLA bound is represented by the dollar
5922                      sign.  */
5923                   spec += "$";
5924                   tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
5925                 }
5926             }
5927           tpbnds = nreverse (tpbnds);
5928           vbchain = chainon (vbchain, tpbnds);
5929           continue;
5930         }
5931
5932       if (pd->kind != cdk_array)
5933         continue;
5934
5935       if (pd->u.array.vla_unspec_p)
5936         {
5937           /* Each unspecified bound is represented by a star.  There
5938              can be any number of these in a declaration (but none in
5939              a definition).  */
5940           spec += '*';
5941           continue;
5942         }
5943
5944       tree nelts = pd->u.array.dimen;
5945       if (!nelts)
5946         {
5947           /* Ordinary array of unspecified size.  There can be at most
5948              one for the most significant bound.  Exit on the next
5949              iteration which determines whether or not PARM is declared
5950              as a pointer or an array.  */
5951           nobound = true;
5952           continue;
5953         }
5954
5955       if (pd->u.array.static_p)
5956         spec += 's';
5957
5958       if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
5959         /* Avoid invalid NELTS.  */
5960         return attrs;
5961
5962       STRIP_NOPS (nelts);
5963       nelts = c_fully_fold (nelts, false, nullptr);
5964       if (TREE_CODE (nelts) == INTEGER_CST)
5965         {
5966           /* Skip all constant bounds except the most significant one.
5967              The interior ones are included in the array type.  */
5968           if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
5969             continue;
5970
5971           if (!tree_fits_uhwi_p (nelts))
5972             /* Bail completely on invalid bounds.  */
5973             return attrs;
5974
5975           char buf[40];
5976           unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
5977           sprintf (buf, "%llu", (unsigned long long)n);
5978           spec += buf;
5979           break;
5980         }
5981
5982       /* Each variable VLA bound is represented by a dollar sign.  */
5983       spec += "$";
5984       vbchain = tree_cons (NULL_TREE, nelts, vbchain);
5985     }
5986
5987   if (spec.empty () && !nobound)
5988     return attrs;
5989
5990   spec.insert (0, "[");
5991   if (nobound)
5992     /* Ordinary array of unspecified bound is represented by a space.
5993        It must be last in the spec.  */
5994     spec += ' ';
5995   spec += ']';
5996
5997   tree acsstr = build_string (spec.length () + 1, spec.c_str ());
5998   tree args = tree_cons (NULL_TREE, acsstr, vbchain);
5999   tree name = get_identifier ("arg spec");
6000   return tree_cons (name, args, attrs);
6001 }
6002
6003 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6004    and push that on the current scope.  EXPR is a pointer to an
6005    expression that needs to be evaluated for the side effects of array
6006    size expressions in the parameters.  */
6007
6008 void
6009 push_parm_decl (const struct c_parm *parm, tree *expr)
6010 {
6011   tree attrs = parm->attrs;
6012   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6013                               &attrs, expr, NULL, DEPRECATED_NORMAL);
6014   if (decl && DECL_P (decl))
6015     DECL_SOURCE_LOCATION (decl) = parm->loc;
6016
6017   attrs = get_parm_array_spec (parm, attrs);
6018   decl_attributes (&decl, attrs, 0);
6019
6020   decl = pushdecl (decl);
6021
6022   finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6023 }
6024
6025 /* Mark all the parameter declarations to date as forward decls.
6026    Also diagnose use of this extension.  */
6027
6028 void
6029 mark_forward_parm_decls (void)
6030 {
6031   struct c_binding *b;
6032
6033   if (pedantic && !current_scope->warned_forward_parm_decls)
6034     {
6035       pedwarn (input_location, OPT_Wpedantic,
6036                "ISO C forbids forward parameter declarations");
6037       current_scope->warned_forward_parm_decls = true;
6038     }
6039
6040   for (b = current_scope->bindings; b; b = b->prev)
6041     if (TREE_CODE (b->decl) == PARM_DECL)
6042       TREE_ASM_WRITTEN (b->decl) = 1;
6043 }
6044 \f
6045 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
6046    literal, which may be an incomplete array type completed by the
6047    initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6048    literal.  NON_CONST is true if the initializers contain something
6049    that cannot occur in a constant expression.  If ALIGNAS_ALIGN is nonzero,
6050    it is the (valid) alignment for this compound literal, as specified
6051    with _Alignas.  SCSPECS are the storage class specifiers (C2x) from the
6052    compound literal.  */
6053
6054 tree
6055 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6056                         unsigned int alignas_align,
6057                         struct c_declspecs *scspecs)
6058 {
6059   /* We do not use start_decl here because we have a type, not a declarator;
6060      and do not use finish_decl because the decl should be stored inside
6061      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
6062   tree decl;
6063   tree complit;
6064   tree stmt;
6065   bool threadp = scspecs ? scspecs->thread_p : false;
6066   enum c_storage_class storage_class = (scspecs
6067                                         ? scspecs->storage_class
6068                                         : csc_none);
6069
6070   if (type == error_mark_node
6071       || init == error_mark_node)
6072     return error_mark_node;
6073
6074   if (current_scope == file_scope && storage_class == csc_register)
6075     {
6076       error_at (loc, "file-scope compound literal specifies %<register%>");
6077       storage_class = csc_none;
6078     }
6079
6080   if (current_scope != file_scope && threadp && storage_class == csc_none)
6081     {
6082       error_at (loc, "compound literal implicitly auto and declared %qs",
6083                 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6084       threadp = false;
6085     }
6086
6087   decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6088   DECL_EXTERNAL (decl) = 0;
6089   TREE_PUBLIC (decl) = 0;
6090   TREE_STATIC (decl) = (current_scope == file_scope
6091                         || storage_class == csc_static);
6092   DECL_CONTEXT (decl) = current_function_decl;
6093   TREE_USED (decl) = 1;
6094   DECL_READ_P (decl) = 1;
6095   DECL_ARTIFICIAL (decl) = 1;
6096   DECL_IGNORED_P (decl) = 1;
6097   C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6098   TREE_TYPE (decl) = type;
6099   if (threadp)
6100     set_decl_tls_model (decl, decl_default_tls_model (decl));
6101   if (storage_class == csc_register)
6102     {
6103       C_DECL_REGISTER (decl) = 1;
6104       DECL_REGISTER (decl) = 1;
6105     }
6106   c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6107   if (alignas_align)
6108     {
6109       SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6110       DECL_USER_ALIGN (decl) = 1;
6111     }
6112   store_init_value (loc, decl, init, NULL_TREE);
6113
6114   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6115     {
6116       int failure = complete_array_type (&TREE_TYPE (decl),
6117                                          DECL_INITIAL (decl), true);
6118       /* If complete_array_type returns 3, it means that the
6119          initial value of the compound literal is empty.  Allow it.  */
6120       gcc_assert (failure == 0 || failure == 3);
6121
6122       type = TREE_TYPE (decl);
6123       TREE_TYPE (DECL_INITIAL (decl)) = type;
6124     }
6125
6126   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6127     {
6128       c_incomplete_type_error (loc, NULL_TREE, type);
6129       return error_mark_node;
6130     }
6131
6132   if (TREE_STATIC (decl)
6133       && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6134     return error_mark_node;
6135
6136   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6137   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6138   TREE_SIDE_EFFECTS (complit) = 1;
6139
6140   layout_decl (decl, 0);
6141
6142   if (TREE_STATIC (decl))
6143     {
6144       /* This decl needs a name for the assembler output.  */
6145       set_compound_literal_name (decl);
6146       DECL_DEFER_OUTPUT (decl) = 1;
6147       DECL_COMDAT (decl) = 1;
6148       pushdecl (decl);
6149       rest_of_decl_compilation (decl, 1, 0);
6150     }
6151   else if (current_function_decl && !current_scope->parm_flag)
6152     pushdecl (decl);
6153
6154   if (non_const)
6155     {
6156       complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6157       C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6158     }
6159
6160   return complit;
6161 }
6162
6163 /* Check the type of a compound literal.  Here we just check that it
6164    is valid for C++.  */
6165
6166 void
6167 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6168 {
6169   if (warn_cxx_compat
6170       && (type_name->specs->typespec_kind == ctsk_tagdef
6171           || type_name->specs->typespec_kind == ctsk_tagfirstref
6172           || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6173     warning_at (loc, OPT_Wc___compat,
6174                 "defining a type in a compound literal is invalid in C++");
6175 }
6176 \f
6177 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6178    replacing with appropriate values if they are invalid.  */
6179
6180 static void
6181 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6182                                tree orig_name)
6183 {
6184   tree type_mv;
6185   unsigned int max_width;
6186   unsigned HOST_WIDE_INT w;
6187   const char *name = (orig_name
6188                       ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6189                       : _("<anonymous>"));
6190
6191   /* Detect and ignore out of range field width and process valid
6192      field widths.  */
6193   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6194     {
6195       error_at (loc, "bit-field %qs width not an integer constant", name);
6196       *width = integer_one_node;
6197     }
6198   else
6199     {
6200       if (TREE_CODE (*width) != INTEGER_CST)
6201         {
6202           *width = c_fully_fold (*width, false, NULL);
6203           if (TREE_CODE (*width) == INTEGER_CST)
6204             pedwarn (loc, OPT_Wpedantic,
6205                      "bit-field %qs width not an integer constant expression",
6206                      name);
6207         }
6208       if (TREE_CODE (*width) != INTEGER_CST)
6209         {
6210           error_at (loc, "bit-field %qs width not an integer constant", name);
6211           *width = integer_one_node;
6212         }
6213       constant_expression_warning (*width);
6214       if (tree_int_cst_sgn (*width) < 0)
6215         {
6216           error_at (loc, "negative width in bit-field %qs", name);
6217           *width = integer_one_node;
6218         }
6219       else if (integer_zerop (*width) && orig_name)
6220         {
6221           error_at (loc, "zero width for bit-field %qs", name);
6222           *width = integer_one_node;
6223         }
6224     }
6225
6226   /* Detect invalid bit-field type.  */
6227   if (TREE_CODE (*type) != INTEGER_TYPE
6228       && TREE_CODE (*type) != BOOLEAN_TYPE
6229       && TREE_CODE (*type) != ENUMERAL_TYPE)
6230     {
6231       error_at (loc, "bit-field %qs has invalid type", name);
6232       *type = unsigned_type_node;
6233     }
6234
6235   if (TYPE_WARN_IF_NOT_ALIGN (*type))
6236     {
6237       error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6238                 name);
6239       *type = unsigned_type_node;
6240     }
6241
6242   type_mv = TYPE_MAIN_VARIANT (*type);
6243   if (!in_system_header_at (input_location)
6244       && type_mv != integer_type_node
6245       && type_mv != unsigned_type_node
6246       && type_mv != boolean_type_node)
6247     pedwarn_c90 (loc, OPT_Wpedantic,
6248                  "type of bit-field %qs is a GCC extension", name);
6249
6250   max_width = TYPE_PRECISION (*type);
6251
6252   if (compare_tree_int (*width, max_width) > 0)
6253     {
6254       error_at (loc, "width of %qs exceeds its type", name);
6255       w = max_width;
6256       *width = build_int_cst (integer_type_node, w);
6257     }
6258   else
6259     w = tree_to_uhwi (*width);
6260
6261   if (TREE_CODE (*type) == ENUMERAL_TYPE)
6262     {
6263       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6264       if (!lt
6265           || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6266           || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6267         warning_at (loc, 0, "%qs is narrower than values of its type", name);
6268     }
6269 }
6270
6271 \f
6272
6273 /* Print warning about variable length array if necessary.  */
6274
6275 static void
6276 warn_variable_length_array (tree name, tree size)
6277 {
6278   if (TREE_CONSTANT (size))
6279     {
6280       if (name)
6281         pedwarn_c90 (input_location, OPT_Wvla,
6282                      "ISO C90 forbids array %qE whose size "
6283                      "cannot be evaluated", name);
6284       else
6285         pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6286                      "whose size cannot be evaluated");
6287     }
6288   else
6289     {
6290       if (name)
6291         pedwarn_c90 (input_location, OPT_Wvla,
6292                      "ISO C90 forbids variable length array %qE", name);
6293       else
6294         pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6295                      "length array");
6296     }
6297 }
6298
6299 /* Print warning about defaulting to int if necessary.  */
6300
6301 static void
6302 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6303 {
6304   diagnostic_info diagnostic;
6305   va_list ap;
6306   rich_location richloc (line_table, location);
6307
6308   va_start (ap, gmsgid);
6309   diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6310                        flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6311   diagnostic.option_index = opt;
6312   diagnostic_report_diagnostic (global_dc, &diagnostic);
6313   va_end (ap);
6314 }
6315
6316 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6317    considering only those c_declspec_words found in LIST, which
6318    must be terminated by cdw_number_of_elements.  */
6319
6320 static location_t
6321 smallest_type_quals_location (const location_t *locations,
6322                               const c_declspec_word *list)
6323 {
6324   location_t loc = UNKNOWN_LOCATION;
6325   while (*list != cdw_number_of_elements)
6326     {
6327       location_t newloc = locations[*list];
6328       if (loc == UNKNOWN_LOCATION
6329           || (newloc != UNKNOWN_LOCATION && newloc < loc))
6330         loc = newloc;
6331       list++;
6332     }
6333
6334   return loc;
6335 }
6336
6337 /* Given declspecs and a declarator,
6338    determine the name and type of the object declared
6339    and construct a ..._DECL node for it.
6340    (In one case we can return a ..._TYPE node instead.
6341     For invalid input we sometimes return NULL_TREE.)
6342
6343    DECLSPECS is a c_declspecs structure for the declaration specifiers.
6344
6345    DECL_CONTEXT says which syntactic context this declaration is in:
6346      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6347      FUNCDEF for a function definition.  Like NORMAL but a few different
6348       error messages in each case.  Return value may be zero meaning
6349       this definition is too screwy to try to parse.
6350      PARM for a parameter declaration (either within a function prototype
6351       or before a function body).  Make a PARM_DECL, or return void_type_node.
6352      TYPENAME if for a typename (in a cast or sizeof).
6353       Don't make a DECL node; just return the ..._TYPE node.
6354      FIELD for a struct or union field; make a FIELD_DECL.
6355    INITIALIZED is true if the decl has an initializer.
6356    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6357    representing the width of the bit-field.
6358    DECL_ATTRS points to the list of attributes that should be added to this
6359      decl.  Any nested attributes that belong on the decl itself will be
6360      added to this list.
6361    If EXPR is not NULL, any expressions that need to be evaluated as
6362      part of evaluating variably modified types will be stored in *EXPR.
6363    If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6364      set to indicate whether operands in *EXPR can be used in constant
6365      expressions.
6366    DEPRECATED_STATE is a deprecated_states value indicating whether
6367    deprecation/unavailability warnings should be suppressed.
6368
6369    In the TYPENAME case, DECLARATOR is really an absolute declarator.
6370    It may also be so in the PARM case, for a prototype where the
6371    argument type is specified but not the name.
6372
6373    This function is where the complicated C meanings of `static'
6374    and `extern' are interpreted.  */
6375
6376 static tree
6377 grokdeclarator (const struct c_declarator *declarator,
6378                 struct c_declspecs *declspecs,
6379                 enum decl_context decl_context, bool initialized, tree *width,
6380                 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6381                 enum deprecated_states deprecated_state)
6382 {
6383   tree type = declspecs->type;
6384   bool threadp = declspecs->thread_p;
6385   enum c_storage_class storage_class = declspecs->storage_class;
6386   int constp;
6387   int restrictp;
6388   int volatilep;
6389   int atomicp;
6390   int type_quals = TYPE_UNQUALIFIED;
6391   tree name = NULL_TREE;
6392   bool funcdef_flag = false;
6393   bool funcdef_syntax = false;
6394   bool size_varies = false;
6395   tree decl_attr = declspecs->decl_attr;
6396   int array_ptr_quals = TYPE_UNQUALIFIED;
6397   tree array_ptr_attrs = NULL_TREE;
6398   bool array_parm_static = false;
6399   bool array_parm_vla_unspec_p = false;
6400   tree returned_attrs = NULL_TREE;
6401   tree decl_id_attrs = NULL_TREE;
6402   bool bitfield = width != NULL;
6403   tree element_type;
6404   tree orig_qual_type = NULL;
6405   size_t orig_qual_indirect = 0;
6406   struct c_arg_info *arg_info = 0;
6407   addr_space_t as1, as2, address_space;
6408   location_t loc = UNKNOWN_LOCATION;
6409   tree expr_dummy;
6410   bool expr_const_operands_dummy;
6411   enum c_declarator_kind first_non_attr_kind;
6412   unsigned int alignas_align = 0;
6413
6414   if (TREE_CODE (type) == ERROR_MARK)
6415     return error_mark_node;
6416   if (expr == NULL)
6417     {
6418       expr = &expr_dummy;
6419       expr_dummy = NULL_TREE;
6420     }
6421   if (expr_const_operands == NULL)
6422     expr_const_operands = &expr_const_operands_dummy;
6423
6424   if (declspecs->expr)
6425     {
6426       if (*expr)
6427         *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6428                         declspecs->expr);
6429       else
6430         *expr = declspecs->expr;
6431     }
6432   *expr_const_operands = declspecs->expr_const_operands;
6433
6434   if (decl_context == FUNCDEF)
6435     funcdef_flag = true, decl_context = NORMAL;
6436
6437   /* Look inside a declarator for the name being declared
6438      and get it as an IDENTIFIER_NODE, for an error message.  */
6439   {
6440     const struct c_declarator *decl = declarator;
6441
6442     first_non_attr_kind = cdk_attrs;
6443     while (decl)
6444       switch (decl->kind)
6445         {
6446         case cdk_array:
6447           loc = decl->id_loc;
6448           /* FALL THRU.  */
6449
6450         case cdk_function:
6451         case cdk_pointer:
6452           funcdef_syntax = (decl->kind == cdk_function);
6453           if (first_non_attr_kind == cdk_attrs)
6454             first_non_attr_kind = decl->kind;
6455           decl = decl->declarator;
6456           break;
6457
6458         case cdk_attrs:
6459           decl = decl->declarator;
6460           break;
6461
6462         case cdk_id:
6463           loc = decl->id_loc;
6464           if (decl->u.id.id)
6465             name = decl->u.id.id;
6466           decl_id_attrs = decl->u.id.attrs;
6467           if (first_non_attr_kind == cdk_attrs)
6468             first_non_attr_kind = decl->kind;
6469           decl = 0;
6470           break;
6471
6472         default:
6473           gcc_unreachable ();
6474         }
6475     if (name == NULL_TREE)
6476       {
6477         gcc_assert (decl_context == PARM
6478                     || decl_context == TYPENAME
6479                     || (decl_context == FIELD
6480                         && declarator->kind == cdk_id));
6481         gcc_assert (!initialized);
6482       }
6483   }
6484
6485   /* A function definition's declarator must have the form of
6486      a function declarator.  */
6487
6488   if (funcdef_flag && !funcdef_syntax)
6489     return NULL_TREE;
6490
6491   /* If this looks like a function definition, make it one,
6492      even if it occurs where parms are expected.
6493      Then store_parm_decls will reject it and not use it as a parm.  */
6494   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6495     decl_context = PARM;
6496
6497   if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6498     {
6499       if (declspecs->unavailable_p)
6500         error_unavailable_use (declspecs->type, declspecs->decl_attr);
6501       else if (declspecs->deprecated_p
6502                 && deprecated_state != DEPRECATED_SUPPRESS)
6503         warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6504     }
6505
6506   if ((decl_context == NORMAL || decl_context == FIELD)
6507       && current_scope == file_scope
6508       && variably_modified_type_p (type, NULL_TREE))
6509     {
6510       if (name)
6511         error_at (loc, "variably modified %qE at file scope", name);
6512       else
6513         error_at (loc, "variably modified field at file scope");
6514       type = integer_type_node;
6515     }
6516
6517   size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6518
6519   /* Diagnose defaulting to "int".  */
6520
6521   if (declspecs->default_int_p && !in_system_header_at (input_location))
6522     {
6523       /* Issue a warning if this is an ISO C 99 program or if
6524          -Wreturn-type and this is a function, or if -Wimplicit;
6525          prefer the former warning since it is more explicit.  */
6526       if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6527           && funcdef_flag)
6528         warn_about_return_type = 1;
6529       else
6530         {
6531           if (name)
6532             warn_defaults_to (loc, OPT_Wimplicit_int,
6533                               "type defaults to %<int%> in declaration "
6534                               "of %qE", name);
6535           else
6536             warn_defaults_to (loc, OPT_Wimplicit_int,
6537                               "type defaults to %<int%> in type name");
6538         }
6539     }
6540
6541   /* Adjust the type if a bit-field is being declared,
6542      -funsigned-bitfields applied and the type is not explicitly
6543      "signed".  */
6544   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6545       && TREE_CODE (type) == INTEGER_TYPE)
6546     type = unsigned_type_for (type);
6547
6548   /* Figure out the type qualifiers for the declaration.  There are
6549      two ways a declaration can become qualified.  One is something
6550      like `const int i' where the `const' is explicit.  Another is
6551      something like `typedef const int CI; CI i' where the type of the
6552      declaration contains the `const'.  A third possibility is that
6553      there is a type qualifier on the element type of a typedefed
6554      array type, in which case we should extract that qualifier so
6555      that c_apply_type_quals_to_decl receives the full list of
6556      qualifiers to work with (C90 is not entirely clear about whether
6557      duplicate qualifiers should be diagnosed in this case, but it
6558      seems most appropriate to do so).  */
6559   element_type = strip_array_types (type);
6560   constp = declspecs->const_p + TYPE_READONLY (element_type);
6561   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6562   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6563   atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6564   as1 = declspecs->address_space;
6565   as2 = TYPE_ADDR_SPACE (element_type);
6566   address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6567
6568   if (constp > 1)
6569     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6570   if (restrictp > 1)
6571     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6572   if (volatilep > 1)
6573     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6574   if (atomicp > 1)
6575     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6576
6577   if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6578     error_at (loc, "conflicting named address spaces (%s vs %s)",
6579               c_addr_space_name (as1), c_addr_space_name (as2));
6580
6581   if ((TREE_CODE (type) == ARRAY_TYPE
6582        || first_non_attr_kind == cdk_array)
6583       && TYPE_QUALS (element_type))
6584     {
6585       orig_qual_type = type;
6586       type = TYPE_MAIN_VARIANT (type);
6587     }
6588   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6589                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6590                 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6591                 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6592                 | ENCODE_QUAL_ADDR_SPACE (address_space));
6593   if (type_quals != TYPE_QUALS (element_type))
6594     orig_qual_type = NULL_TREE;
6595
6596   /* Applying the _Atomic qualifier to an array type (through the use
6597      of typedefs or typeof) must be detected here.  If the qualifier
6598      is introduced later, any appearance of applying it to an array is
6599      actually applying it to an element of that array.  */
6600   if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6601     error_at (loc, "%<_Atomic%>-qualified array type");
6602
6603   /* Warn about storage classes that are invalid for certain
6604      kinds of declarations (parameters, typenames, etc.).  */
6605
6606   if (funcdef_flag
6607       && (threadp
6608           || storage_class == csc_auto
6609           || storage_class == csc_register
6610           || storage_class == csc_typedef))
6611     {
6612       if (storage_class == csc_auto)
6613         pedwarn (loc,
6614                  (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6615                  "function definition declared %<auto%>");
6616       if (storage_class == csc_register)
6617         error_at (loc, "function definition declared %<register%>");
6618       if (storage_class == csc_typedef)
6619         error_at (loc, "function definition declared %<typedef%>");
6620       if (threadp)
6621         error_at (loc, "function definition declared %qs",
6622                   declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6623       threadp = false;
6624       if (storage_class == csc_auto
6625           || storage_class == csc_register
6626           || storage_class == csc_typedef)
6627         storage_class = csc_none;
6628     }
6629   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6630     {
6631       if (decl_context == PARM && storage_class == csc_register)
6632         ;
6633       else
6634         {
6635           switch (decl_context)
6636             {
6637             case FIELD:
6638               if (name)
6639                 error_at (loc, "storage class specified for structure "
6640                           "field %qE", name);
6641               else
6642                 error_at (loc, "storage class specified for structure field");
6643               break;
6644             case PARM:
6645               if (name)
6646                 error_at (loc, "storage class specified for parameter %qE",
6647                           name);
6648               else
6649                 error_at (loc, "storage class specified for unnamed parameter");
6650               break;
6651             default:
6652               error_at (loc, "storage class specified for typename");
6653               break;
6654             }
6655           storage_class = csc_none;
6656           threadp = false;
6657         }
6658     }
6659   else if (storage_class == csc_extern
6660            && initialized
6661            && !funcdef_flag)
6662     {
6663       /* 'extern' with initialization is invalid if not at file scope.  */
6664        if (current_scope == file_scope)
6665          {
6666            /* It is fine to have 'extern const' when compiling at C
6667               and C++ intersection.  */
6668            if (!(warn_cxx_compat && constp))
6669              warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6670                          name);
6671          }
6672       else
6673         error_at (loc, "%qE has both %<extern%> and initializer", name);
6674     }
6675   else if (current_scope == file_scope)
6676     {
6677       if (storage_class == csc_auto)
6678         error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6679                   name);
6680       if (pedantic && storage_class == csc_register)
6681         pedwarn (input_location, OPT_Wpedantic,
6682                  "file-scope declaration of %qE specifies %<register%>", name);
6683     }
6684   else
6685     {
6686       if (storage_class == csc_extern && funcdef_flag)
6687         error_at (loc, "nested function %qE declared %<extern%>", name);
6688       else if (threadp && storage_class == csc_none)
6689         {
6690           error_at (loc, "function-scope %qE implicitly auto and declared "
6691                     "%qs", name,
6692                     declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6693           threadp = false;
6694         }
6695     }
6696
6697   /* Now figure out the structure of the declarator proper.
6698      Descend through it, creating more complex types, until we reach
6699      the declared identifier (or NULL_TREE, in an absolute declarator).
6700      At each stage we maintain an unqualified version of the type
6701      together with any qualifiers that should be applied to it with
6702      c_build_qualified_type; this way, array types including
6703      multidimensional array types are first built up in unqualified
6704      form and then the qualified form is created with
6705      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
6706
6707   while (declarator && declarator->kind != cdk_id)
6708     {
6709       if (type == error_mark_node)
6710         {
6711           declarator = declarator->declarator;
6712           continue;
6713         }
6714
6715       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6716          a cdk_pointer (for *...),
6717          a cdk_function (for ...(...)),
6718          a cdk_attrs (for nested attributes),
6719          or a cdk_id (for the name being declared
6720          or the place in an absolute declarator
6721          where the name was omitted).
6722          For the last case, we have just exited the loop.
6723
6724          At this point, TYPE is the type of elements of an array,
6725          or for a function to return, or for a pointer to point to.
6726          After this sequence of ifs, TYPE is the type of the
6727          array or function or pointer, and DECLARATOR has had its
6728          outermost layer removed.  */
6729
6730       if (array_ptr_quals != TYPE_UNQUALIFIED
6731           || array_ptr_attrs != NULL_TREE
6732           || array_parm_static)
6733         {
6734           /* Only the innermost declarator (making a parameter be of
6735              array type which is converted to pointer type)
6736              may have static or type qualifiers.  */
6737           error_at (loc, "static or type qualifiers in non-parameter array declarator");
6738           array_ptr_quals = TYPE_UNQUALIFIED;
6739           array_ptr_attrs = NULL_TREE;
6740           array_parm_static = false;
6741         }
6742
6743       switch (declarator->kind)
6744         {
6745         case cdk_attrs:
6746           {
6747             /* A declarator with embedded attributes.  */
6748             tree attrs = declarator->u.attrs;
6749             const struct c_declarator *inner_decl;
6750             int attr_flags = 0;
6751             declarator = declarator->declarator;
6752             /* Standard attribute syntax precisely defines what entity
6753                an attribute in each position appertains to, so only
6754                apply laxity about positioning to GNU attribute syntax.
6755                Standard attributes applied to a function or array
6756                declarator apply exactly to that type; standard
6757                attributes applied to the identifier apply to the
6758                declaration rather than to the type, and are specified
6759                using a cdk_id declarator rather than using
6760                cdk_attrs.  */
6761             inner_decl = declarator;
6762             while (inner_decl->kind == cdk_attrs)
6763               inner_decl = inner_decl->declarator;
6764             if (!cxx11_attribute_p (attrs))
6765               {
6766                 if (inner_decl->kind == cdk_id)
6767                   attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6768                 else if (inner_decl->kind == cdk_function)
6769                   attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6770                 else if (inner_decl->kind == cdk_array)
6771                   attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6772               }
6773             attrs = c_warn_type_attributes (attrs);
6774             returned_attrs = decl_attributes (&type,
6775                                               chainon (returned_attrs, attrs),
6776                                               attr_flags);
6777             break;
6778           }
6779         case cdk_array:
6780           {
6781             tree itype = NULL_TREE;
6782             tree size = declarator->u.array.dimen;
6783             /* The index is a signed object `sizetype' bits wide.  */
6784             tree index_type = c_common_signed_type (sizetype);
6785
6786             array_ptr_quals = declarator->u.array.quals;
6787             array_ptr_attrs = declarator->u.array.attrs;
6788             array_parm_static = declarator->u.array.static_p;
6789             array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6790
6791             declarator = declarator->declarator;
6792
6793             /* Check for some types that there cannot be arrays of.  */
6794
6795             if (VOID_TYPE_P (type))
6796               {
6797                 if (name)
6798                   error_at (loc, "declaration of %qE as array of voids", name);
6799                 else
6800                   error_at (loc, "declaration of type name as array of voids");
6801                 type = error_mark_node;
6802               }
6803
6804             if (TREE_CODE (type) == FUNCTION_TYPE)
6805               {
6806                 if (name)
6807                   error_at (loc, "declaration of %qE as array of functions",
6808                             name);
6809                 else
6810                   error_at (loc, "declaration of type name as array of "
6811                             "functions");
6812                 type = error_mark_node;
6813               }
6814
6815             if (pedantic && !in_system_header_at (input_location)
6816                 && flexible_array_type_p (type))
6817               pedwarn (loc, OPT_Wpedantic,
6818                        "invalid use of structure with flexible array member");
6819
6820             if (size == error_mark_node)
6821               type = error_mark_node;
6822
6823             if (type == error_mark_node)
6824               continue;
6825
6826             if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6827               {
6828                 type = error_mark_node;
6829                 continue;
6830               }
6831
6832             /* If size was specified, set ITYPE to a range-type for
6833                that size.  Otherwise, ITYPE remains null.  finish_decl
6834                may figure it out from an initial value.  */
6835
6836             if (size)
6837               {
6838                 bool size_maybe_const = true;
6839                 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6840                                        && !TREE_OVERFLOW (size));
6841                 bool this_size_varies = false;
6842
6843                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6844                    lvalue.  */
6845                 STRIP_TYPE_NOPS (size);
6846
6847                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6848                   {
6849                     if (name)
6850                       error_at (loc, "size of array %qE has non-integer type",
6851                                 name);
6852                     else
6853                       error_at (loc,
6854                                 "size of unnamed array has non-integer type");
6855                     size = integer_one_node;
6856                     size_int_const = true;
6857                   }
6858                 /* This can happen with enum forward declaration.  */
6859                 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6860                   {
6861                     if (name)
6862                       error_at (loc, "size of array %qE has incomplete type",
6863                                 name);
6864                     else
6865                       error_at (loc, "size of unnamed array has incomplete "
6866                                 "type");
6867                     size = integer_one_node;
6868                     size_int_const = true;
6869                   }
6870
6871                 size = c_fully_fold (size, false, &size_maybe_const);
6872
6873                 if (pedantic && size_maybe_const && integer_zerop (size))
6874                   {
6875                     if (name)
6876                       pedwarn (loc, OPT_Wpedantic,
6877                                "ISO C forbids zero-size array %qE", name);
6878                     else
6879                       pedwarn (loc, OPT_Wpedantic,
6880                                "ISO C forbids zero-size array");
6881                   }
6882
6883                 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6884                   {
6885                     constant_expression_warning (size);
6886                     if (tree_int_cst_sgn (size) < 0)
6887                       {
6888                         if (name)
6889                           error_at (loc, "size of array %qE is negative", name);
6890                         else
6891                           error_at (loc, "size of unnamed array is negative");
6892                         size = integer_one_node;
6893                         size_int_const = true;
6894                       }
6895                     /* Handle a size folded to an integer constant but
6896                        not an integer constant expression.  */
6897                     if (!size_int_const)
6898                       {
6899                         /* If this is a file scope declaration of an
6900                            ordinary identifier, this is invalid code;
6901                            diagnosing it here and not subsequently
6902                            treating the type as variable-length avoids
6903                            more confusing diagnostics later.  */
6904                         if ((decl_context == NORMAL || decl_context == FIELD)
6905                             && current_scope == file_scope)
6906                           pedwarn (input_location, 0,
6907                                    "variably modified %qE at file scope",
6908                                    name);
6909                         else
6910                           this_size_varies = size_varies = true;
6911                         warn_variable_length_array (name, size);
6912                       }
6913                   }
6914                 else if ((decl_context == NORMAL || decl_context == FIELD)
6915                          && current_scope == file_scope)
6916                   {
6917                     error_at (loc, "variably modified %qE at file scope", name);
6918                     size = integer_one_node;
6919                   }
6920                 else
6921                   {
6922                     /* Make sure the array size remains visibly
6923                        nonconstant even if it is (eg) a const variable
6924                        with known value.  */
6925                     this_size_varies = size_varies = true;
6926                     warn_variable_length_array (name, size);
6927                     if (sanitize_flags_p (SANITIZE_VLA)
6928                         && current_function_decl != NULL_TREE
6929                         && decl_context == NORMAL)
6930                       {
6931                         /* Evaluate the array size only once.  */
6932                         size = save_expr (size);
6933                         size = c_fully_fold (size, false, NULL);
6934                         size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6935                                             ubsan_instrument_vla (loc, size),
6936                                             size);
6937                       }
6938                   }
6939
6940                 if (integer_zerop (size) && !this_size_varies)
6941                   {
6942                     /* A zero-length array cannot be represented with
6943                        an unsigned index type, which is what we'll
6944                        get with build_index_type.  Create an
6945                        open-ended range instead.  */
6946                     itype = build_range_type (sizetype, size, NULL_TREE);
6947                   }
6948                 else
6949                   {
6950                     /* Arrange for the SAVE_EXPR on the inside of the
6951                        MINUS_EXPR, which allows the -1 to get folded
6952                        with the +1 that happens when building TYPE_SIZE.  */
6953                     if (size_varies)
6954                       size = save_expr (size);
6955                     if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6956                       size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6957                                      integer_zero_node, size);
6958
6959                     /* Compute the maximum valid index, that is, size
6960                        - 1.  Do the calculation in index_type, so that
6961                        if it is a variable the computations will be
6962                        done in the proper mode.  */
6963                     itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6964                                              convert (index_type, size),
6965                                              convert (index_type,
6966                                                       size_one_node));
6967
6968                     /* The above overflows when size does not fit
6969                        in index_type.
6970                        ???  While a size of INT_MAX+1 technically shouldn't
6971                        cause an overflow (because we subtract 1), handling
6972                        this case seems like an unnecessary complication.  */
6973                     if (TREE_CODE (size) == INTEGER_CST
6974                         && !int_fits_type_p (size, index_type))
6975                       {
6976                         if (name)
6977                           error_at (loc, "size of array %qE is too large",
6978                                     name);
6979                         else
6980                           error_at (loc, "size of unnamed array is too large");
6981                         type = error_mark_node;
6982                         continue;
6983                       }
6984
6985                     itype = build_index_type (itype);
6986                   }
6987                 if (this_size_varies)
6988                   {
6989                     if (TREE_SIDE_EFFECTS (size))
6990                       {
6991                         if (*expr)
6992                           *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6993                                           *expr, size);
6994                         else
6995                           *expr = size;
6996                       }
6997                     *expr_const_operands &= size_maybe_const;
6998                   }
6999               }
7000             else if (decl_context == FIELD)
7001               {
7002                 bool flexible_array_member = false;
7003                 if (array_parm_vla_unspec_p)
7004                   /* Field names can in fact have function prototype
7005                      scope so [*] is disallowed here through making
7006                      the field variably modified, not through being
7007                      something other than a declaration with function
7008                      prototype scope.  */
7009                   size_varies = true;
7010                 else
7011                   {
7012                     const struct c_declarator *t = declarator;
7013                     while (t->kind == cdk_attrs)
7014                       t = t->declarator;
7015                     flexible_array_member = (t->kind == cdk_id);
7016                   }
7017                 if (flexible_array_member
7018                     && !in_system_header_at (input_location))
7019                   pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7020                                "support flexible array members");
7021
7022                 /* ISO C99 Flexible array members are effectively
7023                    identical to GCC's zero-length array extension.  */
7024                 if (flexible_array_member || array_parm_vla_unspec_p)
7025                   itype = build_range_type (sizetype, size_zero_node,
7026                                             NULL_TREE);
7027               }
7028             else if (decl_context == PARM)
7029               {
7030                 if (array_parm_vla_unspec_p)
7031                   {
7032                     itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7033                     size_varies = true;
7034                   }
7035               }
7036             else if (decl_context == TYPENAME)
7037               {
7038                 if (array_parm_vla_unspec_p)
7039                   {
7040                     /* C99 6.7.5.2p4 */
7041                     warning (0, "%<[*]%> not in a declaration");
7042                     /* We use this to avoid messing up with incomplete
7043                        array types of the same type, that would
7044                        otherwise be modified below.  */
7045                     itype = build_range_type (sizetype, size_zero_node,
7046                                               NULL_TREE);
7047                     size_varies = true;
7048                   }
7049               }
7050
7051             /* Complain about arrays of incomplete types.  */
7052             if (!COMPLETE_TYPE_P (type))
7053               {
7054                 auto_diagnostic_group d;
7055                 error_at (loc, "array type has incomplete element type %qT",
7056                           type);
7057                 /* See if we can be more helpful.  */
7058                 if (TREE_CODE (type) == ARRAY_TYPE)
7059                   {
7060                     if (name)
7061                       inform (loc, "declaration of %qE as multidimensional "
7062                               "array must have bounds for all dimensions "
7063                               "except the first", name);
7064                     else
7065                       inform (loc, "declaration of multidimensional array "
7066                               "must have bounds for all dimensions except "
7067                               "the first");
7068                   }
7069                 type = error_mark_node;
7070               }
7071             else
7072             /* When itype is NULL, a shared incomplete array type is
7073                returned for all array of a given type.  Elsewhere we
7074                make sure we don't complete that type before copying
7075                it, but here we want to make sure we don't ever
7076                modify the shared type, so we gcc_assert (itype)
7077                below.  */
7078               {
7079                 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7080                 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7081                   type = build_qualified_type (type,
7082                                                ENCODE_QUAL_ADDR_SPACE (as));
7083
7084                 type = build_array_type (type, itype);
7085               }
7086
7087             if (type != error_mark_node)
7088               {
7089                 if (size_varies)
7090                   {
7091                     /* It is ok to modify type here even if itype is
7092                        NULL: if size_varies, we're in a
7093                        multi-dimensional array and the inner type has
7094                        variable size, so the enclosing shared array type
7095                        must too.  */
7096                     if (size && TREE_CODE (size) == INTEGER_CST)
7097                       type
7098                         = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7099                     C_TYPE_VARIABLE_SIZE (type) = 1;
7100                   }
7101
7102                 /* The GCC extension for zero-length arrays differs from
7103                    ISO flexible array members in that sizeof yields
7104                    zero.  */
7105                 if (size && integer_zerop (size))
7106                   {
7107                     gcc_assert (itype);
7108                     type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7109                     TYPE_SIZE (type) = bitsize_zero_node;
7110                     TYPE_SIZE_UNIT (type) = size_zero_node;
7111                     SET_TYPE_STRUCTURAL_EQUALITY (type);
7112                   }
7113                 if (array_parm_vla_unspec_p)
7114                   {
7115                     gcc_assert (itype);
7116                     /* The type is complete.  C99 6.7.5.2p4  */
7117                     type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7118                     TYPE_SIZE (type) = bitsize_zero_node;
7119                     TYPE_SIZE_UNIT (type) = size_zero_node;
7120                     SET_TYPE_STRUCTURAL_EQUALITY (type);
7121                   }
7122
7123                 if (!valid_array_size_p (loc, type, name))
7124                   type = error_mark_node;
7125               }
7126
7127             if (decl_context != PARM
7128                 && (array_ptr_quals != TYPE_UNQUALIFIED
7129                     || array_ptr_attrs != NULL_TREE
7130                     || array_parm_static))
7131               {
7132                 error_at (loc, "static or type qualifiers in non-parameter "
7133                           "array declarator");
7134                 array_ptr_quals = TYPE_UNQUALIFIED;
7135                 array_ptr_attrs = NULL_TREE;
7136                 array_parm_static = false;
7137               }
7138             orig_qual_indirect++;
7139             break;
7140           }
7141         case cdk_function:
7142           {
7143             /* Say it's a definition only for the declarator closest
7144                to the identifier, apart possibly from some
7145                attributes.  */
7146             bool really_funcdef = false;
7147             tree arg_types;
7148             orig_qual_type = NULL_TREE;
7149             if (funcdef_flag)
7150               {
7151                 const struct c_declarator *t = declarator->declarator;
7152                 while (t->kind == cdk_attrs)
7153                   t = t->declarator;
7154                 really_funcdef = (t->kind == cdk_id);
7155               }
7156
7157             /* Declaring a function type.  Make sure we have a valid
7158                type for the function to return.  */
7159             if (type == error_mark_node)
7160               continue;
7161
7162             size_varies = false;
7163
7164             /* Warn about some types functions can't return.  */
7165             if (TREE_CODE (type) == FUNCTION_TYPE)
7166               {
7167                 if (name)
7168                   error_at (loc, "%qE declared as function returning a "
7169                                  "function", name);
7170                 else
7171                   error_at (loc, "type name declared as function "
7172                             "returning a function");
7173                 type = integer_type_node;
7174               }
7175             if (TREE_CODE (type) == ARRAY_TYPE)
7176               {
7177                 if (name)
7178                   error_at (loc, "%qE declared as function returning an array",
7179                             name);
7180                 else
7181                   error_at (loc, "type name declared as function returning "
7182                             "an array");
7183                 type = integer_type_node;
7184               }
7185
7186             /* Construct the function type and go to the next
7187                inner layer of declarator.  */
7188             arg_info = declarator->u.arg_info;
7189             arg_types = grokparms (arg_info, really_funcdef);
7190
7191             /* Type qualifiers before the return type of the function
7192                qualify the return type, not the function type.  */
7193             if (type_quals)
7194               {
7195                 const enum c_declspec_word ignored_quals_list[] =
7196                   {
7197                     cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7198                     cdw_atomic, cdw_number_of_elements
7199                   };
7200                 location_t specs_loc
7201                   = smallest_type_quals_location (declspecs->locations,
7202                                                   ignored_quals_list);
7203                 if (specs_loc == UNKNOWN_LOCATION)
7204                   specs_loc = declspecs->locations[cdw_typedef];
7205                 if (specs_loc == UNKNOWN_LOCATION)
7206                   specs_loc = loc;
7207
7208                 /* Type qualifiers on a function return type are
7209                    normally permitted by the standard but have no
7210                    effect, so give a warning at -Wreturn-type.
7211                    Qualifiers on a void return type are banned on
7212                    function definitions in ISO C; GCC used to used
7213                    them for noreturn functions.  The resolution of C11
7214                    DR#423 means qualifiers (other than _Atomic) are
7215                    actually removed from the return type when
7216                    determining the function type.  */
7217                 int quals_used = type_quals;
7218                 if (flag_isoc11)
7219                   quals_used &= TYPE_QUAL_ATOMIC;
7220                 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7221                   pedwarn (specs_loc, 0,
7222                            "function definition has qualified void "
7223                            "return type");
7224                 else
7225                   warning_at (specs_loc, OPT_Wignored_qualifiers,
7226                               "type qualifiers ignored on function "
7227                               "return type");
7228
7229                 /* Ensure an error for restrict on invalid types; the
7230                    DR#423 resolution is not entirely clear about
7231                    this.  */
7232                 if (flag_isoc11
7233                     && (type_quals & TYPE_QUAL_RESTRICT)
7234                     && (!POINTER_TYPE_P (type)
7235                         || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7236                   error_at (loc, "invalid use of %<restrict%>");
7237                 type = c_build_qualified_type (type, quals_used);
7238               }
7239             type_quals = TYPE_UNQUALIFIED;
7240
7241             type = build_function_type (type, arg_types);
7242             declarator = declarator->declarator;
7243
7244             /* Set the TYPE_CONTEXTs for each tagged type which is local to
7245                the formal parameter list of this FUNCTION_TYPE to point to
7246                the FUNCTION_TYPE node itself.  */
7247             {
7248               c_arg_tag *tag;
7249               unsigned ix;
7250
7251               FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7252                 TYPE_CONTEXT (tag->type) = type;
7253             }
7254             break;
7255           }
7256         case cdk_pointer:
7257           {
7258             /* Merge any constancy or volatility into the target type
7259                for the pointer.  */
7260             if ((type_quals & TYPE_QUAL_ATOMIC)
7261                 && TREE_CODE (type) == FUNCTION_TYPE)
7262               {
7263                 error_at (loc,
7264                           "%<_Atomic%>-qualified function type");
7265                 type_quals &= ~TYPE_QUAL_ATOMIC;
7266               }
7267             else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7268                      && type_quals)
7269               pedwarn (loc, OPT_Wpedantic,
7270                        "ISO C forbids qualified function types");
7271             if (type_quals)
7272               type = c_build_qualified_type (type, type_quals, orig_qual_type,
7273                                              orig_qual_indirect);
7274             orig_qual_type = NULL_TREE;
7275             size_varies = false;
7276
7277             /* When the pointed-to type involves components of variable size,
7278                care must be taken to ensure that the size evaluation code is
7279                emitted early enough to dominate all the possible later uses
7280                and late enough for the variables on which it depends to have
7281                been assigned.
7282
7283                This is expected to happen automatically when the pointed-to
7284                type has a name/declaration of it's own, but special attention
7285                is required if the type is anonymous.
7286
7287                We attach an artificial TYPE_DECL to such pointed-to type
7288                and arrange for it to be included in a DECL_EXPR.  This
7289                forces the sizes evaluation at a safe point and ensures it
7290                is not deferred until e.g. within a deeper conditional context.
7291
7292                PARM contexts have no enclosing statement list that
7293                can hold the DECL_EXPR, so we need to use a BIND_EXPR
7294                instead, and add it to the list of expressions that
7295                need to be evaluated.
7296
7297                TYPENAME contexts do have an enclosing statement list,
7298                but it would be incorrect to use it, as the size should
7299                only be evaluated if the containing expression is
7300                evaluated.  We might also be in the middle of an
7301                expression with side effects on the pointed-to type size
7302                "arguments" prior to the pointer declaration point and
7303                the fake TYPE_DECL in the enclosing context would force
7304                the size evaluation prior to the side effects.  We therefore
7305                use BIND_EXPRs in TYPENAME contexts too.  */
7306             if (!TYPE_NAME (type)
7307                 && variably_modified_type_p (type, NULL_TREE))
7308               {
7309                 tree bind = NULL_TREE;
7310                 if (decl_context == TYPENAME || decl_context == PARM)
7311                   {
7312                     bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7313                                    NULL_TREE, NULL_TREE);
7314                     TREE_SIDE_EFFECTS (bind) = 1;
7315                     BIND_EXPR_BODY (bind) = push_stmt_list ();
7316                     push_scope ();
7317                   }
7318                 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7319                 DECL_ARTIFICIAL (decl) = 1;
7320                 pushdecl (decl);
7321                 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7322                 TYPE_NAME (type) = decl;
7323                 if (bind)
7324                   {
7325                     pop_scope ();
7326                     BIND_EXPR_BODY (bind)
7327                       = pop_stmt_list (BIND_EXPR_BODY (bind));
7328                     if (*expr)
7329                       *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7330                                       bind);
7331                     else
7332                       *expr = bind;
7333                   }
7334               }
7335
7336             type = c_build_pointer_type (type);
7337
7338             /* Process type qualifiers (such as const or volatile)
7339                that were given inside the `*'.  */
7340             type_quals = declarator->u.pointer_quals;
7341
7342             declarator = declarator->declarator;
7343             break;
7344           }
7345         default:
7346           gcc_unreachable ();
7347         }
7348     }
7349   *decl_attrs = chainon (returned_attrs, *decl_attrs);
7350   *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7351
7352   /* Now TYPE has the actual type, apart from any qualifiers in
7353      TYPE_QUALS.  */
7354
7355   /* Warn about address space used for things other than static memory or
7356      pointers.  */
7357   address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7358   if (!ADDR_SPACE_GENERIC_P (address_space))
7359     {
7360       if (decl_context == NORMAL)
7361         {
7362           switch (storage_class)
7363             {
7364             case csc_auto:
7365               error ("%qs combined with %<auto%> qualifier for %qE",
7366                      c_addr_space_name (address_space), name);
7367               break;
7368             case csc_register:
7369               error ("%qs combined with %<register%> qualifier for %qE",
7370                      c_addr_space_name (address_space), name);
7371               break;
7372             case csc_none:
7373               if (current_function_scope)
7374                 {
7375                   error ("%qs specified for auto variable %qE",
7376                          c_addr_space_name (address_space), name);
7377                   break;
7378                 }
7379               break;
7380             case csc_static:
7381             case csc_extern:
7382             case csc_typedef:
7383               break;
7384             default:
7385               gcc_unreachable ();
7386             }
7387         }
7388       else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7389         {
7390           if (name)
7391             error ("%qs specified for parameter %qE",
7392                    c_addr_space_name (address_space), name);
7393           else
7394             error ("%qs specified for unnamed parameter",
7395                    c_addr_space_name (address_space));
7396         }
7397       else if (decl_context == FIELD)
7398         {
7399           if (name)
7400             error ("%qs specified for structure field %qE",
7401                    c_addr_space_name (address_space), name);
7402           else
7403             error ("%qs specified for structure field",
7404                    c_addr_space_name (address_space));
7405         }
7406     }
7407
7408   /* Check the type and width of a bit-field.  */
7409   if (bitfield)
7410     {
7411       check_bitfield_type_and_width (loc, &type, width, name);
7412       /* C11 makes it implementation-defined (6.7.2.1#5) whether
7413          atomic types are permitted for bit-fields; we have no code to
7414          make bit-field accesses atomic, so disallow them.  */
7415       if (type_quals & TYPE_QUAL_ATOMIC)
7416         {
7417           if (name)
7418             error_at (loc, "bit-field %qE has atomic type", name);
7419           else
7420             error_at (loc, "bit-field has atomic type");
7421           type_quals &= ~TYPE_QUAL_ATOMIC;
7422         }
7423     }
7424
7425   /* Reject invalid uses of _Alignas.  */
7426   if (declspecs->alignas_p)
7427     {
7428       if (storage_class == csc_typedef)
7429         error_at (loc, "alignment specified for typedef %qE", name);
7430       else if (storage_class == csc_register)
7431         error_at (loc, "alignment specified for %<register%> object %qE",
7432                   name);
7433       else if (decl_context == PARM)
7434         {
7435           if (name)
7436             error_at (loc, "alignment specified for parameter %qE", name);
7437           else
7438             error_at (loc, "alignment specified for unnamed parameter");
7439         }
7440       else if (bitfield)
7441         {
7442           if (name)
7443             error_at (loc, "alignment specified for bit-field %qE", name);
7444           else
7445             error_at (loc, "alignment specified for unnamed bit-field");
7446         }
7447       else if (TREE_CODE (type) == FUNCTION_TYPE)
7448         error_at (loc, "alignment specified for function %qE", name);
7449       else if (declspecs->align_log != -1 && TYPE_P (type))
7450         {
7451           alignas_align = 1U << declspecs->align_log;
7452           if (alignas_align < min_align_of_type (type))
7453             {
7454               if (name)
7455                 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7456                           "alignment of %qE", name);
7457               else
7458                 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7459                           "alignment of unnamed field");
7460               alignas_align = 0;
7461             }
7462         }
7463     }
7464
7465   /* If this is declaring a typedef name, return a TYPE_DECL.  */
7466
7467   if (storage_class == csc_typedef)
7468     {
7469       tree decl;
7470       if ((type_quals & TYPE_QUAL_ATOMIC)
7471           && TREE_CODE (type) == FUNCTION_TYPE)
7472         {
7473           error_at (loc,
7474                     "%<_Atomic%>-qualified function type");
7475           type_quals &= ~TYPE_QUAL_ATOMIC;
7476         }
7477       else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7478                && type_quals)
7479         pedwarn (loc, OPT_Wpedantic,
7480                  "ISO C forbids qualified function types");
7481       if (type_quals)
7482         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7483                                        orig_qual_indirect);
7484       decl = build_decl (declarator->id_loc,
7485                          TYPE_DECL, declarator->u.id.id, type);
7486       if (declspecs->explicit_signed_p)
7487         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7488       if (declspecs->inline_p)
7489         pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7490       if (declspecs->noreturn_p)
7491         pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7492
7493       if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7494         {
7495           struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7496
7497           if (b != NULL
7498               && b->decl != NULL_TREE
7499               && (B_IN_CURRENT_SCOPE (b)
7500                   || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7501               && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7502             {
7503               auto_diagnostic_group d;
7504               if (warning_at (declarator->id_loc, OPT_Wc___compat,
7505                               ("using %qD as both a typedef and a tag is "
7506                                "invalid in C++"), decl)
7507                   && b->locus != UNKNOWN_LOCATION)
7508                 inform (b->locus, "originally defined here");
7509             }
7510         }
7511
7512       return decl;
7513     }
7514
7515   /* If this is a type name (such as, in a cast or sizeof),
7516      compute the type and return it now.  */
7517
7518   if (decl_context == TYPENAME)
7519     {
7520       /* Note that the grammar rejects storage classes in typenames
7521          and fields.  */
7522       gcc_assert (storage_class == csc_none && !threadp
7523                   && !declspecs->inline_p && !declspecs->noreturn_p);
7524       if ((type_quals & TYPE_QUAL_ATOMIC)
7525           && TREE_CODE (type) == FUNCTION_TYPE)
7526         {
7527           error_at (loc,
7528                     "%<_Atomic%>-qualified function type");
7529           type_quals &= ~TYPE_QUAL_ATOMIC;
7530         }
7531       else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7532                && type_quals)
7533         pedwarn (loc, OPT_Wpedantic,
7534                  "ISO C forbids const or volatile function types");
7535       if (type_quals)
7536         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7537                                        orig_qual_indirect);
7538       return type;
7539     }
7540
7541   if (pedantic && decl_context == FIELD
7542       && variably_modified_type_p (type, NULL_TREE))
7543     {
7544       /* C99 6.7.2.1p8 */
7545       pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7546                "have a variably modified type");
7547     }
7548
7549   /* Aside from typedefs and type names (handle above),
7550      `void' at top level (not within pointer)
7551      is allowed only in public variables.
7552      We don't complain about parms either, but that is because
7553      a better error message can be made later.  */
7554
7555   if (VOID_TYPE_P (type) && decl_context != PARM
7556       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7557             && (storage_class == csc_extern
7558                 || (current_scope == file_scope
7559                     && !(storage_class == csc_static
7560                          || storage_class == csc_register)))))
7561     {
7562       error_at (loc, "variable or field %qE declared void", name);
7563       type = integer_type_node;
7564     }
7565
7566   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7567      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
7568
7569   {
7570     tree decl;
7571
7572     if (decl_context == PARM)
7573       {
7574         tree promoted_type;
7575         bool array_parameter_p = false;
7576
7577         /* A parameter declared as an array of T is really a pointer to T.
7578            One declared as a function is really a pointer to a function.  */
7579
7580         if (TREE_CODE (type) == ARRAY_TYPE)
7581           {
7582             /* Transfer const-ness of array into that of type pointed to.  */
7583             type = TREE_TYPE (type);
7584             if (orig_qual_type != NULL_TREE)
7585               {
7586                 if (orig_qual_indirect == 0)
7587                   orig_qual_type = TREE_TYPE (orig_qual_type);
7588                 else
7589                   orig_qual_indirect--;
7590               }
7591             if (type_quals)
7592               type = c_build_qualified_type (type, type_quals, orig_qual_type,
7593                                              orig_qual_indirect);
7594             type = c_build_pointer_type (type);
7595             type_quals = array_ptr_quals;
7596             if (type_quals)
7597               type = c_build_qualified_type (type, type_quals);
7598
7599             /* We don't yet implement attributes in this context.  */
7600             if (array_ptr_attrs != NULL_TREE)
7601               warning_at (loc, OPT_Wattributes,
7602                           "attributes in parameter array declarator ignored");
7603
7604             size_varies = false;
7605             array_parameter_p = true;
7606           }
7607         else if (TREE_CODE (type) == FUNCTION_TYPE)
7608           {
7609             if (type_quals & TYPE_QUAL_ATOMIC)
7610               {
7611                 error_at (loc,
7612                           "%<_Atomic%>-qualified function type");
7613                 type_quals &= ~TYPE_QUAL_ATOMIC;
7614               }
7615             else if (type_quals)
7616               pedwarn (loc, OPT_Wpedantic,
7617                        "ISO C forbids qualified function types");
7618             if (type_quals)
7619               type = c_build_qualified_type (type, type_quals);
7620             type = c_build_pointer_type (type);
7621             type_quals = TYPE_UNQUALIFIED;
7622           }
7623         else if (type_quals)
7624           type = c_build_qualified_type (type, type_quals);
7625
7626         decl = build_decl (declarator->id_loc,
7627                            PARM_DECL, declarator->u.id.id, type);
7628         if (size_varies)
7629           C_DECL_VARIABLE_SIZE (decl) = 1;
7630         C_ARRAY_PARAMETER (decl) = array_parameter_p;
7631
7632         /* Compute the type actually passed in the parmlist,
7633            for the case where there is no prototype.
7634            (For example, shorts and chars are passed as ints.)
7635            When there is a prototype, this is overridden later.  */
7636
7637         if (type == error_mark_node)
7638           promoted_type = type;
7639         else
7640           promoted_type = c_type_promotes_to (type);
7641
7642         DECL_ARG_TYPE (decl) = promoted_type;
7643         if (declspecs->inline_p)
7644           pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7645         if (declspecs->noreturn_p)
7646           pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7647       }
7648     else if (decl_context == FIELD)
7649       {
7650         /* Note that the grammar rejects storage classes in typenames
7651            and fields.  */
7652         gcc_assert (storage_class == csc_none && !threadp
7653                     && !declspecs->inline_p && !declspecs->noreturn_p);
7654
7655         /* Structure field.  It may not be a function.  */
7656
7657         if (TREE_CODE (type) == FUNCTION_TYPE)
7658           {
7659             error_at (loc, "field %qE declared as a function", name);
7660             type = build_pointer_type (type);
7661           }
7662         else if (TREE_CODE (type) != ERROR_MARK
7663                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7664           {
7665             if (name)
7666               error_at (loc, "field %qE has incomplete type", name);
7667             else
7668               error_at (loc, "unnamed field has incomplete type");
7669             type = error_mark_node;
7670           }
7671         else if (TREE_CODE (type) == ARRAY_TYPE
7672                  && TYPE_DOMAIN (type) == NULL_TREE)
7673           {
7674             /* We have a flexible array member through a typedef.
7675                Set suitable range.  Whether this is a correct position
7676                for a flexible array member will be determined elsewhere.  */
7677             if (!in_system_header_at (input_location))
7678               pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7679                            "support flexible array members");
7680             type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7681             TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7682                                                    NULL_TREE);
7683             if (orig_qual_indirect == 0)
7684               orig_qual_type = NULL_TREE;
7685           }
7686         if (type != error_mark_node
7687             && !verify_type_context (loc, TCTX_FIELD, type))
7688           type = error_mark_node;
7689
7690         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7691                                        orig_qual_indirect);
7692         decl = build_decl (declarator->id_loc,
7693                            FIELD_DECL, declarator->u.id.id, type);
7694         DECL_NONADDRESSABLE_P (decl) = bitfield;
7695         if (bitfield && !declarator->u.id.id)
7696           DECL_PADDING_P (decl) = 1;
7697
7698         if (size_varies)
7699           C_DECL_VARIABLE_SIZE (decl) = 1;
7700       }
7701     else if (TREE_CODE (type) == FUNCTION_TYPE)
7702       {
7703         if (storage_class == csc_register || threadp)
7704           {
7705             error_at (loc, "invalid storage class for function %qE", name);
7706           }
7707         else if (current_scope != file_scope)
7708           {
7709             /* Function declaration not at file scope.  Storage
7710                classes other than `extern' are not allowed, C99
7711                6.7.1p5, and `extern' makes no difference.  However,
7712                GCC allows 'auto', perhaps with 'inline', to support
7713                nested functions.  */
7714             if (storage_class == csc_auto)
7715                 pedwarn (loc, OPT_Wpedantic,
7716                          "invalid storage class for function %qE", name);
7717             else if (storage_class == csc_static)
7718               {
7719                 error_at (loc, "invalid storage class for function %qE", name);
7720                 if (funcdef_flag)
7721                   storage_class = declspecs->storage_class = csc_none;
7722                 else
7723                   return NULL_TREE;
7724               }
7725           }
7726
7727         decl = build_decl (declarator->id_loc,
7728                            FUNCTION_DECL, declarator->u.id.id, type);
7729         decl = build_decl_attribute_variant (decl, decl_attr);
7730
7731         if (type_quals & TYPE_QUAL_ATOMIC)
7732           {
7733             error_at (loc,
7734                       "%<_Atomic%>-qualified function type");
7735             type_quals &= ~TYPE_QUAL_ATOMIC;
7736           }
7737         else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7738           pedwarn (loc, OPT_Wpedantic,
7739                    "ISO C forbids qualified function types");
7740
7741         /* Every function declaration is an external reference
7742            (DECL_EXTERNAL) except for those which are not at file
7743            scope and are explicitly declared "auto".  This is
7744            forbidden by standard C (C99 6.7.1p5) and is interpreted by
7745            GCC to signify a forward declaration of a nested function.  */
7746         if (storage_class == csc_auto && current_scope != file_scope)
7747           DECL_EXTERNAL (decl) = 0;
7748         /* In C99, a function which is declared 'inline' with 'extern'
7749            is not an external reference (which is confusing).  It
7750            means that the later definition of the function must be output
7751            in this file, C99 6.7.4p6.  In GNU C89, a function declared
7752            'extern inline' is an external reference.  */
7753         else if (declspecs->inline_p && storage_class != csc_static)
7754           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7755                                   == flag_gnu89_inline);
7756         else
7757           DECL_EXTERNAL (decl) = !initialized;
7758
7759         /* Record absence of global scope for `static' or `auto'.  */
7760         TREE_PUBLIC (decl)
7761           = !(storage_class == csc_static || storage_class == csc_auto);
7762
7763         /* For a function definition, record the argument information
7764            block where store_parm_decls will look for it.  */
7765         if (funcdef_flag)
7766           current_function_arg_info = arg_info;
7767
7768         if (declspecs->default_int_p)
7769           C_FUNCTION_IMPLICIT_INT (decl) = 1;
7770
7771         /* Record presence of `inline' and `_Noreturn', if it is
7772            reasonable.  */
7773         if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7774           {
7775             if (declspecs->inline_p)
7776               pedwarn (loc, 0, "cannot inline function %<main%>");
7777             if (declspecs->noreturn_p)
7778               pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7779           }
7780         else
7781           {
7782             if (declspecs->inline_p)
7783               /* Record that the function is declared `inline'.  */
7784               DECL_DECLARED_INLINE_P (decl) = 1;
7785             if (declspecs->noreturn_p)
7786               {
7787                 if (flag_isoc99)
7788                   pedwarn_c99 (loc, OPT_Wpedantic,
7789                                "ISO C99 does not support %<_Noreturn%>");
7790                 else
7791                   pedwarn_c99 (loc, OPT_Wpedantic,
7792                                "ISO C90 does not support %<_Noreturn%>");
7793                 TREE_THIS_VOLATILE (decl) = 1;
7794               }
7795           }
7796       }
7797     else
7798       {
7799         /* It's a variable.  */
7800         /* An uninitialized decl with `extern' is a reference.  */
7801         int extern_ref = !initialized && storage_class == csc_extern;
7802
7803         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7804                                        orig_qual_indirect);
7805
7806         /* C99 6.2.2p7: It is invalid (compile-time undefined
7807            behavior) to create an 'extern' declaration for a
7808            variable if there is a global declaration that is
7809            'static' and the global declaration is not visible.
7810            (If the static declaration _is_ currently visible,
7811            the 'extern' declaration is taken to refer to that decl.) */
7812         if (extern_ref && current_scope != file_scope)
7813           {
7814             tree global_decl  = identifier_global_value (declarator->u.id.id);
7815             tree visible_decl = lookup_name (declarator->u.id.id);
7816
7817             if (global_decl
7818                 && global_decl != visible_decl
7819                 && VAR_P (global_decl)
7820                 && !TREE_PUBLIC (global_decl))
7821               error_at (loc, "variable previously declared %<static%> "
7822                         "redeclared %<extern%>");
7823           }
7824
7825         decl = build_decl (declarator->id_loc,
7826                            VAR_DECL, declarator->u.id.id, type);
7827         if (size_varies)
7828           C_DECL_VARIABLE_SIZE (decl) = 1;
7829
7830         if (declspecs->inline_p)
7831           pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7832         if (declspecs->noreturn_p)
7833           pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7834
7835         /* At file scope, an initialized extern declaration may follow
7836            a static declaration.  In that case, DECL_EXTERNAL will be
7837            reset later in start_decl.  */
7838         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7839
7840         /* At file scope, the presence of a `static' or `register' storage
7841            class specifier, or the absence of all storage class specifiers
7842            makes this declaration a definition (perhaps tentative).  Also,
7843            the absence of `static' makes it public.  */
7844         if (current_scope == file_scope)
7845           {
7846             TREE_PUBLIC (decl) = storage_class != csc_static;
7847             TREE_STATIC (decl) = !extern_ref;
7848           }
7849         /* Not at file scope, only `static' makes a static definition.  */
7850         else
7851           {
7852             TREE_STATIC (decl) = (storage_class == csc_static);
7853             TREE_PUBLIC (decl) = extern_ref;
7854           }
7855
7856         if (threadp)
7857           set_decl_tls_model (decl, decl_default_tls_model (decl));
7858       }
7859
7860     if ((storage_class == csc_extern
7861          || (storage_class == csc_none
7862              && TREE_CODE (type) == FUNCTION_TYPE
7863              && !funcdef_flag))
7864         && variably_modified_type_p (type, NULL_TREE))
7865       {
7866         /* C99 6.7.5.2p2 */
7867         if (TREE_CODE (type) == FUNCTION_TYPE)
7868           error_at (loc, "non-nested function with variably modified type");
7869         else
7870           error_at (loc, "object with variably modified type must have "
7871                     "no linkage");
7872       }
7873
7874     /* For nested functions disqualify ones taking VLAs by value
7875        from inlining since the middle-end cannot deal with this.
7876        ???  We should arrange for those to be passed by reference
7877        with emitting the copy on the caller side in the frontend.  */
7878     if (storage_class == csc_none
7879         && TREE_CODE (type) == FUNCTION_TYPE)
7880       for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7881         {
7882           tree arg = TREE_VALUE (al);
7883           if (arg != error_mark_node
7884               && C_TYPE_VARIABLE_SIZE (arg))
7885             {
7886               DECL_UNINLINABLE (decl) = 1;
7887               break;
7888             }
7889         }
7890
7891     /* Record `register' declaration for warnings on &
7892        and in case doing stupid register allocation.  */
7893
7894     if (storage_class == csc_register)
7895       {
7896         C_DECL_REGISTER (decl) = 1;
7897         DECL_REGISTER (decl) = 1;
7898       }
7899
7900     /* Record constancy and volatility.  */
7901     c_apply_type_quals_to_decl (type_quals, decl);
7902
7903     /* Apply _Alignas specifiers.  */
7904     if (alignas_align)
7905       {
7906         SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7907         DECL_USER_ALIGN (decl) = 1;
7908       }
7909
7910     /* If a type has volatile components, it should be stored in memory.
7911        Otherwise, the fact that those components are volatile
7912        will be ignored, and would even crash the compiler.
7913        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
7914     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7915         && (VAR_P (decl) ||  TREE_CODE (decl) == PARM_DECL
7916           || TREE_CODE (decl) == RESULT_DECL))
7917       {
7918         /* It is not an error for a structure with volatile fields to
7919            be declared register, but reset DECL_REGISTER since it
7920            cannot actually go in a register.  */
7921         int was_reg = C_DECL_REGISTER (decl);
7922         C_DECL_REGISTER (decl) = 0;
7923         DECL_REGISTER (decl) = 0;
7924         c_mark_addressable (decl);
7925         C_DECL_REGISTER (decl) = was_reg;
7926       }
7927
7928   /* This is the earliest point at which we might know the assembler
7929      name of a variable.  Thus, if it's known before this, die horribly.  */
7930     gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7931                 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7932
7933     if (warn_cxx_compat
7934         && VAR_P (decl)
7935         && TREE_PUBLIC (decl)
7936         && TREE_STATIC (decl)
7937         && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7938             || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7939         && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7940       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7941                   ("non-local variable %qD with anonymous type is "
7942                    "questionable in C++"),
7943                   decl);
7944
7945     return decl;
7946   }
7947 }
7948 \f
7949 /* Decode the parameter-list info for a function type or function definition.
7950    The argument is the value returned by `get_parm_info' (or made in c-parse.c
7951    if there is an identifier list instead of a parameter decl list).
7952    These two functions are separate because when a function returns
7953    or receives functions then each is called multiple times but the order
7954    of calls is different.  The last call to `grokparms' is always the one
7955    that contains the formal parameter names of a function definition.
7956
7957    Return a list of arg types to use in the FUNCTION_TYPE for this function.
7958
7959    FUNCDEF_FLAG is true for a function definition, false for
7960    a mere declaration.  A nonempty identifier-list gets an error message
7961    when FUNCDEF_FLAG is false.  */
7962
7963 static tree
7964 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7965 {
7966   tree arg_types = arg_info->types;
7967
7968   if (funcdef_flag && arg_info->had_vla_unspec)
7969     {
7970       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
7971       /* C99 6.7.5.2p4 */
7972       error ("%<[*]%> not allowed in other than function prototype scope");
7973     }
7974
7975   if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
7976       && !in_system_header_at (input_location))
7977     warning (OPT_Wstrict_prototypes,
7978              "function declaration isn%'t a prototype");
7979
7980   if (arg_types == error_mark_node)
7981     /* Don't set TYPE_ARG_TYPES in this case.  */
7982     return NULL_TREE;
7983
7984   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7985     {
7986       if (!funcdef_flag)
7987         {
7988           pedwarn (input_location, 0, "parameter names (without types) in "
7989                    "function declaration");
7990           arg_info->parms = NULL_TREE;
7991         }
7992       else
7993         arg_info->parms = arg_info->types;
7994
7995       arg_info->types = NULL_TREE;
7996       return NULL_TREE;
7997     }
7998   else
7999     {
8000       tree parm, type, typelt;
8001       unsigned int parmno;
8002
8003       /* In C2X, convert () to (void).  */
8004       if (flag_isoc2x
8005           && !arg_types
8006           && !arg_info->parms)
8007         arg_types = arg_info->types = void_list_node;
8008
8009       /* If there is a parameter of incomplete type in a definition,
8010          this is an error.  In a declaration this is valid, and a
8011          struct or union type may be completed later, before any calls
8012          or definition of the function.  In the case where the tag was
8013          first declared within the parameter list, a warning has
8014          already been given.  If a parameter has void type, then
8015          however the function cannot be defined or called, so
8016          warn.  */
8017
8018       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8019            parm;
8020            parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8021         {
8022           type = TREE_VALUE (typelt);
8023           if (type == error_mark_node)
8024             continue;
8025
8026           if (!COMPLETE_TYPE_P (type))
8027             {
8028               if (funcdef_flag)
8029                 {
8030                   if (DECL_NAME (parm))
8031                     error_at (input_location,
8032                               "parameter %u (%q+D) has incomplete type",
8033                               parmno, parm);
8034                   else
8035                     error_at (DECL_SOURCE_LOCATION (parm),
8036                               "parameter %u has incomplete type",
8037                               parmno);
8038
8039                   TREE_VALUE (typelt) = error_mark_node;
8040                   TREE_TYPE (parm) = error_mark_node;
8041                   arg_types = NULL_TREE;
8042                 }
8043               else if (VOID_TYPE_P (type))
8044                 {
8045                   if (DECL_NAME (parm))
8046                     warning_at (input_location, 0,
8047                                 "parameter %u (%q+D) has void type",
8048                                 parmno, parm);
8049                   else
8050                     warning_at (DECL_SOURCE_LOCATION (parm), 0,
8051                                 "parameter %u has void type",
8052                                 parmno);
8053                 }
8054             }
8055
8056           if (DECL_NAME (parm) && TREE_USED (parm))
8057             warn_if_shadowing (parm);
8058         }
8059       return arg_types;
8060     }
8061 }
8062
8063 /* Allocate and initialize a c_arg_info structure from the parser's
8064    obstack.  */
8065
8066 struct c_arg_info *
8067 build_arg_info (void)
8068 {
8069   struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8070   ret->parms = NULL_TREE;
8071   ret->tags = NULL;
8072   ret->types = NULL_TREE;
8073   ret->others = NULL_TREE;
8074   ret->pending_sizes = NULL;
8075   ret->had_vla_unspec = 0;
8076   return ret;
8077 }
8078
8079 /* Take apart the current scope and return a c_arg_info structure with
8080    info on a parameter list just parsed.
8081
8082    This structure is later fed to 'grokparms' and 'store_parm_decls'.
8083
8084    ELLIPSIS being true means the argument list ended in '...' so don't
8085    append a sentinel (void_list_node) to the end of the type-list.
8086
8087    EXPR is NULL or an expression that needs to be evaluated for the
8088    side effects of array size expressions in the parameters.  */
8089
8090 struct c_arg_info *
8091 get_parm_info (bool ellipsis, tree expr)
8092 {
8093   struct c_binding *b = current_scope->bindings;
8094   struct c_arg_info *arg_info = build_arg_info ();
8095
8096   tree parms = NULL_TREE;
8097   vec<c_arg_tag, va_gc> *tags = NULL;
8098   tree types = NULL_TREE;
8099   tree others = NULL_TREE;
8100
8101   bool gave_void_only_once_err = false;
8102
8103   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8104
8105   /* The bindings in this scope must not get put into a block.
8106      We will take care of deleting the binding nodes.  */
8107   current_scope->bindings = 0;
8108
8109   /* This function is only called if there was *something* on the
8110      parameter list.  */
8111   gcc_assert (b);
8112
8113   /* A parameter list consisting solely of 'void' indicates that the
8114      function takes no arguments.  But if the 'void' is qualified
8115      (by 'const' or 'volatile'), or has a storage class specifier
8116      ('register'), then the behavior is undefined; issue an error.
8117      Typedefs for 'void' are OK (see DR#157).  */
8118   if (b->prev == 0                          /* one binding */
8119       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
8120       && !DECL_NAME (b->decl)               /* anonymous */
8121       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8122     {
8123       if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8124           || C_DECL_REGISTER (b->decl))
8125         error_at (b->locus, "%<void%> as only parameter may not be qualified");
8126
8127       /* There cannot be an ellipsis.  */
8128       if (ellipsis)
8129         error_at (b->locus, "%<void%> must be the only parameter");
8130
8131       arg_info->types = void_list_node;
8132       return arg_info;
8133     }
8134
8135   if (!ellipsis)
8136     types = void_list_node;
8137
8138   /* Break up the bindings list into parms, tags, types, and others;
8139      apply sanity checks; purge the name-to-decl bindings.  */
8140   while (b)
8141     {
8142       tree decl = b->decl;
8143       tree type = TREE_TYPE (decl);
8144       c_arg_tag tag;
8145       const char *keyword;
8146
8147       switch (TREE_CODE (decl))
8148         {
8149         case PARM_DECL:
8150           if (b->id)
8151             {
8152               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8153               I_SYMBOL_BINDING (b->id) = b->shadowed;
8154             }
8155
8156           /* Check for forward decls that never got their actual decl.  */
8157           if (TREE_ASM_WRITTEN (decl))
8158             error_at (b->locus,
8159                       "parameter %q+D has just a forward declaration", decl);
8160           /* Check for (..., void, ...) and issue an error.  */
8161           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8162             {
8163               if (!gave_void_only_once_err)
8164                 {
8165                   error_at (b->locus, "%<void%> must be the only parameter");
8166                   gave_void_only_once_err = true;
8167                 }
8168             }
8169           else
8170             {
8171               /* Valid parameter, add it to the list.  */
8172               DECL_CHAIN (decl) = parms;
8173               parms = decl;
8174
8175               /* Since there is a prototype, args are passed in their
8176                  declared types.  The back end may override this later.  */
8177               DECL_ARG_TYPE (decl) = type;
8178               types = tree_cons (0, type, types);
8179             }
8180           break;
8181
8182         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8183         case UNION_TYPE:    keyword = "union"; goto tag;
8184         case RECORD_TYPE:   keyword = "struct"; goto tag;
8185         tag:
8186           /* Types may not have tag-names, in which case the type
8187              appears in the bindings list with b->id NULL.  */
8188           if (b->id)
8189             {
8190               gcc_assert (I_TAG_BINDING (b->id) == b);
8191               I_TAG_BINDING (b->id) = b->shadowed;
8192             }
8193
8194           /* Warn about any struct, union or enum tags defined in a
8195              parameter list.  The scope of such types is limited to
8196              the parameter list, which is rarely if ever desirable
8197              (it's impossible to call such a function with type-
8198              correct arguments).  An anonymous union parm type is
8199              meaningful as a GNU extension, so don't warn for that.  */
8200           if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8201             {
8202               if (b->id)
8203                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
8204                 warning_at (b->locus, 0,
8205                             "%<%s %E%> declared inside parameter list"
8206                             " will not be visible outside of this definition or"
8207                             " declaration", keyword, b->id);
8208               else
8209                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
8210                 warning_at (b->locus, 0,
8211                             "anonymous %s declared inside parameter list"
8212                             " will not be visible outside of this definition or"
8213                             " declaration", keyword);
8214             }
8215
8216           tag.id = b->id;
8217           tag.type = decl;
8218           vec_safe_push (tags, tag);
8219           break;
8220
8221         case FUNCTION_DECL:
8222           /* FUNCTION_DECLs appear when there is an implicit function
8223              declaration in the parameter list.  */
8224           gcc_assert (b->nested || seen_error ());
8225           goto set_shadowed;
8226
8227         case CONST_DECL:
8228         case TYPE_DECL:
8229           /* CONST_DECLs appear here when we have an embedded enum,
8230              and TYPE_DECLs appear here when we have an embedded struct
8231              or union.  No warnings for this - we already warned about the
8232              type itself.  */
8233
8234           /* When we reinsert this decl in the function body, we need
8235              to reconstruct whether it was marked as nested.  */
8236           gcc_assert (!b->nested);
8237           DECL_CHAIN (decl) = others;
8238           others = decl;
8239           /* fall through */
8240
8241         case ERROR_MARK:
8242         set_shadowed:
8243           /* error_mark_node appears here when we have an undeclared
8244              variable.  Just throw it away.  */
8245           if (b->id)
8246             {
8247               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8248               I_SYMBOL_BINDING (b->id) = b->shadowed;
8249             }
8250           break;
8251
8252           /* Other things that might be encountered.  */
8253         case LABEL_DECL:
8254         case VAR_DECL:
8255         default:
8256           gcc_unreachable ();
8257         }
8258
8259       b = free_binding_and_advance (b);
8260     }
8261
8262   arg_info->parms = parms;
8263   arg_info->tags = tags;
8264   arg_info->types = types;
8265   arg_info->others = others;
8266   arg_info->pending_sizes = expr;
8267   return arg_info;
8268 }
8269 \f
8270 /* Get the struct, enum or union (CODE says which) with tag NAME.
8271    Define the tag as a forward-reference with location LOC if it is
8272    not defined.  HAVE_STD_ATTRS says whether any standard attributes
8273    were present after the struct, union or enum keyword; ATTRS are the
8274    standard attributes present there.  Return a c_typespec structure
8275    for the type specifier.  */
8276
8277 struct c_typespec
8278 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8279                  bool have_std_attrs, tree attrs)
8280 {
8281   struct c_typespec ret;
8282   tree ref;
8283   location_t refloc;
8284
8285   ret.expr = NULL_TREE;
8286   ret.expr_const_operands = true;
8287
8288   /* If a cross reference is requested, look up the type
8289      already defined for this tag and return it.  */
8290
8291   ref = lookup_tag (code, name, false, &refloc);
8292   /* If this is the right type of tag, return what we found.
8293      (This reference will be shadowed by shadow_tag later if appropriate.)
8294      If this is the wrong type of tag, do not return it.  If it was the
8295      wrong type in the same scope, we will have had an error
8296      message already; if in a different scope and declaring
8297      a name, pending_xref_error will give an error message; but if in a
8298      different scope and not declaring a name, this tag should
8299      shadow the previous declaration of a different type of tag, and
8300      this would not work properly if we return the reference found.
8301      (For example, with "struct foo" in an outer scope, "union foo;"
8302      must shadow that tag with a new one of union type.)  */
8303   ret.kind = (ref
8304               ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8305               : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8306   if (ref && TREE_CODE (ref) == code)
8307     {
8308       decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8309       if (C_TYPE_DEFINED_IN_STRUCT (ref)
8310           && loc != UNKNOWN_LOCATION
8311           && warn_cxx_compat)
8312         {
8313           auto_diagnostic_group d;
8314           switch (code)
8315             {
8316             case ENUMERAL_TYPE:
8317               if (warning_at (loc, OPT_Wc___compat,
8318                               ("enum type defined in struct or union "
8319                                "is not visible in C++")))
8320                   inform (refloc, "enum type defined here");
8321               break;
8322             case RECORD_TYPE:
8323               if (warning_at (loc, OPT_Wc___compat,
8324                               ("struct defined in struct or union "
8325                                "is not visible in C++")))
8326                 inform (refloc, "struct defined here");
8327               break;
8328             case UNION_TYPE:
8329               if (warning_at (loc, OPT_Wc___compat,
8330                               ("union defined in struct or union "
8331                                "is not visible in C++")))
8332                 inform (refloc, "union defined here");
8333               break;
8334             default:
8335               gcc_unreachable();
8336             }
8337         }
8338
8339       ret.spec = ref;
8340       return ret;
8341     }
8342
8343   /* If no such tag is yet defined, create a forward-reference node
8344      and record it as the "definition".
8345      When a real declaration of this type is found,
8346      the forward-reference will be altered into a real type.  */
8347
8348   ref = make_node (code);
8349   if (code == ENUMERAL_TYPE)
8350     {
8351       /* Give the type a default layout like unsigned int
8352          to avoid crashing if it does not get defined.  */
8353       SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8354       SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8355       TYPE_USER_ALIGN (ref) = 0;
8356       TYPE_UNSIGNED (ref) = 1;
8357       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8358       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8359       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8360     }
8361
8362   pushtag (loc, name, ref);
8363   decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8364
8365   ret.spec = ref;
8366   return ret;
8367 }
8368
8369 /* Get the struct, enum or union (CODE says which) with tag NAME.
8370    Define the tag as a forward-reference if it is not defined.
8371    Return a tree for the type.  */
8372
8373 tree
8374 xref_tag (enum tree_code code, tree name)
8375 {
8376   return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
8377 }
8378 \f
8379 /* Make sure that the tag NAME is defined *in the current scope*
8380    at least as a forward reference.
8381    LOC is the location of the struct's definition.
8382    CODE says which kind of tag NAME ought to be.
8383
8384    This stores the current value of the file static STRUCT_PARSE_INFO
8385    in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8386    new c_struct_parse_info structure.  The old value of
8387    STRUCT_PARSE_INFO is restored in finish_struct.  */
8388
8389 tree
8390 start_struct (location_t loc, enum tree_code code, tree name,
8391               class c_struct_parse_info **enclosing_struct_parse_info)
8392 {
8393   /* If there is already a tag defined at this scope
8394      (as a forward reference), just return it.  */
8395
8396   tree ref = NULL_TREE;
8397   location_t refloc = UNKNOWN_LOCATION;
8398
8399   if (name != NULL_TREE)
8400     ref = lookup_tag (code, name, true, &refloc);
8401   if (ref && TREE_CODE (ref) == code)
8402     {
8403       if (TYPE_STUB_DECL (ref))
8404         refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8405
8406       if (TYPE_SIZE (ref))
8407         {
8408           auto_diagnostic_group d;
8409           if (code == UNION_TYPE)
8410             error_at (loc, "redefinition of %<union %E%>", name);
8411           else
8412             error_at (loc, "redefinition of %<struct %E%>", name);
8413           if (refloc != UNKNOWN_LOCATION)
8414             inform (refloc, "originally defined here");
8415           /* Don't create structures using a name already in use.  */
8416           ref = NULL_TREE;
8417         }
8418       else if (C_TYPE_BEING_DEFINED (ref))
8419         {
8420           if (code == UNION_TYPE)
8421             error_at (loc, "nested redefinition of %<union %E%>", name);
8422           else
8423             error_at (loc, "nested redefinition of %<struct %E%>", name);
8424           /* Don't bother to report "originally defined here" for a
8425              nested redefinition; the original definition should be
8426              obvious.  */
8427           /* Don't create structures that contain themselves.  */
8428           ref = NULL_TREE;
8429         }
8430     }
8431
8432   /* Otherwise create a forward-reference just so the tag is in scope.  */
8433
8434   if (ref == NULL_TREE || TREE_CODE (ref) != code)
8435     {
8436       ref = make_node (code);
8437       pushtag (loc, name, ref);
8438     }
8439
8440   C_TYPE_BEING_DEFINED (ref) = 1;
8441   for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8442     TYPE_PACKED (v) = flag_pack_struct;
8443
8444   *enclosing_struct_parse_info = struct_parse_info;
8445   struct_parse_info = new c_struct_parse_info ();
8446
8447   /* FIXME: This will issue a warning for a use of a type defined
8448      within a statement expr used within sizeof, et. al.  This is not
8449      terribly serious as C++ doesn't permit statement exprs within
8450      sizeof anyhow.  */
8451   if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8452     warning_at (loc, OPT_Wc___compat,
8453                 "defining type in %qs expression is invalid in C++",
8454                 (in_sizeof
8455                  ? "sizeof"
8456                  : (in_typeof ? "typeof" : "alignof")));
8457
8458   return ref;
8459 }
8460
8461 /* Process the specs, declarator and width (NULL if omitted)
8462    of a structure component, returning a FIELD_DECL node.
8463    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8464    DECL_ATTRS is as for grokdeclarator.
8465
8466    LOC is the location of the structure component.
8467
8468    This is done during the parsing of the struct declaration.
8469    The FIELD_DECL nodes are chained together and the lot of them
8470    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
8471
8472 tree
8473 grokfield (location_t loc,
8474            struct c_declarator *declarator, struct c_declspecs *declspecs,
8475            tree width, tree *decl_attrs)
8476 {
8477   tree value;
8478
8479   if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8480       && width == NULL_TREE)
8481     {
8482       /* This is an unnamed decl.
8483
8484          If we have something of the form "union { list } ;" then this
8485          is the anonymous union extension.  Similarly for struct.
8486
8487          If this is something of the form "struct foo;", then
8488            If MS or Plan 9 extensions are enabled, this is handled as
8489              an anonymous struct.
8490            Otherwise this is a forward declaration of a structure tag.
8491
8492          If this is something of the form "foo;" and foo is a TYPE_DECL, then
8493            If foo names a structure or union without a tag, then this
8494              is an anonymous struct (this is permitted by C11).
8495            If MS or Plan 9 extensions are enabled and foo names a
8496              structure, then again this is an anonymous struct.
8497            Otherwise this is an error.
8498
8499          Oh what a horrid tangled web we weave.  I wonder if MS consciously
8500          took this from Plan 9 or if it was an accident of implementation
8501          that took root before someone noticed the bug...  */
8502
8503       tree type = declspecs->type;
8504       bool ok = false;
8505
8506       if (RECORD_OR_UNION_TYPE_P (type)
8507           && (flag_ms_extensions
8508               || flag_plan9_extensions
8509               || !declspecs->typedef_p))
8510         {
8511           if (flag_ms_extensions || flag_plan9_extensions)
8512             ok = true;
8513           else if (TYPE_NAME (type) == NULL)
8514             ok = true;
8515           else
8516             ok = false;
8517         }
8518       if (!ok)
8519         {
8520           pedwarn (loc, 0, "declaration does not declare anything");
8521           return NULL_TREE;
8522         }
8523       if (flag_isoc99)
8524         pedwarn_c99 (loc, OPT_Wpedantic,
8525                      "ISO C99 doesn%'t support unnamed structs/unions");
8526       else
8527         pedwarn_c99 (loc, OPT_Wpedantic,
8528                      "ISO C90 doesn%'t support unnamed structs/unions");
8529     }
8530
8531   value = grokdeclarator (declarator, declspecs, FIELD, false,
8532                           width ? &width : NULL, decl_attrs, NULL, NULL,
8533                           DEPRECATED_NORMAL);
8534
8535   finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8536   DECL_INITIAL (value) = width;
8537   if (width)
8538     SET_DECL_C_BIT_FIELD (value);
8539
8540   if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8541     {
8542       /* If we currently have a binding for this field, set the
8543          in_struct field in the binding, so that we warn about lookups
8544          which find it.  */
8545       struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8546       if (b != NULL)
8547         {
8548           /* If the in_struct field is not yet set, push it on a list
8549              to be cleared when this struct is finished.  */
8550           if (!b->in_struct)
8551             {
8552               struct_parse_info->fields.safe_push (b);
8553               b->in_struct = 1;
8554             }
8555         }
8556     }
8557
8558   return value;
8559 }
8560 \f
8561 /* Subroutine of detect_field_duplicates: return whether X and Y,
8562    which are both fields in the same struct, have duplicate field
8563    names.  */
8564
8565 static bool
8566 is_duplicate_field (tree x, tree y)
8567 {
8568   if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8569     return true;
8570
8571   /* When using -fplan9-extensions, an anonymous field whose name is a
8572      typedef can duplicate a field name.  */
8573   if (flag_plan9_extensions
8574       && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8575     {
8576       tree xt, xn, yt, yn;
8577
8578       xt = TREE_TYPE (x);
8579       if (DECL_NAME (x) != NULL_TREE)
8580         xn = DECL_NAME (x);
8581       else if (RECORD_OR_UNION_TYPE_P (xt)
8582                && TYPE_NAME (xt) != NULL_TREE
8583                && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8584         xn = DECL_NAME (TYPE_NAME (xt));
8585       else
8586         xn = NULL_TREE;
8587
8588       yt = TREE_TYPE (y);
8589       if (DECL_NAME (y) != NULL_TREE)
8590         yn = DECL_NAME (y);
8591       else if (RECORD_OR_UNION_TYPE_P (yt)
8592                && TYPE_NAME (yt) != NULL_TREE
8593                && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8594         yn = DECL_NAME (TYPE_NAME (yt));
8595       else
8596         yn = NULL_TREE;
8597
8598       if (xn != NULL_TREE && xn == yn)
8599         return true;
8600     }
8601
8602   return false;
8603 }
8604
8605 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8606    to HTAB, giving errors for any duplicates.  */
8607
8608 static void
8609 detect_field_duplicates_hash (tree fieldlist,
8610                               hash_table<nofree_ptr_hash <tree_node> > *htab)
8611 {
8612   tree x, y;
8613   tree_node **slot;
8614
8615   for (x = fieldlist; x ; x = DECL_CHAIN (x))
8616     if ((y = DECL_NAME (x)) != NULL_TREE)
8617       {
8618         slot = htab->find_slot (y, INSERT);
8619         if (*slot)
8620           {
8621             error ("duplicate member %q+D", x);
8622             DECL_NAME (x) = NULL_TREE;
8623           }
8624         *slot = y;
8625       }
8626     else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8627       {
8628         detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8629
8630         /* When using -fplan9-extensions, an anonymous field whose
8631            name is a typedef can duplicate a field name.  */
8632         if (flag_plan9_extensions
8633             && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8634             && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8635           {
8636             tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8637             slot = htab->find_slot (xn, INSERT);
8638             if (*slot)
8639               error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8640             *slot = xn;
8641           }
8642       }
8643 }
8644
8645 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
8646    the list such that this does not present a problem later.  */
8647
8648 static void
8649 detect_field_duplicates (tree fieldlist)
8650 {
8651   tree x, y;
8652   int timeout = 10;
8653
8654   /* If the struct is the list of instance variables of an Objective-C
8655      class, then we need to check all the instance variables of
8656      superclasses when checking for duplicates (since you can't have
8657      an instance variable in a subclass with the same name as an
8658      instance variable in a superclass).  We pass on this job to the
8659      Objective-C compiler.  objc_detect_field_duplicates() will return
8660      false if we are not checking the list of instance variables and
8661      the C frontend should proceed with the standard field duplicate
8662      checks.  If we are checking the list of instance variables, the
8663      ObjC frontend will do the check, emit the errors if needed, and
8664      then return true.  */
8665   if (c_dialect_objc ())
8666     if (objc_detect_field_duplicates (false))
8667       return;
8668
8669   /* First, see if there are more than "a few" fields.
8670      This is trivially true if there are zero or one fields.  */
8671   if (!fieldlist || !DECL_CHAIN (fieldlist))
8672     return;
8673   x = fieldlist;
8674   do {
8675     timeout--;
8676     if (DECL_NAME (x) == NULL_TREE
8677         && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8678       timeout = 0;
8679     x = DECL_CHAIN (x);
8680   } while (timeout > 0 && x);
8681
8682   /* If there were "few" fields and no anonymous structures or unions,
8683      avoid the overhead of allocating a hash table.  Instead just do
8684      the nested traversal thing.  */
8685   if (timeout > 0)
8686     {
8687       for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8688         /* When using -fplan9-extensions, we can have duplicates
8689            between typedef names and fields.  */
8690         if (DECL_NAME (x)
8691             || (flag_plan9_extensions
8692                 && DECL_NAME (x) == NULL_TREE
8693                 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8694                 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8695                 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8696           {
8697             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8698               if (is_duplicate_field (y, x))
8699                 {
8700                   error ("duplicate member %q+D", x);
8701                   DECL_NAME (x) = NULL_TREE;
8702                 }
8703           }
8704     }
8705   else
8706     {
8707       hash_table<nofree_ptr_hash <tree_node> > htab (37);
8708       detect_field_duplicates_hash (fieldlist, &htab);
8709     }
8710 }
8711
8712 /* Finish up struct info used by -Wc++-compat.  */
8713
8714 static void
8715 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8716                                location_t record_loc)
8717 {
8718   unsigned int ix;
8719   tree x;
8720   struct c_binding *b;
8721
8722   if (fieldlist == NULL_TREE)
8723     {
8724       if (code == RECORD_TYPE)
8725         warning_at (record_loc, OPT_Wc___compat,
8726                     "empty struct has size 0 in C, size 1 in C++");
8727       else
8728         warning_at (record_loc, OPT_Wc___compat,
8729                     "empty union has size 0 in C, size 1 in C++");
8730     }
8731
8732   /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8733      the current struct.  We do this now at the end of the struct
8734      because the flag is used to issue visibility warnings, and we
8735      only want to issue those warnings if the type is referenced
8736      outside of the struct declaration.  */
8737   FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8738     C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8739
8740   /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8741      typedefs used when declaring fields in this struct.  If the name
8742      of any of the fields is also a typedef name then the struct would
8743      not parse in C++, because the C++ lookup rules say that the
8744      typedef name would be looked up in the context of the struct, and
8745      would thus be the field rather than the typedef.  */
8746   if (!struct_parse_info->typedefs_seen.is_empty ()
8747       && fieldlist != NULL_TREE)
8748     {
8749       /* Use a hash_set<tree> using the name of the typedef.  We can use
8750          a hash_set<tree> because identifiers are interned.  */
8751       hash_set<tree> tset;
8752
8753       FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8754         tset.add (DECL_NAME (x));
8755
8756       for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8757         {
8758           if (DECL_NAME (x) != NULL_TREE
8759               && tset.contains (DECL_NAME (x)))
8760             {
8761               warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8762                           ("using %qD as both field and typedef name is "
8763                            "invalid in C++"),
8764                           x);
8765               /* FIXME: It would be nice to report the location where
8766                  the typedef name is used.  */
8767             }
8768         }
8769     }
8770
8771   /* For each field which has a binding and which was not defined in
8772      an enclosing struct, clear the in_struct field.  */
8773   FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8774     b->in_struct = 0;
8775 }
8776
8777 /* Function to help qsort sort FIELD_DECLs by name order.  */
8778
8779 static int
8780 field_decl_cmp (const void *x_p, const void *y_p)
8781 {
8782   const tree *const x = (const tree *) x_p;
8783   const tree *const y = (const tree *) y_p;
8784
8785   if (DECL_NAME (*x) == DECL_NAME (*y))
8786     /* A nontype is "greater" than a type.  */
8787     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8788   if (DECL_NAME (*x) == NULL_TREE)
8789     return -1;
8790   if (DECL_NAME (*y) == NULL_TREE)
8791     return 1;
8792   if (DECL_NAME (*x) < DECL_NAME (*y))
8793     return -1;
8794   return 1;
8795 }
8796
8797 /* If this structure or union completes the type of any previous
8798    variable declaration, lay it out and output its rtl.  */
8799 static void
8800 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8801 {
8802   for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8803     {
8804       tree decl = TREE_VALUE (x);
8805       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8806         layout_array_type (TREE_TYPE (decl));
8807       if (TREE_CODE (decl) != TYPE_DECL)
8808         {
8809           relayout_decl (decl);
8810           if (c_dialect_objc ())
8811             objc_check_decl (decl);
8812           rest_of_decl_compilation (decl, toplevel, 0);
8813         }
8814     }
8815 }
8816
8817
8818 /* Determine whether the FIELD_DECL X is a flexible array member according to
8819    the following info:
8820   A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
8821   B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
8822      or "[1]";
8823   C. flag_strict_flex_arrays;
8824   D. the attribute strict_flex_array that is attached to the field
8825      if presenting.
8826   Return TRUE when it's a flexible array member, FALSE otherwise.  */
8827
8828 static bool
8829 is_flexible_array_member_p (bool is_last_field,
8830                             tree x)
8831 {
8832   /* If not the last field, return false.  */
8833   if (!is_last_field)
8834     return false;
8835
8836   /* If not an array field, return false.  */
8837   if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
8838     return false;
8839
8840   bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
8841   bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
8842   bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
8843
8844   unsigned int strict_flex_array_level = flag_strict_flex_arrays;
8845
8846   tree attr_strict_flex_array = lookup_attribute ("strict_flex_array",
8847                                                   DECL_ATTRIBUTES (x));
8848   /* If there is a strict_flex_array attribute attached to the field,
8849      override the flag_strict_flex_arrays.  */
8850   if (attr_strict_flex_array)
8851     {
8852       /* Get the value of the level first from the attribute.  */
8853       unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
8854       gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
8855       attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
8856       gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
8857       attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
8858       gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
8859       attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
8860
8861       /* The attribute has higher priority than flag_struct_flex_array.  */
8862       strict_flex_array_level = attr_strict_flex_array_level;
8863     }
8864
8865   switch (strict_flex_array_level)
8866     {
8867       case 0:
8868         /* Default, all trailing arrays are flexible array members.  */
8869         return true;
8870       case 1:
8871         /* Level 1: all "[1]", "[0]", and "[]" are flexible array members.  */
8872         if (is_one_element_array)
8873           return true;
8874         /* FALLTHROUGH.  */
8875       case 2:
8876         /* Level 2: all "[0]", and "[]" are flexible array members.  */
8877         if (is_zero_length_array)
8878           return true;
8879         /* FALLTHROUGH.  */
8880       case 3:
8881         /* Level 3: Only "[]" are flexible array members.  */
8882         if (is_flexible_array)
8883           return true;
8884         break;
8885       default:
8886         gcc_unreachable ();
8887     }
8888   return false;
8889 }
8890
8891
8892 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8893    LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8894    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8895    ATTRIBUTES are attributes to be applied to the structure.
8896
8897    ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8898    the struct was started.  */
8899
8900 tree
8901 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8902                class c_struct_parse_info *enclosing_struct_parse_info)
8903 {
8904   tree x;
8905   bool toplevel = file_scope == current_scope;
8906
8907   /* If this type was previously laid out as a forward reference,
8908      make sure we lay it out again.  */
8909
8910   TYPE_SIZE (t) = NULL_TREE;
8911
8912   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8913
8914   if (pedantic)
8915     {
8916       for (x = fieldlist; x; x = DECL_CHAIN (x))
8917         {
8918           if (DECL_NAME (x) != NULL_TREE)
8919             break;
8920           if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8921             break;
8922         }
8923
8924       if (x == NULL_TREE)
8925         {
8926           if (TREE_CODE (t) == UNION_TYPE)
8927             {
8928               if (fieldlist)
8929                 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8930               else
8931                 pedwarn (loc, OPT_Wpedantic, "union has no members");
8932             }
8933           else
8934             {
8935               if (fieldlist)
8936                 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8937               else
8938                 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8939             }
8940         }
8941     }
8942
8943   /* Install struct as DECL_CONTEXT of each field decl.
8944      Also process specified field sizes, found in the DECL_INITIAL,
8945      storing 0 there after the type has been changed to precision equal
8946      to its width, rather than the precision of the specified standard
8947      type.  (Correct layout requires the original type to have been preserved
8948      until now.)  */
8949
8950   bool saw_named_field = false;
8951   for (x = fieldlist; x; x = DECL_CHAIN (x))
8952     {
8953       /* Whether this field is the last field of the structure or union.
8954          for UNION, any field is the last field of it.  */
8955       bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
8956                             || (TREE_CODE (t) == UNION_TYPE);
8957
8958       if (TREE_TYPE (x) == error_mark_node)
8959         continue;
8960
8961       DECL_CONTEXT (x) = t;
8962
8963       /* If any field is const, the structure type is pseudo-const.  */
8964       if (TREE_READONLY (x))
8965         C_TYPE_FIELDS_READONLY (t) = 1;
8966       else
8967         {
8968           /* A field that is pseudo-const makes the structure likewise.  */
8969           tree t1 = strip_array_types (TREE_TYPE (x));
8970           if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8971             C_TYPE_FIELDS_READONLY (t) = 1;
8972         }
8973
8974       /* Any field that is volatile means variables of this type must be
8975          treated in some ways as volatile.  */
8976       if (TREE_THIS_VOLATILE (x))
8977         C_TYPE_FIELDS_VOLATILE (t) = 1;
8978
8979       /* Any field of nominal variable size implies structure is too.  */
8980       if (C_DECL_VARIABLE_SIZE (x))
8981         C_TYPE_VARIABLE_SIZE (t) = 1;
8982
8983       if (DECL_C_BIT_FIELD (x))
8984         {
8985           unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8986           DECL_SIZE (x) = bitsize_int (width);
8987           DECL_BIT_FIELD (x) = 1;
8988         }
8989
8990       if (TYPE_PACKED (t)
8991           && (DECL_BIT_FIELD (x)
8992               || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8993         DECL_PACKED (x) = 1;
8994
8995       /* Detect flexible array member in an invalid context.  */
8996       if (flexible_array_member_type_p (TREE_TYPE (x)))
8997         {
8998           if (TREE_CODE (t) == UNION_TYPE)
8999             {
9000               error_at (DECL_SOURCE_LOCATION (x),
9001                         "flexible array member in union");
9002               TREE_TYPE (x) = error_mark_node;
9003             }
9004           else if (!is_last_field)
9005             {
9006               error_at (DECL_SOURCE_LOCATION (x),
9007                         "flexible array member not at end of struct");
9008               TREE_TYPE (x) = error_mark_node;
9009             }
9010           else if (!saw_named_field)
9011             {
9012               error_at (DECL_SOURCE_LOCATION (x),
9013                         "flexible array member in a struct with no named "
9014                         "members");
9015               TREE_TYPE (x) = error_mark_node;
9016             }
9017         }
9018
9019       if (pedantic && TREE_CODE (t) == RECORD_TYPE
9020           && flexible_array_type_p (TREE_TYPE (x)))
9021         pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9022                  "invalid use of structure with flexible array member");
9023
9024       /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x.  */
9025       DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9026
9027       if (DECL_NAME (x)
9028           || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9029         saw_named_field = true;
9030     }
9031
9032   detect_field_duplicates (fieldlist);
9033
9034   /* Now we have the nearly final fieldlist.  Record it,
9035      then lay out the structure or union (including the fields).  */
9036
9037   TYPE_FIELDS (t) = fieldlist;
9038
9039   maybe_apply_pragma_scalar_storage_order (t);
9040
9041   layout_type (t);
9042
9043   if (TYPE_SIZE_UNIT (t)
9044       && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9045       && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9046       && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9047     error ("type %qT is too large", t);
9048
9049   /* Give bit-fields their proper types and rewrite the type of array fields
9050      with scalar component if the enclosing type has reverse storage order.  */
9051   for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9052     {
9053       if (TREE_CODE (field) == FIELD_DECL
9054           && DECL_INITIAL (field)
9055           && TREE_TYPE (field) != error_mark_node)
9056         {
9057           unsigned HOST_WIDE_INT width
9058             = tree_to_uhwi (DECL_INITIAL (field));
9059           tree type = TREE_TYPE (field);
9060           if (width != TYPE_PRECISION (type))
9061             {
9062               TREE_TYPE (field)
9063                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
9064               SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9065             }
9066           DECL_INITIAL (field) = NULL_TREE;
9067         }
9068       else if (TYPE_REVERSE_STORAGE_ORDER (t)
9069                && TREE_CODE (field) == FIELD_DECL
9070                && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9071         {
9072           tree ftype = TREE_TYPE (field);
9073           tree ctype = strip_array_types (ftype);
9074           if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9075             {
9076               tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9077               tree *typep = &fmain_type;
9078               do {
9079                 *typep = build_distinct_type_copy (*typep);
9080                 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9081                 typep = &TREE_TYPE (*typep);
9082               } while (TREE_CODE (*typep) == ARRAY_TYPE);
9083               TREE_TYPE (field)
9084                 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9085             }
9086         }
9087
9088       /* Warn on problematic type punning for storage order purposes.  */
9089       if (TREE_CODE (t) == UNION_TYPE
9090           && TREE_CODE (field) == FIELD_DECL
9091           && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9092         {
9093           tree ftype = TREE_TYPE (field);
9094           if (TREE_CODE (ftype) == ARRAY_TYPE)
9095             ftype = strip_array_types (ftype);
9096           if (RECORD_OR_UNION_TYPE_P (ftype)
9097               && TYPE_REVERSE_STORAGE_ORDER (ftype)
9098                  != TYPE_REVERSE_STORAGE_ORDER (t))
9099             warning_at (DECL_SOURCE_LOCATION (field),
9100                         OPT_Wscalar_storage_order,
9101                         "type punning toggles scalar storage order");
9102         }
9103     }
9104
9105   /* Now we have the truly final field list.
9106      Store it in this type and in the variants.  */
9107
9108   TYPE_FIELDS (t) = fieldlist;
9109
9110   /* If there are lots of fields, sort so we can look through them fast.
9111      We arbitrarily consider 16 or more elts to be "a lot".  */
9112
9113   {
9114     int len = 0;
9115
9116     for (x = fieldlist; x; x = DECL_CHAIN (x))
9117       {
9118         if (len > 15 || DECL_NAME (x) == NULL)
9119           break;
9120         len += 1;
9121       }
9122
9123     if (len > 15)
9124       {
9125         tree *field_array;
9126         struct lang_type *space;
9127         struct sorted_fields_type *space2;
9128
9129         len += list_length (x);
9130
9131         /* Use the same allocation policy here that make_node uses, to
9132           ensure that this lives as long as the rest of the struct decl.
9133           All decls in an inline function need to be saved.  */
9134
9135         space = ggc_cleared_alloc<struct lang_type> ();
9136         space2 = (sorted_fields_type *) ggc_internal_alloc
9137           (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9138
9139         len = 0;
9140         space->s = space2;
9141         field_array = &space2->elts[0];
9142         for (x = fieldlist; x; x = DECL_CHAIN (x))
9143           {
9144             field_array[len++] = x;
9145
9146             /* If there is anonymous struct or union, break out of the loop.  */
9147             if (DECL_NAME (x) == NULL)
9148               break;
9149           }
9150         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
9151         if (x == NULL)
9152           {
9153             TYPE_LANG_SPECIFIC (t) = space;
9154             TYPE_LANG_SPECIFIC (t)->s->len = len;
9155             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9156             qsort (field_array, len, sizeof (tree), field_decl_cmp);
9157           }
9158       }
9159   }
9160
9161   /* If this was supposed to be a transparent union, but we can't
9162      make it one, warn and turn off the flag.  */
9163   if (TREE_CODE (t) == UNION_TYPE
9164       && TYPE_TRANSPARENT_AGGR (t)
9165       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9166     {
9167       TYPE_TRANSPARENT_AGGR (t) = 0;
9168       warning_at (loc, 0, "union cannot be made transparent");
9169     }
9170
9171   tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9172   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9173     {
9174       TYPE_FIELDS (x) = TYPE_FIELDS (t);
9175       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9176       TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9177       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9178       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9179       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9180       C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9181     }
9182
9183   /* Update type location to the one of the definition, instead of e.g.
9184      a forward declaration.  */
9185   if (TYPE_STUB_DECL (t))
9186     DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9187
9188   /* Finish debugging output for this type.  */
9189   rest_of_type_compilation (t, toplevel);
9190
9191   finish_incomplete_vars (incomplete_vars, toplevel);
9192
9193   /* If we're inside a function proper, i.e. not file-scope and not still
9194      parsing parameters, then arrange for the size of a variable sized type
9195      to be bound now.  */
9196   if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
9197     add_stmt (build_stmt (loc,
9198                           DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9199
9200   if (warn_cxx_compat)
9201     warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9202
9203   delete struct_parse_info;
9204
9205   struct_parse_info = enclosing_struct_parse_info;
9206
9207   /* If this struct is defined inside a struct, add it to
9208      struct_types.  */
9209   if (warn_cxx_compat
9210       && struct_parse_info != NULL
9211       && !in_sizeof && !in_typeof && !in_alignof)
9212     struct_parse_info->struct_types.safe_push (t);
9213
9214   return t;
9215 }
9216
9217 static struct {
9218   gt_pointer_operator new_value;
9219   void *cookie;
9220 } resort_data;
9221
9222 /* This routine compares two fields like field_decl_cmp but using the
9223 pointer operator in resort_data.  */
9224
9225 static int
9226 resort_field_decl_cmp (const void *x_p, const void *y_p)
9227 {
9228   const tree *const x = (const tree *) x_p;
9229   const tree *const y = (const tree *) y_p;
9230
9231   if (DECL_NAME (*x) == DECL_NAME (*y))
9232     /* A nontype is "greater" than a type.  */
9233     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9234   if (DECL_NAME (*x) == NULL_TREE)
9235     return -1;
9236   if (DECL_NAME (*y) == NULL_TREE)
9237     return 1;
9238   {
9239     tree d1 = DECL_NAME (*x);
9240     tree d2 = DECL_NAME (*y);
9241     resort_data.new_value (&d1, &d1, resort_data.cookie);
9242     resort_data.new_value (&d2, &d2, resort_data.cookie);
9243     if (d1 < d2)
9244       return -1;
9245   }
9246   return 1;
9247 }
9248
9249 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
9250
9251 void
9252 resort_sorted_fields (void *obj,
9253                       void * ARG_UNUSED (orig_obj),
9254                       gt_pointer_operator new_value,
9255                       void *cookie)
9256 {
9257   struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9258   resort_data.new_value = new_value;
9259   resort_data.cookie = cookie;
9260   qsort (&sf->elts[0], sf->len, sizeof (tree),
9261          resort_field_decl_cmp);
9262 }
9263
9264 /* Lay out the type T, and its element type, and so on.  */
9265
9266 static void
9267 layout_array_type (tree t)
9268 {
9269   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9270     layout_array_type (TREE_TYPE (t));
9271   layout_type (t);
9272 }
9273 \f
9274 /* Begin compiling the definition of an enumeration type.
9275    NAME is its name (or null if anonymous).
9276    LOC is the enum's location.
9277    Returns the type object, as yet incomplete.
9278    Also records info about it so that build_enumerator
9279    may be used to declare the individual values as they are read.  */
9280
9281 tree
9282 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
9283 {
9284   tree enumtype = NULL_TREE;
9285   location_t enumloc = UNKNOWN_LOCATION;
9286
9287   /* If this is the real definition for a previous forward reference,
9288      fill in the contents in the same object that used to be the
9289      forward reference.  */
9290
9291   if (name != NULL_TREE)
9292     enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9293
9294   if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9295     {
9296       enumtype = make_node (ENUMERAL_TYPE);
9297       pushtag (loc, name, enumtype);
9298     }
9299   /* Update type location to the one of the definition, instead of e.g.
9300      a forward declaration.  */
9301   else if (TYPE_STUB_DECL (enumtype))
9302     {
9303       enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9304       DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9305     }
9306
9307   if (C_TYPE_BEING_DEFINED (enumtype))
9308     error_at (loc, "nested redefinition of %<enum %E%>", name);
9309
9310   C_TYPE_BEING_DEFINED (enumtype) = 1;
9311
9312   if (TYPE_VALUES (enumtype) != NULL_TREE)
9313     {
9314       /* This enum is a named one that has been declared already.  */
9315       auto_diagnostic_group d;
9316       error_at (loc, "redeclaration of %<enum %E%>", name);
9317       if (enumloc != UNKNOWN_LOCATION)
9318         inform (enumloc, "originally defined here");
9319
9320       /* Completely replace its old definition.
9321          The old enumerators remain defined, however.  */
9322       TYPE_VALUES (enumtype) = NULL_TREE;
9323     }
9324
9325   the_enum->enum_next_value = integer_zero_node;
9326   the_enum->enum_overflow = 0;
9327
9328   if (flag_short_enums)
9329     for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9330       TYPE_PACKED (v) = 1;
9331
9332   /* FIXME: This will issue a warning for a use of a type defined
9333      within sizeof in a statement expr.  This is not terribly serious
9334      as C++ doesn't permit statement exprs within sizeof anyhow.  */
9335   if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9336     warning_at (loc, OPT_Wc___compat,
9337                 "defining type in %qs expression is invalid in C++",
9338                 (in_sizeof
9339                  ? "sizeof"
9340                  : (in_typeof ? "typeof" : "alignof")));
9341
9342   return enumtype;
9343 }
9344
9345 /* After processing and defining all the values of an enumeration type,
9346    install their decls in the enumeration type and finish it off.
9347    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9348    and ATTRIBUTES are the specified attributes.
9349    Returns ENUMTYPE.  */
9350
9351 tree
9352 finish_enum (tree enumtype, tree values, tree attributes)
9353 {
9354   tree pair, tem;
9355   tree minnode = NULL_TREE, maxnode = NULL_TREE;
9356   int precision;
9357   signop sign;
9358   bool toplevel = (file_scope == current_scope);
9359   struct lang_type *lt;
9360
9361   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9362
9363   /* Calculate the maximum value of any enumerator in this type.  */
9364
9365   if (values == error_mark_node)
9366     minnode = maxnode = integer_zero_node;
9367   else
9368     {
9369       minnode = maxnode = TREE_VALUE (values);
9370       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9371         {
9372           tree value = TREE_VALUE (pair);
9373           if (tree_int_cst_lt (maxnode, value))
9374             maxnode = value;
9375           if (tree_int_cst_lt (value, minnode))
9376             minnode = value;
9377         }
9378     }
9379
9380   /* Construct the final type of this enumeration.  It is the same
9381      as one of the integral types - the narrowest one that fits, except
9382      that normally we only go as narrow as int - and signed iff any of
9383      the values are negative.  */
9384   sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9385   precision = MAX (tree_int_cst_min_precision (minnode, sign),
9386                    tree_int_cst_min_precision (maxnode, sign));
9387
9388   /* If the precision of the type was specified with an attribute and it
9389      was too small, give an error.  Otherwise, use it.  */
9390   if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9391     {
9392       if (precision > TYPE_PRECISION (enumtype))
9393         {
9394           TYPE_PRECISION (enumtype) = 0;
9395           error ("specified mode too small for enumerated values");
9396         }
9397       else
9398         precision = TYPE_PRECISION (enumtype);
9399     }
9400   else
9401     TYPE_PRECISION (enumtype) = 0;
9402
9403   if (TYPE_PACKED (enumtype)
9404       || precision > TYPE_PRECISION (integer_type_node)
9405       || TYPE_PRECISION (enumtype))
9406     {
9407       tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9408       if (tem == NULL)
9409         {
9410           warning (0, "enumeration values exceed range of largest integer");
9411           tem = long_long_integer_type_node;
9412         }
9413     }
9414   else
9415     tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9416
9417   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9418   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9419   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9420   SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9421   TYPE_SIZE (enumtype) = NULL_TREE;
9422   TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9423
9424   layout_type (enumtype);
9425
9426   if (values != error_mark_node)
9427     {
9428       /* Change the type of the enumerators to be the enum type.  We
9429          need to do this irrespective of the size of the enum, for
9430          proper type checking.  Replace the DECL_INITIALs of the
9431          enumerators, and the value slots of the list, with copies
9432          that have the enum type; they cannot be modified in place
9433          because they may be shared (e.g.  integer_zero_node) Finally,
9434          change the purpose slots to point to the names of the decls.  */
9435       for (pair = values; pair; pair = TREE_CHAIN (pair))
9436         {
9437           tree enu = TREE_PURPOSE (pair);
9438           tree ini = DECL_INITIAL (enu);
9439
9440           TREE_TYPE (enu) = enumtype;
9441
9442           /* The ISO C Standard mandates enumerators to have type int,
9443              even though the underlying type of an enum type is
9444              unspecified.  However, GCC allows enumerators of any
9445              integer type as an extensions.  build_enumerator()
9446              converts any enumerators that fit in an int to type int,
9447              to avoid promotions to unsigned types when comparing
9448              integers with enumerators that fit in the int range.
9449              When -pedantic is given, build_enumerator() would have
9450              already warned about those that don't fit. Here we
9451              convert the rest to the enumerator type. */
9452           if (TREE_TYPE (ini) != integer_type_node)
9453             ini = convert (enumtype, ini);
9454
9455           DECL_INITIAL (enu) = ini;
9456           TREE_PURPOSE (pair) = DECL_NAME (enu);
9457           /* To match the C++ FE, store the CONST_DECL rather than just its
9458              value.  */
9459           TREE_VALUE (pair) = enu;
9460         }
9461
9462       TYPE_VALUES (enumtype) = values;
9463     }
9464
9465   /* Record the min/max values so that we can warn about bit-field
9466      enumerations that are too small for the values.  */
9467   lt = ggc_cleared_alloc<struct lang_type> ();
9468   lt->enum_min = minnode;
9469   lt->enum_max = maxnode;
9470   TYPE_LANG_SPECIFIC (enumtype) = lt;
9471
9472   /* Fix up all variant types of this enum type.  */
9473   tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9474   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9475     {
9476       C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9477       if (tem == enumtype)
9478         continue;
9479       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9480       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9481       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9482       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9483       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9484       SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9485       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9486       SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9487       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9488       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9489       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9490     }
9491
9492   /* Finish debugging output for this type.  */
9493   rest_of_type_compilation (enumtype, toplevel);
9494
9495   finish_incomplete_vars (incomplete_vars, toplevel);
9496
9497   /* If this enum is defined inside a struct, add it to
9498      struct_types.  */
9499   if (warn_cxx_compat
9500       && struct_parse_info != NULL
9501       && !in_sizeof && !in_typeof && !in_alignof)
9502     struct_parse_info->struct_types.safe_push (enumtype);
9503
9504   C_TYPE_BEING_DEFINED (enumtype) = 0;
9505
9506   return enumtype;
9507 }
9508
9509 /* Build and install a CONST_DECL for one value of the
9510    current enumeration type (one that was begun with start_enum).
9511    DECL_LOC is the location of the enumerator.
9512    LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9513    Return a tree-list containing the CONST_DECL and its value.
9514    Assignment of sequential values by default is handled here.  */
9515
9516 tree
9517 build_enumerator (location_t decl_loc, location_t loc,
9518                   struct c_enum_contents *the_enum, tree name, tree value)
9519 {
9520   tree decl, type;
9521
9522   /* Validate and default VALUE.  */
9523
9524   if (value != NULL_TREE)
9525     {
9526       /* Don't issue more errors for error_mark_node (i.e. an
9527          undeclared identifier) - just ignore the value expression.  */
9528       if (value == error_mark_node)
9529         value = NULL_TREE;
9530       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9531         {
9532           error_at (loc, "enumerator value for %qE is not an integer constant",
9533                     name);
9534           value = NULL_TREE;
9535         }
9536       else
9537         {
9538           if (TREE_CODE (value) != INTEGER_CST)
9539             {
9540               value = c_fully_fold (value, false, NULL);
9541               if (TREE_CODE (value) == INTEGER_CST)
9542                 pedwarn (loc, OPT_Wpedantic,
9543                          "enumerator value for %qE is not an integer "
9544                          "constant expression", name);
9545             }
9546           if (TREE_CODE (value) != INTEGER_CST)
9547             {
9548               error ("enumerator value for %qE is not an integer constant",
9549                      name);
9550               value = NULL_TREE;
9551             }
9552           else
9553             {
9554               value = default_conversion (value);
9555               constant_expression_warning (value);
9556             }
9557         }
9558     }
9559
9560   /* Default based on previous value.  */
9561   /* It should no longer be possible to have NON_LVALUE_EXPR
9562      in the default.  */
9563   if (value == NULL_TREE)
9564     {
9565       value = the_enum->enum_next_value;
9566       if (the_enum->enum_overflow)
9567         error_at (loc, "overflow in enumeration values");
9568     }
9569   /* Even though the underlying type of an enum is unspecified, the
9570      type of enumeration constants is explicitly defined as int
9571      (6.4.4.3/2 in the C99 Standard).  GCC allows any integer type as
9572      an extension.  */
9573   else if (!int_fits_type_p (value, integer_type_node))
9574     pedwarn (loc, OPT_Wpedantic,
9575              "ISO C restricts enumerator values to range of %<int%>");
9576
9577   /* The ISO C Standard mandates enumerators to have type int, even
9578      though the underlying type of an enum type is unspecified.
9579      However, GCC allows enumerators of any integer type as an
9580      extensions.  Here we convert any enumerators that fit in an int
9581      to type int, to avoid promotions to unsigned types when comparing
9582      integers with enumerators that fit in the int range.  When
9583      -pedantic is given, we would have already warned about those that
9584      don't fit. We have to do this here rather than in finish_enum
9585      because this value may be used to define more enumerators.  */
9586   if (int_fits_type_p (value, integer_type_node))
9587     value = convert (integer_type_node, value);
9588
9589   /* Set basis for default for next value.  */
9590   the_enum->enum_next_value
9591     = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9592                        PLUS_EXPR, value, integer_one_node, false);
9593   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9594
9595   /* Now create a declaration for the enum value name.  */
9596
9597   type = TREE_TYPE (value);
9598   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9599                                       TYPE_PRECISION (integer_type_node)),
9600                                  (TYPE_PRECISION (type)
9601                                   >= TYPE_PRECISION (integer_type_node)
9602                                   && TYPE_UNSIGNED (type)));
9603
9604   decl = build_decl (decl_loc, CONST_DECL, name, type);
9605   DECL_INITIAL (decl) = convert (type, value);
9606   pushdecl (decl);
9607
9608   return tree_cons (decl, value, NULL_TREE);
9609 }
9610
9611 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL.  */
9612
9613 tree
9614 c_simulate_enum_decl (location_t loc, const char *name,
9615                       vec<string_int_pair> *values_ptr)
9616 {
9617   location_t saved_loc = input_location;
9618   input_location = loc;
9619
9620   struct c_enum_contents the_enum;
9621   tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9622
9623   tree value_chain = NULL_TREE;
9624   string_int_pair *value;
9625   vec<string_int_pair> values = *values_ptr;
9626   unsigned int i;
9627   FOR_EACH_VEC_ELT (values, i, value)
9628     {
9629       tree decl = build_enumerator (loc, loc, &the_enum,
9630                                     get_identifier (value->first),
9631                                     build_int_cst (integer_type_node,
9632                                                    value->second));
9633       TREE_CHAIN (decl) = value_chain;
9634       value_chain = decl;
9635     }
9636
9637   finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9638
9639   input_location = saved_loc;
9640   return enumtype;
9641 }
9642
9643 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL.  */
9644
9645 tree
9646 c_simulate_record_decl (location_t loc, const char *name,
9647                         array_slice<const tree> fields)
9648 {
9649   location_t saved_loc = input_location;
9650   input_location = loc;
9651
9652   class c_struct_parse_info *struct_info;
9653   tree ident = get_identifier (name);
9654   tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
9655
9656   for (unsigned int i = 0; i < fields.size (); ++i)
9657     {
9658       DECL_FIELD_CONTEXT (fields[i]) = type;
9659       if (i > 0)
9660         DECL_CHAIN (fields[i - 1]) = fields[i];
9661     }
9662
9663   finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
9664
9665   tree decl = build_decl (loc, TYPE_DECL, ident, type);
9666   set_underlying_type (decl);
9667   lang_hooks.decls.pushdecl (decl);
9668
9669   input_location = saved_loc;
9670   return type;
9671 }
9672 \f
9673 /* Create the FUNCTION_DECL for a function definition.
9674    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9675    the declaration; they describe the function's name and the type it returns,
9676    but twisted together in a fashion that parallels the syntax of C.
9677
9678    This function creates a binding context for the function body
9679    as well as setting up the FUNCTION_DECL in current_function_decl.
9680
9681    Returns true on success.  If the DECLARATOR is not suitable for a function
9682    (it defines a datum instead), we return false to report a parse error.  */
9683
9684 bool
9685 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9686                 tree attributes)
9687 {
9688   tree decl1, old_decl;
9689   tree restype, resdecl;
9690   location_t loc;
9691
9692   current_function_returns_value = 0;  /* Assume, until we see it does.  */
9693   current_function_returns_null = 0;
9694   current_function_returns_abnormally = 0;
9695   warn_about_return_type = 0;
9696   c_switch_stack = NULL;
9697
9698   /* Indicate no valid break/continue context.  */
9699   in_statement = 0;
9700
9701   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9702                           &attributes, NULL, NULL, DEPRECATED_NORMAL);
9703   invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9704
9705   /* If the declarator is not suitable for a function definition,
9706      cause a syntax error.  */
9707   if (decl1 == NULL_TREE
9708       || TREE_CODE (decl1) != FUNCTION_DECL)
9709     return false;
9710
9711   loc = DECL_SOURCE_LOCATION (decl1);
9712
9713   /* A nested function is not global.  */
9714   if (current_function_decl != NULL_TREE)
9715     TREE_PUBLIC (decl1) = 0;
9716
9717   c_decl_attributes (&decl1, attributes, 0);
9718
9719   if (DECL_DECLARED_INLINE_P (decl1)
9720       && DECL_UNINLINABLE (decl1)
9721       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9722     warning_at (loc, OPT_Wattributes,
9723                 "inline function %qD given attribute %qs",
9724                 decl1, "noinline");
9725
9726   /* Handle gnu_inline attribute.  */
9727   if (declspecs->inline_p
9728       && !flag_gnu89_inline
9729       && TREE_CODE (decl1) == FUNCTION_DECL
9730       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9731           || current_function_decl))
9732     {
9733       if (declspecs->storage_class != csc_static)
9734         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9735     }
9736
9737   announce_function (decl1);
9738
9739   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9740     {
9741       error_at (loc, "return type is an incomplete type");
9742       /* Make it return void instead.  */
9743       TREE_TYPE (decl1)
9744         = build_function_type (void_type_node,
9745                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9746     }
9747
9748   if (warn_about_return_type)
9749     warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9750                            : (warn_return_type > 0 ? OPT_Wreturn_type
9751                               : OPT_Wimplicit_int),
9752                       "return type defaults to %<int%>");
9753
9754   /* Make the init_value nonzero so pushdecl knows this is not tentative.
9755      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
9756   DECL_INITIAL (decl1) = error_mark_node;
9757
9758   /* If this definition isn't a prototype and we had a prototype declaration
9759      before, copy the arg type info from that prototype.  */
9760   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9761   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9762     old_decl = NULL_TREE;
9763
9764   current_function_prototype_locus = UNKNOWN_LOCATION;
9765   current_function_prototype_built_in = false;
9766   current_function_prototype_arg_types = NULL_TREE;
9767   tree newtype = TREE_TYPE (decl1);
9768   tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
9769   if (!prototype_p (newtype))
9770     {
9771       tree oldrt = TREE_TYPE (oldtype);
9772       tree newrt = TREE_TYPE (newtype);
9773       if (old_decl != NULL_TREE
9774           && TREE_CODE (oldtype) == FUNCTION_TYPE
9775           && comptypes (oldrt, newrt))
9776         {
9777           if (stdarg_p (oldtype))
9778             {
9779               auto_diagnostic_group d;
9780               warning_at (loc, 0, "%q+D defined as variadic function "
9781                           "without prototype", decl1);
9782               locate_old_decl (old_decl);
9783             }
9784           TREE_TYPE (decl1) = composite_type (oldtype, newtype);
9785           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9786           current_function_prototype_built_in
9787             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9788           current_function_prototype_arg_types
9789             = TYPE_ARG_TYPES (newtype);
9790         }
9791       if (TREE_PUBLIC (decl1))
9792         {
9793           /* If there is an external prototype declaration of this
9794              function, record its location but do not copy information
9795              to this decl.  This may be an invisible declaration
9796              (built-in or in a scope which has finished) or simply
9797              have more refined argument types than any declaration
9798              found above.  */
9799           struct c_binding *b;
9800           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9801             if (B_IN_SCOPE (b, external_scope))
9802               break;
9803           if (b)
9804             {
9805               tree ext_decl, ext_type;
9806               ext_decl = b->decl;
9807               ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9808               if (TREE_CODE (ext_type) == FUNCTION_TYPE
9809                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9810                                 TREE_TYPE (ext_type)))
9811                 {
9812                   current_function_prototype_locus
9813                     = DECL_SOURCE_LOCATION (ext_decl);
9814                   current_function_prototype_built_in
9815                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9816                   current_function_prototype_arg_types
9817                     = TYPE_ARG_TYPES (ext_type);
9818                 }
9819             }
9820         }
9821     }
9822
9823   /* Optionally warn of old-fashioned def with no previous prototype.  */
9824   if (warn_strict_prototypes
9825       && old_decl != error_mark_node
9826       && !prototype_p (TREE_TYPE (decl1))
9827       && C_DECL_ISNT_PROTOTYPE (old_decl))
9828     warning_at (loc, OPT_Wstrict_prototypes,
9829                 "function declaration isn%'t a prototype");
9830   /* Optionally warn of any global def with no previous prototype.  */
9831   else if (warn_missing_prototypes
9832            && old_decl != error_mark_node
9833            && TREE_PUBLIC (decl1)
9834            && !MAIN_NAME_P (DECL_NAME (decl1))
9835            && C_DECL_ISNT_PROTOTYPE (old_decl)
9836            && !DECL_DECLARED_INLINE_P (decl1))
9837     warning_at (loc, OPT_Wmissing_prototypes,
9838                 "no previous prototype for %qD", decl1);
9839   /* Optionally warn of any def with no previous prototype
9840      if the function has already been used.  */
9841   else if (warn_missing_prototypes
9842            && old_decl != NULL_TREE
9843            && old_decl != error_mark_node
9844            && TREE_USED (old_decl)
9845            && !prototype_p (TREE_TYPE (old_decl)))
9846     warning_at (loc, OPT_Wmissing_prototypes,
9847                 "%qD was used with no prototype before its definition", decl1);
9848   /* Optionally warn of any global def with no previous declaration.  */
9849   else if (warn_missing_declarations
9850            && TREE_PUBLIC (decl1)
9851            && old_decl == NULL_TREE
9852            && !MAIN_NAME_P (DECL_NAME (decl1))
9853            && !DECL_DECLARED_INLINE_P (decl1))
9854     warning_at (loc, OPT_Wmissing_declarations,
9855                 "no previous declaration for %qD",
9856                 decl1);
9857   /* Optionally warn of any def with no previous declaration
9858      if the function has already been used.  */
9859   else if (warn_missing_declarations
9860            && old_decl != NULL_TREE
9861            && old_decl != error_mark_node
9862            && TREE_USED (old_decl)
9863            && C_DECL_IMPLICIT (old_decl))
9864     warning_at (loc, OPT_Wmissing_declarations,
9865                 "%qD was used with no declaration before its definition", decl1);
9866
9867   /* This function exists in static storage.
9868      (This does not mean `static' in the C sense!)  */
9869   TREE_STATIC (decl1) = 1;
9870
9871   /* This is the earliest point at which we might know the assembler
9872      name of the function.  Thus, if it's set before this, die horribly.  */
9873   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9874
9875   /* If #pragma weak was used, mark the decl weak now.  */
9876   if (current_scope == file_scope)
9877     maybe_apply_pragma_weak (decl1);
9878
9879   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
9880   if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9881     {
9882       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9883           != integer_type_node)
9884         pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9885       else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9886         pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9887                  decl1);
9888
9889       check_main_parameter_types (decl1);
9890
9891       if (!TREE_PUBLIC (decl1))
9892         pedwarn (loc, OPT_Wmain,
9893                  "%qD is normally a non-static function", decl1);
9894     }
9895
9896   tree parms = current_function_arg_info->parms;
9897   if (old_decl)
9898     {
9899       location_t origloc = DECL_SOURCE_LOCATION (old_decl);
9900       warn_parm_array_mismatch (origloc, old_decl, parms);
9901     }
9902
9903   /* Record the decl so that the function name is defined.
9904      If we already have a decl for this name, and it is a FUNCTION_DECL,
9905      use the old decl.  */
9906
9907   current_function_decl = pushdecl (decl1);
9908
9909   if (tree access = build_attr_access_from_parms (parms, false))
9910     decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
9911                      old_decl);
9912
9913   push_scope ();
9914   declare_parm_level ();
9915
9916   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9917   resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9918   DECL_ARTIFICIAL (resdecl) = 1;
9919   DECL_IGNORED_P (resdecl) = 1;
9920   DECL_RESULT (current_function_decl) = resdecl;
9921
9922   start_fname_decls ();
9923
9924   return true;
9925 }
9926 \f
9927 /* Subroutine of store_parm_decls which handles new-style function
9928    definitions (prototype format). The parms already have decls, so we
9929    need only record them as in effect and complain if any redundant
9930    old-style parm decls were written.  */
9931 static void
9932 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9933 {
9934   tree decl;
9935   c_arg_tag *tag;
9936   unsigned ix;
9937
9938   if (current_scope->bindings)
9939     {
9940       error_at (DECL_SOURCE_LOCATION (fndecl),
9941                 "old-style parameter declarations in prototyped "
9942                 "function definition");
9943
9944       /* Get rid of the old-style declarations.  */
9945       pop_scope ();
9946       push_scope ();
9947     }
9948   /* Don't issue this warning for nested functions, and don't issue this
9949      warning if we got here because ARG_INFO_TYPES was error_mark_node
9950      (this happens when a function definition has just an ellipsis in
9951      its parameter list).  */
9952   else if (!in_system_header_at (input_location)
9953            && !current_function_scope
9954            && arg_info->types != error_mark_node)
9955     warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9956                 "traditional C rejects ISO C style function definitions");
9957
9958   /* Now make all the parameter declarations visible in the function body.
9959      We can bypass most of the grunt work of pushdecl.  */
9960   for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9961     {
9962       DECL_CONTEXT (decl) = current_function_decl;
9963       if (DECL_NAME (decl))
9964         {
9965           bind (DECL_NAME (decl), decl, current_scope,
9966                 /*invisible=*/false, /*nested=*/false,
9967                 UNKNOWN_LOCATION);
9968           if (!TREE_USED (decl))
9969             warn_if_shadowing (decl);
9970         }
9971       else
9972         pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9973                      "ISO C does not support omitting parameter names in "
9974                      "function definitions before C2X");
9975     }
9976
9977   /* Record the parameter list in the function declaration.  */
9978   DECL_ARGUMENTS (fndecl) = arg_info->parms;
9979
9980   /* Now make all the ancillary declarations visible, likewise.  */
9981   for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9982     {
9983       DECL_CONTEXT (decl) = current_function_decl;
9984       if (DECL_NAME (decl))
9985         bind (DECL_NAME (decl), decl, current_scope,
9986               /*invisible=*/false,
9987               /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9988               UNKNOWN_LOCATION);
9989     }
9990
9991   /* And all the tag declarations.  */
9992   FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9993     if (tag->id)
9994       bind (tag->id, tag->type, current_scope,
9995             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9996 }
9997
9998 /* Subroutine of store_parm_decls which handles old-style function
9999    definitions (separate parameter list and declarations).  */
10000
10001 static void
10002 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10003 {
10004   struct c_binding *b;
10005   tree parm, decl, last;
10006   tree parmids = arg_info->parms;
10007   hash_set<tree> seen_args;
10008
10009   if (!in_system_header_at (input_location))
10010     {
10011       if (flag_isoc2x)
10012         pedwarn (DECL_SOURCE_LOCATION (fndecl),
10013                  OPT_Wold_style_definition, "old-style function definition");
10014       else
10015         warning_at (DECL_SOURCE_LOCATION (fndecl),
10016                     OPT_Wold_style_definition,
10017                     "old-style function definition");
10018     }
10019
10020   if (current_scope->had_vla_unspec)
10021     error ("%<[*]%> not allowed in other than function prototype scope");
10022
10023   /* Match each formal parameter name with its declaration.  Save each
10024      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
10025   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10026     {
10027       if (TREE_VALUE (parm) == NULL_TREE)
10028         {
10029           error_at (DECL_SOURCE_LOCATION (fndecl),
10030                     "parameter name missing from parameter list");
10031           TREE_PURPOSE (parm) = NULL_TREE;
10032           continue;
10033         }
10034
10035       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10036       if (b && B_IN_CURRENT_SCOPE (b))
10037         {
10038           decl = b->decl;
10039           /* Skip erroneous parameters.  */
10040           if (decl == error_mark_node)
10041             continue;
10042           /* If we got something other than a PARM_DECL it is an error.  */
10043           if (TREE_CODE (decl) != PARM_DECL)
10044             {
10045               error_at (DECL_SOURCE_LOCATION (decl),
10046                         "%qD declared as a non-parameter", decl);
10047               continue;
10048             }
10049           /* If the declaration is already marked, we have a duplicate
10050              name.  Complain and ignore the duplicate.  */
10051           else if (seen_args.contains (decl))
10052             {
10053               error_at (DECL_SOURCE_LOCATION (decl),
10054                         "multiple parameters named %qD", decl);
10055               TREE_PURPOSE (parm) = NULL_TREE;
10056               continue;
10057             }
10058           /* If the declaration says "void", complain and turn it into
10059              an int.  */
10060           else if (VOID_TYPE_P (TREE_TYPE (decl)))
10061             {
10062               error_at (DECL_SOURCE_LOCATION (decl),
10063                         "parameter %qD declared with void type", decl);
10064               TREE_TYPE (decl) = integer_type_node;
10065               DECL_ARG_TYPE (decl) = integer_type_node;
10066               layout_decl (decl, 0);
10067             }
10068           warn_if_shadowing (decl);
10069         }
10070       /* If no declaration found, default to int.  */
10071       else
10072         {
10073           /* FIXME diagnostics: This should be the location of the argument,
10074              not the FNDECL.  E.g., for an old-style declaration
10075
10076                int f10(v) { blah; }
10077
10078              We should use the location of the V, not the F10.
10079              Unfortunately, the V is an IDENTIFIER_NODE which has no
10080              location.  In the future we need locations for c_arg_info
10081              entries.
10082
10083              See gcc.dg/Wshadow-3.c for an example of this problem. */
10084           decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10085                              PARM_DECL, TREE_VALUE (parm), integer_type_node);
10086           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10087           pushdecl (decl);
10088           warn_if_shadowing (decl);
10089
10090           if (flag_isoc99)
10091             pedwarn (DECL_SOURCE_LOCATION (decl),
10092                      OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10093                      decl);
10094           else
10095             warning_at (DECL_SOURCE_LOCATION (decl),
10096                         OPT_Wmissing_parameter_type,
10097                         "type of %qD defaults to %<int%>", decl);
10098         }
10099
10100       TREE_PURPOSE (parm) = decl;
10101       seen_args.add (decl);
10102     }
10103
10104   /* Now examine the parms chain for incomplete declarations
10105      and declarations with no corresponding names.  */
10106
10107   for (b = current_scope->bindings; b; b = b->prev)
10108     {
10109       parm = b->decl;
10110       if (TREE_CODE (parm) != PARM_DECL)
10111         continue;
10112
10113       if (TREE_TYPE (parm) != error_mark_node
10114           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10115         {
10116           error_at (DECL_SOURCE_LOCATION (parm),
10117                     "parameter %qD has incomplete type", parm);
10118           TREE_TYPE (parm) = error_mark_node;
10119         }
10120
10121       if (!seen_args.contains (parm))
10122         {
10123           error_at (DECL_SOURCE_LOCATION (parm),
10124                     "declaration for parameter %qD but no such parameter",
10125                     parm);
10126
10127           /* Pretend the parameter was not missing.
10128              This gets us to a standard state and minimizes
10129              further error messages.  */
10130           parmids = chainon (parmids, tree_cons (parm, 0, 0));
10131         }
10132     }
10133
10134   /* Chain the declarations together in the order of the list of
10135      names.  Store that chain in the function decl, replacing the
10136      list of names.  Update the current scope to match.  */
10137   DECL_ARGUMENTS (fndecl) = NULL_TREE;
10138
10139   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10140     if (TREE_PURPOSE (parm))
10141       break;
10142   if (parm && TREE_PURPOSE (parm))
10143     {
10144       last = TREE_PURPOSE (parm);
10145       DECL_ARGUMENTS (fndecl) = last;
10146
10147       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10148         if (TREE_PURPOSE (parm))
10149           {
10150             DECL_CHAIN (last) = TREE_PURPOSE (parm);
10151             last = TREE_PURPOSE (parm);
10152           }
10153       DECL_CHAIN (last) = NULL_TREE;
10154     }
10155
10156   /* If there was a previous prototype,
10157      set the DECL_ARG_TYPE of each argument according to
10158      the type previously specified, and report any mismatches.  */
10159
10160   if (current_function_prototype_arg_types)
10161     {
10162       tree type;
10163       for (parm = DECL_ARGUMENTS (fndecl),
10164              type = current_function_prototype_arg_types;
10165            parm || (type != NULL_TREE
10166                     && TREE_VALUE (type) != error_mark_node
10167                     && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10168            parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10169         {
10170           if (parm == NULL_TREE
10171               || type == NULL_TREE
10172               || (TREE_VALUE (type) != error_mark_node
10173                   && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10174             {
10175               if (current_function_prototype_built_in)
10176                 warning_at (DECL_SOURCE_LOCATION (fndecl),
10177                             0, "number of arguments doesn%'t match "
10178                             "built-in prototype");
10179               else
10180                 {
10181                   /* FIXME diagnostics: This should be the location of
10182                      FNDECL, but there is bug when a prototype is
10183                      declared inside function context, but defined
10184                      outside of it (e.g., gcc.dg/pr15698-2.c).  In
10185                      which case FNDECL gets the location of the
10186                      prototype, not the definition.  */
10187                   error_at (input_location,
10188                             "number of arguments doesn%'t match prototype");
10189
10190                   error_at (current_function_prototype_locus,
10191                             "prototype declaration");
10192                 }
10193               break;
10194             }
10195           /* Type for passing arg must be consistent with that
10196              declared for the arg.  ISO C says we take the unqualified
10197              type for parameters declared with qualified type.  */
10198           if (TREE_TYPE (parm) != error_mark_node
10199               && TREE_VALUE (type) != error_mark_node
10200               && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10201                    != TYPE_ATOMIC (TREE_VALUE (type)))
10202                   || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10203                                  TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10204             {
10205               if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10206                    == TYPE_ATOMIC (TREE_VALUE (type)))
10207                   && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10208                       == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10209                 {
10210                   /* Adjust argument to match prototype.  E.g. a previous
10211                      `int foo(float);' prototype causes
10212                      `int foo(x) float x; {...}' to be treated like
10213                      `int foo(float x) {...}'.  This is particularly
10214                      useful for argument types like uid_t.  */
10215                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10216
10217                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10218                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10219                       && (TYPE_PRECISION (TREE_TYPE (parm))
10220                           < TYPE_PRECISION (integer_type_node)))
10221                     DECL_ARG_TYPE (parm)
10222                       = c_type_promotes_to (TREE_TYPE (parm));
10223
10224                   /* ??? Is it possible to get here with a
10225                      built-in prototype or will it always have
10226                      been diagnosed as conflicting with an
10227                      old-style definition and discarded?  */
10228                   if (current_function_prototype_built_in)
10229                     warning_at (DECL_SOURCE_LOCATION (parm),
10230                                 OPT_Wpedantic, "promoted argument %qD "
10231                                 "doesn%'t match built-in prototype", parm);
10232                   else
10233                     {
10234                       pedwarn (DECL_SOURCE_LOCATION (parm),
10235                                OPT_Wpedantic, "promoted argument %qD "
10236                                "doesn%'t match prototype", parm);
10237                       pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10238                                "prototype declaration");
10239                     }
10240                 }
10241               else
10242                 {
10243                   if (current_function_prototype_built_in)
10244                     warning_at (DECL_SOURCE_LOCATION (parm),
10245                                 0, "argument %qD doesn%'t match "
10246                                 "built-in prototype", parm);
10247                   else
10248                     {
10249                       error_at (DECL_SOURCE_LOCATION (parm),
10250                                 "argument %qD doesn%'t match prototype", parm);
10251                       error_at (current_function_prototype_locus,
10252                                 "prototype declaration");
10253                     }
10254                 }
10255             }
10256         }
10257       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10258     }
10259
10260   /* Otherwise, create a prototype that would match.  */
10261
10262   else
10263     {
10264       tree actual = NULL_TREE, last = NULL_TREE, type;
10265
10266       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10267         {
10268           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10269           if (last)
10270             TREE_CHAIN (last) = type;
10271           else
10272             actual = type;
10273           last = type;
10274         }
10275       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10276       if (last)
10277         TREE_CHAIN (last) = type;
10278       else
10279         actual = type;
10280
10281       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10282          of the type of this function, but we need to avoid having this
10283          affect the types of other similarly-typed functions, so we must
10284          first force the generation of an identical (but separate) type
10285          node for the relevant function type.  The new node we create
10286          will be a variant of the main variant of the original function
10287          type.  */
10288
10289       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10290
10291       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10292     }
10293 }
10294
10295 /* Store parameter declarations passed in ARG_INFO into the current
10296    function declaration.  */
10297
10298 void
10299 store_parm_decls_from (struct c_arg_info *arg_info)
10300 {
10301   current_function_arg_info = arg_info;
10302   store_parm_decls ();
10303 }
10304
10305 /* Called by walk_tree to look for and update context-less labels
10306    or labels with context in the parent function.  */
10307
10308 static tree
10309 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10310 {
10311   tree ctx = static_cast<tree>(data);
10312   if (TREE_CODE (*tp) == LABEL_EXPR
10313       && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10314           || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10315     {
10316       DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10317       *walk_subtrees = 0;
10318     }
10319
10320   return NULL_TREE;
10321 }
10322
10323 /* Store the parameter declarations into the current function declaration.
10324    This is called after parsing the parameter declarations, before
10325    digesting the body of the function.
10326
10327    For an old-style definition, construct a prototype out of the old-style
10328    parameter declarations and inject it into the function's type.  */
10329
10330 void
10331 store_parm_decls (void)
10332 {
10333   tree fndecl = current_function_decl;
10334   bool proto;
10335
10336   /* The argument information block for FNDECL.  */
10337   struct c_arg_info *arg_info = current_function_arg_info;
10338   current_function_arg_info = 0;
10339
10340   /* True if this definition is written with a prototype.  In C2X, an
10341      empty argument list was converted to (void) in grokparms; in
10342      older C standard versions, it does not give the function a type
10343      with a prototype for future calls.  */
10344   proto = arg_info->types != 0;
10345
10346   if (proto)
10347     store_parm_decls_newstyle (fndecl, arg_info);
10348   else
10349     store_parm_decls_oldstyle (fndecl, arg_info);
10350
10351   /* The next call to push_scope will be a function body.  */
10352
10353   next_is_function_body = true;
10354
10355   /* Write a record describing this function definition to the prototypes
10356      file (if requested).  */
10357
10358   gen_aux_info_record (fndecl, 1, 0, proto);
10359
10360   /* Initialize the RTL code for the function.  */
10361   allocate_struct_function (fndecl, false);
10362
10363   if (warn_unused_local_typedefs)
10364     cfun->language = ggc_cleared_alloc<language_function> ();
10365
10366   /* Begin the statement tree for this function.  */
10367   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10368
10369   /* ??? Insert the contents of the pending sizes list into the function
10370      to be evaluated.  The only reason left to have this is
10371         void foo(int n, int array[n++])
10372      because we throw away the array type in favor of a pointer type, and
10373      thus won't naturally see the SAVE_EXPR containing the increment.  All
10374      other pending sizes would be handled by gimplify_parameters.  */
10375   if (arg_info->pending_sizes)
10376     {
10377       /* In very special circumstances, e.g. for code like
10378            _Atomic int i = 5;
10379            void f (int a[i += 2]) {}
10380          we need to execute the atomic assignment on function entry.
10381          But in this case, it is not just a straight store, it has the
10382          op= form, which means that build_atomic_assign has generated
10383          gotos, labels, etc.  Because at that time the function decl
10384          for F has not been created yet, those labels do not have any
10385          function context.  But we have the fndecl now, so update the
10386          labels accordingly.  gimplify_expr would crash otherwise.
10387          Or with nested functions the labels could be created with parent
10388          function's context, while when the statement is emitted at the
10389          start of the nested function, it needs the nested function's
10390          context.  */
10391       walk_tree_without_duplicates (&arg_info->pending_sizes,
10392                                     set_labels_context_r, fndecl);
10393       add_stmt (arg_info->pending_sizes);
10394     }
10395 }
10396
10397 /* Store PARM_DECLs in PARMS into scope temporarily.  Used for
10398    c_finish_omp_declare_simd for function prototypes.  No diagnostics
10399    should be done.  */
10400
10401 void
10402 temp_store_parm_decls (tree fndecl, tree parms)
10403 {
10404   push_scope ();
10405   for (tree p = parms; p; p = DECL_CHAIN (p))
10406     {
10407       DECL_CONTEXT (p) = fndecl;
10408       if (DECL_NAME (p))
10409         bind (DECL_NAME (p), p, current_scope,
10410               /*invisible=*/false, /*nested=*/false,
10411               UNKNOWN_LOCATION);
10412     }
10413 }
10414
10415 /* Undo what temp_store_parm_decls did.  */
10416
10417 void
10418 temp_pop_parm_decls (void)
10419 {
10420   /* Clear all bindings in this temporary scope, so that
10421      pop_scope doesn't create a BLOCK.  */
10422   struct c_binding *b = current_scope->bindings;
10423   current_scope->bindings = NULL;
10424   for (; b; b = free_binding_and_advance (b))
10425     {
10426       gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10427                   || b->decl == error_mark_node);
10428       gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10429       I_SYMBOL_BINDING (b->id) = b->shadowed;
10430       if (b->shadowed && b->shadowed->u.type)
10431         TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10432     }
10433   pop_scope ();
10434 }
10435 \f
10436
10437 /* Finish up a function declaration and compile that function
10438    all the way to assembler language output.  Then free the storage
10439    for the function definition.
10440
10441    This is called after parsing the body of the function definition.  */
10442
10443 void
10444 finish_function (location_t end_loc)
10445 {
10446   tree fndecl = current_function_decl;
10447   
10448   if (c_dialect_objc ())
10449     objc_finish_function ();
10450
10451   if (TREE_CODE (fndecl) == FUNCTION_DECL
10452       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10453     {
10454       tree args = DECL_ARGUMENTS (fndecl);
10455       for (; args; args = DECL_CHAIN (args))
10456         {
10457           tree type = TREE_TYPE (args);
10458           if (INTEGRAL_TYPE_P (type)
10459               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10460             DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10461         }
10462     }
10463
10464   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10465     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10466
10467   /* Must mark the RESULT_DECL as being in this function.  */
10468
10469   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10470     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10471
10472   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
10473       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10474       == integer_type_node && flag_isoc99)
10475     {
10476       /* Hack.  We don't want the middle-end to warn that this return
10477          is unreachable, so we mark its location as special.  Using
10478          UNKNOWN_LOCATION has the problem that it gets clobbered in
10479          annotate_one_with_locus.  A cleaner solution might be to
10480          ensure ! should_carry_locus_p (stmt), but that needs a flag.
10481       */
10482       c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10483     }
10484
10485   /* Tie off the statement tree for this function.  */
10486   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10487
10488   finish_fname_decls ();
10489
10490   /* Complain if there's no return statement only if option specified on
10491      command line.  */
10492   if (warn_return_type > 0
10493       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10494       && !current_function_returns_value && !current_function_returns_null
10495       /* Don't complain if we are no-return.  */
10496       && !current_function_returns_abnormally
10497       /* Don't complain if we are declared noreturn.  */
10498       && !TREE_THIS_VOLATILE (fndecl)
10499       /* Don't warn for main().  */
10500       && !MAIN_NAME_P (DECL_NAME (fndecl))
10501       /* Or if they didn't actually specify a return type.  */
10502       && !C_FUNCTION_IMPLICIT_INT (fndecl)
10503       /* Normally, with -Wreturn-type, flow will complain, but we might
10504          optimize out static functions.  */
10505       && !TREE_PUBLIC (fndecl)
10506       && targetm.warn_func_return (fndecl)
10507       && warning (OPT_Wreturn_type,
10508                   "no return statement in function returning non-void"))
10509     suppress_warning (fndecl, OPT_Wreturn_type);
10510
10511   /* Complain about parameters that are only set, but never otherwise used.  */
10512   if (warn_unused_but_set_parameter)
10513     {
10514       tree decl;
10515
10516       for (decl = DECL_ARGUMENTS (fndecl);
10517            decl;
10518            decl = DECL_CHAIN (decl))
10519         if (TREE_USED (decl)
10520             && TREE_CODE (decl) == PARM_DECL
10521             && !DECL_READ_P (decl)
10522             && DECL_NAME (decl)
10523             && !DECL_ARTIFICIAL (decl)
10524             && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10525           warning_at (DECL_SOURCE_LOCATION (decl),
10526                       OPT_Wunused_but_set_parameter,
10527                       "parameter %qD set but not used", decl);
10528     }
10529
10530   /* Complain about locally defined typedefs that are not used in this
10531      function.  */
10532   maybe_warn_unused_local_typedefs ();
10533
10534   /* Possibly warn about unused parameters.  */
10535   if (warn_unused_parameter)
10536     do_warn_unused_parameter (fndecl);
10537
10538   /* Store the end of the function, so that we get good line number
10539      info for the epilogue.  */
10540   cfun->function_end_locus = end_loc;
10541
10542   /* Finalize the ELF visibility for the function.  */
10543   c_determine_visibility (fndecl);
10544
10545   /* For GNU C extern inline functions disregard inline limits.  */
10546   if (DECL_EXTERNAL (fndecl)
10547       && DECL_DECLARED_INLINE_P (fndecl)
10548       && (flag_gnu89_inline
10549           || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10550     DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10551
10552   /* Genericize before inlining.  Delay genericizing nested functions
10553      until their parent function is genericized.  Since finalizing
10554      requires GENERIC, delay that as well.  */
10555
10556   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10557       && !undef_nested_function)
10558     {
10559       if (!decl_function_context (fndecl))
10560         {
10561           invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10562           c_genericize (fndecl);
10563
10564           /* ??? Objc emits functions after finalizing the compilation unit.
10565              This should be cleaned up later and this conditional removed.  */
10566           if (symtab->global_info_ready)
10567             {
10568               cgraph_node::add_new_function (fndecl, false);
10569               return;
10570             }
10571           cgraph_node::finalize_function (fndecl, false);
10572         }
10573       else
10574         {
10575           /* Register this function with cgraph just far enough to get it
10576             added to our parent's nested function list.  Handy, since the
10577             C front end doesn't have such a list.  */
10578           (void) cgraph_node::get_create (fndecl);
10579         }
10580     }
10581
10582   if (!decl_function_context (fndecl))
10583     undef_nested_function = false;
10584
10585   if (cfun->language != NULL)
10586     {
10587       ggc_free (cfun->language);
10588       cfun->language = NULL;
10589     }
10590
10591   /* We're leaving the context of this function, so zap cfun.
10592      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10593      tree_rest_of_compilation.  */
10594   set_cfun (NULL);
10595   invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10596   current_function_decl = NULL;
10597 }
10598 \f
10599 /* Check the declarations given in a for-loop for satisfying the C99
10600    constraints.  If exactly one such decl is found, return it.  LOC is
10601    the location of the opening parenthesis of the for loop.  The last
10602    parameter allows you to control the "for loop initial declarations
10603    are only allowed in C99 mode".  Normally, you should pass
10604    flag_isoc99 as that parameter.  But in some cases (Objective-C
10605    foreach loop, for example) we want to run the checks in this
10606    function even if not in C99 mode, so we allow the caller to turn
10607    off the error about not being in C99 mode.
10608 */
10609
10610 tree
10611 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10612 {
10613   struct c_binding *b;
10614   tree one_decl = NULL_TREE;
10615   int n_decls = 0;
10616
10617   if (!turn_off_iso_c99_error)
10618     {
10619       static bool hint = true;
10620       /* If we get here, declarations have been used in a for loop without
10621          the C99 for loop scope.  This doesn't make much sense, so don't
10622          allow it.  */
10623       auto_diagnostic_group d;
10624       error_at (loc, "%<for%> loop initial declarations "
10625                 "are only allowed in C99 or C11 mode");
10626       if (hint)
10627         {
10628           inform (loc,
10629                   "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10630                   "%<-std=gnu11%> to compile your code");
10631           hint = false;
10632         }
10633       return NULL_TREE;
10634     }
10635   else
10636     pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10637                  "initial declarations");
10638
10639   /* C99 subclause 6.8.5 paragraph 3:
10640
10641        [#3]  The  declaration  part  of  a for statement shall only
10642        declare identifiers for objects having storage class auto or
10643        register.
10644
10645      It isn't clear whether, in this sentence, "identifiers" binds to
10646      "shall only declare" or to "objects" - that is, whether all identifiers
10647      declared must be identifiers for objects, or whether the restriction
10648      only applies to those that are.  (A question on this in comp.std.c
10649      in November 2000 received no answer.)  We implement the strictest
10650      interpretation, to avoid creating an extension which later causes
10651      problems.  */
10652
10653   for (b = current_scope->bindings; b; b = b->prev)
10654     {
10655       tree id = b->id;
10656       tree decl = b->decl;
10657
10658       if (!id)
10659         continue;
10660
10661       switch (TREE_CODE (decl))
10662         {
10663         case VAR_DECL:
10664           {
10665             location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10666             if (TREE_STATIC (decl))
10667               error_at (decl_loc,
10668                         "declaration of static variable %qD in %<for%> loop "
10669                         "initial declaration", decl);
10670             else if (DECL_EXTERNAL (decl))
10671               error_at (decl_loc,
10672                         "declaration of %<extern%> variable %qD in %<for%> loop "
10673                         "initial declaration", decl);
10674           }
10675           break;
10676
10677         case RECORD_TYPE:
10678           error_at (loc,
10679                     "%<struct %E%> declared in %<for%> loop initial "
10680                     "declaration", id);
10681           break;
10682         case UNION_TYPE:
10683           error_at (loc,
10684                     "%<union %E%> declared in %<for%> loop initial declaration",
10685                     id);
10686           break;
10687         case ENUMERAL_TYPE:
10688           error_at (loc, "%<enum %E%> declared in %<for%> loop "
10689                     "initial declaration", id);
10690           break;
10691         default:
10692           error_at (loc, "declaration of non-variable "
10693                     "%qD in %<for%> loop initial declaration", decl);
10694         }
10695
10696       n_decls++;
10697       one_decl = decl;
10698     }
10699
10700   return n_decls == 1 ? one_decl : NULL_TREE;
10701 }
10702 \f
10703 /* Save and reinitialize the variables
10704    used during compilation of a C function.  */
10705
10706 void
10707 c_push_function_context (void)
10708 {
10709   struct language_function *p = cfun->language;
10710   /* cfun->language might have been already allocated by the use of
10711      -Wunused-local-typedefs.  In that case, just re-use it.  */
10712   if (p == NULL)
10713     cfun->language = p = ggc_cleared_alloc<language_function> ();
10714
10715   p->base.x_stmt_tree = c_stmt_tree;
10716   c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10717   p->x_in_statement = in_statement;
10718   p->x_switch_stack = c_switch_stack;
10719   p->arg_info = current_function_arg_info;
10720   p->returns_value = current_function_returns_value;
10721   p->returns_null = current_function_returns_null;
10722   p->returns_abnormally = current_function_returns_abnormally;
10723   p->warn_about_return_type = warn_about_return_type;
10724
10725   push_function_context ();
10726 }
10727
10728 /* Restore the variables used during compilation of a C function.  */
10729
10730 void
10731 c_pop_function_context (void)
10732 {
10733   struct language_function *p;
10734
10735   pop_function_context ();
10736   p = cfun->language;
10737
10738   /* When -Wunused-local-typedefs is in effect, cfun->languages is
10739      used to store data throughout the life time of the current cfun,
10740      So don't deallocate it.  */
10741   if (!warn_unused_local_typedefs)
10742     cfun->language = NULL;
10743
10744   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10745       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10746     {
10747       /* Stop pointing to the local nodes about to be freed.  */
10748       /* But DECL_INITIAL must remain nonzero so we know this
10749          was an actual function definition.  */
10750       DECL_INITIAL (current_function_decl) = error_mark_node;
10751       DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10752     }
10753
10754   c_stmt_tree = p->base.x_stmt_tree;
10755   p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10756   in_statement = p->x_in_statement;
10757   c_switch_stack = p->x_switch_stack;
10758   current_function_arg_info = p->arg_info;
10759   current_function_returns_value = p->returns_value;
10760   current_function_returns_null = p->returns_null;
10761   current_function_returns_abnormally = p->returns_abnormally;
10762   warn_about_return_type = p->warn_about_return_type;
10763 }
10764
10765 /* The functions below are required for functionality of doing
10766    function at once processing in the C front end. Currently these
10767    functions are not called from anywhere in the C front end, but as
10768    these changes continue, that will change.  */
10769
10770 /* Returns the stmt_tree (if any) to which statements are currently
10771    being added.  If there is no active statement-tree, NULL is
10772    returned.  */
10773
10774 stmt_tree
10775 current_stmt_tree (void)
10776 {
10777   return &c_stmt_tree;
10778 }
10779
10780 /* Return the global value of T as a symbol.  */
10781
10782 tree
10783 identifier_global_value (tree t)
10784 {
10785   struct c_binding *b;
10786
10787   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10788     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10789       return b->decl;
10790
10791   return NULL_TREE;
10792 }
10793
10794 /* Return the global value of tag T as a symbol.  */
10795
10796 tree
10797 identifier_global_tag (tree t)
10798 {
10799   struct c_binding *b;
10800
10801   for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10802     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10803       return b->decl;
10804
10805   return NULL_TREE;
10806 }
10807
10808 /* Returns true if NAME refers to a built-in function or function-like
10809    operator.  */
10810
10811 bool
10812 names_builtin_p (const char *name)
10813 {
10814   tree id = get_identifier (name);
10815   if (tree decl = identifier_global_value (id))
10816     return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
10817
10818   /* Also detect common reserved C words that aren't strictly built-in
10819      functions.  */
10820   switch (C_RID_CODE (id))
10821     {
10822     case RID_BUILTIN_CONVERTVECTOR:
10823     case RID_BUILTIN_HAS_ATTRIBUTE:
10824     case RID_BUILTIN_SHUFFLE:
10825     case RID_BUILTIN_SHUFFLEVECTOR:
10826     case RID_BUILTIN_ASSOC_BARRIER:
10827     case RID_CHOOSE_EXPR:
10828     case RID_OFFSETOF:
10829     case RID_TYPES_COMPATIBLE_P:
10830       return true;
10831     default:
10832       break;
10833     }
10834
10835   return false;
10836 }
10837
10838 /* In C, the only C-linkage public declaration is at file scope.  */
10839
10840 tree
10841 c_linkage_bindings (tree name)
10842 {
10843   return identifier_global_value (name);
10844 }
10845
10846 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
10847    otherwise the name is found in ridpointers from RID_INDEX.  */
10848
10849 void
10850 record_builtin_type (enum rid rid_index, const char *name, tree type)
10851 {
10852   tree id, decl;
10853   if (name == 0)
10854     id = ridpointers[(int) rid_index];
10855   else
10856     id = get_identifier (name);
10857   decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10858   pushdecl (decl);
10859   if (debug_hooks->type_decl)
10860     debug_hooks->type_decl (decl, false);
10861 }
10862
10863 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
10864
10865 struct c_parm *
10866 build_c_parm (struct c_declspecs *specs, tree attrs,
10867               struct c_declarator *declarator,
10868               location_t loc)
10869 {
10870   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10871   ret->specs = specs;
10872   ret->attrs = attrs;
10873   ret->declarator = declarator;
10874   ret->loc = loc;
10875   return ret;
10876 }
10877
10878 /* Return a declarator with nested attributes.  TARGET is the inner
10879    declarator to which these attributes apply.  ATTRS are the
10880    attributes.  */
10881
10882 struct c_declarator *
10883 build_attrs_declarator (tree attrs, struct c_declarator *target)
10884 {
10885   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10886   ret->kind = cdk_attrs;
10887   ret->declarator = target;
10888   ret->u.attrs = attrs;
10889   return ret;
10890 }
10891
10892 /* Return a declarator for a function with arguments specified by ARGS
10893    and return type specified by TARGET.  */
10894
10895 struct c_declarator *
10896 build_function_declarator (struct c_arg_info *args,
10897                            struct c_declarator *target)
10898 {
10899   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10900   ret->kind = cdk_function;
10901   ret->declarator = target;
10902   ret->u.arg_info = args;
10903   return ret;
10904 }
10905
10906 /* Return a declarator for the identifier IDENT (which may be
10907    NULL_TREE for an abstract declarator).  */
10908
10909 struct c_declarator *
10910 build_id_declarator (tree ident)
10911 {
10912   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10913   ret->kind = cdk_id;
10914   ret->declarator = 0;
10915   ret->u.id.id = ident;
10916   ret->u.id.attrs = NULL_TREE;
10917   /* Default value - may get reset to a more precise location. */
10918   ret->id_loc = input_location;
10919   return ret;
10920 }
10921
10922 /* Return something to represent absolute declarators containing a *.
10923    TARGET is the absolute declarator that the * contains.
10924    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10925    to apply to the pointer type.  */
10926
10927 struct c_declarator *
10928 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10929                          struct c_declarator *target)
10930 {
10931   tree attrs;
10932   int quals = 0;
10933   struct c_declarator *itarget = target;
10934   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10935   if (type_quals_attrs)
10936     {
10937       attrs = type_quals_attrs->attrs;
10938       quals = quals_from_declspecs (type_quals_attrs);
10939       if (attrs != NULL_TREE)
10940         itarget = build_attrs_declarator (attrs, target);
10941     }
10942   ret->kind = cdk_pointer;
10943   ret->declarator = itarget;
10944   ret->u.pointer_quals = quals;
10945   return ret;
10946 }
10947
10948 /* Return a pointer to a structure for an empty list of declaration
10949    specifiers.  */
10950
10951 struct c_declspecs *
10952 build_null_declspecs (void)
10953 {
10954   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10955   memset (ret, 0, sizeof *ret);
10956   ret->align_log = -1;
10957   ret->typespec_word = cts_none;
10958   ret->storage_class = csc_none;
10959   ret->expr_const_operands = true;
10960   ret->typespec_kind = ctsk_none;
10961   ret->address_space = ADDR_SPACE_GENERIC;
10962   return ret;
10963 }
10964
10965 /* Add the address space ADDRSPACE to the declaration specifiers
10966    SPECS, returning SPECS.  */
10967
10968 struct c_declspecs *
10969 declspecs_add_addrspace (location_t location,
10970                          struct c_declspecs *specs, addr_space_t as)
10971 {
10972   specs->non_sc_seen_p = true;
10973   specs->declspecs_seen_p = true;
10974   specs->non_std_attrs_seen_p = true;
10975
10976   if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10977       && specs->address_space != as)
10978     error ("incompatible address space qualifiers %qs and %qs",
10979            c_addr_space_name (as),
10980            c_addr_space_name (specs->address_space));
10981   else
10982     {
10983       specs->address_space = as;
10984       specs->locations[cdw_address_space] = location;
10985     }
10986   return specs;
10987 }
10988
10989 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10990    returning SPECS.  */
10991
10992 struct c_declspecs *
10993 declspecs_add_qual (location_t loc,
10994                     struct c_declspecs *specs, tree qual)
10995 {
10996   enum rid i;
10997   bool dupe = false;
10998   specs->non_sc_seen_p = true;
10999   specs->declspecs_seen_p = true;
11000   specs->non_std_attrs_seen_p = true;
11001   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11002               && C_IS_RESERVED_WORD (qual));
11003   i = C_RID_CODE (qual);
11004   location_t prev_loc = UNKNOWN_LOCATION;
11005   switch (i)
11006     {
11007     case RID_CONST:
11008       dupe = specs->const_p;
11009       specs->const_p = true;
11010       prev_loc = specs->locations[cdw_const];
11011       specs->locations[cdw_const] = loc;
11012       break;
11013     case RID_VOLATILE:
11014       dupe = specs->volatile_p;
11015       specs->volatile_p = true;
11016       prev_loc = specs->locations[cdw_volatile];
11017       specs->locations[cdw_volatile] = loc;
11018       break;
11019     case RID_RESTRICT:
11020       dupe = specs->restrict_p;
11021       specs->restrict_p = true;
11022       prev_loc = specs->locations[cdw_restrict];
11023       specs->locations[cdw_restrict] = loc;
11024       break;
11025     case RID_ATOMIC:
11026       dupe = specs->atomic_p;
11027       specs->atomic_p = true;
11028       prev_loc = specs->locations[cdw_atomic];
11029       specs->locations[cdw_atomic] = loc;
11030       break;
11031     default:
11032       gcc_unreachable ();
11033     }
11034   if (dupe)
11035     {
11036       bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11037                                  "duplicate %qE declaration specifier", qual);
11038       if (!warned
11039           && warn_duplicate_decl_specifier
11040           && prev_loc >= RESERVED_LOCATION_COUNT
11041           && !from_macro_expansion_at (prev_loc)
11042           && !from_macro_expansion_at (loc))
11043         warning_at (loc, OPT_Wduplicate_decl_specifier,
11044                     "duplicate %qE declaration specifier", qual);
11045     }
11046   return specs;
11047 }
11048
11049 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11050    returning SPECS.  */
11051
11052 struct c_declspecs *
11053 declspecs_add_type (location_t loc, struct c_declspecs *specs,
11054                     struct c_typespec spec)
11055 {
11056   tree type = spec.spec;
11057   specs->non_sc_seen_p = true;
11058   specs->declspecs_seen_p = true;
11059   specs->non_std_attrs_seen_p = true;
11060   specs->typespec_kind = spec.kind;
11061   if (TREE_DEPRECATED (type))
11062     specs->deprecated_p = true;
11063   if (TREE_UNAVAILABLE (type))
11064     specs->unavailable_p = true;
11065
11066   /* Handle type specifier keywords.  */
11067   if (TREE_CODE (type) == IDENTIFIER_NODE
11068       && C_IS_RESERVED_WORD (type)
11069       && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11070     {
11071       enum rid i = C_RID_CODE (type);
11072       if (specs->type)
11073         {
11074           error_at (loc, "two or more data types in declaration specifiers");
11075           return specs;
11076         }
11077       if ((int) i <= (int) RID_LAST_MODIFIER)
11078         {
11079           /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat".  */
11080           bool dupe = false;
11081           switch (i)
11082             {
11083             case RID_LONG:
11084               if (specs->long_long_p)
11085                 {
11086                   error_at (loc, "%<long long long%> is too long for GCC");
11087                   break;
11088                 }
11089               if (specs->long_p)
11090                 {
11091                   if (specs->typespec_word == cts_double)
11092                     {
11093                       error_at (loc,
11094                                 ("both %<long long%> and %<double%> in "
11095                                  "declaration specifiers"));
11096                       break;
11097                     }
11098                   pedwarn_c90 (loc, OPT_Wlong_long,
11099                                "ISO C90 does not support %<long long%>");
11100                   specs->long_long_p = 1;
11101                   specs->locations[cdw_long_long] = loc;
11102                   break;
11103                 }
11104               if (specs->short_p)
11105                 error_at (loc,
11106                           ("both %<long%> and %<short%> in "
11107                            "declaration specifiers"));
11108               else if (specs->typespec_word == cts_auto_type)
11109                 error_at (loc,
11110                           ("both %<long%> and %<__auto_type%> in "
11111                            "declaration specifiers"));
11112               else if (specs->typespec_word == cts_void)
11113                 error_at (loc,
11114                           ("both %<long%> and %<void%> in "
11115                            "declaration specifiers"));
11116               else if (specs->typespec_word == cts_int_n)
11117                   error_at (loc,
11118                             ("both %<long%> and %<__int%d%> in "
11119                              "declaration specifiers"),
11120                             int_n_data[specs->int_n_idx].bitsize);
11121               else if (specs->typespec_word == cts_bool)
11122                 error_at (loc,
11123                           ("both %<long%> and %<_Bool%> in "
11124                            "declaration specifiers"));
11125               else if (specs->typespec_word == cts_char)
11126                 error_at (loc,
11127                           ("both %<long%> and %<char%> in "
11128                            "declaration specifiers"));
11129               else if (specs->typespec_word == cts_float)
11130                 error_at (loc,
11131                           ("both %<long%> and %<float%> in "
11132                            "declaration specifiers"));
11133               else if (specs->typespec_word == cts_floatn_nx)
11134                 error_at (loc,
11135                           ("both %<long%> and %<_Float%d%s%> in "
11136                            "declaration specifiers"),
11137                           floatn_nx_types[specs->floatn_nx_idx].n,
11138                           (floatn_nx_types[specs->floatn_nx_idx].extended
11139                            ? "x"
11140                            : ""));
11141               else if (specs->typespec_word == cts_dfloat32)
11142                 error_at (loc,
11143                           ("both %<long%> and %<_Decimal32%> in "
11144                            "declaration specifiers"));
11145               else if (specs->typespec_word == cts_dfloat64)
11146                 error_at (loc,
11147                           ("both %<long%> and %<_Decimal64%> in "
11148                            "declaration specifiers"));
11149               else if (specs->typespec_word == cts_dfloat128)
11150                 error_at (loc,
11151                           ("both %<long%> and %<_Decimal128%> in "
11152                            "declaration specifiers"));
11153               else
11154                 {
11155                   specs->long_p = true;
11156                   specs->locations[cdw_long] = loc;
11157                 }
11158               break;
11159             case RID_SHORT:
11160               dupe = specs->short_p;
11161               if (specs->long_p)
11162                 error_at (loc,
11163                           ("both %<long%> and %<short%> in "
11164                            "declaration specifiers"));
11165               else if (specs->typespec_word == cts_auto_type)
11166                 error_at (loc,
11167                           ("both %<short%> and %<__auto_type%> in "
11168                            "declaration specifiers"));
11169               else if (specs->typespec_word == cts_void)
11170                 error_at (loc,
11171                           ("both %<short%> and %<void%> in "
11172                            "declaration specifiers"));
11173               else if (specs->typespec_word == cts_int_n)
11174                 error_at (loc,
11175                           ("both %<short%> and %<__int%d%> in "
11176                            "declaration specifiers"),
11177                           int_n_data[specs->int_n_idx].bitsize);
11178               else if (specs->typespec_word == cts_bool)
11179                 error_at (loc,
11180                           ("both %<short%> and %<_Bool%> in "
11181                            "declaration specifiers"));
11182               else if (specs->typespec_word == cts_char)
11183                 error_at (loc,
11184                           ("both %<short%> and %<char%> in "
11185                            "declaration specifiers"));
11186               else if (specs->typespec_word == cts_float)
11187                 error_at (loc,
11188                           ("both %<short%> and %<float%> in "
11189                            "declaration specifiers"));
11190               else if (specs->typespec_word == cts_double)
11191                 error_at (loc,
11192                           ("both %<short%> and %<double%> in "
11193                            "declaration specifiers"));
11194               else if (specs->typespec_word == cts_floatn_nx)
11195                 error_at (loc,
11196                           ("both %<short%> and %<_Float%d%s%> in "
11197                            "declaration specifiers"),
11198                           floatn_nx_types[specs->floatn_nx_idx].n,
11199                           (floatn_nx_types[specs->floatn_nx_idx].extended
11200                            ? "x"
11201                            : ""));
11202               else if (specs->typespec_word == cts_dfloat32)
11203                 error_at (loc,
11204                           ("both %<short%> and %<_Decimal32%> in "
11205                            "declaration specifiers"));
11206               else if (specs->typespec_word == cts_dfloat64)
11207                 error_at (loc,
11208                           ("both %<short%> and %<_Decimal64%> in "
11209                            "declaration specifiers"));
11210               else if (specs->typespec_word == cts_dfloat128)
11211                 error_at (loc,
11212                           ("both %<short%> and %<_Decimal128%> in "
11213                            "declaration specifiers"));
11214               else
11215                 {
11216                   specs->short_p = true;
11217                   specs->locations[cdw_short] = loc;
11218                 }
11219               break;
11220             case RID_SIGNED:
11221               dupe = specs->signed_p;
11222               if (specs->unsigned_p)
11223                 error_at (loc,
11224                           ("both %<signed%> and %<unsigned%> in "
11225                            "declaration specifiers"));
11226               else if (specs->typespec_word == cts_auto_type)
11227                 error_at (loc,
11228                           ("both %<signed%> and %<__auto_type%> in "
11229                            "declaration specifiers"));
11230               else if (specs->typespec_word == cts_void)
11231                 error_at (loc,
11232                           ("both %<signed%> and %<void%> in "
11233                            "declaration specifiers"));
11234               else if (specs->typespec_word == cts_bool)
11235                 error_at (loc,
11236                           ("both %<signed%> and %<_Bool%> in "
11237                            "declaration specifiers"));
11238               else if (specs->typespec_word == cts_float)
11239                 error_at (loc,
11240                           ("both %<signed%> and %<float%> in "
11241                            "declaration specifiers"));
11242               else if (specs->typespec_word == cts_double)
11243                 error_at (loc,
11244                           ("both %<signed%> and %<double%> in "
11245                            "declaration specifiers"));
11246               else if (specs->typespec_word == cts_floatn_nx)
11247                 error_at (loc,
11248                           ("both %<signed%> and %<_Float%d%s%> in "
11249                            "declaration specifiers"),
11250                           floatn_nx_types[specs->floatn_nx_idx].n,
11251                           (floatn_nx_types[specs->floatn_nx_idx].extended
11252                            ? "x"
11253                            : ""));
11254               else if (specs->typespec_word == cts_dfloat32)
11255                 error_at (loc,
11256                           ("both %<signed%> and %<_Decimal32%> in "
11257                            "declaration specifiers"));
11258               else if (specs->typespec_word == cts_dfloat64)
11259                 error_at (loc,
11260                           ("both %<signed%> and %<_Decimal64%> in "
11261                            "declaration specifiers"));
11262               else if (specs->typespec_word == cts_dfloat128)
11263                 error_at (loc,
11264                           ("both %<signed%> and %<_Decimal128%> in "
11265                            "declaration specifiers"));
11266               else
11267                 {
11268                   specs->signed_p = true;
11269                   specs->locations[cdw_signed] = loc;
11270                 }
11271               break;
11272             case RID_UNSIGNED:
11273               dupe = specs->unsigned_p;
11274               if (specs->signed_p)
11275                 error_at (loc,
11276                           ("both %<signed%> and %<unsigned%> in "
11277                            "declaration specifiers"));
11278               else if (specs->typespec_word == cts_auto_type)
11279                 error_at (loc,
11280                           ("both %<unsigned%> and %<__auto_type%> in "
11281                            "declaration specifiers"));
11282               else if (specs->typespec_word == cts_void)
11283                 error_at (loc,
11284                           ("both %<unsigned%> and %<void%> in "
11285                            "declaration specifiers"));
11286               else if (specs->typespec_word == cts_bool)
11287                 error_at (loc,
11288                           ("both %<unsigned%> and %<_Bool%> in "
11289                            "declaration specifiers"));
11290               else if (specs->typespec_word == cts_float)
11291                 error_at (loc,
11292                           ("both %<unsigned%> and %<float%> in "
11293                            "declaration specifiers"));
11294               else if (specs->typespec_word == cts_double)
11295                 error_at (loc,
11296                           ("both %<unsigned%> and %<double%> in "
11297                            "declaration specifiers"));
11298               else if (specs->typespec_word == cts_floatn_nx)
11299                 error_at (loc,
11300                           ("both %<unsigned%> and %<_Float%d%s%> in "
11301                            "declaration specifiers"),
11302                           floatn_nx_types[specs->floatn_nx_idx].n,
11303                           (floatn_nx_types[specs->floatn_nx_idx].extended
11304                            ? "x"
11305                            : ""));
11306               else if (specs->typespec_word == cts_dfloat32)
11307                 error_at (loc,
11308                           ("both %<unsigned%> and %<_Decimal32%> in "
11309                            "declaration specifiers"));
11310               else if (specs->typespec_word == cts_dfloat64)
11311                 error_at (loc,
11312                           ("both %<unsigned%> and %<_Decimal64%> in "
11313                            "declaration specifiers"));
11314               else if (specs->typespec_word == cts_dfloat128)
11315                 error_at (loc,
11316                           ("both %<unsigned%> and %<_Decimal128%> in "
11317                            "declaration specifiers"));
11318               else
11319                 {
11320                   specs->unsigned_p = true;
11321                   specs->locations[cdw_unsigned] = loc;
11322                 }
11323               break;
11324             case RID_COMPLEX:
11325               dupe = specs->complex_p;
11326               if (!in_system_header_at (loc))
11327                 pedwarn_c90 (loc, OPT_Wpedantic,
11328                              "ISO C90 does not support complex types");
11329               if (specs->typespec_word == cts_auto_type)
11330                 error_at (loc,
11331                           ("both %<complex%> and %<__auto_type%> in "
11332                            "declaration specifiers"));
11333               else if (specs->typespec_word == cts_void)
11334                 error_at (loc,
11335                           ("both %<complex%> and %<void%> in "
11336                            "declaration specifiers"));
11337               else if (specs->typespec_word == cts_bool)
11338                 error_at (loc,
11339                           ("both %<complex%> and %<_Bool%> in "
11340                            "declaration specifiers"));
11341               else if (specs->typespec_word == cts_dfloat32)
11342                 error_at (loc,
11343                           ("both %<complex%> and %<_Decimal32%> in "
11344                            "declaration specifiers"));
11345               else if (specs->typespec_word == cts_dfloat64)
11346                 error_at (loc,
11347                           ("both %<complex%> and %<_Decimal64%> in "
11348                            "declaration specifiers"));
11349               else if (specs->typespec_word == cts_dfloat128)
11350                 error_at (loc,
11351                           ("both %<complex%> and %<_Decimal128%> in "
11352                            "declaration specifiers"));
11353               else if (specs->typespec_word == cts_fract)
11354                 error_at (loc,
11355                           ("both %<complex%> and %<_Fract%> in "
11356                            "declaration specifiers"));
11357               else if (specs->typespec_word == cts_accum)
11358                 error_at (loc,
11359                           ("both %<complex%> and %<_Accum%> in "
11360                            "declaration specifiers"));
11361               else if (specs->saturating_p)
11362                 error_at (loc,
11363                           ("both %<complex%> and %<_Sat%> in "
11364                            "declaration specifiers"));
11365               else
11366                 {
11367                   specs->complex_p = true;
11368                   specs->locations[cdw_complex] = loc;
11369                 }
11370               break;
11371             case RID_SAT:
11372               dupe = specs->saturating_p;
11373               pedwarn (loc, OPT_Wpedantic,
11374                        "ISO C does not support saturating types");
11375               if (specs->typespec_word == cts_int_n)
11376                 {
11377                   error_at (loc,
11378                             ("both %<_Sat%> and %<__int%d%> in "
11379                              "declaration specifiers"),
11380                             int_n_data[specs->int_n_idx].bitsize);
11381                 }
11382               else if (specs->typespec_word == cts_auto_type)
11383                 error_at (loc,
11384                           ("both %<_Sat%> and %<__auto_type%> in "
11385                            "declaration specifiers"));
11386               else if (specs->typespec_word == cts_void)
11387                 error_at (loc,
11388                           ("both %<_Sat%> and %<void%> in "
11389                            "declaration specifiers"));
11390               else if (specs->typespec_word == cts_bool)
11391                 error_at (loc,
11392                           ("both %<_Sat%> and %<_Bool%> in "
11393                            "declaration specifiers"));
11394               else if (specs->typespec_word == cts_char)
11395                 error_at (loc,
11396                           ("both %<_Sat%> and %<char%> in "
11397                            "declaration specifiers"));
11398               else if (specs->typespec_word == cts_int)
11399                 error_at (loc,
11400                           ("both %<_Sat%> and %<int%> in "
11401                            "declaration specifiers"));
11402               else if (specs->typespec_word == cts_float)
11403                 error_at (loc,
11404                           ("both %<_Sat%> and %<float%> in "
11405                            "declaration specifiers"));
11406               else if (specs->typespec_word == cts_double)
11407                 error_at (loc,
11408                           ("both %<_Sat%> and %<double%> in "
11409                            "declaration specifiers"));
11410               else if (specs->typespec_word == cts_floatn_nx)
11411                 error_at (loc,
11412                           ("both %<_Sat%> and %<_Float%d%s%> in "
11413                            "declaration specifiers"),
11414                           floatn_nx_types[specs->floatn_nx_idx].n,
11415                           (floatn_nx_types[specs->floatn_nx_idx].extended
11416                            ? "x"
11417                            : ""));
11418               else if (specs->typespec_word == cts_dfloat32)
11419                 error_at (loc,
11420                           ("both %<_Sat%> and %<_Decimal32%> in "
11421                            "declaration specifiers"));
11422               else if (specs->typespec_word == cts_dfloat64)
11423                 error_at (loc,
11424                           ("both %<_Sat%> and %<_Decimal64%> in "
11425                            "declaration specifiers"));
11426               else if (specs->typespec_word == cts_dfloat128)
11427                 error_at (loc,
11428                           ("both %<_Sat%> and %<_Decimal128%> in "
11429                            "declaration specifiers"));
11430               else if (specs->complex_p)
11431                 error_at (loc,
11432                           ("both %<_Sat%> and %<complex%> in "
11433                            "declaration specifiers"));
11434               else
11435                 {
11436                   specs->saturating_p = true;
11437                   specs->locations[cdw_saturating] = loc;
11438                 }
11439               break;
11440             default:
11441               gcc_unreachable ();
11442             }
11443
11444           if (dupe)
11445             error_at (loc, "duplicate %qE", type);
11446
11447           return specs;
11448         }
11449       else
11450         {
11451           /* "void", "_Bool", "char", "int", "float", "double",
11452              "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11453              "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11454              "__auto_type".  */
11455           if (specs->typespec_word != cts_none)
11456             {
11457               error_at (loc,
11458                         "two or more data types in declaration specifiers");
11459               return specs;
11460             }
11461           switch (i)
11462             {
11463             case RID_AUTO_TYPE:
11464               if (specs->long_p)
11465                 error_at (loc,
11466                           ("both %<long%> and %<__auto_type%> in "
11467                            "declaration specifiers"));
11468               else if (specs->short_p)
11469                 error_at (loc,
11470                           ("both %<short%> and %<__auto_type%> in "
11471                            "declaration specifiers"));
11472               else if (specs->signed_p)
11473                 error_at (loc,
11474                           ("both %<signed%> and %<__auto_type%> in "
11475                            "declaration specifiers"));
11476               else if (specs->unsigned_p)
11477                 error_at (loc,
11478                           ("both %<unsigned%> and %<__auto_type%> in "
11479                            "declaration specifiers"));
11480               else if (specs->complex_p)
11481                 error_at (loc,
11482                           ("both %<complex%> and %<__auto_type%> in "
11483                            "declaration specifiers"));
11484               else if (specs->saturating_p)
11485                 error_at (loc,
11486                           ("both %<_Sat%> and %<__auto_type%> in "
11487                            "declaration specifiers"));
11488               else
11489                 {
11490                   specs->typespec_word = cts_auto_type;
11491                   specs->locations[cdw_typespec] = loc;
11492                 }
11493               return specs;
11494             case RID_INT_N_0:
11495             case RID_INT_N_1:
11496             case RID_INT_N_2:
11497             case RID_INT_N_3:
11498               specs->int_n_idx = i - RID_INT_N_0;
11499               if (!in_system_header_at (input_location)
11500                   /* If the INT_N type ends in "__", and so is of the format
11501                      "__intN__", don't pedwarn.  */
11502                   && (strncmp (IDENTIFIER_POINTER (type)
11503                                + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11504                 pedwarn (loc, OPT_Wpedantic,
11505                          "ISO C does not support %<__int%d%> types",
11506                          int_n_data[specs->int_n_idx].bitsize);
11507
11508               if (specs->long_p)
11509                 error_at (loc,
11510                           ("both %<__int%d%> and %<long%> in "
11511                            "declaration specifiers"),
11512                           int_n_data[specs->int_n_idx].bitsize);
11513               else if (specs->saturating_p)
11514                 error_at (loc,
11515                           ("both %<_Sat%> and %<__int%d%> in "
11516                            "declaration specifiers"),
11517                           int_n_data[specs->int_n_idx].bitsize);
11518               else if (specs->short_p)
11519                 error_at (loc,
11520                           ("both %<__int%d%> and %<short%> in "
11521                            "declaration specifiers"),
11522                           int_n_data[specs->int_n_idx].bitsize);
11523               else if (! int_n_enabled_p[specs->int_n_idx])
11524                 {
11525                   specs->typespec_word = cts_int_n;
11526                   error_at (loc,
11527                             "%<__int%d%> is not supported on this target",
11528                             int_n_data[specs->int_n_idx].bitsize);
11529                 }
11530               else
11531                 {
11532                   specs->typespec_word = cts_int_n;
11533                   specs->locations[cdw_typespec] = loc;
11534                 }
11535               return specs;
11536             case RID_VOID:
11537               if (specs->long_p)
11538                 error_at (loc,
11539                           ("both %<long%> and %<void%> in "
11540                            "declaration specifiers"));
11541               else if (specs->short_p)
11542                 error_at (loc,
11543                           ("both %<short%> and %<void%> in "
11544                            "declaration specifiers"));
11545               else if (specs->signed_p)
11546                 error_at (loc,
11547                           ("both %<signed%> and %<void%> in "
11548                            "declaration specifiers"));
11549               else if (specs->unsigned_p)
11550                 error_at (loc,
11551                           ("both %<unsigned%> and %<void%> in "
11552                            "declaration specifiers"));
11553               else if (specs->complex_p)
11554                 error_at (loc,
11555                           ("both %<complex%> and %<void%> in "
11556                            "declaration specifiers"));
11557               else if (specs->saturating_p)
11558                 error_at (loc,
11559                           ("both %<_Sat%> and %<void%> in "
11560                            "declaration specifiers"));
11561               else
11562                 {
11563                   specs->typespec_word = cts_void;
11564                   specs->locations[cdw_typespec] = loc;
11565                 }
11566               return specs;
11567             case RID_BOOL:
11568               if (!in_system_header_at (loc))
11569                 pedwarn_c90 (loc, OPT_Wpedantic,
11570                              "ISO C90 does not support boolean types");
11571               if (specs->long_p)
11572                 error_at (loc,
11573                           ("both %<long%> and %<_Bool%> in "
11574                            "declaration specifiers"));
11575               else if (specs->short_p)
11576                 error_at (loc,
11577                           ("both %<short%> and %<_Bool%> in "
11578                            "declaration specifiers"));
11579               else if (specs->signed_p)
11580                 error_at (loc,
11581                           ("both %<signed%> and %<_Bool%> in "
11582                            "declaration specifiers"));
11583               else if (specs->unsigned_p)
11584                 error_at (loc,
11585                           ("both %<unsigned%> and %<_Bool%> in "
11586                            "declaration specifiers"));
11587               else if (specs->complex_p)
11588                 error_at (loc,
11589                           ("both %<complex%> and %<_Bool%> in "
11590                            "declaration specifiers"));
11591               else if (specs->saturating_p)
11592                 error_at (loc,
11593                           ("both %<_Sat%> and %<_Bool%> in "
11594                            "declaration specifiers"));
11595               else
11596                 {
11597                   specs->typespec_word = cts_bool;
11598                   specs->locations[cdw_typespec] = loc;
11599                 }
11600               return specs;
11601             case RID_CHAR:
11602               if (specs->long_p)
11603                 error_at (loc,
11604                           ("both %<long%> and %<char%> in "
11605                            "declaration specifiers"));
11606               else if (specs->short_p)
11607                 error_at (loc,
11608                           ("both %<short%> and %<char%> in "
11609                            "declaration specifiers"));
11610               else if (specs->saturating_p)
11611                 error_at (loc,
11612                           ("both %<_Sat%> and %<char%> in "
11613                            "declaration specifiers"));
11614               else
11615                 {
11616                   specs->typespec_word = cts_char;
11617                   specs->locations[cdw_typespec] = loc;
11618                 }
11619               return specs;
11620             case RID_INT:
11621               if (specs->saturating_p)
11622                 error_at (loc,
11623                           ("both %<_Sat%> and %<int%> in "
11624                            "declaration specifiers"));
11625               else
11626                 {
11627                   specs->typespec_word = cts_int;
11628                   specs->locations[cdw_typespec] = loc;
11629                 }
11630               return specs;
11631             case RID_FLOAT:
11632               if (specs->long_p)
11633                 error_at (loc,
11634                           ("both %<long%> and %<float%> in "
11635                            "declaration specifiers"));
11636               else if (specs->short_p)
11637                 error_at (loc,
11638                           ("both %<short%> and %<float%> in "
11639                            "declaration specifiers"));
11640               else if (specs->signed_p)
11641                 error_at (loc,
11642                           ("both %<signed%> and %<float%> in "
11643                            "declaration specifiers"));
11644               else if (specs->unsigned_p)
11645                 error_at (loc,
11646                           ("both %<unsigned%> and %<float%> in "
11647                            "declaration specifiers"));
11648               else if (specs->saturating_p)
11649                 error_at (loc,
11650                           ("both %<_Sat%> and %<float%> in "
11651                            "declaration specifiers"));
11652               else
11653                 {
11654                   specs->typespec_word = cts_float;
11655                   specs->locations[cdw_typespec] = loc;
11656                 }
11657               return specs;
11658             case RID_DOUBLE:
11659               if (specs->long_long_p)
11660                 error_at (loc,
11661                           ("both %<long long%> and %<double%> in "
11662                            "declaration specifiers"));
11663               else if (specs->short_p)
11664                 error_at (loc,
11665                           ("both %<short%> and %<double%> in "
11666                            "declaration specifiers"));
11667               else if (specs->signed_p)
11668                 error_at (loc,
11669                           ("both %<signed%> and %<double%> in "
11670                            "declaration specifiers"));
11671               else if (specs->unsigned_p)
11672                 error_at (loc,
11673                           ("both %<unsigned%> and %<double%> in "
11674                            "declaration specifiers"));
11675               else if (specs->saturating_p)
11676                 error_at (loc,
11677                           ("both %<_Sat%> and %<double%> in "
11678                            "declaration specifiers"));
11679               else
11680                 {
11681                   specs->typespec_word = cts_double;
11682                   specs->locations[cdw_typespec] = loc;
11683                 }
11684               return specs;
11685             CASE_RID_FLOATN_NX:
11686               specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11687               if (!in_system_header_at (input_location))
11688                 pedwarn (loc, OPT_Wpedantic,
11689                          "ISO C does not support the %<_Float%d%s%> type",
11690                          floatn_nx_types[specs->floatn_nx_idx].n,
11691                          (floatn_nx_types[specs->floatn_nx_idx].extended
11692                           ? "x"
11693                           : ""));
11694
11695               if (specs->long_p)
11696                 error_at (loc,
11697                           ("both %<long%> and %<_Float%d%s%> in "
11698                            "declaration specifiers"),
11699                           floatn_nx_types[specs->floatn_nx_idx].n,
11700                           (floatn_nx_types[specs->floatn_nx_idx].extended
11701                            ? "x"
11702                            : ""));
11703               else if (specs->short_p)
11704                 error_at (loc,
11705                           ("both %<short%> and %<_Float%d%s%> in "
11706                            "declaration specifiers"),
11707                           floatn_nx_types[specs->floatn_nx_idx].n,
11708                           (floatn_nx_types[specs->floatn_nx_idx].extended
11709                            ? "x"
11710                            : ""));
11711               else if (specs->signed_p)
11712                 error_at (loc,
11713                           ("both %<signed%> and %<_Float%d%s%> in "
11714                            "declaration specifiers"),
11715                           floatn_nx_types[specs->floatn_nx_idx].n,
11716                           (floatn_nx_types[specs->floatn_nx_idx].extended
11717                            ? "x"
11718                            : ""));
11719               else if (specs->unsigned_p)
11720                 error_at (loc,
11721                           ("both %<unsigned%> and %<_Float%d%s%> in "
11722                            "declaration specifiers"),
11723                           floatn_nx_types[specs->floatn_nx_idx].n,
11724                           (floatn_nx_types[specs->floatn_nx_idx].extended
11725                            ? "x"
11726                            : ""));
11727               else if (specs->saturating_p)
11728                 error_at (loc,
11729                           ("both %<_Sat%> and %<_Float%d%s%> in "
11730                            "declaration specifiers"),
11731                           floatn_nx_types[specs->floatn_nx_idx].n,
11732                           (floatn_nx_types[specs->floatn_nx_idx].extended
11733                            ? "x"
11734                            : ""));
11735               else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11736                 {
11737                   specs->typespec_word = cts_floatn_nx;
11738                   error_at (loc,
11739                             "%<_Float%d%s%> is not supported on this target",
11740                             floatn_nx_types[specs->floatn_nx_idx].n,
11741                             (floatn_nx_types[specs->floatn_nx_idx].extended
11742                              ? "x"
11743                              : ""));
11744                 }
11745               else
11746                 {
11747                   specs->typespec_word = cts_floatn_nx;
11748                   specs->locations[cdw_typespec] = loc;
11749                 }
11750               return specs;
11751             case RID_DFLOAT32:
11752             case RID_DFLOAT64:
11753             case RID_DFLOAT128:
11754               {
11755                 const char *str;
11756                 if (i == RID_DFLOAT32)
11757                   str = "_Decimal32";
11758                 else if (i == RID_DFLOAT64)
11759                   str = "_Decimal64";
11760                 else
11761                   str = "_Decimal128";
11762                 if (specs->long_long_p)
11763                   error_at (loc,
11764                             ("both %<long long%> and %qs in "
11765                              "declaration specifiers"),
11766                             str);
11767                 if (specs->long_p)
11768                   error_at (loc,
11769                             ("both %<long%> and %qs in "
11770                              "declaration specifiers"),
11771                             str);
11772                 else if (specs->short_p)
11773                   error_at (loc,
11774                             ("both %<short%> and %qs in "
11775                              "declaration specifiers"),
11776                             str);
11777                 else if (specs->signed_p)
11778                   error_at (loc,
11779                             ("both %<signed%> and %qs in "
11780                              "declaration specifiers"),
11781                             str);
11782                 else if (specs->unsigned_p)
11783                   error_at (loc,
11784                             ("both %<unsigned%> and %qs in "
11785                              "declaration specifiers"),
11786                             str);
11787                 else if (specs->complex_p)
11788                   error_at (loc,
11789                             ("both %<complex%> and %qs in "
11790                              "declaration specifiers"),
11791                             str);
11792                 else if (specs->saturating_p)
11793                   error_at (loc,
11794                             ("both %<_Sat%> and %qs in "
11795                              "declaration specifiers"),
11796                             str);
11797                 else if (i == RID_DFLOAT32)
11798                   specs->typespec_word = cts_dfloat32;
11799                 else if (i == RID_DFLOAT64)
11800                   specs->typespec_word = cts_dfloat64;
11801                 else
11802                   specs->typespec_word = cts_dfloat128;
11803                 specs->locations[cdw_typespec] = loc;
11804               }
11805               if (!targetm.decimal_float_supported_p ())
11806                 error_at (loc,
11807                           ("decimal floating-point not supported "
11808                            "for this target"));
11809               pedwarn_c11 (loc, OPT_Wpedantic,
11810                            "ISO C does not support decimal floating-point "
11811                            "before C2X");
11812               return specs;
11813             case RID_FRACT:
11814             case RID_ACCUM:
11815               {
11816                 const char *str;
11817                 if (i == RID_FRACT)
11818                   str = "_Fract";
11819                 else
11820                   str = "_Accum";
11821                 if (specs->complex_p)
11822                   error_at (loc,
11823                             ("both %<complex%> and %qs in "
11824                              "declaration specifiers"),
11825                             str);
11826                 else if (i == RID_FRACT)
11827                     specs->typespec_word = cts_fract;
11828                 else
11829                     specs->typespec_word = cts_accum;
11830                 specs->locations[cdw_typespec] = loc;
11831               }
11832               if (!targetm.fixed_point_supported_p ())
11833                 error_at (loc,
11834                           "fixed-point types not supported for this target");
11835               pedwarn (loc, OPT_Wpedantic,
11836                        "ISO C does not support fixed-point types");
11837               return specs;
11838             default:
11839               /* ObjC reserved word "id", handled below.  */
11840               break;
11841             }
11842         }
11843     }
11844
11845   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11846      form of ObjC type, cases such as "int" and "long" being handled
11847      above), a TYPE (struct, union, enum and typeof specifiers) or an
11848      ERROR_MARK.  In none of these cases may there have previously
11849      been any type specifiers.  */
11850   if (specs->type || specs->typespec_word != cts_none
11851       || specs->long_p || specs->short_p || specs->signed_p
11852       || specs->unsigned_p || specs->complex_p)
11853     error_at (loc, "two or more data types in declaration specifiers");
11854   else if (TREE_CODE (type) == TYPE_DECL)
11855     {
11856       if (TREE_TYPE (type) == error_mark_node)
11857         ; /* Allow the type to default to int to avoid cascading errors.  */
11858       else
11859         {
11860           specs->type = TREE_TYPE (type);
11861           specs->decl_attr = DECL_ATTRIBUTES (type);
11862           specs->typedef_p = true;
11863           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11864           specs->locations[cdw_typedef] = loc;
11865
11866           /* If this typedef name is defined in a struct, then a C++
11867              lookup would return a different value.  */
11868           if (warn_cxx_compat
11869               && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11870             warning_at (loc, OPT_Wc___compat,
11871                         "C++ lookup of %qD would return a field, not a type",
11872                         type);
11873
11874           /* If we are parsing a struct, record that a struct field
11875              used a typedef.  */
11876           if (warn_cxx_compat && struct_parse_info != NULL)
11877             struct_parse_info->typedefs_seen.safe_push (type);
11878         }
11879     }
11880   else if (TREE_CODE (type) == IDENTIFIER_NODE)
11881     {
11882       tree t = lookup_name (type);
11883       if (!t || TREE_CODE (t) != TYPE_DECL)
11884         error_at (loc, "%qE fails to be a typedef or built in type", type);
11885       else if (TREE_TYPE (t) == error_mark_node)
11886         ;
11887       else
11888         {
11889           specs->type = TREE_TYPE (t);
11890           specs->locations[cdw_typespec] = loc;
11891         }
11892     }
11893   else
11894     {
11895       if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11896         {
11897           specs->typedef_p = true;
11898           specs->locations[cdw_typedef] = loc;
11899           if (spec.expr)
11900             {
11901               if (specs->expr)
11902                 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11903                                       specs->expr, spec.expr);
11904               else
11905                 specs->expr = spec.expr;
11906               specs->expr_const_operands &= spec.expr_const_operands;
11907             }
11908         }
11909       specs->type = type;
11910     }
11911
11912   return specs;
11913 }
11914
11915 /* Add the storage class specifier or function specifier SCSPEC to the
11916    declaration specifiers SPECS, returning SPECS.  */
11917
11918 struct c_declspecs *
11919 declspecs_add_scspec (location_t loc,
11920                       struct c_declspecs *specs,
11921                       tree scspec)
11922 {
11923   enum rid i;
11924   enum c_storage_class n = csc_none;
11925   bool dupe = false;
11926   specs->declspecs_seen_p = true;
11927   specs->non_std_attrs_seen_p = true;
11928   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11929               && C_IS_RESERVED_WORD (scspec));
11930   i = C_RID_CODE (scspec);
11931   if (specs->non_sc_seen_p)
11932     warning (OPT_Wold_style_declaration,
11933              "%qE is not at beginning of declaration", scspec);
11934   switch (i)
11935     {
11936     case RID_INLINE:
11937       /* C99 permits duplicate inline.  Although of doubtful utility,
11938          it seems simplest to permit it in gnu89 mode as well, as
11939          there is also little utility in maintaining this as a
11940          difference between gnu89 and C99 inline.  */
11941       dupe = false;
11942       specs->inline_p = true;
11943       specs->locations[cdw_inline] = loc;
11944       break;
11945     case RID_NORETURN:
11946       /* Duplicate _Noreturn is permitted.  */
11947       dupe = false;
11948       specs->noreturn_p = true;
11949       specs->locations[cdw_noreturn] = loc;
11950       break;
11951     case RID_THREAD:
11952       dupe = specs->thread_p;
11953       if (specs->storage_class == csc_auto)
11954         error ("%qE used with %<auto%>", scspec);
11955       else if (specs->storage_class == csc_register)
11956         error ("%qE used with %<register%>", scspec);
11957       else if (specs->storage_class == csc_typedef)
11958         error ("%qE used with %<typedef%>", scspec);
11959       else
11960         {
11961           specs->thread_p = true;
11962           specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11963                                          "__thread") == 0);
11964           /* A diagnostic is not required for the use of this
11965              identifier in the implementation namespace; only diagnose
11966              it for the C11 spelling because of existing code using
11967              the other spelling.  */
11968           if (!specs->thread_gnu_p)
11969             {
11970               if (flag_isoc99)
11971                 pedwarn_c99 (loc, OPT_Wpedantic,
11972                              "ISO C99 does not support %qE", scspec);
11973               else
11974                 pedwarn_c99 (loc, OPT_Wpedantic,
11975                              "ISO C90 does not support %qE", scspec);
11976             }
11977           specs->locations[cdw_thread] = loc;
11978         }
11979       break;
11980     case RID_AUTO:
11981       n = csc_auto;
11982       break;
11983     case RID_EXTERN:
11984       n = csc_extern;
11985       /* Diagnose "__thread extern".  */
11986       if (specs->thread_p && specs->thread_gnu_p)
11987         error ("%<__thread%> before %<extern%>");
11988       break;
11989     case RID_REGISTER:
11990       n = csc_register;
11991       break;
11992     case RID_STATIC:
11993       n = csc_static;
11994       /* Diagnose "__thread static".  */
11995       if (specs->thread_p && specs->thread_gnu_p)
11996         error ("%<__thread%> before %<static%>");
11997       break;
11998     case RID_TYPEDEF:
11999       n = csc_typedef;
12000       break;
12001     default:
12002       gcc_unreachable ();
12003     }
12004   if (n != csc_none && n == specs->storage_class)
12005     dupe = true;
12006   if (dupe)
12007     {
12008       if (i == RID_THREAD)
12009         error ("duplicate %<_Thread_local%> or %<__thread%>");
12010       else
12011         error ("duplicate %qE", scspec);
12012     }
12013   if (n != csc_none)
12014     {
12015       if (specs->storage_class != csc_none && n != specs->storage_class)
12016         {
12017           error ("multiple storage classes in declaration specifiers");
12018         }
12019       else
12020         {
12021           specs->storage_class = n;
12022           specs->locations[cdw_storage_class] = loc;
12023           if (n != csc_extern && n != csc_static && specs->thread_p)
12024             {
12025               error ("%qs used with %qE",
12026                      specs->thread_gnu_p ? "__thread" : "_Thread_local",
12027                      scspec);
12028               specs->thread_p = false;
12029             }
12030         }
12031     }
12032   return specs;
12033 }
12034
12035 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12036    returning SPECS.  */
12037
12038 struct c_declspecs *
12039 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12040 {
12041   specs->attrs = chainon (attrs, specs->attrs);
12042   specs->locations[cdw_attributes] = loc;
12043   specs->declspecs_seen_p = true;
12044   /* In the case of standard attributes at the start of the
12045      declaration, the caller will reset this.  */
12046   specs->non_std_attrs_seen_p = true;
12047   return specs;
12048 }
12049
12050 /* Add an _Alignas specifier (expression ALIGN, or type whose
12051    alignment is ALIGN) to the declaration specifiers SPECS, returning
12052    SPECS.  */
12053 struct c_declspecs *
12054 declspecs_add_alignas (location_t loc,
12055                        struct c_declspecs *specs, tree align)
12056 {
12057   specs->alignas_p = true;
12058   specs->locations[cdw_alignas] = loc;
12059   if (align == error_mark_node)
12060     return specs;
12061
12062   /* Only accept the alignment if it's valid and greater than
12063      the current one.  Zero is invalid but by C11 required to
12064      be silently ignored.  */
12065   int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12066   if (align_log > specs->align_log)
12067     specs->align_log = align_log;
12068   return specs;
12069 }
12070
12071 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12072    specifiers with any other type specifier to determine the resulting
12073    type.  This is where ISO C checks on complex types are made, since
12074    "_Complex long" is a prefix of the valid ISO C type "_Complex long
12075    double".  Also apply postfix standard attributes to modify the type.  */
12076
12077 struct c_declspecs *
12078 finish_declspecs (struct c_declspecs *specs)
12079 {
12080   /* If a type was specified as a whole, we have no modifiers and are
12081      done.  */
12082   if (specs->type != NULL_TREE)
12083     {
12084       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12085                   && !specs->signed_p && !specs->unsigned_p
12086                   && !specs->complex_p);
12087
12088       /* Set a dummy type.  */
12089       if (TREE_CODE (specs->type) == ERROR_MARK)
12090         specs->type = integer_type_node;
12091       goto handle_postfix_attrs;
12092     }
12093
12094   /* If none of "void", "_Bool", "char", "int", "float" or "double"
12095      has been specified, treat it as "int" unless "_Complex" is
12096      present and there are no other specifiers.  If we just have
12097      "_Complex", it is equivalent to "_Complex double", but e.g.
12098      "_Complex short" is equivalent to "_Complex short int".  */
12099   if (specs->typespec_word == cts_none)
12100     {
12101       if (specs->saturating_p)
12102         {
12103           error_at (specs->locations[cdw_saturating],
12104                     "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12105           if (!targetm.fixed_point_supported_p ())
12106             error_at (specs->locations[cdw_saturating],
12107                       "fixed-point types not supported for this target");
12108           specs->typespec_word = cts_fract;
12109         }
12110       else if (specs->long_p || specs->short_p
12111                || specs->signed_p || specs->unsigned_p)
12112         {
12113           specs->typespec_word = cts_int;
12114         }
12115       else if (specs->complex_p)
12116         {
12117           specs->typespec_word = cts_double;
12118           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12119                    "ISO C does not support plain %<complex%> meaning "
12120                    "%<double complex%>");
12121         }
12122       else
12123         {
12124           specs->typespec_word = cts_int;
12125           specs->default_int_p = true;
12126           /* We don't diagnose this here because grokdeclarator will
12127              give more specific diagnostics according to whether it is
12128              a function definition.  */
12129         }
12130     }
12131
12132   /* If "signed" was specified, record this to distinguish "int" and
12133      "signed int" in the case of a bit-field with
12134      -funsigned-bitfields.  */
12135   specs->explicit_signed_p = specs->signed_p;
12136
12137   /* Now compute the actual type.  */
12138   switch (specs->typespec_word)
12139     {
12140     case cts_auto_type:
12141       gcc_assert (!specs->long_p && !specs->short_p
12142                   && !specs->signed_p && !specs->unsigned_p
12143                   && !specs->complex_p);
12144       /* Type to be filled in later.  */
12145       if (specs->postfix_attrs)
12146         error ("%<__auto_type%> followed by %<[[]]%> attributes");
12147       break;
12148     case cts_void:
12149       gcc_assert (!specs->long_p && !specs->short_p
12150                   && !specs->signed_p && !specs->unsigned_p
12151                   && !specs->complex_p);
12152       specs->type = void_type_node;
12153       break;
12154     case cts_bool:
12155       gcc_assert (!specs->long_p && !specs->short_p
12156                   && !specs->signed_p && !specs->unsigned_p
12157                   && !specs->complex_p);
12158       specs->type = boolean_type_node;
12159       break;
12160     case cts_char:
12161       gcc_assert (!specs->long_p && !specs->short_p);
12162       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12163       if (specs->signed_p)
12164         specs->type = signed_char_type_node;
12165       else if (specs->unsigned_p)
12166         specs->type = unsigned_char_type_node;
12167       else
12168         specs->type = char_type_node;
12169       if (specs->complex_p)
12170         {
12171           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12172                    "ISO C does not support complex integer types");
12173           specs->type = build_complex_type (specs->type);
12174         }
12175       break;
12176     case cts_int_n:
12177       gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12178       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12179       if (! int_n_enabled_p[specs->int_n_idx])
12180         specs->type = integer_type_node;
12181       else
12182         specs->type = (specs->unsigned_p
12183                        ? int_n_trees[specs->int_n_idx].unsigned_type
12184                        : int_n_trees[specs->int_n_idx].signed_type);
12185       if (specs->complex_p)
12186         {
12187           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12188                    "ISO C does not support complex integer types");
12189           specs->type = build_complex_type (specs->type);
12190         }
12191       break;
12192     case cts_int:
12193       gcc_assert (!(specs->long_p && specs->short_p));
12194       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12195       if (specs->long_long_p)
12196         specs->type = (specs->unsigned_p
12197                        ? long_long_unsigned_type_node
12198                        : long_long_integer_type_node);
12199       else if (specs->long_p)
12200         specs->type = (specs->unsigned_p
12201                        ? long_unsigned_type_node
12202                        : long_integer_type_node);
12203       else if (specs->short_p)
12204         specs->type = (specs->unsigned_p
12205                        ? short_unsigned_type_node
12206                        : short_integer_type_node);
12207       else
12208         specs->type = (specs->unsigned_p
12209                        ? unsigned_type_node
12210                        : integer_type_node);
12211       if (specs->complex_p)
12212         {
12213           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12214                    "ISO C does not support complex integer types");
12215           specs->type = build_complex_type (specs->type);
12216         }
12217       break;
12218     case cts_float:
12219       gcc_assert (!specs->long_p && !specs->short_p
12220                   && !specs->signed_p && !specs->unsigned_p);
12221       specs->type = (specs->complex_p
12222                      ? complex_float_type_node
12223                      : float_type_node);
12224       break;
12225     case cts_double:
12226       gcc_assert (!specs->long_long_p && !specs->short_p
12227                   && !specs->signed_p && !specs->unsigned_p);
12228       if (specs->long_p)
12229         {
12230           specs->type = (specs->complex_p
12231                          ? complex_long_double_type_node
12232                          : long_double_type_node);
12233         }
12234       else
12235         {
12236           specs->type = (specs->complex_p
12237                          ? complex_double_type_node
12238                          : double_type_node);
12239         }
12240       break;
12241     case cts_floatn_nx:
12242       gcc_assert (!specs->long_p && !specs->short_p
12243                   && !specs->signed_p && !specs->unsigned_p);
12244       if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12245         specs->type = integer_type_node;
12246       else if (specs->complex_p)
12247         specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12248       else
12249         specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12250       break;
12251     case cts_dfloat32:
12252     case cts_dfloat64:
12253     case cts_dfloat128:
12254       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12255                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12256       if (!targetm.decimal_float_supported_p ())
12257         specs->type = integer_type_node;
12258       else if (specs->typespec_word == cts_dfloat32)
12259         specs->type = dfloat32_type_node;
12260       else if (specs->typespec_word == cts_dfloat64)
12261         specs->type = dfloat64_type_node;
12262       else
12263         specs->type = dfloat128_type_node;
12264       break;
12265     case cts_fract:
12266       gcc_assert (!specs->complex_p);
12267       if (!targetm.fixed_point_supported_p ())
12268         specs->type = integer_type_node;
12269       else if (specs->saturating_p)
12270         {
12271           if (specs->long_long_p)
12272             specs->type = specs->unsigned_p
12273                           ? sat_unsigned_long_long_fract_type_node
12274                           : sat_long_long_fract_type_node;
12275           else if (specs->long_p)
12276             specs->type = specs->unsigned_p
12277                           ? sat_unsigned_long_fract_type_node
12278                           : sat_long_fract_type_node;
12279           else if (specs->short_p)
12280             specs->type = specs->unsigned_p
12281                           ? sat_unsigned_short_fract_type_node
12282                           : sat_short_fract_type_node;
12283           else
12284             specs->type = specs->unsigned_p
12285                           ? sat_unsigned_fract_type_node
12286                           : sat_fract_type_node;
12287         }
12288       else
12289         {
12290           if (specs->long_long_p)
12291             specs->type = specs->unsigned_p
12292                           ? unsigned_long_long_fract_type_node
12293                           : long_long_fract_type_node;
12294           else if (specs->long_p)
12295             specs->type = specs->unsigned_p
12296                           ? unsigned_long_fract_type_node
12297                           : long_fract_type_node;
12298           else if (specs->short_p)
12299             specs->type = specs->unsigned_p
12300                           ? unsigned_short_fract_type_node
12301                           : short_fract_type_node;
12302           else
12303             specs->type = specs->unsigned_p
12304                           ? unsigned_fract_type_node
12305                           : fract_type_node;
12306         }
12307       break;
12308     case cts_accum:
12309       gcc_assert (!specs->complex_p);
12310       if (!targetm.fixed_point_supported_p ())
12311         specs->type = integer_type_node;
12312       else if (specs->saturating_p)
12313         {
12314           if (specs->long_long_p)
12315             specs->type = specs->unsigned_p
12316                           ? sat_unsigned_long_long_accum_type_node
12317                           : sat_long_long_accum_type_node;
12318           else if (specs->long_p)
12319             specs->type = specs->unsigned_p
12320                           ? sat_unsigned_long_accum_type_node
12321                           : sat_long_accum_type_node;
12322           else if (specs->short_p)
12323             specs->type = specs->unsigned_p
12324                           ? sat_unsigned_short_accum_type_node
12325                           : sat_short_accum_type_node;
12326           else
12327             specs->type = specs->unsigned_p
12328                           ? sat_unsigned_accum_type_node
12329                           : sat_accum_type_node;
12330         }
12331       else
12332         {
12333           if (specs->long_long_p)
12334             specs->type = specs->unsigned_p
12335                           ? unsigned_long_long_accum_type_node
12336                           : long_long_accum_type_node;
12337           else if (specs->long_p)
12338             specs->type = specs->unsigned_p
12339                           ? unsigned_long_accum_type_node
12340                           : long_accum_type_node;
12341           else if (specs->short_p)
12342             specs->type = specs->unsigned_p
12343                           ? unsigned_short_accum_type_node
12344                           : short_accum_type_node;
12345           else
12346             specs->type = specs->unsigned_p
12347                           ? unsigned_accum_type_node
12348                           : accum_type_node;
12349         }
12350       break;
12351     default:
12352       gcc_unreachable ();
12353     }
12354  handle_postfix_attrs:
12355   if (specs->type != NULL)
12356     {
12357       specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12358       decl_attributes (&specs->type, specs->postfix_attrs, 0);
12359       specs->postfix_attrs = NULL_TREE;
12360     }
12361
12362   return specs;
12363 }
12364
12365 /* Perform final processing on one file scope's declarations (or the
12366    external scope's declarations), GLOBALS.  */
12367
12368 static void
12369 c_write_global_declarations_1 (tree globals)
12370 {
12371   tree decl;
12372   bool reconsider;
12373
12374   /* Process the decls in the order they were written.  */
12375   for (decl = globals; decl; decl = DECL_CHAIN (decl))
12376     {
12377       /* Check for used but undefined static functions using the C
12378          standard's definition of "used", and set TREE_NO_WARNING so
12379          that check_global_declaration doesn't repeat the check.  */
12380       if (TREE_CODE (decl) == FUNCTION_DECL
12381           && DECL_INITIAL (decl) == NULL_TREE
12382           && DECL_EXTERNAL (decl)
12383           && !TREE_PUBLIC (decl))
12384         {
12385           if (C_DECL_USED (decl))
12386             {
12387               /* TODO: Add OPT_Wundefined-inline.  */
12388               if (pedwarn (input_location, 0, "%q+F used but never defined",
12389                            decl))
12390                 suppress_warning (decl /* OPT_Wundefined-inline.  */);
12391             }
12392           /* For -Wunused-function warn about unused static prototypes.  */
12393           else if (warn_unused_function
12394                    && ! DECL_ARTIFICIAL (decl)
12395                    && ! warning_suppressed_p (decl, OPT_Wunused_function))
12396             {
12397               if (warning (OPT_Wunused_function,
12398                            "%q+F declared %<static%> but never defined",
12399                            decl))
12400                 suppress_warning (decl, OPT_Wunused_function);
12401             }
12402         }
12403
12404       wrapup_global_declaration_1 (decl);
12405     }
12406
12407   do
12408     {
12409       reconsider = false;
12410       for (decl = globals; decl; decl = DECL_CHAIN (decl))
12411         reconsider |= wrapup_global_declaration_2 (decl);
12412     }
12413   while (reconsider);
12414 }
12415
12416 /* Preserve the external declarations scope across a garbage collect.  */
12417 static GTY(()) tree ext_block;
12418
12419 /* Collect all references relevant to SOURCE_FILE.  */
12420
12421 static void
12422 collect_all_refs (const char *source_file)
12423 {
12424   tree t;
12425   unsigned i;
12426
12427   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12428     collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12429
12430   collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12431 }
12432
12433 /* Collect source file references at global level.  */
12434
12435 static void
12436 collect_source_refs (void)
12437 {
12438   tree t;
12439   tree decls;
12440   tree decl;
12441   unsigned i;
12442
12443   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12444     { 
12445       decls = DECL_INITIAL (t);
12446       for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12447         if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12448           collect_source_ref (DECL_SOURCE_FILE (decl));
12449     }
12450
12451   for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12452     if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12453       collect_source_ref (DECL_SOURCE_FILE (decl));
12454 }
12455
12456 /* Free attribute access data that are not needed by the middle end. */
12457
12458 static void
12459 free_attr_access_data ()
12460 {
12461   struct cgraph_node *n;
12462
12463   /* Iterate over all functions declared in the translation unit.  */
12464   FOR_EACH_FUNCTION (n)
12465     {
12466       for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12467         if (tree attrs = DECL_ATTRIBUTES (parm))
12468           attr_access::free_lang_data (attrs);
12469
12470       tree fntype = TREE_TYPE (n->decl);
12471       if (!fntype || fntype == error_mark_node)
12472         continue;
12473       tree attrs = TYPE_ATTRIBUTES (fntype);
12474       if (!attrs)
12475         continue;
12476
12477       attr_access::free_lang_data (attrs);
12478     }
12479 }
12480
12481 /* Perform any final parser cleanups and generate initial debugging
12482    information.  */
12483
12484 void
12485 c_parse_final_cleanups (void)
12486 {
12487   tree t;
12488   unsigned i;
12489
12490   /* We don't want to do this if generating a PCH.  */
12491   if (pch_file)
12492     return;
12493
12494   timevar_stop (TV_PHASE_PARSING);
12495   timevar_start (TV_PHASE_DEFERRED);
12496
12497   /* Do the Objective-C stuff.  This is where all the Objective-C
12498      module stuff gets generated (symtab, class/protocol/selector
12499      lists etc).  */
12500   if (c_dialect_objc ())
12501     objc_write_global_declarations ();
12502
12503   /* Close the external scope.  */
12504   ext_block = pop_scope ();
12505   external_scope = 0;
12506   gcc_assert (!current_scope);
12507
12508   /* Handle -fdump-ada-spec[-slim]. */
12509   if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12510     {
12511       /* Build a table of files to generate specs for */
12512       collect_source_ref (main_input_filename);
12513       if (!flag_dump_ada_spec_slim)
12514         collect_source_refs ();
12515
12516       dump_ada_specs (collect_all_refs, NULL);
12517     }
12518
12519   /* Process all file scopes in this compilation, and the external_scope,
12520      through wrapup_global_declarations.  */
12521   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12522     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12523   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12524
12525   if (!in_lto_p)
12526     free_attr_access_data ();
12527
12528   timevar_stop (TV_PHASE_DEFERRED);
12529   timevar_start (TV_PHASE_PARSING);
12530
12531   ext_block = NULL;
12532 }
12533
12534 /* Register reserved keyword WORD as qualifier for address space AS.  */
12535
12536 void
12537 c_register_addr_space (const char *word, addr_space_t as)
12538 {
12539   int rid = RID_FIRST_ADDR_SPACE + as;
12540   tree id;
12541
12542   /* Address space qualifiers are only supported
12543      in C with GNU extensions enabled.  */
12544   if (c_dialect_objc () || flag_no_asm)
12545     return;
12546
12547   id = get_identifier (word);
12548   C_SET_RID_CODE (id, rid);
12549   C_IS_RESERVED_WORD (id) = 1;
12550   ridpointers [rid] = id;
12551 }
12552
12553 /* Return identifier to look up for omp declare reduction.  */
12554
12555 tree
12556 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
12557 {
12558   const char *p = NULL;
12559   switch (reduction_code)
12560     {
12561     case PLUS_EXPR: p = "+"; break;
12562     case MULT_EXPR: p = "*"; break;
12563     case MINUS_EXPR: p = "-"; break;
12564     case BIT_AND_EXPR: p = "&"; break;
12565     case BIT_XOR_EXPR: p = "^"; break;
12566     case BIT_IOR_EXPR: p = "|"; break;
12567     case TRUTH_ANDIF_EXPR: p = "&&"; break;
12568     case TRUTH_ORIF_EXPR: p = "||"; break;
12569     case MIN_EXPR: p = "min"; break;
12570     case MAX_EXPR: p = "max"; break;
12571     default:
12572       break;
12573     }
12574
12575   if (p == NULL)
12576     {
12577       if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12578         return error_mark_node;
12579       p = IDENTIFIER_POINTER (reduction_id);
12580     }
12581
12582   const char prefix[] = "omp declare reduction ";
12583   size_t lenp = sizeof (prefix);
12584   size_t len = strlen (p);
12585   char *name = XALLOCAVEC (char, lenp + len);
12586   memcpy (name, prefix, lenp - 1);
12587   memcpy (name + lenp - 1, p, len + 1);
12588   return get_identifier (name);
12589 }
12590
12591 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12592    VAR_DECL, bind it into the current scope and return it.  */
12593
12594 tree
12595 c_omp_reduction_decl (tree reduction_id)
12596 {
12597   struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12598   if (b != NULL && B_IN_CURRENT_SCOPE (b))
12599     return b->decl;
12600
12601   tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12602                           reduction_id, integer_type_node);
12603   DECL_ARTIFICIAL (decl) = 1;
12604   DECL_EXTERNAL (decl) = 1;
12605   TREE_STATIC (decl) = 1;
12606   TREE_PUBLIC (decl) = 0;
12607   bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12608   return decl;
12609 }
12610
12611 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE.  */
12612
12613 tree
12614 c_omp_reduction_lookup (tree reduction_id, tree type)
12615 {
12616   struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12617   while (b)
12618     {
12619       tree t;
12620       for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12621         if (comptypes (TREE_PURPOSE (t), type))
12622           return TREE_VALUE (t);
12623       b = b->shadowed;
12624     }
12625   return error_mark_node;
12626 }
12627
12628 /* Helper function called via walk_tree, to diagnose invalid
12629    #pragma omp declare reduction combiners or initializers.  */
12630
12631 tree
12632 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12633 {
12634   tree *vars = (tree *) data;
12635   if (SSA_VAR_P (*tp)
12636       && !DECL_ARTIFICIAL (*tp)
12637       && *tp != vars[0]
12638       && *tp != vars[1])
12639     {
12640       location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12641       if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12642         error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12643                        "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12644                   *tp);
12645       else
12646         error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12647                        "to variable %qD which is not %<omp_priv%> nor "
12648                        "%<omp_orig%>",
12649                   *tp);
12650       return *tp;
12651     }
12652   return NULL_TREE;
12653 }
12654
12655
12656 bool
12657 c_check_in_current_scope (tree decl)
12658 {
12659   struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12660   return b != NULL && B_IN_CURRENT_SCOPE (b);
12661 }
12662
12663 #include "gt-c-c-decl.h"