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