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