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