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