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