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