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