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