c: C2x noreturn attribute
[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 /* If non-zero, implicit "omp declare target" attribute is added into the
157    attribute lists.  */
158 int 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 /* INIT is a constructor that forms DECL's initializer.  If the final
5038    element initializes a flexible array field, add the size of that
5039    initializer to DECL's size.  */
5040
5041 static void
5042 add_flexible_array_elts_to_size (tree decl, tree init)
5043 {
5044   tree elt, type;
5045
5046   if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5047     return;
5048
5049   elt = CONSTRUCTOR_ELTS (init)->last ().value;
5050   type = TREE_TYPE (elt);
5051   if (TREE_CODE (type) == ARRAY_TYPE
5052       && TYPE_SIZE (type) == NULL_TREE
5053       && TYPE_DOMAIN (type) != NULL_TREE
5054       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5055     {
5056       complete_array_type (&type, elt, false);
5057       DECL_SIZE (decl)
5058         = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5059       DECL_SIZE_UNIT (decl)
5060         = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5061     }
5062 }
5063 \f
5064 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5065    Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5066    before the type name, and set *EXPR_CONST_OPERANDS, if
5067    EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5068    appear in a constant expression.  */
5069
5070 tree
5071 groktypename (struct c_type_name *type_name, tree *expr,
5072               bool *expr_const_operands)
5073 {
5074   tree type;
5075   tree attrs = type_name->specs->attrs;
5076
5077   type_name->specs->attrs = NULL_TREE;
5078
5079   type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5080                          false, NULL, &attrs, expr, expr_const_operands,
5081                          DEPRECATED_NORMAL);
5082
5083   /* Apply attributes.  */
5084   attrs = c_warn_type_attributes (attrs);
5085   decl_attributes (&type, attrs, 0);
5086
5087   return type;
5088 }
5089
5090 /* Looks up the most recent pushed declaration corresponding to DECL.  */
5091
5092 static tree
5093 lookup_last_decl (tree decl)
5094 {
5095   tree last_decl = lookup_name (DECL_NAME (decl));
5096   if (!last_decl)
5097     last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5098   return last_decl;
5099 }
5100
5101 /* Wrapper for decl_attributes that adds some implicit attributes
5102    to VAR_DECLs or FUNCTION_DECLs.  */
5103
5104 static tree
5105 c_decl_attributes (tree *node, tree attributes, int flags)
5106 {
5107   /* Add implicit "omp declare target" attribute if requested.  */
5108   if (current_omp_declare_target_attribute
5109       && ((VAR_P (*node) && is_global_var (*node))
5110           || TREE_CODE (*node) == FUNCTION_DECL))
5111     {
5112       if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5113         attributes = tree_cons (get_identifier ("omp declare target implicit"),
5114                                 NULL_TREE, attributes);
5115       else
5116         {
5117           attributes = tree_cons (get_identifier ("omp declare target"),
5118                                   NULL_TREE, attributes);
5119           attributes = tree_cons (get_identifier ("omp declare target block"),
5120                                   NULL_TREE, attributes);
5121         }
5122     }
5123
5124   /* Look up the current declaration with all the attributes merged
5125      so far so that attributes on the current declaration that's
5126      about to be pushed that conflict with the former can be detected,
5127      diagnosed, and rejected as appropriate.  */
5128   tree last_decl = lookup_last_decl (*node);
5129   return decl_attributes (node, attributes, flags, last_decl);
5130 }
5131
5132
5133 /* Decode a declarator in an ordinary declaration or data definition.
5134    This is called as soon as the type information and variable name
5135    have been parsed, before parsing the initializer if any.
5136    Here we create the ..._DECL node, fill in its type,
5137    and put it on the list of decls for the current context.
5138    When nonnull, set *LASTLOC to the location of the prior declaration
5139    of the same entity if one exists.
5140    The ..._DECL node is returned as the value.
5141
5142    Exception: for arrays where the length is not specified,
5143    the type is left null, to be filled in by `finish_decl'.
5144
5145    Function definitions do not come here; they go to start_function
5146    instead.  However, external and forward declarations of functions
5147    do go through here.  Structure field declarations are done by
5148    grokfield and not through here.  */
5149
5150 tree
5151 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5152             bool initialized, tree attributes, location_t *lastloc /* = NULL */)
5153 {
5154   tree decl;
5155   tree tem;
5156   tree expr = NULL_TREE;
5157   enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5158
5159   /* An object declared as __attribute__((unavailable)) suppresses
5160      warnings and errors from __attribute__((deprecated/unavailable))
5161      components.
5162      An object declared as __attribute__((deprecated)) suppresses
5163      warnings of uses of other deprecated items.  */
5164   if (lookup_attribute ("unavailable", attributes))
5165     deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5166   else if (lookup_attribute ("deprecated", attributes))
5167     deprecated_state = DEPRECATED_SUPPRESS;
5168
5169   decl = grokdeclarator (declarator, declspecs,
5170                          NORMAL, initialized, NULL, &attributes, &expr, NULL,
5171                          deprecated_state);
5172   if (!decl || decl == error_mark_node)
5173     return NULL_TREE;
5174
5175   if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5176     if (lastdecl != error_mark_node)
5177       *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5178
5179   if (expr)
5180     add_stmt (fold_convert (void_type_node, expr));
5181
5182   if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5183       && TREE_PUBLIC (decl))
5184     warning (OPT_Wmain, "%q+D is usually a function", decl);
5185
5186   if (initialized)
5187     /* Is it valid for this decl to have an initializer at all?
5188        If not, set INITIALIZED to zero, which will indirectly
5189        tell 'finish_decl' to ignore the initializer once it is parsed.  */
5190     switch (TREE_CODE (decl))
5191       {
5192       case TYPE_DECL:
5193         error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5194         initialized = false;
5195         break;
5196
5197       case FUNCTION_DECL:
5198         error ("function %qD is initialized like a variable", decl);
5199         initialized = false;
5200         break;
5201
5202       case PARM_DECL:
5203         /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
5204         error ("parameter %qD is initialized", decl);
5205         initialized = false;
5206         break;
5207
5208       default:
5209         /* Don't allow initializations for incomplete types except for
5210            arrays which might be completed by the initialization.  */
5211
5212         /* This can happen if the array size is an undefined macro.
5213            We already gave a warning, so we don't need another one.  */
5214         if (TREE_TYPE (decl) == error_mark_node)
5215           initialized = false;
5216         else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5217           {
5218             /* A complete type is ok if size is fixed.  If the size is
5219                variable, an empty initializer is OK and nonempty
5220                initializers will be diagnosed in the parser.  */
5221           }
5222         else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5223           {
5224             error ("variable %qD has initializer but incomplete type", decl);
5225             initialized = false;
5226           }
5227       }
5228
5229   if (initialized)
5230     {
5231       if (current_scope == file_scope)
5232         TREE_STATIC (decl) = 1;
5233
5234       /* Tell 'pushdecl' this is an initialized decl
5235          even though we don't yet have the initializer expression.
5236          Also tell 'finish_decl' it may store the real initializer.  */
5237       DECL_INITIAL (decl) = error_mark_node;
5238     }
5239
5240   /* If this is a function declaration, write a record describing it to the
5241      prototypes file (if requested).  */
5242
5243   if (TREE_CODE (decl) == FUNCTION_DECL)
5244     gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5245
5246   /* ANSI specifies that a tentative definition which is not merged with
5247      a non-tentative definition behaves exactly like a definition with an
5248      initializer equal to zero.  (Section 3.7.2)
5249
5250      -fno-common gives strict ANSI behavior, though this tends to break
5251      a large body of code that grew up without this rule.
5252
5253      Thread-local variables are never common, since there's no entrenched
5254      body of code to break, and it allows more efficient variable references
5255      in the presence of dynamic linking.  */
5256
5257   if (VAR_P (decl)
5258       && !initialized
5259       && TREE_PUBLIC (decl)
5260       && !DECL_THREAD_LOCAL_P (decl)
5261       && !flag_no_common)
5262     DECL_COMMON (decl) = 1;
5263
5264   /* Set attributes here so if duplicate decl, will have proper attributes.  */
5265   c_decl_attributes (&decl, attributes, 0);
5266
5267   /* Handle gnu_inline attribute.  */
5268   if (declspecs->inline_p
5269       && !flag_gnu89_inline
5270       && TREE_CODE (decl) == FUNCTION_DECL
5271       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5272           || current_function_decl))
5273     {
5274       if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5275         ;
5276       else if (declspecs->storage_class != csc_static)
5277         DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5278     }
5279
5280   if (TREE_CODE (decl) == FUNCTION_DECL
5281       && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5282     {
5283       struct c_declarator *ce = declarator;
5284
5285       if (ce->kind == cdk_pointer)
5286         ce = declarator->declarator;
5287       if (ce->kind == cdk_function)
5288         {
5289           tree args = ce->u.arg_info->parms;
5290           for (; args; args = DECL_CHAIN (args))
5291             {
5292               tree type = TREE_TYPE (args);
5293               if (type && INTEGRAL_TYPE_P (type)
5294                   && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5295                 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5296             }
5297         }
5298     }
5299
5300   if (TREE_CODE (decl) == FUNCTION_DECL
5301       && DECL_DECLARED_INLINE_P (decl)
5302       && DECL_UNINLINABLE (decl)
5303       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5304     warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5305              decl, "noinline");
5306
5307   /* C99 6.7.4p3: An inline definition of a function with external
5308      linkage shall not contain a definition of a modifiable object
5309      with static storage duration...  */
5310   if (VAR_P (decl)
5311       && current_scope != file_scope
5312       && TREE_STATIC (decl)
5313       && !TREE_READONLY (decl)
5314       && DECL_DECLARED_INLINE_P (current_function_decl)
5315       && DECL_EXTERNAL (current_function_decl))
5316     record_inline_static (input_location, current_function_decl,
5317                           decl, csi_modifiable);
5318
5319   if (c_dialect_objc ()
5320       && VAR_OR_FUNCTION_DECL_P (decl))
5321       objc_check_global_decl (decl);
5322
5323   /* Add this decl to the current scope.
5324      TEM may equal DECL or it may be a previous decl of the same name.  */
5325   tem = pushdecl (decl);
5326
5327   if (initialized && DECL_EXTERNAL (tem))
5328     {
5329       DECL_EXTERNAL (tem) = 0;
5330       TREE_STATIC (tem) = 1;
5331     }
5332
5333   return tem;
5334 }
5335
5336 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5337    DECL or the non-array element type if DECL is an uninitialized array.
5338    If that type has a const member, diagnose this. */
5339
5340 static void
5341 diagnose_uninitialized_cst_member (tree decl, tree type)
5342 {
5343   tree field;
5344   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5345     {
5346       tree field_type;
5347       if (TREE_CODE (field) != FIELD_DECL)
5348         continue;
5349       field_type = strip_array_types (TREE_TYPE (field));
5350
5351       if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5352         {
5353           auto_diagnostic_group d;
5354           if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5355                           "uninitialized const member in %qT is invalid in C++",
5356                           strip_array_types (TREE_TYPE (decl))))
5357             inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5358         }
5359
5360       if (RECORD_OR_UNION_TYPE_P (field_type))
5361         diagnose_uninitialized_cst_member (decl, field_type);
5362     }
5363 }
5364
5365 /* Finish processing of a declaration;
5366    install its initial value.
5367    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5368    If the length of an array type is not known before,
5369    it must be determined now, from the initial value, or it is an error.
5370
5371    INIT_LOC is the location of the initial value.  */
5372
5373 void
5374 finish_decl (tree decl, location_t init_loc, tree init,
5375              tree origtype, tree asmspec_tree)
5376 {
5377   tree type;
5378   bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5379   const char *asmspec = 0;
5380
5381   /* If a name was specified, get the string.  */
5382   if (VAR_OR_FUNCTION_DECL_P (decl)
5383       && DECL_FILE_SCOPE_P (decl))
5384     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5385   if (asmspec_tree)
5386     asmspec = TREE_STRING_POINTER (asmspec_tree);
5387
5388   if (VAR_P (decl)
5389       && TREE_STATIC (decl)
5390       && global_bindings_p ())
5391     /* So decl is a global variable. Record the types it uses
5392        so that we can decide later to emit debug info for them.  */
5393     record_types_used_by_current_var_decl (decl);
5394
5395   /* If `start_decl' didn't like having an initialization, ignore it now.  */
5396   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5397     init = NULL_TREE;
5398
5399   /* Don't crash if parm is initialized.  */
5400   if (TREE_CODE (decl) == PARM_DECL)
5401     init = NULL_TREE;
5402
5403   if (init)
5404     store_init_value (init_loc, decl, init, origtype);
5405
5406   if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5407                             || TREE_CODE (decl) == FIELD_DECL))
5408     objc_check_decl (decl);
5409
5410   type = TREE_TYPE (decl);
5411
5412   /* Deduce size of array from initialization, if not already known.
5413      This is only needed for an initialization in the current scope;
5414      it must not be done for a file-scope initialization of a
5415      declaration with external linkage, redeclared in an inner scope
5416      with the outer declaration shadowed in an intermediate scope.  */
5417   if (TREE_CODE (type) == ARRAY_TYPE
5418       && TYPE_DOMAIN (type) == NULL_TREE
5419       && TREE_CODE (decl) != TYPE_DECL
5420       && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5421     {
5422       bool do_default
5423         = (TREE_STATIC (decl)
5424            /* Even if pedantic, an external linkage array
5425               may have incomplete type at first.  */
5426            ? pedantic && !TREE_PUBLIC (decl)
5427            : !DECL_EXTERNAL (decl));
5428       int failure
5429         = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5430                                do_default);
5431
5432       /* Get the completed type made by complete_array_type.  */
5433       type = TREE_TYPE (decl);
5434
5435       switch (failure)
5436         {
5437         case 1:
5438           error ("initializer fails to determine size of %q+D", decl);
5439           break;
5440
5441         case 2:
5442           if (do_default)
5443             error ("array size missing in %q+D", decl);
5444           break;
5445
5446         case 3:
5447           error ("zero or negative size array %q+D", decl);
5448           break;
5449
5450         case 0:
5451           /* For global variables, update the copy of the type that
5452              exists in the binding.  */
5453           if (TREE_PUBLIC (decl))
5454             {
5455               struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5456               while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5457                 b_ext = b_ext->shadowed;
5458               if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5459                 {
5460                   if (b_ext->u.type && comptypes (b_ext->u.type, type))
5461                     b_ext->u.type = composite_type (b_ext->u.type, type);
5462                   else
5463                     b_ext->u.type = type;
5464                 }
5465             }
5466           break;
5467
5468         default:
5469           gcc_unreachable ();
5470         }
5471
5472       if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5473         TREE_TYPE (DECL_INITIAL (decl)) = type;
5474
5475       relayout_decl (decl);
5476     }
5477
5478   /* Look for braced array initializers for character arrays and
5479      recursively convert them into STRING_CSTs.  */
5480   if (tree init = DECL_INITIAL (decl))
5481     DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5482
5483   if (VAR_P (decl))
5484     {
5485       if (init && TREE_CODE (init) == CONSTRUCTOR)
5486         add_flexible_array_elts_to_size (decl, init);
5487
5488       complete_flexible_array_elts (DECL_INITIAL (decl));
5489
5490       if (is_global_var (decl))
5491         {
5492           type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5493                                        ? TCTX_THREAD_STORAGE
5494                                        : TCTX_STATIC_STORAGE);
5495           if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5496             TREE_TYPE (decl) = error_mark_node;
5497         }
5498
5499       if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5500           && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5501         layout_decl (decl, 0);
5502
5503       if (DECL_SIZE (decl) == NULL_TREE
5504           /* Don't give an error if we already gave one earlier.  */
5505           && TREE_TYPE (decl) != error_mark_node
5506           && (TREE_STATIC (decl)
5507               /* A static variable with an incomplete type
5508                  is an error if it is initialized.
5509                  Also if it is not file scope.
5510                  Otherwise, let it through, but if it is not `extern'
5511                  then it may cause an error message later.  */
5512               ? (DECL_INITIAL (decl) != NULL_TREE
5513                  || !DECL_FILE_SCOPE_P (decl))
5514               /* An automatic variable with an incomplete type
5515                  is an error.  */
5516               : !DECL_EXTERNAL (decl)))
5517          {
5518            error ("storage size of %q+D isn%'t known", decl);
5519            TREE_TYPE (decl) = error_mark_node;
5520          }
5521
5522       if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5523           || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5524           && DECL_SIZE (decl) == NULL_TREE
5525           && TREE_STATIC (decl))
5526         incomplete_record_decls.safe_push (decl);
5527
5528       if (is_global_var (decl)
5529           && DECL_SIZE (decl) != NULL_TREE
5530           && TREE_TYPE (decl) != error_mark_node)
5531         {
5532           if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5533             constant_expression_warning (DECL_SIZE (decl));
5534           else
5535             {
5536               error ("storage size of %q+D isn%'t constant", decl);
5537               TREE_TYPE (decl) = error_mark_node;
5538             }
5539         }
5540
5541       if (TREE_USED (type))
5542         {
5543           TREE_USED (decl) = 1;
5544           DECL_READ_P (decl) = 1;
5545         }
5546     }
5547
5548   /* If this is a function and an assembler name is specified, reset DECL_RTL
5549      so we can give it its new name.  Also, update builtin_decl if it
5550      was a normal built-in.  */
5551   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5552     {
5553       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5554         set_builtin_user_assembler_name (decl, asmspec);
5555       set_user_assembler_name (decl, asmspec);
5556     }
5557
5558   /* If #pragma weak was used, mark the decl weak now.  */
5559   maybe_apply_pragma_weak (decl);
5560
5561   /* Output the assembler code and/or RTL code for variables and functions,
5562      unless the type is an undefined structure or union.
5563      If not, it will get done when the type is completed.  */
5564
5565   if (VAR_OR_FUNCTION_DECL_P (decl))
5566     {
5567       /* Determine the ELF visibility.  */
5568       if (TREE_PUBLIC (decl))
5569         c_determine_visibility (decl);
5570
5571       /* This is a no-op in c-lang.cc or something real in objc-act.cc.  */
5572       if (c_dialect_objc ())
5573         objc_check_decl (decl);
5574
5575       if (asmspec)
5576         {
5577           /* If this is not a static variable, issue a warning.
5578              It doesn't make any sense to give an ASMSPEC for an
5579              ordinary, non-register local variable.  Historically,
5580              GCC has accepted -- but ignored -- the ASMSPEC in
5581              this case.  */
5582           if (!DECL_FILE_SCOPE_P (decl)
5583               && VAR_P (decl)
5584               && !C_DECL_REGISTER (decl)
5585               && !TREE_STATIC (decl))
5586             warning (0, "ignoring %<asm%> specifier for non-static local "
5587                      "variable %q+D", decl);
5588           else
5589             set_user_assembler_name (decl, asmspec);
5590         }
5591
5592       if (DECL_FILE_SCOPE_P (decl))
5593         {
5594           if (DECL_INITIAL (decl) == NULL_TREE
5595               || DECL_INITIAL (decl) == error_mark_node)
5596             /* Don't output anything
5597                when a tentative file-scope definition is seen.
5598                But at end of compilation, do output code for them.  */
5599             DECL_DEFER_OUTPUT (decl) = 1;
5600           if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5601             DECL_HARD_REGISTER (decl) = 1;
5602           rest_of_decl_compilation (decl, true, 0);
5603
5604           if (TREE_CODE (decl) == FUNCTION_DECL)
5605             {
5606               tree parms = DECL_ARGUMENTS (decl);
5607               const bool builtin = fndecl_built_in_p (decl);
5608               if (tree access = build_attr_access_from_parms (parms, !builtin))
5609                 decl_attributes (&decl, access, 0);
5610             }
5611         }
5612       else
5613         {
5614           /* In conjunction with an ASMSPEC, the `register'
5615              keyword indicates that we should place the variable
5616              in a particular register.  */
5617           if (asmspec && C_DECL_REGISTER (decl))
5618             {
5619               DECL_HARD_REGISTER (decl) = 1;
5620               /* This cannot be done for a structure with volatile
5621                  fields, on which DECL_REGISTER will have been
5622                  reset.  */
5623               if (!DECL_REGISTER (decl))
5624                 error ("cannot put object with volatile field into register");
5625             }
5626
5627           if (TREE_CODE (decl) != FUNCTION_DECL)
5628             {
5629               /* If we're building a variable sized type, and we might be
5630                  reachable other than via the top of the current binding
5631                  level, then create a new BIND_EXPR so that we deallocate
5632                  the object at the right time.  */
5633               /* Note that DECL_SIZE can be null due to errors.  */
5634               if (DECL_SIZE (decl)
5635                   && !TREE_CONSTANT (DECL_SIZE (decl))
5636                   && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5637                 {
5638                   tree bind;
5639                   bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5640                   TREE_SIDE_EFFECTS (bind) = 1;
5641                   add_stmt (bind);
5642                   BIND_EXPR_BODY (bind) = push_stmt_list ();
5643                 }
5644               add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5645                                     DECL_EXPR, decl));
5646             }
5647         }
5648
5649
5650       if (!DECL_FILE_SCOPE_P (decl))
5651         {
5652           /* Recompute the RTL of a local array now
5653              if it used to be an incomplete type.  */
5654           if (was_incomplete && !is_global_var (decl))
5655             {
5656               /* If we used it already as memory, it must stay in memory.  */
5657               TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5658               /* If it's still incomplete now, no init will save it.  */
5659               if (DECL_SIZE (decl) == NULL_TREE)
5660                 DECL_INITIAL (decl) = NULL_TREE;
5661             }
5662         }
5663     }
5664
5665   if (TREE_CODE (decl) == TYPE_DECL)
5666     {
5667       if (!DECL_FILE_SCOPE_P (decl)
5668           && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5669         add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5670
5671       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5672     }
5673
5674   /* Install a cleanup (aka destructor) if one was given.  */
5675   if (VAR_P (decl) && !TREE_STATIC (decl))
5676     {
5677       tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5678       if (attr)
5679         {
5680           tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5681           tree cleanup_decl = lookup_name (cleanup_id);
5682           tree cleanup;
5683           vec<tree, va_gc> *v;
5684
5685           /* Build "cleanup(&decl)" for the destructor.  */
5686           cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5687           vec_alloc (v, 1);
5688           v->quick_push (cleanup);
5689           cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5690                                                vNULL, cleanup_decl, v, NULL);
5691           vec_free (v);
5692
5693           /* Don't warn about decl unused; the cleanup uses it.  */
5694           TREE_USED (decl) = 1;
5695           TREE_USED (cleanup_decl) = 1;
5696           DECL_READ_P (decl) = 1;
5697
5698           push_cleanup (decl, cleanup, false);
5699         }
5700     }
5701
5702   if (warn_cxx_compat
5703       && VAR_P (decl)
5704       && !DECL_EXTERNAL (decl)
5705       && DECL_INITIAL (decl) == NULL_TREE)
5706     {
5707       type = strip_array_types (type);
5708       if (TREE_READONLY (decl))
5709         warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5710                     "uninitialized %<const %D%> is invalid in C++", decl);
5711       else if (RECORD_OR_UNION_TYPE_P (type)
5712                && C_TYPE_FIELDS_READONLY (type))
5713         diagnose_uninitialized_cst_member (decl, type);
5714     }
5715
5716   if (flag_openmp
5717       && VAR_P (decl)
5718       && lookup_attribute ("omp declare target implicit",
5719                            DECL_ATTRIBUTES (decl)))
5720     {
5721       DECL_ATTRIBUTES (decl)
5722         = remove_attribute ("omp declare target implicit",
5723                             DECL_ATTRIBUTES (decl));
5724       if (!omp_mappable_type (TREE_TYPE (decl)))
5725         error ("%q+D in declare target directive does not have mappable type",
5726                decl);
5727       else if (!lookup_attribute ("omp declare target",
5728                                   DECL_ATTRIBUTES (decl))
5729                && !lookup_attribute ("omp declare target link",
5730                                      DECL_ATTRIBUTES (decl)))
5731         {
5732           DECL_ATTRIBUTES (decl)
5733             = tree_cons (get_identifier ("omp declare target"),
5734                          NULL_TREE, DECL_ATTRIBUTES (decl));
5735             symtab_node *node = symtab_node::get (decl);
5736             if (node != NULL)
5737               {
5738                 node->offloadable = 1;
5739                 if (ENABLE_OFFLOADING)
5740                   {
5741                     g->have_offload = true;
5742                     if (is_a <varpool_node *> (node))
5743                       vec_safe_push (offload_vars, decl);
5744                   }
5745               }
5746         }
5747     }
5748
5749   /* This is the last point we can lower alignment so give the target the
5750      chance to do so.  */
5751   if (VAR_P (decl)
5752       && !is_global_var (decl)
5753       && !DECL_HARD_REGISTER (decl))
5754     targetm.lower_local_decl_alignment (decl);
5755
5756   invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5757 }
5758
5759 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5760    EXPR is NULL or a pointer to an expression that needs to be
5761    evaluated for the side effects of array size expressions in the
5762    parameters.  */
5763
5764 tree
5765 grokparm (const struct c_parm *parm, tree *expr)
5766 {
5767   tree attrs = parm->attrs;
5768   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5769                               NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5770
5771   decl_attributes (&decl, attrs, 0);
5772
5773   return decl;
5774 }
5775
5776 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5777    described by PARM, concatenated onto attributes ATTRS.
5778    The spec consists of one dollar symbol for each specified variable
5779    bound, one asterisk for each unspecified variable bound, followed
5780    by at most one specification of the most significant bound of
5781    an ordinary array parameter.  For ordinary arrays the specification
5782    is either the constant bound itself, or the space character for
5783    an array with an unspecified bound (the [] form).  Finally, a chain
5784    of specified variable bounds is appended to the spec, starting with
5785    the most significant bound.  For example, the PARM T a[2][m][3][n]
5786    will produce __attribute__((arg spec ("[$$2]", m, n)).
5787    For T a typedef for an array with variable bounds, the bounds are
5788    included in the specification in the expected order.
5789    No "arg spec"  is created for parameters of pointer types, making
5790    a distinction between T(*)[N] (or, equivalently, T[][N]) and
5791    the T[M][N] form, all of which have the same type and are represented
5792    the same, but only the last of which gets an "arg spec" describing
5793    the most significant bound M.  */
5794
5795 static tree
5796 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5797 {
5798   /* The attribute specification string, minor bound first.  */
5799   std::string spec;
5800
5801   /* A list of VLA variable bounds, major first, or null if unspecified
5802      or not a VLA.  */
5803   tree vbchain = NULL_TREE;
5804   /* True for a pointer parameter.  */
5805   bool pointer = false;
5806   /* True for an ordinary array with an unpecified bound.  */
5807   bool nobound = false;
5808
5809   /* Create a string representation for the bounds of the array/VLA.  */
5810   for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
5811     {
5812       next = pd->declarator;
5813       while (next && next->kind == cdk_attrs)
5814         next = next->declarator;
5815
5816       /* Remember if a pointer has been seen to avoid storing the constant
5817          bound.  */
5818       if (pd->kind == cdk_pointer)
5819         pointer = true;
5820
5821       if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
5822           && (!next || next->kind == cdk_id))
5823         {
5824           /* Do nothing for the common case of a pointer.  The fact that
5825              the parameter is one can be deduced from the absence of
5826              an arg spec for it.  */
5827           return attrs;
5828         }
5829
5830       if (pd->kind == cdk_id)
5831         {
5832           if (pointer
5833               || !parm->specs->type
5834               || TREE_CODE (parm->specs->type) != ARRAY_TYPE
5835               || !TYPE_DOMAIN (parm->specs->type)
5836               || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
5837             continue;
5838
5839           tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
5840           if (!vbchain
5841               && TREE_CODE (max) == INTEGER_CST)
5842             {
5843               /* Extract the upper bound from a parameter of an array type
5844                  unless the parameter is an ordinary array of unspecified
5845                  bound in which case a next iteration of the loop will
5846                  exit.  */
5847               if (spec.empty () || spec.end ()[-1] != ' ')
5848                 {
5849                   if (!tree_fits_shwi_p (max))
5850                     continue;
5851
5852                   /* The upper bound is the value of the largest valid
5853                      index.  */
5854                   HOST_WIDE_INT n = tree_to_shwi (max) + 1;
5855                   char buf[40];
5856                   sprintf (buf, "%lu", (unsigned long)n);
5857                   spec += buf;
5858                 }
5859               continue;
5860             }
5861
5862           /* For a VLA typedef, create a list of its variable bounds and
5863              append it in the expected order to VBCHAIN.  */
5864           tree tpbnds = NULL_TREE;
5865           for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
5866                type = TREE_TYPE (type))
5867             {
5868               tree nelts = array_type_nelts (type);
5869               if (error_operand_p (nelts))
5870                 return attrs;
5871               if (TREE_CODE (nelts) != INTEGER_CST)
5872                 {
5873                   /* Each variable VLA bound is represented by the dollar
5874                      sign.  */
5875                   spec += "$";
5876                   tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
5877                 }
5878             }
5879           tpbnds = nreverse (tpbnds);
5880           vbchain = chainon (vbchain, tpbnds);
5881           continue;
5882         }
5883
5884       if (pd->kind != cdk_array)
5885         continue;
5886
5887       if (pd->u.array.vla_unspec_p)
5888         {
5889           /* Each unspecified bound is represented by a star.  There
5890              can be any number of these in a declaration (but none in
5891              a definition).  */
5892           spec += '*';
5893           continue;
5894         }
5895
5896       tree nelts = pd->u.array.dimen;
5897       if (!nelts)
5898         {
5899           /* Ordinary array of unspecified size.  There can be at most
5900              one for the most significant bound.  Exit on the next
5901              iteration which determines whether or not PARM is declared
5902              as a pointer or an array.  */
5903           nobound = true;
5904           continue;
5905         }
5906
5907       if (pd->u.array.static_p)
5908         spec += 's';
5909
5910       if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
5911         /* Avoid invalid NELTS.  */
5912         return attrs;
5913
5914       STRIP_NOPS (nelts);
5915       nelts = c_fully_fold (nelts, false, nullptr);
5916       if (TREE_CODE (nelts) == INTEGER_CST)
5917         {
5918           /* Skip all constant bounds except the most significant one.
5919              The interior ones are included in the array type.  */
5920           if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
5921             continue;
5922
5923           if (!tree_fits_uhwi_p (nelts))
5924             /* Bail completely on invalid bounds.  */
5925             return attrs;
5926
5927           char buf[40];
5928           unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
5929           sprintf (buf, "%llu", (unsigned long long)n);
5930           spec += buf;
5931           break;
5932         }
5933
5934       /* Each variable VLA bound is represented by a dollar sign.  */
5935       spec += "$";
5936       vbchain = tree_cons (NULL_TREE, nelts, vbchain);
5937     }
5938
5939   if (spec.empty () && !nobound)
5940     return attrs;
5941
5942   spec.insert (0, "[");
5943   if (nobound)
5944     /* Ordinary array of unspecified bound is represented by a space.
5945        It must be last in the spec.  */
5946     spec += ' ';
5947   spec += ']';
5948
5949   tree acsstr = build_string (spec.length () + 1, spec.c_str ());
5950   tree args = tree_cons (NULL_TREE, acsstr, vbchain);
5951   tree name = get_identifier ("arg spec");
5952   return tree_cons (name, args, attrs);
5953 }
5954
5955 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5956    and push that on the current scope.  EXPR is a pointer to an
5957    expression that needs to be evaluated for the side effects of array
5958    size expressions in the parameters.  */
5959
5960 void
5961 push_parm_decl (const struct c_parm *parm, tree *expr)
5962 {
5963   tree attrs = parm->attrs;
5964   tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5965                               &attrs, expr, NULL, DEPRECATED_NORMAL);
5966   if (decl && DECL_P (decl))
5967     DECL_SOURCE_LOCATION (decl) = parm->loc;
5968
5969   attrs = get_parm_array_spec (parm, attrs);
5970   decl_attributes (&decl, attrs, 0);
5971
5972   decl = pushdecl (decl);
5973
5974   finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5975 }
5976
5977 /* Mark all the parameter declarations to date as forward decls.
5978    Also diagnose use of this extension.  */
5979
5980 void
5981 mark_forward_parm_decls (void)
5982 {
5983   struct c_binding *b;
5984
5985   if (pedantic && !current_scope->warned_forward_parm_decls)
5986     {
5987       pedwarn (input_location, OPT_Wpedantic,
5988                "ISO C forbids forward parameter declarations");
5989       current_scope->warned_forward_parm_decls = true;
5990     }
5991
5992   for (b = current_scope->bindings; b; b = b->prev)
5993     if (TREE_CODE (b->decl) == PARM_DECL)
5994       TREE_ASM_WRITTEN (b->decl) = 1;
5995 }
5996 \f
5997 /* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
5998    literal, which may be an incomplete array type completed by the
5999    initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6000    literal.  NON_CONST is true if the initializers contain something
6001    that cannot occur in a constant expression.  If ALIGNAS_ALIGN is nonzero,
6002    it is the (valid) alignment for this compound literal, as specified
6003    with _Alignas.  */
6004
6005 tree
6006 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6007                         unsigned int alignas_align)
6008 {
6009   /* We do not use start_decl here because we have a type, not a declarator;
6010      and do not use finish_decl because the decl should be stored inside
6011      the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
6012   tree decl;
6013   tree complit;
6014   tree stmt;
6015
6016   if (type == error_mark_node
6017       || init == error_mark_node)
6018     return error_mark_node;
6019
6020   decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6021   DECL_EXTERNAL (decl) = 0;
6022   TREE_PUBLIC (decl) = 0;
6023   TREE_STATIC (decl) = (current_scope == file_scope);
6024   DECL_CONTEXT (decl) = current_function_decl;
6025   TREE_USED (decl) = 1;
6026   DECL_READ_P (decl) = 1;
6027   DECL_ARTIFICIAL (decl) = 1;
6028   DECL_IGNORED_P (decl) = 1;
6029   C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6030   TREE_TYPE (decl) = type;
6031   c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6032   if (alignas_align)
6033     {
6034       SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6035       DECL_USER_ALIGN (decl) = 1;
6036     }
6037   store_init_value (loc, decl, init, NULL_TREE);
6038
6039   if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6040     {
6041       int failure = complete_array_type (&TREE_TYPE (decl),
6042                                          DECL_INITIAL (decl), true);
6043       /* If complete_array_type returns 3, it means that the
6044          initial value of the compound literal is empty.  Allow it.  */
6045       gcc_assert (failure == 0 || failure == 3);
6046
6047       type = TREE_TYPE (decl);
6048       TREE_TYPE (DECL_INITIAL (decl)) = type;
6049     }
6050
6051   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6052     {
6053       c_incomplete_type_error (loc, NULL_TREE, type);
6054       return error_mark_node;
6055     }
6056
6057   if (TREE_STATIC (decl)
6058       && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6059     return error_mark_node;
6060
6061   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6062   complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6063   TREE_SIDE_EFFECTS (complit) = 1;
6064
6065   layout_decl (decl, 0);
6066
6067   if (TREE_STATIC (decl))
6068     {
6069       /* This decl needs a name for the assembler output.  */
6070       set_compound_literal_name (decl);
6071       DECL_DEFER_OUTPUT (decl) = 1;
6072       DECL_COMDAT (decl) = 1;
6073       pushdecl (decl);
6074       rest_of_decl_compilation (decl, 1, 0);
6075     }
6076   else if (current_function_decl && !current_scope->parm_flag)
6077     pushdecl (decl);
6078
6079   if (non_const)
6080     {
6081       complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6082       C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6083     }
6084
6085   return complit;
6086 }
6087
6088 /* Check the type of a compound literal.  Here we just check that it
6089    is valid for C++.  */
6090
6091 void
6092 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6093 {
6094   if (warn_cxx_compat
6095       && (type_name->specs->typespec_kind == ctsk_tagdef
6096           || type_name->specs->typespec_kind == ctsk_tagfirstref
6097           || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6098     warning_at (loc, OPT_Wc___compat,
6099                 "defining a type in a compound literal is invalid in C++");
6100 }
6101 \f
6102 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6103    replacing with appropriate values if they are invalid.  */
6104
6105 static void
6106 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6107                                tree orig_name)
6108 {
6109   tree type_mv;
6110   unsigned int max_width;
6111   unsigned HOST_WIDE_INT w;
6112   const char *name = (orig_name
6113                       ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6114                       : _("<anonymous>"));
6115
6116   /* Detect and ignore out of range field width and process valid
6117      field widths.  */
6118   if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6119     {
6120       error_at (loc, "bit-field %qs width not an integer constant", name);
6121       *width = integer_one_node;
6122     }
6123   else
6124     {
6125       if (TREE_CODE (*width) != INTEGER_CST)
6126         {
6127           *width = c_fully_fold (*width, false, NULL);
6128           if (TREE_CODE (*width) == INTEGER_CST)
6129             pedwarn (loc, OPT_Wpedantic,
6130                      "bit-field %qs width not an integer constant expression",
6131                      name);
6132         }
6133       if (TREE_CODE (*width) != INTEGER_CST)
6134         {
6135           error_at (loc, "bit-field %qs width not an integer constant", name);
6136           *width = integer_one_node;
6137         }
6138       constant_expression_warning (*width);
6139       if (tree_int_cst_sgn (*width) < 0)
6140         {
6141           error_at (loc, "negative width in bit-field %qs", name);
6142           *width = integer_one_node;
6143         }
6144       else if (integer_zerop (*width) && orig_name)
6145         {
6146           error_at (loc, "zero width for bit-field %qs", name);
6147           *width = integer_one_node;
6148         }
6149     }
6150
6151   /* Detect invalid bit-field type.  */
6152   if (TREE_CODE (*type) != INTEGER_TYPE
6153       && TREE_CODE (*type) != BOOLEAN_TYPE
6154       && TREE_CODE (*type) != ENUMERAL_TYPE)
6155     {
6156       error_at (loc, "bit-field %qs has invalid type", name);
6157       *type = unsigned_type_node;
6158     }
6159
6160   if (TYPE_WARN_IF_NOT_ALIGN (*type))
6161     {
6162       error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6163                 name);
6164       *type = unsigned_type_node;
6165     }
6166
6167   type_mv = TYPE_MAIN_VARIANT (*type);
6168   if (!in_system_header_at (input_location)
6169       && type_mv != integer_type_node
6170       && type_mv != unsigned_type_node
6171       && type_mv != boolean_type_node)
6172     pedwarn_c90 (loc, OPT_Wpedantic,
6173                  "type of bit-field %qs is a GCC extension", name);
6174
6175   max_width = TYPE_PRECISION (*type);
6176
6177   if (compare_tree_int (*width, max_width) > 0)
6178     {
6179       error_at (loc, "width of %qs exceeds its type", name);
6180       w = max_width;
6181       *width = build_int_cst (integer_type_node, w);
6182     }
6183   else
6184     w = tree_to_uhwi (*width);
6185
6186   if (TREE_CODE (*type) == ENUMERAL_TYPE)
6187     {
6188       struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6189       if (!lt
6190           || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6191           || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6192         warning_at (loc, 0, "%qs is narrower than values of its type", name);
6193     }
6194 }
6195
6196 \f
6197
6198 /* Print warning about variable length array if necessary.  */
6199
6200 static void
6201 warn_variable_length_array (tree name, tree size)
6202 {
6203   if (TREE_CONSTANT (size))
6204     {
6205       if (name)
6206         pedwarn_c90 (input_location, OPT_Wvla,
6207                      "ISO C90 forbids array %qE whose size "
6208                      "cannot be evaluated", name);
6209       else
6210         pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6211                      "whose size cannot be evaluated");
6212     }
6213   else
6214     {
6215       if (name)
6216         pedwarn_c90 (input_location, OPT_Wvla,
6217                      "ISO C90 forbids variable length array %qE", name);
6218       else
6219         pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6220                      "length array");
6221     }
6222 }
6223
6224 /* Print warning about defaulting to int if necessary.  */
6225
6226 static void
6227 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6228 {
6229   diagnostic_info diagnostic;
6230   va_list ap;
6231   rich_location richloc (line_table, location);
6232
6233   va_start (ap, gmsgid);
6234   diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6235                        flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6236   diagnostic.option_index = opt;
6237   diagnostic_report_diagnostic (global_dc, &diagnostic);
6238   va_end (ap);
6239 }
6240
6241 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6242    considering only those c_declspec_words found in LIST, which
6243    must be terminated by cdw_number_of_elements.  */
6244
6245 static location_t
6246 smallest_type_quals_location (const location_t *locations,
6247                               const c_declspec_word *list)
6248 {
6249   location_t loc = UNKNOWN_LOCATION;
6250   while (*list != cdw_number_of_elements)
6251     {
6252       location_t newloc = locations[*list];
6253       if (loc == UNKNOWN_LOCATION
6254           || (newloc != UNKNOWN_LOCATION && newloc < loc))
6255         loc = newloc;
6256       list++;
6257     }
6258
6259   return loc;
6260 }
6261
6262 /* Given declspecs and a declarator,
6263    determine the name and type of the object declared
6264    and construct a ..._DECL node for it.
6265    (In one case we can return a ..._TYPE node instead.
6266     For invalid input we sometimes return NULL_TREE.)
6267
6268    DECLSPECS is a c_declspecs structure for the declaration specifiers.
6269
6270    DECL_CONTEXT says which syntactic context this declaration is in:
6271      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6272      FUNCDEF for a function definition.  Like NORMAL but a few different
6273       error messages in each case.  Return value may be zero meaning
6274       this definition is too screwy to try to parse.
6275      PARM for a parameter declaration (either within a function prototype
6276       or before a function body).  Make a PARM_DECL, or return void_type_node.
6277      TYPENAME if for a typename (in a cast or sizeof).
6278       Don't make a DECL node; just return the ..._TYPE node.
6279      FIELD for a struct or union field; make a FIELD_DECL.
6280    INITIALIZED is true if the decl has an initializer.
6281    WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6282    representing the width of the bit-field.
6283    DECL_ATTRS points to the list of attributes that should be added to this
6284      decl.  Any nested attributes that belong on the decl itself will be
6285      added to this list.
6286    If EXPR is not NULL, any expressions that need to be evaluated as
6287      part of evaluating variably modified types will be stored in *EXPR.
6288    If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6289      set to indicate whether operands in *EXPR can be used in constant
6290      expressions.
6291    DEPRECATED_STATE is a deprecated_states value indicating whether
6292    deprecation/unavailability warnings should be suppressed.
6293
6294    In the TYPENAME case, DECLARATOR is really an absolute declarator.
6295    It may also be so in the PARM case, for a prototype where the
6296    argument type is specified but not the name.
6297
6298    This function is where the complicated C meanings of `static'
6299    and `extern' are interpreted.  */
6300
6301 static tree
6302 grokdeclarator (const struct c_declarator *declarator,
6303                 struct c_declspecs *declspecs,
6304                 enum decl_context decl_context, bool initialized, tree *width,
6305                 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6306                 enum deprecated_states deprecated_state)
6307 {
6308   tree type = declspecs->type;
6309   bool threadp = declspecs->thread_p;
6310   enum c_storage_class storage_class = declspecs->storage_class;
6311   int constp;
6312   int restrictp;
6313   int volatilep;
6314   int atomicp;
6315   int type_quals = TYPE_UNQUALIFIED;
6316   tree name = NULL_TREE;
6317   bool funcdef_flag = false;
6318   bool funcdef_syntax = false;
6319   bool size_varies = false;
6320   tree decl_attr = declspecs->decl_attr;
6321   int array_ptr_quals = TYPE_UNQUALIFIED;
6322   tree array_ptr_attrs = NULL_TREE;
6323   bool array_parm_static = false;
6324   bool array_parm_vla_unspec_p = false;
6325   tree returned_attrs = NULL_TREE;
6326   tree decl_id_attrs = NULL_TREE;
6327   bool bitfield = width != NULL;
6328   tree element_type;
6329   tree orig_qual_type = NULL;
6330   size_t orig_qual_indirect = 0;
6331   struct c_arg_info *arg_info = 0;
6332   addr_space_t as1, as2, address_space;
6333   location_t loc = UNKNOWN_LOCATION;
6334   tree expr_dummy;
6335   bool expr_const_operands_dummy;
6336   enum c_declarator_kind first_non_attr_kind;
6337   unsigned int alignas_align = 0;
6338
6339   if (TREE_CODE (type) == ERROR_MARK)
6340     return error_mark_node;
6341   if (expr == NULL)
6342     {
6343       expr = &expr_dummy;
6344       expr_dummy = NULL_TREE;
6345     }
6346   if (expr_const_operands == NULL)
6347     expr_const_operands = &expr_const_operands_dummy;
6348
6349   if (declspecs->expr)
6350     {
6351       if (*expr)
6352         *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6353                         declspecs->expr);
6354       else
6355         *expr = declspecs->expr;
6356     }
6357   *expr_const_operands = declspecs->expr_const_operands;
6358
6359   if (decl_context == FUNCDEF)
6360     funcdef_flag = true, decl_context = NORMAL;
6361
6362   /* Look inside a declarator for the name being declared
6363      and get it as an IDENTIFIER_NODE, for an error message.  */
6364   {
6365     const struct c_declarator *decl = declarator;
6366
6367     first_non_attr_kind = cdk_attrs;
6368     while (decl)
6369       switch (decl->kind)
6370         {
6371         case cdk_array:
6372           loc = decl->id_loc;
6373           /* FALL THRU.  */
6374
6375         case cdk_function:
6376         case cdk_pointer:
6377           funcdef_syntax = (decl->kind == cdk_function);
6378           if (first_non_attr_kind == cdk_attrs)
6379             first_non_attr_kind = decl->kind;
6380           decl = decl->declarator;
6381           break;
6382
6383         case cdk_attrs:
6384           decl = decl->declarator;
6385           break;
6386
6387         case cdk_id:
6388           loc = decl->id_loc;
6389           if (decl->u.id.id)
6390             name = decl->u.id.id;
6391           decl_id_attrs = decl->u.id.attrs;
6392           if (first_non_attr_kind == cdk_attrs)
6393             first_non_attr_kind = decl->kind;
6394           decl = 0;
6395           break;
6396
6397         default:
6398           gcc_unreachable ();
6399         }
6400     if (name == NULL_TREE)
6401       {
6402         gcc_assert (decl_context == PARM
6403                     || decl_context == TYPENAME
6404                     || (decl_context == FIELD
6405                         && declarator->kind == cdk_id));
6406         gcc_assert (!initialized);
6407       }
6408   }
6409
6410   /* A function definition's declarator must have the form of
6411      a function declarator.  */
6412
6413   if (funcdef_flag && !funcdef_syntax)
6414     return NULL_TREE;
6415
6416   /* If this looks like a function definition, make it one,
6417      even if it occurs where parms are expected.
6418      Then store_parm_decls will reject it and not use it as a parm.  */
6419   if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6420     decl_context = PARM;
6421
6422   if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6423     {
6424       if (declspecs->unavailable_p)
6425         error_unavailable_use (declspecs->type, declspecs->decl_attr);
6426       else if (declspecs->deprecated_p
6427                 && deprecated_state != DEPRECATED_SUPPRESS)
6428         warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6429     }
6430
6431   if ((decl_context == NORMAL || decl_context == FIELD)
6432       && current_scope == file_scope
6433       && variably_modified_type_p (type, NULL_TREE))
6434     {
6435       if (name)
6436         error_at (loc, "variably modified %qE at file scope", name);
6437       else
6438         error_at (loc, "variably modified field at file scope");
6439       type = integer_type_node;
6440     }
6441
6442   size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6443
6444   /* Diagnose defaulting to "int".  */
6445
6446   if (declspecs->default_int_p && !in_system_header_at (input_location))
6447     {
6448       /* Issue a warning if this is an ISO C 99 program or if
6449          -Wreturn-type and this is a function, or if -Wimplicit;
6450          prefer the former warning since it is more explicit.  */
6451       if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6452           && funcdef_flag)
6453         warn_about_return_type = 1;
6454       else
6455         {
6456           if (name)
6457             warn_defaults_to (loc, OPT_Wimplicit_int,
6458                               "type defaults to %<int%> in declaration "
6459                               "of %qE", name);
6460           else
6461             warn_defaults_to (loc, OPT_Wimplicit_int,
6462                               "type defaults to %<int%> in type name");
6463         }
6464     }
6465
6466   /* Adjust the type if a bit-field is being declared,
6467      -funsigned-bitfields applied and the type is not explicitly
6468      "signed".  */
6469   if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6470       && TREE_CODE (type) == INTEGER_TYPE)
6471     type = unsigned_type_for (type);
6472
6473   /* Figure out the type qualifiers for the declaration.  There are
6474      two ways a declaration can become qualified.  One is something
6475      like `const int i' where the `const' is explicit.  Another is
6476      something like `typedef const int CI; CI i' where the type of the
6477      declaration contains the `const'.  A third possibility is that
6478      there is a type qualifier on the element type of a typedefed
6479      array type, in which case we should extract that qualifier so
6480      that c_apply_type_quals_to_decl receives the full list of
6481      qualifiers to work with (C90 is not entirely clear about whether
6482      duplicate qualifiers should be diagnosed in this case, but it
6483      seems most appropriate to do so).  */
6484   element_type = strip_array_types (type);
6485   constp = declspecs->const_p + TYPE_READONLY (element_type);
6486   restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6487   volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6488   atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6489   as1 = declspecs->address_space;
6490   as2 = TYPE_ADDR_SPACE (element_type);
6491   address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6492
6493   if (constp > 1)
6494     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6495   if (restrictp > 1)
6496     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6497   if (volatilep > 1)
6498     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6499   if (atomicp > 1)
6500     pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6501
6502   if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6503     error_at (loc, "conflicting named address spaces (%s vs %s)",
6504               c_addr_space_name (as1), c_addr_space_name (as2));
6505
6506   if ((TREE_CODE (type) == ARRAY_TYPE
6507        || first_non_attr_kind == cdk_array)
6508       && TYPE_QUALS (element_type))
6509     {
6510       orig_qual_type = type;
6511       type = TYPE_MAIN_VARIANT (type);
6512     }
6513   type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6514                 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6515                 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6516                 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6517                 | ENCODE_QUAL_ADDR_SPACE (address_space));
6518   if (type_quals != TYPE_QUALS (element_type))
6519     orig_qual_type = NULL_TREE;
6520
6521   /* Applying the _Atomic qualifier to an array type (through the use
6522      of typedefs or typeof) must be detected here.  If the qualifier
6523      is introduced later, any appearance of applying it to an array is
6524      actually applying it to an element of that array.  */
6525   if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6526     error_at (loc, "%<_Atomic%>-qualified array type");
6527
6528   /* Warn about storage classes that are invalid for certain
6529      kinds of declarations (parameters, typenames, etc.).  */
6530
6531   if (funcdef_flag
6532       && (threadp
6533           || storage_class == csc_auto
6534           || storage_class == csc_register
6535           || storage_class == csc_typedef))
6536     {
6537       if (storage_class == csc_auto)
6538         pedwarn (loc,
6539                  (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6540                  "function definition declared %<auto%>");
6541       if (storage_class == csc_register)
6542         error_at (loc, "function definition declared %<register%>");
6543       if (storage_class == csc_typedef)
6544         error_at (loc, "function definition declared %<typedef%>");
6545       if (threadp)
6546         error_at (loc, "function definition declared %qs",
6547                   declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6548       threadp = false;
6549       if (storage_class == csc_auto
6550           || storage_class == csc_register
6551           || storage_class == csc_typedef)
6552         storage_class = csc_none;
6553     }
6554   else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6555     {
6556       if (decl_context == PARM && storage_class == csc_register)
6557         ;
6558       else
6559         {
6560           switch (decl_context)
6561             {
6562             case FIELD:
6563               if (name)
6564                 error_at (loc, "storage class specified for structure "
6565                           "field %qE", name);
6566               else
6567                 error_at (loc, "storage class specified for structure field");
6568               break;
6569             case PARM:
6570               if (name)
6571                 error_at (loc, "storage class specified for parameter %qE",
6572                           name);
6573               else
6574                 error_at (loc, "storage class specified for unnamed parameter");
6575               break;
6576             default:
6577               error_at (loc, "storage class specified for typename");
6578               break;
6579             }
6580           storage_class = csc_none;
6581           threadp = false;
6582         }
6583     }
6584   else if (storage_class == csc_extern
6585            && initialized
6586            && !funcdef_flag)
6587     {
6588       /* 'extern' with initialization is invalid if not at file scope.  */
6589        if (current_scope == file_scope)
6590          {
6591            /* It is fine to have 'extern const' when compiling at C
6592               and C++ intersection.  */
6593            if (!(warn_cxx_compat && constp))
6594              warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6595                          name);
6596          }
6597       else
6598         error_at (loc, "%qE has both %<extern%> and initializer", name);
6599     }
6600   else if (current_scope == file_scope)
6601     {
6602       if (storage_class == csc_auto)
6603         error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6604                   name);
6605       if (pedantic && storage_class == csc_register)
6606         pedwarn (input_location, OPT_Wpedantic,
6607                  "file-scope declaration of %qE specifies %<register%>", name);
6608     }
6609   else
6610     {
6611       if (storage_class == csc_extern && funcdef_flag)
6612         error_at (loc, "nested function %qE declared %<extern%>", name);
6613       else if (threadp && storage_class == csc_none)
6614         {
6615           error_at (loc, "function-scope %qE implicitly auto and declared "
6616                     "%qs", name,
6617                     declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6618           threadp = false;
6619         }
6620     }
6621
6622   /* Now figure out the structure of the declarator proper.
6623      Descend through it, creating more complex types, until we reach
6624      the declared identifier (or NULL_TREE, in an absolute declarator).
6625      At each stage we maintain an unqualified version of the type
6626      together with any qualifiers that should be applied to it with
6627      c_build_qualified_type; this way, array types including
6628      multidimensional array types are first built up in unqualified
6629      form and then the qualified form is created with
6630      TYPE_MAIN_VARIANT pointing to the unqualified form.  */
6631
6632   while (declarator && declarator->kind != cdk_id)
6633     {
6634       if (type == error_mark_node)
6635         {
6636           declarator = declarator->declarator;
6637           continue;
6638         }
6639
6640       /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6641          a cdk_pointer (for *...),
6642          a cdk_function (for ...(...)),
6643          a cdk_attrs (for nested attributes),
6644          or a cdk_id (for the name being declared
6645          or the place in an absolute declarator
6646          where the name was omitted).
6647          For the last case, we have just exited the loop.
6648
6649          At this point, TYPE is the type of elements of an array,
6650          or for a function to return, or for a pointer to point to.
6651          After this sequence of ifs, TYPE is the type of the
6652          array or function or pointer, and DECLARATOR has had its
6653          outermost layer removed.  */
6654
6655       if (array_ptr_quals != TYPE_UNQUALIFIED
6656           || array_ptr_attrs != NULL_TREE
6657           || array_parm_static)
6658         {
6659           /* Only the innermost declarator (making a parameter be of
6660              array type which is converted to pointer type)
6661              may have static or type qualifiers.  */
6662           error_at (loc, "static or type qualifiers in non-parameter array declarator");
6663           array_ptr_quals = TYPE_UNQUALIFIED;
6664           array_ptr_attrs = NULL_TREE;
6665           array_parm_static = false;
6666         }
6667
6668       switch (declarator->kind)
6669         {
6670         case cdk_attrs:
6671           {
6672             /* A declarator with embedded attributes.  */
6673             tree attrs = declarator->u.attrs;
6674             const struct c_declarator *inner_decl;
6675             int attr_flags = 0;
6676             declarator = declarator->declarator;
6677             /* Standard attribute syntax precisely defines what entity
6678                an attribute in each position appertains to, so only
6679                apply laxity about positioning to GNU attribute syntax.
6680                Standard attributes applied to a function or array
6681                declarator apply exactly to that type; standard
6682                attributes applied to the identifier apply to the
6683                declaration rather than to the type, and are specified
6684                using a cdk_id declarator rather than using
6685                cdk_attrs.  */
6686             inner_decl = declarator;
6687             while (inner_decl->kind == cdk_attrs)
6688               inner_decl = inner_decl->declarator;
6689             if (!cxx11_attribute_p (attrs))
6690               {
6691                 if (inner_decl->kind == cdk_id)
6692                   attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6693                 else if (inner_decl->kind == cdk_function)
6694                   attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6695                 else if (inner_decl->kind == cdk_array)
6696                   attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6697               }
6698             attrs = c_warn_type_attributes (attrs);
6699             returned_attrs = decl_attributes (&type,
6700                                               chainon (returned_attrs, attrs),
6701                                               attr_flags);
6702             break;
6703           }
6704         case cdk_array:
6705           {
6706             tree itype = NULL_TREE;
6707             tree size = declarator->u.array.dimen;
6708             /* The index is a signed object `sizetype' bits wide.  */
6709             tree index_type = c_common_signed_type (sizetype);
6710
6711             array_ptr_quals = declarator->u.array.quals;
6712             array_ptr_attrs = declarator->u.array.attrs;
6713             array_parm_static = declarator->u.array.static_p;
6714             array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6715
6716             declarator = declarator->declarator;
6717
6718             /* Check for some types that there cannot be arrays of.  */
6719
6720             if (VOID_TYPE_P (type))
6721               {
6722                 if (name)
6723                   error_at (loc, "declaration of %qE as array of voids", name);
6724                 else
6725                   error_at (loc, "declaration of type name as array of voids");
6726                 type = error_mark_node;
6727               }
6728
6729             if (TREE_CODE (type) == FUNCTION_TYPE)
6730               {
6731                 if (name)
6732                   error_at (loc, "declaration of %qE as array of functions",
6733                             name);
6734                 else
6735                   error_at (loc, "declaration of type name as array of "
6736                             "functions");
6737                 type = error_mark_node;
6738               }
6739
6740             if (pedantic && !in_system_header_at (input_location)
6741                 && flexible_array_type_p (type))
6742               pedwarn (loc, OPT_Wpedantic,
6743                        "invalid use of structure with flexible array member");
6744
6745             if (size == error_mark_node)
6746               type = error_mark_node;
6747
6748             if (type == error_mark_node)
6749               continue;
6750
6751             if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6752               {
6753                 type = error_mark_node;
6754                 continue;
6755               }
6756
6757             /* If size was specified, set ITYPE to a range-type for
6758                that size.  Otherwise, ITYPE remains null.  finish_decl
6759                may figure it out from an initial value.  */
6760
6761             if (size)
6762               {
6763                 bool size_maybe_const = true;
6764                 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6765                                        && !TREE_OVERFLOW (size));
6766                 bool this_size_varies = false;
6767
6768                 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6769                    lvalue.  */
6770                 STRIP_TYPE_NOPS (size);
6771
6772                 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6773                   {
6774                     if (name)
6775                       error_at (loc, "size of array %qE has non-integer type",
6776                                 name);
6777                     else
6778                       error_at (loc,
6779                                 "size of unnamed array has non-integer type");
6780                     size = integer_one_node;
6781                     size_int_const = true;
6782                   }
6783                 /* This can happen with enum forward declaration.  */
6784                 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6785                   {
6786                     if (name)
6787                       error_at (loc, "size of array %qE has incomplete type",
6788                                 name);
6789                     else
6790                       error_at (loc, "size of unnamed array has incomplete "
6791                                 "type");
6792                     size = integer_one_node;
6793                     size_int_const = true;
6794                   }
6795
6796                 size = c_fully_fold (size, false, &size_maybe_const);
6797
6798                 if (pedantic && size_maybe_const && integer_zerop (size))
6799                   {
6800                     if (name)
6801                       pedwarn (loc, OPT_Wpedantic,
6802                                "ISO C forbids zero-size array %qE", name);
6803                     else
6804                       pedwarn (loc, OPT_Wpedantic,
6805                                "ISO C forbids zero-size array");
6806                   }
6807
6808                 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6809                   {
6810                     constant_expression_warning (size);
6811                     if (tree_int_cst_sgn (size) < 0)
6812                       {
6813                         if (name)
6814                           error_at (loc, "size of array %qE is negative", name);
6815                         else
6816                           error_at (loc, "size of unnamed array is negative");
6817                         size = integer_one_node;
6818                         size_int_const = true;
6819                       }
6820                     /* Handle a size folded to an integer constant but
6821                        not an integer constant expression.  */
6822                     if (!size_int_const)
6823                       {
6824                         /* If this is a file scope declaration of an
6825                            ordinary identifier, this is invalid code;
6826                            diagnosing it here and not subsequently
6827                            treating the type as variable-length avoids
6828                            more confusing diagnostics later.  */
6829                         if ((decl_context == NORMAL || decl_context == FIELD)
6830                             && current_scope == file_scope)
6831                           pedwarn (input_location, 0,
6832                                    "variably modified %qE at file scope",
6833                                    name);
6834                         else
6835                           this_size_varies = size_varies = true;
6836                         warn_variable_length_array (name, size);
6837                       }
6838                   }
6839                 else if ((decl_context == NORMAL || decl_context == FIELD)
6840                          && current_scope == file_scope)
6841                   {
6842                     error_at (loc, "variably modified %qE at file scope", name);
6843                     size = integer_one_node;
6844                   }
6845                 else
6846                   {
6847                     /* Make sure the array size remains visibly
6848                        nonconstant even if it is (eg) a const variable
6849                        with known value.  */
6850                     this_size_varies = size_varies = true;
6851                     warn_variable_length_array (name, size);
6852                     if (sanitize_flags_p (SANITIZE_VLA)
6853                         && current_function_decl != NULL_TREE
6854                         && decl_context == NORMAL)
6855                       {
6856                         /* Evaluate the array size only once.  */
6857                         size = save_expr (size);
6858                         size = c_fully_fold (size, false, NULL);
6859                         size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6860                                             ubsan_instrument_vla (loc, size),
6861                                             size);
6862                       }
6863                   }
6864
6865                 if (integer_zerop (size) && !this_size_varies)
6866                   {
6867                     /* A zero-length array cannot be represented with
6868                        an unsigned index type, which is what we'll
6869                        get with build_index_type.  Create an
6870                        open-ended range instead.  */
6871                     itype = build_range_type (sizetype, size, NULL_TREE);
6872                   }
6873                 else
6874                   {
6875                     /* Arrange for the SAVE_EXPR on the inside of the
6876                        MINUS_EXPR, which allows the -1 to get folded
6877                        with the +1 that happens when building TYPE_SIZE.  */
6878                     if (size_varies)
6879                       size = save_expr (size);
6880                     if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6881                       size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6882                                      integer_zero_node, size);
6883
6884                     /* Compute the maximum valid index, that is, size
6885                        - 1.  Do the calculation in index_type, so that
6886                        if it is a variable the computations will be
6887                        done in the proper mode.  */
6888                     itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6889                                              convert (index_type, size),
6890                                              convert (index_type,
6891                                                       size_one_node));
6892
6893                     /* The above overflows when size does not fit
6894                        in index_type.
6895                        ???  While a size of INT_MAX+1 technically shouldn't
6896                        cause an overflow (because we subtract 1), handling
6897                        this case seems like an unnecessary complication.  */
6898                     if (TREE_CODE (size) == INTEGER_CST
6899                         && !int_fits_type_p (size, index_type))
6900                       {
6901                         if (name)
6902                           error_at (loc, "size of array %qE is too large",
6903                                     name);
6904                         else
6905                           error_at (loc, "size of unnamed array is too large");
6906                         type = error_mark_node;
6907                         continue;
6908                       }
6909
6910                     itype = build_index_type (itype);
6911                   }
6912                 if (this_size_varies)
6913                   {
6914                     if (TREE_SIDE_EFFECTS (size))
6915                       {
6916                         if (*expr)
6917                           *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6918                                           *expr, size);
6919                         else
6920                           *expr = size;
6921                       }
6922                     *expr_const_operands &= size_maybe_const;
6923                   }
6924               }
6925             else if (decl_context == FIELD)
6926               {
6927                 bool flexible_array_member = false;
6928                 if (array_parm_vla_unspec_p)
6929                   /* Field names can in fact have function prototype
6930                      scope so [*] is disallowed here through making
6931                      the field variably modified, not through being
6932                      something other than a declaration with function
6933                      prototype scope.  */
6934                   size_varies = true;
6935                 else
6936                   {
6937                     const struct c_declarator *t = declarator;
6938                     while (t->kind == cdk_attrs)
6939                       t = t->declarator;
6940                     flexible_array_member = (t->kind == cdk_id);
6941                   }
6942                 if (flexible_array_member
6943                     && !in_system_header_at (input_location))
6944                   pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6945                                "support flexible array members");
6946
6947                 /* ISO C99 Flexible array members are effectively
6948                    identical to GCC's zero-length array extension.  */
6949                 if (flexible_array_member || array_parm_vla_unspec_p)
6950                   itype = build_range_type (sizetype, size_zero_node,
6951                                             NULL_TREE);
6952               }
6953             else if (decl_context == PARM)
6954               {
6955                 if (array_parm_vla_unspec_p)
6956                   {
6957                     itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6958                     size_varies = true;
6959                   }
6960               }
6961             else if (decl_context == TYPENAME)
6962               {
6963                 if (array_parm_vla_unspec_p)
6964                   {
6965                     /* C99 6.7.5.2p4 */
6966                     warning (0, "%<[*]%> not in a declaration");
6967                     /* We use this to avoid messing up with incomplete
6968                        array types of the same type, that would
6969                        otherwise be modified below.  */
6970                     itype = build_range_type (sizetype, size_zero_node,
6971                                               NULL_TREE);
6972                     size_varies = true;
6973                   }
6974               }
6975
6976             /* Complain about arrays of incomplete types.  */
6977             if (!COMPLETE_TYPE_P (type))
6978               {
6979                 auto_diagnostic_group d;
6980                 error_at (loc, "array type has incomplete element type %qT",
6981                           type);
6982                 /* See if we can be more helpful.  */
6983                 if (TREE_CODE (type) == ARRAY_TYPE)
6984                   {
6985                     if (name)
6986                       inform (loc, "declaration of %qE as multidimensional "
6987                               "array must have bounds for all dimensions "
6988                               "except the first", name);
6989                     else
6990                       inform (loc, "declaration of multidimensional array "
6991                               "must have bounds for all dimensions except "
6992                               "the first");
6993                   }
6994                 type = error_mark_node;
6995               }
6996             else
6997             /* When itype is NULL, a shared incomplete array type is
6998                returned for all array of a given type.  Elsewhere we
6999                make sure we don't complete that type before copying
7000                it, but here we want to make sure we don't ever
7001                modify the shared type, so we gcc_assert (itype)
7002                below.  */
7003               {
7004                 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7005                 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7006                   type = build_qualified_type (type,
7007                                                ENCODE_QUAL_ADDR_SPACE (as));
7008
7009                 type = build_array_type (type, itype);
7010               }
7011
7012             if (type != error_mark_node)
7013               {
7014                 if (size_varies)
7015                   {
7016                     /* It is ok to modify type here even if itype is
7017                        NULL: if size_varies, we're in a
7018                        multi-dimensional array and the inner type has
7019                        variable size, so the enclosing shared array type
7020                        must too.  */
7021                     if (size && TREE_CODE (size) == INTEGER_CST)
7022                       type
7023                         = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7024                     C_TYPE_VARIABLE_SIZE (type) = 1;
7025                   }
7026
7027                 /* The GCC extension for zero-length arrays differs from
7028                    ISO flexible array members in that sizeof yields
7029                    zero.  */
7030                 if (size && integer_zerop (size))
7031                   {
7032                     gcc_assert (itype);
7033                     type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7034                     TYPE_SIZE (type) = bitsize_zero_node;
7035                     TYPE_SIZE_UNIT (type) = size_zero_node;
7036                     SET_TYPE_STRUCTURAL_EQUALITY (type);
7037                   }
7038                 if (array_parm_vla_unspec_p)
7039                   {
7040                     gcc_assert (itype);
7041                     /* The type is complete.  C99 6.7.5.2p4  */
7042                     type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7043                     TYPE_SIZE (type) = bitsize_zero_node;
7044                     TYPE_SIZE_UNIT (type) = size_zero_node;
7045                     SET_TYPE_STRUCTURAL_EQUALITY (type);
7046                   }
7047
7048                 if (!valid_array_size_p (loc, type, name))
7049                   type = error_mark_node;
7050               }
7051
7052             if (decl_context != PARM
7053                 && (array_ptr_quals != TYPE_UNQUALIFIED
7054                     || array_ptr_attrs != NULL_TREE
7055                     || array_parm_static))
7056               {
7057                 error_at (loc, "static or type qualifiers in non-parameter "
7058                           "array declarator");
7059                 array_ptr_quals = TYPE_UNQUALIFIED;
7060                 array_ptr_attrs = NULL_TREE;
7061                 array_parm_static = false;
7062               }
7063             orig_qual_indirect++;
7064             break;
7065           }
7066         case cdk_function:
7067           {
7068             /* Say it's a definition only for the declarator closest
7069                to the identifier, apart possibly from some
7070                attributes.  */
7071             bool really_funcdef = false;
7072             tree arg_types;
7073             orig_qual_type = NULL_TREE;
7074             if (funcdef_flag)
7075               {
7076                 const struct c_declarator *t = declarator->declarator;
7077                 while (t->kind == cdk_attrs)
7078                   t = t->declarator;
7079                 really_funcdef = (t->kind == cdk_id);
7080               }
7081
7082             /* Declaring a function type.  Make sure we have a valid
7083                type for the function to return.  */
7084             if (type == error_mark_node)
7085               continue;
7086
7087             size_varies = false;
7088
7089             /* Warn about some types functions can't return.  */
7090             if (TREE_CODE (type) == FUNCTION_TYPE)
7091               {
7092                 if (name)
7093                   error_at (loc, "%qE declared as function returning a "
7094                                  "function", name);
7095                 else
7096                   error_at (loc, "type name declared as function "
7097                             "returning a function");
7098                 type = integer_type_node;
7099               }
7100             if (TREE_CODE (type) == ARRAY_TYPE)
7101               {
7102                 if (name)
7103                   error_at (loc, "%qE declared as function returning an array",
7104                             name);
7105                 else
7106                   error_at (loc, "type name declared as function returning "
7107                             "an array");
7108                 type = integer_type_node;
7109               }
7110
7111             /* Construct the function type and go to the next
7112                inner layer of declarator.  */
7113             arg_info = declarator->u.arg_info;
7114             arg_types = grokparms (arg_info, really_funcdef);
7115
7116             /* Type qualifiers before the return type of the function
7117                qualify the return type, not the function type.  */
7118             if (type_quals)
7119               {
7120                 const enum c_declspec_word ignored_quals_list[] =
7121                   {
7122                     cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7123                     cdw_atomic, cdw_number_of_elements
7124                   };
7125                 location_t specs_loc
7126                   = smallest_type_quals_location (declspecs->locations,
7127                                                   ignored_quals_list);
7128                 if (specs_loc == UNKNOWN_LOCATION)
7129                   specs_loc = declspecs->locations[cdw_typedef];
7130                 if (specs_loc == UNKNOWN_LOCATION)
7131                   specs_loc = loc;
7132
7133                 /* Type qualifiers on a function return type are
7134                    normally permitted by the standard but have no
7135                    effect, so give a warning at -Wreturn-type.
7136                    Qualifiers on a void return type are banned on
7137                    function definitions in ISO C; GCC used to used
7138                    them for noreturn functions.  The resolution of C11
7139                    DR#423 means qualifiers (other than _Atomic) are
7140                    actually removed from the return type when
7141                    determining the function type.  */
7142                 int quals_used = type_quals;
7143                 if (flag_isoc11)
7144                   quals_used &= TYPE_QUAL_ATOMIC;
7145                 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7146                   pedwarn (specs_loc, 0,
7147                            "function definition has qualified void "
7148                            "return type");
7149                 else
7150                   warning_at (specs_loc, OPT_Wignored_qualifiers,
7151                               "type qualifiers ignored on function "
7152                               "return type");
7153
7154                 /* Ensure an error for restrict on invalid types; the
7155                    DR#423 resolution is not entirely clear about
7156                    this.  */
7157                 if (flag_isoc11
7158                     && (type_quals & TYPE_QUAL_RESTRICT)
7159                     && (!POINTER_TYPE_P (type)
7160                         || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7161                   error_at (loc, "invalid use of %<restrict%>");
7162                 type = c_build_qualified_type (type, quals_used);
7163               }
7164             type_quals = TYPE_UNQUALIFIED;
7165
7166             type = build_function_type (type, arg_types);
7167             declarator = declarator->declarator;
7168
7169             /* Set the TYPE_CONTEXTs for each tagged type which is local to
7170                the formal parameter list of this FUNCTION_TYPE to point to
7171                the FUNCTION_TYPE node itself.  */
7172             {
7173               c_arg_tag *tag;
7174               unsigned ix;
7175
7176               FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7177                 TYPE_CONTEXT (tag->type) = type;
7178             }
7179             break;
7180           }
7181         case cdk_pointer:
7182           {
7183             /* Merge any constancy or volatility into the target type
7184                for the pointer.  */
7185             if ((type_quals & TYPE_QUAL_ATOMIC)
7186                 && TREE_CODE (type) == FUNCTION_TYPE)
7187               {
7188                 error_at (loc,
7189                           "%<_Atomic%>-qualified function type");
7190                 type_quals &= ~TYPE_QUAL_ATOMIC;
7191               }
7192             else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7193                      && type_quals)
7194               pedwarn (loc, OPT_Wpedantic,
7195                        "ISO C forbids qualified function types");
7196             if (type_quals)
7197               type = c_build_qualified_type (type, type_quals, orig_qual_type,
7198                                              orig_qual_indirect);
7199             orig_qual_type = NULL_TREE;
7200             size_varies = false;
7201
7202             /* When the pointed-to type involves components of variable size,
7203                care must be taken to ensure that the size evaluation code is
7204                emitted early enough to dominate all the possible later uses
7205                and late enough for the variables on which it depends to have
7206                been assigned.
7207
7208                This is expected to happen automatically when the pointed-to
7209                type has a name/declaration of it's own, but special attention
7210                is required if the type is anonymous.
7211
7212                We attach an artificial TYPE_DECL to such pointed-to type
7213                and arrange for it to be included in a DECL_EXPR.  This
7214                forces the sizes evaluation at a safe point and ensures it
7215                is not deferred until e.g. within a deeper conditional context.
7216
7217                PARM contexts have no enclosing statement list that
7218                can hold the DECL_EXPR, so we need to use a BIND_EXPR
7219                instead, and add it to the list of expressions that
7220                need to be evaluated.
7221
7222                TYPENAME contexts do have an enclosing statement list,
7223                but it would be incorrect to use it, as the size should
7224                only be evaluated if the containing expression is
7225                evaluated.  We might also be in the middle of an
7226                expression with side effects on the pointed-to type size
7227                "arguments" prior to the pointer declaration point and
7228                the fake TYPE_DECL in the enclosing context would force
7229                the size evaluation prior to the side effects.  We therefore
7230                use BIND_EXPRs in TYPENAME contexts too.  */
7231             if (!TYPE_NAME (type)
7232                 && variably_modified_type_p (type, NULL_TREE))
7233               {
7234                 tree bind = NULL_TREE;
7235                 if (decl_context == TYPENAME || decl_context == PARM)
7236                   {
7237                     bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7238                                    NULL_TREE, NULL_TREE);
7239                     TREE_SIDE_EFFECTS (bind) = 1;
7240                     BIND_EXPR_BODY (bind) = push_stmt_list ();
7241                     push_scope ();
7242                   }
7243                 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7244                 DECL_ARTIFICIAL (decl) = 1;
7245                 pushdecl (decl);
7246                 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7247                 TYPE_NAME (type) = decl;
7248                 if (bind)
7249                   {
7250                     pop_scope ();
7251                     BIND_EXPR_BODY (bind)
7252                       = pop_stmt_list (BIND_EXPR_BODY (bind));
7253                     if (*expr)
7254                       *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7255                                       bind);
7256                     else
7257                       *expr = bind;
7258                   }
7259               }
7260
7261             type = c_build_pointer_type (type);
7262
7263             /* Process type qualifiers (such as const or volatile)
7264                that were given inside the `*'.  */
7265             type_quals = declarator->u.pointer_quals;
7266
7267             declarator = declarator->declarator;
7268             break;
7269           }
7270         default:
7271           gcc_unreachable ();
7272         }
7273     }
7274   *decl_attrs = chainon (returned_attrs, *decl_attrs);
7275   *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7276
7277   /* Now TYPE has the actual type, apart from any qualifiers in
7278      TYPE_QUALS.  */
7279
7280   /* Warn about address space used for things other than static memory or
7281      pointers.  */
7282   address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7283   if (!ADDR_SPACE_GENERIC_P (address_space))
7284     {
7285       if (decl_context == NORMAL)
7286         {
7287           switch (storage_class)
7288             {
7289             case csc_auto:
7290               error ("%qs combined with %<auto%> qualifier for %qE",
7291                      c_addr_space_name (address_space), name);
7292               break;
7293             case csc_register:
7294               error ("%qs combined with %<register%> qualifier for %qE",
7295                      c_addr_space_name (address_space), name);
7296               break;
7297             case csc_none:
7298               if (current_function_scope)
7299                 {
7300                   error ("%qs specified for auto variable %qE",
7301                          c_addr_space_name (address_space), name);
7302                   break;
7303                 }
7304               break;
7305             case csc_static:
7306             case csc_extern:
7307             case csc_typedef:
7308               break;
7309             default:
7310               gcc_unreachable ();
7311             }
7312         }
7313       else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7314         {
7315           if (name)
7316             error ("%qs specified for parameter %qE",
7317                    c_addr_space_name (address_space), name);
7318           else
7319             error ("%qs specified for unnamed parameter",
7320                    c_addr_space_name (address_space));
7321         }
7322       else if (decl_context == FIELD)
7323         {
7324           if (name)
7325             error ("%qs specified for structure field %qE",
7326                    c_addr_space_name (address_space), name);
7327           else
7328             error ("%qs specified for structure field",
7329                    c_addr_space_name (address_space));
7330         }
7331     }
7332
7333   /* Check the type and width of a bit-field.  */
7334   if (bitfield)
7335     {
7336       check_bitfield_type_and_width (loc, &type, width, name);
7337       /* C11 makes it implementation-defined (6.7.2.1#5) whether
7338          atomic types are permitted for bit-fields; we have no code to
7339          make bit-field accesses atomic, so disallow them.  */
7340       if (type_quals & TYPE_QUAL_ATOMIC)
7341         {
7342           if (name)
7343             error_at (loc, "bit-field %qE has atomic type", name);
7344           else
7345             error_at (loc, "bit-field has atomic type");
7346           type_quals &= ~TYPE_QUAL_ATOMIC;
7347         }
7348     }
7349
7350   /* Reject invalid uses of _Alignas.  */
7351   if (declspecs->alignas_p)
7352     {
7353       if (storage_class == csc_typedef)
7354         error_at (loc, "alignment specified for typedef %qE", name);
7355       else if (storage_class == csc_register)
7356         error_at (loc, "alignment specified for %<register%> object %qE",
7357                   name);
7358       else if (decl_context == PARM)
7359         {
7360           if (name)
7361             error_at (loc, "alignment specified for parameter %qE", name);
7362           else
7363             error_at (loc, "alignment specified for unnamed parameter");
7364         }
7365       else if (bitfield)
7366         {
7367           if (name)
7368             error_at (loc, "alignment specified for bit-field %qE", name);
7369           else
7370             error_at (loc, "alignment specified for unnamed bit-field");
7371         }
7372       else if (TREE_CODE (type) == FUNCTION_TYPE)
7373         error_at (loc, "alignment specified for function %qE", name);
7374       else if (declspecs->align_log != -1 && TYPE_P (type))
7375         {
7376           alignas_align = 1U << declspecs->align_log;
7377           if (alignas_align < min_align_of_type (type))
7378             {
7379               if (name)
7380                 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7381                           "alignment of %qE", name);
7382               else
7383                 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7384                           "alignment of unnamed field");
7385               alignas_align = 0;
7386             }
7387         }
7388     }
7389
7390   /* If this is declaring a typedef name, return a TYPE_DECL.  */
7391
7392   if (storage_class == csc_typedef)
7393     {
7394       tree decl;
7395       if ((type_quals & TYPE_QUAL_ATOMIC)
7396           && TREE_CODE (type) == FUNCTION_TYPE)
7397         {
7398           error_at (loc,
7399                     "%<_Atomic%>-qualified function type");
7400           type_quals &= ~TYPE_QUAL_ATOMIC;
7401         }
7402       else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7403                && type_quals)
7404         pedwarn (loc, OPT_Wpedantic,
7405                  "ISO C forbids qualified function types");
7406       if (type_quals)
7407         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7408                                        orig_qual_indirect);
7409       decl = build_decl (declarator->id_loc,
7410                          TYPE_DECL, declarator->u.id.id, type);
7411       if (declspecs->explicit_signed_p)
7412         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7413       if (declspecs->inline_p)
7414         pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7415       if (declspecs->noreturn_p)
7416         pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7417
7418       if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7419         {
7420           struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7421
7422           if (b != NULL
7423               && b->decl != NULL_TREE
7424               && (B_IN_CURRENT_SCOPE (b)
7425                   || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7426               && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7427             {
7428               auto_diagnostic_group d;
7429               if (warning_at (declarator->id_loc, OPT_Wc___compat,
7430                               ("using %qD as both a typedef and a tag is "
7431                                "invalid in C++"), decl)
7432                   && b->locus != UNKNOWN_LOCATION)
7433                 inform (b->locus, "originally defined here");
7434             }
7435         }
7436
7437       return decl;
7438     }
7439
7440   /* If this is a type name (such as, in a cast or sizeof),
7441      compute the type and return it now.  */
7442
7443   if (decl_context == TYPENAME)
7444     {
7445       /* Note that the grammar rejects storage classes in typenames
7446          and fields.  */
7447       gcc_assert (storage_class == csc_none && !threadp
7448                   && !declspecs->inline_p && !declspecs->noreturn_p);
7449       if ((type_quals & TYPE_QUAL_ATOMIC)
7450           && TREE_CODE (type) == FUNCTION_TYPE)
7451         {
7452           error_at (loc,
7453                     "%<_Atomic%>-qualified function type");
7454           type_quals &= ~TYPE_QUAL_ATOMIC;
7455         }
7456       else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7457                && type_quals)
7458         pedwarn (loc, OPT_Wpedantic,
7459                  "ISO C forbids const or volatile function types");
7460       if (type_quals)
7461         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7462                                        orig_qual_indirect);
7463       return type;
7464     }
7465
7466   if (pedantic && decl_context == FIELD
7467       && variably_modified_type_p (type, NULL_TREE))
7468     {
7469       /* C99 6.7.2.1p8 */
7470       pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7471                "have a variably modified type");
7472     }
7473
7474   /* Aside from typedefs and type names (handle above),
7475      `void' at top level (not within pointer)
7476      is allowed only in public variables.
7477      We don't complain about parms either, but that is because
7478      a better error message can be made later.  */
7479
7480   if (VOID_TYPE_P (type) && decl_context != PARM
7481       && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7482             && (storage_class == csc_extern
7483                 || (current_scope == file_scope
7484                     && !(storage_class == csc_static
7485                          || storage_class == csc_register)))))
7486     {
7487       error_at (loc, "variable or field %qE declared void", name);
7488       type = integer_type_node;
7489     }
7490
7491   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7492      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
7493
7494   {
7495     tree decl;
7496
7497     if (decl_context == PARM)
7498       {
7499         tree promoted_type;
7500         bool array_parameter_p = false;
7501
7502         /* A parameter declared as an array of T is really a pointer to T.
7503            One declared as a function is really a pointer to a function.  */
7504
7505         if (TREE_CODE (type) == ARRAY_TYPE)
7506           {
7507             /* Transfer const-ness of array into that of type pointed to.  */
7508             type = TREE_TYPE (type);
7509             if (orig_qual_type != NULL_TREE)
7510               {
7511                 if (orig_qual_indirect == 0)
7512                   orig_qual_type = TREE_TYPE (orig_qual_type);
7513                 else
7514                   orig_qual_indirect--;
7515               }
7516             if (type_quals)
7517               type = c_build_qualified_type (type, type_quals, orig_qual_type,
7518                                              orig_qual_indirect);
7519             type = c_build_pointer_type (type);
7520             type_quals = array_ptr_quals;
7521             if (type_quals)
7522               type = c_build_qualified_type (type, type_quals);
7523
7524             /* We don't yet implement attributes in this context.  */
7525             if (array_ptr_attrs != NULL_TREE)
7526               warning_at (loc, OPT_Wattributes,
7527                           "attributes in parameter array declarator ignored");
7528
7529             size_varies = false;
7530             array_parameter_p = true;
7531           }
7532         else if (TREE_CODE (type) == FUNCTION_TYPE)
7533           {
7534             if (type_quals & TYPE_QUAL_ATOMIC)
7535               {
7536                 error_at (loc,
7537                           "%<_Atomic%>-qualified function type");
7538                 type_quals &= ~TYPE_QUAL_ATOMIC;
7539               }
7540             else if (type_quals)
7541               pedwarn (loc, OPT_Wpedantic,
7542                        "ISO C forbids qualified function types");
7543             if (type_quals)
7544               type = c_build_qualified_type (type, type_quals);
7545             type = c_build_pointer_type (type);
7546             type_quals = TYPE_UNQUALIFIED;
7547           }
7548         else if (type_quals)
7549           type = c_build_qualified_type (type, type_quals);
7550
7551         decl = build_decl (declarator->id_loc,
7552                            PARM_DECL, declarator->u.id.id, type);
7553         if (size_varies)
7554           C_DECL_VARIABLE_SIZE (decl) = 1;
7555         C_ARRAY_PARAMETER (decl) = array_parameter_p;
7556
7557         /* Compute the type actually passed in the parmlist,
7558            for the case where there is no prototype.
7559            (For example, shorts and chars are passed as ints.)
7560            When there is a prototype, this is overridden later.  */
7561
7562         if (type == error_mark_node)
7563           promoted_type = type;
7564         else
7565           promoted_type = c_type_promotes_to (type);
7566
7567         DECL_ARG_TYPE (decl) = promoted_type;
7568         if (declspecs->inline_p)
7569           pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7570         if (declspecs->noreturn_p)
7571           pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7572       }
7573     else if (decl_context == FIELD)
7574       {
7575         /* Note that the grammar rejects storage classes in typenames
7576            and fields.  */
7577         gcc_assert (storage_class == csc_none && !threadp
7578                     && !declspecs->inline_p && !declspecs->noreturn_p);
7579
7580         /* Structure field.  It may not be a function.  */
7581
7582         if (TREE_CODE (type) == FUNCTION_TYPE)
7583           {
7584             error_at (loc, "field %qE declared as a function", name);
7585             type = build_pointer_type (type);
7586           }
7587         else if (TREE_CODE (type) != ERROR_MARK
7588                  && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7589           {
7590             if (name)
7591               error_at (loc, "field %qE has incomplete type", name);
7592             else
7593               error_at (loc, "unnamed field has incomplete type");
7594             type = error_mark_node;
7595           }
7596         else if (TREE_CODE (type) == ARRAY_TYPE
7597                  && TYPE_DOMAIN (type) == NULL_TREE)
7598           {
7599             /* We have a flexible array member through a typedef.
7600                Set suitable range.  Whether this is a correct position
7601                for a flexible array member will be determined elsewhere.  */
7602             if (!in_system_header_at (input_location))
7603               pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7604                            "support flexible array members");
7605             type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7606             TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7607                                                    NULL_TREE);
7608             if (orig_qual_indirect == 0)
7609               orig_qual_type = NULL_TREE;
7610           }
7611         if (type != error_mark_node
7612             && !verify_type_context (loc, TCTX_FIELD, type))
7613           type = error_mark_node;
7614
7615         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7616                                        orig_qual_indirect);
7617         decl = build_decl (declarator->id_loc,
7618                            FIELD_DECL, declarator->u.id.id, type);
7619         DECL_NONADDRESSABLE_P (decl) = bitfield;
7620         if (bitfield && !declarator->u.id.id)
7621           DECL_PADDING_P (decl) = 1;
7622
7623         if (size_varies)
7624           C_DECL_VARIABLE_SIZE (decl) = 1;
7625       }
7626     else if (TREE_CODE (type) == FUNCTION_TYPE)
7627       {
7628         if (storage_class == csc_register || threadp)
7629           {
7630             error_at (loc, "invalid storage class for function %qE", name);
7631           }
7632         else if (current_scope != file_scope)
7633           {
7634             /* Function declaration not at file scope.  Storage
7635                classes other than `extern' are not allowed, C99
7636                6.7.1p5, and `extern' makes no difference.  However,
7637                GCC allows 'auto', perhaps with 'inline', to support
7638                nested functions.  */
7639             if (storage_class == csc_auto)
7640                 pedwarn (loc, OPT_Wpedantic,
7641                          "invalid storage class for function %qE", name);
7642             else if (storage_class == csc_static)
7643               {
7644                 error_at (loc, "invalid storage class for function %qE", name);
7645                 if (funcdef_flag)
7646                   storage_class = declspecs->storage_class = csc_none;
7647                 else
7648                   return NULL_TREE;
7649               }
7650           }
7651
7652         decl = build_decl (declarator->id_loc,
7653                            FUNCTION_DECL, declarator->u.id.id, type);
7654         decl = build_decl_attribute_variant (decl, decl_attr);
7655
7656         if (type_quals & TYPE_QUAL_ATOMIC)
7657           {
7658             error_at (loc,
7659                       "%<_Atomic%>-qualified function type");
7660             type_quals &= ~TYPE_QUAL_ATOMIC;
7661           }
7662         else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7663           pedwarn (loc, OPT_Wpedantic,
7664                    "ISO C forbids qualified function types");
7665
7666         /* Every function declaration is an external reference
7667            (DECL_EXTERNAL) except for those which are not at file
7668            scope and are explicitly declared "auto".  This is
7669            forbidden by standard C (C99 6.7.1p5) and is interpreted by
7670            GCC to signify a forward declaration of a nested function.  */
7671         if (storage_class == csc_auto && current_scope != file_scope)
7672           DECL_EXTERNAL (decl) = 0;
7673         /* In C99, a function which is declared 'inline' with 'extern'
7674            is not an external reference (which is confusing).  It
7675            means that the later definition of the function must be output
7676            in this file, C99 6.7.4p6.  In GNU C89, a function declared
7677            'extern inline' is an external reference.  */
7678         else if (declspecs->inline_p && storage_class != csc_static)
7679           DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7680                                   == flag_gnu89_inline);
7681         else
7682           DECL_EXTERNAL (decl) = !initialized;
7683
7684         /* Record absence of global scope for `static' or `auto'.  */
7685         TREE_PUBLIC (decl)
7686           = !(storage_class == csc_static || storage_class == csc_auto);
7687
7688         /* For a function definition, record the argument information
7689            block where store_parm_decls will look for it.  */
7690         if (funcdef_flag)
7691           current_function_arg_info = arg_info;
7692
7693         if (declspecs->default_int_p)
7694           C_FUNCTION_IMPLICIT_INT (decl) = 1;
7695
7696         /* Record presence of `inline' and `_Noreturn', if it is
7697            reasonable.  */
7698         if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7699           {
7700             if (declspecs->inline_p)
7701               pedwarn (loc, 0, "cannot inline function %<main%>");
7702             if (declspecs->noreturn_p)
7703               pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7704           }
7705         else
7706           {
7707             if (declspecs->inline_p)
7708               /* Record that the function is declared `inline'.  */
7709               DECL_DECLARED_INLINE_P (decl) = 1;
7710             if (declspecs->noreturn_p)
7711               {
7712                 if (flag_isoc99)
7713                   pedwarn_c99 (loc, OPT_Wpedantic,
7714                                "ISO C99 does not support %<_Noreturn%>");
7715                 else
7716                   pedwarn_c99 (loc, OPT_Wpedantic,
7717                                "ISO C90 does not support %<_Noreturn%>");
7718                 TREE_THIS_VOLATILE (decl) = 1;
7719               }
7720           }
7721       }
7722     else
7723       {
7724         /* It's a variable.  */
7725         /* An uninitialized decl with `extern' is a reference.  */
7726         int extern_ref = !initialized && storage_class == csc_extern;
7727
7728         type = c_build_qualified_type (type, type_quals, orig_qual_type,
7729                                        orig_qual_indirect);
7730
7731         /* C99 6.2.2p7: It is invalid (compile-time undefined
7732            behavior) to create an 'extern' declaration for a
7733            variable if there is a global declaration that is
7734            'static' and the global declaration is not visible.
7735            (If the static declaration _is_ currently visible,
7736            the 'extern' declaration is taken to refer to that decl.) */
7737         if (extern_ref && current_scope != file_scope)
7738           {
7739             tree global_decl  = identifier_global_value (declarator->u.id.id);
7740             tree visible_decl = lookup_name (declarator->u.id.id);
7741
7742             if (global_decl
7743                 && global_decl != visible_decl
7744                 && VAR_P (global_decl)
7745                 && !TREE_PUBLIC (global_decl))
7746               error_at (loc, "variable previously declared %<static%> "
7747                         "redeclared %<extern%>");
7748           }
7749
7750         decl = build_decl (declarator->id_loc,
7751                            VAR_DECL, declarator->u.id.id, type);
7752         if (size_varies)
7753           C_DECL_VARIABLE_SIZE (decl) = 1;
7754
7755         if (declspecs->inline_p)
7756           pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7757         if (declspecs->noreturn_p)
7758           pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7759
7760         /* At file scope, an initialized extern declaration may follow
7761            a static declaration.  In that case, DECL_EXTERNAL will be
7762            reset later in start_decl.  */
7763         DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7764
7765         /* At file scope, the presence of a `static' or `register' storage
7766            class specifier, or the absence of all storage class specifiers
7767            makes this declaration a definition (perhaps tentative).  Also,
7768            the absence of `static' makes it public.  */
7769         if (current_scope == file_scope)
7770           {
7771             TREE_PUBLIC (decl) = storage_class != csc_static;
7772             TREE_STATIC (decl) = !extern_ref;
7773           }
7774         /* Not at file scope, only `static' makes a static definition.  */
7775         else
7776           {
7777             TREE_STATIC (decl) = (storage_class == csc_static);
7778             TREE_PUBLIC (decl) = extern_ref;
7779           }
7780
7781         if (threadp)
7782           set_decl_tls_model (decl, decl_default_tls_model (decl));
7783       }
7784
7785     if ((storage_class == csc_extern
7786          || (storage_class == csc_none
7787              && TREE_CODE (type) == FUNCTION_TYPE
7788              && !funcdef_flag))
7789         && variably_modified_type_p (type, NULL_TREE))
7790       {
7791         /* C99 6.7.5.2p2 */
7792         if (TREE_CODE (type) == FUNCTION_TYPE)
7793           error_at (loc, "non-nested function with variably modified type");
7794         else
7795           error_at (loc, "object with variably modified type must have "
7796                     "no linkage");
7797       }
7798
7799     /* For nested functions disqualify ones taking VLAs by value
7800        from inlining since the middle-end cannot deal with this.
7801        ???  We should arrange for those to be passed by reference
7802        with emitting the copy on the caller side in the frontend.  */
7803     if (storage_class == csc_none
7804         && TREE_CODE (type) == FUNCTION_TYPE)
7805       for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7806         {
7807           tree arg = TREE_VALUE (al);
7808           if (arg != error_mark_node
7809               && C_TYPE_VARIABLE_SIZE (arg))
7810             {
7811               DECL_UNINLINABLE (decl) = 1;
7812               break;
7813             }
7814         }
7815
7816     /* Record `register' declaration for warnings on &
7817        and in case doing stupid register allocation.  */
7818
7819     if (storage_class == csc_register)
7820       {
7821         C_DECL_REGISTER (decl) = 1;
7822         DECL_REGISTER (decl) = 1;
7823       }
7824
7825     /* Record constancy and volatility.  */
7826     c_apply_type_quals_to_decl (type_quals, decl);
7827
7828     /* Apply _Alignas specifiers.  */
7829     if (alignas_align)
7830       {
7831         SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7832         DECL_USER_ALIGN (decl) = 1;
7833       }
7834
7835     /* If a type has volatile components, it should be stored in memory.
7836        Otherwise, the fact that those components are volatile
7837        will be ignored, and would even crash the compiler.
7838        Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
7839     if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7840         && (VAR_P (decl) ||  TREE_CODE (decl) == PARM_DECL
7841           || TREE_CODE (decl) == RESULT_DECL))
7842       {
7843         /* It is not an error for a structure with volatile fields to
7844            be declared register, but reset DECL_REGISTER since it
7845            cannot actually go in a register.  */
7846         int was_reg = C_DECL_REGISTER (decl);
7847         C_DECL_REGISTER (decl) = 0;
7848         DECL_REGISTER (decl) = 0;
7849         c_mark_addressable (decl);
7850         C_DECL_REGISTER (decl) = was_reg;
7851       }
7852
7853   /* This is the earliest point at which we might know the assembler
7854      name of a variable.  Thus, if it's known before this, die horribly.  */
7855     gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7856                 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7857
7858     if (warn_cxx_compat
7859         && VAR_P (decl)
7860         && TREE_PUBLIC (decl)
7861         && TREE_STATIC (decl)
7862         && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7863             || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7864         && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7865       warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7866                   ("non-local variable %qD with anonymous type is "
7867                    "questionable in C++"),
7868                   decl);
7869
7870     return decl;
7871   }
7872 }
7873 \f
7874 /* Decode the parameter-list info for a function type or function definition.
7875    The argument is the value returned by `get_parm_info' (or made in c-parse.c
7876    if there is an identifier list instead of a parameter decl list).
7877    These two functions are separate because when a function returns
7878    or receives functions then each is called multiple times but the order
7879    of calls is different.  The last call to `grokparms' is always the one
7880    that contains the formal parameter names of a function definition.
7881
7882    Return a list of arg types to use in the FUNCTION_TYPE for this function.
7883
7884    FUNCDEF_FLAG is true for a function definition, false for
7885    a mere declaration.  A nonempty identifier-list gets an error message
7886    when FUNCDEF_FLAG is false.  */
7887
7888 static tree
7889 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7890 {
7891   tree arg_types = arg_info->types;
7892
7893   if (funcdef_flag && arg_info->had_vla_unspec)
7894     {
7895       /* A function definition isn't function prototype scope C99 6.2.1p4.  */
7896       /* C99 6.7.5.2p4 */
7897       error ("%<[*]%> not allowed in other than function prototype scope");
7898     }
7899
7900   if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
7901       && !in_system_header_at (input_location))
7902     warning (OPT_Wstrict_prototypes,
7903              "function declaration isn%'t a prototype");
7904
7905   if (arg_types == error_mark_node)
7906     /* Don't set TYPE_ARG_TYPES in this case.  */
7907     return NULL_TREE;
7908
7909   else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7910     {
7911       if (!funcdef_flag)
7912         {
7913           pedwarn (input_location, 0, "parameter names (without types) in "
7914                    "function declaration");
7915           arg_info->parms = NULL_TREE;
7916         }
7917       else
7918         arg_info->parms = arg_info->types;
7919
7920       arg_info->types = NULL_TREE;
7921       return NULL_TREE;
7922     }
7923   else
7924     {
7925       tree parm, type, typelt;
7926       unsigned int parmno;
7927
7928       /* In C2X, convert () to (void).  */
7929       if (flag_isoc2x
7930           && !arg_types
7931           && !arg_info->parms)
7932         arg_types = arg_info->types = void_list_node;
7933
7934       /* If there is a parameter of incomplete type in a definition,
7935          this is an error.  In a declaration this is valid, and a
7936          struct or union type may be completed later, before any calls
7937          or definition of the function.  In the case where the tag was
7938          first declared within the parameter list, a warning has
7939          already been given.  If a parameter has void type, then
7940          however the function cannot be defined or called, so
7941          warn.  */
7942
7943       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7944            parm;
7945            parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7946         {
7947           type = TREE_VALUE (typelt);
7948           if (type == error_mark_node)
7949             continue;
7950
7951           if (!COMPLETE_TYPE_P (type))
7952             {
7953               if (funcdef_flag)
7954                 {
7955                   if (DECL_NAME (parm))
7956                     error_at (input_location,
7957                               "parameter %u (%q+D) has incomplete type",
7958                               parmno, parm);
7959                   else
7960                     error_at (DECL_SOURCE_LOCATION (parm),
7961                               "parameter %u has incomplete type",
7962                               parmno);
7963
7964                   TREE_VALUE (typelt) = error_mark_node;
7965                   TREE_TYPE (parm) = error_mark_node;
7966                   arg_types = NULL_TREE;
7967                 }
7968               else if (VOID_TYPE_P (type))
7969                 {
7970                   if (DECL_NAME (parm))
7971                     warning_at (input_location, 0,
7972                                 "parameter %u (%q+D) has void type",
7973                                 parmno, parm);
7974                   else
7975                     warning_at (DECL_SOURCE_LOCATION (parm), 0,
7976                                 "parameter %u has void type",
7977                                 parmno);
7978                 }
7979             }
7980
7981           if (DECL_NAME (parm) && TREE_USED (parm))
7982             warn_if_shadowing (parm);
7983         }
7984       return arg_types;
7985     }
7986 }
7987
7988 /* Allocate and initialize a c_arg_info structure from the parser's
7989    obstack.  */
7990
7991 struct c_arg_info *
7992 build_arg_info (void)
7993 {
7994   struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7995   ret->parms = NULL_TREE;
7996   ret->tags = NULL;
7997   ret->types = NULL_TREE;
7998   ret->others = NULL_TREE;
7999   ret->pending_sizes = NULL;
8000   ret->had_vla_unspec = 0;
8001   return ret;
8002 }
8003
8004 /* Take apart the current scope and return a c_arg_info structure with
8005    info on a parameter list just parsed.
8006
8007    This structure is later fed to 'grokparms' and 'store_parm_decls'.
8008
8009    ELLIPSIS being true means the argument list ended in '...' so don't
8010    append a sentinel (void_list_node) to the end of the type-list.
8011
8012    EXPR is NULL or an expression that needs to be evaluated for the
8013    side effects of array size expressions in the parameters.  */
8014
8015 struct c_arg_info *
8016 get_parm_info (bool ellipsis, tree expr)
8017 {
8018   struct c_binding *b = current_scope->bindings;
8019   struct c_arg_info *arg_info = build_arg_info ();
8020
8021   tree parms = NULL_TREE;
8022   vec<c_arg_tag, va_gc> *tags = NULL;
8023   tree types = NULL_TREE;
8024   tree others = NULL_TREE;
8025
8026   bool gave_void_only_once_err = false;
8027
8028   arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8029
8030   /* The bindings in this scope must not get put into a block.
8031      We will take care of deleting the binding nodes.  */
8032   current_scope->bindings = 0;
8033
8034   /* This function is only called if there was *something* on the
8035      parameter list.  */
8036   gcc_assert (b);
8037
8038   /* A parameter list consisting solely of 'void' indicates that the
8039      function takes no arguments.  But if the 'void' is qualified
8040      (by 'const' or 'volatile'), or has a storage class specifier
8041      ('register'), then the behavior is undefined; issue an error.
8042      Typedefs for 'void' are OK (see DR#157).  */
8043   if (b->prev == 0                          /* one binding */
8044       && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
8045       && !DECL_NAME (b->decl)               /* anonymous */
8046       && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8047     {
8048       if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8049           || C_DECL_REGISTER (b->decl))
8050         error_at (b->locus, "%<void%> as only parameter may not be qualified");
8051
8052       /* There cannot be an ellipsis.  */
8053       if (ellipsis)
8054         error_at (b->locus, "%<void%> must be the only parameter");
8055
8056       arg_info->types = void_list_node;
8057       return arg_info;
8058     }
8059
8060   if (!ellipsis)
8061     types = void_list_node;
8062
8063   /* Break up the bindings list into parms, tags, types, and others;
8064      apply sanity checks; purge the name-to-decl bindings.  */
8065   while (b)
8066     {
8067       tree decl = b->decl;
8068       tree type = TREE_TYPE (decl);
8069       c_arg_tag tag;
8070       const char *keyword;
8071
8072       switch (TREE_CODE (decl))
8073         {
8074         case PARM_DECL:
8075           if (b->id)
8076             {
8077               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8078               I_SYMBOL_BINDING (b->id) = b->shadowed;
8079             }
8080
8081           /* Check for forward decls that never got their actual decl.  */
8082           if (TREE_ASM_WRITTEN (decl))
8083             error_at (b->locus,
8084                       "parameter %q+D has just a forward declaration", decl);
8085           /* Check for (..., void, ...) and issue an error.  */
8086           else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8087             {
8088               if (!gave_void_only_once_err)
8089                 {
8090                   error_at (b->locus, "%<void%> must be the only parameter");
8091                   gave_void_only_once_err = true;
8092                 }
8093             }
8094           else
8095             {
8096               /* Valid parameter, add it to the list.  */
8097               DECL_CHAIN (decl) = parms;
8098               parms = decl;
8099
8100               /* Since there is a prototype, args are passed in their
8101                  declared types.  The back end may override this later.  */
8102               DECL_ARG_TYPE (decl) = type;
8103               types = tree_cons (0, type, types);
8104             }
8105           break;
8106
8107         case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8108         case UNION_TYPE:    keyword = "union"; goto tag;
8109         case RECORD_TYPE:   keyword = "struct"; goto tag;
8110         tag:
8111           /* Types may not have tag-names, in which case the type
8112              appears in the bindings list with b->id NULL.  */
8113           if (b->id)
8114             {
8115               gcc_assert (I_TAG_BINDING (b->id) == b);
8116               I_TAG_BINDING (b->id) = b->shadowed;
8117             }
8118
8119           /* Warn about any struct, union or enum tags defined in a
8120              parameter list.  The scope of such types is limited to
8121              the parameter list, which is rarely if ever desirable
8122              (it's impossible to call such a function with type-
8123              correct arguments).  An anonymous union parm type is
8124              meaningful as a GNU extension, so don't warn for that.  */
8125           if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8126             {
8127               if (b->id)
8128                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
8129                 warning_at (b->locus, 0,
8130                             "%<%s %E%> declared inside parameter list"
8131                             " will not be visible outside of this definition or"
8132                             " declaration", keyword, b->id);
8133               else
8134                 /* The %s will be one of 'struct', 'union', or 'enum'.  */
8135                 warning_at (b->locus, 0,
8136                             "anonymous %s declared inside parameter list"
8137                             " will not be visible outside of this definition or"
8138                             " declaration", keyword);
8139             }
8140
8141           tag.id = b->id;
8142           tag.type = decl;
8143           vec_safe_push (tags, tag);
8144           break;
8145
8146         case FUNCTION_DECL:
8147           /* FUNCTION_DECLs appear when there is an implicit function
8148              declaration in the parameter list.  */
8149           gcc_assert (b->nested || seen_error ());
8150           goto set_shadowed;
8151
8152         case CONST_DECL:
8153         case TYPE_DECL:
8154           /* CONST_DECLs appear here when we have an embedded enum,
8155              and TYPE_DECLs appear here when we have an embedded struct
8156              or union.  No warnings for this - we already warned about the
8157              type itself.  */
8158
8159           /* When we reinsert this decl in the function body, we need
8160              to reconstruct whether it was marked as nested.  */
8161           gcc_assert (!b->nested);
8162           DECL_CHAIN (decl) = others;
8163           others = decl;
8164           /* fall through */
8165
8166         case ERROR_MARK:
8167         set_shadowed:
8168           /* error_mark_node appears here when we have an undeclared
8169              variable.  Just throw it away.  */
8170           if (b->id)
8171             {
8172               gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8173               I_SYMBOL_BINDING (b->id) = b->shadowed;
8174             }
8175           break;
8176
8177           /* Other things that might be encountered.  */
8178         case LABEL_DECL:
8179         case VAR_DECL:
8180         default:
8181           gcc_unreachable ();
8182         }
8183
8184       b = free_binding_and_advance (b);
8185     }
8186
8187   arg_info->parms = parms;
8188   arg_info->tags = tags;
8189   arg_info->types = types;
8190   arg_info->others = others;
8191   arg_info->pending_sizes = expr;
8192   return arg_info;
8193 }
8194 \f
8195 /* Get the struct, enum or union (CODE says which) with tag NAME.
8196    Define the tag as a forward-reference with location LOC if it is
8197    not defined.  HAVE_STD_ATTRS says whether any standard attributes
8198    were present after the struct, union or enum keyword; ATTRS are the
8199    standard attributes present there.  Return a c_typespec structure
8200    for the type specifier.  */
8201
8202 struct c_typespec
8203 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8204                  bool have_std_attrs, tree attrs)
8205 {
8206   struct c_typespec ret;
8207   tree ref;
8208   location_t refloc;
8209
8210   ret.expr = NULL_TREE;
8211   ret.expr_const_operands = true;
8212
8213   /* If a cross reference is requested, look up the type
8214      already defined for this tag and return it.  */
8215
8216   ref = lookup_tag (code, name, false, &refloc);
8217   /* If this is the right type of tag, return what we found.
8218      (This reference will be shadowed by shadow_tag later if appropriate.)
8219      If this is the wrong type of tag, do not return it.  If it was the
8220      wrong type in the same scope, we will have had an error
8221      message already; if in a different scope and declaring
8222      a name, pending_xref_error will give an error message; but if in a
8223      different scope and not declaring a name, this tag should
8224      shadow the previous declaration of a different type of tag, and
8225      this would not work properly if we return the reference found.
8226      (For example, with "struct foo" in an outer scope, "union foo;"
8227      must shadow that tag with a new one of union type.)  */
8228   ret.kind = (ref
8229               ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8230               : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8231   if (ref && TREE_CODE (ref) == code)
8232     {
8233       decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8234       if (C_TYPE_DEFINED_IN_STRUCT (ref)
8235           && loc != UNKNOWN_LOCATION
8236           && warn_cxx_compat)
8237         {
8238           auto_diagnostic_group d;
8239           switch (code)
8240             {
8241             case ENUMERAL_TYPE:
8242               if (warning_at (loc, OPT_Wc___compat,
8243                               ("enum type defined in struct or union "
8244                                "is not visible in C++")))
8245                   inform (refloc, "enum type defined here");
8246               break;
8247             case RECORD_TYPE:
8248               if (warning_at (loc, OPT_Wc___compat,
8249                               ("struct defined in struct or union "
8250                                "is not visible in C++")))
8251                 inform (refloc, "struct defined here");
8252               break;
8253             case UNION_TYPE:
8254               if (warning_at (loc, OPT_Wc___compat,
8255                               ("union defined in struct or union "
8256                                "is not visible in C++")))
8257                 inform (refloc, "union defined here");
8258               break;
8259             default:
8260               gcc_unreachable();
8261             }
8262         }
8263
8264       ret.spec = ref;
8265       return ret;
8266     }
8267
8268   /* If no such tag is yet defined, create a forward-reference node
8269      and record it as the "definition".
8270      When a real declaration of this type is found,
8271      the forward-reference will be altered into a real type.  */
8272
8273   ref = make_node (code);
8274   if (code == ENUMERAL_TYPE)
8275     {
8276       /* Give the type a default layout like unsigned int
8277          to avoid crashing if it does not get defined.  */
8278       SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8279       SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8280       TYPE_USER_ALIGN (ref) = 0;
8281       TYPE_UNSIGNED (ref) = 1;
8282       TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8283       TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8284       TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8285     }
8286
8287   pushtag (loc, name, ref);
8288   decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8289
8290   ret.spec = ref;
8291   return ret;
8292 }
8293
8294 /* Get the struct, enum or union (CODE says which) with tag NAME.
8295    Define the tag as a forward-reference if it is not defined.
8296    Return a tree for the type.  */
8297
8298 tree
8299 xref_tag (enum tree_code code, tree name)
8300 {
8301   return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
8302 }
8303 \f
8304 /* Make sure that the tag NAME is defined *in the current scope*
8305    at least as a forward reference.
8306    LOC is the location of the struct's definition.
8307    CODE says which kind of tag NAME ought to be.
8308
8309    This stores the current value of the file static STRUCT_PARSE_INFO
8310    in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8311    new c_struct_parse_info structure.  The old value of
8312    STRUCT_PARSE_INFO is restored in finish_struct.  */
8313
8314 tree
8315 start_struct (location_t loc, enum tree_code code, tree name,
8316               class c_struct_parse_info **enclosing_struct_parse_info)
8317 {
8318   /* If there is already a tag defined at this scope
8319      (as a forward reference), just return it.  */
8320
8321   tree ref = NULL_TREE;
8322   location_t refloc = UNKNOWN_LOCATION;
8323
8324   if (name != NULL_TREE)
8325     ref = lookup_tag (code, name, true, &refloc);
8326   if (ref && TREE_CODE (ref) == code)
8327     {
8328       if (TYPE_STUB_DECL (ref))
8329         refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8330
8331       if (TYPE_SIZE (ref))
8332         {
8333           auto_diagnostic_group d;
8334           if (code == UNION_TYPE)
8335             error_at (loc, "redefinition of %<union %E%>", name);
8336           else
8337             error_at (loc, "redefinition of %<struct %E%>", name);
8338           if (refloc != UNKNOWN_LOCATION)
8339             inform (refloc, "originally defined here");
8340           /* Don't create structures using a name already in use.  */
8341           ref = NULL_TREE;
8342         }
8343       else if (C_TYPE_BEING_DEFINED (ref))
8344         {
8345           if (code == UNION_TYPE)
8346             error_at (loc, "nested redefinition of %<union %E%>", name);
8347           else
8348             error_at (loc, "nested redefinition of %<struct %E%>", name);
8349           /* Don't bother to report "originally defined here" for a
8350              nested redefinition; the original definition should be
8351              obvious.  */
8352           /* Don't create structures that contain themselves.  */
8353           ref = NULL_TREE;
8354         }
8355     }
8356
8357   /* Otherwise create a forward-reference just so the tag is in scope.  */
8358
8359   if (ref == NULL_TREE || TREE_CODE (ref) != code)
8360     {
8361       ref = make_node (code);
8362       pushtag (loc, name, ref);
8363     }
8364
8365   C_TYPE_BEING_DEFINED (ref) = 1;
8366   for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8367     TYPE_PACKED (v) = flag_pack_struct;
8368
8369   *enclosing_struct_parse_info = struct_parse_info;
8370   struct_parse_info = new c_struct_parse_info ();
8371
8372   /* FIXME: This will issue a warning for a use of a type defined
8373      within a statement expr used within sizeof, et. al.  This is not
8374      terribly serious as C++ doesn't permit statement exprs within
8375      sizeof anyhow.  */
8376   if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8377     warning_at (loc, OPT_Wc___compat,
8378                 "defining type in %qs expression is invalid in C++",
8379                 (in_sizeof
8380                  ? "sizeof"
8381                  : (in_typeof ? "typeof" : "alignof")));
8382
8383   return ref;
8384 }
8385
8386 /* Process the specs, declarator and width (NULL if omitted)
8387    of a structure component, returning a FIELD_DECL node.
8388    WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8389    DECL_ATTRS is as for grokdeclarator.
8390
8391    LOC is the location of the structure component.
8392
8393    This is done during the parsing of the struct declaration.
8394    The FIELD_DECL nodes are chained together and the lot of them
8395    are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
8396
8397 tree
8398 grokfield (location_t loc,
8399            struct c_declarator *declarator, struct c_declspecs *declspecs,
8400            tree width, tree *decl_attrs)
8401 {
8402   tree value;
8403
8404   if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8405       && width == NULL_TREE)
8406     {
8407       /* This is an unnamed decl.
8408
8409          If we have something of the form "union { list } ;" then this
8410          is the anonymous union extension.  Similarly for struct.
8411
8412          If this is something of the form "struct foo;", then
8413            If MS or Plan 9 extensions are enabled, this is handled as
8414              an anonymous struct.
8415            Otherwise this is a forward declaration of a structure tag.
8416
8417          If this is something of the form "foo;" and foo is a TYPE_DECL, then
8418            If foo names a structure or union without a tag, then this
8419              is an anonymous struct (this is permitted by C11).
8420            If MS or Plan 9 extensions are enabled and foo names a
8421              structure, then again this is an anonymous struct.
8422            Otherwise this is an error.
8423
8424          Oh what a horrid tangled web we weave.  I wonder if MS consciously
8425          took this from Plan 9 or if it was an accident of implementation
8426          that took root before someone noticed the bug...  */
8427
8428       tree type = declspecs->type;
8429       bool ok = false;
8430
8431       if (RECORD_OR_UNION_TYPE_P (type)
8432           && (flag_ms_extensions
8433               || flag_plan9_extensions
8434               || !declspecs->typedef_p))
8435         {
8436           if (flag_ms_extensions || flag_plan9_extensions)
8437             ok = true;
8438           else if (TYPE_NAME (type) == NULL)
8439             ok = true;
8440           else
8441             ok = false;
8442         }
8443       if (!ok)
8444         {
8445           pedwarn (loc, 0, "declaration does not declare anything");
8446           return NULL_TREE;
8447         }
8448       if (flag_isoc99)
8449         pedwarn_c99 (loc, OPT_Wpedantic,
8450                      "ISO C99 doesn%'t support unnamed structs/unions");
8451       else
8452         pedwarn_c99 (loc, OPT_Wpedantic,
8453                      "ISO C90 doesn%'t support unnamed structs/unions");
8454     }
8455
8456   value = grokdeclarator (declarator, declspecs, FIELD, false,
8457                           width ? &width : NULL, decl_attrs, NULL, NULL,
8458                           DEPRECATED_NORMAL);
8459
8460   finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8461   DECL_INITIAL (value) = width;
8462   if (width)
8463     SET_DECL_C_BIT_FIELD (value);
8464
8465   if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8466     {
8467       /* If we currently have a binding for this field, set the
8468          in_struct field in the binding, so that we warn about lookups
8469          which find it.  */
8470       struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8471       if (b != NULL)
8472         {
8473           /* If the in_struct field is not yet set, push it on a list
8474              to be cleared when this struct is finished.  */
8475           if (!b->in_struct)
8476             {
8477               struct_parse_info->fields.safe_push (b);
8478               b->in_struct = 1;
8479             }
8480         }
8481     }
8482
8483   return value;
8484 }
8485 \f
8486 /* Subroutine of detect_field_duplicates: return whether X and Y,
8487    which are both fields in the same struct, have duplicate field
8488    names.  */
8489
8490 static bool
8491 is_duplicate_field (tree x, tree y)
8492 {
8493   if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8494     return true;
8495
8496   /* When using -fplan9-extensions, an anonymous field whose name is a
8497      typedef can duplicate a field name.  */
8498   if (flag_plan9_extensions
8499       && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8500     {
8501       tree xt, xn, yt, yn;
8502
8503       xt = TREE_TYPE (x);
8504       if (DECL_NAME (x) != NULL_TREE)
8505         xn = DECL_NAME (x);
8506       else if (RECORD_OR_UNION_TYPE_P (xt)
8507                && TYPE_NAME (xt) != NULL_TREE
8508                && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8509         xn = DECL_NAME (TYPE_NAME (xt));
8510       else
8511         xn = NULL_TREE;
8512
8513       yt = TREE_TYPE (y);
8514       if (DECL_NAME (y) != NULL_TREE)
8515         yn = DECL_NAME (y);
8516       else if (RECORD_OR_UNION_TYPE_P (yt)
8517                && TYPE_NAME (yt) != NULL_TREE
8518                && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8519         yn = DECL_NAME (TYPE_NAME (yt));
8520       else
8521         yn = NULL_TREE;
8522
8523       if (xn != NULL_TREE && xn == yn)
8524         return true;
8525     }
8526
8527   return false;
8528 }
8529
8530 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8531    to HTAB, giving errors for any duplicates.  */
8532
8533 static void
8534 detect_field_duplicates_hash (tree fieldlist,
8535                               hash_table<nofree_ptr_hash <tree_node> > *htab)
8536 {
8537   tree x, y;
8538   tree_node **slot;
8539
8540   for (x = fieldlist; x ; x = DECL_CHAIN (x))
8541     if ((y = DECL_NAME (x)) != NULL_TREE)
8542       {
8543         slot = htab->find_slot (y, INSERT);
8544         if (*slot)
8545           {
8546             error ("duplicate member %q+D", x);
8547             DECL_NAME (x) = NULL_TREE;
8548           }
8549         *slot = y;
8550       }
8551     else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8552       {
8553         detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8554
8555         /* When using -fplan9-extensions, an anonymous field whose
8556            name is a typedef can duplicate a field name.  */
8557         if (flag_plan9_extensions
8558             && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8559             && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8560           {
8561             tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8562             slot = htab->find_slot (xn, INSERT);
8563             if (*slot)
8564               error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8565             *slot = xn;
8566           }
8567       }
8568 }
8569
8570 /* Generate an error for any duplicate field names in FIELDLIST.  Munge
8571    the list such that this does not present a problem later.  */
8572
8573 static void
8574 detect_field_duplicates (tree fieldlist)
8575 {
8576   tree x, y;
8577   int timeout = 10;
8578
8579   /* If the struct is the list of instance variables of an Objective-C
8580      class, then we need to check all the instance variables of
8581      superclasses when checking for duplicates (since you can't have
8582      an instance variable in a subclass with the same name as an
8583      instance variable in a superclass).  We pass on this job to the
8584      Objective-C compiler.  objc_detect_field_duplicates() will return
8585      false if we are not checking the list of instance variables and
8586      the C frontend should proceed with the standard field duplicate
8587      checks.  If we are checking the list of instance variables, the
8588      ObjC frontend will do the check, emit the errors if needed, and
8589      then return true.  */
8590   if (c_dialect_objc ())
8591     if (objc_detect_field_duplicates (false))
8592       return;
8593
8594   /* First, see if there are more than "a few" fields.
8595      This is trivially true if there are zero or one fields.  */
8596   if (!fieldlist || !DECL_CHAIN (fieldlist))
8597     return;
8598   x = fieldlist;
8599   do {
8600     timeout--;
8601     if (DECL_NAME (x) == NULL_TREE
8602         && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8603       timeout = 0;
8604     x = DECL_CHAIN (x);
8605   } while (timeout > 0 && x);
8606
8607   /* If there were "few" fields and no anonymous structures or unions,
8608      avoid the overhead of allocating a hash table.  Instead just do
8609      the nested traversal thing.  */
8610   if (timeout > 0)
8611     {
8612       for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8613         /* When using -fplan9-extensions, we can have duplicates
8614            between typedef names and fields.  */
8615         if (DECL_NAME (x)
8616             || (flag_plan9_extensions
8617                 && DECL_NAME (x) == NULL_TREE
8618                 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8619                 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8620                 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8621           {
8622             for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8623               if (is_duplicate_field (y, x))
8624                 {
8625                   error ("duplicate member %q+D", x);
8626                   DECL_NAME (x) = NULL_TREE;
8627                 }
8628           }
8629     }
8630   else
8631     {
8632       hash_table<nofree_ptr_hash <tree_node> > htab (37);
8633       detect_field_duplicates_hash (fieldlist, &htab);
8634     }
8635 }
8636
8637 /* Finish up struct info used by -Wc++-compat.  */
8638
8639 static void
8640 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8641                                location_t record_loc)
8642 {
8643   unsigned int ix;
8644   tree x;
8645   struct c_binding *b;
8646
8647   if (fieldlist == NULL_TREE)
8648     {
8649       if (code == RECORD_TYPE)
8650         warning_at (record_loc, OPT_Wc___compat,
8651                     "empty struct has size 0 in C, size 1 in C++");
8652       else
8653         warning_at (record_loc, OPT_Wc___compat,
8654                     "empty union has size 0 in C, size 1 in C++");
8655     }
8656
8657   /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8658      the current struct.  We do this now at the end of the struct
8659      because the flag is used to issue visibility warnings, and we
8660      only want to issue those warnings if the type is referenced
8661      outside of the struct declaration.  */
8662   FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8663     C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8664
8665   /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8666      typedefs used when declaring fields in this struct.  If the name
8667      of any of the fields is also a typedef name then the struct would
8668      not parse in C++, because the C++ lookup rules say that the
8669      typedef name would be looked up in the context of the struct, and
8670      would thus be the field rather than the typedef.  */
8671   if (!struct_parse_info->typedefs_seen.is_empty ()
8672       && fieldlist != NULL_TREE)
8673     {
8674       /* Use a hash_set<tree> using the name of the typedef.  We can use
8675          a hash_set<tree> because identifiers are interned.  */
8676       hash_set<tree> tset;
8677
8678       FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8679         tset.add (DECL_NAME (x));
8680
8681       for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8682         {
8683           if (DECL_NAME (x) != NULL_TREE
8684               && tset.contains (DECL_NAME (x)))
8685             {
8686               warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8687                           ("using %qD as both field and typedef name is "
8688                            "invalid in C++"),
8689                           x);
8690               /* FIXME: It would be nice to report the location where
8691                  the typedef name is used.  */
8692             }
8693         }
8694     }
8695
8696   /* For each field which has a binding and which was not defined in
8697      an enclosing struct, clear the in_struct field.  */
8698   FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8699     b->in_struct = 0;
8700 }
8701
8702 /* Function to help qsort sort FIELD_DECLs by name order.  */
8703
8704 static int
8705 field_decl_cmp (const void *x_p, const void *y_p)
8706 {
8707   const tree *const x = (const tree *) x_p;
8708   const tree *const y = (const tree *) y_p;
8709
8710   if (DECL_NAME (*x) == DECL_NAME (*y))
8711     /* A nontype is "greater" than a type.  */
8712     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8713   if (DECL_NAME (*x) == NULL_TREE)
8714     return -1;
8715   if (DECL_NAME (*y) == NULL_TREE)
8716     return 1;
8717   if (DECL_NAME (*x) < DECL_NAME (*y))
8718     return -1;
8719   return 1;
8720 }
8721
8722 /* If this structure or union completes the type of any previous
8723    variable declaration, lay it out and output its rtl.  */
8724 static void
8725 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8726 {
8727   for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8728     {
8729       tree decl = TREE_VALUE (x);
8730       if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8731         layout_array_type (TREE_TYPE (decl));
8732       if (TREE_CODE (decl) != TYPE_DECL)
8733         {
8734           relayout_decl (decl);
8735           if (c_dialect_objc ())
8736             objc_check_decl (decl);
8737           rest_of_decl_compilation (decl, toplevel, 0);
8738         }
8739     }
8740 }
8741
8742 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8743    LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8744    FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8745    ATTRIBUTES are attributes to be applied to the structure.
8746
8747    ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8748    the struct was started.  */
8749
8750 tree
8751 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8752                class c_struct_parse_info *enclosing_struct_parse_info)
8753 {
8754   tree x;
8755   bool toplevel = file_scope == current_scope;
8756
8757   /* If this type was previously laid out as a forward reference,
8758      make sure we lay it out again.  */
8759
8760   TYPE_SIZE (t) = NULL_TREE;
8761
8762   decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8763
8764   if (pedantic)
8765     {
8766       for (x = fieldlist; x; x = DECL_CHAIN (x))
8767         {
8768           if (DECL_NAME (x) != NULL_TREE)
8769             break;
8770           if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8771             break;
8772         }
8773
8774       if (x == NULL_TREE)
8775         {
8776           if (TREE_CODE (t) == UNION_TYPE)
8777             {
8778               if (fieldlist)
8779                 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8780               else
8781                 pedwarn (loc, OPT_Wpedantic, "union has no members");
8782             }
8783           else
8784             {
8785               if (fieldlist)
8786                 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8787               else
8788                 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8789             }
8790         }
8791     }
8792
8793   /* Install struct as DECL_CONTEXT of each field decl.
8794      Also process specified field sizes, found in the DECL_INITIAL,
8795      storing 0 there after the type has been changed to precision equal
8796      to its width, rather than the precision of the specified standard
8797      type.  (Correct layout requires the original type to have been preserved
8798      until now.)  */
8799
8800   bool saw_named_field = false;
8801   for (x = fieldlist; x; x = DECL_CHAIN (x))
8802     {
8803       if (TREE_TYPE (x) == error_mark_node)
8804         continue;
8805
8806       DECL_CONTEXT (x) = t;
8807
8808       /* If any field is const, the structure type is pseudo-const.  */
8809       if (TREE_READONLY (x))
8810         C_TYPE_FIELDS_READONLY (t) = 1;
8811       else
8812         {
8813           /* A field that is pseudo-const makes the structure likewise.  */
8814           tree t1 = strip_array_types (TREE_TYPE (x));
8815           if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8816             C_TYPE_FIELDS_READONLY (t) = 1;
8817         }
8818
8819       /* Any field that is volatile means variables of this type must be
8820          treated in some ways as volatile.  */
8821       if (TREE_THIS_VOLATILE (x))
8822         C_TYPE_FIELDS_VOLATILE (t) = 1;
8823
8824       /* Any field of nominal variable size implies structure is too.  */
8825       if (C_DECL_VARIABLE_SIZE (x))
8826         C_TYPE_VARIABLE_SIZE (t) = 1;
8827
8828       if (DECL_C_BIT_FIELD (x))
8829         {
8830           unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8831           DECL_SIZE (x) = bitsize_int (width);
8832           DECL_BIT_FIELD (x) = 1;
8833         }
8834
8835       if (TYPE_PACKED (t)
8836           && (DECL_BIT_FIELD (x)
8837               || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8838         DECL_PACKED (x) = 1;
8839
8840       /* Detect flexible array member in an invalid context.  */
8841       if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8842           && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8843           && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8844           && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8845         {
8846           if (TREE_CODE (t) == UNION_TYPE)
8847             {
8848               error_at (DECL_SOURCE_LOCATION (x),
8849                         "flexible array member in union");
8850               TREE_TYPE (x) = error_mark_node;
8851             }
8852           else if (DECL_CHAIN (x) != NULL_TREE)
8853             {
8854               error_at (DECL_SOURCE_LOCATION (x),
8855                         "flexible array member not at end of struct");
8856               TREE_TYPE (x) = error_mark_node;
8857             }
8858           else if (!saw_named_field)
8859             {
8860               error_at (DECL_SOURCE_LOCATION (x),
8861                         "flexible array member in a struct with no named "
8862                         "members");
8863               TREE_TYPE (x) = error_mark_node;
8864             }
8865         }
8866
8867       if (pedantic && TREE_CODE (t) == RECORD_TYPE
8868           && flexible_array_type_p (TREE_TYPE (x)))
8869         pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8870                  "invalid use of structure with flexible array member");
8871
8872       if (DECL_NAME (x)
8873           || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8874         saw_named_field = true;
8875     }
8876
8877   detect_field_duplicates (fieldlist);
8878
8879   /* Now we have the nearly final fieldlist.  Record it,
8880      then lay out the structure or union (including the fields).  */
8881
8882   TYPE_FIELDS (t) = fieldlist;
8883
8884   maybe_apply_pragma_scalar_storage_order (t);
8885
8886   layout_type (t);
8887
8888   if (TYPE_SIZE_UNIT (t)
8889       && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8890       && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8891       && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8892     error ("type %qT is too large", t);
8893
8894   /* Give bit-fields their proper types and rewrite the type of array fields
8895      with scalar component if the enclosing type has reverse storage order.  */
8896   for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8897     {
8898       if (TREE_CODE (field) == FIELD_DECL
8899           && DECL_INITIAL (field)
8900           && TREE_TYPE (field) != error_mark_node)
8901         {
8902           unsigned HOST_WIDE_INT width
8903             = tree_to_uhwi (DECL_INITIAL (field));
8904           tree type = TREE_TYPE (field);
8905           if (width != TYPE_PRECISION (type))
8906             {
8907               TREE_TYPE (field)
8908                 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8909               SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8910             }
8911           DECL_INITIAL (field) = NULL_TREE;
8912         }
8913       else if (TYPE_REVERSE_STORAGE_ORDER (t)
8914                && TREE_CODE (field) == FIELD_DECL
8915                && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8916         {
8917           tree ftype = TREE_TYPE (field);
8918           tree ctype = strip_array_types (ftype);
8919           if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8920             {
8921               tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8922               tree *typep = &fmain_type;
8923               do {
8924                 *typep = build_distinct_type_copy (*typep);
8925                 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8926                 typep = &TREE_TYPE (*typep);
8927               } while (TREE_CODE (*typep) == ARRAY_TYPE);
8928               TREE_TYPE (field)
8929                 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8930             }
8931         }
8932
8933       /* Warn on problematic type punning for storage order purposes.  */
8934       if (TREE_CODE (t) == UNION_TYPE
8935           && TREE_CODE (field) == FIELD_DECL
8936           && AGGREGATE_TYPE_P (TREE_TYPE (field)))
8937         {
8938           tree ftype = TREE_TYPE (field);
8939           if (TREE_CODE (ftype) == ARRAY_TYPE)
8940             ftype = strip_array_types (ftype);
8941           if (RECORD_OR_UNION_TYPE_P (ftype)
8942               && TYPE_REVERSE_STORAGE_ORDER (ftype)
8943                  != TYPE_REVERSE_STORAGE_ORDER (t))
8944             warning_at (DECL_SOURCE_LOCATION (field),
8945                         OPT_Wscalar_storage_order,
8946                         "type punning toggles scalar storage order");
8947         }
8948     }
8949
8950   /* Now we have the truly final field list.
8951      Store it in this type and in the variants.  */
8952
8953   TYPE_FIELDS (t) = fieldlist;
8954
8955   /* If there are lots of fields, sort so we can look through them fast.
8956      We arbitrarily consider 16 or more elts to be "a lot".  */
8957
8958   {
8959     int len = 0;
8960
8961     for (x = fieldlist; x; x = DECL_CHAIN (x))
8962       {
8963         if (len > 15 || DECL_NAME (x) == NULL)
8964           break;
8965         len += 1;
8966       }
8967
8968     if (len > 15)
8969       {
8970         tree *field_array;
8971         struct lang_type *space;
8972         struct sorted_fields_type *space2;
8973
8974         len += list_length (x);
8975
8976         /* Use the same allocation policy here that make_node uses, to
8977           ensure that this lives as long as the rest of the struct decl.
8978           All decls in an inline function need to be saved.  */
8979
8980         space = ggc_cleared_alloc<struct lang_type> ();
8981         space2 = (sorted_fields_type *) ggc_internal_alloc
8982           (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8983
8984         len = 0;
8985         space->s = space2;
8986         field_array = &space2->elts[0];
8987         for (x = fieldlist; x; x = DECL_CHAIN (x))
8988           {
8989             field_array[len++] = x;
8990
8991             /* If there is anonymous struct or union, break out of the loop.  */
8992             if (DECL_NAME (x) == NULL)
8993               break;
8994           }
8995         /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
8996         if (x == NULL)
8997           {
8998             TYPE_LANG_SPECIFIC (t) = space;
8999             TYPE_LANG_SPECIFIC (t)->s->len = len;
9000             field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9001             qsort (field_array, len, sizeof (tree), field_decl_cmp);
9002           }
9003       }
9004   }
9005
9006   /* If this was supposed to be a transparent union, but we can't
9007      make it one, warn and turn off the flag.  */
9008   if (TREE_CODE (t) == UNION_TYPE
9009       && TYPE_TRANSPARENT_AGGR (t)
9010       && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9011     {
9012       TYPE_TRANSPARENT_AGGR (t) = 0;
9013       warning_at (loc, 0, "union cannot be made transparent");
9014     }
9015
9016   tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9017   for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9018     {
9019       TYPE_FIELDS (x) = TYPE_FIELDS (t);
9020       TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9021       TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9022       C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9023       C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9024       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9025       C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9026     }
9027
9028   /* Update type location to the one of the definition, instead of e.g.
9029      a forward declaration.  */
9030   if (TYPE_STUB_DECL (t))
9031     DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9032
9033   /* Finish debugging output for this type.  */
9034   rest_of_type_compilation (t, toplevel);
9035
9036   finish_incomplete_vars (incomplete_vars, toplevel);
9037
9038   /* If we're inside a function proper, i.e. not file-scope and not still
9039      parsing parameters, then arrange for the size of a variable sized type
9040      to be bound now.  */
9041   if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
9042     add_stmt (build_stmt (loc,
9043                           DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9044
9045   if (warn_cxx_compat)
9046     warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9047
9048   delete struct_parse_info;
9049
9050   struct_parse_info = enclosing_struct_parse_info;
9051
9052   /* If this struct is defined inside a struct, add it to
9053      struct_types.  */
9054   if (warn_cxx_compat
9055       && struct_parse_info != NULL
9056       && !in_sizeof && !in_typeof && !in_alignof)
9057     struct_parse_info->struct_types.safe_push (t);
9058
9059   return t;
9060 }
9061
9062 static struct {
9063   gt_pointer_operator new_value;
9064   void *cookie;
9065 } resort_data;
9066
9067 /* This routine compares two fields like field_decl_cmp but using the
9068 pointer operator in resort_data.  */
9069
9070 static int
9071 resort_field_decl_cmp (const void *x_p, const void *y_p)
9072 {
9073   const tree *const x = (const tree *) x_p;
9074   const tree *const y = (const tree *) y_p;
9075
9076   if (DECL_NAME (*x) == DECL_NAME (*y))
9077     /* A nontype is "greater" than a type.  */
9078     return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9079   if (DECL_NAME (*x) == NULL_TREE)
9080     return -1;
9081   if (DECL_NAME (*y) == NULL_TREE)
9082     return 1;
9083   {
9084     tree d1 = DECL_NAME (*x);
9085     tree d2 = DECL_NAME (*y);
9086     resort_data.new_value (&d1, &d1, resort_data.cookie);
9087     resort_data.new_value (&d2, &d2, resort_data.cookie);
9088     if (d1 < d2)
9089       return -1;
9090   }
9091   return 1;
9092 }
9093
9094 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
9095
9096 void
9097 resort_sorted_fields (void *obj,
9098                       void * ARG_UNUSED (orig_obj),
9099                       gt_pointer_operator new_value,
9100                       void *cookie)
9101 {
9102   struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9103   resort_data.new_value = new_value;
9104   resort_data.cookie = cookie;
9105   qsort (&sf->elts[0], sf->len, sizeof (tree),
9106          resort_field_decl_cmp);
9107 }
9108
9109 /* Lay out the type T, and its element type, and so on.  */
9110
9111 static void
9112 layout_array_type (tree t)
9113 {
9114   if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9115     layout_array_type (TREE_TYPE (t));
9116   layout_type (t);
9117 }
9118 \f
9119 /* Begin compiling the definition of an enumeration type.
9120    NAME is its name (or null if anonymous).
9121    LOC is the enum's location.
9122    Returns the type object, as yet incomplete.
9123    Also records info about it so that build_enumerator
9124    may be used to declare the individual values as they are read.  */
9125
9126 tree
9127 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
9128 {
9129   tree enumtype = NULL_TREE;
9130   location_t enumloc = UNKNOWN_LOCATION;
9131
9132   /* If this is the real definition for a previous forward reference,
9133      fill in the contents in the same object that used to be the
9134      forward reference.  */
9135
9136   if (name != NULL_TREE)
9137     enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9138
9139   if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9140     {
9141       enumtype = make_node (ENUMERAL_TYPE);
9142       pushtag (loc, name, enumtype);
9143     }
9144   /* Update type location to the one of the definition, instead of e.g.
9145      a forward declaration.  */
9146   else if (TYPE_STUB_DECL (enumtype))
9147     {
9148       enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9149       DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9150     }
9151
9152   if (C_TYPE_BEING_DEFINED (enumtype))
9153     error_at (loc, "nested redefinition of %<enum %E%>", name);
9154
9155   C_TYPE_BEING_DEFINED (enumtype) = 1;
9156
9157   if (TYPE_VALUES (enumtype) != NULL_TREE)
9158     {
9159       /* This enum is a named one that has been declared already.  */
9160       auto_diagnostic_group d;
9161       error_at (loc, "redeclaration of %<enum %E%>", name);
9162       if (enumloc != UNKNOWN_LOCATION)
9163         inform (enumloc, "originally defined here");
9164
9165       /* Completely replace its old definition.
9166          The old enumerators remain defined, however.  */
9167       TYPE_VALUES (enumtype) = NULL_TREE;
9168     }
9169
9170   the_enum->enum_next_value = integer_zero_node;
9171   the_enum->enum_overflow = 0;
9172
9173   if (flag_short_enums)
9174     for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9175       TYPE_PACKED (v) = 1;
9176
9177   /* FIXME: This will issue a warning for a use of a type defined
9178      within sizeof in a statement expr.  This is not terribly serious
9179      as C++ doesn't permit statement exprs within sizeof anyhow.  */
9180   if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9181     warning_at (loc, OPT_Wc___compat,
9182                 "defining type in %qs expression is invalid in C++",
9183                 (in_sizeof
9184                  ? "sizeof"
9185                  : (in_typeof ? "typeof" : "alignof")));
9186
9187   return enumtype;
9188 }
9189
9190 /* After processing and defining all the values of an enumeration type,
9191    install their decls in the enumeration type and finish it off.
9192    ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9193    and ATTRIBUTES are the specified attributes.
9194    Returns ENUMTYPE.  */
9195
9196 tree
9197 finish_enum (tree enumtype, tree values, tree attributes)
9198 {
9199   tree pair, tem;
9200   tree minnode = NULL_TREE, maxnode = NULL_TREE;
9201   int precision;
9202   signop sign;
9203   bool toplevel = (file_scope == current_scope);
9204   struct lang_type *lt;
9205
9206   decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9207
9208   /* Calculate the maximum value of any enumerator in this type.  */
9209
9210   if (values == error_mark_node)
9211     minnode = maxnode = integer_zero_node;
9212   else
9213     {
9214       minnode = maxnode = TREE_VALUE (values);
9215       for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9216         {
9217           tree value = TREE_VALUE (pair);
9218           if (tree_int_cst_lt (maxnode, value))
9219             maxnode = value;
9220           if (tree_int_cst_lt (value, minnode))
9221             minnode = value;
9222         }
9223     }
9224
9225   /* Construct the final type of this enumeration.  It is the same
9226      as one of the integral types - the narrowest one that fits, except
9227      that normally we only go as narrow as int - and signed iff any of
9228      the values are negative.  */
9229   sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9230   precision = MAX (tree_int_cst_min_precision (minnode, sign),
9231                    tree_int_cst_min_precision (maxnode, sign));
9232
9233   /* If the precision of the type was specified with an attribute and it
9234      was too small, give an error.  Otherwise, use it.  */
9235   if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9236     {
9237       if (precision > TYPE_PRECISION (enumtype))
9238         {
9239           TYPE_PRECISION (enumtype) = 0;
9240           error ("specified mode too small for enumerated values");
9241         }
9242       else
9243         precision = TYPE_PRECISION (enumtype);
9244     }
9245   else
9246     TYPE_PRECISION (enumtype) = 0;
9247
9248   if (TYPE_PACKED (enumtype)
9249       || precision > TYPE_PRECISION (integer_type_node)
9250       || TYPE_PRECISION (enumtype))
9251     {
9252       tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9253       if (tem == NULL)
9254         {
9255           warning (0, "enumeration values exceed range of largest integer");
9256           tem = long_long_integer_type_node;
9257         }
9258     }
9259   else
9260     tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9261
9262   TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9263   TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9264   TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9265   SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9266   TYPE_SIZE (enumtype) = NULL_TREE;
9267   TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9268
9269   layout_type (enumtype);
9270
9271   if (values != error_mark_node)
9272     {
9273       /* Change the type of the enumerators to be the enum type.  We
9274          need to do this irrespective of the size of the enum, for
9275          proper type checking.  Replace the DECL_INITIALs of the
9276          enumerators, and the value slots of the list, with copies
9277          that have the enum type; they cannot be modified in place
9278          because they may be shared (e.g.  integer_zero_node) Finally,
9279          change the purpose slots to point to the names of the decls.  */
9280       for (pair = values; pair; pair = TREE_CHAIN (pair))
9281         {
9282           tree enu = TREE_PURPOSE (pair);
9283           tree ini = DECL_INITIAL (enu);
9284
9285           TREE_TYPE (enu) = enumtype;
9286
9287           /* The ISO C Standard mandates enumerators to have type int,
9288              even though the underlying type of an enum type is
9289              unspecified.  However, GCC allows enumerators of any
9290              integer type as an extensions.  build_enumerator()
9291              converts any enumerators that fit in an int to type int,
9292              to avoid promotions to unsigned types when comparing
9293              integers with enumerators that fit in the int range.
9294              When -pedantic is given, build_enumerator() would have
9295              already warned about those that don't fit. Here we
9296              convert the rest to the enumerator type. */
9297           if (TREE_TYPE (ini) != integer_type_node)
9298             ini = convert (enumtype, ini);
9299
9300           DECL_INITIAL (enu) = ini;
9301           TREE_PURPOSE (pair) = DECL_NAME (enu);
9302           /* To match the C++ FE, store the CONST_DECL rather than just its
9303              value.  */
9304           TREE_VALUE (pair) = enu;
9305         }
9306
9307       TYPE_VALUES (enumtype) = values;
9308     }
9309
9310   /* Record the min/max values so that we can warn about bit-field
9311      enumerations that are too small for the values.  */
9312   lt = ggc_cleared_alloc<struct lang_type> ();
9313   lt->enum_min = minnode;
9314   lt->enum_max = maxnode;
9315   TYPE_LANG_SPECIFIC (enumtype) = lt;
9316
9317   /* Fix up all variant types of this enum type.  */
9318   tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9319   for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9320     {
9321       C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9322       if (tem == enumtype)
9323         continue;
9324       TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9325       TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9326       TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9327       TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9328       TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9329       SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9330       TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9331       SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9332       TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9333       TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9334       TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9335     }
9336
9337   /* Finish debugging output for this type.  */
9338   rest_of_type_compilation (enumtype, toplevel);
9339
9340   finish_incomplete_vars (incomplete_vars, toplevel);
9341
9342   /* If this enum is defined inside a struct, add it to
9343      struct_types.  */
9344   if (warn_cxx_compat
9345       && struct_parse_info != NULL
9346       && !in_sizeof && !in_typeof && !in_alignof)
9347     struct_parse_info->struct_types.safe_push (enumtype);
9348
9349   C_TYPE_BEING_DEFINED (enumtype) = 0;
9350
9351   return enumtype;
9352 }
9353
9354 /* Build and install a CONST_DECL for one value of the
9355    current enumeration type (one that was begun with start_enum).
9356    DECL_LOC is the location of the enumerator.
9357    LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9358    Return a tree-list containing the CONST_DECL and its value.
9359    Assignment of sequential values by default is handled here.  */
9360
9361 tree
9362 build_enumerator (location_t decl_loc, location_t loc,
9363                   struct c_enum_contents *the_enum, tree name, tree value)
9364 {
9365   tree decl, type;
9366
9367   /* Validate and default VALUE.  */
9368
9369   if (value != NULL_TREE)
9370     {
9371       /* Don't issue more errors for error_mark_node (i.e. an
9372          undeclared identifier) - just ignore the value expression.  */
9373       if (value == error_mark_node)
9374         value = NULL_TREE;
9375       else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9376         {
9377           error_at (loc, "enumerator value for %qE is not an integer constant",
9378                     name);
9379           value = NULL_TREE;
9380         }
9381       else
9382         {
9383           if (TREE_CODE (value) != INTEGER_CST)
9384             {
9385               value = c_fully_fold (value, false, NULL);
9386               if (TREE_CODE (value) == INTEGER_CST)
9387                 pedwarn (loc, OPT_Wpedantic,
9388                          "enumerator value for %qE is not an integer "
9389                          "constant expression", name);
9390             }
9391           if (TREE_CODE (value) != INTEGER_CST)
9392             {
9393               error ("enumerator value for %qE is not an integer constant",
9394                      name);
9395               value = NULL_TREE;
9396             }
9397           else
9398             {
9399               value = default_conversion (value);
9400               constant_expression_warning (value);
9401             }
9402         }
9403     }
9404
9405   /* Default based on previous value.  */
9406   /* It should no longer be possible to have NON_LVALUE_EXPR
9407      in the default.  */
9408   if (value == NULL_TREE)
9409     {
9410       value = the_enum->enum_next_value;
9411       if (the_enum->enum_overflow)
9412         error_at (loc, "overflow in enumeration values");
9413     }
9414   /* Even though the underlying type of an enum is unspecified, the
9415      type of enumeration constants is explicitly defined as int
9416      (6.4.4.3/2 in the C99 Standard).  GCC allows any integer type as
9417      an extension.  */
9418   else if (!int_fits_type_p (value, integer_type_node))
9419     pedwarn (loc, OPT_Wpedantic,
9420              "ISO C restricts enumerator values to range of %<int%>");
9421
9422   /* The ISO C Standard mandates enumerators to have type int, even
9423      though the underlying type of an enum type is unspecified.
9424      However, GCC allows enumerators of any integer type as an
9425      extensions.  Here we convert any enumerators that fit in an int
9426      to type int, to avoid promotions to unsigned types when comparing
9427      integers with enumerators that fit in the int range.  When
9428      -pedantic is given, we would have already warned about those that
9429      don't fit. We have to do this here rather than in finish_enum
9430      because this value may be used to define more enumerators.  */
9431   if (int_fits_type_p (value, integer_type_node))
9432     value = convert (integer_type_node, value);
9433
9434   /* Set basis for default for next value.  */
9435   the_enum->enum_next_value
9436     = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9437                        PLUS_EXPR, value, integer_one_node, false);
9438   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9439
9440   /* Now create a declaration for the enum value name.  */
9441
9442   type = TREE_TYPE (value);
9443   type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9444                                       TYPE_PRECISION (integer_type_node)),
9445                                  (TYPE_PRECISION (type)
9446                                   >= TYPE_PRECISION (integer_type_node)
9447                                   && TYPE_UNSIGNED (type)));
9448
9449   decl = build_decl (decl_loc, CONST_DECL, name, type);
9450   DECL_INITIAL (decl) = convert (type, value);
9451   pushdecl (decl);
9452
9453   return tree_cons (decl, value, NULL_TREE);
9454 }
9455
9456 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL.  */
9457
9458 tree
9459 c_simulate_enum_decl (location_t loc, const char *name,
9460                       vec<string_int_pair> *values_ptr)
9461 {
9462   location_t saved_loc = input_location;
9463   input_location = loc;
9464
9465   struct c_enum_contents the_enum;
9466   tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9467
9468   tree value_chain = NULL_TREE;
9469   string_int_pair *value;
9470   vec<string_int_pair> values = *values_ptr;
9471   unsigned int i;
9472   FOR_EACH_VEC_ELT (values, i, value)
9473     {
9474       tree decl = build_enumerator (loc, loc, &the_enum,
9475                                     get_identifier (value->first),
9476                                     build_int_cst (integer_type_node,
9477                                                    value->second));
9478       TREE_CHAIN (decl) = value_chain;
9479       value_chain = decl;
9480     }
9481
9482   finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9483
9484   input_location = saved_loc;
9485   return enumtype;
9486 }
9487
9488 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL.  */
9489
9490 tree
9491 c_simulate_record_decl (location_t loc, const char *name,
9492                         array_slice<const tree> fields)
9493 {
9494   location_t saved_loc = input_location;
9495   input_location = loc;
9496
9497   class c_struct_parse_info *struct_info;
9498   tree ident = get_identifier (name);
9499   tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
9500
9501   for (unsigned int i = 0; i < fields.size (); ++i)
9502     {
9503       DECL_FIELD_CONTEXT (fields[i]) = type;
9504       if (i > 0)
9505         DECL_CHAIN (fields[i - 1]) = fields[i];
9506     }
9507
9508   finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
9509
9510   tree decl = build_decl (loc, TYPE_DECL, ident, type);
9511   set_underlying_type (decl);
9512   lang_hooks.decls.pushdecl (decl);
9513
9514   input_location = saved_loc;
9515   return type;
9516 }
9517 \f
9518 /* Create the FUNCTION_DECL for a function definition.
9519    DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9520    the declaration; they describe the function's name and the type it returns,
9521    but twisted together in a fashion that parallels the syntax of C.
9522
9523    This function creates a binding context for the function body
9524    as well as setting up the FUNCTION_DECL in current_function_decl.
9525
9526    Returns true on success.  If the DECLARATOR is not suitable for a function
9527    (it defines a datum instead), we return false to report a parse error.  */
9528
9529 bool
9530 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9531                 tree attributes)
9532 {
9533   tree decl1, old_decl;
9534   tree restype, resdecl;
9535   location_t loc;
9536
9537   current_function_returns_value = 0;  /* Assume, until we see it does.  */
9538   current_function_returns_null = 0;
9539   current_function_returns_abnormally = 0;
9540   warn_about_return_type = 0;
9541   c_switch_stack = NULL;
9542
9543   /* Indicate no valid break/continue context.  */
9544   in_statement = 0;
9545
9546   decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9547                           &attributes, NULL, NULL, DEPRECATED_NORMAL);
9548   invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9549
9550   /* If the declarator is not suitable for a function definition,
9551      cause a syntax error.  */
9552   if (decl1 == NULL_TREE
9553       || TREE_CODE (decl1) != FUNCTION_DECL)
9554     return false;
9555
9556   loc = DECL_SOURCE_LOCATION (decl1);
9557
9558   /* A nested function is not global.  */
9559   if (current_function_decl != NULL_TREE)
9560     TREE_PUBLIC (decl1) = 0;
9561
9562   c_decl_attributes (&decl1, attributes, 0);
9563
9564   if (DECL_DECLARED_INLINE_P (decl1)
9565       && DECL_UNINLINABLE (decl1)
9566       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9567     warning_at (loc, OPT_Wattributes,
9568                 "inline function %qD given attribute %qs",
9569                 decl1, "noinline");
9570
9571   /* Handle gnu_inline attribute.  */
9572   if (declspecs->inline_p
9573       && !flag_gnu89_inline
9574       && TREE_CODE (decl1) == FUNCTION_DECL
9575       && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9576           || current_function_decl))
9577     {
9578       if (declspecs->storage_class != csc_static)
9579         DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9580     }
9581
9582   announce_function (decl1);
9583
9584   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9585     {
9586       error_at (loc, "return type is an incomplete type");
9587       /* Make it return void instead.  */
9588       TREE_TYPE (decl1)
9589         = build_function_type (void_type_node,
9590                                TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9591     }
9592
9593   if (warn_about_return_type)
9594     warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9595                            : (warn_return_type > 0 ? OPT_Wreturn_type
9596                               : OPT_Wimplicit_int),
9597                       "return type defaults to %<int%>");
9598
9599   /* Make the init_value nonzero so pushdecl knows this is not tentative.
9600      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
9601   DECL_INITIAL (decl1) = error_mark_node;
9602
9603   /* If this definition isn't a prototype and we had a prototype declaration
9604      before, copy the arg type info from that prototype.  */
9605   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9606   if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9607     old_decl = NULL_TREE;
9608
9609   current_function_prototype_locus = UNKNOWN_LOCATION;
9610   current_function_prototype_built_in = false;
9611   current_function_prototype_arg_types = NULL_TREE;
9612   tree newtype = TREE_TYPE (decl1);
9613   tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
9614   if (!prototype_p (newtype))
9615     {
9616       tree oldrt = TREE_TYPE (oldtype);
9617       tree newrt = TREE_TYPE (newtype);
9618       if (old_decl != NULL_TREE
9619           && TREE_CODE (oldtype) == FUNCTION_TYPE
9620           && comptypes (oldrt, newrt))
9621         {
9622           if (stdarg_p (oldtype))
9623             {
9624               auto_diagnostic_group d;
9625               warning_at (loc, 0, "%q+D defined as variadic function "
9626                           "without prototype", decl1);
9627               locate_old_decl (old_decl);
9628             }
9629           TREE_TYPE (decl1) = composite_type (oldtype, newtype);
9630           current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9631           current_function_prototype_built_in
9632             = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9633           current_function_prototype_arg_types
9634             = TYPE_ARG_TYPES (newtype);
9635         }
9636       if (TREE_PUBLIC (decl1))
9637         {
9638           /* If there is an external prototype declaration of this
9639              function, record its location but do not copy information
9640              to this decl.  This may be an invisible declaration
9641              (built-in or in a scope which has finished) or simply
9642              have more refined argument types than any declaration
9643              found above.  */
9644           struct c_binding *b;
9645           for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9646             if (B_IN_SCOPE (b, external_scope))
9647               break;
9648           if (b)
9649             {
9650               tree ext_decl, ext_type;
9651               ext_decl = b->decl;
9652               ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9653               if (TREE_CODE (ext_type) == FUNCTION_TYPE
9654                   && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9655                                 TREE_TYPE (ext_type)))
9656                 {
9657                   current_function_prototype_locus
9658                     = DECL_SOURCE_LOCATION (ext_decl);
9659                   current_function_prototype_built_in
9660                     = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9661                   current_function_prototype_arg_types
9662                     = TYPE_ARG_TYPES (ext_type);
9663                 }
9664             }
9665         }
9666     }
9667
9668   /* Optionally warn of old-fashioned def with no previous prototype.  */
9669   if (warn_strict_prototypes
9670       && old_decl != error_mark_node
9671       && !prototype_p (TREE_TYPE (decl1))
9672       && C_DECL_ISNT_PROTOTYPE (old_decl))
9673     warning_at (loc, OPT_Wstrict_prototypes,
9674                 "function declaration isn%'t a prototype");
9675   /* Optionally warn of any global def with no previous prototype.  */
9676   else if (warn_missing_prototypes
9677            && old_decl != error_mark_node
9678            && TREE_PUBLIC (decl1)
9679            && !MAIN_NAME_P (DECL_NAME (decl1))
9680            && C_DECL_ISNT_PROTOTYPE (old_decl)
9681            && !DECL_DECLARED_INLINE_P (decl1))
9682     warning_at (loc, OPT_Wmissing_prototypes,
9683                 "no previous prototype for %qD", decl1);
9684   /* Optionally warn of any def with no previous prototype
9685      if the function has already been used.  */
9686   else if (warn_missing_prototypes
9687            && old_decl != NULL_TREE
9688            && old_decl != error_mark_node
9689            && TREE_USED (old_decl)
9690            && !prototype_p (TREE_TYPE (old_decl)))
9691     warning_at (loc, OPT_Wmissing_prototypes,
9692                 "%qD was used with no prototype before its definition", decl1);
9693   /* Optionally warn of any global def with no previous declaration.  */
9694   else if (warn_missing_declarations
9695            && TREE_PUBLIC (decl1)
9696            && old_decl == NULL_TREE
9697            && !MAIN_NAME_P (DECL_NAME (decl1))
9698            && !DECL_DECLARED_INLINE_P (decl1))
9699     warning_at (loc, OPT_Wmissing_declarations,
9700                 "no previous declaration for %qD",
9701                 decl1);
9702   /* Optionally warn of any def with no previous declaration
9703      if the function has already been used.  */
9704   else if (warn_missing_declarations
9705            && old_decl != NULL_TREE
9706            && old_decl != error_mark_node
9707            && TREE_USED (old_decl)
9708            && C_DECL_IMPLICIT (old_decl))
9709     warning_at (loc, OPT_Wmissing_declarations,
9710                 "%qD was used with no declaration before its definition", decl1);
9711
9712   /* This function exists in static storage.
9713      (This does not mean `static' in the C sense!)  */
9714   TREE_STATIC (decl1) = 1;
9715
9716   /* This is the earliest point at which we might know the assembler
9717      name of the function.  Thus, if it's set before this, die horribly.  */
9718   gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9719
9720   /* If #pragma weak was used, mark the decl weak now.  */
9721   if (current_scope == file_scope)
9722     maybe_apply_pragma_weak (decl1);
9723
9724   /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
9725   if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9726     {
9727       if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9728           != integer_type_node)
9729         pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9730       else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9731         pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9732                  decl1);
9733
9734       check_main_parameter_types (decl1);
9735
9736       if (!TREE_PUBLIC (decl1))
9737         pedwarn (loc, OPT_Wmain,
9738                  "%qD is normally a non-static function", decl1);
9739     }
9740
9741   tree parms = current_function_arg_info->parms;
9742   if (old_decl)
9743     {
9744       location_t origloc = DECL_SOURCE_LOCATION (old_decl);
9745       warn_parm_array_mismatch (origloc, old_decl, parms);
9746     }
9747
9748   /* Record the decl so that the function name is defined.
9749      If we already have a decl for this name, and it is a FUNCTION_DECL,
9750      use the old decl.  */
9751
9752   current_function_decl = pushdecl (decl1);
9753
9754   if (tree access = build_attr_access_from_parms (parms, false))
9755     decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
9756                      old_decl);
9757
9758   push_scope ();
9759   declare_parm_level ();
9760
9761   restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9762   resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9763   DECL_ARTIFICIAL (resdecl) = 1;
9764   DECL_IGNORED_P (resdecl) = 1;
9765   DECL_RESULT (current_function_decl) = resdecl;
9766
9767   start_fname_decls ();
9768
9769   return true;
9770 }
9771 \f
9772 /* Subroutine of store_parm_decls which handles new-style function
9773    definitions (prototype format). The parms already have decls, so we
9774    need only record them as in effect and complain if any redundant
9775    old-style parm decls were written.  */
9776 static void
9777 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9778 {
9779   tree decl;
9780   c_arg_tag *tag;
9781   unsigned ix;
9782
9783   if (current_scope->bindings)
9784     {
9785       error_at (DECL_SOURCE_LOCATION (fndecl),
9786                 "old-style parameter declarations in prototyped "
9787                 "function definition");
9788
9789       /* Get rid of the old-style declarations.  */
9790       pop_scope ();
9791       push_scope ();
9792     }
9793   /* Don't issue this warning for nested functions, and don't issue this
9794      warning if we got here because ARG_INFO_TYPES was error_mark_node
9795      (this happens when a function definition has just an ellipsis in
9796      its parameter list).  */
9797   else if (!in_system_header_at (input_location)
9798            && !current_function_scope
9799            && arg_info->types != error_mark_node)
9800     warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9801                 "traditional C rejects ISO C style function definitions");
9802
9803   /* Now make all the parameter declarations visible in the function body.
9804      We can bypass most of the grunt work of pushdecl.  */
9805   for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9806     {
9807       DECL_CONTEXT (decl) = current_function_decl;
9808       if (DECL_NAME (decl))
9809         {
9810           bind (DECL_NAME (decl), decl, current_scope,
9811                 /*invisible=*/false, /*nested=*/false,
9812                 UNKNOWN_LOCATION);
9813           if (!TREE_USED (decl))
9814             warn_if_shadowing (decl);
9815         }
9816       else
9817         pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9818                      "ISO C does not support omitting parameter names in "
9819                      "function definitions before C2X");
9820     }
9821
9822   /* Record the parameter list in the function declaration.  */
9823   DECL_ARGUMENTS (fndecl) = arg_info->parms;
9824
9825   /* Now make all the ancillary declarations visible, likewise.  */
9826   for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9827     {
9828       DECL_CONTEXT (decl) = current_function_decl;
9829       if (DECL_NAME (decl))
9830         bind (DECL_NAME (decl), decl, current_scope,
9831               /*invisible=*/false,
9832               /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9833               UNKNOWN_LOCATION);
9834     }
9835
9836   /* And all the tag declarations.  */
9837   FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9838     if (tag->id)
9839       bind (tag->id, tag->type, current_scope,
9840             /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9841 }
9842
9843 /* Subroutine of store_parm_decls which handles old-style function
9844    definitions (separate parameter list and declarations).  */
9845
9846 static void
9847 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9848 {
9849   struct c_binding *b;
9850   tree parm, decl, last;
9851   tree parmids = arg_info->parms;
9852   hash_set<tree> seen_args;
9853
9854   if (!in_system_header_at (input_location))
9855     {
9856       if (flag_isoc2x)
9857         pedwarn (DECL_SOURCE_LOCATION (fndecl),
9858                  OPT_Wold_style_definition, "old-style function definition");
9859       else
9860         warning_at (DECL_SOURCE_LOCATION (fndecl),
9861                     OPT_Wold_style_definition,
9862                     "old-style function definition");
9863     }
9864
9865   if (current_scope->had_vla_unspec)
9866     error ("%<[*]%> not allowed in other than function prototype scope");
9867
9868   /* Match each formal parameter name with its declaration.  Save each
9869      decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
9870   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9871     {
9872       if (TREE_VALUE (parm) == NULL_TREE)
9873         {
9874           error_at (DECL_SOURCE_LOCATION (fndecl),
9875                     "parameter name missing from parameter list");
9876           TREE_PURPOSE (parm) = NULL_TREE;
9877           continue;
9878         }
9879
9880       b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9881       if (b && B_IN_CURRENT_SCOPE (b))
9882         {
9883           decl = b->decl;
9884           /* Skip erroneous parameters.  */
9885           if (decl == error_mark_node)
9886             continue;
9887           /* If we got something other than a PARM_DECL it is an error.  */
9888           if (TREE_CODE (decl) != PARM_DECL)
9889             {
9890               error_at (DECL_SOURCE_LOCATION (decl),
9891                         "%qD declared as a non-parameter", decl);
9892               continue;
9893             }
9894           /* If the declaration is already marked, we have a duplicate
9895              name.  Complain and ignore the duplicate.  */
9896           else if (seen_args.contains (decl))
9897             {
9898               error_at (DECL_SOURCE_LOCATION (decl),
9899                         "multiple parameters named %qD", decl);
9900               TREE_PURPOSE (parm) = NULL_TREE;
9901               continue;
9902             }
9903           /* If the declaration says "void", complain and turn it into
9904              an int.  */
9905           else if (VOID_TYPE_P (TREE_TYPE (decl)))
9906             {
9907               error_at (DECL_SOURCE_LOCATION (decl),
9908                         "parameter %qD declared with void type", decl);
9909               TREE_TYPE (decl) = integer_type_node;
9910               DECL_ARG_TYPE (decl) = integer_type_node;
9911               layout_decl (decl, 0);
9912             }
9913           warn_if_shadowing (decl);
9914         }
9915       /* If no declaration found, default to int.  */
9916       else
9917         {
9918           /* FIXME diagnostics: This should be the location of the argument,
9919              not the FNDECL.  E.g., for an old-style declaration
9920
9921                int f10(v) { blah; }
9922
9923              We should use the location of the V, not the F10.
9924              Unfortunately, the V is an IDENTIFIER_NODE which has no
9925              location.  In the future we need locations for c_arg_info
9926              entries.
9927
9928              See gcc.dg/Wshadow-3.c for an example of this problem. */
9929           decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9930                              PARM_DECL, TREE_VALUE (parm), integer_type_node);
9931           DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9932           pushdecl (decl);
9933           warn_if_shadowing (decl);
9934
9935           if (flag_isoc99)
9936             pedwarn (DECL_SOURCE_LOCATION (decl),
9937                      OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9938                      decl);
9939           else
9940             warning_at (DECL_SOURCE_LOCATION (decl),
9941                         OPT_Wmissing_parameter_type,
9942                         "type of %qD defaults to %<int%>", decl);
9943         }
9944
9945       TREE_PURPOSE (parm) = decl;
9946       seen_args.add (decl);
9947     }
9948
9949   /* Now examine the parms chain for incomplete declarations
9950      and declarations with no corresponding names.  */
9951
9952   for (b = current_scope->bindings; b; b = b->prev)
9953     {
9954       parm = b->decl;
9955       if (TREE_CODE (parm) != PARM_DECL)
9956         continue;
9957
9958       if (TREE_TYPE (parm) != error_mark_node
9959           && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9960         {
9961           error_at (DECL_SOURCE_LOCATION (parm),
9962                     "parameter %qD has incomplete type", parm);
9963           TREE_TYPE (parm) = error_mark_node;
9964         }
9965
9966       if (!seen_args.contains (parm))
9967         {
9968           error_at (DECL_SOURCE_LOCATION (parm),
9969                     "declaration for parameter %qD but no such parameter",
9970                     parm);
9971
9972           /* Pretend the parameter was not missing.
9973              This gets us to a standard state and minimizes
9974              further error messages.  */
9975           parmids = chainon (parmids, tree_cons (parm, 0, 0));
9976         }
9977     }
9978
9979   /* Chain the declarations together in the order of the list of
9980      names.  Store that chain in the function decl, replacing the
9981      list of names.  Update the current scope to match.  */
9982   DECL_ARGUMENTS (fndecl) = NULL_TREE;
9983
9984   for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9985     if (TREE_PURPOSE (parm))
9986       break;
9987   if (parm && TREE_PURPOSE (parm))
9988     {
9989       last = TREE_PURPOSE (parm);
9990       DECL_ARGUMENTS (fndecl) = last;
9991
9992       for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9993         if (TREE_PURPOSE (parm))
9994           {
9995             DECL_CHAIN (last) = TREE_PURPOSE (parm);
9996             last = TREE_PURPOSE (parm);
9997           }
9998       DECL_CHAIN (last) = NULL_TREE;
9999     }
10000
10001   /* If there was a previous prototype,
10002      set the DECL_ARG_TYPE of each argument according to
10003      the type previously specified, and report any mismatches.  */
10004
10005   if (current_function_prototype_arg_types)
10006     {
10007       tree type;
10008       for (parm = DECL_ARGUMENTS (fndecl),
10009              type = current_function_prototype_arg_types;
10010            parm || (type != NULL_TREE
10011                     && TREE_VALUE (type) != error_mark_node
10012                     && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10013            parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10014         {
10015           if (parm == NULL_TREE
10016               || type == NULL_TREE
10017               || (TREE_VALUE (type) != error_mark_node
10018                   && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10019             {
10020               if (current_function_prototype_built_in)
10021                 warning_at (DECL_SOURCE_LOCATION (fndecl),
10022                             0, "number of arguments doesn%'t match "
10023                             "built-in prototype");
10024               else
10025                 {
10026                   /* FIXME diagnostics: This should be the location of
10027                      FNDECL, but there is bug when a prototype is
10028                      declared inside function context, but defined
10029                      outside of it (e.g., gcc.dg/pr15698-2.c).  In
10030                      which case FNDECL gets the location of the
10031                      prototype, not the definition.  */
10032                   error_at (input_location,
10033                             "number of arguments doesn%'t match prototype");
10034
10035                   error_at (current_function_prototype_locus,
10036                             "prototype declaration");
10037                 }
10038               break;
10039             }
10040           /* Type for passing arg must be consistent with that
10041              declared for the arg.  ISO C says we take the unqualified
10042              type for parameters declared with qualified type.  */
10043           if (TREE_TYPE (parm) != error_mark_node
10044               && TREE_VALUE (type) != error_mark_node
10045               && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10046                    != TYPE_ATOMIC (TREE_VALUE (type)))
10047                   || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10048                                  TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10049             {
10050               if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10051                    == TYPE_ATOMIC (TREE_VALUE (type)))
10052                   && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10053                       == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10054                 {
10055                   /* Adjust argument to match prototype.  E.g. a previous
10056                      `int foo(float);' prototype causes
10057                      `int foo(x) float x; {...}' to be treated like
10058                      `int foo(float x) {...}'.  This is particularly
10059                      useful for argument types like uid_t.  */
10060                   DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10061
10062                   if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10063                       && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10064                       && (TYPE_PRECISION (TREE_TYPE (parm))
10065                           < TYPE_PRECISION (integer_type_node)))
10066                     DECL_ARG_TYPE (parm)
10067                       = c_type_promotes_to (TREE_TYPE (parm));
10068
10069                   /* ??? Is it possible to get here with a
10070                      built-in prototype or will it always have
10071                      been diagnosed as conflicting with an
10072                      old-style definition and discarded?  */
10073                   if (current_function_prototype_built_in)
10074                     warning_at (DECL_SOURCE_LOCATION (parm),
10075                                 OPT_Wpedantic, "promoted argument %qD "
10076                                 "doesn%'t match built-in prototype", parm);
10077                   else
10078                     {
10079                       pedwarn (DECL_SOURCE_LOCATION (parm),
10080                                OPT_Wpedantic, "promoted argument %qD "
10081                                "doesn%'t match prototype", parm);
10082                       pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10083                                "prototype declaration");
10084                     }
10085                 }
10086               else
10087                 {
10088                   if (current_function_prototype_built_in)
10089                     warning_at (DECL_SOURCE_LOCATION (parm),
10090                                 0, "argument %qD doesn%'t match "
10091                                 "built-in prototype", parm);
10092                   else
10093                     {
10094                       error_at (DECL_SOURCE_LOCATION (parm),
10095                                 "argument %qD doesn%'t match prototype", parm);
10096                       error_at (current_function_prototype_locus,
10097                                 "prototype declaration");
10098                     }
10099                 }
10100             }
10101         }
10102       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10103     }
10104
10105   /* Otherwise, create a prototype that would match.  */
10106
10107   else
10108     {
10109       tree actual = NULL_TREE, last = NULL_TREE, type;
10110
10111       for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10112         {
10113           type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10114           if (last)
10115             TREE_CHAIN (last) = type;
10116           else
10117             actual = type;
10118           last = type;
10119         }
10120       type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10121       if (last)
10122         TREE_CHAIN (last) = type;
10123       else
10124         actual = type;
10125
10126       /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10127          of the type of this function, but we need to avoid having this
10128          affect the types of other similarly-typed functions, so we must
10129          first force the generation of an identical (but separate) type
10130          node for the relevant function type.  The new node we create
10131          will be a variant of the main variant of the original function
10132          type.  */
10133
10134       TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10135
10136       TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10137     }
10138 }
10139
10140 /* Store parameter declarations passed in ARG_INFO into the current
10141    function declaration.  */
10142
10143 void
10144 store_parm_decls_from (struct c_arg_info *arg_info)
10145 {
10146   current_function_arg_info = arg_info;
10147   store_parm_decls ();
10148 }
10149
10150 /* Called by walk_tree to look for and update context-less labels
10151    or labels with context in the parent function.  */
10152
10153 static tree
10154 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10155 {
10156   tree ctx = static_cast<tree>(data);
10157   if (TREE_CODE (*tp) == LABEL_EXPR
10158       && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10159           || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10160     {
10161       DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10162       *walk_subtrees = 0;
10163     }
10164
10165   return NULL_TREE;
10166 }
10167
10168 /* Store the parameter declarations into the current function declaration.
10169    This is called after parsing the parameter declarations, before
10170    digesting the body of the function.
10171
10172    For an old-style definition, construct a prototype out of the old-style
10173    parameter declarations and inject it into the function's type.  */
10174
10175 void
10176 store_parm_decls (void)
10177 {
10178   tree fndecl = current_function_decl;
10179   bool proto;
10180
10181   /* The argument information block for FNDECL.  */
10182   struct c_arg_info *arg_info = current_function_arg_info;
10183   current_function_arg_info = 0;
10184
10185   /* True if this definition is written with a prototype.  In C2X, an
10186      empty argument list was converted to (void) in grokparms; in
10187      older C standard versions, it does not give the function a type
10188      with a prototype for future calls.  */
10189   proto = arg_info->types != 0;
10190
10191   if (proto)
10192     store_parm_decls_newstyle (fndecl, arg_info);
10193   else
10194     store_parm_decls_oldstyle (fndecl, arg_info);
10195
10196   /* The next call to push_scope will be a function body.  */
10197
10198   next_is_function_body = true;
10199
10200   /* Write a record describing this function definition to the prototypes
10201      file (if requested).  */
10202
10203   gen_aux_info_record (fndecl, 1, 0, proto);
10204
10205   /* Initialize the RTL code for the function.  */
10206   allocate_struct_function (fndecl, false);
10207
10208   if (warn_unused_local_typedefs)
10209     cfun->language = ggc_cleared_alloc<language_function> ();
10210
10211   /* Begin the statement tree for this function.  */
10212   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10213
10214   /* ??? Insert the contents of the pending sizes list into the function
10215      to be evaluated.  The only reason left to have this is
10216         void foo(int n, int array[n++])
10217      because we throw away the array type in favor of a pointer type, and
10218      thus won't naturally see the SAVE_EXPR containing the increment.  All
10219      other pending sizes would be handled by gimplify_parameters.  */
10220   if (arg_info->pending_sizes)
10221     {
10222       /* In very special circumstances, e.g. for code like
10223            _Atomic int i = 5;
10224            void f (int a[i += 2]) {}
10225          we need to execute the atomic assignment on function entry.
10226          But in this case, it is not just a straight store, it has the
10227          op= form, which means that build_atomic_assign has generated
10228          gotos, labels, etc.  Because at that time the function decl
10229          for F has not been created yet, those labels do not have any
10230          function context.  But we have the fndecl now, so update the
10231          labels accordingly.  gimplify_expr would crash otherwise.
10232          Or with nested functions the labels could be created with parent
10233          function's context, while when the statement is emitted at the
10234          start of the nested function, it needs the nested function's
10235          context.  */
10236       walk_tree_without_duplicates (&arg_info->pending_sizes,
10237                                     set_labels_context_r, fndecl);
10238       add_stmt (arg_info->pending_sizes);
10239     }
10240 }
10241
10242 /* Store PARM_DECLs in PARMS into scope temporarily.  Used for
10243    c_finish_omp_declare_simd for function prototypes.  No diagnostics
10244    should be done.  */
10245
10246 void
10247 temp_store_parm_decls (tree fndecl, tree parms)
10248 {
10249   push_scope ();
10250   for (tree p = parms; p; p = DECL_CHAIN (p))
10251     {
10252       DECL_CONTEXT (p) = fndecl;
10253       if (DECL_NAME (p))
10254         bind (DECL_NAME (p), p, current_scope,
10255               /*invisible=*/false, /*nested=*/false,
10256               UNKNOWN_LOCATION);
10257     }
10258 }
10259
10260 /* Undo what temp_store_parm_decls did.  */
10261
10262 void
10263 temp_pop_parm_decls (void)
10264 {
10265   /* Clear all bindings in this temporary scope, so that
10266      pop_scope doesn't create a BLOCK.  */
10267   struct c_binding *b = current_scope->bindings;
10268   current_scope->bindings = NULL;
10269   for (; b; b = free_binding_and_advance (b))
10270     {
10271       gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10272                   || b->decl == error_mark_node);
10273       gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10274       I_SYMBOL_BINDING (b->id) = b->shadowed;
10275       if (b->shadowed && b->shadowed->u.type)
10276         TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10277     }
10278   pop_scope ();
10279 }
10280 \f
10281
10282 /* Finish up a function declaration and compile that function
10283    all the way to assembler language output.  Then free the storage
10284    for the function definition.
10285
10286    This is called after parsing the body of the function definition.  */
10287
10288 void
10289 finish_function (location_t end_loc)
10290 {
10291   tree fndecl = current_function_decl;
10292   
10293   if (c_dialect_objc ())
10294     objc_finish_function ();
10295
10296   if (TREE_CODE (fndecl) == FUNCTION_DECL
10297       && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10298     {
10299       tree args = DECL_ARGUMENTS (fndecl);
10300       for (; args; args = DECL_CHAIN (args))
10301         {
10302           tree type = TREE_TYPE (args);
10303           if (INTEGRAL_TYPE_P (type)
10304               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10305             DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10306         }
10307     }
10308
10309   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10310     BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10311
10312   /* Must mark the RESULT_DECL as being in this function.  */
10313
10314   if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10315     DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10316
10317   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
10318       && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10319       == integer_type_node && flag_isoc99)
10320     {
10321       /* Hack.  We don't want the middle-end to warn that this return
10322          is unreachable, so we mark its location as special.  Using
10323          UNKNOWN_LOCATION has the problem that it gets clobbered in
10324          annotate_one_with_locus.  A cleaner solution might be to
10325          ensure ! should_carry_locus_p (stmt), but that needs a flag.
10326       */
10327       c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10328     }
10329
10330   /* Tie off the statement tree for this function.  */
10331   DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10332
10333   finish_fname_decls ();
10334
10335   /* Complain if there's no return statement only if option specified on
10336      command line.  */
10337   if (warn_return_type > 0
10338       && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10339       && !current_function_returns_value && !current_function_returns_null
10340       /* Don't complain if we are no-return.  */
10341       && !current_function_returns_abnormally
10342       /* Don't complain if we are declared noreturn.  */
10343       && !TREE_THIS_VOLATILE (fndecl)
10344       /* Don't warn for main().  */
10345       && !MAIN_NAME_P (DECL_NAME (fndecl))
10346       /* Or if they didn't actually specify a return type.  */
10347       && !C_FUNCTION_IMPLICIT_INT (fndecl)
10348       /* Normally, with -Wreturn-type, flow will complain, but we might
10349          optimize out static functions.  */
10350       && !TREE_PUBLIC (fndecl)
10351       && targetm.warn_func_return (fndecl)
10352       && warning (OPT_Wreturn_type,
10353                   "no return statement in function returning non-void"))
10354     suppress_warning (fndecl, OPT_Wreturn_type);
10355
10356   /* Complain about parameters that are only set, but never otherwise used.  */
10357   if (warn_unused_but_set_parameter)
10358     {
10359       tree decl;
10360
10361       for (decl = DECL_ARGUMENTS (fndecl);
10362            decl;
10363            decl = DECL_CHAIN (decl))
10364         if (TREE_USED (decl)
10365             && TREE_CODE (decl) == PARM_DECL
10366             && !DECL_READ_P (decl)
10367             && DECL_NAME (decl)
10368             && !DECL_ARTIFICIAL (decl)
10369             && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10370           warning_at (DECL_SOURCE_LOCATION (decl),
10371                       OPT_Wunused_but_set_parameter,
10372                       "parameter %qD set but not used", decl);
10373     }
10374
10375   /* Complain about locally defined typedefs that are not used in this
10376      function.  */
10377   maybe_warn_unused_local_typedefs ();
10378
10379   /* Possibly warn about unused parameters.  */
10380   if (warn_unused_parameter)
10381     do_warn_unused_parameter (fndecl);
10382
10383   /* Store the end of the function, so that we get good line number
10384      info for the epilogue.  */
10385   cfun->function_end_locus = end_loc;
10386
10387   /* Finalize the ELF visibility for the function.  */
10388   c_determine_visibility (fndecl);
10389
10390   /* For GNU C extern inline functions disregard inline limits.  */
10391   if (DECL_EXTERNAL (fndecl)
10392       && DECL_DECLARED_INLINE_P (fndecl)
10393       && (flag_gnu89_inline
10394           || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10395     DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10396
10397   /* Genericize before inlining.  Delay genericizing nested functions
10398      until their parent function is genericized.  Since finalizing
10399      requires GENERIC, delay that as well.  */
10400
10401   if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10402       && !undef_nested_function)
10403     {
10404       if (!decl_function_context (fndecl))
10405         {
10406           invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10407           c_genericize (fndecl);
10408
10409           /* ??? Objc emits functions after finalizing the compilation unit.
10410              This should be cleaned up later and this conditional removed.  */
10411           if (symtab->global_info_ready)
10412             {
10413               cgraph_node::add_new_function (fndecl, false);
10414               return;
10415             }
10416           cgraph_node::finalize_function (fndecl, false);
10417         }
10418       else
10419         {
10420           /* Register this function with cgraph just far enough to get it
10421             added to our parent's nested function list.  Handy, since the
10422             C front end doesn't have such a list.  */
10423           (void) cgraph_node::get_create (fndecl);
10424         }
10425     }
10426
10427   if (!decl_function_context (fndecl))
10428     undef_nested_function = false;
10429
10430   if (cfun->language != NULL)
10431     {
10432       ggc_free (cfun->language);
10433       cfun->language = NULL;
10434     }
10435
10436   /* We're leaving the context of this function, so zap cfun.
10437      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10438      tree_rest_of_compilation.  */
10439   set_cfun (NULL);
10440   invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10441   current_function_decl = NULL;
10442 }
10443 \f
10444 /* Check the declarations given in a for-loop for satisfying the C99
10445    constraints.  If exactly one such decl is found, return it.  LOC is
10446    the location of the opening parenthesis of the for loop.  The last
10447    parameter allows you to control the "for loop initial declarations
10448    are only allowed in C99 mode".  Normally, you should pass
10449    flag_isoc99 as that parameter.  But in some cases (Objective-C
10450    foreach loop, for example) we want to run the checks in this
10451    function even if not in C99 mode, so we allow the caller to turn
10452    off the error about not being in C99 mode.
10453 */
10454
10455 tree
10456 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10457 {
10458   struct c_binding *b;
10459   tree one_decl = NULL_TREE;
10460   int n_decls = 0;
10461
10462   if (!turn_off_iso_c99_error)
10463     {
10464       static bool hint = true;
10465       /* If we get here, declarations have been used in a for loop without
10466          the C99 for loop scope.  This doesn't make much sense, so don't
10467          allow it.  */
10468       auto_diagnostic_group d;
10469       error_at (loc, "%<for%> loop initial declarations "
10470                 "are only allowed in C99 or C11 mode");
10471       if (hint)
10472         {
10473           inform (loc,
10474                   "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10475                   "%<-std=gnu11%> to compile your code");
10476           hint = false;
10477         }
10478       return NULL_TREE;
10479     }
10480   else
10481     pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10482                  "initial declarations");
10483
10484   /* C99 subclause 6.8.5 paragraph 3:
10485
10486        [#3]  The  declaration  part  of  a for statement shall only
10487        declare identifiers for objects having storage class auto or
10488        register.
10489
10490      It isn't clear whether, in this sentence, "identifiers" binds to
10491      "shall only declare" or to "objects" - that is, whether all identifiers
10492      declared must be identifiers for objects, or whether the restriction
10493      only applies to those that are.  (A question on this in comp.std.c
10494      in November 2000 received no answer.)  We implement the strictest
10495      interpretation, to avoid creating an extension which later causes
10496      problems.  */
10497
10498   for (b = current_scope->bindings; b; b = b->prev)
10499     {
10500       tree id = b->id;
10501       tree decl = b->decl;
10502
10503       if (!id)
10504         continue;
10505
10506       switch (TREE_CODE (decl))
10507         {
10508         case VAR_DECL:
10509           {
10510             location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10511             if (TREE_STATIC (decl))
10512               error_at (decl_loc,
10513                         "declaration of static variable %qD in %<for%> loop "
10514                         "initial declaration", decl);
10515             else if (DECL_EXTERNAL (decl))
10516               error_at (decl_loc,
10517                         "declaration of %<extern%> variable %qD in %<for%> loop "
10518                         "initial declaration", decl);
10519           }
10520           break;
10521
10522         case RECORD_TYPE:
10523           error_at (loc,
10524                     "%<struct %E%> declared in %<for%> loop initial "
10525                     "declaration", id);
10526           break;
10527         case UNION_TYPE:
10528           error_at (loc,
10529                     "%<union %E%> declared in %<for%> loop initial declaration",
10530                     id);
10531           break;
10532         case ENUMERAL_TYPE:
10533           error_at (loc, "%<enum %E%> declared in %<for%> loop "
10534                     "initial declaration", id);
10535           break;
10536         default:
10537           error_at (loc, "declaration of non-variable "
10538                     "%qD in %<for%> loop initial declaration", decl);
10539         }
10540
10541       n_decls++;
10542       one_decl = decl;
10543     }
10544
10545   return n_decls == 1 ? one_decl : NULL_TREE;
10546 }
10547 \f
10548 /* Save and reinitialize the variables
10549    used during compilation of a C function.  */
10550
10551 void
10552 c_push_function_context (void)
10553 {
10554   struct language_function *p = cfun->language;
10555   /* cfun->language might have been already allocated by the use of
10556      -Wunused-local-typedefs.  In that case, just re-use it.  */
10557   if (p == NULL)
10558     cfun->language = p = ggc_cleared_alloc<language_function> ();
10559
10560   p->base.x_stmt_tree = c_stmt_tree;
10561   c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10562   p->x_in_statement = in_statement;
10563   p->x_switch_stack = c_switch_stack;
10564   p->arg_info = current_function_arg_info;
10565   p->returns_value = current_function_returns_value;
10566   p->returns_null = current_function_returns_null;
10567   p->returns_abnormally = current_function_returns_abnormally;
10568   p->warn_about_return_type = warn_about_return_type;
10569
10570   push_function_context ();
10571 }
10572
10573 /* Restore the variables used during compilation of a C function.  */
10574
10575 void
10576 c_pop_function_context (void)
10577 {
10578   struct language_function *p;
10579
10580   pop_function_context ();
10581   p = cfun->language;
10582
10583   /* When -Wunused-local-typedefs is in effect, cfun->languages is
10584      used to store data throughout the life time of the current cfun,
10585      So don't deallocate it.  */
10586   if (!warn_unused_local_typedefs)
10587     cfun->language = NULL;
10588
10589   if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10590       && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10591     {
10592       /* Stop pointing to the local nodes about to be freed.  */
10593       /* But DECL_INITIAL must remain nonzero so we know this
10594          was an actual function definition.  */
10595       DECL_INITIAL (current_function_decl) = error_mark_node;
10596       DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10597     }
10598
10599   c_stmt_tree = p->base.x_stmt_tree;
10600   p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10601   in_statement = p->x_in_statement;
10602   c_switch_stack = p->x_switch_stack;
10603   current_function_arg_info = p->arg_info;
10604   current_function_returns_value = p->returns_value;
10605   current_function_returns_null = p->returns_null;
10606   current_function_returns_abnormally = p->returns_abnormally;
10607   warn_about_return_type = p->warn_about_return_type;
10608 }
10609
10610 /* The functions below are required for functionality of doing
10611    function at once processing in the C front end. Currently these
10612    functions are not called from anywhere in the C front end, but as
10613    these changes continue, that will change.  */
10614
10615 /* Returns the stmt_tree (if any) to which statements are currently
10616    being added.  If there is no active statement-tree, NULL is
10617    returned.  */
10618
10619 stmt_tree
10620 current_stmt_tree (void)
10621 {
10622   return &c_stmt_tree;
10623 }
10624
10625 /* Return the global value of T as a symbol.  */
10626
10627 tree
10628 identifier_global_value (tree t)
10629 {
10630   struct c_binding *b;
10631
10632   for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10633     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10634       return b->decl;
10635
10636   return NULL_TREE;
10637 }
10638
10639 /* Return the global value of tag T as a symbol.  */
10640
10641 tree
10642 identifier_global_tag (tree t)
10643 {
10644   struct c_binding *b;
10645
10646   for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10647     if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10648       return b->decl;
10649
10650   return NULL_TREE;
10651 }
10652
10653 /* Returns true if NAME refers to a built-in function or function-like
10654    operator.  */
10655
10656 bool
10657 names_builtin_p (const char *name)
10658 {
10659   tree id = get_identifier (name);
10660   if (tree decl = identifier_global_value (id))
10661     return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
10662
10663   /* Also detect common reserved C words that aren't strictly built-in
10664      functions.  */
10665   switch (C_RID_CODE (id))
10666     {
10667     case RID_BUILTIN_CONVERTVECTOR:
10668     case RID_BUILTIN_HAS_ATTRIBUTE:
10669     case RID_BUILTIN_SHUFFLE:
10670     case RID_BUILTIN_SHUFFLEVECTOR:
10671     case RID_BUILTIN_ASSOC_BARRIER:
10672     case RID_CHOOSE_EXPR:
10673     case RID_OFFSETOF:
10674     case RID_TYPES_COMPATIBLE_P:
10675       return true;
10676     default:
10677       break;
10678     }
10679
10680   return false;
10681 }
10682
10683 /* In C, the only C-linkage public declaration is at file scope.  */
10684
10685 tree
10686 c_linkage_bindings (tree name)
10687 {
10688   return identifier_global_value (name);
10689 }
10690
10691 /* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
10692    otherwise the name is found in ridpointers from RID_INDEX.  */
10693
10694 void
10695 record_builtin_type (enum rid rid_index, const char *name, tree type)
10696 {
10697   tree id, decl;
10698   if (name == 0)
10699     id = ridpointers[(int) rid_index];
10700   else
10701     id = get_identifier (name);
10702   decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10703   pushdecl (decl);
10704   if (debug_hooks->type_decl)
10705     debug_hooks->type_decl (decl, false);
10706 }
10707
10708 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
10709
10710 struct c_parm *
10711 build_c_parm (struct c_declspecs *specs, tree attrs,
10712               struct c_declarator *declarator,
10713               location_t loc)
10714 {
10715   struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10716   ret->specs = specs;
10717   ret->attrs = attrs;
10718   ret->declarator = declarator;
10719   ret->loc = loc;
10720   return ret;
10721 }
10722
10723 /* Return a declarator with nested attributes.  TARGET is the inner
10724    declarator to which these attributes apply.  ATTRS are the
10725    attributes.  */
10726
10727 struct c_declarator *
10728 build_attrs_declarator (tree attrs, struct c_declarator *target)
10729 {
10730   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10731   ret->kind = cdk_attrs;
10732   ret->declarator = target;
10733   ret->u.attrs = attrs;
10734   return ret;
10735 }
10736
10737 /* Return a declarator for a function with arguments specified by ARGS
10738    and return type specified by TARGET.  */
10739
10740 struct c_declarator *
10741 build_function_declarator (struct c_arg_info *args,
10742                            struct c_declarator *target)
10743 {
10744   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10745   ret->kind = cdk_function;
10746   ret->declarator = target;
10747   ret->u.arg_info = args;
10748   return ret;
10749 }
10750
10751 /* Return a declarator for the identifier IDENT (which may be
10752    NULL_TREE for an abstract declarator).  */
10753
10754 struct c_declarator *
10755 build_id_declarator (tree ident)
10756 {
10757   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10758   ret->kind = cdk_id;
10759   ret->declarator = 0;
10760   ret->u.id.id = ident;
10761   ret->u.id.attrs = NULL_TREE;
10762   /* Default value - may get reset to a more precise location. */
10763   ret->id_loc = input_location;
10764   return ret;
10765 }
10766
10767 /* Return something to represent absolute declarators containing a *.
10768    TARGET is the absolute declarator that the * contains.
10769    TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10770    to apply to the pointer type.  */
10771
10772 struct c_declarator *
10773 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10774                          struct c_declarator *target)
10775 {
10776   tree attrs;
10777   int quals = 0;
10778   struct c_declarator *itarget = target;
10779   struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10780   if (type_quals_attrs)
10781     {
10782       attrs = type_quals_attrs->attrs;
10783       quals = quals_from_declspecs (type_quals_attrs);
10784       if (attrs != NULL_TREE)
10785         itarget = build_attrs_declarator (attrs, target);
10786     }
10787   ret->kind = cdk_pointer;
10788   ret->declarator = itarget;
10789   ret->u.pointer_quals = quals;
10790   return ret;
10791 }
10792
10793 /* Return a pointer to a structure for an empty list of declaration
10794    specifiers.  */
10795
10796 struct c_declspecs *
10797 build_null_declspecs (void)
10798 {
10799   struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10800   memset (ret, 0, sizeof *ret);
10801   ret->align_log = -1;
10802   ret->typespec_word = cts_none;
10803   ret->storage_class = csc_none;
10804   ret->expr_const_operands = true;
10805   ret->typespec_kind = ctsk_none;
10806   ret->address_space = ADDR_SPACE_GENERIC;
10807   return ret;
10808 }
10809
10810 /* Add the address space ADDRSPACE to the declaration specifiers
10811    SPECS, returning SPECS.  */
10812
10813 struct c_declspecs *
10814 declspecs_add_addrspace (location_t location,
10815                          struct c_declspecs *specs, addr_space_t as)
10816 {
10817   specs->non_sc_seen_p = true;
10818   specs->declspecs_seen_p = true;
10819   specs->non_std_attrs_seen_p = true;
10820
10821   if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10822       && specs->address_space != as)
10823     error ("incompatible address space qualifiers %qs and %qs",
10824            c_addr_space_name (as),
10825            c_addr_space_name (specs->address_space));
10826   else
10827     {
10828       specs->address_space = as;
10829       specs->locations[cdw_address_space] = location;
10830     }
10831   return specs;
10832 }
10833
10834 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10835    returning SPECS.  */
10836
10837 struct c_declspecs *
10838 declspecs_add_qual (location_t loc,
10839                     struct c_declspecs *specs, tree qual)
10840 {
10841   enum rid i;
10842   bool dupe = false;
10843   specs->non_sc_seen_p = true;
10844   specs->declspecs_seen_p = true;
10845   specs->non_std_attrs_seen_p = true;
10846   gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10847               && C_IS_RESERVED_WORD (qual));
10848   i = C_RID_CODE (qual);
10849   location_t prev_loc = UNKNOWN_LOCATION;
10850   switch (i)
10851     {
10852     case RID_CONST:
10853       dupe = specs->const_p;
10854       specs->const_p = true;
10855       prev_loc = specs->locations[cdw_const];
10856       specs->locations[cdw_const] = loc;
10857       break;
10858     case RID_VOLATILE:
10859       dupe = specs->volatile_p;
10860       specs->volatile_p = true;
10861       prev_loc = specs->locations[cdw_volatile];
10862       specs->locations[cdw_volatile] = loc;
10863       break;
10864     case RID_RESTRICT:
10865       dupe = specs->restrict_p;
10866       specs->restrict_p = true;
10867       prev_loc = specs->locations[cdw_restrict];
10868       specs->locations[cdw_restrict] = loc;
10869       break;
10870     case RID_ATOMIC:
10871       dupe = specs->atomic_p;
10872       specs->atomic_p = true;
10873       prev_loc = specs->locations[cdw_atomic];
10874       specs->locations[cdw_atomic] = loc;
10875       break;
10876     default:
10877       gcc_unreachable ();
10878     }
10879   if (dupe)
10880     {
10881       bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10882                                  "duplicate %qE declaration specifier", qual);
10883       if (!warned
10884           && warn_duplicate_decl_specifier
10885           && prev_loc >= RESERVED_LOCATION_COUNT
10886           && !from_macro_expansion_at (prev_loc)
10887           && !from_macro_expansion_at (loc))
10888         warning_at (loc, OPT_Wduplicate_decl_specifier,
10889                     "duplicate %qE declaration specifier", qual);
10890     }
10891   return specs;
10892 }
10893
10894 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10895    returning SPECS.  */
10896
10897 struct c_declspecs *
10898 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10899                     struct c_typespec spec)
10900 {
10901   tree type = spec.spec;
10902   specs->non_sc_seen_p = true;
10903   specs->declspecs_seen_p = true;
10904   specs->non_std_attrs_seen_p = true;
10905   specs->typespec_kind = spec.kind;
10906   if (TREE_DEPRECATED (type))
10907     specs->deprecated_p = true;
10908   if (TREE_UNAVAILABLE (type))
10909     specs->unavailable_p = true;
10910
10911   /* Handle type specifier keywords.  */
10912   if (TREE_CODE (type) == IDENTIFIER_NODE
10913       && C_IS_RESERVED_WORD (type)
10914       && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10915     {
10916       enum rid i = C_RID_CODE (type);
10917       if (specs->type)
10918         {
10919           error_at (loc, "two or more data types in declaration specifiers");
10920           return specs;
10921         }
10922       if ((int) i <= (int) RID_LAST_MODIFIER)
10923         {
10924           /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat".  */
10925           bool dupe = false;
10926           switch (i)
10927             {
10928             case RID_LONG:
10929               if (specs->long_long_p)
10930                 {
10931                   error_at (loc, "%<long long long%> is too long for GCC");
10932                   break;
10933                 }
10934               if (specs->long_p)
10935                 {
10936                   if (specs->typespec_word == cts_double)
10937                     {
10938                       error_at (loc,
10939                                 ("both %<long long%> and %<double%> in "
10940                                  "declaration specifiers"));
10941                       break;
10942                     }
10943                   pedwarn_c90 (loc, OPT_Wlong_long,
10944                                "ISO C90 does not support %<long long%>");
10945                   specs->long_long_p = 1;
10946                   specs->locations[cdw_long_long] = loc;
10947                   break;
10948                 }
10949               if (specs->short_p)
10950                 error_at (loc,
10951                           ("both %<long%> and %<short%> in "
10952                            "declaration specifiers"));
10953               else if (specs->typespec_word == cts_auto_type)
10954                 error_at (loc,
10955                           ("both %<long%> and %<__auto_type%> in "
10956                            "declaration specifiers"));
10957               else if (specs->typespec_word == cts_void)
10958                 error_at (loc,
10959                           ("both %<long%> and %<void%> in "
10960                            "declaration specifiers"));
10961               else if (specs->typespec_word == cts_int_n)
10962                   error_at (loc,
10963                             ("both %<long%> and %<__int%d%> in "
10964                              "declaration specifiers"),
10965                             int_n_data[specs->int_n_idx].bitsize);
10966               else if (specs->typespec_word == cts_bool)
10967                 error_at (loc,
10968                           ("both %<long%> and %<_Bool%> in "
10969                            "declaration specifiers"));
10970               else if (specs->typespec_word == cts_char)
10971                 error_at (loc,
10972                           ("both %<long%> and %<char%> in "
10973                            "declaration specifiers"));
10974               else if (specs->typespec_word == cts_float)
10975                 error_at (loc,
10976                           ("both %<long%> and %<float%> in "
10977                            "declaration specifiers"));
10978               else if (specs->typespec_word == cts_floatn_nx)
10979                 error_at (loc,
10980                           ("both %<long%> and %<_Float%d%s%> in "
10981                            "declaration specifiers"),
10982                           floatn_nx_types[specs->floatn_nx_idx].n,
10983                           (floatn_nx_types[specs->floatn_nx_idx].extended
10984                            ? "x"
10985                            : ""));
10986               else if (specs->typespec_word == cts_dfloat32)
10987                 error_at (loc,
10988                           ("both %<long%> and %<_Decimal32%> in "
10989                            "declaration specifiers"));
10990               else if (specs->typespec_word == cts_dfloat64)
10991                 error_at (loc,
10992                           ("both %<long%> and %<_Decimal64%> in "
10993                            "declaration specifiers"));
10994               else if (specs->typespec_word == cts_dfloat128)
10995                 error_at (loc,
10996                           ("both %<long%> and %<_Decimal128%> in "
10997                            "declaration specifiers"));
10998               else
10999                 {
11000                   specs->long_p = true;
11001                   specs->locations[cdw_long] = loc;
11002                 }
11003               break;
11004             case RID_SHORT:
11005               dupe = specs->short_p;
11006               if (specs->long_p)
11007                 error_at (loc,
11008                           ("both %<long%> and %<short%> in "
11009                            "declaration specifiers"));
11010               else if (specs->typespec_word == cts_auto_type)
11011                 error_at (loc,
11012                           ("both %<short%> and %<__auto_type%> in "
11013                            "declaration specifiers"));
11014               else if (specs->typespec_word == cts_void)
11015                 error_at (loc,
11016                           ("both %<short%> and %<void%> in "
11017                            "declaration specifiers"));
11018               else if (specs->typespec_word == cts_int_n)
11019                 error_at (loc,
11020                           ("both %<short%> and %<__int%d%> in "
11021                            "declaration specifiers"),
11022                           int_n_data[specs->int_n_idx].bitsize);
11023               else if (specs->typespec_word == cts_bool)
11024                 error_at (loc,
11025                           ("both %<short%> and %<_Bool%> in "
11026                            "declaration specifiers"));
11027               else if (specs->typespec_word == cts_char)
11028                 error_at (loc,
11029                           ("both %<short%> and %<char%> in "
11030                            "declaration specifiers"));
11031               else if (specs->typespec_word == cts_float)
11032                 error_at (loc,
11033                           ("both %<short%> and %<float%> in "
11034                            "declaration specifiers"));
11035               else if (specs->typespec_word == cts_double)
11036                 error_at (loc,
11037                           ("both %<short%> and %<double%> in "
11038                            "declaration specifiers"));
11039               else if (specs->typespec_word == cts_floatn_nx)
11040                 error_at (loc,
11041                           ("both %<short%> and %<_Float%d%s%> in "
11042                            "declaration specifiers"),
11043                           floatn_nx_types[specs->floatn_nx_idx].n,
11044                           (floatn_nx_types[specs->floatn_nx_idx].extended
11045                            ? "x"
11046                            : ""));
11047               else if (specs->typespec_word == cts_dfloat32)
11048                 error_at (loc,
11049                           ("both %<short%> and %<_Decimal32%> in "
11050                            "declaration specifiers"));
11051               else if (specs->typespec_word == cts_dfloat64)
11052                 error_at (loc,
11053                           ("both %<short%> and %<_Decimal64%> in "
11054                            "declaration specifiers"));
11055               else if (specs->typespec_word == cts_dfloat128)
11056                 error_at (loc,
11057                           ("both %<short%> and %<_Decimal128%> in "
11058                            "declaration specifiers"));
11059               else
11060                 {
11061                   specs->short_p = true;
11062                   specs->locations[cdw_short] = loc;
11063                 }
11064               break;
11065             case RID_SIGNED:
11066               dupe = specs->signed_p;
11067               if (specs->unsigned_p)
11068                 error_at (loc,
11069                           ("both %<signed%> and %<unsigned%> in "
11070                            "declaration specifiers"));
11071               else if (specs->typespec_word == cts_auto_type)
11072                 error_at (loc,
11073                           ("both %<signed%> and %<__auto_type%> in "
11074                            "declaration specifiers"));
11075               else if (specs->typespec_word == cts_void)
11076                 error_at (loc,
11077                           ("both %<signed%> and %<void%> in "
11078                            "declaration specifiers"));
11079               else if (specs->typespec_word == cts_bool)
11080                 error_at (loc,
11081                           ("both %<signed%> and %<_Bool%> in "
11082                            "declaration specifiers"));
11083               else if (specs->typespec_word == cts_float)
11084                 error_at (loc,
11085                           ("both %<signed%> and %<float%> in "
11086                            "declaration specifiers"));
11087               else if (specs->typespec_word == cts_double)
11088                 error_at (loc,
11089                           ("both %<signed%> and %<double%> in "
11090                            "declaration specifiers"));
11091               else if (specs->typespec_word == cts_floatn_nx)
11092                 error_at (loc,
11093                           ("both %<signed%> and %<_Float%d%s%> in "
11094                            "declaration specifiers"),
11095                           floatn_nx_types[specs->floatn_nx_idx].n,
11096                           (floatn_nx_types[specs->floatn_nx_idx].extended
11097                            ? "x"
11098                            : ""));
11099               else if (specs->typespec_word == cts_dfloat32)
11100                 error_at (loc,
11101                           ("both %<signed%> and %<_Decimal32%> in "
11102                            "declaration specifiers"));
11103               else if (specs->typespec_word == cts_dfloat64)
11104                 error_at (loc,
11105                           ("both %<signed%> and %<_Decimal64%> in "
11106                            "declaration specifiers"));
11107               else if (specs->typespec_word == cts_dfloat128)
11108                 error_at (loc,
11109                           ("both %<signed%> and %<_Decimal128%> in "
11110                            "declaration specifiers"));
11111               else
11112                 {
11113                   specs->signed_p = true;
11114                   specs->locations[cdw_signed] = loc;
11115                 }
11116               break;
11117             case RID_UNSIGNED:
11118               dupe = specs->unsigned_p;
11119               if (specs->signed_p)
11120                 error_at (loc,
11121                           ("both %<signed%> and %<unsigned%> in "
11122                            "declaration specifiers"));
11123               else if (specs->typespec_word == cts_auto_type)
11124                 error_at (loc,
11125                           ("both %<unsigned%> and %<__auto_type%> in "
11126                            "declaration specifiers"));
11127               else if (specs->typespec_word == cts_void)
11128                 error_at (loc,
11129                           ("both %<unsigned%> and %<void%> in "
11130                            "declaration specifiers"));
11131               else if (specs->typespec_word == cts_bool)
11132                 error_at (loc,
11133                           ("both %<unsigned%> and %<_Bool%> in "
11134                            "declaration specifiers"));
11135               else if (specs->typespec_word == cts_float)
11136                 error_at (loc,
11137                           ("both %<unsigned%> and %<float%> in "
11138                            "declaration specifiers"));
11139               else if (specs->typespec_word == cts_double)
11140                 error_at (loc,
11141                           ("both %<unsigned%> and %<double%> in "
11142                            "declaration specifiers"));
11143               else if (specs->typespec_word == cts_floatn_nx)
11144                 error_at (loc,
11145                           ("both %<unsigned%> and %<_Float%d%s%> in "
11146                            "declaration specifiers"),
11147                           floatn_nx_types[specs->floatn_nx_idx].n,
11148                           (floatn_nx_types[specs->floatn_nx_idx].extended
11149                            ? "x"
11150                            : ""));
11151               else if (specs->typespec_word == cts_dfloat32)
11152                 error_at (loc,
11153                           ("both %<unsigned%> and %<_Decimal32%> in "
11154                            "declaration specifiers"));
11155               else if (specs->typespec_word == cts_dfloat64)
11156                 error_at (loc,
11157                           ("both %<unsigned%> and %<_Decimal64%> in "
11158                            "declaration specifiers"));
11159               else if (specs->typespec_word == cts_dfloat128)
11160                 error_at (loc,
11161                           ("both %<unsigned%> and %<_Decimal128%> in "
11162                            "declaration specifiers"));
11163               else
11164                 {
11165                   specs->unsigned_p = true;
11166                   specs->locations[cdw_unsigned] = loc;
11167                 }
11168               break;
11169             case RID_COMPLEX:
11170               dupe = specs->complex_p;
11171               if (!in_system_header_at (loc))
11172                 pedwarn_c90 (loc, OPT_Wpedantic,
11173                              "ISO C90 does not support complex types");
11174               if (specs->typespec_word == cts_auto_type)
11175                 error_at (loc,
11176                           ("both %<complex%> and %<__auto_type%> in "
11177                            "declaration specifiers"));
11178               else if (specs->typespec_word == cts_void)
11179                 error_at (loc,
11180                           ("both %<complex%> and %<void%> in "
11181                            "declaration specifiers"));
11182               else if (specs->typespec_word == cts_bool)
11183                 error_at (loc,
11184                           ("both %<complex%> and %<_Bool%> in "
11185                            "declaration specifiers"));
11186               else if (specs->typespec_word == cts_dfloat32)
11187                 error_at (loc,
11188                           ("both %<complex%> and %<_Decimal32%> in "
11189                            "declaration specifiers"));
11190               else if (specs->typespec_word == cts_dfloat64)
11191                 error_at (loc,
11192                           ("both %<complex%> and %<_Decimal64%> in "
11193                            "declaration specifiers"));
11194               else if (specs->typespec_word == cts_dfloat128)
11195                 error_at (loc,
11196                           ("both %<complex%> and %<_Decimal128%> in "
11197                            "declaration specifiers"));
11198               else if (specs->typespec_word == cts_fract)
11199                 error_at (loc,
11200                           ("both %<complex%> and %<_Fract%> in "
11201                            "declaration specifiers"));
11202               else if (specs->typespec_word == cts_accum)
11203                 error_at (loc,
11204                           ("both %<complex%> and %<_Accum%> in "
11205                            "declaration specifiers"));
11206               else if (specs->saturating_p)
11207                 error_at (loc,
11208                           ("both %<complex%> and %<_Sat%> in "
11209                            "declaration specifiers"));
11210               else
11211                 {
11212                   specs->complex_p = true;
11213                   specs->locations[cdw_complex] = loc;
11214                 }
11215               break;
11216             case RID_SAT:
11217               dupe = specs->saturating_p;
11218               pedwarn (loc, OPT_Wpedantic,
11219                        "ISO C does not support saturating types");
11220               if (specs->typespec_word == cts_int_n)
11221                 {
11222                   error_at (loc,
11223                             ("both %<_Sat%> and %<__int%d%> in "
11224                              "declaration specifiers"),
11225                             int_n_data[specs->int_n_idx].bitsize);
11226                 }
11227               else if (specs->typespec_word == cts_auto_type)
11228                 error_at (loc,
11229                           ("both %<_Sat%> and %<__auto_type%> in "
11230                            "declaration specifiers"));
11231               else if (specs->typespec_word == cts_void)
11232                 error_at (loc,
11233                           ("both %<_Sat%> and %<void%> in "
11234                            "declaration specifiers"));
11235               else if (specs->typespec_word == cts_bool)
11236                 error_at (loc,
11237                           ("both %<_Sat%> and %<_Bool%> in "
11238                            "declaration specifiers"));
11239               else if (specs->typespec_word == cts_char)
11240                 error_at (loc,
11241                           ("both %<_Sat%> and %<char%> in "
11242                            "declaration specifiers"));
11243               else if (specs->typespec_word == cts_int)
11244                 error_at (loc,
11245                           ("both %<_Sat%> and %<int%> in "
11246                            "declaration specifiers"));
11247               else if (specs->typespec_word == cts_float)
11248                 error_at (loc,
11249                           ("both %<_Sat%> and %<float%> in "
11250                            "declaration specifiers"));
11251               else if (specs->typespec_word == cts_double)
11252                 error_at (loc,
11253                           ("both %<_Sat%> and %<double%> in "
11254                            "declaration specifiers"));
11255               else if (specs->typespec_word == cts_floatn_nx)
11256                 error_at (loc,
11257                           ("both %<_Sat%> and %<_Float%d%s%> in "
11258                            "declaration specifiers"),
11259                           floatn_nx_types[specs->floatn_nx_idx].n,
11260                           (floatn_nx_types[specs->floatn_nx_idx].extended
11261                            ? "x"
11262                            : ""));
11263               else if (specs->typespec_word == cts_dfloat32)
11264                 error_at (loc,
11265                           ("both %<_Sat%> and %<_Decimal32%> in "
11266                            "declaration specifiers"));
11267               else if (specs->typespec_word == cts_dfloat64)
11268                 error_at (loc,
11269                           ("both %<_Sat%> and %<_Decimal64%> in "
11270                            "declaration specifiers"));
11271               else if (specs->typespec_word == cts_dfloat128)
11272                 error_at (loc,
11273                           ("both %<_Sat%> and %<_Decimal128%> in "
11274                            "declaration specifiers"));
11275               else if (specs->complex_p)
11276                 error_at (loc,
11277                           ("both %<_Sat%> and %<complex%> in "
11278                            "declaration specifiers"));
11279               else
11280                 {
11281                   specs->saturating_p = true;
11282                   specs->locations[cdw_saturating] = loc;
11283                 }
11284               break;
11285             default:
11286               gcc_unreachable ();
11287             }
11288
11289           if (dupe)
11290             error_at (loc, "duplicate %qE", type);
11291
11292           return specs;
11293         }
11294       else
11295         {
11296           /* "void", "_Bool", "char", "int", "float", "double",
11297              "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11298              "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11299              "__auto_type".  */
11300           if (specs->typespec_word != cts_none)
11301             {
11302               error_at (loc,
11303                         "two or more data types in declaration specifiers");
11304               return specs;
11305             }
11306           switch (i)
11307             {
11308             case RID_AUTO_TYPE:
11309               if (specs->long_p)
11310                 error_at (loc,
11311                           ("both %<long%> and %<__auto_type%> in "
11312                            "declaration specifiers"));
11313               else if (specs->short_p)
11314                 error_at (loc,
11315                           ("both %<short%> and %<__auto_type%> in "
11316                            "declaration specifiers"));
11317               else if (specs->signed_p)
11318                 error_at (loc,
11319                           ("both %<signed%> and %<__auto_type%> in "
11320                            "declaration specifiers"));
11321               else if (specs->unsigned_p)
11322                 error_at (loc,
11323                           ("both %<unsigned%> and %<__auto_type%> in "
11324                            "declaration specifiers"));
11325               else if (specs->complex_p)
11326                 error_at (loc,
11327                           ("both %<complex%> and %<__auto_type%> in "
11328                            "declaration specifiers"));
11329               else if (specs->saturating_p)
11330                 error_at (loc,
11331                           ("both %<_Sat%> and %<__auto_type%> in "
11332                            "declaration specifiers"));
11333               else
11334                 {
11335                   specs->typespec_word = cts_auto_type;
11336                   specs->locations[cdw_typespec] = loc;
11337                 }
11338               return specs;
11339             case RID_INT_N_0:
11340             case RID_INT_N_1:
11341             case RID_INT_N_2:
11342             case RID_INT_N_3:
11343               specs->int_n_idx = i - RID_INT_N_0;
11344               if (!in_system_header_at (input_location)
11345                   /* If the INT_N type ends in "__", and so is of the format
11346                      "__intN__", don't pedwarn.  */
11347                   && (strncmp (IDENTIFIER_POINTER (type)
11348                                + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11349                 pedwarn (loc, OPT_Wpedantic,
11350                          "ISO C does not support %<__int%d%> types",
11351                          int_n_data[specs->int_n_idx].bitsize);
11352
11353               if (specs->long_p)
11354                 error_at (loc,
11355                           ("both %<__int%d%> and %<long%> in "
11356                            "declaration specifiers"),
11357                           int_n_data[specs->int_n_idx].bitsize);
11358               else if (specs->saturating_p)
11359                 error_at (loc,
11360                           ("both %<_Sat%> and %<__int%d%> in "
11361                            "declaration specifiers"),
11362                           int_n_data[specs->int_n_idx].bitsize);
11363               else if (specs->short_p)
11364                 error_at (loc,
11365                           ("both %<__int%d%> and %<short%> in "
11366                            "declaration specifiers"),
11367                           int_n_data[specs->int_n_idx].bitsize);
11368               else if (! int_n_enabled_p[specs->int_n_idx])
11369                 {
11370                   specs->typespec_word = cts_int_n;
11371                   error_at (loc,
11372                             "%<__int%d%> is not supported on this target",
11373                             int_n_data[specs->int_n_idx].bitsize);
11374                 }
11375               else
11376                 {
11377                   specs->typespec_word = cts_int_n;
11378                   specs->locations[cdw_typespec] = loc;
11379                 }
11380               return specs;
11381             case RID_VOID:
11382               if (specs->long_p)
11383                 error_at (loc,
11384                           ("both %<long%> and %<void%> in "
11385                            "declaration specifiers"));
11386               else if (specs->short_p)
11387                 error_at (loc,
11388                           ("both %<short%> and %<void%> in "
11389                            "declaration specifiers"));
11390               else if (specs->signed_p)
11391                 error_at (loc,
11392                           ("both %<signed%> and %<void%> in "
11393                            "declaration specifiers"));
11394               else if (specs->unsigned_p)
11395                 error_at (loc,
11396                           ("both %<unsigned%> and %<void%> in "
11397                            "declaration specifiers"));
11398               else if (specs->complex_p)
11399                 error_at (loc,
11400                           ("both %<complex%> and %<void%> in "
11401                            "declaration specifiers"));
11402               else if (specs->saturating_p)
11403                 error_at (loc,
11404                           ("both %<_Sat%> and %<void%> in "
11405                            "declaration specifiers"));
11406               else
11407                 {
11408                   specs->typespec_word = cts_void;
11409                   specs->locations[cdw_typespec] = loc;
11410                 }
11411               return specs;
11412             case RID_BOOL:
11413               if (!in_system_header_at (loc))
11414                 pedwarn_c90 (loc, OPT_Wpedantic,
11415                              "ISO C90 does not support boolean types");
11416               if (specs->long_p)
11417                 error_at (loc,
11418                           ("both %<long%> and %<_Bool%> in "
11419                            "declaration specifiers"));
11420               else if (specs->short_p)
11421                 error_at (loc,
11422                           ("both %<short%> and %<_Bool%> in "
11423                            "declaration specifiers"));
11424               else if (specs->signed_p)
11425                 error_at (loc,
11426                           ("both %<signed%> and %<_Bool%> in "
11427                            "declaration specifiers"));
11428               else if (specs->unsigned_p)
11429                 error_at (loc,
11430                           ("both %<unsigned%> and %<_Bool%> in "
11431                            "declaration specifiers"));
11432               else if (specs->complex_p)
11433                 error_at (loc,
11434                           ("both %<complex%> and %<_Bool%> in "
11435                            "declaration specifiers"));
11436               else if (specs->saturating_p)
11437                 error_at (loc,
11438                           ("both %<_Sat%> and %<_Bool%> in "
11439                            "declaration specifiers"));
11440               else
11441                 {
11442                   specs->typespec_word = cts_bool;
11443                   specs->locations[cdw_typespec] = loc;
11444                 }
11445               return specs;
11446             case RID_CHAR:
11447               if (specs->long_p)
11448                 error_at (loc,
11449                           ("both %<long%> and %<char%> in "
11450                            "declaration specifiers"));
11451               else if (specs->short_p)
11452                 error_at (loc,
11453                           ("both %<short%> and %<char%> in "
11454                            "declaration specifiers"));
11455               else if (specs->saturating_p)
11456                 error_at (loc,
11457                           ("both %<_Sat%> and %<char%> in "
11458                            "declaration specifiers"));
11459               else
11460                 {
11461                   specs->typespec_word = cts_char;
11462                   specs->locations[cdw_typespec] = loc;
11463                 }
11464               return specs;
11465             case RID_INT:
11466               if (specs->saturating_p)
11467                 error_at (loc,
11468                           ("both %<_Sat%> and %<int%> in "
11469                            "declaration specifiers"));
11470               else
11471                 {
11472                   specs->typespec_word = cts_int;
11473                   specs->locations[cdw_typespec] = loc;
11474                 }
11475               return specs;
11476             case RID_FLOAT:
11477               if (specs->long_p)
11478                 error_at (loc,
11479                           ("both %<long%> and %<float%> in "
11480                            "declaration specifiers"));
11481               else if (specs->short_p)
11482                 error_at (loc,
11483                           ("both %<short%> and %<float%> in "
11484                            "declaration specifiers"));
11485               else if (specs->signed_p)
11486                 error_at (loc,
11487                           ("both %<signed%> and %<float%> in "
11488                            "declaration specifiers"));
11489               else if (specs->unsigned_p)
11490                 error_at (loc,
11491                           ("both %<unsigned%> and %<float%> in "
11492                            "declaration specifiers"));
11493               else if (specs->saturating_p)
11494                 error_at (loc,
11495                           ("both %<_Sat%> and %<float%> in "
11496                            "declaration specifiers"));
11497               else
11498                 {
11499                   specs->typespec_word = cts_float;
11500                   specs->locations[cdw_typespec] = loc;
11501                 }
11502               return specs;
11503             case RID_DOUBLE:
11504               if (specs->long_long_p)
11505                 error_at (loc,
11506                           ("both %<long long%> and %<double%> in "
11507                            "declaration specifiers"));
11508               else if (specs->short_p)
11509                 error_at (loc,
11510                           ("both %<short%> and %<double%> in "
11511                            "declaration specifiers"));
11512               else if (specs->signed_p)
11513                 error_at (loc,
11514                           ("both %<signed%> and %<double%> in "
11515                            "declaration specifiers"));
11516               else if (specs->unsigned_p)
11517                 error_at (loc,
11518                           ("both %<unsigned%> and %<double%> in "
11519                            "declaration specifiers"));
11520               else if (specs->saturating_p)
11521                 error_at (loc,
11522                           ("both %<_Sat%> and %<double%> in "
11523                            "declaration specifiers"));
11524               else
11525                 {
11526                   specs->typespec_word = cts_double;
11527                   specs->locations[cdw_typespec] = loc;
11528                 }
11529               return specs;
11530             CASE_RID_FLOATN_NX:
11531               specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11532               if (!in_system_header_at (input_location))
11533                 pedwarn (loc, OPT_Wpedantic,
11534                          "ISO C does not support the %<_Float%d%s%> type",
11535                          floatn_nx_types[specs->floatn_nx_idx].n,
11536                          (floatn_nx_types[specs->floatn_nx_idx].extended
11537                           ? "x"
11538                           : ""));
11539
11540               if (specs->long_p)
11541                 error_at (loc,
11542                           ("both %<long%> and %<_Float%d%s%> in "
11543                            "declaration specifiers"),
11544                           floatn_nx_types[specs->floatn_nx_idx].n,
11545                           (floatn_nx_types[specs->floatn_nx_idx].extended
11546                            ? "x"
11547                            : ""));
11548               else if (specs->short_p)
11549                 error_at (loc,
11550                           ("both %<short%> and %<_Float%d%s%> in "
11551                            "declaration specifiers"),
11552                           floatn_nx_types[specs->floatn_nx_idx].n,
11553                           (floatn_nx_types[specs->floatn_nx_idx].extended
11554                            ? "x"
11555                            : ""));
11556               else if (specs->signed_p)
11557                 error_at (loc,
11558                           ("both %<signed%> and %<_Float%d%s%> in "
11559                            "declaration specifiers"),
11560                           floatn_nx_types[specs->floatn_nx_idx].n,
11561                           (floatn_nx_types[specs->floatn_nx_idx].extended
11562                            ? "x"
11563                            : ""));
11564               else if (specs->unsigned_p)
11565                 error_at (loc,
11566                           ("both %<unsigned%> and %<_Float%d%s%> in "
11567                            "declaration specifiers"),
11568                           floatn_nx_types[specs->floatn_nx_idx].n,
11569                           (floatn_nx_types[specs->floatn_nx_idx].extended
11570                            ? "x"
11571                            : ""));
11572               else if (specs->saturating_p)
11573                 error_at (loc,
11574                           ("both %<_Sat%> and %<_Float%d%s%> in "
11575                            "declaration specifiers"),
11576                           floatn_nx_types[specs->floatn_nx_idx].n,
11577                           (floatn_nx_types[specs->floatn_nx_idx].extended
11578                            ? "x"
11579                            : ""));
11580               else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11581                 {
11582                   specs->typespec_word = cts_floatn_nx;
11583                   error_at (loc,
11584                             "%<_Float%d%s%> is not supported on this target",
11585                             floatn_nx_types[specs->floatn_nx_idx].n,
11586                             (floatn_nx_types[specs->floatn_nx_idx].extended
11587                              ? "x"
11588                              : ""));
11589                 }
11590               else
11591                 {
11592                   specs->typespec_word = cts_floatn_nx;
11593                   specs->locations[cdw_typespec] = loc;
11594                 }
11595               return specs;
11596             case RID_DFLOAT32:
11597             case RID_DFLOAT64:
11598             case RID_DFLOAT128:
11599               {
11600                 const char *str;
11601                 if (i == RID_DFLOAT32)
11602                   str = "_Decimal32";
11603                 else if (i == RID_DFLOAT64)
11604                   str = "_Decimal64";
11605                 else
11606                   str = "_Decimal128";
11607                 if (specs->long_long_p)
11608                   error_at (loc,
11609                             ("both %<long long%> and %qs in "
11610                              "declaration specifiers"),
11611                             str);
11612                 if (specs->long_p)
11613                   error_at (loc,
11614                             ("both %<long%> and %qs in "
11615                              "declaration specifiers"),
11616                             str);
11617                 else if (specs->short_p)
11618                   error_at (loc,
11619                             ("both %<short%> and %qs in "
11620                              "declaration specifiers"),
11621                             str);
11622                 else if (specs->signed_p)
11623                   error_at (loc,
11624                             ("both %<signed%> and %qs in "
11625                              "declaration specifiers"),
11626                             str);
11627                 else if (specs->unsigned_p)
11628                   error_at (loc,
11629                             ("both %<unsigned%> and %qs in "
11630                              "declaration specifiers"),
11631                             str);
11632                 else if (specs->complex_p)
11633                   error_at (loc,
11634                             ("both %<complex%> and %qs in "
11635                              "declaration specifiers"),
11636                             str);
11637                 else if (specs->saturating_p)
11638                   error_at (loc,
11639                             ("both %<_Sat%> and %qs in "
11640                              "declaration specifiers"),
11641                             str);
11642                 else if (i == RID_DFLOAT32)
11643                   specs->typespec_word = cts_dfloat32;
11644                 else if (i == RID_DFLOAT64)
11645                   specs->typespec_word = cts_dfloat64;
11646                 else
11647                   specs->typespec_word = cts_dfloat128;
11648                 specs->locations[cdw_typespec] = loc;
11649               }
11650               if (!targetm.decimal_float_supported_p ())
11651                 error_at (loc,
11652                           ("decimal floating-point not supported "
11653                            "for this target"));
11654               pedwarn_c11 (loc, OPT_Wpedantic,
11655                            "ISO C does not support decimal floating-point "
11656                            "before C2X");
11657               return specs;
11658             case RID_FRACT:
11659             case RID_ACCUM:
11660               {
11661                 const char *str;
11662                 if (i == RID_FRACT)
11663                   str = "_Fract";
11664                 else
11665                   str = "_Accum";
11666                 if (specs->complex_p)
11667                   error_at (loc,
11668                             ("both %<complex%> and %qs in "
11669                              "declaration specifiers"),
11670                             str);
11671                 else if (i == RID_FRACT)
11672                     specs->typespec_word = cts_fract;
11673                 else
11674                     specs->typespec_word = cts_accum;
11675                 specs->locations[cdw_typespec] = loc;
11676               }
11677               if (!targetm.fixed_point_supported_p ())
11678                 error_at (loc,
11679                           "fixed-point types not supported for this target");
11680               pedwarn (loc, OPT_Wpedantic,
11681                        "ISO C does not support fixed-point types");
11682               return specs;
11683             default:
11684               /* ObjC reserved word "id", handled below.  */
11685               break;
11686             }
11687         }
11688     }
11689
11690   /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11691      form of ObjC type, cases such as "int" and "long" being handled
11692      above), a TYPE (struct, union, enum and typeof specifiers) or an
11693      ERROR_MARK.  In none of these cases may there have previously
11694      been any type specifiers.  */
11695   if (specs->type || specs->typespec_word != cts_none
11696       || specs->long_p || specs->short_p || specs->signed_p
11697       || specs->unsigned_p || specs->complex_p)
11698     error_at (loc, "two or more data types in declaration specifiers");
11699   else if (TREE_CODE (type) == TYPE_DECL)
11700     {
11701       if (TREE_TYPE (type) == error_mark_node)
11702         ; /* Allow the type to default to int to avoid cascading errors.  */
11703       else
11704         {
11705           specs->type = TREE_TYPE (type);
11706           specs->decl_attr = DECL_ATTRIBUTES (type);
11707           specs->typedef_p = true;
11708           specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11709           specs->locations[cdw_typedef] = loc;
11710
11711           /* If this typedef name is defined in a struct, then a C++
11712              lookup would return a different value.  */
11713           if (warn_cxx_compat
11714               && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11715             warning_at (loc, OPT_Wc___compat,
11716                         "C++ lookup of %qD would return a field, not a type",
11717                         type);
11718
11719           /* If we are parsing a struct, record that a struct field
11720              used a typedef.  */
11721           if (warn_cxx_compat && struct_parse_info != NULL)
11722             struct_parse_info->typedefs_seen.safe_push (type);
11723         }
11724     }
11725   else if (TREE_CODE (type) == IDENTIFIER_NODE)
11726     {
11727       tree t = lookup_name (type);
11728       if (!t || TREE_CODE (t) != TYPE_DECL)
11729         error_at (loc, "%qE fails to be a typedef or built in type", type);
11730       else if (TREE_TYPE (t) == error_mark_node)
11731         ;
11732       else
11733         {
11734           specs->type = TREE_TYPE (t);
11735           specs->locations[cdw_typespec] = loc;
11736         }
11737     }
11738   else
11739     {
11740       if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11741         {
11742           specs->typedef_p = true;
11743           specs->locations[cdw_typedef] = loc;
11744           if (spec.expr)
11745             {
11746               if (specs->expr)
11747                 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11748                                       specs->expr, spec.expr);
11749               else
11750                 specs->expr = spec.expr;
11751               specs->expr_const_operands &= spec.expr_const_operands;
11752             }
11753         }
11754       specs->type = type;
11755     }
11756
11757   return specs;
11758 }
11759
11760 /* Add the storage class specifier or function specifier SCSPEC to the
11761    declaration specifiers SPECS, returning SPECS.  */
11762
11763 struct c_declspecs *
11764 declspecs_add_scspec (location_t loc,
11765                       struct c_declspecs *specs,
11766                       tree scspec)
11767 {
11768   enum rid i;
11769   enum c_storage_class n = csc_none;
11770   bool dupe = false;
11771   specs->declspecs_seen_p = true;
11772   specs->non_std_attrs_seen_p = true;
11773   gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11774               && C_IS_RESERVED_WORD (scspec));
11775   i = C_RID_CODE (scspec);
11776   if (specs->non_sc_seen_p)
11777     warning (OPT_Wold_style_declaration,
11778              "%qE is not at beginning of declaration", scspec);
11779   switch (i)
11780     {
11781     case RID_INLINE:
11782       /* C99 permits duplicate inline.  Although of doubtful utility,
11783          it seems simplest to permit it in gnu89 mode as well, as
11784          there is also little utility in maintaining this as a
11785          difference between gnu89 and C99 inline.  */
11786       dupe = false;
11787       specs->inline_p = true;
11788       specs->locations[cdw_inline] = loc;
11789       break;
11790     case RID_NORETURN:
11791       /* Duplicate _Noreturn is permitted.  */
11792       dupe = false;
11793       specs->noreturn_p = true;
11794       specs->locations[cdw_noreturn] = loc;
11795       break;
11796     case RID_THREAD:
11797       dupe = specs->thread_p;
11798       if (specs->storage_class == csc_auto)
11799         error ("%qE used with %<auto%>", scspec);
11800       else if (specs->storage_class == csc_register)
11801         error ("%qE used with %<register%>", scspec);
11802       else if (specs->storage_class == csc_typedef)
11803         error ("%qE used with %<typedef%>", scspec);
11804       else
11805         {
11806           specs->thread_p = true;
11807           specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11808                                          "__thread") == 0);
11809           /* A diagnostic is not required for the use of this
11810              identifier in the implementation namespace; only diagnose
11811              it for the C11 spelling because of existing code using
11812              the other spelling.  */
11813           if (!specs->thread_gnu_p)
11814             {
11815               if (flag_isoc99)
11816                 pedwarn_c99 (loc, OPT_Wpedantic,
11817                              "ISO C99 does not support %qE", scspec);
11818               else
11819                 pedwarn_c99 (loc, OPT_Wpedantic,
11820                              "ISO C90 does not support %qE", scspec);
11821             }
11822           specs->locations[cdw_thread] = loc;
11823         }
11824       break;
11825     case RID_AUTO:
11826       n = csc_auto;
11827       break;
11828     case RID_EXTERN:
11829       n = csc_extern;
11830       /* Diagnose "__thread extern".  */
11831       if (specs->thread_p && specs->thread_gnu_p)
11832         error ("%<__thread%> before %<extern%>");
11833       break;
11834     case RID_REGISTER:
11835       n = csc_register;
11836       break;
11837     case RID_STATIC:
11838       n = csc_static;
11839       /* Diagnose "__thread static".  */
11840       if (specs->thread_p && specs->thread_gnu_p)
11841         error ("%<__thread%> before %<static%>");
11842       break;
11843     case RID_TYPEDEF:
11844       n = csc_typedef;
11845       break;
11846     default:
11847       gcc_unreachable ();
11848     }
11849   if (n != csc_none && n == specs->storage_class)
11850     dupe = true;
11851   if (dupe)
11852     {
11853       if (i == RID_THREAD)
11854         error ("duplicate %<_Thread_local%> or %<__thread%>");
11855       else
11856         error ("duplicate %qE", scspec);
11857     }
11858   if (n != csc_none)
11859     {
11860       if (specs->storage_class != csc_none && n != specs->storage_class)
11861         {
11862           error ("multiple storage classes in declaration specifiers");
11863         }
11864       else
11865         {
11866           specs->storage_class = n;
11867           specs->locations[cdw_storage_class] = loc;
11868           if (n != csc_extern && n != csc_static && specs->thread_p)
11869             {
11870               error ("%qs used with %qE",
11871                      specs->thread_gnu_p ? "__thread" : "_Thread_local",
11872                      scspec);
11873               specs->thread_p = false;
11874             }
11875         }
11876     }
11877   return specs;
11878 }
11879
11880 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11881    returning SPECS.  */
11882
11883 struct c_declspecs *
11884 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11885 {
11886   specs->attrs = chainon (attrs, specs->attrs);
11887   specs->locations[cdw_attributes] = loc;
11888   specs->declspecs_seen_p = true;
11889   /* In the case of standard attributes at the start of the
11890      declaration, the caller will reset this.  */
11891   specs->non_std_attrs_seen_p = true;
11892   return specs;
11893 }
11894
11895 /* Add an _Alignas specifier (expression ALIGN, or type whose
11896    alignment is ALIGN) to the declaration specifiers SPECS, returning
11897    SPECS.  */
11898 struct c_declspecs *
11899 declspecs_add_alignas (location_t loc,
11900                        struct c_declspecs *specs, tree align)
11901 {
11902   specs->alignas_p = true;
11903   specs->locations[cdw_alignas] = loc;
11904   if (align == error_mark_node)
11905     return specs;
11906
11907   /* Only accept the alignment if it's valid and greater than
11908      the current one.  Zero is invalid but by C11 required to
11909      be silently ignored.  */
11910   int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11911   if (align_log > specs->align_log)
11912     specs->align_log = align_log;
11913   return specs;
11914 }
11915
11916 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11917    specifiers with any other type specifier to determine the resulting
11918    type.  This is where ISO C checks on complex types are made, since
11919    "_Complex long" is a prefix of the valid ISO C type "_Complex long
11920    double".  Also apply postfix standard attributes to modify the type.  */
11921
11922 struct c_declspecs *
11923 finish_declspecs (struct c_declspecs *specs)
11924 {
11925   /* If a type was specified as a whole, we have no modifiers and are
11926      done.  */
11927   if (specs->type != NULL_TREE)
11928     {
11929       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11930                   && !specs->signed_p && !specs->unsigned_p
11931                   && !specs->complex_p);
11932
11933       /* Set a dummy type.  */
11934       if (TREE_CODE (specs->type) == ERROR_MARK)
11935         specs->type = integer_type_node;
11936       goto handle_postfix_attrs;
11937     }
11938
11939   /* If none of "void", "_Bool", "char", "int", "float" or "double"
11940      has been specified, treat it as "int" unless "_Complex" is
11941      present and there are no other specifiers.  If we just have
11942      "_Complex", it is equivalent to "_Complex double", but e.g.
11943      "_Complex short" is equivalent to "_Complex short int".  */
11944   if (specs->typespec_word == cts_none)
11945     {
11946       if (specs->saturating_p)
11947         {
11948           error_at (specs->locations[cdw_saturating],
11949                     "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11950           if (!targetm.fixed_point_supported_p ())
11951             error_at (specs->locations[cdw_saturating],
11952                       "fixed-point types not supported for this target");
11953           specs->typespec_word = cts_fract;
11954         }
11955       else if (specs->long_p || specs->short_p
11956                || specs->signed_p || specs->unsigned_p)
11957         {
11958           specs->typespec_word = cts_int;
11959         }
11960       else if (specs->complex_p)
11961         {
11962           specs->typespec_word = cts_double;
11963           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11964                    "ISO C does not support plain %<complex%> meaning "
11965                    "%<double complex%>");
11966         }
11967       else
11968         {
11969           specs->typespec_word = cts_int;
11970           specs->default_int_p = true;
11971           /* We don't diagnose this here because grokdeclarator will
11972              give more specific diagnostics according to whether it is
11973              a function definition.  */
11974         }
11975     }
11976
11977   /* If "signed" was specified, record this to distinguish "int" and
11978      "signed int" in the case of a bit-field with
11979      -funsigned-bitfields.  */
11980   specs->explicit_signed_p = specs->signed_p;
11981
11982   /* Now compute the actual type.  */
11983   switch (specs->typespec_word)
11984     {
11985     case cts_auto_type:
11986       gcc_assert (!specs->long_p && !specs->short_p
11987                   && !specs->signed_p && !specs->unsigned_p
11988                   && !specs->complex_p);
11989       /* Type to be filled in later.  */
11990       if (specs->postfix_attrs)
11991         error ("%<__auto_type%> followed by %<[[]]%> attributes");
11992       break;
11993     case cts_void:
11994       gcc_assert (!specs->long_p && !specs->short_p
11995                   && !specs->signed_p && !specs->unsigned_p
11996                   && !specs->complex_p);
11997       specs->type = void_type_node;
11998       break;
11999     case cts_bool:
12000       gcc_assert (!specs->long_p && !specs->short_p
12001                   && !specs->signed_p && !specs->unsigned_p
12002                   && !specs->complex_p);
12003       specs->type = boolean_type_node;
12004       break;
12005     case cts_char:
12006       gcc_assert (!specs->long_p && !specs->short_p);
12007       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12008       if (specs->signed_p)
12009         specs->type = signed_char_type_node;
12010       else if (specs->unsigned_p)
12011         specs->type = unsigned_char_type_node;
12012       else
12013         specs->type = char_type_node;
12014       if (specs->complex_p)
12015         {
12016           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12017                    "ISO C does not support complex integer types");
12018           specs->type = build_complex_type (specs->type);
12019         }
12020       break;
12021     case cts_int_n:
12022       gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12023       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12024       if (! int_n_enabled_p[specs->int_n_idx])
12025         specs->type = integer_type_node;
12026       else
12027         specs->type = (specs->unsigned_p
12028                        ? int_n_trees[specs->int_n_idx].unsigned_type
12029                        : int_n_trees[specs->int_n_idx].signed_type);
12030       if (specs->complex_p)
12031         {
12032           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12033                    "ISO C does not support complex integer types");
12034           specs->type = build_complex_type (specs->type);
12035         }
12036       break;
12037     case cts_int:
12038       gcc_assert (!(specs->long_p && specs->short_p));
12039       gcc_assert (!(specs->signed_p && specs->unsigned_p));
12040       if (specs->long_long_p)
12041         specs->type = (specs->unsigned_p
12042                        ? long_long_unsigned_type_node
12043                        : long_long_integer_type_node);
12044       else if (specs->long_p)
12045         specs->type = (specs->unsigned_p
12046                        ? long_unsigned_type_node
12047                        : long_integer_type_node);
12048       else if (specs->short_p)
12049         specs->type = (specs->unsigned_p
12050                        ? short_unsigned_type_node
12051                        : short_integer_type_node);
12052       else
12053         specs->type = (specs->unsigned_p
12054                        ? unsigned_type_node
12055                        : integer_type_node);
12056       if (specs->complex_p)
12057         {
12058           pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12059                    "ISO C does not support complex integer types");
12060           specs->type = build_complex_type (specs->type);
12061         }
12062       break;
12063     case cts_float:
12064       gcc_assert (!specs->long_p && !specs->short_p
12065                   && !specs->signed_p && !specs->unsigned_p);
12066       specs->type = (specs->complex_p
12067                      ? complex_float_type_node
12068                      : float_type_node);
12069       break;
12070     case cts_double:
12071       gcc_assert (!specs->long_long_p && !specs->short_p
12072                   && !specs->signed_p && !specs->unsigned_p);
12073       if (specs->long_p)
12074         {
12075           specs->type = (specs->complex_p
12076                          ? complex_long_double_type_node
12077                          : long_double_type_node);
12078         }
12079       else
12080         {
12081           specs->type = (specs->complex_p
12082                          ? complex_double_type_node
12083                          : double_type_node);
12084         }
12085       break;
12086     case cts_floatn_nx:
12087       gcc_assert (!specs->long_p && !specs->short_p
12088                   && !specs->signed_p && !specs->unsigned_p);
12089       if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12090         specs->type = integer_type_node;
12091       else if (specs->complex_p)
12092         specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12093       else
12094         specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12095       break;
12096     case cts_dfloat32:
12097     case cts_dfloat64:
12098     case cts_dfloat128:
12099       gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12100                   && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12101       if (!targetm.decimal_float_supported_p ())
12102         specs->type = integer_type_node;
12103       else if (specs->typespec_word == cts_dfloat32)
12104         specs->type = dfloat32_type_node;
12105       else if (specs->typespec_word == cts_dfloat64)
12106         specs->type = dfloat64_type_node;
12107       else
12108         specs->type = dfloat128_type_node;
12109       break;
12110     case cts_fract:
12111       gcc_assert (!specs->complex_p);
12112       if (!targetm.fixed_point_supported_p ())
12113         specs->type = integer_type_node;
12114       else if (specs->saturating_p)
12115         {
12116           if (specs->long_long_p)
12117             specs->type = specs->unsigned_p
12118                           ? sat_unsigned_long_long_fract_type_node
12119                           : sat_long_long_fract_type_node;
12120           else if (specs->long_p)
12121             specs->type = specs->unsigned_p
12122                           ? sat_unsigned_long_fract_type_node
12123                           : sat_long_fract_type_node;
12124           else if (specs->short_p)
12125             specs->type = specs->unsigned_p
12126                           ? sat_unsigned_short_fract_type_node
12127                           : sat_short_fract_type_node;
12128           else
12129             specs->type = specs->unsigned_p
12130                           ? sat_unsigned_fract_type_node
12131                           : sat_fract_type_node;
12132         }
12133       else
12134         {
12135           if (specs->long_long_p)
12136             specs->type = specs->unsigned_p
12137                           ? unsigned_long_long_fract_type_node
12138                           : long_long_fract_type_node;
12139           else if (specs->long_p)
12140             specs->type = specs->unsigned_p
12141                           ? unsigned_long_fract_type_node
12142                           : long_fract_type_node;
12143           else if (specs->short_p)
12144             specs->type = specs->unsigned_p
12145                           ? unsigned_short_fract_type_node
12146                           : short_fract_type_node;
12147           else
12148             specs->type = specs->unsigned_p
12149                           ? unsigned_fract_type_node
12150                           : fract_type_node;
12151         }
12152       break;
12153     case cts_accum:
12154       gcc_assert (!specs->complex_p);
12155       if (!targetm.fixed_point_supported_p ())
12156         specs->type = integer_type_node;
12157       else if (specs->saturating_p)
12158         {
12159           if (specs->long_long_p)
12160             specs->type = specs->unsigned_p
12161                           ? sat_unsigned_long_long_accum_type_node
12162                           : sat_long_long_accum_type_node;
12163           else if (specs->long_p)
12164             specs->type = specs->unsigned_p
12165                           ? sat_unsigned_long_accum_type_node
12166                           : sat_long_accum_type_node;
12167           else if (specs->short_p)
12168             specs->type = specs->unsigned_p
12169                           ? sat_unsigned_short_accum_type_node
12170                           : sat_short_accum_type_node;
12171           else
12172             specs->type = specs->unsigned_p
12173                           ? sat_unsigned_accum_type_node
12174                           : sat_accum_type_node;
12175         }
12176       else
12177         {
12178           if (specs->long_long_p)
12179             specs->type = specs->unsigned_p
12180                           ? unsigned_long_long_accum_type_node
12181                           : long_long_accum_type_node;
12182           else if (specs->long_p)
12183             specs->type = specs->unsigned_p
12184                           ? unsigned_long_accum_type_node
12185                           : long_accum_type_node;
12186           else if (specs->short_p)
12187             specs->type = specs->unsigned_p
12188                           ? unsigned_short_accum_type_node
12189                           : short_accum_type_node;
12190           else
12191             specs->type = specs->unsigned_p
12192                           ? unsigned_accum_type_node
12193                           : accum_type_node;
12194         }
12195       break;
12196     default:
12197       gcc_unreachable ();
12198     }
12199  handle_postfix_attrs:
12200   if (specs->type != NULL)
12201     {
12202       specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12203       decl_attributes (&specs->type, specs->postfix_attrs, 0);
12204       specs->postfix_attrs = NULL_TREE;
12205     }
12206
12207   return specs;
12208 }
12209
12210 /* Perform final processing on one file scope's declarations (or the
12211    external scope's declarations), GLOBALS.  */
12212
12213 static void
12214 c_write_global_declarations_1 (tree globals)
12215 {
12216   tree decl;
12217   bool reconsider;
12218
12219   /* Process the decls in the order they were written.  */
12220   for (decl = globals; decl; decl = DECL_CHAIN (decl))
12221     {
12222       /* Check for used but undefined static functions using the C
12223          standard's definition of "used", and set TREE_NO_WARNING so
12224          that check_global_declaration doesn't repeat the check.  */
12225       if (TREE_CODE (decl) == FUNCTION_DECL
12226           && DECL_INITIAL (decl) == NULL_TREE
12227           && DECL_EXTERNAL (decl)
12228           && !TREE_PUBLIC (decl))
12229         {
12230           if (C_DECL_USED (decl))
12231             {
12232               /* TODO: Add OPT_Wundefined-inline.  */
12233               if (pedwarn (input_location, 0, "%q+F used but never defined",
12234                            decl))
12235                 suppress_warning (decl /* OPT_Wundefined-inline.  */);
12236             }
12237           /* For -Wunused-function warn about unused static prototypes.  */
12238           else if (warn_unused_function
12239                    && ! DECL_ARTIFICIAL (decl)
12240                    && ! warning_suppressed_p (decl, OPT_Wunused_function))
12241             {
12242               if (warning (OPT_Wunused_function,
12243                            "%q+F declared %<static%> but never defined",
12244                            decl))
12245                 suppress_warning (decl, OPT_Wunused_function);
12246             }
12247         }
12248
12249       wrapup_global_declaration_1 (decl);
12250     }
12251
12252   do
12253     {
12254       reconsider = false;
12255       for (decl = globals; decl; decl = DECL_CHAIN (decl))
12256         reconsider |= wrapup_global_declaration_2 (decl);
12257     }
12258   while (reconsider);
12259 }
12260
12261 /* Preserve the external declarations scope across a garbage collect.  */
12262 static GTY(()) tree ext_block;
12263
12264 /* Collect all references relevant to SOURCE_FILE.  */
12265
12266 static void
12267 collect_all_refs (const char *source_file)
12268 {
12269   tree t;
12270   unsigned i;
12271
12272   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12273     collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12274
12275   collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12276 }
12277
12278 /* Collect source file references at global level.  */
12279
12280 static void
12281 collect_source_refs (void)
12282 {
12283   tree t;
12284   tree decls;
12285   tree decl;
12286   unsigned i;
12287
12288   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12289     { 
12290       decls = DECL_INITIAL (t);
12291       for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12292         if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12293           collect_source_ref (DECL_SOURCE_FILE (decl));
12294     }
12295
12296   for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12297     if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12298       collect_source_ref (DECL_SOURCE_FILE (decl));
12299 }
12300
12301 /* Free attribute access data that are not needed by the middle end. */
12302
12303 static void
12304 free_attr_access_data ()
12305 {
12306   struct cgraph_node *n;
12307
12308   /* Iterate over all functions declared in the translation unit.  */
12309   FOR_EACH_FUNCTION (n)
12310     {
12311       for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12312         if (tree attrs = DECL_ATTRIBUTES (parm))
12313           attr_access::free_lang_data (attrs);
12314
12315       tree fntype = TREE_TYPE (n->decl);
12316       if (!fntype || fntype == error_mark_node)
12317         continue;
12318       tree attrs = TYPE_ATTRIBUTES (fntype);
12319       if (!attrs)
12320         continue;
12321
12322       attr_access::free_lang_data (attrs);
12323     }
12324 }
12325
12326 /* Perform any final parser cleanups and generate initial debugging
12327    information.  */
12328
12329 void
12330 c_parse_final_cleanups (void)
12331 {
12332   tree t;
12333   unsigned i;
12334
12335   /* We don't want to do this if generating a PCH.  */
12336   if (pch_file)
12337     return;
12338
12339   timevar_stop (TV_PHASE_PARSING);
12340   timevar_start (TV_PHASE_DEFERRED);
12341
12342   /* Do the Objective-C stuff.  This is where all the Objective-C
12343      module stuff gets generated (symtab, class/protocol/selector
12344      lists etc).  */
12345   if (c_dialect_objc ())
12346     objc_write_global_declarations ();
12347
12348   /* Close the external scope.  */
12349   ext_block = pop_scope ();
12350   external_scope = 0;
12351   gcc_assert (!current_scope);
12352
12353   /* Handle -fdump-ada-spec[-slim]. */
12354   if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12355     {
12356       /* Build a table of files to generate specs for */
12357       collect_source_ref (main_input_filename);
12358       if (!flag_dump_ada_spec_slim)
12359         collect_source_refs ();
12360
12361       dump_ada_specs (collect_all_refs, NULL);
12362     }
12363
12364   /* Process all file scopes in this compilation, and the external_scope,
12365      through wrapup_global_declarations.  */
12366   FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12367     c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12368   c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12369
12370   if (!in_lto_p)
12371     free_attr_access_data ();
12372
12373   timevar_stop (TV_PHASE_DEFERRED);
12374   timevar_start (TV_PHASE_PARSING);
12375
12376   ext_block = NULL;
12377 }
12378
12379 /* Register reserved keyword WORD as qualifier for address space AS.  */
12380
12381 void
12382 c_register_addr_space (const char *word, addr_space_t as)
12383 {
12384   int rid = RID_FIRST_ADDR_SPACE + as;
12385   tree id;
12386
12387   /* Address space qualifiers are only supported
12388      in C with GNU extensions enabled.  */
12389   if (c_dialect_objc () || flag_no_asm)
12390     return;
12391
12392   id = get_identifier (word);
12393   C_SET_RID_CODE (id, rid);
12394   C_IS_RESERVED_WORD (id) = 1;
12395   ridpointers [rid] = id;
12396 }
12397
12398 /* Return identifier to look up for omp declare reduction.  */
12399
12400 tree
12401 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
12402 {
12403   const char *p = NULL;
12404   switch (reduction_code)
12405     {
12406     case PLUS_EXPR: p = "+"; break;
12407     case MULT_EXPR: p = "*"; break;
12408     case MINUS_EXPR: p = "-"; break;
12409     case BIT_AND_EXPR: p = "&"; break;
12410     case BIT_XOR_EXPR: p = "^"; break;
12411     case BIT_IOR_EXPR: p = "|"; break;
12412     case TRUTH_ANDIF_EXPR: p = "&&"; break;
12413     case TRUTH_ORIF_EXPR: p = "||"; break;
12414     case MIN_EXPR: p = "min"; break;
12415     case MAX_EXPR: p = "max"; break;
12416     default:
12417       break;
12418     }
12419
12420   if (p == NULL)
12421     {
12422       if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12423         return error_mark_node;
12424       p = IDENTIFIER_POINTER (reduction_id);
12425     }
12426
12427   const char prefix[] = "omp declare reduction ";
12428   size_t lenp = sizeof (prefix);
12429   size_t len = strlen (p);
12430   char *name = XALLOCAVEC (char, lenp + len);
12431   memcpy (name, prefix, lenp - 1);
12432   memcpy (name + lenp - 1, p, len + 1);
12433   return get_identifier (name);
12434 }
12435
12436 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12437    VAR_DECL, bind it into the current scope and return it.  */
12438
12439 tree
12440 c_omp_reduction_decl (tree reduction_id)
12441 {
12442   struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12443   if (b != NULL && B_IN_CURRENT_SCOPE (b))
12444     return b->decl;
12445
12446   tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12447                           reduction_id, integer_type_node);
12448   DECL_ARTIFICIAL (decl) = 1;
12449   DECL_EXTERNAL (decl) = 1;
12450   TREE_STATIC (decl) = 1;
12451   TREE_PUBLIC (decl) = 0;
12452   bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12453   return decl;
12454 }
12455
12456 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE.  */
12457
12458 tree
12459 c_omp_reduction_lookup (tree reduction_id, tree type)
12460 {
12461   struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12462   while (b)
12463     {
12464       tree t;
12465       for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12466         if (comptypes (TREE_PURPOSE (t), type))
12467           return TREE_VALUE (t);
12468       b = b->shadowed;
12469     }
12470   return error_mark_node;
12471 }
12472
12473 /* Helper function called via walk_tree, to diagnose invalid
12474    #pragma omp declare reduction combiners or initializers.  */
12475
12476 tree
12477 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12478 {
12479   tree *vars = (tree *) data;
12480   if (SSA_VAR_P (*tp)
12481       && !DECL_ARTIFICIAL (*tp)
12482       && *tp != vars[0]
12483       && *tp != vars[1])
12484     {
12485       location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12486       if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12487         error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12488                        "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12489                   *tp);
12490       else
12491         error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12492                        "to variable %qD which is not %<omp_priv%> nor "
12493                        "%<omp_orig%>",
12494                   *tp);
12495       return *tp;
12496     }
12497   return NULL_TREE;
12498 }
12499
12500
12501 bool
12502 c_check_in_current_scope (tree decl)
12503 {
12504   struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12505   return b != NULL && B_IN_CURRENT_SCOPE (b);
12506 }
12507
12508 #include "gt-c-c-decl.h"