]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-decl.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-decl.cc
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2024 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 #define INCLUDE_STRING
29 #define INCLUDE_MEMORY
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "function.h"
34 #include "c-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "intl.h"
39 #include "print-tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "attribs.h"
43 #include "toplev.h"
44 #include "debug.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "c-family/c-ubsan.h"
48 #include "c-lang.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
51 #include "dumpfile.h"
52 #include "plugin.h"
53 #include "c-family/c-ada-spec.h"
54 #include "builtins.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
57 #include "asan.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.h"
60 #include "c-family/c-spellcheck.h"
61 #include "context.h" /* For 'g'. */
62 #include "omp-general.h"
63 #include "omp-offload.h" /* For offload_vars. */
64 #include "c-parser.h"
65
66 #include "tree-pretty-print.h"
67
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 enum decl_context
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
75
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)) or __attribute__((unavailable)).
78 An object declared as __attribute__((unavailable)) should suppress
79 any reports of being declared with unavailable or deprecated items.
80 An object declared as __attribute__((deprecated)) should suppress
81 warnings of uses of other deprecated items. */
82
83 enum deprecated_states {
84 DEPRECATED_NORMAL,
85 DEPRECATED_SUPPRESS,
86 UNAVAILABLE_DEPRECATED_SUPPRESS
87 };
88
89 \f
90 /* Nonzero if we have seen an invalid cross reference
91 to a struct, union, or enum, but not yet printed the message. */
92 tree pending_invalid_xref;
93
94 /* File and line to appear in the eventual error message. */
95 location_t pending_invalid_xref_location;
96
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
100
101 static location_t current_function_prototype_locus;
102
103 /* Whether this prototype was built-in. */
104
105 static bool current_function_prototype_built_in;
106
107 /* The argument type information of this prototype. */
108
109 static tree current_function_prototype_arg_types;
110
111 /* The argument information structure for the function currently being
112 defined. */
113
114 static struct c_arg_info *current_function_arg_info;
115
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
118 allocated. */
119 struct obstack parser_obstack;
120
121 /* The current statement tree. */
122
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
124
125 /* Zero if we are not in an iteration or switch statement, otherwise
126 a bitmask. See bitmask definitions in c-tree.h. */
127 unsigned char in_statement;
128
129 /* A list of decls to be made automatically visible in each file scope. */
130 static GTY(()) tree visible_builtins;
131
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a return statement that specifies a return value is seen. */
134
135 int current_function_returns_value;
136
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement with no argument is seen. */
139
140 int current_function_returns_null;
141
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a call to a noreturn function is seen. */
144
145 int current_function_returns_abnormally;
146
147 /* Set to nonzero by `grokdeclarator' for a function
148 whose return type is defaulted, if warnings for this are desired. */
149
150 static int warn_about_return_type;
151
152 /* Nonzero when the current toplevel function contains a declaration
153 of a nested function which is never defined. */
154
155 static bool undef_nested_function;
156
157 /* Vector of implicit "omp declare target" attributes to be added into
158 the attribute lists. */
159 vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160
161 /* Vector of
162 #pragma omp begin assumes ... #pragma omp end assumes regions
163 we are in. */
164 vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
165 \f
166 /* Each c_binding structure describes one binding of an identifier to
167 a decl. All the decls in a scope - irrespective of namespace - are
168 chained together by the ->prev field, which (as the name implies)
169 runs in reverse order. All the decls in a given namespace bound to
170 a given identifier are chained by the ->shadowed field, which runs
171 from inner to outer scopes.
172
173 The ->decl field usually points to a DECL node, but there are two
174 exceptions. In the namespace of type tags, the bound entity is a
175 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
176 identifier is encountered, it is bound to error_mark_node to
177 suppress further errors about that identifier in the current
178 function.
179
180 The ->u.type field stores the type of the declaration in this scope;
181 if NULL, the type is the type of the ->decl field. This is only of
182 relevance for objects with external or internal linkage which may
183 be redeclared in inner scopes, forming composite types that only
184 persist for the duration of those scopes. In the external scope,
185 this stores the composite of all the types declared for this
186 object, visible or not. The ->inner_comp field (used only at file
187 scope) stores whether an incomplete array type at file scope was
188 completed at an inner scope to an array size other than 1.
189
190 The ->u.label field is used for labels. It points to a structure
191 which stores additional information used for warnings.
192
193 The depth field is copied from the scope structure that holds this
194 decl. It is used to preserve the proper ordering of the ->shadowed
195 field (see bind()) and also for a handful of special-case checks.
196 Finally, the invisible bit is true for a decl which should be
197 ignored for purposes of normal name lookup, and the nested bit is
198 true for a decl that's been bound a second time in an inner scope;
199 in all such cases, the binding in the outer scope will have its
200 invisible bit true. */
201
202 struct GTY((chain_next ("%h.prev"))) c_binding {
203 union GTY(()) { /* first so GTY desc can use decl */
204 tree GTY((tag ("0"))) type; /* the type in this scope */
205 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
206 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
207 tree decl; /* the decl bound */
208 tree id; /* the identifier it's bound to */
209 struct c_binding *prev; /* the previous decl in this scope */
210 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
211 unsigned int depth : 28; /* depth of this scope */
212 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
213 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
214 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
215 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
216 location_t locus; /* location for nested bindings */
217 };
218 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
219 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
220 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
221 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
222
223 /* Each C symbol points to three linked lists of c_binding structures.
224 These describe the values of the identifier in the three different
225 namespaces defined by the language. */
226
227 struct GTY(()) lang_identifier {
228 struct c_common_identifier common_id;
229 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
230 struct c_binding *tag_binding; /* struct/union/enum tags */
231 struct c_binding *label_binding; /* labels */
232 };
233
234 /* Validate c-lang.cc's assumptions. */
235 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
236 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
237
238 /* The binding oracle; see c-tree.h. */
239 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
240
241 /* This flag is set on an identifier if we have previously asked the
242 binding oracle for this identifier's symbol binding. */
243 #define I_SYMBOL_CHECKED(node) \
244 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
245
246 static inline struct c_binding* *
247 i_symbol_binding (tree node)
248 {
249 struct lang_identifier *lid
250 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
251
252 if (lid->symbol_binding == NULL
253 && c_binding_oracle != NULL
254 && !I_SYMBOL_CHECKED (node))
255 {
256 /* Set the "checked" flag first, to avoid infinite recursion
257 when the binding oracle calls back into gcc. */
258 I_SYMBOL_CHECKED (node) = 1;
259 c_binding_oracle (C_ORACLE_SYMBOL, node);
260 }
261
262 return &lid->symbol_binding;
263 }
264
265 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
266
267 #define I_SYMBOL_DECL(node) \
268 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
269
270 /* This flag is set on an identifier if we have previously asked the
271 binding oracle for this identifier's tag binding. */
272 #define I_TAG_CHECKED(node) \
273 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
274
275 static inline struct c_binding **
276 i_tag_binding (tree node)
277 {
278 struct lang_identifier *lid
279 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
280
281 if (lid->tag_binding == NULL
282 && c_binding_oracle != NULL
283 && !I_TAG_CHECKED (node))
284 {
285 /* Set the "checked" flag first, to avoid infinite recursion
286 when the binding oracle calls back into gcc. */
287 I_TAG_CHECKED (node) = 1;
288 c_binding_oracle (C_ORACLE_TAG, node);
289 }
290
291 return &lid->tag_binding;
292 }
293
294 #define I_TAG_BINDING(node) (*i_tag_binding (node))
295
296 #define I_TAG_DECL(node) \
297 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
298
299 /* This flag is set on an identifier if we have previously asked the
300 binding oracle for this identifier's label binding. */
301 #define I_LABEL_CHECKED(node) \
302 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
303
304 static inline struct c_binding **
305 i_label_binding (tree node)
306 {
307 struct lang_identifier *lid
308 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
309
310 if (lid->label_binding == NULL
311 && c_binding_oracle != NULL
312 && !I_LABEL_CHECKED (node))
313 {
314 /* Set the "checked" flag first, to avoid infinite recursion
315 when the binding oracle calls back into gcc. */
316 I_LABEL_CHECKED (node) = 1;
317 c_binding_oracle (C_ORACLE_LABEL, node);
318 }
319
320 return &lid->label_binding;
321 }
322
323 #define I_LABEL_BINDING(node) (*i_label_binding (node))
324
325 #define I_LABEL_DECL(node) \
326 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
327
328 /* Used by C_TOKEN_VEC tree. */
329 struct GTY (()) c_tree_token_vec {
330 struct tree_base base;
331 vec<c_token, va_gc> *tokens;
332 };
333
334 STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
335 STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
336 == offsetof (c_tree_token_vec_struct, tokens));
337
338 /* The resulting tree type. */
339
340 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
341 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
342 {
343 union tree_node GTY ((tag ("0"),
344 desc ("tree_node_structure (&%h)")))
345 generic;
346 struct lang_identifier GTY ((tag ("1"))) identifier;
347 struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
348 };
349
350 /* Langhook for tree_size. */
351 size_t
352 c_tree_size (enum tree_code code)
353 {
354 gcc_checking_assert (code >= NUM_TREE_CODES);
355 switch (code)
356 {
357 case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
358 default:
359 switch (TREE_CODE_CLASS (code))
360 {
361 case tcc_declaration: return sizeof (tree_decl_non_common);
362 case tcc_type: return sizeof (tree_type_non_common);
363 default: gcc_unreachable ();
364 }
365 }
366 }
367
368 /* Track bindings and other things that matter for goto warnings. For
369 efficiency, we do not gather all the decls at the point of
370 definition. Instead, we point into the bindings structure. As
371 scopes are popped, we update these structures and gather the decls
372 that matter at that time. */
373
374 struct GTY(()) c_spot_bindings {
375 /* The currently open scope which holds bindings defined when the
376 label was defined or the goto statement was found. */
377 struct c_scope *scope;
378 /* The bindings in the scope field which were defined at the point
379 of the label or goto. This lets us look at older or newer
380 bindings in the scope, as appropriate. */
381 struct c_binding *bindings_in_scope;
382 /* The number of statement expressions that have started since this
383 label or goto statement was defined. This is zero if we are at
384 the same statement expression level. It is positive if we are in
385 a statement expression started since this spot. It is negative
386 if this spot was in a statement expression and we have left
387 it. */
388 int stmt_exprs;
389 /* Whether we started in a statement expression but are no longer in
390 it. This is set to true if stmt_exprs ever goes negative. */
391 bool left_stmt_expr;
392 };
393
394 /* This structure is used to keep track of bindings seen when a goto
395 statement is defined. This is only used if we see the goto
396 statement before we see the label. */
397
398 struct GTY(()) c_goto_bindings {
399 /* The location of the goto statement. */
400 location_t loc;
401 /* The bindings of the goto statement. */
402 struct c_spot_bindings goto_bindings;
403 };
404
405 typedef struct c_goto_bindings *c_goto_bindings_p;
406
407 /* The additional information we keep track of for a label binding.
408 These fields are updated as scopes are popped. */
409
410 struct GTY(()) c_label_vars {
411 /* The shadowed c_label_vars, when one label shadows another (which
412 can only happen using a __label__ declaration). */
413 struct c_label_vars *shadowed;
414 /* The bindings when the label was defined. */
415 struct c_spot_bindings label_bindings;
416 /* A list of decls that we care about: decls about which we should
417 warn if a goto branches to this label from later in the function.
418 Decls are added to this list as scopes are popped. We only add
419 the decls that matter. */
420 vec<tree, va_gc> *decls_in_scope;
421 /* A list of goto statements to this label. This is only used for
422 goto statements seen before the label was defined, so that we can
423 issue appropriate warnings for them. */
424 vec<c_goto_bindings_p, va_gc> *gotos;
425 };
426
427 /* Each c_scope structure describes the complete contents of one
428 scope. Four scopes are distinguished specially: the innermost or
429 current scope, the innermost function scope, the file scope (always
430 the second to outermost) and the outermost or external scope.
431
432 Most declarations are recorded in the current scope.
433
434 All normal label declarations are recorded in the innermost
435 function scope, as are bindings of undeclared identifiers to
436 error_mark_node. (GCC permits nested functions as an extension,
437 hence the 'innermost' qualifier.) Explicitly declared labels
438 (using the __label__ extension) appear in the current scope.
439
440 Being in the file scope (current_scope == file_scope) causes
441 special behavior in several places below. Also, under some
442 conditions the Objective-C front end records declarations in the
443 file scope even though that isn't the current scope.
444
445 All declarations with external linkage are recorded in the external
446 scope, even if they aren't visible there; this models the fact that
447 such declarations are visible to the entire program, and (with a
448 bit of cleverness, see pushdecl) allows diagnosis of some violations
449 of C99 6.2.2p7 and 6.2.7p2:
450
451 If, within the same translation unit, the same identifier appears
452 with both internal and external linkage, the behavior is
453 undefined.
454
455 All declarations that refer to the same object or function shall
456 have compatible type; otherwise, the behavior is undefined.
457
458 Initially only the built-in declarations, which describe compiler
459 intrinsic functions plus a subset of the standard library, are in
460 this scope.
461
462 The order of the blocks list matters, and it is frequently appended
463 to. To avoid having to walk all the way to the end of the list on
464 each insertion, or reverse the list later, we maintain a pointer to
465 the last list entry. (FIXME: It should be feasible to use a reversed
466 list here.)
467
468 The bindings list is strictly in reverse order of declarations;
469 pop_scope relies on this. */
470
471
472 struct GTY((chain_next ("%h.outer"))) c_scope {
473 /* The scope containing this one. */
474 struct c_scope *outer;
475
476 /* The next outermost function scope. */
477 struct c_scope *outer_function;
478
479 /* All bindings in this scope. */
480 struct c_binding *bindings;
481
482 /* For each scope (except the global one), a chain of BLOCK nodes
483 for all the scopes that were entered and exited one level down. */
484 tree blocks;
485 tree blocks_last;
486
487 /* The depth of this scope. Used to keep the ->shadowed chain of
488 bindings sorted innermost to outermost. */
489 unsigned int depth : 28;
490
491 /* True if we are currently filling this scope with parameter
492 declarations. */
493 BOOL_BITFIELD parm_flag : 1;
494
495 /* True if we saw [*] in this scope. Used to give an error messages
496 if these appears in a function definition. */
497 BOOL_BITFIELD had_vla_unspec : 1;
498
499 /* True if we already complained about forward parameter decls
500 in this scope. This prevents double warnings on
501 foo (int a; int b; ...) */
502 BOOL_BITFIELD warned_forward_parm_decls : 1;
503
504 /* True if this is the outermost block scope of a function body.
505 This scope contains the parameters, the local variables declared
506 in the outermost block, and all the labels (except those in
507 nested functions, or declared at block scope with __label__). */
508 BOOL_BITFIELD function_body : 1;
509
510 /* True means make a BLOCK for this scope no matter what. */
511 BOOL_BITFIELD keep : 1;
512
513 /* True means that an unsuffixed float constant is _Decimal64. */
514 BOOL_BITFIELD float_const_decimal64 : 1;
515
516 /* True if this scope has any label bindings. This is used to speed
517 up searching for labels when popping scopes, particularly since
518 labels are normally only found at function scope. */
519 BOOL_BITFIELD has_label_bindings : 1;
520
521 /* True if we should issue a warning if a goto statement crosses any
522 of the bindings. We still need to check the list of bindings to
523 find the specific ones we need to warn about. This is true if
524 decl_jump_unsafe would return true for any of the bindings. This
525 is used to avoid looping over all the bindings unnecessarily. */
526 BOOL_BITFIELD has_jump_unsafe_decl : 1;
527 };
528
529 /* The scope currently in effect. */
530
531 static GTY(()) struct c_scope *current_scope;
532
533 /* The innermost function scope. Ordinary (not explicitly declared)
534 labels, bindings to error_mark_node, and the lazily-created
535 bindings of __func__ and its friends get this scope. */
536
537 static GTY(()) struct c_scope *current_function_scope;
538
539 /* The C file scope. This is reset for each input translation unit. */
540
541 static GTY(()) struct c_scope *file_scope;
542
543 /* The outermost scope. This is used for all declarations with
544 external linkage, and only these, hence the name. */
545
546 static GTY(()) struct c_scope *external_scope;
547
548 /* A chain of c_scope structures awaiting reuse. */
549
550 static GTY((deletable)) struct c_scope *scope_freelist;
551
552 /* A chain of c_binding structures awaiting reuse. */
553
554 static GTY((deletable)) struct c_binding *binding_freelist;
555
556 /* Append VAR to LIST in scope SCOPE. */
557 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
558 struct c_scope *s_ = (scope); \
559 tree d_ = (decl); \
560 if (s_->list##_last) \
561 BLOCK_CHAIN (s_->list##_last) = d_; \
562 else \
563 s_->list = d_; \
564 s_->list##_last = d_; \
565 } while (0)
566
567 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
568 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
569 struct c_scope *t_ = (tscope); \
570 struct c_scope *f_ = (fscope); \
571 if (t_->to##_last) \
572 BLOCK_CHAIN (t_->to##_last) = f_->from; \
573 else \
574 t_->to = f_->from; \
575 t_->to##_last = f_->from##_last; \
576 } while (0)
577
578 /* A c_inline_static structure stores details of a static identifier
579 referenced in a definition of a function that may be an inline
580 definition if no subsequent declaration of that function uses
581 "extern" or does not use "inline". */
582
583 struct GTY((chain_next ("%h.next"))) c_inline_static {
584 /* The location for a diagnostic. */
585 location_t location;
586
587 /* The function that may be an inline definition. */
588 tree function;
589
590 /* The object or function referenced. */
591 tree static_decl;
592
593 /* What sort of reference this is. */
594 enum c_inline_static_type type;
595
596 /* The next such structure or NULL. */
597 struct c_inline_static *next;
598 };
599
600 /* List of static identifiers used or referenced in functions that may
601 be inline definitions. */
602 static GTY(()) struct c_inline_static *c_inline_statics;
603
604 /* True means unconditionally make a BLOCK for the next scope pushed. */
605
606 static bool keep_next_level_flag;
607
608 /* True means the next call to push_scope will be the outermost scope
609 of a function body, so do not push a new scope, merely cease
610 expecting parameter decls. */
611
612 static bool next_is_function_body;
613
614 /* A vector of pointers to c_binding structures. */
615
616 typedef struct c_binding *c_binding_ptr;
617
618 /* Information that we keep for a struct or union while it is being
619 parsed. */
620
621 class c_struct_parse_info
622 {
623 public:
624 /* If warn_cxx_compat, a list of types defined within this
625 struct. */
626 auto_vec<tree> struct_types;
627 /* If warn_cxx_compat, a list of field names which have bindings,
628 and which are defined in this struct, but which are not defined
629 in any enclosing struct. This is used to clear the in_struct
630 field of the c_bindings structure. */
631 auto_vec<c_binding_ptr> fields;
632 /* If warn_cxx_compat, a list of typedef names used when defining
633 fields in this struct. */
634 auto_vec<tree> typedefs_seen;
635 };
636
637
638 /* Hash table for structs and unions. */
639 struct c_struct_hasher : ggc_ptr_hash<tree_node>
640 {
641 static hashval_t hash (tree t);
642 static bool equal (tree, tree);
643 };
644
645 /* Hash an RECORD OR UNION. */
646 hashval_t
647 c_struct_hasher::hash (tree type)
648 {
649 inchash::hash hstate;
650
651 hstate.add_int (TREE_CODE (type));
652 hstate.add_object (TYPE_NAME (type));
653
654 return hstate.end ();
655 }
656
657 /* Compare two RECORD or UNION types. */
658 bool
659 c_struct_hasher::equal (tree t1, tree t2)
660 {
661 return comptypes_equiv_p (t1, t2);
662 }
663
664 /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
665 static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
666
667 /* Information for the struct or union currently being parsed, or
668 NULL if not parsing a struct or union. */
669 static class c_struct_parse_info *struct_parse_info;
670
671 /* Forward declarations. */
672 static tree lookup_name_in_scope (tree, struct c_scope *);
673 static tree c_make_fname_decl (location_t, tree, int);
674 static tree grokdeclarator (const struct c_declarator *,
675 struct c_declspecs *,
676 enum decl_context, bool, tree *, tree *, tree *,
677 bool *, enum deprecated_states);
678 static tree grokparms (struct c_arg_info *, bool);
679 static void layout_array_type (tree);
680 static const char *header_for_builtin_fn (tree);
681 \f
682 /* T is a statement. Add it to the statement-tree. This is the
683 C/ObjC version--C++ has a slightly different version of this
684 function. */
685
686 tree
687 add_stmt (tree t)
688 {
689 enum tree_code code = TREE_CODE (t);
690
691 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
692 {
693 if (!EXPR_HAS_LOCATION (t))
694 SET_EXPR_LOCATION (t, input_location);
695 }
696
697 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
698 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
699
700 /* Add T to the statement-tree. Non-side-effect statements need to be
701 recorded during statement expressions. */
702 if (!building_stmt_list_p ())
703 push_stmt_list ();
704 append_to_statement_list_force (t, &cur_stmt_list);
705
706 return t;
707 }
708 \f
709 /* Build a pointer type using the default pointer mode. */
710
711 static tree
712 c_build_pointer_type (tree to_type)
713 {
714 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
715 : TYPE_ADDR_SPACE (to_type);
716 machine_mode pointer_mode;
717
718 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
719 pointer_mode = targetm.addr_space.pointer_mode (as);
720 else
721 pointer_mode = c_default_pointer_mode;
722 return build_pointer_type_for_mode (to_type, pointer_mode, false);
723 }
724
725 \f
726 /* Return true if we will want to say something if a goto statement
727 crosses DECL. */
728
729 static bool
730 decl_jump_unsafe (tree decl)
731 {
732 if (error_operand_p (decl))
733 return false;
734
735 /* Don't warn for compound literals. If a goto statement crosses
736 their initialization, it should cross also all the places where
737 the complit is used or where the complit address might be saved
738 into some variable, so code after the label to which goto jumps
739 should not be able to refer to the compound literal. */
740 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
741 return false;
742
743 if (flag_openmp
744 && VAR_P (decl)
745 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
746 return true;
747
748 /* Always warn about crossing variably modified types. */
749 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
750 && c_type_variably_modified_p (TREE_TYPE (decl)))
751 return true;
752
753 /* Otherwise, only warn if -Wgoto-misses-init and this is an
754 initialized automatic decl. */
755 if (warn_jump_misses_init
756 && VAR_P (decl)
757 && !TREE_STATIC (decl)
758 && DECL_INITIAL (decl) != NULL_TREE)
759 return true;
760
761 return false;
762 }
763 \f
764
765 void
766 c_print_identifier (FILE *file, tree node, int indent)
767 {
768 void (*save) (enum c_oracle_request, tree identifier);
769
770 /* Temporarily hide any binding oracle. Without this, calls to
771 debug_tree from the debugger will end up calling into the oracle,
772 making for a confusing debug session. As the oracle isn't needed
773 here for normal operation, it's simplest to suppress it. */
774 save = c_binding_oracle;
775 c_binding_oracle = NULL;
776
777 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
778 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
779 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
780 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
781 {
782 tree rid = ridpointers[C_RID_CODE (node)];
783 indent_to (file, indent + 4);
784 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
785 (void *) rid, IDENTIFIER_POINTER (rid));
786 }
787
788 c_binding_oracle = save;
789 }
790
791 /* Establish that the scope contains declarations that are sensitive to
792 jumps that cross a binding. Together with decl_jump_unsafe, this is
793 used to diagnose such jumps. */
794 void
795 c_mark_decl_jump_unsafe_in_current_scope ()
796 {
797 current_scope->has_jump_unsafe_decl = 1;
798 }
799
800 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
801 which may be any of several kinds of DECL or TYPE or error_mark_node,
802 in the scope SCOPE. */
803 static void
804 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
805 bool nested, location_t locus)
806 {
807 struct c_binding *b, **here;
808
809 if (binding_freelist)
810 {
811 b = binding_freelist;
812 binding_freelist = b->prev;
813 }
814 else
815 b = ggc_alloc<c_binding> ();
816
817 b->shadowed = 0;
818 b->decl = decl;
819 b->id = name;
820 b->depth = scope->depth;
821 b->invisible = invisible;
822 b->nested = nested;
823 b->inner_comp = 0;
824 b->in_struct = 0;
825 b->locus = locus;
826
827 b->u.type = NULL;
828
829 b->prev = scope->bindings;
830 scope->bindings = b;
831
832 if (decl_jump_unsafe (decl))
833 scope->has_jump_unsafe_decl = 1;
834
835 if (!name)
836 return;
837
838 switch (TREE_CODE (decl))
839 {
840 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
841 case ENUMERAL_TYPE:
842 case UNION_TYPE:
843 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
844 case VAR_DECL:
845 case FUNCTION_DECL:
846 case TYPE_DECL:
847 case CONST_DECL:
848 case PARM_DECL:
849 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
850
851 default:
852 gcc_unreachable ();
853 }
854
855 /* Locate the appropriate place in the chain of shadowed decls
856 to insert this binding. Normally, scope == current_scope and
857 this does nothing. */
858 while (*here && (*here)->depth > scope->depth)
859 here = &(*here)->shadowed;
860
861 b->shadowed = *here;
862 *here = b;
863 }
864
865 /* Clear the binding structure B, stick it on the binding_freelist,
866 and return the former value of b->prev. This is used by pop_scope
867 and get_parm_info to iterate destructively over all the bindings
868 from a given scope. */
869 static struct c_binding *
870 free_binding_and_advance (struct c_binding *b)
871 {
872 struct c_binding *prev = b->prev;
873
874 memset (b, 0, sizeof (struct c_binding));
875 b->prev = binding_freelist;
876 binding_freelist = b;
877
878 return prev;
879 }
880
881 /* Bind a label. Like bind, but skip fields which aren't used for
882 labels, and add the LABEL_VARS value. */
883 static void
884 bind_label (tree name, tree label, struct c_scope *scope,
885 struct c_label_vars *label_vars)
886 {
887 struct c_binding *b;
888
889 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
890 UNKNOWN_LOCATION);
891
892 scope->has_label_bindings = true;
893
894 b = scope->bindings;
895 gcc_assert (b->decl == label);
896 label_vars->shadowed = b->u.label;
897 b->u.label = label_vars;
898 }
899 \f
900 /* Hook called at end of compilation to assume 1 elt
901 for a file-scope tentative array defn that wasn't complete before. */
902
903 void
904 c_finish_incomplete_decl (tree decl)
905 {
906 if (VAR_P (decl))
907 {
908 tree type = TREE_TYPE (decl);
909 if (type != error_mark_node
910 && TREE_CODE (type) == ARRAY_TYPE
911 && !DECL_EXTERNAL (decl)
912 && TYPE_DOMAIN (type) == NULL_TREE)
913 {
914 warning_at (DECL_SOURCE_LOCATION (decl),
915 0, "array %q+D assumed to have one element", decl);
916
917 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
918
919 relayout_decl (decl);
920 }
921 }
922 }
923 \f
924 /* Record that inline function FUNC contains a reference (location
925 LOC) to static DECL (file-scope or function-local according to
926 TYPE). */
927
928 void
929 record_inline_static (location_t loc, tree func, tree decl,
930 enum c_inline_static_type type)
931 {
932 c_inline_static *csi = ggc_alloc<c_inline_static> ();
933 csi->location = loc;
934 csi->function = func;
935 csi->static_decl = decl;
936 csi->type = type;
937 csi->next = c_inline_statics;
938 c_inline_statics = csi;
939 }
940
941 /* Check for references to static declarations in inline functions at
942 the end of the translation unit and diagnose them if the functions
943 are still inline definitions. */
944
945 static void
946 check_inline_statics (void)
947 {
948 struct c_inline_static *csi;
949 for (csi = c_inline_statics; csi; csi = csi->next)
950 {
951 if (DECL_EXTERNAL (csi->function))
952 switch (csi->type)
953 {
954 case csi_internal:
955 pedwarn (csi->location, 0,
956 "%qD is static but used in inline function %qD "
957 "which is not static", csi->static_decl, csi->function);
958 break;
959 case csi_modifiable:
960 pedwarn (csi->location, 0,
961 "%q+D is static but declared in inline function %qD "
962 "which is not static", csi->static_decl, csi->function);
963 break;
964 default:
965 gcc_unreachable ();
966 }
967 }
968 c_inline_statics = NULL;
969 }
970 \f
971 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
972 for the current state, otherwise set it to uninitialized. */
973
974 static void
975 set_spot_bindings (struct c_spot_bindings *p, bool defining)
976 {
977 if (defining)
978 {
979 p->scope = current_scope;
980 p->bindings_in_scope = current_scope->bindings;
981 }
982 else
983 {
984 p->scope = NULL;
985 p->bindings_in_scope = NULL;
986 }
987 p->stmt_exprs = 0;
988 p->left_stmt_expr = false;
989 }
990
991 /* Update spot bindings P as we pop out of SCOPE. Return true if we
992 should push decls for a label. */
993
994 static bool
995 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
996 {
997 if (p->scope != scope)
998 {
999 /* This label or goto is defined in some other scope, or it is a
1000 label which is not yet defined. There is nothing to
1001 update. */
1002 return false;
1003 }
1004
1005 /* Adjust the spot bindings to refer to the bindings already defined
1006 in the enclosing scope. */
1007 p->scope = scope->outer;
1008 p->bindings_in_scope = p->scope->bindings;
1009
1010 return true;
1011 }
1012 \f
1013 /* The Objective-C front-end often needs to determine the current scope. */
1014
1015 void *
1016 objc_get_current_scope (void)
1017 {
1018 return current_scope;
1019 }
1020
1021 /* The following function is used only by Objective-C. It needs to live here
1022 because it accesses the innards of c_scope. */
1023
1024 void
1025 objc_mark_locals_volatile (void *enclosing_blk)
1026 {
1027 struct c_scope *scope;
1028 struct c_binding *b;
1029
1030 for (scope = current_scope;
1031 scope && scope != enclosing_blk;
1032 scope = scope->outer)
1033 {
1034 for (b = scope->bindings; b; b = b->prev)
1035 objc_volatilize_decl (b->decl);
1036
1037 /* Do not climb up past the current function. */
1038 if (scope->function_body)
1039 break;
1040 }
1041 }
1042
1043 /* Return true if we are in the global binding level. */
1044
1045 bool
1046 global_bindings_p (void)
1047 {
1048 return current_scope == file_scope;
1049 }
1050
1051 /* Return true if we're declaring parameters in an old-style function
1052 declaration. */
1053
1054 bool
1055 old_style_parameter_scope (void)
1056 {
1057 /* If processing parameters and there is no function statement list, we
1058 * have an old-style function declaration. */
1059 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1060 }
1061
1062 void
1063 keep_next_level (void)
1064 {
1065 keep_next_level_flag = true;
1066 }
1067
1068 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1069
1070 void
1071 set_float_const_decimal64 (void)
1072 {
1073 current_scope->float_const_decimal64 = true;
1074 }
1075
1076 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1077
1078 void
1079 clear_float_const_decimal64 (void)
1080 {
1081 current_scope->float_const_decimal64 = false;
1082 }
1083
1084 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1085
1086 bool
1087 float_const_decimal64_p (void)
1088 {
1089 return current_scope->float_const_decimal64;
1090 }
1091
1092 /* Identify this scope as currently being filled with parameters. */
1093
1094 void
1095 declare_parm_level (void)
1096 {
1097 current_scope->parm_flag = true;
1098 }
1099
1100 void
1101 push_scope (void)
1102 {
1103 if (next_is_function_body)
1104 {
1105 /* This is the transition from the parameters to the top level
1106 of the function body. These are the same scope
1107 (C99 6.2.1p4,6) so we do not push another scope structure.
1108 next_is_function_body is set only by store_parm_decls, which
1109 in turn is called when and only when we are about to
1110 encounter the opening curly brace for the function body.
1111
1112 The outermost block of a function always gets a BLOCK node,
1113 because the debugging output routines expect that each
1114 function has at least one BLOCK. */
1115 current_scope->parm_flag = false;
1116 current_scope->function_body = true;
1117 current_scope->keep = true;
1118 current_scope->outer_function = current_function_scope;
1119 current_function_scope = current_scope;
1120
1121 keep_next_level_flag = false;
1122 next_is_function_body = false;
1123
1124 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1125 if (current_scope->outer)
1126 current_scope->float_const_decimal64
1127 = current_scope->outer->float_const_decimal64;
1128 else
1129 current_scope->float_const_decimal64 = false;
1130 }
1131 else
1132 {
1133 struct c_scope *scope;
1134 if (scope_freelist)
1135 {
1136 scope = scope_freelist;
1137 scope_freelist = scope->outer;
1138 }
1139 else
1140 scope = ggc_cleared_alloc<c_scope> ();
1141
1142 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1143 if (current_scope)
1144 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1145 else
1146 scope->float_const_decimal64 = false;
1147
1148 scope->keep = keep_next_level_flag;
1149 scope->outer = current_scope;
1150 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1151
1152 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1153 possible. */
1154 if (current_scope && scope->depth == 0)
1155 {
1156 scope->depth--;
1157 sorry ("GCC supports only %u nested scopes", scope->depth);
1158 }
1159
1160 current_scope = scope;
1161 keep_next_level_flag = false;
1162 }
1163 }
1164
1165 /* This is called when we are leaving SCOPE. For each label defined
1166 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1167 These are the decls whose initialization will be skipped by a goto
1168 later in the function. */
1169
1170 static void
1171 update_label_decls (struct c_scope *scope)
1172 {
1173 struct c_scope *s;
1174
1175 s = scope;
1176 while (s != NULL)
1177 {
1178 if (s->has_label_bindings)
1179 {
1180 struct c_binding *b;
1181
1182 for (b = s->bindings; b != NULL; b = b->prev)
1183 {
1184 struct c_label_vars *label_vars;
1185 struct c_binding *b1;
1186 bool hjud;
1187 unsigned int ix;
1188 struct c_goto_bindings *g;
1189
1190 if (TREE_CODE (b->decl) != LABEL_DECL)
1191 continue;
1192 label_vars = b->u.label;
1193
1194 b1 = label_vars->label_bindings.bindings_in_scope;
1195 if (label_vars->label_bindings.scope == NULL)
1196 hjud = false;
1197 else
1198 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1199 if (update_spot_bindings (scope, &label_vars->label_bindings))
1200 {
1201 /* This label is defined in this scope. */
1202 if (hjud)
1203 {
1204 for (; b1 != NULL; b1 = b1->prev)
1205 {
1206 /* A goto from later in the function to this
1207 label will never see the initialization
1208 of B1, if any. Save it to issue a
1209 warning if needed. */
1210 if (decl_jump_unsafe (b1->decl))
1211 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1212 }
1213 }
1214 }
1215
1216 /* Update the bindings of any goto statements associated
1217 with this label. */
1218 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1219 update_spot_bindings (scope, &g->goto_bindings);
1220 }
1221 }
1222
1223 /* Don't search beyond the current function. */
1224 if (s == current_function_scope)
1225 break;
1226
1227 s = s->outer;
1228 }
1229 }
1230
1231 /* Exit a scope. Restore the state of the identifier-decl mappings
1232 that were in effect when this scope was entered. Return a BLOCK
1233 node containing all the DECLs in this scope that are of interest
1234 to debug info generation. */
1235
1236 tree
1237 pop_scope (void)
1238 {
1239 struct c_scope *scope = current_scope;
1240 tree block, context, p;
1241 struct c_binding *b;
1242
1243 bool functionbody = scope->function_body;
1244 bool keep = functionbody || scope->keep || scope->bindings;
1245
1246 update_label_decls (scope);
1247
1248 /* If appropriate, create a BLOCK to record the decls for the life
1249 of this function. */
1250 block = NULL_TREE;
1251 if (keep)
1252 {
1253 block = make_node (BLOCK);
1254 BLOCK_SUBBLOCKS (block) = scope->blocks;
1255 TREE_USED (block) = 1;
1256
1257 /* In each subblock, record that this is its superior. */
1258 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1259 BLOCK_SUPERCONTEXT (p) = block;
1260
1261 BLOCK_VARS (block) = NULL_TREE;
1262 }
1263
1264 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1265 scope must be set so that they point to the appropriate
1266 construct, i.e. either to the current FUNCTION_DECL node, or
1267 else to the BLOCK node we just constructed.
1268
1269 Note that for tagged types whose scope is just the formal
1270 parameter list for some function type specification, we can't
1271 properly set their TYPE_CONTEXTs here, because we don't have a
1272 pointer to the appropriate FUNCTION_TYPE node readily available
1273 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1274 type nodes get set in `grokdeclarator' as soon as we have created
1275 the FUNCTION_TYPE node which will represent the "scope" for these
1276 "parameter list local" tagged types. */
1277 if (scope->function_body)
1278 context = current_function_decl;
1279 else if (scope == file_scope)
1280 {
1281 tree file_decl
1282 = build_translation_unit_decl (get_identifier (main_input_filename));
1283 context = file_decl;
1284 debug_hooks->register_main_translation_unit (file_decl);
1285 }
1286 else
1287 context = block;
1288
1289 /* Clear all bindings in this scope. */
1290 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1291 {
1292 p = b->decl;
1293 switch (TREE_CODE (p))
1294 {
1295 case LABEL_DECL:
1296 /* Warnings for unused labels, errors for undefined labels. */
1297 if (TREE_USED (p) && !DECL_INITIAL (p))
1298 {
1299 error ("label %q+D used but not defined", p);
1300 DECL_INITIAL (p) = error_mark_node;
1301 }
1302 else
1303 warn_for_unused_label (p);
1304
1305 /* Labels go in BLOCK_VARS. */
1306 DECL_CHAIN (p) = BLOCK_VARS (block);
1307 BLOCK_VARS (block) = p;
1308 gcc_assert (I_LABEL_BINDING (b->id) == b);
1309 I_LABEL_BINDING (b->id) = b->shadowed;
1310
1311 /* Also pop back to the shadowed label_vars. */
1312 release_tree_vector (b->u.label->decls_in_scope);
1313 b->u.label = b->u.label->shadowed;
1314 break;
1315
1316 case ENUMERAL_TYPE:
1317 case UNION_TYPE:
1318 case RECORD_TYPE:
1319
1320 /* Types may not have tag-names, in which case the type
1321 appears in the bindings list with b->id NULL. */
1322 if (b->id)
1323 {
1324 gcc_assert (I_TAG_BINDING (b->id) == b);
1325 I_TAG_BINDING (b->id) = b->shadowed;
1326 }
1327 break;
1328
1329 case FUNCTION_DECL:
1330 /* Propagate TREE_ADDRESSABLE from nested functions to their
1331 containing functions. */
1332 if (!TREE_ASM_WRITTEN (p)
1333 && DECL_INITIAL (p) != NULL_TREE
1334 && TREE_ADDRESSABLE (p)
1335 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1336 && DECL_ABSTRACT_ORIGIN (p) != p)
1337 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1338 if (!TREE_PUBLIC (p)
1339 && !DECL_INITIAL (p)
1340 && !b->nested
1341 && scope != file_scope
1342 && scope != external_scope)
1343 {
1344 error ("nested function %q+D declared but never defined", p);
1345 undef_nested_function = true;
1346 }
1347 else if (DECL_DECLARED_INLINE_P (p)
1348 && TREE_PUBLIC (p)
1349 && !DECL_INITIAL (p))
1350 {
1351 /* C99 6.7.4p6: "a function with external linkage... declared
1352 with an inline function specifier ... shall also be defined
1353 in the same translation unit." */
1354 if (!flag_gnu89_inline
1355 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1356 && scope == external_scope)
1357 pedwarn (input_location, 0,
1358 "inline function %q+D declared but never defined", p);
1359 DECL_EXTERNAL (p) = 1;
1360 }
1361
1362 goto common_symbol;
1363
1364 case VAR_DECL:
1365 /* Warnings for unused variables. */
1366 if ((!TREE_USED (p) || !DECL_READ_P (p))
1367 && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1368 && !DECL_IN_SYSTEM_HEADER (p)
1369 && DECL_NAME (p)
1370 && !DECL_ARTIFICIAL (p)
1371 && scope != file_scope
1372 && scope != external_scope)
1373 {
1374 if (!TREE_USED (p))
1375 {
1376 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1377 suppress_warning (p, OPT_Wunused_variable);
1378 }
1379 else if (DECL_CONTEXT (p) == current_function_decl)
1380 warning_at (DECL_SOURCE_LOCATION (p),
1381 OPT_Wunused_but_set_variable,
1382 "variable %qD set but not used", p);
1383 }
1384
1385 if (b->inner_comp)
1386 {
1387 error ("type of array %q+D completed incompatibly with"
1388 " implicit initialization", p);
1389 }
1390
1391 /* Fall through. */
1392 case TYPE_DECL:
1393 case CONST_DECL:
1394 common_symbol:
1395 /* All of these go in BLOCK_VARS, but only if this is the
1396 binding in the home scope. */
1397 if (!b->nested)
1398 {
1399 DECL_CHAIN (p) = BLOCK_VARS (block);
1400 BLOCK_VARS (block) = p;
1401 }
1402 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1403 {
1404 /* For block local externs add a special
1405 DECL_EXTERNAL decl for debug info generation. */
1406 tree extp = copy_node (p);
1407
1408 DECL_EXTERNAL (extp) = 1;
1409 TREE_STATIC (extp) = 0;
1410 TREE_PUBLIC (extp) = 1;
1411 DECL_INITIAL (extp) = NULL_TREE;
1412 DECL_LANG_SPECIFIC (extp) = NULL;
1413 DECL_CONTEXT (extp) = current_function_decl;
1414 if (TREE_CODE (p) == FUNCTION_DECL)
1415 {
1416 DECL_RESULT (extp) = NULL_TREE;
1417 DECL_SAVED_TREE (extp) = NULL_TREE;
1418 DECL_STRUCT_FUNCTION (extp) = NULL;
1419 }
1420 if (b->locus != UNKNOWN_LOCATION)
1421 DECL_SOURCE_LOCATION (extp) = b->locus;
1422 DECL_CHAIN (extp) = BLOCK_VARS (block);
1423 BLOCK_VARS (block) = extp;
1424 }
1425 /* If this is the file scope set DECL_CONTEXT of each decl to
1426 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1427 work. */
1428 if (scope == file_scope)
1429 DECL_CONTEXT (p) = context;
1430
1431 gcc_fallthrough ();
1432 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1433 already been put there by store_parm_decls. Unused-
1434 parameter warnings are handled by function.cc.
1435 error_mark_node obviously does not go in BLOCK_VARS and
1436 does not get unused-variable warnings. */
1437 case PARM_DECL:
1438 case ERROR_MARK:
1439 /* It is possible for a decl not to have a name. We get
1440 here with b->id NULL in this case. */
1441 if (b->id)
1442 {
1443 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1444 I_SYMBOL_BINDING (b->id) = b->shadowed;
1445 if (b->shadowed && b->shadowed->u.type)
1446 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1447 }
1448 break;
1449
1450 default:
1451 gcc_unreachable ();
1452 }
1453 }
1454
1455
1456 /* Dispose of the block that we just made inside some higher level. */
1457 if ((scope->function_body || scope == file_scope) && context)
1458 {
1459 DECL_INITIAL (context) = block;
1460 BLOCK_SUPERCONTEXT (block) = context;
1461 }
1462 else if (scope->outer)
1463 {
1464 if (block)
1465 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1466 /* If we did not make a block for the scope just exited, any
1467 blocks made for inner scopes must be carried forward so they
1468 will later become subblocks of something else. */
1469 else if (scope->blocks)
1470 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1471 }
1472
1473 /* Pop the current scope, and free the structure for reuse. */
1474 current_scope = scope->outer;
1475 if (scope->function_body)
1476 current_function_scope = scope->outer_function;
1477
1478 memset (scope, 0, sizeof (struct c_scope));
1479 scope->outer = scope_freelist;
1480 scope_freelist = scope;
1481
1482 return block;
1483 }
1484
1485 void
1486 push_file_scope (void)
1487 {
1488 tree decl;
1489
1490 if (file_scope)
1491 return;
1492
1493 push_scope ();
1494 file_scope = current_scope;
1495
1496 start_fname_decls ();
1497
1498 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1499 bind (DECL_NAME (decl), decl, file_scope,
1500 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1501 }
1502
1503 void
1504 pop_file_scope (void)
1505 {
1506 /* In case there were missing closebraces, get us back to the global
1507 binding level. */
1508 while (current_scope != file_scope)
1509 pop_scope ();
1510
1511 /* __FUNCTION__ is defined at file scope (""). This
1512 call may not be necessary as my tests indicate it
1513 still works without it. */
1514 finish_fname_decls ();
1515
1516 check_inline_statics ();
1517
1518 /* This is the point to write out a PCH if we're doing that.
1519 In that case we do not want to do anything else. */
1520 if (pch_file)
1521 {
1522 c_common_write_pch ();
1523 /* Ensure even the callers don't try to finalize the CU. */
1524 flag_syntax_only = 1;
1525 return;
1526 }
1527
1528 /* Pop off the file scope and close this translation unit. */
1529 pop_scope ();
1530 file_scope = 0;
1531
1532 maybe_apply_pending_pragma_weaks ();
1533 }
1534 \f
1535 /* Whether we are curently inside the initializer for an
1536 underspecified object definition (C23 auto or constexpr). */
1537 static bool in_underspecified_init;
1538
1539 /* Start an underspecified object definition for NAME at LOC. This
1540 means that NAME is shadowed inside its initializer, so neither the
1541 definition being initialized, nor any definition from an outer
1542 scope, may be referenced during that initializer. Return state to
1543 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1544 underspecified object is a (constexpr) compound literal; there is
1545 no shadowing in that case, but all the other restrictions on
1546 underspecified object definitions still apply. */
1547 unsigned int
1548 start_underspecified_init (location_t loc, tree name)
1549 {
1550 bool prev = in_underspecified_init;
1551 bool ok;
1552 if (name == NULL_TREE)
1553 ok = true;
1554 else
1555 {
1556 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1557 C_DECL_UNDERSPECIFIED (decl) = 1;
1558 struct c_scope *scope = current_scope;
1559 struct c_binding *b = I_SYMBOL_BINDING (name);
1560 if (b && B_IN_SCOPE (b, scope))
1561 {
1562 error_at (loc, "underspecified declaration of %qE, which is already "
1563 "declared in this scope", name);
1564 ok = false;
1565 }
1566 else
1567 {
1568 bind (name, decl, scope, false, false, loc);
1569 ok = true;
1570 }
1571 }
1572 in_underspecified_init = true;
1573 return ok | (prev << 1);
1574 }
1575
1576 /* Finish an underspecified object definition for NAME, before that
1577 name is bound to the real declaration instead of a placeholder.
1578 PREV_STATE is the value returned by the call to
1579 start_underspecified_init. If NAME is NULL_TREE, this means a
1580 compound literal, as for start_underspecified_init. */
1581 void
1582 finish_underspecified_init (tree name, unsigned int prev_state)
1583 {
1584 if (name != NULL_TREE && (prev_state & 1))
1585 {
1586 /* A VAR_DECL was bound to the name to shadow any previous
1587 declarations for the name; remove that binding now. */
1588 struct c_scope *scope = current_scope;
1589 struct c_binding *b = I_SYMBOL_BINDING (name);
1590 gcc_assert (b);
1591 gcc_assert (B_IN_SCOPE (b, scope));
1592 gcc_assert (VAR_P (b->decl));
1593 gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1594 I_SYMBOL_BINDING (name) = b->shadowed;
1595 /* In erroneous cases there may be other bindings added to this
1596 scope during the initializer. */
1597 struct c_binding **p = &scope->bindings;
1598 while (*p != b)
1599 p = &((*p)->prev);
1600 *p = free_binding_and_advance (*p);
1601 }
1602 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1603 }
1604 \f
1605 /* Adjust the bindings for the start of a statement expression. */
1606
1607 void
1608 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1609 {
1610 struct c_scope *scope;
1611
1612 for (scope = current_scope; scope != NULL; scope = scope->outer)
1613 {
1614 struct c_binding *b;
1615
1616 if (!scope->has_label_bindings)
1617 continue;
1618
1619 for (b = scope->bindings; b != NULL; b = b->prev)
1620 {
1621 struct c_label_vars *label_vars;
1622 unsigned int ix;
1623 struct c_goto_bindings *g;
1624
1625 if (TREE_CODE (b->decl) != LABEL_DECL)
1626 continue;
1627 label_vars = b->u.label;
1628 ++label_vars->label_bindings.stmt_exprs;
1629 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1630 ++g->goto_bindings.stmt_exprs;
1631 }
1632 }
1633
1634 if (switch_bindings != NULL)
1635 ++switch_bindings->stmt_exprs;
1636 }
1637
1638 /* Adjust the bindings for the end of a statement expression. */
1639
1640 void
1641 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1642 {
1643 struct c_scope *scope;
1644
1645 for (scope = current_scope; scope != NULL; scope = scope->outer)
1646 {
1647 struct c_binding *b;
1648
1649 if (!scope->has_label_bindings)
1650 continue;
1651
1652 for (b = scope->bindings; b != NULL; b = b->prev)
1653 {
1654 struct c_label_vars *label_vars;
1655 unsigned int ix;
1656 struct c_goto_bindings *g;
1657
1658 if (TREE_CODE (b->decl) != LABEL_DECL)
1659 continue;
1660 label_vars = b->u.label;
1661 --label_vars->label_bindings.stmt_exprs;
1662 if (label_vars->label_bindings.stmt_exprs < 0)
1663 {
1664 label_vars->label_bindings.left_stmt_expr = true;
1665 label_vars->label_bindings.stmt_exprs = 0;
1666 }
1667 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1668 {
1669 --g->goto_bindings.stmt_exprs;
1670 if (g->goto_bindings.stmt_exprs < 0)
1671 {
1672 g->goto_bindings.left_stmt_expr = true;
1673 g->goto_bindings.stmt_exprs = 0;
1674 }
1675 }
1676 }
1677 }
1678
1679 if (switch_bindings != NULL)
1680 {
1681 --switch_bindings->stmt_exprs;
1682 gcc_assert (switch_bindings->stmt_exprs >= 0);
1683 }
1684 }
1685 \f
1686 /* Push a definition or a declaration of struct, union or enum tag "name".
1687 "type" should be the type node.
1688 We assume that the tag "name" is not already defined, and has a location
1689 of LOC.
1690
1691 Note that the definition may really be just a forward reference.
1692 In that case, the TYPE_SIZE will be zero. */
1693
1694 static void
1695 pushtag (location_t loc, tree name, tree type)
1696 {
1697 /* Record the identifier as the type's name if it has none. */
1698 if (name && !TYPE_NAME (type))
1699 TYPE_NAME (type) = name;
1700 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1701
1702 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1703 tagged type we just added to the current scope. This fake
1704 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1705 to output a representation of a tagged type, and it also gives
1706 us a convenient place to record the "scope start" address for the
1707 tagged type. */
1708
1709 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1710 TYPE_DECL, NULL_TREE, type));
1711
1712 /* An approximation for now, so we can tell this is a function-scope tag.
1713 This will be updated in pop_scope. */
1714 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1715
1716 if (warn_cxx_compat && name != NULL_TREE)
1717 {
1718 struct c_binding *b = I_SYMBOL_BINDING (name);
1719
1720 if (b != NULL
1721 && b->decl != NULL_TREE
1722 && TREE_CODE (b->decl) == TYPE_DECL
1723 && (B_IN_CURRENT_SCOPE (b)
1724 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1725 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1726 != TYPE_MAIN_VARIANT (type)))
1727 {
1728 auto_diagnostic_group d;
1729 if (warning_at (loc, OPT_Wc___compat,
1730 ("using %qD as both a typedef and a tag is "
1731 "invalid in C++"), b->decl)
1732 && b->locus != UNKNOWN_LOCATION)
1733 inform (b->locus, "originally defined here");
1734 }
1735 }
1736 }
1737
1738 /* An exported interface to pushtag. This is used by the gdb plugin's
1739 binding oracle to introduce a new tag binding. */
1740
1741 void
1742 c_pushtag (location_t loc, tree name, tree type)
1743 {
1744 pushtag (loc, name, type);
1745 }
1746
1747 /* An exported interface to bind a declaration. LOC is the location
1748 to use. DECL is the declaration to bind. The decl's name is used
1749 to determine how it is bound. If DECL is a VAR_DECL, then
1750 IS_GLOBAL determines whether the decl is put into the global (file
1751 and external) scope or the current function's scope; if DECL is not
1752 a VAR_DECL then it is always put into the file scope. */
1753
1754 void
1755 c_bind (location_t loc, tree decl, bool is_global)
1756 {
1757 struct c_scope *scope;
1758 bool nested = false;
1759
1760 if (!VAR_P (decl) || current_function_scope == NULL)
1761 {
1762 /* Types and functions are always considered to be global. */
1763 scope = file_scope;
1764 DECL_EXTERNAL (decl) = 1;
1765 TREE_PUBLIC (decl) = 1;
1766 }
1767 else if (is_global)
1768 {
1769 /* Also bind it into the external scope. */
1770 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1771 nested = true;
1772 scope = file_scope;
1773 DECL_EXTERNAL (decl) = 1;
1774 TREE_PUBLIC (decl) = 1;
1775 }
1776 else
1777 {
1778 DECL_CONTEXT (decl) = current_function_decl;
1779 TREE_PUBLIC (decl) = 0;
1780 scope = current_function_scope;
1781 }
1782
1783 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1784 }
1785 \f
1786
1787 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1788 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1789 to the builtin_structptr_types array. Subsequent declarations of such
1790 built-ins are expected to refer to it rather than to fileptr_type_node,
1791 etc. which is just void* (or to any other type).
1792 Used only by match_builtin_function_types. */
1793
1794 static const unsigned builtin_structptr_type_count
1795 = ARRAY_SIZE (builtin_structptr_types);
1796
1797 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1798
1799 /* Returns true if types T1 and T2 representing return types or types
1800 of function arguments are close enough to be considered interchangeable
1801 in redeclarations of built-in functions. */
1802
1803 static bool
1804 types_close_enough_to_match (tree t1, tree t2)
1805 {
1806 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1807 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1808 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1809 }
1810
1811 /* Subroutine of compare_decls. Allow harmless mismatches in return
1812 and argument types provided that the type modes match. Set *STRICT
1813 and *ARGNO to the expected argument type and number in case of
1814 an argument type mismatch or null and zero otherwise. Return
1815 a unified type given a suitable match, and 0 otherwise. */
1816
1817 static tree
1818 match_builtin_function_types (tree newtype, tree oldtype,
1819 tree *strict, unsigned *argno)
1820 {
1821 *argno = 0;
1822 *strict = NULL_TREE;
1823
1824 /* Accept the return type of the new declaration if it has the same
1825 mode and if they're both pointers or if neither is. */
1826 tree oldrettype = TREE_TYPE (oldtype);
1827 tree newrettype = TREE_TYPE (newtype);
1828
1829 if (!types_close_enough_to_match (oldrettype, newrettype))
1830 return NULL_TREE;
1831
1832 /* Check that the return types are compatible but don't fail if they
1833 are not (e.g., int vs long in ILP32) and just let the caller know. */
1834 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1835 TYPE_MAIN_VARIANT (newrettype)))
1836 *strict = oldrettype;
1837
1838 tree oldargs = TYPE_ARG_TYPES (oldtype);
1839 tree newargs = TYPE_ARG_TYPES (newtype);
1840 tree tryargs = newargs;
1841
1842 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1843 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1844
1845 gcc_checking_assert (nlst == nbst);
1846
1847 for (unsigned i = 1; oldargs || newargs; ++i)
1848 {
1849 if (!oldargs
1850 || !newargs
1851 || !TREE_VALUE (oldargs)
1852 || !TREE_VALUE (newargs))
1853 return NULL_TREE;
1854
1855 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1856 tree newtype = TREE_VALUE (newargs);
1857 if (newtype == error_mark_node)
1858 return NULL_TREE;
1859 newtype = TYPE_MAIN_VARIANT (newtype);
1860
1861 if (!types_close_enough_to_match (oldtype, newtype))
1862 return NULL_TREE;
1863
1864 unsigned j = nbst;
1865 if (POINTER_TYPE_P (oldtype))
1866 /* Iterate over well-known struct types like FILE (whose types
1867 aren't known to us) and compare the pointer to each to
1868 the pointer argument. */
1869 for (j = 0; j < nbst; ++j)
1870 {
1871 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1872 continue;
1873 /* Store the first FILE* etc. argument type (whatever it is), and
1874 expect any subsequent declarations of file I/O etc. built-ins
1875 to refer to it rather than to fileptr_type_node etc. which is
1876 just void* (or const void*). */
1877 if (last_structptr_types[j])
1878 {
1879 if (!comptypes (last_structptr_types[j], newtype))
1880 {
1881 *argno = i;
1882 *strict = last_structptr_types[j];
1883 }
1884 }
1885 else
1886 last_structptr_types[j] = newtype;
1887 break;
1888 }
1889
1890 if (j == nbst && !comptypes (oldtype, newtype))
1891 {
1892 if (POINTER_TYPE_P (oldtype))
1893 {
1894 /* For incompatible pointers, only reject differences in
1895 the unqualified variants of the referenced types but
1896 consider differences in qualifiers as benign (report
1897 those to caller via *STRICT below). */
1898 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1899 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1900 if (!comptypes (oldref, newref))
1901 return NULL_TREE;
1902 }
1903
1904 if (!*strict)
1905 {
1906 *argno = i;
1907 *strict = oldtype;
1908 }
1909 }
1910
1911 oldargs = TREE_CHAIN (oldargs);
1912 newargs = TREE_CHAIN (newargs);
1913 }
1914
1915 tree trytype = build_function_type (newrettype, tryargs);
1916
1917 /* Allow declaration to change transaction_safe attribute. */
1918 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1919 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1920 tree newattrs = TYPE_ATTRIBUTES (newtype);
1921 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1922 if (oldtsafe && !newtsafe)
1923 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1924 else if (newtsafe && !oldtsafe)
1925 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1926 NULL_TREE, oldattrs);
1927
1928 return build_type_attribute_variant (trytype, oldattrs);
1929 }
1930
1931 /* Subroutine of diagnose_mismatched_decls. Check for function type
1932 mismatch involving an empty arglist vs a nonempty one and give clearer
1933 diagnostics. */
1934 static void
1935 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1936 tree newtype, tree oldtype)
1937 {
1938 tree t;
1939
1940 if (TREE_CODE (olddecl) != FUNCTION_DECL
1941 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1942 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1943 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1944 return;
1945
1946 t = TYPE_ARG_TYPES (oldtype);
1947 if (t == NULL_TREE)
1948 t = TYPE_ARG_TYPES (newtype);
1949 for (; t; t = TREE_CHAIN (t))
1950 {
1951 tree type = TREE_VALUE (t);
1952
1953 if (TREE_CHAIN (t) == NULL_TREE
1954 && TYPE_MAIN_VARIANT (type) != void_type_node)
1955 {
1956 inform (input_location, "a parameter list with an ellipsis "
1957 "cannot match an empty parameter name list declaration");
1958 break;
1959 }
1960
1961 if (!error_operand_p (type)
1962 && c_type_promotes_to (type) != type)
1963 {
1964 inform (input_location, "an argument type that has a default "
1965 "promotion cannot match an empty parameter name list "
1966 "declaration");
1967 break;
1968 }
1969 }
1970 }
1971
1972 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1973 old-style function definition, NEWDECL is a prototype declaration.
1974 Diagnose inconsistencies in the argument list. Returns TRUE if
1975 the prototype is compatible, FALSE if not. */
1976 static bool
1977 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1978 {
1979 tree newargs, oldargs;
1980 int i;
1981
1982 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1983
1984 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1985 newargs = TYPE_ARG_TYPES (newtype);
1986 i = 1;
1987
1988 for (;;)
1989 {
1990 tree oldargtype = TREE_VALUE (oldargs);
1991 tree newargtype = TREE_VALUE (newargs);
1992
1993 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1994 return false;
1995
1996 oldargtype = (TYPE_ATOMIC (oldargtype)
1997 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1998 TYPE_QUAL_ATOMIC)
1999 : TYPE_MAIN_VARIANT (oldargtype));
2000 newargtype = (TYPE_ATOMIC (newargtype)
2001 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2002 TYPE_QUAL_ATOMIC)
2003 : TYPE_MAIN_VARIANT (newargtype));
2004
2005 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2006 break;
2007
2008 /* Reaching the end of just one list means the two decls don't
2009 agree on the number of arguments. */
2010 if (END_OF_ARGLIST (oldargtype))
2011 {
2012 error ("prototype for %q+D declares more arguments "
2013 "than previous old-style definition", newdecl);
2014 return false;
2015 }
2016 else if (END_OF_ARGLIST (newargtype))
2017 {
2018 error ("prototype for %q+D declares fewer arguments "
2019 "than previous old-style definition", newdecl);
2020 return false;
2021 }
2022
2023 /* Type for passing arg must be consistent with that declared
2024 for the arg. */
2025 else if (!comptypes (oldargtype, newargtype))
2026 {
2027 error ("prototype for %q+D declares argument %d"
2028 " with incompatible type",
2029 newdecl, i);
2030 return false;
2031 }
2032
2033 oldargs = TREE_CHAIN (oldargs);
2034 newargs = TREE_CHAIN (newargs);
2035 i++;
2036 }
2037
2038 /* If we get here, no errors were found, but do issue a warning
2039 for this poor-style construct. */
2040 warning (0, "prototype for %q+D follows non-prototype definition",
2041 newdecl);
2042 return true;
2043 #undef END_OF_ARGLIST
2044 }
2045
2046 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2047 first in a pair of mismatched declarations, using the diagnostic
2048 function DIAG. */
2049 static void
2050 locate_old_decl (tree decl)
2051 {
2052 if (TREE_CODE (decl) == FUNCTION_DECL
2053 && fndecl_built_in_p (decl)
2054 && !C_DECL_DECLARED_BUILTIN (decl))
2055 ;
2056 else if (DECL_INITIAL (decl))
2057 inform (input_location,
2058 "previous definition of %q+D with type %qT",
2059 decl, TREE_TYPE (decl));
2060 else if (C_DECL_IMPLICIT (decl))
2061 inform (input_location,
2062 "previous implicit declaration of %q+D with type %qT",
2063 decl, TREE_TYPE (decl));
2064 else
2065 inform (input_location,
2066 "previous declaration of %q+D with type %qT",
2067 decl, TREE_TYPE (decl));
2068 }
2069
2070
2071 /* Helper function. For a tagged type, it finds the declaration
2072 for a visible tag declared in the the same scope if such a
2073 declaration exists. */
2074 static tree
2075 previous_tag (tree type)
2076 {
2077 struct c_binding *b = NULL;
2078 tree name = TYPE_NAME (type);
2079
2080 if (name)
2081 b = I_TAG_BINDING (name);
2082
2083 if (b)
2084 b = b->shadowed;
2085
2086 if (b && B_IN_CURRENT_SCOPE (b))
2087 return b->decl;
2088
2089 return NULL_TREE;
2090 }
2091
2092 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2093 Returns true if the caller should proceed to merge the two, false
2094 if OLDDECL should simply be discarded. As a side effect, issues
2095 all necessary diagnostics for invalid or poor-style combinations.
2096 If it returns true, writes the types of NEWDECL and OLDDECL to
2097 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2098 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2099
2100 static bool
2101 diagnose_mismatched_decls (tree newdecl, tree olddecl,
2102 tree *newtypep, tree *oldtypep)
2103 {
2104 tree newtype, oldtype;
2105 bool retval = true;
2106
2107 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2108 && DECL_EXTERNAL (DECL))
2109
2110 /* If we have error_mark_node for either decl or type, just discard
2111 the previous decl - we're in an error cascade already. */
2112 if (olddecl == error_mark_node || newdecl == error_mark_node)
2113 return false;
2114 *oldtypep = oldtype = TREE_TYPE (olddecl);
2115 *newtypep = newtype = TREE_TYPE (newdecl);
2116 if (oldtype == error_mark_node || newtype == error_mark_node)
2117 return false;
2118
2119 /* Two different categories of symbol altogether. This is an error
2120 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2121 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2122 {
2123 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2124 && fndecl_built_in_p (olddecl)
2125 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2126 {
2127 auto_diagnostic_group d;
2128 error ("%q+D redeclared as different kind of symbol", newdecl);
2129 locate_old_decl (olddecl);
2130 }
2131 else if (TREE_PUBLIC (newdecl))
2132 warning (OPT_Wbuiltin_declaration_mismatch,
2133 "built-in function %q+D declared as non-function",
2134 newdecl);
2135 else
2136 warning (OPT_Wshadow, "declaration of %q+D shadows "
2137 "a built-in function", newdecl);
2138 return false;
2139 }
2140
2141 /* Enumerators have no linkage, so may only be declared once in a
2142 given scope. */
2143 if (TREE_CODE (olddecl) == CONST_DECL)
2144 {
2145 if (flag_isoc23
2146 && TYPE_NAME (DECL_CONTEXT (newdecl))
2147 && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2148 && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2149 {
2150 if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2151 {
2152 auto_diagnostic_group d;
2153 error ("conflicting redeclaration of enumerator %q+D", newdecl);
2154 locate_old_decl (olddecl);
2155 }
2156 }
2157 else
2158 {
2159 auto_diagnostic_group d;
2160 error ("redeclaration of enumerator %q+D", newdecl);
2161 locate_old_decl (olddecl);
2162 }
2163 return false;
2164 }
2165
2166 bool pedwarned = false;
2167 bool warned = false;
2168 bool enum_and_int_p = false;
2169 auto_diagnostic_group d;
2170
2171 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2172 &enum_and_int_p);
2173 if (!comptypes_result)
2174 {
2175 if (TREE_CODE (olddecl) == FUNCTION_DECL
2176 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2177 && !C_DECL_DECLARED_BUILTIN (olddecl))
2178 {
2179 /* Accept "harmless" mismatches in function types such
2180 as missing qualifiers or int vs long when they're the same
2181 size. However, diagnose return and argument types that are
2182 incompatible according to language rules. */
2183 tree mismatch_expect;
2184 unsigned mismatch_argno;
2185
2186 tree trytype = match_builtin_function_types (newtype, oldtype,
2187 &mismatch_expect,
2188 &mismatch_argno);
2189
2190 if (trytype && comptypes (newtype, trytype))
2191 *oldtypep = oldtype = trytype;
2192 else
2193 {
2194 /* If types don't match for a built-in, throw away the
2195 built-in. No point in calling locate_old_decl here, it
2196 won't print anything. */
2197 const char *header = header_for_builtin_fn (olddecl);
2198 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2199 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2200 "conflicting types for built-in function %q+D; "
2201 "expected %qT",
2202 newdecl, oldtype)
2203 && header)
2204 {
2205 /* Suggest the right header to include as the preferred
2206 solution rather than the spelling of the declaration. */
2207 rich_location richloc (line_table, loc);
2208 maybe_add_include_fixit (&richloc, header, true);
2209 inform (&richloc,
2210 "%qD is declared in header %qs", olddecl, header);
2211 }
2212 return false;
2213 }
2214
2215 if (mismatch_expect && extra_warnings)
2216 {
2217 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2218 bool warned = false;
2219 if (mismatch_argno)
2220 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2221 "mismatch in argument %u type of built-in "
2222 "function %qD; expected %qT",
2223 mismatch_argno, newdecl, mismatch_expect);
2224 else
2225 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2226 "mismatch in return type of built-in "
2227 "function %qD; expected %qT",
2228 newdecl, mismatch_expect);
2229 const char *header = header_for_builtin_fn (olddecl);
2230 if (warned && header)
2231 {
2232 rich_location richloc (line_table, newloc);
2233 maybe_add_include_fixit (&richloc, header, true);
2234 inform (&richloc,
2235 "%qD is declared in header %qs", olddecl, header);
2236 }
2237 }
2238 }
2239 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2240 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2241 {
2242 /* A conflicting function declaration for a predeclared
2243 function that isn't actually built in. Objective C uses
2244 these. The new declaration silently overrides everything
2245 but the volatility (i.e. noreturn) indication. See also
2246 below. FIXME: Make Objective C use normal builtins. */
2247 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2248 return false;
2249 }
2250 /* Permit void foo (...) to match int foo (...) if the latter is
2251 the definition and implicit int was used. See
2252 c-torture/compile/920625-2.c. */
2253 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2254 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2255 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2256 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2257 {
2258 pedwarned = pedwarn (input_location, 0,
2259 "conflicting types for %q+D", newdecl);
2260 /* Make sure we keep void as the return type. */
2261 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2262 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2263 }
2264 /* Permit void foo (...) to match an earlier call to foo (...) with
2265 no declared type (thus, implicitly int). */
2266 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2267 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2268 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2269 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2270 {
2271 pedwarned = pedwarn (input_location, 0,
2272 "conflicting types for %q+D; have %qT",
2273 newdecl, newtype);
2274 /* Make sure we keep void as the return type. */
2275 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2276 }
2277 else
2278 {
2279 int new_quals = TYPE_QUALS (newtype);
2280 int old_quals = TYPE_QUALS (oldtype);
2281
2282 if (new_quals != old_quals)
2283 {
2284 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2285 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2286 if (new_addr != old_addr)
2287 {
2288 if (ADDR_SPACE_GENERIC_P (new_addr))
2289 error ("conflicting named address spaces (generic vs %s) "
2290 "for %q+D",
2291 c_addr_space_name (old_addr), newdecl);
2292 else if (ADDR_SPACE_GENERIC_P (old_addr))
2293 error ("conflicting named address spaces (%s vs generic) "
2294 "for %q+D",
2295 c_addr_space_name (new_addr), newdecl);
2296 else
2297 error ("conflicting named address spaces (%s vs %s) "
2298 "for %q+D",
2299 c_addr_space_name (new_addr),
2300 c_addr_space_name (old_addr),
2301 newdecl);
2302 }
2303
2304 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2305 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2306 error ("conflicting type qualifiers for %q+D", newdecl);
2307 }
2308 else
2309 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2310 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2311 locate_old_decl (olddecl);
2312 return false;
2313 }
2314 }
2315 /* Warn about enum/integer type mismatches. They are compatible types
2316 (C23 6.7.2.2/5), but may pose portability problems. */
2317 else if (enum_and_int_p
2318 && TREE_CODE (newdecl) != TYPE_DECL
2319 /* Don't warn about about acc_on_device built-in redeclaration,
2320 the built-in is declared with int rather than enum because
2321 the enum isn't intrinsic. */
2322 && !(TREE_CODE (olddecl) == FUNCTION_DECL
2323 && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2324 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2325 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2326 OPT_Wenum_int_mismatch,
2327 "conflicting types for %q+D due to enum/integer "
2328 "mismatch; have %qT", newdecl, newtype);
2329
2330 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2331 but silently ignore the redeclaration if either is in a system
2332 header. (Conflicting redeclarations were handled above.) This
2333 is allowed for C11 if the types are the same, not just
2334 compatible. */
2335 if (TREE_CODE (newdecl) == TYPE_DECL)
2336 {
2337 bool types_different = false;
2338
2339 comptypes_result
2340 = comptypes_check_different_types (oldtype, newtype, &types_different);
2341
2342 if (comptypes_result != 1 || types_different)
2343 {
2344 error ("redefinition of typedef %q+D with different type", newdecl);
2345 locate_old_decl (olddecl);
2346 return false;
2347 }
2348
2349 if (DECL_IN_SYSTEM_HEADER (newdecl)
2350 || DECL_IN_SYSTEM_HEADER (olddecl)
2351 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2352 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2353 return true; /* Allow OLDDECL to continue in use. */
2354
2355 if (c_type_variably_modified_p (newtype))
2356 {
2357 error ("redefinition of typedef %q+D with variably modified type",
2358 newdecl);
2359 locate_old_decl (olddecl);
2360 }
2361 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2362 "redefinition of typedef %q+D", newdecl))
2363 locate_old_decl (olddecl);
2364
2365 return true;
2366 }
2367
2368 /* Function declarations can either be 'static' or 'extern' (no
2369 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2370 can never conflict with each other on account of linkage
2371 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2372 gnu89 mode permits two definitions if one is 'extern inline' and
2373 one is not. The non- extern-inline definition supersedes the
2374 extern-inline definition. */
2375
2376 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2377 {
2378 /* If you declare a built-in function name as static, or
2379 define the built-in with an old-style definition (so we
2380 can't validate the argument list) the built-in definition is
2381 overridden, but optionally warn this was a bad choice of name. */
2382 if (fndecl_built_in_p (olddecl)
2383 && !C_DECL_DECLARED_BUILTIN (olddecl))
2384 {
2385 if (!TREE_PUBLIC (newdecl)
2386 || (DECL_INITIAL (newdecl)
2387 && !prototype_p (TREE_TYPE (newdecl))))
2388 {
2389 warning_at (DECL_SOURCE_LOCATION (newdecl),
2390 OPT_Wshadow, "declaration of %qD shadows "
2391 "a built-in function", newdecl);
2392 /* Discard the old built-in function. */
2393 return false;
2394 }
2395
2396 if (!prototype_p (TREE_TYPE (newdecl)))
2397 {
2398 /* Set for built-ins that take no arguments. */
2399 bool func_void_args = false;
2400 if (tree at = TYPE_ARG_TYPES (oldtype))
2401 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2402
2403 if (extra_warnings && !func_void_args)
2404 warning_at (DECL_SOURCE_LOCATION (newdecl),
2405 OPT_Wbuiltin_declaration_mismatch,
2406 "declaration of built-in function %qD without "
2407 "a prototype; expected %qT",
2408 newdecl, TREE_TYPE (olddecl));
2409 }
2410 }
2411
2412 if (DECL_INITIAL (newdecl))
2413 {
2414 if (DECL_INITIAL (olddecl))
2415 {
2416 /* If the new declaration isn't overriding an extern inline
2417 reject the new decl. In c99, no overriding is allowed
2418 in the same translation unit. */
2419 if (!DECL_EXTERN_INLINE (olddecl)
2420 || DECL_EXTERN_INLINE (newdecl)
2421 || (!flag_gnu89_inline
2422 && (!DECL_DECLARED_INLINE_P (olddecl)
2423 || !lookup_attribute ("gnu_inline",
2424 DECL_ATTRIBUTES (olddecl)))
2425 && (!DECL_DECLARED_INLINE_P (newdecl)
2426 || !lookup_attribute ("gnu_inline",
2427 DECL_ATTRIBUTES (newdecl)))))
2428 {
2429 auto_diagnostic_group d;
2430 error ("redefinition of %q+D", newdecl);
2431 locate_old_decl (olddecl);
2432 return false;
2433 }
2434 }
2435 }
2436 /* If we have a prototype after an old-style function definition,
2437 the argument types must be checked specially. */
2438 else if (DECL_INITIAL (olddecl)
2439 && !prototype_p (oldtype) && prototype_p (newtype)
2440 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2441 {
2442 auto_diagnostic_group d;
2443 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2444 {
2445 locate_old_decl (olddecl);
2446 return false;
2447 }
2448 }
2449 /* A non-static declaration (even an "extern") followed by a
2450 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2451 The same is true for a static forward declaration at block
2452 scope followed by a non-static declaration/definition at file
2453 scope. Static followed by non-static at the same scope is
2454 not undefined behavior, and is the most convenient way to get
2455 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2456 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2457 we do diagnose it if -Wtraditional. */
2458 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2459 {
2460 /* Two exceptions to the rule. If olddecl is an extern
2461 inline, or a predeclared function that isn't actually
2462 built in, newdecl silently overrides olddecl. The latter
2463 occur only in Objective C; see also above. (FIXME: Make
2464 Objective C use normal builtins.) */
2465 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2466 && !DECL_EXTERN_INLINE (olddecl))
2467 {
2468 auto_diagnostic_group d;
2469 error ("static declaration of %q+D follows "
2470 "non-static declaration", newdecl);
2471 locate_old_decl (olddecl);
2472 }
2473 return false;
2474 }
2475 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2476 {
2477 if (DECL_CONTEXT (olddecl))
2478 {
2479 auto_diagnostic_group d;
2480 error ("non-static declaration of %q+D follows "
2481 "static declaration", newdecl);
2482 locate_old_decl (olddecl);
2483 return false;
2484 }
2485 else if (warn_traditional)
2486 {
2487 warned |= warning (OPT_Wtraditional,
2488 "non-static declaration of %q+D "
2489 "follows static declaration", newdecl);
2490 }
2491 }
2492
2493 /* Make sure gnu_inline attribute is either not present, or
2494 present on all inline decls. */
2495 if (DECL_DECLARED_INLINE_P (olddecl)
2496 && DECL_DECLARED_INLINE_P (newdecl))
2497 {
2498 bool newa = lookup_attribute ("gnu_inline",
2499 DECL_ATTRIBUTES (newdecl)) != NULL;
2500 bool olda = lookup_attribute ("gnu_inline",
2501 DECL_ATTRIBUTES (olddecl)) != NULL;
2502 if (newa != olda)
2503 {
2504 auto_diagnostic_group d;
2505 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2506 newa ? newdecl : olddecl);
2507 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2508 "but not here");
2509 }
2510 }
2511 }
2512 else if (VAR_P (newdecl))
2513 {
2514 /* Only variables can be thread-local, and all declarations must
2515 agree on this property. */
2516 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2517 {
2518 /* Nothing to check. Since OLDDECL is marked threadprivate
2519 and NEWDECL does not have a thread-local attribute, we
2520 will merge the threadprivate attribute into NEWDECL. */
2521 ;
2522 }
2523 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2524 {
2525 auto_diagnostic_group d;
2526 if (DECL_THREAD_LOCAL_P (newdecl))
2527 error ("thread-local declaration of %q+D follows "
2528 "non-thread-local declaration", newdecl);
2529 else
2530 error ("non-thread-local declaration of %q+D follows "
2531 "thread-local declaration", newdecl);
2532
2533 locate_old_decl (olddecl);
2534 return false;
2535 }
2536
2537 /* Multiple initialized definitions are not allowed (6.9p3,5).
2538 For this purpose, C23 makes it clear that thread-local
2539 declarations without extern are definitions, not tentative
2540 definitions, whether or not they have initializers. The
2541 wording before C23 was unclear; literally it would have made
2542 uninitialized thread-local declarations into tentative
2543 definitions only if they also used static, but without saying
2544 explicitly whether or not other cases count as
2545 definitions at all. */
2546 if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2547 || (flag_isoc23
2548 && DECL_THREAD_LOCAL_P (newdecl)
2549 && !DECL_EXTERNAL (newdecl)
2550 && !DECL_EXTERNAL (olddecl)))
2551 {
2552 auto_diagnostic_group d;
2553 error ("redefinition of %q+D", newdecl);
2554 locate_old_decl (olddecl);
2555 return false;
2556 }
2557
2558 /* Objects declared at file scope: if the first declaration had
2559 external linkage (even if it was an external reference) the
2560 second must have external linkage as well, or the behavior is
2561 undefined. If the first declaration had internal linkage, then
2562 the second must too, or else be an external reference (in which
2563 case the composite declaration still has internal linkage).
2564 As for function declarations, we warn about the static-then-
2565 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2566 if (DECL_FILE_SCOPE_P (newdecl)
2567 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2568 {
2569 if (DECL_EXTERNAL (newdecl))
2570 {
2571 if (!DECL_FILE_SCOPE_P (olddecl))
2572 {
2573 auto_diagnostic_group d;
2574 error ("extern declaration of %q+D follows "
2575 "declaration with no linkage", newdecl);
2576 locate_old_decl (olddecl);
2577 return false;
2578 }
2579 else if (warn_traditional)
2580 {
2581 warned |= warning (OPT_Wtraditional,
2582 "non-static declaration of %q+D "
2583 "follows static declaration", newdecl);
2584 }
2585 }
2586 else
2587 {
2588 auto_diagnostic_group d;
2589 if (TREE_PUBLIC (newdecl))
2590 error ("non-static declaration of %q+D follows "
2591 "static declaration", newdecl);
2592 else
2593 error ("static declaration of %q+D follows "
2594 "non-static declaration", newdecl);
2595
2596 locate_old_decl (olddecl);
2597 return false;
2598 }
2599 }
2600 /* Two objects with the same name declared at the same block
2601 scope must both be external references (6.7p3). */
2602 else if (!DECL_FILE_SCOPE_P (newdecl))
2603 {
2604 if (DECL_EXTERNAL (newdecl))
2605 {
2606 /* Extern with initializer at block scope, which will
2607 already have received an error. */
2608 }
2609 else if (DECL_EXTERNAL (olddecl))
2610 {
2611 auto_diagnostic_group d;
2612 error ("declaration of %q+D with no linkage follows "
2613 "extern declaration", newdecl);
2614 locate_old_decl (olddecl);
2615 }
2616 else
2617 {
2618 auto_diagnostic_group d;
2619 error ("redeclaration of %q+D with no linkage", newdecl);
2620 locate_old_decl (olddecl);
2621 }
2622
2623 return false;
2624 }
2625
2626 /* C++ does not permit a decl to appear multiple times at file
2627 scope. */
2628 if (warn_cxx_compat
2629 && DECL_FILE_SCOPE_P (newdecl)
2630 && !DECL_EXTERNAL (newdecl)
2631 && !DECL_EXTERNAL (olddecl))
2632 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2633 OPT_Wc___compat,
2634 ("duplicate declaration of %qD is "
2635 "invalid in C++"),
2636 newdecl);
2637 }
2638
2639 /* warnings */
2640 /* All decls must agree on a visibility. */
2641 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2642 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2643 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2644 {
2645 warned |= warning (0, "redeclaration of %q+D with different visibility "
2646 "(old visibility preserved)", newdecl);
2647 }
2648
2649 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2650 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2651 else /* PARM_DECL, VAR_DECL */
2652 {
2653 /* Redeclaration of a parameter is a constraint violation (this is
2654 not explicitly stated, but follows from C99 6.7p3 [no more than
2655 one declaration of the same identifier with no linkage in the
2656 same scope, except type tags] and 6.2.2p6 [parameters have no
2657 linkage]). We must check for a forward parameter declaration,
2658 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2659 an extension, the mandatory diagnostic for which is handled by
2660 mark_forward_parm_decls. */
2661
2662 if (TREE_CODE (newdecl) == PARM_DECL
2663 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2664 {
2665 auto_diagnostic_group d;
2666 error ("redefinition of parameter %q+D", newdecl);
2667 locate_old_decl (olddecl);
2668 return false;
2669 }
2670 }
2671
2672 /* Optional warning for completely redundant decls. */
2673 if (!warned && !pedwarned
2674 && warn_redundant_decls
2675 /* Don't warn about a function declaration followed by a
2676 definition. */
2677 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2678 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2679 /* Don't warn about redundant redeclarations of builtins. */
2680 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2681 && !fndecl_built_in_p (newdecl)
2682 && fndecl_built_in_p (olddecl)
2683 && !C_DECL_DECLARED_BUILTIN (olddecl))
2684 /* Don't warn about an extern followed by a definition. */
2685 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2686 /* Don't warn about forward parameter decls. */
2687 && !(TREE_CODE (newdecl) == PARM_DECL
2688 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2689 /* Don't warn about a variable definition following a declaration. */
2690 && !(VAR_P (newdecl)
2691 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2692 {
2693 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2694 newdecl);
2695 }
2696
2697 /* Report location of previous decl/defn. */
2698 if (warned || pedwarned)
2699 locate_old_decl (olddecl);
2700
2701 #undef DECL_EXTERN_INLINE
2702
2703 return retval;
2704 }
2705
2706 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2707 consistent with OLDDECL, but carries new information. Merge the
2708 new information into OLDDECL. This function issues no
2709 diagnostics. */
2710
2711 static void
2712 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2713 {
2714 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2715 && DECL_INITIAL (newdecl) != NULL_TREE);
2716 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2717 && prototype_p (TREE_TYPE (newdecl)));
2718 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2719 && prototype_p (TREE_TYPE (olddecl)));
2720
2721 /* For real parm decl following a forward decl, rechain the old decl
2722 in its new location and clear TREE_ASM_WRITTEN (it's not a
2723 forward decl anymore). */
2724 if (TREE_CODE (newdecl) == PARM_DECL
2725 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2726 {
2727 struct c_binding *b, **here;
2728
2729 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2730 if ((*here)->decl == olddecl)
2731 goto found;
2732 gcc_unreachable ();
2733
2734 found:
2735 b = *here;
2736 *here = b->prev;
2737 b->prev = current_scope->bindings;
2738 current_scope->bindings = b;
2739
2740 TREE_ASM_WRITTEN (olddecl) = 0;
2741 }
2742
2743 DECL_ATTRIBUTES (newdecl)
2744 = targetm.merge_decl_attributes (olddecl, newdecl);
2745
2746 /* For typedefs use the old type, as the new type's DECL_NAME points
2747 at newdecl, which will be ggc_freed. */
2748 if (TREE_CODE (newdecl) == TYPE_DECL)
2749 {
2750 /* But NEWTYPE might have an attribute, honor that. */
2751 tree tem = newtype;
2752 newtype = oldtype;
2753
2754 if (TYPE_USER_ALIGN (tem))
2755 {
2756 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2757 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2758 TYPE_USER_ALIGN (newtype) = true;
2759 }
2760
2761 /* And remove the new type from the variants list. */
2762 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2763 {
2764 tree remove = TREE_TYPE (newdecl);
2765 if (TYPE_MAIN_VARIANT (remove) == remove)
2766 {
2767 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2768 /* If remove is the main variant, no need to remove that
2769 from the list. One of the DECL_ORIGINAL_TYPE
2770 variants, e.g. created for aligned attribute, might still
2771 refer to the newdecl TYPE_DECL though, so remove that one
2772 in that case. */
2773 if (DECL_ORIGINAL_TYPE (newdecl)
2774 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2775 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2776 t; t = TYPE_MAIN_VARIANT (t))
2777 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2778 {
2779 TYPE_NEXT_VARIANT (t)
2780 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2781 break;
2782 }
2783 }
2784 else
2785 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2786 t = TYPE_NEXT_VARIANT (t))
2787 if (TYPE_NEXT_VARIANT (t) == remove)
2788 {
2789 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2790 break;
2791 }
2792 }
2793 }
2794
2795 /* Merge the data types specified in the two decls. */
2796 TREE_TYPE (newdecl)
2797 = TREE_TYPE (olddecl)
2798 = composite_type (newtype, oldtype);
2799
2800 /* Lay the type out, unless already done. */
2801 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2802 {
2803 if (TREE_TYPE (newdecl) != error_mark_node)
2804 layout_type (TREE_TYPE (newdecl));
2805 if (TREE_CODE (newdecl) != FUNCTION_DECL
2806 && TREE_CODE (newdecl) != TYPE_DECL
2807 && TREE_CODE (newdecl) != CONST_DECL)
2808 layout_decl (newdecl, 0);
2809 }
2810 else
2811 {
2812 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2813 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2814 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2815 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2816 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2817 {
2818 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2819 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2820 }
2821 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2822 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2823 DECL_USER_ALIGN (newdecl) = 1;
2824 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2825 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2826 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2827 DECL_WARN_IF_NOT_ALIGN (olddecl));
2828 }
2829
2830 /* Keep the old rtl since we can safely use it. */
2831 if (HAS_RTL_P (olddecl))
2832 COPY_DECL_RTL (olddecl, newdecl);
2833
2834 /* Merge the type qualifiers. */
2835 if (TREE_READONLY (newdecl))
2836 TREE_READONLY (olddecl) = 1;
2837
2838 if (TREE_THIS_VOLATILE (newdecl))
2839 TREE_THIS_VOLATILE (olddecl) = 1;
2840
2841 /* Merge deprecatedness. */
2842 if (TREE_DEPRECATED (newdecl))
2843 TREE_DEPRECATED (olddecl) = 1;
2844
2845 /* Merge unavailability. */
2846 if (TREE_UNAVAILABLE (newdecl))
2847 TREE_UNAVAILABLE (olddecl) = 1;
2848
2849 /* If a decl is in a system header and the other isn't, keep the one on the
2850 system header. Otherwise, keep source location of definition rather than
2851 declaration and of prototype rather than non-prototype unless that
2852 prototype is built-in. */
2853 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2854 && DECL_IN_SYSTEM_HEADER (olddecl)
2855 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2856 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2857 else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2858 && DECL_IN_SYSTEM_HEADER (newdecl)
2859 && !DECL_IN_SYSTEM_HEADER (olddecl))
2860 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2861 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2862 && DECL_INITIAL (olddecl) != NULL_TREE)
2863 || (old_is_prototype && !new_is_prototype
2864 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2865 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2866
2867 /* Merge the initialization information. */
2868 if (DECL_INITIAL (newdecl) == NULL_TREE)
2869 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2870
2871 /* Merge 'constexpr' information. */
2872 if (VAR_P (olddecl) && VAR_P (newdecl))
2873 {
2874 if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2875 C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2876 else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2877 C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2878 }
2879
2880 /* Merge the threadprivate attribute. */
2881 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2882 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2883
2884 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2885 {
2886 /* Copy the assembler name.
2887 Currently, it can only be defined in the prototype. */
2888 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2889
2890 /* Use visibility of whichever declaration had it specified */
2891 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2892 {
2893 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2894 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2895 }
2896
2897 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2898 {
2899 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2900 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2901 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2902 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2903 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2904 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2905 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2906 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2907 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2908 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2909 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2910 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2911 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2912 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2913 }
2914
2915 /* Merge the storage class information. */
2916 merge_weak (newdecl, olddecl);
2917
2918 /* For functions, static overrides non-static. */
2919 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2920 {
2921 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2922 /* This is since we don't automatically
2923 copy the attributes of NEWDECL into OLDDECL. */
2924 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2925 /* If this clears `static', clear it in the identifier too. */
2926 if (!TREE_PUBLIC (olddecl))
2927 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2928 }
2929 }
2930
2931 /* In c99, 'extern' declaration before (or after) 'inline' means this
2932 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2933 is present. */
2934 if (TREE_CODE (newdecl) == FUNCTION_DECL
2935 && !flag_gnu89_inline
2936 && (DECL_DECLARED_INLINE_P (newdecl)
2937 || DECL_DECLARED_INLINE_P (olddecl))
2938 && (!DECL_DECLARED_INLINE_P (newdecl)
2939 || !DECL_DECLARED_INLINE_P (olddecl)
2940 || !DECL_EXTERNAL (olddecl))
2941 && DECL_EXTERNAL (newdecl)
2942 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2943 && !current_function_decl)
2944 DECL_EXTERNAL (newdecl) = 0;
2945
2946 /* An inline definition following a static declaration is not
2947 DECL_EXTERNAL. */
2948 if (new_is_definition
2949 && (DECL_DECLARED_INLINE_P (newdecl)
2950 || DECL_DECLARED_INLINE_P (olddecl))
2951 && !TREE_PUBLIC (olddecl))
2952 DECL_EXTERNAL (newdecl) = 0;
2953
2954 if (DECL_EXTERNAL (newdecl))
2955 {
2956 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2957 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2958
2959 /* An extern decl does not override previous storage class. */
2960 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2961 if (!DECL_EXTERNAL (newdecl))
2962 {
2963 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2964 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2965 }
2966 }
2967 else
2968 {
2969 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2970 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2971 }
2972
2973 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2974 {
2975 /* If we're redefining a function previously defined as extern
2976 inline, make sure we emit debug info for the inline before we
2977 throw it away, in case it was inlined into a function that
2978 hasn't been written out yet. */
2979 if (new_is_definition && DECL_INITIAL (olddecl))
2980 /* The new defn must not be inline. */
2981 DECL_UNINLINABLE (newdecl) = 1;
2982 else
2983 {
2984 /* If either decl says `inline', this fn is inline, unless
2985 its definition was passed already. */
2986 if (DECL_DECLARED_INLINE_P (newdecl)
2987 || DECL_DECLARED_INLINE_P (olddecl))
2988 DECL_DECLARED_INLINE_P (newdecl) = 1;
2989
2990 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2991 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2992
2993 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2994 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2995 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2996 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2997 }
2998
2999 if (fndecl_built_in_p (olddecl))
3000 {
3001 /* If redeclaring a builtin function, it stays built in.
3002 But it gets tagged as having been declared. */
3003 copy_decl_built_in_function (newdecl, olddecl);
3004 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3005 if (new_is_prototype)
3006 {
3007 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3008 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3009 {
3010 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3011 switch (fncode)
3012 {
3013 /* If a compatible prototype of these builtin functions
3014 is seen, assume the runtime implements it with the
3015 expected semantics. */
3016 case BUILT_IN_STPCPY:
3017 if (builtin_decl_explicit_p (fncode))
3018 set_builtin_decl_implicit_p (fncode, true);
3019 break;
3020 default:
3021 if (builtin_decl_explicit_p (fncode))
3022 set_builtin_decl_declared_p (fncode, true);
3023 break;
3024 }
3025
3026 copy_attributes_to_builtin (newdecl);
3027 }
3028 }
3029 else
3030 C_DECL_BUILTIN_PROTOTYPE (newdecl)
3031 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3032 }
3033
3034 /* Preserve function specific target and optimization options */
3035 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3036 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3037 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3038 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3039
3040 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3041 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3042 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3043 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3044
3045 /* Also preserve various other info from the definition. */
3046 if (!new_is_definition)
3047 {
3048 tree t;
3049 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3050 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3051 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3052 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3053 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3054 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3055 DECL_CONTEXT (t) = newdecl;
3056
3057 /* See if we've got a function to instantiate from. */
3058 if (DECL_SAVED_TREE (olddecl))
3059 DECL_ABSTRACT_ORIGIN (newdecl)
3060 = DECL_ABSTRACT_ORIGIN (olddecl);
3061 }
3062 }
3063
3064 /* Merge the USED information. */
3065 if (TREE_USED (olddecl))
3066 TREE_USED (newdecl) = 1;
3067 else if (TREE_USED (newdecl))
3068 TREE_USED (olddecl) = 1;
3069 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3070 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3071 if (DECL_PRESERVE_P (olddecl))
3072 DECL_PRESERVE_P (newdecl) = 1;
3073 else if (DECL_PRESERVE_P (newdecl))
3074 DECL_PRESERVE_P (olddecl) = 1;
3075
3076 /* Merge DECL_COMMON */
3077 if (VAR_P (olddecl) && VAR_P (newdecl)
3078 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3079 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3080 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3081
3082 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3083 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3084 DECL_ARGUMENTS (if appropriate). */
3085 {
3086 unsigned olddecl_uid = DECL_UID (olddecl);
3087 tree olddecl_context = DECL_CONTEXT (olddecl);
3088 tree olddecl_arguments = NULL;
3089 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3090 olddecl_arguments = DECL_ARGUMENTS (olddecl);
3091
3092 memcpy ((char *) olddecl + sizeof (struct tree_common),
3093 (char *) newdecl + sizeof (struct tree_common),
3094 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3095 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3096 switch (TREE_CODE (olddecl))
3097 {
3098 case FUNCTION_DECL:
3099 case VAR_DECL:
3100 {
3101 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3102
3103 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3104 (char *) newdecl + sizeof (struct tree_decl_common),
3105 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3106 olddecl->decl_with_vis.symtab_node = snode;
3107
3108 if ((DECL_EXTERNAL (olddecl)
3109 || TREE_PUBLIC (olddecl)
3110 || TREE_STATIC (olddecl))
3111 && DECL_SECTION_NAME (newdecl) != NULL)
3112 set_decl_section_name (olddecl, newdecl);
3113
3114 /* This isn't quite correct for something like
3115 int __thread x attribute ((tls_model ("local-exec")));
3116 extern int __thread x;
3117 as we'll lose the "local-exec" model. */
3118 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3119 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3120 break;
3121 }
3122
3123 case FIELD_DECL:
3124 case PARM_DECL:
3125 case LABEL_DECL:
3126 case RESULT_DECL:
3127 case CONST_DECL:
3128 case TYPE_DECL:
3129 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3130 (char *) newdecl + sizeof (struct tree_decl_common),
3131 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3132 break;
3133
3134 default:
3135
3136 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3137 (char *) newdecl + sizeof (struct tree_decl_common),
3138 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3139 }
3140 DECL_UID (olddecl) = olddecl_uid;
3141 DECL_CONTEXT (olddecl) = olddecl_context;
3142 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3143 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3144 }
3145
3146 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3147 so that encode_section_info has a chance to look at the new decl
3148 flags and attributes. */
3149 if (DECL_RTL_SET_P (olddecl)
3150 && (TREE_CODE (olddecl) == FUNCTION_DECL
3151 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3152 make_decl_rtl (olddecl);
3153 }
3154
3155 /* Handle when a new declaration NEWDECL has the same name as an old
3156 one OLDDECL in the same binding contour. Prints an error message
3157 if appropriate.
3158
3159 If safely possible, alter OLDDECL to look like NEWDECL, and return
3160 true. Otherwise, return false. */
3161
3162 static bool
3163 duplicate_decls (tree newdecl, tree olddecl)
3164 {
3165 tree newtype = NULL, oldtype = NULL;
3166
3167 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3168 {
3169 /* Avoid `unused variable' and other warnings for OLDDECL. */
3170 suppress_warning (olddecl, OPT_Wunused);
3171 /* If the types are completely different, poison them both with
3172 error_mark_node. */
3173 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3174 && olddecl != error_mark_node
3175 && seen_error ())
3176 {
3177 if (TREE_CODE (olddecl) != FUNCTION_DECL)
3178 TREE_TYPE (olddecl) = error_mark_node;
3179 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3180 TREE_TYPE (newdecl) = error_mark_node;
3181 }
3182 return false;
3183 }
3184
3185 merge_decls (newdecl, olddecl, newtype, oldtype);
3186
3187 /* The NEWDECL will no longer be needed.
3188
3189 Before releasing the node, be sure to remove function from symbol
3190 table that might have been inserted there to record comdat group.
3191 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3192 structure is shared in between NEWDECL and OLDECL. */
3193 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3194 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3195 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3196 {
3197 struct symtab_node *snode = symtab_node::get (newdecl);
3198 if (snode)
3199 snode->remove ();
3200 }
3201 ggc_free (newdecl);
3202 return true;
3203 }
3204
3205 \f
3206 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3207 static void
3208 warn_if_shadowing (tree new_decl)
3209 {
3210 struct c_binding *b;
3211
3212 /* Shadow warnings wanted? */
3213 if (!(warn_shadow
3214 || warn_shadow_local
3215 || warn_shadow_compatible_local)
3216 /* No shadow warnings for internally generated vars. */
3217 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3218 return;
3219
3220 /* Is anything being shadowed? Invisible decls do not count. */
3221 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3222 if (b->decl && b->decl != new_decl && !b->invisible
3223 && (b->decl == error_mark_node
3224 || diagnostic_report_warnings_p (global_dc,
3225 DECL_SOURCE_LOCATION (b->decl))))
3226 {
3227 tree old_decl = b->decl;
3228
3229 if (old_decl == error_mark_node)
3230 {
3231 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3232 "non-variable", new_decl);
3233 break;
3234 }
3235
3236 bool warned = false;
3237 auto_diagnostic_group d;
3238 if (TREE_CODE (old_decl) == PARM_DECL)
3239 {
3240 enum opt_code warning_code;
3241
3242 /* If '-Wshadow=compatible-local' is specified without other
3243 -Wshadow= flags, we will warn only when the types of the
3244 shadowing variable (i.e. new_decl) and the shadowed variable
3245 (old_decl) are compatible. */
3246 if (warn_shadow)
3247 warning_code = OPT_Wshadow;
3248 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3249 warning_code = OPT_Wshadow_compatible_local;
3250 else
3251 warning_code = OPT_Wshadow_local;
3252 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3253 "declaration of %qD shadows a parameter",
3254 new_decl);
3255 }
3256 else if (DECL_FILE_SCOPE_P (old_decl))
3257 {
3258 /* Do not warn if a variable shadows a function, unless
3259 the variable is a function or a pointer-to-function. */
3260 if (TREE_CODE (old_decl) == FUNCTION_DECL
3261 && TREE_CODE (new_decl) != FUNCTION_DECL
3262 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3263 continue;
3264
3265 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3266 "declaration of %qD shadows a global "
3267 "declaration",
3268 new_decl);
3269 }
3270 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3271 && fndecl_built_in_p (old_decl))
3272 {
3273 warning (OPT_Wshadow, "declaration of %q+D shadows "
3274 "a built-in function", new_decl);
3275 break;
3276 }
3277 else
3278 {
3279 enum opt_code warning_code;
3280
3281 /* If '-Wshadow=compatible-local' is specified without other
3282 -Wshadow= flags, we will warn only when the types of the
3283 shadowing variable (i.e. new_decl) and the shadowed variable
3284 (old_decl) are compatible. */
3285 if (warn_shadow)
3286 warning_code = OPT_Wshadow;
3287 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3288 warning_code = OPT_Wshadow_compatible_local;
3289 else
3290 warning_code = OPT_Wshadow_local;
3291 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3292 "declaration of %qD shadows a previous local",
3293 new_decl);
3294 }
3295
3296 if (warned)
3297 inform (DECL_SOURCE_LOCATION (old_decl),
3298 "shadowed declaration is here");
3299
3300 break;
3301 }
3302 }
3303
3304 /* Record a decl-node X as belonging to the current lexical scope.
3305 Check for errors (such as an incompatible declaration for the same
3306 name already seen in the same scope).
3307
3308 Returns either X or an old decl for the same name.
3309 If an old decl is returned, it may have been smashed
3310 to agree with what X says. */
3311
3312 tree
3313 pushdecl (tree x)
3314 {
3315 tree name = DECL_NAME (x);
3316 struct c_scope *scope = current_scope;
3317 struct c_binding *b;
3318 bool nested = false;
3319 location_t locus = DECL_SOURCE_LOCATION (x);
3320
3321 /* Must set DECL_CONTEXT for everything not at file scope or
3322 DECL_FILE_SCOPE_P won't work. Local externs don't count
3323 unless they have initializers (which generate code). We
3324 also exclude CONST_DECLs because enumerators will get the
3325 type of the enum as context. */
3326 if (current_function_decl
3327 && TREE_CODE (x) != CONST_DECL
3328 && (!VAR_OR_FUNCTION_DECL_P (x)
3329 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3330 DECL_CONTEXT (x) = current_function_decl;
3331
3332 /* Anonymous decls are just inserted in the scope. */
3333 if (!name)
3334 {
3335 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3336 locus);
3337 return x;
3338 }
3339
3340 /* First, see if there is another declaration with the same name in
3341 the current scope. If there is, duplicate_decls may do all the
3342 work for us. If duplicate_decls returns false, that indicates
3343 two incompatible decls in the same scope; we are to silently
3344 replace the old one (duplicate_decls has issued all appropriate
3345 diagnostics). In particular, we should not consider possible
3346 duplicates in the external scope, or shadowing. */
3347 b = I_SYMBOL_BINDING (name);
3348 if (b && B_IN_SCOPE (b, scope))
3349 {
3350 struct c_binding *b_ext, *b_use;
3351 tree type = TREE_TYPE (x);
3352 tree visdecl = b->decl;
3353 tree vistype = TREE_TYPE (visdecl);
3354 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3355 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3356 b->inner_comp = false;
3357 b_use = b;
3358 b_ext = b;
3359 /* If this is an external linkage declaration, we should check
3360 for compatibility with the type in the external scope before
3361 setting the type at this scope based on the visible
3362 information only. */
3363 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3364 {
3365 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3366 b_ext = b_ext->shadowed;
3367 if (b_ext)
3368 {
3369 b_use = b_ext;
3370 if (b_use->u.type)
3371 TREE_TYPE (b_use->decl) = b_use->u.type;
3372 }
3373 }
3374 if (duplicate_decls (x, b_use->decl))
3375 {
3376 if (b_use != b)
3377 {
3378 /* Save the updated type in the external scope and
3379 restore the proper type for this scope. */
3380 tree thistype;
3381 if (comptypes (vistype, type))
3382 thistype = composite_type (vistype, type);
3383 else
3384 thistype = TREE_TYPE (b_use->decl);
3385 b_use->u.type = TREE_TYPE (b_use->decl);
3386 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3387 && fndecl_built_in_p (b_use->decl))
3388 thistype
3389 = build_type_attribute_variant (thistype,
3390 TYPE_ATTRIBUTES
3391 (b_use->u.type));
3392 TREE_TYPE (b_use->decl) = thistype;
3393 }
3394 return b_use->decl;
3395 }
3396 else
3397 goto skip_external_and_shadow_checks;
3398 }
3399
3400 /* All declarations with external linkage, and all external
3401 references, go in the external scope, no matter what scope is
3402 current. However, the binding in that scope is ignored for
3403 purposes of normal name lookup. A separate binding structure is
3404 created in the requested scope; this governs the normal
3405 visibility of the symbol.
3406
3407 The binding in the externals scope is used exclusively for
3408 detecting duplicate declarations of the same object, no matter
3409 what scope they are in; this is what we do here. (C99 6.2.7p2:
3410 All declarations that refer to the same object or function shall
3411 have compatible type; otherwise, the behavior is undefined.)
3412 However, in Objective-C, we also want to detect declarations
3413 conflicting with those of the basic types. */
3414 if ((DECL_EXTERNAL (x) || scope == file_scope)
3415 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3416 {
3417 tree type = TREE_TYPE (x);
3418 tree vistype = NULL_TREE;
3419 tree visdecl = NULL_TREE;
3420 bool type_saved = false;
3421 if (b && !B_IN_EXTERNAL_SCOPE (b)
3422 && VAR_OR_FUNCTION_DECL_P (b->decl)
3423 && DECL_FILE_SCOPE_P (b->decl))
3424 {
3425 visdecl = b->decl;
3426 vistype = TREE_TYPE (visdecl);
3427 }
3428 if (scope != file_scope
3429 && !DECL_IN_SYSTEM_HEADER (x))
3430 warning_at (locus, OPT_Wnested_externs,
3431 "nested extern declaration of %qD", x);
3432
3433 while (b && !B_IN_EXTERNAL_SCOPE (b))
3434 {
3435 /* If this decl might be modified, save its type. This is
3436 done here rather than when the decl is first bound
3437 because the type may change after first binding, through
3438 being completed or through attributes being added. If we
3439 encounter multiple such decls, only the first should have
3440 its type saved; the others will already have had their
3441 proper types saved and the types will not have changed as
3442 their scopes will not have been re-entered. */
3443 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3444 {
3445 b->u.type = TREE_TYPE (b->decl);
3446 type_saved = true;
3447 }
3448 if (B_IN_FILE_SCOPE (b)
3449 && VAR_P (b->decl)
3450 && TREE_STATIC (b->decl)
3451 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3452 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3453 && TREE_CODE (type) == ARRAY_TYPE
3454 && TYPE_DOMAIN (type)
3455 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3456 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3457 {
3458 /* Array type completed in inner scope, which should be
3459 diagnosed if the completion does not have size 1 and
3460 it does not get completed in the file scope. */
3461 b->inner_comp = true;
3462 }
3463 b = b->shadowed;
3464 }
3465
3466 /* If a matching external declaration has been found, set its
3467 type to the composite of all the types of that declaration.
3468 After the consistency checks, it will be reset to the
3469 composite of the visible types only. */
3470 if (b && b->u.type)
3471 TREE_TYPE (b->decl) = b->u.type;
3472
3473 /* the static does not go in the externals scope. */
3474 if (b && duplicate_decls (x, b->decl))
3475 {
3476 tree thistype;
3477 if (vistype)
3478 {
3479 if (comptypes (vistype, type))
3480 thistype = composite_type (vistype, type);
3481 else
3482 thistype = TREE_TYPE (b->decl);
3483 }
3484 else
3485 thistype = type;
3486 b->u.type = TREE_TYPE (b->decl);
3487 /* Propagate the type attributes to the decl. */
3488 thistype
3489 = build_type_attribute_variant (thistype,
3490 TYPE_ATTRIBUTES (b->u.type));
3491 TREE_TYPE (b->decl) = thistype;
3492 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3493 locus);
3494 return b->decl;
3495 }
3496 else if (TREE_PUBLIC (x))
3497 {
3498 if (visdecl && !b && duplicate_decls (x, visdecl))
3499 {
3500 /* An external declaration at block scope referring to a
3501 visible entity with internal linkage. The composite
3502 type will already be correct for this scope, so we
3503 just need to fall through to make the declaration in
3504 this scope. */
3505 nested = true;
3506 x = visdecl;
3507 }
3508 else
3509 {
3510 bind (name, x, external_scope, /*invisible=*/true,
3511 /*nested=*/false, locus);
3512 nested = true;
3513 }
3514 }
3515 }
3516
3517 if (TREE_CODE (x) != PARM_DECL)
3518 warn_if_shadowing (x);
3519
3520 skip_external_and_shadow_checks:
3521 if (TREE_CODE (x) == TYPE_DECL)
3522 {
3523 /* So this is a typedef, set its underlying type. */
3524 set_underlying_type (x);
3525
3526 /* If X is a typedef defined in the current function, record it
3527 for the purpose of implementing the -Wunused-local-typedefs
3528 warning. */
3529 record_locally_defined_typedef (x);
3530 }
3531
3532 bind (name, x, scope, /*invisible=*/false, nested, locus);
3533
3534 /* If x's type is incomplete because it's based on a
3535 structure or union which has not yet been fully declared,
3536 attach it to that structure or union type, so we can go
3537 back and complete the variable declaration later, if the
3538 structure or union gets fully declared.
3539
3540 If the input is erroneous, we can have error_mark in the type
3541 slot (e.g. "f(void a, ...)") - that doesn't count as an
3542 incomplete type. */
3543 if (TREE_TYPE (x) != error_mark_node
3544 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3545 {
3546 tree element = TREE_TYPE (x);
3547
3548 while (TREE_CODE (element) == ARRAY_TYPE)
3549 element = TREE_TYPE (element);
3550 element = TYPE_MAIN_VARIANT (element);
3551
3552 if ((RECORD_OR_UNION_TYPE_P (element)
3553 || TREE_CODE (element) == ENUMERAL_TYPE)
3554 && (TREE_CODE (x) != TYPE_DECL
3555 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3556 && !COMPLETE_TYPE_P (element))
3557 C_TYPE_INCOMPLETE_VARS (element)
3558 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3559 }
3560 return x;
3561 }
3562 \f
3563
3564 /* Issue a permerror about implicit function declaration. ID is the function
3565 identifier, OLDDECL is a declaration of the function in a different scope,
3566 or NULL_TREE. */
3567
3568 static void
3569 implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3570 {
3571 if (!warn_implicit_function_declaration)
3572 return;
3573
3574 bool warned;
3575 auto_diagnostic_group d;
3576 name_hint hint;
3577 if (!olddecl)
3578 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3579
3580 if (flag_isoc99)
3581 {
3582 if (const char *suggestion = hint.suggestion ())
3583 {
3584 gcc_rich_location richloc (loc);
3585 richloc.add_fixit_replace (suggestion);
3586 warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3587 "implicit declaration of function %qE;"
3588 " did you mean %qs?",
3589 id, suggestion);
3590 }
3591 else
3592 warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3593 "implicit declaration of function %qE", id);
3594 }
3595 else if (const char *suggestion = hint.suggestion ())
3596 {
3597 gcc_rich_location richloc (loc);
3598 richloc.add_fixit_replace (suggestion);
3599 warned = warning_at
3600 (&richloc, OPT_Wimplicit_function_declaration,
3601 G_("implicit declaration of function %qE; did you mean %qs?"),
3602 id, suggestion);
3603 }
3604 else
3605 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3606 G_("implicit declaration of function %qE"), id);
3607
3608 if (warned)
3609 {
3610 /* Whether the olddecl is an undeclared builtin function.
3611 locate_old_decl will not generate a diagnostic for those,
3612 so in that case we want to look elsewhere. */
3613 bool undeclared_builtin = (olddecl
3614 && TREE_CODE (olddecl) == FUNCTION_DECL
3615 && fndecl_built_in_p (olddecl)
3616 && !C_DECL_DECLARED_BUILTIN (olddecl));
3617 if (undeclared_builtin)
3618 {
3619 const char *header = header_for_builtin_fn (olddecl);
3620 if (header)
3621 {
3622 rich_location richloc (line_table, loc);
3623 maybe_add_include_fixit (&richloc, header, true);
3624 inform (&richloc,
3625 "include %qs or provide a declaration of %qE",
3626 header, id);
3627 }
3628 }
3629 else if (olddecl)
3630 locate_old_decl (olddecl);
3631 }
3632
3633 if (!warned)
3634 hint.suppress ();
3635 }
3636
3637 /* Return the name of the header file that declares built-in function
3638 FNDECL, or null if either we don't know or don't expect to see an
3639 explicit declaration. */
3640
3641 static const char *
3642 header_for_builtin_fn (tree fndecl)
3643 {
3644 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3645 return NULL;
3646
3647 switch (DECL_FUNCTION_CODE (fndecl))
3648 {
3649 CASE_FLT_FN (BUILT_IN_ACOS):
3650 CASE_FLT_FN (BUILT_IN_ACOSH):
3651 CASE_FLT_FN (BUILT_IN_ASIN):
3652 CASE_FLT_FN (BUILT_IN_ASINH):
3653 CASE_FLT_FN (BUILT_IN_ATAN):
3654 CASE_FLT_FN (BUILT_IN_ATANH):
3655 CASE_FLT_FN (BUILT_IN_ATAN2):
3656 CASE_FLT_FN (BUILT_IN_CBRT):
3657 CASE_FLT_FN (BUILT_IN_CEIL):
3658 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3659 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3660 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3661 CASE_FLT_FN (BUILT_IN_COS):
3662 CASE_FLT_FN (BUILT_IN_COSH):
3663 CASE_FLT_FN (BUILT_IN_ERF):
3664 CASE_FLT_FN (BUILT_IN_ERFC):
3665 CASE_FLT_FN (BUILT_IN_EXP):
3666 CASE_FLT_FN (BUILT_IN_EXP2):
3667 CASE_FLT_FN (BUILT_IN_EXPM1):
3668 CASE_FLT_FN (BUILT_IN_FABS):
3669 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3670 CASE_FLT_FN (BUILT_IN_FDIM):
3671 CASE_FLT_FN (BUILT_IN_FLOOR):
3672 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3673 CASE_FLT_FN (BUILT_IN_FMA):
3674 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3675 CASE_FLT_FN (BUILT_IN_FMAX):
3676 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3677 CASE_FLT_FN (BUILT_IN_FMIN):
3678 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3679 CASE_FLT_FN (BUILT_IN_FMOD):
3680 CASE_FLT_FN (BUILT_IN_FREXP):
3681 CASE_FLT_FN (BUILT_IN_HYPOT):
3682 CASE_FLT_FN (BUILT_IN_ILOGB):
3683 CASE_FLT_FN (BUILT_IN_LDEXP):
3684 CASE_FLT_FN (BUILT_IN_LGAMMA):
3685 CASE_FLT_FN (BUILT_IN_LLRINT):
3686 CASE_FLT_FN (BUILT_IN_LLROUND):
3687 CASE_FLT_FN (BUILT_IN_LOG):
3688 CASE_FLT_FN (BUILT_IN_LOG10):
3689 CASE_FLT_FN (BUILT_IN_LOG1P):
3690 CASE_FLT_FN (BUILT_IN_LOG2):
3691 CASE_FLT_FN (BUILT_IN_LOGB):
3692 CASE_FLT_FN (BUILT_IN_LRINT):
3693 CASE_FLT_FN (BUILT_IN_LROUND):
3694 CASE_FLT_FN (BUILT_IN_MODF):
3695 CASE_FLT_FN (BUILT_IN_NAN):
3696 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3697 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3698 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3699 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3700 CASE_FLT_FN (BUILT_IN_POW):
3701 CASE_FLT_FN (BUILT_IN_REMAINDER):
3702 CASE_FLT_FN (BUILT_IN_REMQUO):
3703 CASE_FLT_FN (BUILT_IN_RINT):
3704 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3705 CASE_FLT_FN (BUILT_IN_ROUND):
3706 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3707 CASE_FLT_FN (BUILT_IN_SCALBLN):
3708 CASE_FLT_FN (BUILT_IN_SCALBN):
3709 CASE_FLT_FN (BUILT_IN_SIN):
3710 CASE_FLT_FN (BUILT_IN_SINH):
3711 CASE_FLT_FN (BUILT_IN_SINCOS):
3712 CASE_FLT_FN (BUILT_IN_SQRT):
3713 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3714 CASE_FLT_FN (BUILT_IN_TAN):
3715 CASE_FLT_FN (BUILT_IN_TANH):
3716 CASE_FLT_FN (BUILT_IN_TGAMMA):
3717 CASE_FLT_FN (BUILT_IN_TRUNC):
3718 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3719 case BUILT_IN_ISINF:
3720 case BUILT_IN_ISNAN:
3721 return "<math.h>";
3722 CASE_FLT_FN (BUILT_IN_CABS):
3723 CASE_FLT_FN (BUILT_IN_CACOS):
3724 CASE_FLT_FN (BUILT_IN_CACOSH):
3725 CASE_FLT_FN (BUILT_IN_CARG):
3726 CASE_FLT_FN (BUILT_IN_CASIN):
3727 CASE_FLT_FN (BUILT_IN_CASINH):
3728 CASE_FLT_FN (BUILT_IN_CATAN):
3729 CASE_FLT_FN (BUILT_IN_CATANH):
3730 CASE_FLT_FN (BUILT_IN_CCOS):
3731 CASE_FLT_FN (BUILT_IN_CCOSH):
3732 CASE_FLT_FN (BUILT_IN_CEXP):
3733 CASE_FLT_FN (BUILT_IN_CIMAG):
3734 CASE_FLT_FN (BUILT_IN_CLOG):
3735 CASE_FLT_FN (BUILT_IN_CONJ):
3736 CASE_FLT_FN (BUILT_IN_CPOW):
3737 CASE_FLT_FN (BUILT_IN_CPROJ):
3738 CASE_FLT_FN (BUILT_IN_CREAL):
3739 CASE_FLT_FN (BUILT_IN_CSIN):
3740 CASE_FLT_FN (BUILT_IN_CSINH):
3741 CASE_FLT_FN (BUILT_IN_CSQRT):
3742 CASE_FLT_FN (BUILT_IN_CTAN):
3743 CASE_FLT_FN (BUILT_IN_CTANH):
3744 return "<complex.h>";
3745 case BUILT_IN_MEMCHR:
3746 case BUILT_IN_MEMCMP:
3747 case BUILT_IN_MEMCPY:
3748 case BUILT_IN_MEMMOVE:
3749 case BUILT_IN_MEMSET:
3750 case BUILT_IN_STRCAT:
3751 case BUILT_IN_STRCHR:
3752 case BUILT_IN_STRCMP:
3753 case BUILT_IN_STRCPY:
3754 case BUILT_IN_STRCSPN:
3755 case BUILT_IN_STRLEN:
3756 case BUILT_IN_STRNCAT:
3757 case BUILT_IN_STRNCMP:
3758 case BUILT_IN_STRNCPY:
3759 case BUILT_IN_STRPBRK:
3760 case BUILT_IN_STRRCHR:
3761 case BUILT_IN_STRSPN:
3762 case BUILT_IN_STRSTR:
3763 return "<string.h>";
3764 case BUILT_IN_FPRINTF:
3765 case BUILT_IN_PUTC:
3766 case BUILT_IN_FPUTC:
3767 case BUILT_IN_FPUTS:
3768 case BUILT_IN_FSCANF:
3769 case BUILT_IN_FWRITE:
3770 case BUILT_IN_PRINTF:
3771 case BUILT_IN_PUTCHAR:
3772 case BUILT_IN_PUTS:
3773 case BUILT_IN_SCANF:
3774 case BUILT_IN_SNPRINTF:
3775 case BUILT_IN_SPRINTF:
3776 case BUILT_IN_SSCANF:
3777 case BUILT_IN_VFPRINTF:
3778 case BUILT_IN_VFSCANF:
3779 case BUILT_IN_VPRINTF:
3780 case BUILT_IN_VSCANF:
3781 case BUILT_IN_VSNPRINTF:
3782 case BUILT_IN_VSPRINTF:
3783 case BUILT_IN_VSSCANF:
3784 return "<stdio.h>";
3785 case BUILT_IN_ISALNUM:
3786 case BUILT_IN_ISALPHA:
3787 case BUILT_IN_ISBLANK:
3788 case BUILT_IN_ISCNTRL:
3789 case BUILT_IN_ISDIGIT:
3790 case BUILT_IN_ISGRAPH:
3791 case BUILT_IN_ISLOWER:
3792 case BUILT_IN_ISPRINT:
3793 case BUILT_IN_ISPUNCT:
3794 case BUILT_IN_ISSPACE:
3795 case BUILT_IN_ISUPPER:
3796 case BUILT_IN_ISXDIGIT:
3797 case BUILT_IN_TOLOWER:
3798 case BUILT_IN_TOUPPER:
3799 return "<ctype.h>";
3800 case BUILT_IN_ISWALNUM:
3801 case BUILT_IN_ISWALPHA:
3802 case BUILT_IN_ISWBLANK:
3803 case BUILT_IN_ISWCNTRL:
3804 case BUILT_IN_ISWDIGIT:
3805 case BUILT_IN_ISWGRAPH:
3806 case BUILT_IN_ISWLOWER:
3807 case BUILT_IN_ISWPRINT:
3808 case BUILT_IN_ISWPUNCT:
3809 case BUILT_IN_ISWSPACE:
3810 case BUILT_IN_ISWUPPER:
3811 case BUILT_IN_ISWXDIGIT:
3812 case BUILT_IN_TOWLOWER:
3813 case BUILT_IN_TOWUPPER:
3814 return "<wctype.h>";
3815 case BUILT_IN_ABORT:
3816 case BUILT_IN_ABS:
3817 case BUILT_IN_CALLOC:
3818 case BUILT_IN_EXIT:
3819 case BUILT_IN_FREE:
3820 case BUILT_IN_LABS:
3821 case BUILT_IN_LLABS:
3822 case BUILT_IN_MALLOC:
3823 case BUILT_IN_REALLOC:
3824 case BUILT_IN__EXIT2:
3825 case BUILT_IN_ALIGNED_ALLOC:
3826 return "<stdlib.h>";
3827 case BUILT_IN_IMAXABS:
3828 return "<inttypes.h>";
3829 case BUILT_IN_STRFTIME:
3830 return "<time.h>";
3831 default:
3832 return NULL;
3833 }
3834 }
3835
3836 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3837 function of type int (). */
3838
3839 tree
3840 implicitly_declare (location_t loc, tree functionid)
3841 {
3842 struct c_binding *b;
3843 tree decl = NULL_TREE;
3844 tree asmspec_tree;
3845
3846 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3847 {
3848 if (B_IN_SCOPE (b, external_scope))
3849 {
3850 decl = b->decl;
3851 break;
3852 }
3853 }
3854
3855 if (decl)
3856 {
3857 if (TREE_CODE (decl) != FUNCTION_DECL)
3858 return decl;
3859
3860 /* FIXME: Objective-C has weird not-really-builtin functions
3861 which are supposed to be visible automatically. They wind up
3862 in the external scope because they're pushed before the file
3863 scope gets created. Catch this here and rebind them into the
3864 file scope. */
3865 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3866 {
3867 bind (functionid, decl, file_scope,
3868 /*invisible=*/false, /*nested=*/true,
3869 DECL_SOURCE_LOCATION (decl));
3870 return decl;
3871 }
3872 else
3873 {
3874 tree newtype = default_function_type;
3875 if (b->u.type)
3876 TREE_TYPE (decl) = b->u.type;
3877 /* Implicit declaration of a function already declared
3878 (somehow) in a different scope, or as a built-in.
3879 If this is the first time this has happened, warn;
3880 then recycle the old declaration but with the new type. */
3881 if (!C_DECL_IMPLICIT (decl))
3882 {
3883 implicit_decl_permerror (loc, functionid, decl);
3884 C_DECL_IMPLICIT (decl) = 1;
3885 }
3886 if (fndecl_built_in_p (decl))
3887 {
3888 newtype = build_type_attribute_variant (newtype,
3889 TYPE_ATTRIBUTES
3890 (TREE_TYPE (decl)));
3891 if (!comptypes (newtype, TREE_TYPE (decl)))
3892 {
3893 auto_diagnostic_group d;
3894 bool warned = warning_at (loc,
3895 OPT_Wbuiltin_declaration_mismatch,
3896 "incompatible implicit "
3897 "declaration of built-in "
3898 "function %qD", decl);
3899 /* See if we can hint which header to include. */
3900 const char *header = header_for_builtin_fn (decl);
3901 if (header != NULL && warned)
3902 {
3903 rich_location richloc (line_table, loc);
3904 maybe_add_include_fixit (&richloc, header, true);
3905 inform (&richloc,
3906 "include %qs or provide a declaration of %qD",
3907 header, decl);
3908 }
3909 newtype = TREE_TYPE (decl);
3910 }
3911 }
3912 else
3913 {
3914 if (!comptypes (newtype, TREE_TYPE (decl)))
3915 {
3916 auto_diagnostic_group d;
3917 error_at (loc, "incompatible implicit declaration of "
3918 "function %qD", decl);
3919 locate_old_decl (decl);
3920 }
3921 }
3922 b->u.type = TREE_TYPE (decl);
3923 TREE_TYPE (decl) = newtype;
3924 bind (functionid, decl, current_scope,
3925 /*invisible=*/false, /*nested=*/true,
3926 DECL_SOURCE_LOCATION (decl));
3927 return decl;
3928 }
3929 }
3930
3931 /* Not seen before. */
3932 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3933 DECL_EXTERNAL (decl) = 1;
3934 TREE_PUBLIC (decl) = 1;
3935 C_DECL_IMPLICIT (decl) = 1;
3936 implicit_decl_permerror (loc, functionid, 0);
3937 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3938 if (asmspec_tree)
3939 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3940
3941 /* C89 says implicit declarations are in the innermost block.
3942 So we record the decl in the standard fashion. */
3943 decl = pushdecl (decl);
3944
3945 /* No need to call objc_check_decl here - it's a function type. */
3946 rest_of_decl_compilation (decl, 0, 0);
3947
3948 /* Write a record describing this implicit function declaration
3949 to the prototypes file (if requested). */
3950 gen_aux_info_record (decl, 0, 1, 0);
3951
3952 /* Possibly apply some default attributes to this implicit declaration. */
3953 decl_attributes (&decl, NULL_TREE, 0);
3954
3955 return decl;
3956 }
3957
3958 /* Issue an error message for a reference to an undeclared variable
3959 ID, including a reference to a builtin outside of function-call
3960 context. Establish a binding of the identifier to error_mark_node
3961 in an appropriate scope, which will suppress further errors for the
3962 same identifier. The error message should be given location LOC. */
3963 void
3964 undeclared_variable (location_t loc, tree id)
3965 {
3966 static bool already = false;
3967 struct c_scope *scope;
3968
3969 auto_diagnostic_group d;
3970 if (current_function_decl == NULL_TREE)
3971 {
3972 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3973 if (const char *suggestion = guessed_id.suggestion ())
3974 {
3975 gcc_rich_location richloc (loc);
3976 richloc.add_fixit_replace (suggestion);
3977 error_at (&richloc,
3978 "%qE undeclared here (not in a function);"
3979 " did you mean %qs?",
3980 id, suggestion);
3981 }
3982 else
3983 error_at (loc, "%qE undeclared here (not in a function)", id);
3984 scope = current_scope;
3985 }
3986 else
3987 {
3988 if (!objc_diagnose_private_ivar (id))
3989 {
3990 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3991 if (const char *suggestion = guessed_id.suggestion ())
3992 {
3993 gcc_rich_location richloc (loc);
3994 richloc.add_fixit_replace (suggestion);
3995 error_at (&richloc,
3996 "%qE undeclared (first use in this function);"
3997 " did you mean %qs?",
3998 id, suggestion);
3999 }
4000 else
4001 error_at (loc, "%qE undeclared (first use in this function)", id);
4002 }
4003 if (!already)
4004 {
4005 inform (loc, "each undeclared identifier is reported only"
4006 " once for each function it appears in");
4007 already = true;
4008 }
4009
4010 /* If we are parsing old-style parameter decls, current_function_decl
4011 will be nonnull but current_function_scope will be null. */
4012 scope = current_function_scope ? current_function_scope : current_scope;
4013 }
4014 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4015 UNKNOWN_LOCATION);
4016 }
4017 \f
4018 /* Subroutine of lookup_label, declare_label, define_label: construct a
4019 LABEL_DECL with all the proper frills. Also create a struct
4020 c_label_vars initialized for the current scope. */
4021
4022 static tree
4023 make_label (location_t location, tree name, bool defining,
4024 struct c_label_vars **p_label_vars)
4025 {
4026 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4027 DECL_CONTEXT (label) = current_function_decl;
4028 SET_DECL_MODE (label, VOIDmode);
4029
4030 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4031 label_vars->shadowed = NULL;
4032 set_spot_bindings (&label_vars->label_bindings, defining);
4033 label_vars->decls_in_scope = make_tree_vector ();
4034 label_vars->gotos = NULL;
4035 *p_label_vars = label_vars;
4036
4037 return label;
4038 }
4039
4040 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4041 Create one if none exists so far for the current function.
4042 This is called when a label is used in a goto expression or
4043 has its address taken. */
4044
4045 tree
4046 lookup_label (tree name)
4047 {
4048 tree label;
4049 struct c_label_vars *label_vars;
4050
4051 if (current_function_scope == 0)
4052 {
4053 error ("label %qE referenced outside of any function", name);
4054 return NULL_TREE;
4055 }
4056
4057 /* Use a label already defined or ref'd with this name, but not if
4058 it is inherited from a containing function and wasn't declared
4059 using __label__. */
4060 label = I_LABEL_DECL (name);
4061 if (label && (DECL_CONTEXT (label) == current_function_decl
4062 || C_DECLARED_LABEL_FLAG (label)))
4063 {
4064 /* If the label has only been declared, update its apparent
4065 location to point here, for better diagnostics if it
4066 turns out not to have been defined. */
4067 if (DECL_INITIAL (label) == NULL_TREE)
4068 DECL_SOURCE_LOCATION (label) = input_location;
4069 return label;
4070 }
4071
4072 /* No label binding for that identifier; make one. */
4073 label = make_label (input_location, name, false, &label_vars);
4074
4075 /* Ordinary labels go in the current function scope. */
4076 bind_label (name, label, current_function_scope, label_vars);
4077
4078 return label;
4079 }
4080
4081 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4082 to LABEL. */
4083
4084 static void
4085 warn_about_goto (location_t goto_loc, tree label, tree decl)
4086 {
4087 auto_diagnostic_group d;
4088 if (c_type_variably_modified_p (TREE_TYPE (decl)))
4089 error_at (goto_loc,
4090 "jump into scope of identifier with variably modified type");
4091 else if (flag_openmp
4092 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4093 error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4094 else
4095 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4096 "jump skips variable initialization"))
4097 return;
4098 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4099 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4100 }
4101
4102 /* Look up a label because of a goto statement. This is like
4103 lookup_label, but also issues any appropriate warnings. */
4104
4105 tree
4106 lookup_label_for_goto (location_t loc, tree name)
4107 {
4108 tree label;
4109 struct c_label_vars *label_vars;
4110 unsigned int ix;
4111 tree decl;
4112
4113 label = lookup_label (name);
4114 if (label == NULL_TREE)
4115 return NULL_TREE;
4116
4117 /* If we are jumping to a different function, we can't issue any
4118 useful warnings. */
4119 if (DECL_CONTEXT (label) != current_function_decl)
4120 {
4121 gcc_assert (C_DECLARED_LABEL_FLAG (label));
4122 return label;
4123 }
4124
4125 label_vars = I_LABEL_BINDING (name)->u.label;
4126
4127 /* If the label has not yet been defined, then push this goto on a
4128 list for possible later warnings. */
4129 if (label_vars->label_bindings.scope == NULL)
4130 {
4131 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4132
4133 g->loc = loc;
4134 set_spot_bindings (&g->goto_bindings, true);
4135 vec_safe_push (label_vars->gotos, g);
4136 return label;
4137 }
4138
4139 /* If there are any decls in label_vars->decls_in_scope, then this
4140 goto has missed the declaration of the decl. This happens for a
4141 case like
4142 int i = 1;
4143 lab:
4144 ...
4145 goto lab;
4146 Issue a warning or error. */
4147 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4148 warn_about_goto (loc, label, decl);
4149
4150 if (label_vars->label_bindings.left_stmt_expr)
4151 {
4152 auto_diagnostic_group d;
4153 error_at (loc, "jump into statement expression");
4154 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4155 }
4156
4157 return label;
4158 }
4159
4160 /* Make a label named NAME in the current function, shadowing silently
4161 any that may be inherited from containing functions or containing
4162 scopes. This is called for __label__ declarations. */
4163
4164 tree
4165 declare_label (tree name)
4166 {
4167 struct c_binding *b = I_LABEL_BINDING (name);
4168 tree label;
4169 struct c_label_vars *label_vars;
4170
4171 /* Check to make sure that the label hasn't already been declared
4172 at this scope */
4173 if (b && B_IN_CURRENT_SCOPE (b))
4174 {
4175 auto_diagnostic_group d;
4176 error ("duplicate label declaration %qE", name);
4177 locate_old_decl (b->decl);
4178
4179 /* Just use the previous declaration. */
4180 return b->decl;
4181 }
4182
4183 label = make_label (input_location, name, false, &label_vars);
4184 C_DECLARED_LABEL_FLAG (label) = 1;
4185
4186 /* Declared labels go in the current scope. */
4187 bind_label (name, label, current_scope, label_vars);
4188
4189 return label;
4190 }
4191
4192 /* When we define a label, issue any appropriate warnings if there are
4193 any gotos earlier in the function which jump to this label. */
4194
4195 static void
4196 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4197 {
4198 unsigned int ix;
4199 struct c_goto_bindings *g;
4200
4201 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4202 {
4203 struct c_binding *b;
4204 struct c_scope *scope;
4205
4206 /* We have a goto to this label. The goto is going forward. In
4207 g->scope, the goto is going to skip any binding which was
4208 defined after g->bindings_in_scope. */
4209 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4210 {
4211 for (b = g->goto_bindings.scope->bindings;
4212 b != g->goto_bindings.bindings_in_scope;
4213 b = b->prev)
4214 {
4215 if (decl_jump_unsafe (b->decl))
4216 warn_about_goto (g->loc, label, b->decl);
4217 }
4218 }
4219
4220 /* We also need to warn about decls defined in any scopes
4221 between the scope of the label and the scope of the goto. */
4222 for (scope = label_vars->label_bindings.scope;
4223 scope != g->goto_bindings.scope;
4224 scope = scope->outer)
4225 {
4226 gcc_assert (scope != NULL);
4227 if (scope->has_jump_unsafe_decl)
4228 {
4229 if (scope == label_vars->label_bindings.scope)
4230 b = label_vars->label_bindings.bindings_in_scope;
4231 else
4232 b = scope->bindings;
4233 for (; b != NULL; b = b->prev)
4234 {
4235 if (decl_jump_unsafe (b->decl))
4236 warn_about_goto (g->loc, label, b->decl);
4237 }
4238 }
4239 }
4240
4241 if (g->goto_bindings.stmt_exprs > 0)
4242 {
4243 auto_diagnostic_group d;
4244 error_at (g->loc, "jump into statement expression");
4245 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4246 label);
4247 }
4248 }
4249
4250 /* Now that the label is defined, we will issue warnings about
4251 subsequent gotos to this label when we see them. */
4252 vec_safe_truncate (label_vars->gotos, 0);
4253 label_vars->gotos = NULL;
4254 }
4255
4256 /* Define a label, specifying the location in the source file.
4257 Return the LABEL_DECL node for the label, if the definition is valid.
4258 Otherwise return NULL_TREE. */
4259
4260 tree
4261 define_label (location_t location, tree name)
4262 {
4263 /* Find any preexisting label with this name. It is an error
4264 if that label has already been defined in this function, or
4265 if there is a containing function with a declared label with
4266 the same name. */
4267 tree label = I_LABEL_DECL (name);
4268
4269 if (label
4270 && ((DECL_CONTEXT (label) == current_function_decl
4271 && DECL_INITIAL (label) != NULL_TREE)
4272 || (DECL_CONTEXT (label) != current_function_decl
4273 && C_DECLARED_LABEL_FLAG (label))))
4274 {
4275 auto_diagnostic_group d;
4276 error_at (location, "duplicate label %qD", label);
4277 locate_old_decl (label);
4278 return NULL_TREE;
4279 }
4280 else if (label && DECL_CONTEXT (label) == current_function_decl)
4281 {
4282 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4283
4284 /* The label has been used or declared already in this function,
4285 but not defined. Update its location to point to this
4286 definition. */
4287 DECL_SOURCE_LOCATION (label) = location;
4288 set_spot_bindings (&label_vars->label_bindings, true);
4289
4290 /* Issue warnings as required about any goto statements from
4291 earlier in the function. */
4292 check_earlier_gotos (label, label_vars);
4293 }
4294 else
4295 {
4296 struct c_label_vars *label_vars;
4297
4298 /* No label binding for that identifier; make one. */
4299 label = make_label (location, name, true, &label_vars);
4300
4301 /* Ordinary labels go in the current function scope. */
4302 bind_label (name, label, current_function_scope, label_vars);
4303 }
4304
4305 if (!in_system_header_at (input_location) && lookup_name (name))
4306 warning_at (location, OPT_Wtraditional,
4307 "traditional C lacks a separate namespace "
4308 "for labels, identifier %qE conflicts", name);
4309
4310 /* Mark label as having been defined. */
4311 DECL_INITIAL (label) = error_mark_node;
4312 return label;
4313 }
4314 \f
4315 /* Get the bindings for a new switch statement. This is used to issue
4316 warnings as appropriate for jumps from the switch to case or
4317 default labels. */
4318
4319 struct c_spot_bindings *
4320 c_get_switch_bindings (void)
4321 {
4322 struct c_spot_bindings *switch_bindings;
4323
4324 switch_bindings = XNEW (struct c_spot_bindings);
4325 set_spot_bindings (switch_bindings, true);
4326 return switch_bindings;
4327 }
4328
4329 void
4330 c_release_switch_bindings (struct c_spot_bindings *bindings)
4331 {
4332 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4333 XDELETE (bindings);
4334 }
4335
4336 /* This is called at the point of a case or default label to issue
4337 warnings about decls as needed. It returns true if it found an
4338 error, not just a warning. */
4339
4340 bool
4341 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4342 location_t switch_loc, location_t case_loc)
4343 {
4344 bool saw_error;
4345 struct c_scope *scope;
4346
4347 saw_error = false;
4348 for (scope = current_scope;
4349 scope != switch_bindings->scope;
4350 scope = scope->outer)
4351 {
4352 struct c_binding *b;
4353
4354 gcc_assert (scope != NULL);
4355
4356 if (!scope->has_jump_unsafe_decl)
4357 continue;
4358
4359 for (b = scope->bindings; b != NULL; b = b->prev)
4360 {
4361 if (decl_jump_unsafe (b->decl))
4362 {
4363 auto_diagnostic_group d;
4364 bool emitted;
4365 if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4366 {
4367 saw_error = true;
4368 error_at (case_loc,
4369 ("switch jumps into scope of identifier with "
4370 "variably modified type"));
4371 emitted = true;
4372 }
4373 else if (flag_openmp
4374 && lookup_attribute ("omp allocate",
4375 DECL_ATTRIBUTES (b->decl)))
4376 {
4377 saw_error = true;
4378 error_at (case_loc,
4379 "switch jumps over OpenMP %<allocate%> allocation");
4380 emitted = true;
4381 }
4382 else
4383 emitted
4384 = warning_at (case_loc, OPT_Wjump_misses_init,
4385 "switch jumps over variable initialization");
4386 if (emitted)
4387 {
4388 inform (switch_loc, "switch starts here");
4389 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4390 b->decl);
4391 }
4392 }
4393 }
4394 }
4395
4396 if (switch_bindings->stmt_exprs > 0)
4397 {
4398 saw_error = true;
4399 auto_diagnostic_group d;
4400 error_at (case_loc, "switch jumps into statement expression");
4401 inform (switch_loc, "switch starts here");
4402 }
4403
4404 return saw_error;
4405 }
4406 \f
4407 /* Given NAME, an IDENTIFIER_NODE,
4408 return the structure (or union or enum) definition for that name.
4409 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4410 CODE says which kind of type the caller wants;
4411 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4412 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4413 location where the tag was defined.
4414 If the wrong kind of type is found, an error is reported. */
4415
4416 static tree
4417 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4418 location_t *ploc)
4419 {
4420 struct c_binding *b = I_TAG_BINDING (name);
4421 bool thislevel = false;
4422
4423 if (!b || !b->decl)
4424 return NULL_TREE;
4425
4426 /* We only care about whether it's in this level if
4427 thislevel_only was set or it might be a type clash. */
4428 if (thislevel_only || TREE_CODE (b->decl) != code)
4429 {
4430 /* For our purposes, a tag in the external scope is the same as
4431 a tag in the file scope. (Primarily relevant to Objective-C
4432 and its builtin structure tags, which get pushed before the
4433 file scope is created.) */
4434 if (B_IN_CURRENT_SCOPE (b)
4435 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4436 thislevel = true;
4437 }
4438
4439 if (thislevel_only && !thislevel)
4440 return NULL_TREE;
4441
4442 if (TREE_CODE (b->decl) != code)
4443 {
4444 /* Definition isn't the kind we were looking for. */
4445 pending_invalid_xref = name;
4446 pending_invalid_xref_location = input_location;
4447
4448 /* If in the same binding level as a declaration as a tag
4449 of a different type, this must not be allowed to
4450 shadow that tag, so give the error immediately.
4451 (For example, "struct foo; union foo;" is invalid.) */
4452 if (thislevel)
4453 pending_xref_error ();
4454 }
4455
4456 if (ploc != NULL)
4457 *ploc = b->locus;
4458
4459 return b->decl;
4460 }
4461
4462 /* Return true if a definition exists for NAME with code CODE. */
4463
4464 bool
4465 tag_exists_p (enum tree_code code, tree name)
4466 {
4467 struct c_binding *b = I_TAG_BINDING (name);
4468
4469 if (b == NULL || b->decl == NULL_TREE)
4470 return false;
4471 return TREE_CODE (b->decl) == code;
4472 }
4473
4474 /* Print an error message now
4475 for a recent invalid struct, union or enum cross reference.
4476 We don't print them immediately because they are not invalid
4477 when used in the `struct foo;' construct for shadowing. */
4478
4479 void
4480 pending_xref_error (void)
4481 {
4482 if (pending_invalid_xref != NULL_TREE)
4483 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4484 pending_invalid_xref);
4485 pending_invalid_xref = NULL_TREE;
4486 }
4487
4488 \f
4489 /* Look up NAME in the current scope and its superiors
4490 in the namespace of variables, functions and typedefs.
4491 Return a ..._DECL node of some kind representing its definition,
4492 or return NULL_TREE if it is undefined. */
4493
4494 tree
4495 lookup_name (tree name)
4496 {
4497 struct c_binding *b = I_SYMBOL_BINDING (name);
4498 if (b && !b->invisible)
4499 {
4500 maybe_record_typedef_use (b->decl);
4501 return b->decl;
4502 }
4503 return NULL_TREE;
4504 }
4505
4506 /* Similar to `lookup_name' but look only at the indicated scope. */
4507
4508 static tree
4509 lookup_name_in_scope (tree name, struct c_scope *scope)
4510 {
4511 struct c_binding *b;
4512
4513 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4514 if (B_IN_SCOPE (b, scope))
4515 return b->decl;
4516 return NULL_TREE;
4517 }
4518
4519 /* Look for the closest match for NAME within the currently valid
4520 scopes.
4521
4522 This finds the identifier with the lowest Levenshtein distance to
4523 NAME. If there are multiple candidates with equal minimal distance,
4524 the first one found is returned. Scopes are searched from innermost
4525 outwards, and within a scope in reverse order of declaration, thus
4526 benefiting candidates "near" to the current scope.
4527
4528 The function also looks for similar macro names to NAME, since a
4529 misspelled macro name will not be expanded, and hence looks like an
4530 identifier to the C frontend.
4531
4532 It also looks for start_typename keywords, to detect "singed" vs "signed"
4533 typos.
4534
4535 Use LOC for any deferred diagnostics. */
4536
4537 name_hint
4538 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4539 {
4540 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4541
4542 /* First, try some well-known names in the C standard library, in case
4543 the user forgot a #include. */
4544 const char *header_hint
4545 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4546
4547 if (header_hint)
4548 return name_hint (NULL,
4549 new suggest_missing_header (loc,
4550 IDENTIFIER_POINTER (name),
4551 header_hint));
4552
4553 /* Only suggest names reserved for the implementation if NAME begins
4554 with an underscore. */
4555 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4556
4557 best_match<tree, tree> bm (name);
4558
4559 /* Look within currently valid scopes. */
4560 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4561 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4562 {
4563 if (!binding->id || binding->invisible)
4564 continue;
4565 if (binding->decl == error_mark_node)
4566 continue;
4567 /* Don't use bindings from implicitly declared functions,
4568 as they were likely misspellings themselves. */
4569 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4570 if (C_DECL_IMPLICIT (binding->decl))
4571 continue;
4572 /* Don't suggest names that are reserved for use by the
4573 implementation, unless NAME began with an underscore. */
4574 if (!consider_implementation_names)
4575 {
4576 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4577 if (name_reserved_for_implementation_p (suggestion_str))
4578 continue;
4579 }
4580 switch (kind)
4581 {
4582 case FUZZY_LOOKUP_TYPENAME:
4583 if (TREE_CODE (binding->decl) != TYPE_DECL)
4584 continue;
4585 break;
4586
4587 case FUZZY_LOOKUP_FUNCTION_NAME:
4588 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4589 {
4590 /* Allow function pointers. */
4591 if ((VAR_P (binding->decl)
4592 || TREE_CODE (binding->decl) == PARM_DECL)
4593 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4594 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4595 == FUNCTION_TYPE))
4596 break;
4597 continue;
4598 }
4599 break;
4600
4601 default:
4602 break;
4603 }
4604 bm.consider (binding->id);
4605 }
4606
4607 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4608 as:
4609 x = SOME_OTHER_MACRO (y);
4610 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4611 as a misspelled identifier.
4612
4613 Use the best distance so far so that a candidate is only set if
4614 a macro is better than anything so far. This allows early rejection
4615 (without calculating the edit distance) of macro names that must have
4616 distance >= bm.get_best_distance (), and means that we only get a
4617 non-NULL result for best_macro_match if it's better than any of
4618 the identifiers already checked, which avoids needless creation
4619 of identifiers for macro hashnodes. */
4620 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4621 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4622 /* If a macro is the closest so far to NAME, use it, creating an
4623 identifier tree node for it. */
4624 if (best_macro)
4625 {
4626 const char *id = (const char *)best_macro->ident.str;
4627 tree macro_as_identifier
4628 = get_identifier_with_length (id, best_macro->ident.len);
4629 bm.set_best_so_far (macro_as_identifier,
4630 bmm.get_best_distance (),
4631 bmm.get_best_candidate_length ());
4632 }
4633
4634 /* Try the "start_typename" keywords to detect
4635 "singed" vs "signed" typos. */
4636 if (kind == FUZZY_LOOKUP_TYPENAME)
4637 {
4638 for (unsigned i = 0; i < num_c_common_reswords; i++)
4639 {
4640 const c_common_resword *resword = &c_common_reswords[i];
4641 if (!c_keyword_starts_typename (resword->rid))
4642 continue;
4643 tree resword_identifier = ridpointers [resword->rid];
4644 if (!resword_identifier)
4645 continue;
4646 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4647 bm.consider (resword_identifier);
4648 }
4649 }
4650
4651 tree best = bm.get_best_meaningful_candidate ();
4652 if (best)
4653 return name_hint (IDENTIFIER_POINTER (best), NULL);
4654 else
4655 return name_hint (NULL, NULL);
4656 }
4657
4658 \f
4659 /* Handle the standard [[nodiscard]] attribute. */
4660
4661 static tree
4662 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4663 int /*flags*/, bool *no_add_attrs)
4664 {
4665 if (TREE_CODE (*node) == FUNCTION_DECL)
4666 {
4667 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4668 warning_at (DECL_SOURCE_LOCATION (*node),
4669 OPT_Wattributes, "%qE attribute applied to %qD with void "
4670 "return type", name, *node);
4671 }
4672 else if (RECORD_OR_UNION_TYPE_P (*node)
4673 || TREE_CODE (*node) == ENUMERAL_TYPE)
4674 /* OK */;
4675 else
4676 {
4677 pedwarn (input_location,
4678 OPT_Wattributes, "%qE attribute can only be applied to "
4679 "functions or to structure, union or enumeration types", name);
4680 *no_add_attrs = true;
4681 }
4682 return NULL_TREE;
4683 }
4684
4685 /* Handle the standard [[noreturn]] attribute. */
4686
4687 static tree
4688 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4689 int flags, bool *no_add_attrs)
4690 {
4691 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4692 only applies to functions, not function pointers. */
4693 if (TREE_CODE (*node) == FUNCTION_DECL)
4694 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4695 else
4696 {
4697 pedwarn (input_location, OPT_Wattributes,
4698 "standard %qE attribute can only be applied to functions",
4699 name);
4700 *no_add_attrs = true;
4701 return NULL_TREE;
4702 }
4703 }
4704
4705 /* Table of supported standard (C23) attributes. */
4706 static const attribute_spec std_attributes[] =
4707 {
4708 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4709 affects_type_identity, handler, exclude } */
4710 { "_Noreturn", 0, 0, false, false, false, false,
4711 handle_std_noreturn_attribute, NULL },
4712 { "deprecated", 0, 1, false, false, false, false,
4713 handle_deprecated_attribute, NULL },
4714 { "fallthrough", 0, 0, false, false, false, false,
4715 handle_fallthrough_attribute, NULL },
4716 { "maybe_unused", 0, 0, false, false, false, false,
4717 handle_unused_attribute, NULL },
4718 { "nodiscard", 0, 1, false, false, false, false,
4719 handle_nodiscard_attribute, NULL },
4720 { "noreturn", 0, 0, false, false, false, false,
4721 handle_std_noreturn_attribute, NULL }
4722 };
4723
4724 const scoped_attribute_specs std_attribute_table =
4725 {
4726 nullptr, { std_attributes }
4727 };
4728
4729 /* Create the predefined scalar types of C,
4730 and some nodes representing standard constants (0, 1, (void *) 0).
4731 Initialize the global scope.
4732 Make definitions for built-in primitive functions. */
4733
4734 void
4735 c_init_decl_processing (void)
4736 {
4737 location_t save_loc = input_location;
4738
4739 /* Initialize reserved words for parser. */
4740 c_parse_init ();
4741
4742 current_function_decl = NULL_TREE;
4743
4744 gcc_obstack_init (&parser_obstack);
4745
4746 /* Make the externals scope. */
4747 push_scope ();
4748 external_scope = current_scope;
4749
4750 /* Declarations from c_common_nodes_and_builtins must not be associated
4751 with this input file, lest we get differences between using and not
4752 using preprocessed headers. */
4753 input_location = BUILTINS_LOCATION;
4754
4755 c_common_nodes_and_builtins ();
4756
4757 /* In C, comparisons and TRUTH_* expressions have type int. */
4758 truthvalue_type_node = integer_type_node;
4759 truthvalue_true_node = integer_one_node;
4760 truthvalue_false_node = integer_zero_node;
4761
4762 /* Even in C99, which has a real boolean type. */
4763 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4764 boolean_type_node));
4765
4766 /* C-specific nullptr initialization. */
4767 record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4768 /* The size and alignment of nullptr_t is the same as for a pointer to
4769 character type. */
4770 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4771
4772 input_location = save_loc;
4773
4774 make_fname_decl = c_make_fname_decl;
4775 start_fname_decls ();
4776 }
4777
4778 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4779 give the decl, NAME is the initialization string and TYPE_DEP
4780 indicates whether NAME depended on the type of the function. As we
4781 don't yet implement delayed emission of static data, we mark the
4782 decl as emitted so it is not placed in the output. Anything using
4783 it must therefore pull out the STRING_CST initializer directly.
4784 FIXME. */
4785
4786 static tree
4787 c_make_fname_decl (location_t loc, tree id, int type_dep)
4788 {
4789 const char *name = fname_as_string (type_dep);
4790 tree decl, type, init;
4791 size_t length = strlen (name);
4792
4793 type = build_array_type (char_type_node,
4794 build_index_type (size_int (length)));
4795 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4796
4797 decl = build_decl (loc, VAR_DECL, id, type);
4798
4799 TREE_STATIC (decl) = 1;
4800 TREE_READONLY (decl) = 1;
4801 DECL_ARTIFICIAL (decl) = 1;
4802
4803 init = build_string (length + 1, name);
4804 free (CONST_CAST (char *, name));
4805 TREE_TYPE (init) = type;
4806 DECL_INITIAL (decl) = init;
4807
4808 TREE_USED (decl) = 1;
4809
4810 if (current_function_decl
4811 /* For invalid programs like this:
4812
4813 void foo()
4814 const char* p = __FUNCTION__;
4815
4816 the __FUNCTION__ is believed to appear in K&R style function
4817 parameter declarator. In that case we still don't have
4818 function_scope. */
4819 && current_function_scope)
4820 {
4821 DECL_CONTEXT (decl) = current_function_decl;
4822 bind (id, decl, current_function_scope,
4823 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4824 }
4825
4826 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4827
4828 return decl;
4829 }
4830
4831 tree
4832 c_builtin_function (tree decl)
4833 {
4834 tree type = TREE_TYPE (decl);
4835 tree id = DECL_NAME (decl);
4836
4837 const char *name = IDENTIFIER_POINTER (id);
4838 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4839
4840 /* Should never be called on a symbol with a preexisting meaning. */
4841 gcc_assert (!I_SYMBOL_BINDING (id));
4842
4843 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4844 UNKNOWN_LOCATION);
4845
4846 /* Builtins in the implementation namespace are made visible without
4847 needing to be explicitly declared. See push_file_scope. */
4848 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4849 {
4850 DECL_CHAIN (decl) = visible_builtins;
4851 visible_builtins = decl;
4852 }
4853
4854 return decl;
4855 }
4856
4857 tree
4858 c_builtin_function_ext_scope (tree decl)
4859 {
4860 tree type = TREE_TYPE (decl);
4861 tree id = DECL_NAME (decl);
4862
4863 const char *name = IDENTIFIER_POINTER (id);
4864 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4865
4866 if (external_scope)
4867 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4868 UNKNOWN_LOCATION);
4869
4870 /* Builtins in the implementation namespace are made visible without
4871 needing to be explicitly declared. See push_file_scope. */
4872 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4873 {
4874 DECL_CHAIN (decl) = visible_builtins;
4875 visible_builtins = decl;
4876 }
4877
4878 return decl;
4879 }
4880
4881 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4882
4883 tree
4884 c_simulate_builtin_function_decl (tree decl)
4885 {
4886 tree type = TREE_TYPE (decl);
4887 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4888 return pushdecl (decl);
4889 }
4890
4891 /* Warn about attributes in a context where they are unused
4892 (attribute-declarations, except for the "fallthrough" case, and
4893 attributes on statements). */
4894
4895 void
4896 c_warn_unused_attributes (tree attrs)
4897 {
4898 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4899 if (get_attribute_namespace (t) == NULL_TREE)
4900 /* The specifications of standard attributes mean this is a
4901 constraint violation. */
4902 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4903 get_attribute_name (t));
4904 else if (!attribute_ignored_p (t))
4905 warning (OPT_Wattributes, "%qE attribute ignored",
4906 get_attribute_name (t));
4907 }
4908
4909 /* Warn for standard attributes being applied to a type that is not
4910 being defined, where that is a constraint violation, and return a
4911 list of attributes with them removed. */
4912
4913 tree
4914 c_warn_type_attributes (tree attrs)
4915 {
4916 tree *attr_ptr = &attrs;
4917 while (*attr_ptr)
4918 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4919 {
4920 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4921 get_attribute_name (*attr_ptr));
4922 *attr_ptr = TREE_CHAIN (*attr_ptr);
4923 }
4924 else
4925 attr_ptr = &TREE_CHAIN (*attr_ptr);
4926 return attrs;
4927 }
4928 \f
4929 /* Called when a declaration is seen that contains no names to declare.
4930 If its type is a reference to a structure, union or enum inherited
4931 from a containing scope, shadow that tag name for the current scope
4932 with a forward reference.
4933 If its type defines a new named structure or union
4934 or defines an enum, it is valid but we need not do anything here.
4935 Otherwise, it is an error. */
4936
4937 void
4938 shadow_tag (const struct c_declspecs *declspecs)
4939 {
4940 shadow_tag_warned (declspecs, 0);
4941 }
4942
4943 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4944 but no pedwarn. */
4945 void
4946 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4947 {
4948 bool found_tag = false;
4949
4950 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4951 {
4952 tree value = declspecs->type;
4953 enum tree_code code = TREE_CODE (value);
4954
4955 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4956 /* Used to test also that TYPE_SIZE (value) != 0.
4957 That caused warning for `struct foo;' at top level in the file. */
4958 {
4959 tree name = TYPE_NAME (value);
4960 tree t;
4961
4962 found_tag = true;
4963
4964 if (declspecs->restrict_p)
4965 {
4966 error ("invalid use of %<restrict%>");
4967 warned = 1;
4968 }
4969
4970 if (in_underspecified_init)
4971 {
4972 /* This can only occur with extensions such as statement
4973 expressions, but is still appropriate as an error to
4974 avoid types declared in such a context escaping to
4975 the type of an auto variable. */
4976 error ("%qT declared in underspecified object initializer",
4977 value);
4978 warned = 1;
4979 }
4980
4981 if (name == NULL_TREE)
4982 {
4983 if (warned != 1 && code != ENUMERAL_TYPE)
4984 /* Empty unnamed enum OK */
4985 {
4986 pedwarn (input_location, 0,
4987 "unnamed struct/union that defines no instances");
4988 warned = 1;
4989 }
4990 }
4991 else if (declspecs->typespec_kind != ctsk_tagdef
4992 && declspecs->typespec_kind != ctsk_tagfirstref
4993 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4994 && declspecs->storage_class != csc_none)
4995 {
4996 if (warned != 1)
4997 pedwarn (input_location, 0,
4998 "empty declaration with storage class specifier "
4999 "does not redeclare tag");
5000 warned = 1;
5001 pending_xref_error ();
5002 }
5003 else if (declspecs->typespec_kind != ctsk_tagdef
5004 && declspecs->typespec_kind != ctsk_tagfirstref
5005 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5006 && (declspecs->const_p
5007 || declspecs->volatile_p
5008 || declspecs->atomic_p
5009 || declspecs->restrict_p
5010 || declspecs->address_space))
5011 {
5012 if (warned != 1)
5013 pedwarn (input_location, 0,
5014 "empty declaration with type qualifier "
5015 "does not redeclare tag");
5016 warned = 1;
5017 pending_xref_error ();
5018 }
5019 else if (declspecs->typespec_kind != ctsk_tagdef
5020 && declspecs->typespec_kind != ctsk_tagfirstref
5021 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5022 && declspecs->alignas_p)
5023 {
5024 if (warned != 1)
5025 pedwarn (input_location, 0,
5026 "empty declaration with %<_Alignas%> "
5027 "does not redeclare tag");
5028 warned = 1;
5029 pending_xref_error ();
5030 }
5031 else if (declspecs->typespec_kind != ctsk_tagdef
5032 && declspecs->typespec_kind != ctsk_tagfirstref
5033 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5034 && code == ENUMERAL_TYPE
5035 && !declspecs->enum_type_specifier_ref_p)
5036 {
5037 bool warned_enum = false;
5038 if (warned != 1)
5039 warned_enum = pedwarn (input_location, OPT_Wpedantic,
5040 "empty declaration of %<enum%> type "
5041 "does not redeclare tag");
5042 if (warned_enum)
5043 warned = 1;
5044 pending_xref_error ();
5045 }
5046 else
5047 {
5048 pending_invalid_xref = NULL_TREE;
5049 t = lookup_tag (code, name, true, NULL);
5050
5051 if (t == NULL_TREE)
5052 {
5053 t = make_node (code);
5054 pushtag (input_location, name, t);
5055 }
5056 }
5057 }
5058 else
5059 {
5060 if (warned != 1 && !in_system_header_at (input_location))
5061 {
5062 pedwarn (input_location, 0,
5063 "useless type name in empty declaration");
5064 warned = 1;
5065 }
5066 }
5067 }
5068 else if (warned != 1 && !in_system_header_at (input_location)
5069 && declspecs->typedef_p)
5070 {
5071 pedwarn (input_location, 0, "useless type name in empty declaration");
5072 warned = 1;
5073 }
5074
5075 pending_invalid_xref = NULL_TREE;
5076
5077 if (declspecs->inline_p)
5078 {
5079 error ("%<inline%> in empty declaration");
5080 warned = 1;
5081 }
5082
5083 if (declspecs->noreturn_p)
5084 {
5085 error ("%<_Noreturn%> in empty declaration");
5086 warned = 1;
5087 }
5088
5089 if (declspecs->constexpr_p)
5090 {
5091 error ("%<constexpr%> in empty declaration");
5092 warned = 1;
5093 }
5094
5095 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5096 {
5097 error ("%<auto%> in file-scope empty declaration");
5098 warned = 1;
5099 }
5100
5101 if (current_scope == file_scope && declspecs->storage_class == csc_register)
5102 {
5103 error ("%<register%> in file-scope empty declaration");
5104 warned = 1;
5105 }
5106
5107 if (declspecs->enum_type_specifier_ref_p && !warned)
5108 {
5109 if (declspecs->storage_class != csc_none)
5110 {
5111 error ("storage class specifier in empty declaration with %<enum%> "
5112 "underlying type");
5113 warned = 1;
5114 }
5115 else if (declspecs->thread_p)
5116 {
5117 error ("%qs in empty declaration with %<enum%> underlying type",
5118 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5119 warned = 1;
5120 }
5121 else if (declspecs->const_p
5122 || declspecs->volatile_p
5123 || declspecs->atomic_p
5124 || declspecs->restrict_p
5125 || declspecs->address_space)
5126 {
5127 error ("type qualifier in empty declaration with %<enum%> "
5128 "underlying type");
5129 warned = 1;
5130 }
5131 else if (declspecs->alignas_p)
5132 {
5133 error ("%<alignas%> in empty declaration with %<enum%> "
5134 "underlying type");
5135 warned = 1;
5136 }
5137 }
5138
5139 if (!warned && !in_system_header_at (input_location)
5140 && declspecs->storage_class != csc_none)
5141 {
5142 warning (0, "useless storage class specifier in empty declaration");
5143 warned = 2;
5144 }
5145
5146 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5147 {
5148 warning (0, "useless %qs in empty declaration",
5149 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5150 warned = 2;
5151 }
5152
5153 if (!warned
5154 && !in_system_header_at (input_location)
5155 && (declspecs->const_p
5156 || declspecs->volatile_p
5157 || declspecs->atomic_p
5158 || declspecs->restrict_p
5159 || declspecs->address_space))
5160 {
5161 warning (0, "useless type qualifier in empty declaration");
5162 warned = 2;
5163 }
5164
5165 if (!warned && !in_system_header_at (input_location)
5166 && declspecs->alignas_p)
5167 {
5168 warning (0, "useless %<_Alignas%> in empty declaration");
5169 warned = 2;
5170 }
5171
5172 if (found_tag
5173 && warned == 2
5174 && (declspecs->typespec_kind == ctsk_tagref_attrs
5175 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5176 {
5177 /* Standard attributes after the "struct" or "union" keyword are
5178 only permitted when the contents of the type are defined, or
5179 in the form "struct-or-union attribute-specifier-sequence
5180 identifier;". If the ';' was not present, attributes were
5181 diagnosed in the parser. Here, ensure that any other useless
5182 elements of the declaration result in a pedwarn, not just a
5183 warning. Forward declarations of enum types are not part of
5184 standard C, but handle them the same. */
5185 pedwarn (input_location, 0,
5186 "invalid use of attributes in empty declaration");
5187 warned = 1;
5188 }
5189
5190 if (warned != 1)
5191 {
5192 if (declspecs->declspecs_seen_p
5193 && !declspecs->non_std_attrs_seen_p)
5194 /* An attribute declaration (but not a fallthrough attribute
5195 declaration, which was handled separately); warn if there
5196 are any attributes being ignored (but not if the attributes
5197 were empty). */
5198 c_warn_unused_attributes (declspecs->attrs);
5199 else if (!found_tag)
5200 pedwarn (input_location, 0, "empty declaration");
5201 }
5202 }
5203 \f
5204
5205 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5206 bits. SPECS represents declaration specifiers that the grammar
5207 only permits to contain type qualifiers and attributes. */
5208
5209 int
5210 quals_from_declspecs (const struct c_declspecs *specs)
5211 {
5212 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5213 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5214 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5215 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5216 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5217 gcc_assert (!specs->type
5218 && !specs->decl_attr
5219 && specs->typespec_word == cts_none
5220 && specs->storage_class == csc_none
5221 && !specs->typedef_p
5222 && !specs->explicit_signed_p
5223 && !specs->deprecated_p
5224 && !specs->unavailable_p
5225 && !specs->long_p
5226 && !specs->long_long_p
5227 && !specs->short_p
5228 && !specs->signed_p
5229 && !specs->unsigned_p
5230 && !specs->complex_p
5231 && !specs->inline_p
5232 && !specs->noreturn_p
5233 && !specs->thread_p);
5234 return quals;
5235 }
5236
5237 /* Construct an array declarator. LOC is the location of the
5238 beginning of the array (usually the opening brace). EXPR is the
5239 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5240 inside the [] (to be applied to the pointer to which a parameter
5241 array is converted). STATIC_P is true if "static" is inside the
5242 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5243 VLA of unspecified length which is nevertheless a complete type,
5244 false otherwise. The field for the contained declarator is left to
5245 be filled in by set_array_declarator_inner. */
5246
5247 struct c_declarator *
5248 build_array_declarator (location_t loc,
5249 tree expr, struct c_declspecs *quals, bool static_p,
5250 bool vla_unspec_p)
5251 {
5252 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5253 struct c_declarator);
5254 declarator->id_loc = loc;
5255 declarator->kind = cdk_array;
5256 declarator->declarator = 0;
5257 declarator->u.array.dimen = expr;
5258 if (quals)
5259 {
5260 declarator->u.array.attrs = quals->attrs;
5261 declarator->u.array.quals = quals_from_declspecs (quals);
5262 }
5263 else
5264 {
5265 declarator->u.array.attrs = NULL_TREE;
5266 declarator->u.array.quals = 0;
5267 }
5268 declarator->u.array.static_p = static_p;
5269 declarator->u.array.vla_unspec_p = vla_unspec_p;
5270 if (static_p || quals != NULL)
5271 pedwarn_c90 (loc, OPT_Wpedantic,
5272 "ISO C90 does not support %<static%> or type "
5273 "qualifiers in parameter array declarators");
5274 if (vla_unspec_p)
5275 pedwarn_c90 (loc, OPT_Wpedantic,
5276 "ISO C90 does not support %<[*]%> array declarators");
5277 if (vla_unspec_p)
5278 {
5279 if (!current_scope->parm_flag)
5280 {
5281 /* C99 6.7.5.2p4 */
5282 error_at (loc, "%<[*]%> not allowed in other than "
5283 "function prototype scope");
5284 declarator->u.array.vla_unspec_p = false;
5285 return NULL;
5286 }
5287 current_scope->had_vla_unspec = true;
5288 }
5289 return declarator;
5290 }
5291
5292 /* Set the contained declarator of an array declarator. DECL is the
5293 declarator, as constructed by build_array_declarator; INNER is what
5294 appears on the left of the []. */
5295
5296 struct c_declarator *
5297 set_array_declarator_inner (struct c_declarator *decl,
5298 struct c_declarator *inner)
5299 {
5300 decl->declarator = inner;
5301 return decl;
5302 }
5303
5304 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5305 static bool
5306 flexible_array_member_type_p (const_tree type)
5307 {
5308 if (TREE_CODE (type) == ARRAY_TYPE
5309 && TYPE_SIZE (type) == NULL_TREE
5310 && TYPE_DOMAIN (type) != NULL_TREE
5311 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5312 return true;
5313
5314 return false;
5315 }
5316
5317 /* Determine whether TYPE is a one-element array type "[1]". */
5318 static bool
5319 one_element_array_type_p (const_tree type)
5320 {
5321 if (TREE_CODE (type) != ARRAY_TYPE)
5322 return false;
5323 return integer_zerop (array_type_nelts (type));
5324 }
5325
5326 /* Determine whether TYPE is a zero-length array type "[0]". */
5327 static bool
5328 zero_length_array_type_p (const_tree type)
5329 {
5330 if (TREE_CODE (type) == ARRAY_TYPE)
5331 if (tree type_size = TYPE_SIZE_UNIT (type))
5332 if ((integer_zerop (type_size))
5333 && TYPE_DOMAIN (type) != NULL_TREE
5334 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5335 return true;
5336 return false;
5337 }
5338
5339 /* INIT is a constructor that forms DECL's initializer. If the final
5340 element initializes a flexible array field, add the size of that
5341 initializer to DECL's size. */
5342
5343 static void
5344 add_flexible_array_elts_to_size (tree decl, tree init)
5345 {
5346 tree elt, type;
5347
5348 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5349 return;
5350
5351 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5352 type = TREE_TYPE (elt);
5353 if (flexible_array_member_type_p (type))
5354 {
5355 complete_array_type (&type, elt, false);
5356 DECL_SIZE (decl)
5357 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5358 DECL_SIZE_UNIT (decl)
5359 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5360 }
5361 }
5362 \f
5363 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5364 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5365 before the type name, and set *EXPR_CONST_OPERANDS, if
5366 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5367 appear in a constant expression. */
5368
5369 tree
5370 groktypename (struct c_type_name *type_name, tree *expr,
5371 bool *expr_const_operands)
5372 {
5373 tree type;
5374 tree attrs = type_name->specs->attrs;
5375
5376 type_name->specs->attrs = NULL_TREE;
5377
5378 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5379 false, NULL, &attrs, expr, expr_const_operands,
5380 DEPRECATED_NORMAL);
5381
5382 /* Apply attributes. */
5383 attrs = c_warn_type_attributes (attrs);
5384 decl_attributes (&type, attrs, 0);
5385
5386 return type;
5387 }
5388
5389 /* Looks up the most recent pushed declaration corresponding to DECL. */
5390
5391 static tree
5392 lookup_last_decl (tree decl)
5393 {
5394 tree last_decl = lookup_name (DECL_NAME (decl));
5395 if (!last_decl)
5396 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5397 return last_decl;
5398 }
5399
5400 /* Wrapper for decl_attributes that adds some implicit attributes
5401 to VAR_DECLs or FUNCTION_DECLs. */
5402
5403 static tree
5404 c_decl_attributes (tree *node, tree attributes, int flags)
5405 {
5406 /* Add implicit "omp declare target" attribute if requested. */
5407 if (vec_safe_length (current_omp_declare_target_attribute)
5408 && ((VAR_P (*node) && is_global_var (*node))
5409 || TREE_CODE (*node) == FUNCTION_DECL))
5410 {
5411 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5412 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5413 NULL_TREE, attributes);
5414 else
5415 {
5416 attributes = tree_cons (get_identifier ("omp declare target"),
5417 NULL_TREE, attributes);
5418 attributes = tree_cons (get_identifier ("omp declare target block"),
5419 NULL_TREE, attributes);
5420 }
5421 if (TREE_CODE (*node) == FUNCTION_DECL)
5422 {
5423 int device_type
5424 = current_omp_declare_target_attribute->last ().device_type;
5425 device_type = MAX (device_type, 0);
5426 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5427 && !lookup_attribute ("omp declare target host", attributes))
5428 attributes
5429 = tree_cons (get_identifier ("omp declare target host"),
5430 NULL_TREE, attributes);
5431 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5432 && !lookup_attribute ("omp declare target nohost", attributes))
5433 attributes
5434 = tree_cons (get_identifier ("omp declare target nohost"),
5435 NULL_TREE, attributes);
5436
5437 int indirect
5438 = current_omp_declare_target_attribute->last ().indirect;
5439 if (indirect && !lookup_attribute ("omp declare target indirect",
5440 attributes))
5441 attributes
5442 = tree_cons (get_identifier ("omp declare target indirect"),
5443 NULL_TREE, attributes);
5444 }
5445 }
5446
5447 if (flag_openmp || flag_openmp_simd)
5448 {
5449 bool diagnosed = false;
5450 for (tree *pa = &attributes; *pa; )
5451 {
5452 if (is_attribute_namespace_p ("omp", *pa))
5453 {
5454 tree name = get_attribute_name (*pa);
5455 if (is_attribute_p ("directive", name)
5456 || is_attribute_p ("sequence", name)
5457 || is_attribute_p ("decl", name))
5458 {
5459 const char *p = NULL;
5460 if (TREE_VALUE (*pa) == NULL_TREE)
5461 p = IDENTIFIER_POINTER (name);
5462 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5463 {
5464 tree d = TREE_VALUE (a);
5465 gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5466 if (TREE_PUBLIC (d)
5467 && (VAR_P (*node)
5468 || TREE_CODE (*node) == FUNCTION_DECL)
5469 && c_maybe_parse_omp_decl (*node, d))
5470 continue;
5471 p = TREE_PUBLIC (d) ? "decl" : "directive";
5472 }
5473 if (p && !diagnosed)
5474 {
5475 error ("%<omp::%s%> not allowed to be specified in "
5476 "this context", p);
5477 diagnosed = true;
5478 }
5479 if (p)
5480 {
5481 *pa = TREE_CHAIN (*pa);
5482 continue;
5483 }
5484 }
5485 }
5486 pa = &TREE_CHAIN (*pa);
5487 }
5488 }
5489
5490 /* Look up the current declaration with all the attributes merged
5491 so far so that attributes on the current declaration that's
5492 about to be pushed that conflict with the former can be detected,
5493 diagnosed, and rejected as appropriate. */
5494 tree last_decl = lookup_last_decl (*node);
5495 return decl_attributes (node, attributes, flags, last_decl);
5496 }
5497
5498
5499 /* Decode a declarator in an ordinary declaration or data definition.
5500 This is called as soon as the type information and variable name
5501 have been parsed, before parsing the initializer if any.
5502 Here we create the ..._DECL node, fill in its type,
5503 and (if DO_PUSH) put it on the list of decls for the current context.
5504 When nonnull, set *LASTLOC to the location of the prior declaration
5505 of the same entity if one exists.
5506 The ..._DECL node is returned as the value.
5507
5508 Exception: for arrays where the length is not specified,
5509 the type is left null, to be filled in by `finish_decl'.
5510
5511 Function definitions do not come here; they go to start_function
5512 instead. However, external and forward declarations of functions
5513 do go through here. Structure field declarations are done by
5514 grokfield and not through here. */
5515
5516 tree
5517 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5518 bool initialized, tree attributes, bool do_push /* = true */,
5519 location_t *lastloc /* = NULL */)
5520 {
5521 tree decl;
5522 tree old_decl;
5523 tree tem;
5524 tree expr = NULL_TREE;
5525 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5526
5527 /* An object declared as __attribute__((unavailable)) suppresses
5528 warnings and errors from __attribute__((deprecated/unavailable))
5529 components.
5530 An object declared as __attribute__((deprecated)) suppresses
5531 warnings of uses of other deprecated items. */
5532 if (lookup_attribute ("unavailable", attributes))
5533 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5534 else if (lookup_attribute ("deprecated", attributes))
5535 deprecated_state = DEPRECATED_SUPPRESS;
5536
5537 decl = grokdeclarator (declarator, declspecs,
5538 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5539 deprecated_state);
5540 if (!decl || decl == error_mark_node)
5541 return NULL_TREE;
5542
5543 old_decl = lookup_last_decl (decl);
5544
5545 if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5546 if (lastdecl != error_mark_node)
5547 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5548
5549 /* Make sure the size expression is evaluated at this point. */
5550 if (expr && !current_scope->parm_flag)
5551 add_stmt (fold_convert (void_type_node, expr));
5552
5553 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5554 && TREE_PUBLIC (decl))
5555 warning (OPT_Wmain, "%q+D is usually a function", decl);
5556
5557 if (warn_missing_variable_declarations && VAR_P (decl)
5558 && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5559 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5560 "no previous declaration for %qD", decl);
5561
5562 if (initialized)
5563 /* Is it valid for this decl to have an initializer at all?
5564 If not, set INITIALIZED to zero, which will indirectly
5565 tell 'finish_decl' to ignore the initializer once it is parsed. */
5566 switch (TREE_CODE (decl))
5567 {
5568 case TYPE_DECL:
5569 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5570 initialized = false;
5571 break;
5572
5573 case FUNCTION_DECL:
5574 error ("function %qD is initialized like a variable", decl);
5575 initialized = false;
5576 break;
5577
5578 case PARM_DECL:
5579 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5580 error ("parameter %qD is initialized", decl);
5581 initialized = false;
5582 break;
5583
5584 default:
5585 /* Don't allow initializations for incomplete types except for
5586 arrays which might be completed by the initialization. */
5587
5588 /* This can happen if the array size is an undefined macro.
5589 We already gave a warning, so we don't need another one. */
5590 if (TREE_TYPE (decl) == error_mark_node)
5591 initialized = false;
5592 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5593 {
5594 /* A complete type is ok if size is fixed. If the size is
5595 variable, an empty initializer is OK and nonempty
5596 initializers will be diagnosed in the parser. */
5597 }
5598 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5599 {
5600 error ("variable %qD has initializer but incomplete type", decl);
5601 initialized = false;
5602 }
5603 }
5604
5605 if (initialized)
5606 {
5607 if (current_scope == file_scope)
5608 TREE_STATIC (decl) = 1;
5609
5610 /* Tell 'pushdecl' this is an initialized decl
5611 even though we don't yet have the initializer expression.
5612 Also tell 'finish_decl' it may store the real initializer. */
5613 DECL_INITIAL (decl) = error_mark_node;
5614 }
5615
5616 /* If this is a function declaration, write a record describing it to the
5617 prototypes file (if requested). */
5618
5619 if (TREE_CODE (decl) == FUNCTION_DECL)
5620 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5621
5622 /* ANSI specifies that a tentative definition which is not merged with
5623 a non-tentative definition behaves exactly like a definition with an
5624 initializer equal to zero. (Section 3.7.2)
5625
5626 -fno-common gives strict ANSI behavior, though this tends to break
5627 a large body of code that grew up without this rule.
5628
5629 Thread-local variables are never common, since there's no entrenched
5630 body of code to break, and it allows more efficient variable references
5631 in the presence of dynamic linking. */
5632
5633 if (VAR_P (decl)
5634 && !initialized
5635 && TREE_PUBLIC (decl)
5636 && !DECL_THREAD_LOCAL_P (decl)
5637 && !flag_no_common)
5638 DECL_COMMON (decl) = 1;
5639
5640 /* Set attributes here so if duplicate decl, will have proper attributes. */
5641 c_decl_attributes (&decl, attributes, 0);
5642
5643 /* Handle gnu_inline attribute. */
5644 if (declspecs->inline_p
5645 && !flag_gnu89_inline
5646 && TREE_CODE (decl) == FUNCTION_DECL
5647 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5648 || current_function_decl))
5649 {
5650 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5651 ;
5652 else if (declspecs->storage_class != csc_static)
5653 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5654 }
5655
5656 if (TREE_CODE (decl) == FUNCTION_DECL
5657 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5658 {
5659 struct c_declarator *ce = declarator;
5660
5661 if (ce->kind == cdk_pointer)
5662 ce = declarator->declarator;
5663 if (ce->kind == cdk_function)
5664 {
5665 tree args = ce->u.arg_info->parms;
5666 for (; args; args = DECL_CHAIN (args))
5667 {
5668 tree type = TREE_TYPE (args);
5669 if (type && INTEGRAL_TYPE_P (type)
5670 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5671 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5672 }
5673 }
5674 }
5675
5676 if (TREE_CODE (decl) == FUNCTION_DECL
5677 && DECL_DECLARED_INLINE_P (decl)
5678 && DECL_UNINLINABLE (decl)
5679 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5680 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5681 decl, "noinline");
5682
5683 /* C99 6.7.4p3: An inline definition of a function with external
5684 linkage shall not contain a definition of a modifiable object
5685 with static storage duration... */
5686 if (VAR_P (decl)
5687 && current_scope != file_scope
5688 && TREE_STATIC (decl)
5689 && !TREE_READONLY (decl)
5690 && DECL_DECLARED_INLINE_P (current_function_decl)
5691 && DECL_EXTERNAL (current_function_decl))
5692 record_inline_static (input_location, current_function_decl,
5693 decl, csi_modifiable);
5694
5695 if (c_dialect_objc ()
5696 && VAR_OR_FUNCTION_DECL_P (decl))
5697 objc_check_global_decl (decl);
5698
5699 /* Add this decl to the current scope.
5700 TEM may equal DECL or it may be a previous decl of the same name. */
5701 if (do_push)
5702 {
5703 tem = pushdecl (decl);
5704
5705 if (initialized && DECL_EXTERNAL (tem))
5706 {
5707 DECL_EXTERNAL (tem) = 0;
5708 TREE_STATIC (tem) = 1;
5709 }
5710
5711 return tem;
5712 }
5713 else
5714 return decl;
5715 }
5716
5717 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5718 DECL or the non-array element type if DECL is an uninitialized array.
5719 If that type has a const member, diagnose this. */
5720
5721 static void
5722 diagnose_uninitialized_cst_member (tree decl, tree type)
5723 {
5724 tree field;
5725 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5726 {
5727 tree field_type;
5728 if (TREE_CODE (field) != FIELD_DECL)
5729 continue;
5730 field_type = strip_array_types (TREE_TYPE (field));
5731
5732 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5733 {
5734 auto_diagnostic_group d;
5735 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5736 "uninitialized const member in %qT is invalid in C++",
5737 strip_array_types (TREE_TYPE (decl))))
5738 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5739 }
5740
5741 if (RECORD_OR_UNION_TYPE_P (field_type))
5742 diagnose_uninitialized_cst_member (decl, field_type);
5743 }
5744 }
5745
5746 /* Finish processing of a declaration;
5747 install its initial value.
5748 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5749 If the length of an array type is not known before,
5750 it must be determined now, from the initial value, or it is an error.
5751
5752 INIT_LOC is the location of the initial value. */
5753
5754 void
5755 finish_decl (tree decl, location_t init_loc, tree init,
5756 tree origtype, tree asmspec_tree)
5757 {
5758 tree type;
5759 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5760 const char *asmspec = 0;
5761
5762 /* If a name was specified, get the string. */
5763 if (VAR_OR_FUNCTION_DECL_P (decl)
5764 && DECL_FILE_SCOPE_P (decl))
5765 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5766 if (asmspec_tree)
5767 asmspec = TREE_STRING_POINTER (asmspec_tree);
5768
5769 if (VAR_P (decl)
5770 && TREE_STATIC (decl)
5771 && global_bindings_p ())
5772 /* So decl is a global variable. Record the types it uses
5773 so that we can decide later to emit debug info for them. */
5774 record_types_used_by_current_var_decl (decl);
5775
5776 /* If `start_decl' didn't like having an initialization, ignore it now. */
5777 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5778 init = NULL_TREE;
5779
5780 /* Don't crash if parm is initialized. */
5781 if (TREE_CODE (decl) == PARM_DECL)
5782 init = NULL_TREE;
5783
5784 if (init)
5785 store_init_value (init_loc, decl, init, origtype);
5786
5787 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5788 || TREE_CODE (decl) == FIELD_DECL))
5789 objc_check_decl (decl);
5790
5791 type = TREE_TYPE (decl);
5792
5793 /* Deduce size of array from initialization, if not already known.
5794 This is only needed for an initialization in the current scope;
5795 it must not be done for a file-scope initialization of a
5796 declaration with external linkage, redeclared in an inner scope
5797 with the outer declaration shadowed in an intermediate scope. */
5798 if (TREE_CODE (type) == ARRAY_TYPE
5799 && TYPE_DOMAIN (type) == NULL_TREE
5800 && TREE_CODE (decl) != TYPE_DECL
5801 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5802 {
5803 bool do_default
5804 = (TREE_STATIC (decl)
5805 /* Even if pedantic, an external linkage array
5806 may have incomplete type at first. */
5807 ? pedantic && !TREE_PUBLIC (decl)
5808 : !DECL_EXTERNAL (decl));
5809 int failure
5810 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5811 do_default);
5812
5813 /* Get the completed type made by complete_array_type. */
5814 type = TREE_TYPE (decl);
5815
5816 switch (failure)
5817 {
5818 case 1:
5819 error ("initializer fails to determine size of %q+D", decl);
5820 break;
5821
5822 case 2:
5823 if (do_default)
5824 error ("array size missing in %q+D", decl);
5825 break;
5826
5827 case 3:
5828 error ("zero or negative size array %q+D", decl);
5829 break;
5830
5831 case 0:
5832 /* For global variables, update the copy of the type that
5833 exists in the binding. */
5834 if (TREE_PUBLIC (decl))
5835 {
5836 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5837 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5838 b_ext = b_ext->shadowed;
5839 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5840 {
5841 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5842 b_ext->u.type = composite_type (b_ext->u.type, type);
5843 else
5844 b_ext->u.type = type;
5845 }
5846 }
5847 break;
5848
5849 default:
5850 gcc_unreachable ();
5851 }
5852
5853 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5854 TREE_TYPE (DECL_INITIAL (decl)) = type;
5855
5856 relayout_decl (decl);
5857 }
5858
5859 /* Look for braced array initializers for character arrays and
5860 recursively convert them into STRING_CSTs. */
5861 if (tree init = DECL_INITIAL (decl))
5862 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5863
5864 if (VAR_P (decl))
5865 {
5866 if (init && TREE_CODE (init) == CONSTRUCTOR)
5867 add_flexible_array_elts_to_size (decl, init);
5868
5869 complete_flexible_array_elts (DECL_INITIAL (decl));
5870
5871 if (is_global_var (decl))
5872 {
5873 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5874 ? TCTX_THREAD_STORAGE
5875 : TCTX_STATIC_STORAGE);
5876 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5877 TREE_TYPE (decl) = error_mark_node;
5878 }
5879
5880 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5881 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5882 layout_decl (decl, 0);
5883
5884 if (DECL_SIZE (decl) == NULL_TREE
5885 /* Don't give an error if we already gave one earlier. */
5886 && TREE_TYPE (decl) != error_mark_node
5887 && (TREE_STATIC (decl)
5888 /* A static variable with an incomplete type
5889 is an error if it is initialized.
5890 Also if it is not file scope.
5891 Also if it is thread-local (in C23).
5892 Otherwise, let it through, but if it is not `extern'
5893 then it may cause an error message later. */
5894 ? (DECL_INITIAL (decl) != NULL_TREE
5895 || !DECL_FILE_SCOPE_P (decl)
5896 || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5897 /* An automatic variable with an incomplete type
5898 is an error. */
5899 : !DECL_EXTERNAL (decl)))
5900 {
5901 error ("storage size of %q+D isn%'t known", decl);
5902 TREE_TYPE (decl) = error_mark_node;
5903 }
5904
5905 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5906 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5907 && DECL_SIZE (decl) == NULL_TREE
5908 && TREE_STATIC (decl))
5909 incomplete_record_decls.safe_push (decl);
5910
5911 if (is_global_var (decl)
5912 && DECL_SIZE (decl) != NULL_TREE
5913 && TREE_TYPE (decl) != error_mark_node)
5914 {
5915 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5916 constant_expression_warning (DECL_SIZE (decl));
5917 else
5918 {
5919 error ("storage size of %q+D isn%'t constant", decl);
5920 TREE_TYPE (decl) = error_mark_node;
5921 }
5922 }
5923
5924 if (TREE_USED (type))
5925 {
5926 TREE_USED (decl) = 1;
5927 DECL_READ_P (decl) = 1;
5928 }
5929 }
5930
5931 /* If this is a function and an assembler name is specified, reset DECL_RTL
5932 so we can give it its new name. Also, update builtin_decl if it
5933 was a normal built-in. */
5934 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5935 {
5936 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5937 set_builtin_user_assembler_name (decl, asmspec);
5938 set_user_assembler_name (decl, asmspec);
5939 }
5940
5941 /* If #pragma weak was used, mark the decl weak now. */
5942 maybe_apply_pragma_weak (decl);
5943
5944 /* Output the assembler code and/or RTL code for variables and functions,
5945 unless the type is an undefined structure or union.
5946 If not, it will get done when the type is completed. */
5947
5948 if (VAR_OR_FUNCTION_DECL_P (decl))
5949 {
5950 /* Determine the ELF visibility. */
5951 if (TREE_PUBLIC (decl))
5952 c_determine_visibility (decl);
5953
5954 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5955 if (c_dialect_objc ())
5956 objc_check_decl (decl);
5957
5958 if (asmspec)
5959 {
5960 /* If this is not a static variable, issue a warning.
5961 It doesn't make any sense to give an ASMSPEC for an
5962 ordinary, non-register local variable. Historically,
5963 GCC has accepted -- but ignored -- the ASMSPEC in
5964 this case. */
5965 if (!DECL_FILE_SCOPE_P (decl)
5966 && VAR_P (decl)
5967 && !C_DECL_REGISTER (decl)
5968 && !TREE_STATIC (decl))
5969 warning (0, "ignoring %<asm%> specifier for non-static local "
5970 "variable %q+D", decl);
5971 else
5972 set_user_assembler_name (decl, asmspec);
5973 }
5974
5975 if (DECL_FILE_SCOPE_P (decl))
5976 {
5977 if (DECL_INITIAL (decl) == NULL_TREE
5978 || DECL_INITIAL (decl) == error_mark_node)
5979 /* Don't output anything
5980 when a tentative file-scope definition is seen.
5981 But at end of compilation, do output code for them. */
5982 DECL_DEFER_OUTPUT (decl) = 1;
5983 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5984 DECL_HARD_REGISTER (decl) = 1;
5985 rest_of_decl_compilation (decl, true, 0);
5986
5987 if (TREE_CODE (decl) == FUNCTION_DECL)
5988 {
5989 tree parms = DECL_ARGUMENTS (decl);
5990 const bool builtin = fndecl_built_in_p (decl);
5991 if (tree access = build_attr_access_from_parms (parms, !builtin))
5992 decl_attributes (&decl, access, 0);
5993 }
5994 }
5995 else
5996 {
5997 /* In conjunction with an ASMSPEC, the `register'
5998 keyword indicates that we should place the variable
5999 in a particular register. */
6000 if (asmspec && C_DECL_REGISTER (decl))
6001 {
6002 DECL_HARD_REGISTER (decl) = 1;
6003 /* This cannot be done for a structure with volatile
6004 fields, on which DECL_REGISTER will have been
6005 reset. */
6006 if (!DECL_REGISTER (decl))
6007 error ("cannot put object with volatile field into register");
6008 }
6009
6010 if (TREE_CODE (decl) != FUNCTION_DECL)
6011 {
6012 /* If we're building a variable sized type, and we might be
6013 reachable other than via the top of the current binding
6014 level, then create a new BIND_EXPR so that we deallocate
6015 the object at the right time. */
6016 /* Note that DECL_SIZE can be null due to errors. */
6017 if (DECL_SIZE (decl)
6018 && !TREE_CONSTANT (DECL_SIZE (decl))
6019 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6020 {
6021 tree bind;
6022 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6023 TREE_SIDE_EFFECTS (bind) = 1;
6024 add_stmt (bind);
6025 BIND_EXPR_BODY (bind) = push_stmt_list ();
6026 }
6027 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6028 DECL_EXPR, decl));
6029 }
6030 }
6031
6032
6033 if (!DECL_FILE_SCOPE_P (decl))
6034 {
6035 /* Recompute the RTL of a local array now
6036 if it used to be an incomplete type. */
6037 if (was_incomplete && !is_global_var (decl))
6038 {
6039 /* If we used it already as memory, it must stay in memory. */
6040 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6041 /* If it's still incomplete now, no init will save it. */
6042 if (DECL_SIZE (decl) == NULL_TREE)
6043 DECL_INITIAL (decl) = NULL_TREE;
6044 }
6045 }
6046 }
6047
6048 if (TREE_CODE (decl) == TYPE_DECL)
6049 {
6050 if (!DECL_FILE_SCOPE_P (decl)
6051 && c_type_variably_modified_p (TREE_TYPE (decl)))
6052 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6053
6054 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6055 }
6056
6057 /* Install a cleanup (aka destructor) if one was given. */
6058 if (VAR_P (decl) && !TREE_STATIC (decl))
6059 {
6060 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6061 if (attr)
6062 {
6063 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6064 tree cleanup_decl = lookup_name (cleanup_id);
6065 tree cleanup;
6066 vec<tree, va_gc> *v;
6067
6068 /* Build "cleanup(&decl)" for the destructor. */
6069 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6070 vec_alloc (v, 1);
6071 v->quick_push (cleanup);
6072 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6073 vNULL, cleanup_decl, v, NULL);
6074 vec_free (v);
6075
6076 /* Don't warn about decl unused; the cleanup uses it. */
6077 TREE_USED (decl) = 1;
6078 TREE_USED (cleanup_decl) = 1;
6079 DECL_READ_P (decl) = 1;
6080
6081 push_cleanup (decl, cleanup, false);
6082 }
6083 }
6084
6085 if (warn_cxx_compat
6086 && VAR_P (decl)
6087 && !DECL_EXTERNAL (decl)
6088 && DECL_INITIAL (decl) == NULL_TREE)
6089 {
6090 type = strip_array_types (type);
6091 if (TREE_READONLY (decl))
6092 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6093 "uninitialized %<const %D%> is invalid in C++", decl);
6094 else if (RECORD_OR_UNION_TYPE_P (type)
6095 && C_TYPE_FIELDS_READONLY (type))
6096 diagnose_uninitialized_cst_member (decl, type);
6097 }
6098
6099 if (flag_openmp
6100 && VAR_P (decl)
6101 && lookup_attribute ("omp declare target implicit",
6102 DECL_ATTRIBUTES (decl)))
6103 {
6104 DECL_ATTRIBUTES (decl)
6105 = remove_attribute ("omp declare target implicit",
6106 DECL_ATTRIBUTES (decl));
6107 if (!omp_mappable_type (TREE_TYPE (decl)))
6108 error ("%q+D in declare target directive does not have mappable type",
6109 decl);
6110 else if (!lookup_attribute ("omp declare target",
6111 DECL_ATTRIBUTES (decl))
6112 && !lookup_attribute ("omp declare target link",
6113 DECL_ATTRIBUTES (decl)))
6114 {
6115 DECL_ATTRIBUTES (decl)
6116 = tree_cons (get_identifier ("omp declare target"),
6117 NULL_TREE, DECL_ATTRIBUTES (decl));
6118 symtab_node *node = symtab_node::get (decl);
6119 if (node != NULL)
6120 {
6121 node->offloadable = 1;
6122 if (ENABLE_OFFLOADING)
6123 {
6124 g->have_offload = true;
6125 if (is_a <varpool_node *> (node))
6126 vec_safe_push (offload_vars, decl);
6127 }
6128 }
6129 }
6130 }
6131
6132 /* This is the last point we can lower alignment so give the target the
6133 chance to do so. */
6134 if (VAR_P (decl)
6135 && !is_global_var (decl)
6136 && !DECL_HARD_REGISTER (decl))
6137 targetm.lower_local_decl_alignment (decl);
6138
6139 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6140 }
6141
6142 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6143 EXPR is NULL or a pointer to an expression that needs to be
6144 evaluated for the side effects of array size expressions in the
6145 parameters. */
6146
6147 tree
6148 grokparm (const struct c_parm *parm, tree *expr)
6149 {
6150 tree attrs = parm->attrs;
6151 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6152 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6153
6154 decl_attributes (&decl, attrs, 0);
6155
6156 return decl;
6157 }
6158
6159 /* Return attribute "arg spec" corresponding to an array/VLA parameter
6160 described by PARM, concatenated onto attributes ATTRS.
6161 The spec consists of one dollar symbol for each specified variable
6162 bound, one asterisk for each unspecified variable bound, followed
6163 by at most one specification of the most significant bound of
6164 an ordinary array parameter. For ordinary arrays the specification
6165 is either the constant bound itself, or the space character for
6166 an array with an unspecified bound (the [] form). Finally, a chain
6167 of specified variable bounds is appended to the spec, starting with
6168 the most significant bound. For example, the PARM T a[2][m][3][n]
6169 will produce __attribute__((arg spec ("[$$2]", m, n)).
6170 For T a typedef for an array with variable bounds, the bounds are
6171 included in the specification in the expected order.
6172 No "arg spec" is created for parameters of pointer types, making
6173 a distinction between T(*)[N] (or, equivalently, T[][N]) and
6174 the T[M][N] form, all of which have the same type and are represented
6175 the same, but only the last of which gets an "arg spec" describing
6176 the most significant bound M. */
6177
6178 static tree
6179 get_parm_array_spec (const struct c_parm *parm, tree attrs)
6180 {
6181 /* The attribute specification string, minor bound first. */
6182 std::string spec;
6183
6184 /* A list of VLA variable bounds, major first, or null if unspecified
6185 or not a VLA. */
6186 tree vbchain = NULL_TREE;
6187 /* True for a pointer parameter. */
6188 bool pointer = false;
6189 /* True for an ordinary array with an unpecified bound. */
6190 bool nobound = false;
6191
6192 /* Create a string representation for the bounds of the array/VLA. */
6193 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6194 {
6195 next = pd->declarator;
6196 while (next && next->kind == cdk_attrs)
6197 next = next->declarator;
6198
6199 /* Remember if a pointer has been seen to avoid storing the constant
6200 bound. */
6201 if (pd->kind == cdk_pointer)
6202 pointer = true;
6203
6204 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6205 && (!next || next->kind == cdk_id))
6206 {
6207 /* Do nothing for the common case of a pointer. The fact that
6208 the parameter is one can be deduced from the absence of
6209 an arg spec for it. */
6210 return attrs;
6211 }
6212
6213 if (pd->kind == cdk_id)
6214 {
6215 if (pointer
6216 || !parm->specs->type
6217 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6218 || !TYPE_DOMAIN (parm->specs->type)
6219 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6220 continue;
6221
6222 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6223 if (!vbchain
6224 && TREE_CODE (max) == INTEGER_CST)
6225 {
6226 /* Extract the upper bound from a parameter of an array type
6227 unless the parameter is an ordinary array of unspecified
6228 bound in which case a next iteration of the loop will
6229 exit. */
6230 if (spec.empty () || spec.end ()[-1] != ' ')
6231 {
6232 if (!tree_fits_shwi_p (max))
6233 continue;
6234
6235 /* The upper bound is the value of the largest valid
6236 index. */
6237 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6238 char buf[40];
6239 sprintf (buf, "%lu", (unsigned long)n);
6240 spec += buf;
6241 }
6242 continue;
6243 }
6244
6245 /* For a VLA typedef, create a list of its variable bounds and
6246 append it in the expected order to VBCHAIN. */
6247 tree tpbnds = NULL_TREE;
6248 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6249 type = TREE_TYPE (type))
6250 {
6251 tree nelts = array_type_nelts (type);
6252 if (error_operand_p (nelts))
6253 return attrs;
6254 if (TREE_CODE (nelts) != INTEGER_CST)
6255 {
6256 /* Each variable VLA bound is represented by the dollar
6257 sign. */
6258 spec += "$";
6259 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
6260 }
6261 }
6262 tpbnds = nreverse (tpbnds);
6263 vbchain = chainon (vbchain, tpbnds);
6264 continue;
6265 }
6266
6267 if (pd->kind != cdk_array)
6268 continue;
6269
6270 if (pd->u.array.vla_unspec_p)
6271 {
6272 /* Each unspecified bound is represented by a star. There
6273 can be any number of these in a declaration (but none in
6274 a definition). */
6275 spec += '*';
6276 continue;
6277 }
6278
6279 tree nelts = pd->u.array.dimen;
6280 if (!nelts)
6281 {
6282 /* Ordinary array of unspecified size. There can be at most
6283 one for the most significant bound. Exit on the next
6284 iteration which determines whether or not PARM is declared
6285 as a pointer or an array. */
6286 nobound = true;
6287 continue;
6288 }
6289
6290 if (pd->u.array.static_p)
6291 spec += 's';
6292
6293 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6294 /* Avoid invalid NELTS. */
6295 return attrs;
6296
6297 STRIP_NOPS (nelts);
6298 nelts = c_fully_fold (nelts, false, nullptr);
6299 if (TREE_CODE (nelts) == INTEGER_CST)
6300 {
6301 /* Skip all constant bounds except the most significant one.
6302 The interior ones are included in the array type. */
6303 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6304 continue;
6305
6306 if (!tree_fits_uhwi_p (nelts))
6307 /* Bail completely on invalid bounds. */
6308 return attrs;
6309
6310 char buf[40];
6311 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6312 sprintf (buf, "%llu", (unsigned long long)n);
6313 spec += buf;
6314 break;
6315 }
6316
6317 /* Each variable VLA bound is represented by a dollar sign. */
6318 spec += "$";
6319 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6320 }
6321
6322 if (spec.empty () && !nobound)
6323 return attrs;
6324
6325 spec.insert (0, "[");
6326 if (nobound)
6327 /* Ordinary array of unspecified bound is represented by a space.
6328 It must be last in the spec. */
6329 spec += ' ';
6330 spec += ']';
6331
6332 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6333 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6334 tree name = get_identifier ("arg spec");
6335 return tree_cons (name, args, attrs);
6336 }
6337
6338 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6339 and push that on the current scope. EXPR is a pointer to an
6340 expression that needs to be evaluated for the side effects of array
6341 size expressions in the parameters. */
6342
6343 void
6344 push_parm_decl (const struct c_parm *parm, tree *expr)
6345 {
6346 tree attrs = parm->attrs;
6347 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6348 &attrs, expr, NULL, DEPRECATED_NORMAL);
6349 if (decl && DECL_P (decl))
6350 DECL_SOURCE_LOCATION (decl) = parm->loc;
6351
6352 attrs = get_parm_array_spec (parm, attrs);
6353 decl_attributes (&decl, attrs, 0);
6354
6355 decl = pushdecl (decl);
6356
6357 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6358 }
6359
6360 /* Mark all the parameter declarations to date as forward decls.
6361 Also diagnose use of this extension. */
6362
6363 void
6364 mark_forward_parm_decls (void)
6365 {
6366 struct c_binding *b;
6367
6368 if (pedantic && !current_scope->warned_forward_parm_decls)
6369 {
6370 pedwarn (input_location, OPT_Wpedantic,
6371 "ISO C forbids forward parameter declarations");
6372 current_scope->warned_forward_parm_decls = true;
6373 }
6374
6375 for (b = current_scope->bindings; b; b = b->prev)
6376 if (TREE_CODE (b->decl) == PARM_DECL)
6377 TREE_ASM_WRITTEN (b->decl) = 1;
6378 }
6379 \f
6380 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6381 literal, which may be an incomplete array type completed by the
6382 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6383 literal. NON_CONST is true if the initializers contain something
6384 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6385 it is the (valid) alignment for this compound literal, as specified
6386 with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6387 compound literal. */
6388
6389 tree
6390 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6391 unsigned int alignas_align,
6392 struct c_declspecs *scspecs)
6393 {
6394 /* We do not use start_decl here because we have a type, not a declarator;
6395 and do not use finish_decl because the decl should be stored inside
6396 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6397 tree decl;
6398 tree complit;
6399 tree stmt;
6400 bool threadp = scspecs ? scspecs->thread_p : false;
6401 enum c_storage_class storage_class = (scspecs
6402 ? scspecs->storage_class
6403 : csc_none);
6404
6405 if (type == error_mark_node
6406 || init == error_mark_node)
6407 return error_mark_node;
6408
6409 if (current_scope == file_scope && storage_class == csc_register)
6410 {
6411 error_at (loc, "file-scope compound literal specifies %<register%>");
6412 storage_class = csc_none;
6413 }
6414
6415 if (current_scope != file_scope && threadp && storage_class == csc_none)
6416 {
6417 error_at (loc, "compound literal implicitly auto and declared %qs",
6418 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6419 threadp = false;
6420 }
6421
6422 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6423 DECL_EXTERNAL (decl) = 0;
6424 TREE_PUBLIC (decl) = 0;
6425 TREE_STATIC (decl) = (current_scope == file_scope
6426 || storage_class == csc_static);
6427 DECL_CONTEXT (decl) = current_function_decl;
6428 TREE_USED (decl) = 1;
6429 DECL_READ_P (decl) = 1;
6430 DECL_ARTIFICIAL (decl) = 1;
6431 DECL_IGNORED_P (decl) = 1;
6432 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6433 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6434 TREE_TYPE (decl) = type;
6435 if (threadp)
6436 set_decl_tls_model (decl, decl_default_tls_model (decl));
6437 if (storage_class == csc_register)
6438 {
6439 C_DECL_REGISTER (decl) = 1;
6440 DECL_REGISTER (decl) = 1;
6441 }
6442 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6443 if (alignas_align)
6444 {
6445 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6446 DECL_USER_ALIGN (decl) = 1;
6447 }
6448 store_init_value (loc, decl, init, NULL_TREE);
6449 if (current_scope != file_scope
6450 && TREE_STATIC (decl)
6451 && !TREE_READONLY (decl)
6452 && DECL_DECLARED_INLINE_P (current_function_decl)
6453 && DECL_EXTERNAL (current_function_decl))
6454 record_inline_static (input_location, current_function_decl,
6455 decl, csi_modifiable);
6456
6457 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6458 {
6459 int failure = complete_array_type (&TREE_TYPE (decl),
6460 DECL_INITIAL (decl), true);
6461 /* If complete_array_type returns 3, it means that the
6462 initial value of the compound literal is empty. Allow it. */
6463 gcc_assert (failure == 0 || failure == 3);
6464
6465 type = TREE_TYPE (decl);
6466 TREE_TYPE (DECL_INITIAL (decl)) = type;
6467 }
6468
6469 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6470 {
6471 c_incomplete_type_error (loc, NULL_TREE, type);
6472 return error_mark_node;
6473 }
6474
6475 if (TREE_STATIC (decl)
6476 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6477 return error_mark_node;
6478
6479 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6480 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6481 TREE_SIDE_EFFECTS (complit) = 1;
6482
6483 layout_decl (decl, 0);
6484
6485 if (TREE_STATIC (decl))
6486 {
6487 /* This decl needs a name for the assembler output. */
6488 set_compound_literal_name (decl);
6489 DECL_DEFER_OUTPUT (decl) = 1;
6490 DECL_COMDAT (decl) = 1;
6491 pushdecl (decl);
6492 rest_of_decl_compilation (decl, 1, 0);
6493 }
6494 else if (current_function_decl && !current_scope->parm_flag)
6495 pushdecl (decl);
6496
6497 if (non_const)
6498 {
6499 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6500 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6501 }
6502
6503 return complit;
6504 }
6505
6506 /* Check the type of a compound literal. Here we just check that it
6507 is valid for C++. */
6508
6509 void
6510 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6511 {
6512 if (warn_cxx_compat
6513 && (type_name->specs->typespec_kind == ctsk_tagdef
6514 || type_name->specs->typespec_kind == ctsk_tagfirstref
6515 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6516 warning_at (loc, OPT_Wc___compat,
6517 "defining a type in a compound literal is invalid in C++");
6518 }
6519 \f
6520 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6521 replacing with appropriate values if they are invalid. */
6522
6523 static void
6524 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6525 tree orig_name)
6526 {
6527 tree type_mv;
6528 unsigned int max_width;
6529 unsigned HOST_WIDE_INT w;
6530 const char *name = (orig_name
6531 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6532 : _("<anonymous>"));
6533
6534 /* Detect and ignore out of range field width and process valid
6535 field widths. */
6536 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6537 {
6538 error_at (loc, "bit-field %qs width not an integer constant", name);
6539 *width = integer_one_node;
6540 }
6541 else
6542 {
6543 if (TREE_CODE (*width) != INTEGER_CST)
6544 {
6545 *width = c_fully_fold (*width, false, NULL);
6546 if (TREE_CODE (*width) == INTEGER_CST)
6547 pedwarn (loc, OPT_Wpedantic,
6548 "bit-field %qs width not an integer constant expression",
6549 name);
6550 }
6551 if (TREE_CODE (*width) != INTEGER_CST)
6552 {
6553 error_at (loc, "bit-field %qs width not an integer constant", name);
6554 *width = integer_one_node;
6555 }
6556 constant_expression_warning (*width);
6557 if (tree_int_cst_sgn (*width) < 0)
6558 {
6559 error_at (loc, "negative width in bit-field %qs", name);
6560 *width = integer_one_node;
6561 }
6562 else if (integer_zerop (*width) && orig_name)
6563 {
6564 error_at (loc, "zero width for bit-field %qs", name);
6565 *width = integer_one_node;
6566 }
6567 }
6568
6569 /* Detect invalid bit-field type. */
6570 if (TREE_CODE (*type) != INTEGER_TYPE
6571 && TREE_CODE (*type) != BOOLEAN_TYPE
6572 && TREE_CODE (*type) != ENUMERAL_TYPE
6573 && TREE_CODE (*type) != BITINT_TYPE)
6574 {
6575 error_at (loc, "bit-field %qs has invalid type", name);
6576 *type = unsigned_type_node;
6577 }
6578
6579 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6580 {
6581 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6582 name);
6583 *type = unsigned_type_node;
6584 }
6585
6586 type_mv = TYPE_MAIN_VARIANT (*type);
6587 if (!in_system_header_at (input_location)
6588 && type_mv != integer_type_node
6589 && type_mv != unsigned_type_node
6590 && type_mv != boolean_type_node)
6591 pedwarn_c90 (loc, OPT_Wpedantic,
6592 "type of bit-field %qs is a GCC extension", name);
6593
6594 max_width = TYPE_PRECISION (*type);
6595
6596 if (compare_tree_int (*width, max_width) > 0)
6597 {
6598 error_at (loc, "width of %qs exceeds its type", name);
6599 w = max_width;
6600 *width = build_int_cst (integer_type_node, w);
6601 }
6602 else
6603 w = tree_to_uhwi (*width);
6604
6605 /* Truncation of hardbool false and true representation values is always safe:
6606 either the values remain different, or we'll report a problem when creating
6607 the narrower type. */
6608 if (c_hardbool_type_attr (*type))
6609 return;
6610
6611 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6612 {
6613 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6614 if (!lt
6615 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6616 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6617 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6618 }
6619 }
6620
6621 \f
6622
6623 /* Print warning about variable length array if necessary. */
6624
6625 static void
6626 warn_variable_length_array (tree name, tree size)
6627 {
6628 if (TREE_CONSTANT (size))
6629 {
6630 if (name)
6631 pedwarn_c90 (input_location, OPT_Wvla,
6632 "ISO C90 forbids array %qE whose size "
6633 "cannot be evaluated", name);
6634 else
6635 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6636 "whose size cannot be evaluated");
6637 }
6638 else
6639 {
6640 if (name)
6641 pedwarn_c90 (input_location, OPT_Wvla,
6642 "ISO C90 forbids variable length array %qE", name);
6643 else
6644 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6645 "length array");
6646 }
6647 }
6648
6649 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6650 considering only those c_declspec_words found in LIST, which
6651 must be terminated by cdw_number_of_elements. */
6652
6653 static location_t
6654 smallest_type_quals_location (const location_t *locations,
6655 const c_declspec_word *list)
6656 {
6657 location_t loc = UNKNOWN_LOCATION;
6658 while (*list != cdw_number_of_elements)
6659 {
6660 location_t newloc = locations[*list];
6661 if (loc == UNKNOWN_LOCATION
6662 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6663 loc = newloc;
6664 list++;
6665 }
6666
6667 return loc;
6668 }
6669
6670
6671 /* We attach an artificial TYPE_DECL to pointed-to type
6672 and arrange for it to be included in a DECL_EXPR. This
6673 forces the sizes evaluation at a safe point and ensures it
6674 is not deferred until e.g. within a deeper conditional context.
6675
6676 PARM contexts have no enclosing statement list that
6677 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6678 instead, and add it to the list of expressions that
6679 need to be evaluated.
6680
6681 TYPENAME contexts do have an enclosing statement list,
6682 but it would be incorrect to use it, as the size should
6683 only be evaluated if the containing expression is
6684 evaluated. We might also be in the middle of an
6685 expression with side effects on the pointed-to type size
6686 "arguments" prior to the pointer declaration point and
6687 the fake TYPE_DECL in the enclosing context would force
6688 the size evaluation prior to the side effects. We therefore
6689 use BIND_EXPRs in TYPENAME contexts too. */
6690 static void
6691 add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6692 {
6693 tree bind = NULL_TREE;
6694 if (expr)
6695 {
6696 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6697 NULL_TREE);
6698 TREE_SIDE_EFFECTS (bind) = 1;
6699 BIND_EXPR_BODY (bind) = push_stmt_list ();
6700 push_scope ();
6701 }
6702
6703 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6704 pushdecl (decl);
6705 DECL_ARTIFICIAL (decl) = 1;
6706 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6707 if (set_name_p)
6708 TYPE_NAME (type) = decl;
6709
6710 if (bind)
6711 {
6712 pop_scope ();
6713 BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6714 if (*expr)
6715 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6716 else
6717 *expr = bind;
6718 }
6719 }
6720
6721 /* Given declspecs and a declarator,
6722 determine the name and type of the object declared
6723 and construct a ..._DECL node for it.
6724 (In one case we can return a ..._TYPE node instead.
6725 For invalid input we sometimes return NULL_TREE.)
6726
6727 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6728
6729 DECL_CONTEXT says which syntactic context this declaration is in:
6730 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6731 FUNCDEF for a function definition. Like NORMAL but a few different
6732 error messages in each case. Return value may be zero meaning
6733 this definition is too screwy to try to parse.
6734 PARM for a parameter declaration (either within a function prototype
6735 or before a function body). Make a PARM_DECL, or return void_type_node.
6736 TYPENAME if for a typename (in a cast or sizeof).
6737 Don't make a DECL node; just return the ..._TYPE node.
6738 FIELD for a struct or union field; make a FIELD_DECL.
6739 INITIALIZED is true if the decl has an initializer.
6740 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6741 representing the width of the bit-field.
6742 DECL_ATTRS points to the list of attributes that should be added to this
6743 decl. Any nested attributes that belong on the decl itself will be
6744 added to this list.
6745 If EXPR is not NULL, any expressions that need to be evaluated as
6746 part of evaluating variably modified types will be stored in *EXPR.
6747 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6748 set to indicate whether operands in *EXPR can be used in constant
6749 expressions.
6750 DEPRECATED_STATE is a deprecated_states value indicating whether
6751 deprecation/unavailability warnings should be suppressed.
6752
6753 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6754 It may also be so in the PARM case, for a prototype where the
6755 argument type is specified but not the name.
6756
6757 This function is where the complicated C meanings of `static'
6758 and `extern' are interpreted. */
6759
6760 static tree
6761 grokdeclarator (const struct c_declarator *declarator,
6762 struct c_declspecs *declspecs,
6763 enum decl_context decl_context, bool initialized, tree *width,
6764 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6765 enum deprecated_states deprecated_state)
6766 {
6767 tree type = declspecs->type;
6768 bool threadp = declspecs->thread_p;
6769 bool constexprp = declspecs->constexpr_p;
6770 enum c_storage_class storage_class = declspecs->storage_class;
6771 int constp;
6772 int restrictp;
6773 int volatilep;
6774 int atomicp;
6775 int type_quals = TYPE_UNQUALIFIED;
6776 tree name = NULL_TREE;
6777 bool funcdef_flag = false;
6778 bool funcdef_syntax = false;
6779 bool size_varies = false;
6780 tree decl_attr = declspecs->decl_attr;
6781 int array_ptr_quals = TYPE_UNQUALIFIED;
6782 tree array_ptr_attrs = NULL_TREE;
6783 bool array_parm_static = false;
6784 bool array_parm_vla_unspec_p = false;
6785 tree returned_attrs = NULL_TREE;
6786 tree decl_id_attrs = NULL_TREE;
6787 bool bitfield = width != NULL;
6788 tree element_type;
6789 tree orig_qual_type = NULL;
6790 size_t orig_qual_indirect = 0;
6791 struct c_arg_info *arg_info = 0;
6792 addr_space_t as1, as2, address_space;
6793 location_t loc = UNKNOWN_LOCATION;
6794 tree expr_dummy;
6795 bool expr_const_operands_dummy;
6796 enum c_declarator_kind first_non_attr_kind;
6797 unsigned int alignas_align = 0;
6798
6799 if (type == NULL_TREE)
6800 {
6801 /* This can occur for auto on a parameter in C23 mode. Set a
6802 dummy type here so subsequent code can give diagnostics for
6803 this case. */
6804 gcc_assert (declspecs->c23_auto_p);
6805 gcc_assert (decl_context == PARM);
6806 type = declspecs->type = integer_type_node;
6807 }
6808 if (TREE_CODE (type) == ERROR_MARK)
6809 return error_mark_node;
6810 if (expr == NULL)
6811 {
6812 expr = &expr_dummy;
6813 expr_dummy = NULL_TREE;
6814 }
6815 if (expr_const_operands == NULL)
6816 expr_const_operands = &expr_const_operands_dummy;
6817
6818 if (declspecs->expr)
6819 {
6820 if (*expr)
6821 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6822 declspecs->expr);
6823 else
6824 *expr = declspecs->expr;
6825 }
6826 *expr_const_operands = declspecs->expr_const_operands;
6827
6828 if (decl_context == FUNCDEF)
6829 funcdef_flag = true, decl_context = NORMAL;
6830
6831 /* Look inside a declarator for the name being declared
6832 and get it as an IDENTIFIER_NODE, for an error message. */
6833 {
6834 const struct c_declarator *decl = declarator;
6835
6836 first_non_attr_kind = cdk_attrs;
6837 while (decl)
6838 switch (decl->kind)
6839 {
6840 case cdk_array:
6841 loc = decl->id_loc;
6842 /* FALL THRU. */
6843
6844 case cdk_function:
6845 case cdk_pointer:
6846 funcdef_syntax = (decl->kind == cdk_function);
6847 if (first_non_attr_kind == cdk_attrs)
6848 first_non_attr_kind = decl->kind;
6849 decl = decl->declarator;
6850 break;
6851
6852 case cdk_attrs:
6853 decl = decl->declarator;
6854 break;
6855
6856 case cdk_id:
6857 loc = decl->id_loc;
6858 if (decl->u.id.id)
6859 name = decl->u.id.id;
6860 decl_id_attrs = decl->u.id.attrs;
6861 if (first_non_attr_kind == cdk_attrs)
6862 first_non_attr_kind = decl->kind;
6863 decl = 0;
6864 break;
6865
6866 default:
6867 gcc_unreachable ();
6868 }
6869 if (name == NULL_TREE)
6870 {
6871 gcc_assert (decl_context == PARM
6872 || decl_context == TYPENAME
6873 || (decl_context == FIELD
6874 && declarator->kind == cdk_id));
6875 gcc_assert (!initialized);
6876 }
6877 }
6878
6879 /* An enum type specifier (": specifier-qualifier-list") may only be
6880 specified when the enum is being defined or in an empty
6881 declaration of the form "enum identifier enum-type-specifier;".
6882 Except for the case of an empty declaration that has additional
6883 declaration specifiers, all invalid contexts (declarations that
6884 aren't empty, type names, parameter declarations, member
6885 declarations) pass through grokdeclarator. */
6886 if (declspecs->enum_type_specifier_ref_p)
6887 error_at (loc, "%<enum%> underlying type may not be specified here");
6888
6889 /* A function definition's declarator must have the form of
6890 a function declarator. */
6891
6892 if (funcdef_flag && !funcdef_syntax)
6893 return NULL_TREE;
6894
6895 /* If this looks like a function definition, make it one,
6896 even if it occurs where parms are expected.
6897 Then store_parm_decls will reject it and not use it as a parm. */
6898 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6899 decl_context = PARM;
6900
6901 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6902 {
6903 if (declspecs->unavailable_p)
6904 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6905 else if (declspecs->deprecated_p
6906 && deprecated_state != DEPRECATED_SUPPRESS)
6907 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6908 }
6909
6910 if ((decl_context == NORMAL || decl_context == FIELD)
6911 && current_scope == file_scope
6912 && c_type_variably_modified_p (type))
6913 {
6914 if (name)
6915 error_at (loc, "variably modified %qE at file scope", name);
6916 else
6917 error_at (loc, "variably modified field at file scope");
6918 type = integer_type_node;
6919 }
6920
6921 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6922
6923 /* Diagnose defaulting to "int". */
6924
6925 if (declspecs->default_int_p)
6926 {
6927 /* Issue a warning if this is an ISO C 99 program or if
6928 -Wreturn-type and this is a function, or if -Wimplicit;
6929 prefer the former warning since it is more explicit. */
6930 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6931 && funcdef_flag)
6932 warn_about_return_type = 1;
6933 else
6934 {
6935 if (name)
6936 permerror_opt (loc, OPT_Wimplicit_int,
6937 "type defaults to %<int%> in declaration "
6938 "of %qE", name);
6939 else
6940 permerror_opt (loc, OPT_Wimplicit_int,
6941 "type defaults to %<int%> in type name");
6942 }
6943 }
6944
6945 /* Adjust the type if a bit-field is being declared,
6946 -funsigned-bitfields applied and the type is not explicitly
6947 "signed". */
6948 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6949 && TREE_CODE (type) == INTEGER_TYPE)
6950 type = unsigned_type_for (type);
6951
6952 /* Figure out the type qualifiers for the declaration. There are
6953 two ways a declaration can become qualified. One is something
6954 like `const int i' where the `const' is explicit. Another is
6955 something like `typedef const int CI; CI i' where the type of the
6956 declaration contains the `const'. A third possibility is that
6957 there is a type qualifier on the element type of a typedefed
6958 array type, in which case we should extract that qualifier so
6959 that c_apply_type_quals_to_decl receives the full list of
6960 qualifiers to work with (C90 is not entirely clear about whether
6961 duplicate qualifiers should be diagnosed in this case, but it
6962 seems most appropriate to do so). */
6963 element_type = strip_array_types (type);
6964 constp = declspecs->const_p + TYPE_READONLY (element_type);
6965 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6966 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6967 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6968 as1 = declspecs->address_space;
6969 as2 = TYPE_ADDR_SPACE (element_type);
6970 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6971
6972 if (constp > 1)
6973 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6974 if (restrictp > 1)
6975 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6976 if (volatilep > 1)
6977 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6978 if (atomicp > 1)
6979 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6980
6981 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6982 error_at (loc, "conflicting named address spaces (%s vs %s)",
6983 c_addr_space_name (as1), c_addr_space_name (as2));
6984
6985 if ((TREE_CODE (type) == ARRAY_TYPE
6986 || first_non_attr_kind == cdk_array)
6987 && TYPE_QUALS (element_type))
6988 {
6989 orig_qual_type = type;
6990 type = TYPE_MAIN_VARIANT (type);
6991 }
6992 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6993 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6994 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6995 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6996 | ENCODE_QUAL_ADDR_SPACE (address_space));
6997 if (type_quals != TYPE_QUALS (element_type))
6998 orig_qual_type = NULL_TREE;
6999
7000 /* Applying the _Atomic qualifier to an array type (through the use
7001 of typedefs or typeof) must be detected here. If the qualifier
7002 is introduced later, any appearance of applying it to an array is
7003 actually applying it to an element of that array. */
7004 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7005 error_at (loc, "%<_Atomic%>-qualified array type");
7006
7007 /* Warn about storage classes that are invalid for certain
7008 kinds of declarations (parameters, typenames, etc.). */
7009
7010 if (funcdef_flag
7011 && (threadp
7012 || constexprp
7013 || storage_class == csc_auto
7014 || storage_class == csc_register
7015 || storage_class == csc_typedef))
7016 {
7017 if (storage_class == csc_auto)
7018 pedwarn (loc,
7019 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7020 "function definition declared %<auto%>");
7021 if (storage_class == csc_register)
7022 error_at (loc, "function definition declared %<register%>");
7023 if (storage_class == csc_typedef)
7024 error_at (loc, "function definition declared %<typedef%>");
7025 if (threadp)
7026 error_at (loc, "function definition declared %qs",
7027 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7028 threadp = false;
7029 /* The parser ensures a constexpr function definition never
7030 reaches here. */
7031 gcc_assert (!constexprp);
7032 if (storage_class == csc_auto
7033 || storage_class == csc_register
7034 || storage_class == csc_typedef)
7035 storage_class = csc_none;
7036 }
7037 else if (decl_context != NORMAL && (storage_class != csc_none
7038 || threadp
7039 || constexprp
7040 || declspecs->c23_auto_p))
7041 {
7042 if (decl_context == PARM
7043 && storage_class == csc_register
7044 && !constexprp
7045 && !declspecs->c23_auto_p)
7046 ;
7047 else
7048 {
7049 switch (decl_context)
7050 {
7051 case FIELD:
7052 if (name)
7053 error_at (loc, "storage class specified for structure "
7054 "field %qE", name);
7055 else
7056 error_at (loc, "storage class specified for structure field");
7057 break;
7058 case PARM:
7059 if (name)
7060 error_at (loc, "storage class specified for parameter %qE",
7061 name);
7062 else
7063 error_at (loc, "storage class specified for unnamed parameter");
7064 break;
7065 default:
7066 error_at (loc, "storage class specified for typename");
7067 break;
7068 }
7069 storage_class = csc_none;
7070 threadp = false;
7071 constexprp = false;
7072 }
7073 }
7074 else if (storage_class == csc_extern
7075 && initialized
7076 && !funcdef_flag)
7077 {
7078 /* 'extern' with initialization is invalid if not at file scope. */
7079 if (current_scope == file_scope)
7080 {
7081 /* It is fine to have 'extern const' when compiling at C
7082 and C++ intersection. */
7083 if (!(warn_cxx_compat && constp))
7084 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7085 name);
7086 }
7087 else
7088 error_at (loc, "%qE has both %<extern%> and initializer", name);
7089 }
7090 else if (current_scope == file_scope)
7091 {
7092 if (storage_class == csc_auto)
7093 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7094 name);
7095 if (pedantic && storage_class == csc_register)
7096 pedwarn (input_location, OPT_Wpedantic,
7097 "file-scope declaration of %qE specifies %<register%>", name);
7098 }
7099 else
7100 {
7101 if (storage_class == csc_extern && funcdef_flag)
7102 error_at (loc, "nested function %qE declared %<extern%>", name);
7103 else if (threadp && storage_class == csc_none)
7104 {
7105 error_at (loc, "function-scope %qE implicitly auto and declared "
7106 "%qs", name,
7107 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7108 threadp = false;
7109 }
7110 }
7111
7112 /* Now figure out the structure of the declarator proper.
7113 Descend through it, creating more complex types, until we reach
7114 the declared identifier (or NULL_TREE, in an absolute declarator).
7115 At each stage we maintain an unqualified version of the type
7116 together with any qualifiers that should be applied to it with
7117 c_build_qualified_type; this way, array types including
7118 multidimensional array types are first built up in unqualified
7119 form and then the qualified form is created with
7120 TYPE_MAIN_VARIANT pointing to the unqualified form. */
7121
7122 while (declarator && declarator->kind != cdk_id)
7123 {
7124 if (type == error_mark_node)
7125 {
7126 declarator = declarator->declarator;
7127 continue;
7128 }
7129
7130 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7131 a cdk_pointer (for *...),
7132 a cdk_function (for ...(...)),
7133 a cdk_attrs (for nested attributes),
7134 or a cdk_id (for the name being declared
7135 or the place in an absolute declarator
7136 where the name was omitted).
7137 For the last case, we have just exited the loop.
7138
7139 At this point, TYPE is the type of elements of an array,
7140 or for a function to return, or for a pointer to point to.
7141 After this sequence of ifs, TYPE is the type of the
7142 array or function or pointer, and DECLARATOR has had its
7143 outermost layer removed. */
7144
7145 if (array_ptr_quals != TYPE_UNQUALIFIED
7146 || array_ptr_attrs != NULL_TREE
7147 || array_parm_static)
7148 {
7149 /* Only the innermost declarator (making a parameter be of
7150 array type which is converted to pointer type)
7151 may have static or type qualifiers. */
7152 error_at (loc, "static or type qualifiers in non-parameter array declarator");
7153 array_ptr_quals = TYPE_UNQUALIFIED;
7154 array_ptr_attrs = NULL_TREE;
7155 array_parm_static = false;
7156 }
7157
7158 bool varmod = C_TYPE_VARIABLY_MODIFIED (type);
7159
7160 switch (declarator->kind)
7161 {
7162 case cdk_attrs:
7163 {
7164 /* A declarator with embedded attributes. */
7165 tree attrs = declarator->u.attrs;
7166 const struct c_declarator *inner_decl;
7167 int attr_flags = 0;
7168 declarator = declarator->declarator;
7169 /* Standard attribute syntax precisely defines what entity
7170 an attribute in each position appertains to, so only
7171 apply laxity about positioning to GNU attribute syntax.
7172 Standard attributes applied to a function or array
7173 declarator apply exactly to that type; standard
7174 attributes applied to the identifier apply to the
7175 declaration rather than to the type, and are specified
7176 using a cdk_id declarator rather than using
7177 cdk_attrs. */
7178 inner_decl = declarator;
7179 while (inner_decl->kind == cdk_attrs)
7180 inner_decl = inner_decl->declarator;
7181 if (!cxx11_attribute_p (attrs))
7182 {
7183 if (inner_decl->kind == cdk_id)
7184 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7185 else if (inner_decl->kind == cdk_function)
7186 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7187 else if (inner_decl->kind == cdk_array)
7188 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7189 }
7190 attrs = c_warn_type_attributes (attrs);
7191 returned_attrs = decl_attributes (&type,
7192 chainon (returned_attrs, attrs),
7193 attr_flags);
7194 break;
7195 }
7196 case cdk_array:
7197 {
7198 tree itype = NULL_TREE;
7199 tree size = declarator->u.array.dimen;
7200 /* The index is a signed object `sizetype' bits wide. */
7201 tree index_type = c_common_signed_type (sizetype);
7202
7203 array_ptr_quals = declarator->u.array.quals;
7204 array_ptr_attrs = declarator->u.array.attrs;
7205 array_parm_static = declarator->u.array.static_p;
7206 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7207
7208 declarator = declarator->declarator;
7209
7210 /* Check for some types that there cannot be arrays of. */
7211
7212 if (VOID_TYPE_P (type))
7213 {
7214 if (name)
7215 error_at (loc, "declaration of %qE as array of voids", name);
7216 else
7217 error_at (loc, "declaration of type name as array of voids");
7218 type = error_mark_node;
7219 }
7220
7221 if (TREE_CODE (type) == FUNCTION_TYPE)
7222 {
7223 if (name)
7224 error_at (loc, "declaration of %qE as array of functions",
7225 name);
7226 else
7227 error_at (loc, "declaration of type name as array of "
7228 "functions");
7229 type = error_mark_node;
7230 }
7231
7232 if (pedantic && !in_system_header_at (input_location)
7233 && flexible_array_type_p (type))
7234 pedwarn (loc, OPT_Wpedantic,
7235 "invalid use of structure with flexible array member");
7236
7237 if (size == error_mark_node)
7238 type = error_mark_node;
7239
7240 if (type == error_mark_node)
7241 continue;
7242
7243 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7244 {
7245 type = error_mark_node;
7246 continue;
7247 }
7248
7249 /* If size was specified, set ITYPE to a range-type for
7250 that size. Otherwise, ITYPE remains null. finish_decl
7251 may figure it out from an initial value. */
7252
7253 if (size)
7254 {
7255 bool size_maybe_const = true;
7256 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7257 && !TREE_OVERFLOW (size));
7258 bool this_size_varies = false;
7259
7260 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7261 lvalue. */
7262 STRIP_TYPE_NOPS (size);
7263
7264 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7265 {
7266 if (name)
7267 error_at (loc, "size of array %qE has non-integer type",
7268 name);
7269 else
7270 error_at (loc,
7271 "size of unnamed array has non-integer type");
7272 size = integer_one_node;
7273 size_int_const = true;
7274 }
7275 /* This can happen with enum forward declaration. */
7276 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7277 {
7278 if (name)
7279 error_at (loc, "size of array %qE has incomplete type",
7280 name);
7281 else
7282 error_at (loc, "size of unnamed array has incomplete "
7283 "type");
7284 size = integer_one_node;
7285 size_int_const = true;
7286 }
7287
7288 size = c_fully_fold (size, false, &size_maybe_const);
7289
7290 if (pedantic && size_maybe_const && integer_zerop (size))
7291 {
7292 if (name)
7293 pedwarn (loc, OPT_Wpedantic,
7294 "ISO C forbids zero-size array %qE", name);
7295 else
7296 pedwarn (loc, OPT_Wpedantic,
7297 "ISO C forbids zero-size array");
7298 }
7299
7300 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7301 {
7302 constant_expression_warning (size);
7303 if (tree_int_cst_sgn (size) < 0)
7304 {
7305 if (name)
7306 error_at (loc, "size of array %qE is negative", name);
7307 else
7308 error_at (loc, "size of unnamed array is negative");
7309 size = integer_one_node;
7310 size_int_const = true;
7311 }
7312 /* Handle a size folded to an integer constant but
7313 not an integer constant expression. */
7314 if (!size_int_const)
7315 {
7316 /* If this is a file scope declaration of an
7317 ordinary identifier, this is invalid code;
7318 diagnosing it here and not subsequently
7319 treating the type as variable-length avoids
7320 more confusing diagnostics later. */
7321 if ((decl_context == NORMAL || decl_context == FIELD)
7322 && current_scope == file_scope)
7323 pedwarn (input_location, 0,
7324 "variably modified %qE at file scope",
7325 name);
7326 else
7327 this_size_varies = size_varies = true;
7328 warn_variable_length_array (name, size);
7329 }
7330 }
7331 else if ((decl_context == NORMAL || decl_context == FIELD)
7332 && current_scope == file_scope)
7333 {
7334 error_at (loc, "variably modified %qE at file scope", name);
7335 size = integer_one_node;
7336 }
7337 else
7338 {
7339 /* Make sure the array size remains visibly
7340 nonconstant even if it is (eg) a const variable
7341 with known value. */
7342 this_size_varies = size_varies = true;
7343 warn_variable_length_array (name, size);
7344 if (sanitize_flags_p (SANITIZE_VLA)
7345 && current_function_decl != NULL_TREE
7346 && decl_context == NORMAL)
7347 {
7348 /* Evaluate the array size only once. */
7349 size = save_expr (size);
7350 size = c_fully_fold (size, false, NULL);
7351 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7352 ubsan_instrument_vla (loc, size),
7353 size);
7354 }
7355 }
7356
7357 if (integer_zerop (size) && !this_size_varies)
7358 {
7359 /* A zero-length array cannot be represented with
7360 an unsigned index type, which is what we'll
7361 get with build_index_type. Create an
7362 open-ended range instead. */
7363 itype = build_range_type (sizetype, size, NULL_TREE);
7364 }
7365 else
7366 {
7367 /* Arrange for the SAVE_EXPR on the inside of the
7368 MINUS_EXPR, which allows the -1 to get folded
7369 with the +1 that happens when building TYPE_SIZE. */
7370 if (size_varies)
7371 size = save_expr (size);
7372 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7373 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7374 integer_zero_node, size);
7375
7376 /* Compute the maximum valid index, that is, size
7377 - 1. Do the calculation in index_type, so that
7378 if it is a variable the computations will be
7379 done in the proper mode. */
7380 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7381 convert (index_type, size),
7382 convert (index_type,
7383 size_one_node));
7384
7385 /* The above overflows when size does not fit
7386 in index_type.
7387 ??? While a size of INT_MAX+1 technically shouldn't
7388 cause an overflow (because we subtract 1), handling
7389 this case seems like an unnecessary complication. */
7390 if (TREE_CODE (size) == INTEGER_CST
7391 && !int_fits_type_p (size, index_type))
7392 {
7393 if (name)
7394 error_at (loc, "size of array %qE is too large",
7395 name);
7396 else
7397 error_at (loc, "size of unnamed array is too large");
7398 type = error_mark_node;
7399 continue;
7400 }
7401
7402 itype = build_index_type (itype);
7403 }
7404 if (this_size_varies)
7405 {
7406 if (TREE_SIDE_EFFECTS (size))
7407 {
7408 if (*expr)
7409 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7410 *expr, size);
7411 else
7412 *expr = size;
7413 }
7414 *expr_const_operands &= size_maybe_const;
7415 }
7416 }
7417 else if (decl_context == FIELD)
7418 {
7419 bool flexible_array_member = false;
7420 if (array_parm_vla_unspec_p)
7421 /* Field names can in fact have function prototype
7422 scope so [*] is disallowed here through making
7423 the field variably modified, not through being
7424 something other than a declaration with function
7425 prototype scope. */
7426 size_varies = true;
7427 else
7428 {
7429 const struct c_declarator *t = declarator;
7430 while (t->kind == cdk_attrs)
7431 t = t->declarator;
7432 flexible_array_member = (t->kind == cdk_id);
7433 }
7434 if (flexible_array_member
7435 && !in_system_header_at (input_location))
7436 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7437 "support flexible array members");
7438
7439 /* ISO C99 Flexible array members are effectively
7440 identical to GCC's zero-length array extension. */
7441 if (flexible_array_member || array_parm_vla_unspec_p)
7442 itype = build_range_type (sizetype, size_zero_node,
7443 NULL_TREE);
7444 }
7445 else if (decl_context == PARM)
7446 {
7447 if (array_parm_vla_unspec_p)
7448 {
7449 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7450 size_varies = true;
7451 }
7452 }
7453 else if (decl_context == TYPENAME)
7454 {
7455 if (array_parm_vla_unspec_p)
7456 {
7457 /* C99 6.7.5.2p4 */
7458 warning (0, "%<[*]%> not in a declaration");
7459 /* We use this to avoid messing up with incomplete
7460 array types of the same type, that would
7461 otherwise be modified below. */
7462 itype = build_range_type (sizetype, size_zero_node,
7463 NULL_TREE);
7464 size_varies = true;
7465 }
7466 }
7467
7468 /* Complain about arrays of incomplete types. */
7469 if (!COMPLETE_TYPE_P (type))
7470 {
7471 auto_diagnostic_group d;
7472 error_at (loc, "array type has incomplete element type %qT",
7473 type);
7474 /* See if we can be more helpful. */
7475 if (TREE_CODE (type) == ARRAY_TYPE)
7476 {
7477 if (name)
7478 inform (loc, "declaration of %qE as multidimensional "
7479 "array must have bounds for all dimensions "
7480 "except the first", name);
7481 else
7482 inform (loc, "declaration of multidimensional array "
7483 "must have bounds for all dimensions except "
7484 "the first");
7485 }
7486 type = error_mark_node;
7487 }
7488 else
7489 /* When itype is NULL, a shared incomplete array type is
7490 returned for all array of a given type. Elsewhere we
7491 make sure we don't complete that type before copying
7492 it, but here we want to make sure we don't ever
7493 modify the shared type, so we gcc_assert (itype)
7494 below. */
7495 {
7496 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7497 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7498 type = build_qualified_type (type,
7499 ENCODE_QUAL_ADDR_SPACE (as));
7500
7501 type = build_array_type (type, itype);
7502 }
7503
7504 if (type != error_mark_node)
7505 {
7506 if (size_varies)
7507 {
7508 /* It is ok to modify type here even if itype is
7509 NULL: if size_varies, we're in a
7510 multi-dimensional array and the inner type has
7511 variable size, so the enclosing shared array type
7512 must too. */
7513 if (size && TREE_CODE (size) == INTEGER_CST)
7514 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7515 C_TYPE_VARIABLE_SIZE (type) = 1;
7516 }
7517
7518 /* The GCC extension for zero-length arrays differs from
7519 ISO flexible array members in that sizeof yields
7520 zero. */
7521 if (size && integer_zerop (size))
7522 {
7523 gcc_assert (itype);
7524 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7525 TYPE_SIZE (type) = bitsize_zero_node;
7526 TYPE_SIZE_UNIT (type) = size_zero_node;
7527 SET_TYPE_STRUCTURAL_EQUALITY (type);
7528 }
7529 if (array_parm_vla_unspec_p)
7530 {
7531 gcc_assert (itype);
7532 /* The type is complete. C99 6.7.5.2p4 */
7533 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7534 TYPE_SIZE (type) = bitsize_zero_node;
7535 TYPE_SIZE_UNIT (type) = size_zero_node;
7536 SET_TYPE_STRUCTURAL_EQUALITY (type);
7537 }
7538
7539 if (!valid_array_size_p (loc, type, name))
7540 type = error_mark_node;
7541 }
7542
7543 if (decl_context != PARM
7544 && (array_ptr_quals != TYPE_UNQUALIFIED
7545 || array_ptr_attrs != NULL_TREE
7546 || array_parm_static))
7547 {
7548 error_at (loc, "static or type qualifiers in non-parameter "
7549 "array declarator");
7550 array_ptr_quals = TYPE_UNQUALIFIED;
7551 array_ptr_attrs = NULL_TREE;
7552 array_parm_static = false;
7553 }
7554 orig_qual_indirect++;
7555 break;
7556 }
7557 case cdk_function:
7558 {
7559 /* Say it's a definition only for the declarator closest
7560 to the identifier, apart possibly from some
7561 attributes. */
7562 bool really_funcdef = false;
7563 tree arg_types;
7564 orig_qual_type = NULL_TREE;
7565 if (funcdef_flag)
7566 {
7567 const struct c_declarator *t = declarator->declarator;
7568 while (t->kind == cdk_attrs)
7569 t = t->declarator;
7570 really_funcdef = (t->kind == cdk_id);
7571 }
7572
7573 /* Declaring a function type. Make sure we have a valid
7574 type for the function to return. */
7575 if (type == error_mark_node)
7576 continue;
7577
7578 size_varies = false;
7579
7580 /* Warn about some types functions can't return. */
7581 if (TREE_CODE (type) == FUNCTION_TYPE)
7582 {
7583 if (name)
7584 error_at (loc, "%qE declared as function returning a "
7585 "function", name);
7586 else
7587 error_at (loc, "type name declared as function "
7588 "returning a function");
7589 type = integer_type_node;
7590 }
7591 if (TREE_CODE (type) == ARRAY_TYPE)
7592 {
7593 if (name)
7594 error_at (loc, "%qE declared as function returning an array",
7595 name);
7596 else
7597 error_at (loc, "type name declared as function returning "
7598 "an array");
7599 type = integer_type_node;
7600 }
7601
7602 /* Construct the function type and go to the next
7603 inner layer of declarator. */
7604 arg_info = declarator->u.arg_info;
7605 arg_types = grokparms (arg_info, really_funcdef);
7606
7607 /* Type qualifiers before the return type of the function
7608 qualify the return type, not the function type. */
7609 if (type_quals)
7610 {
7611 const enum c_declspec_word ignored_quals_list[] =
7612 {
7613 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7614 cdw_atomic, cdw_number_of_elements
7615 };
7616 location_t specs_loc
7617 = smallest_type_quals_location (declspecs->locations,
7618 ignored_quals_list);
7619 if (specs_loc == UNKNOWN_LOCATION)
7620 specs_loc = declspecs->locations[cdw_typedef];
7621 if (specs_loc == UNKNOWN_LOCATION)
7622 specs_loc = loc;
7623
7624 /* Type qualifiers on a function return type are
7625 normally permitted by the standard but have no
7626 effect, so give a warning at -Wreturn-type.
7627 Qualifiers on a void return type are banned on
7628 function definitions in ISO C; GCC used to used
7629 them for noreturn functions. The resolution of C11
7630 DR#423 means qualifiers (other than _Atomic) are
7631 actually removed from the return type when
7632 determining the function type. For C23, _Atomic is
7633 removed as well. */
7634 int quals_used = type_quals;
7635 if (flag_isoc23)
7636 quals_used = 0;
7637 else if (flag_isoc11)
7638 quals_used &= TYPE_QUAL_ATOMIC;
7639 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7640 pedwarn (specs_loc, 0,
7641 "function definition has qualified void "
7642 "return type");
7643 else
7644 warning_at (specs_loc, OPT_Wignored_qualifiers,
7645 "type qualifiers ignored on function "
7646 "return type");
7647
7648 /* Ensure an error for restrict on invalid types; the
7649 DR#423 resolution is not entirely clear about
7650 this. */
7651 if (flag_isoc11
7652 && (type_quals & TYPE_QUAL_RESTRICT)
7653 && (!POINTER_TYPE_P (type)
7654 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7655 error_at (loc, "invalid use of %<restrict%>");
7656 type = c_build_qualified_type (type, quals_used);
7657 }
7658 type_quals = TYPE_UNQUALIFIED;
7659
7660 type = build_function_type (type, arg_types,
7661 arg_info->no_named_args_stdarg_p);
7662 declarator = declarator->declarator;
7663
7664 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7665 the formal parameter list of this FUNCTION_TYPE to point to
7666 the FUNCTION_TYPE node itself. */
7667 {
7668 c_arg_tag *tag;
7669 unsigned ix;
7670
7671 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7672 TYPE_CONTEXT (tag->type) = type;
7673 }
7674 break;
7675 }
7676 case cdk_pointer:
7677 {
7678 /* Merge any constancy or volatility into the target type
7679 for the pointer. */
7680 if ((type_quals & TYPE_QUAL_ATOMIC)
7681 && TREE_CODE (type) == FUNCTION_TYPE)
7682 {
7683 error_at (loc,
7684 "%<_Atomic%>-qualified function type");
7685 type_quals &= ~TYPE_QUAL_ATOMIC;
7686 }
7687 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7688 && type_quals)
7689 pedwarn (loc, OPT_Wpedantic,
7690 "ISO C forbids qualified function types");
7691 if (type_quals)
7692 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7693 orig_qual_indirect);
7694 orig_qual_type = NULL_TREE;
7695 size_varies = false;
7696
7697 /* When the pointed-to type involves components of variable size,
7698 care must be taken to ensure that the size evaluation code is
7699 emitted early enough to dominate all the possible later uses
7700 and late enough for the variables on which it depends to have
7701 been assigned.
7702
7703 This is expected to happen automatically when the pointed-to
7704 type has a name/declaration of it's own, but special attention
7705 is required if the type is anonymous. */
7706 if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7707 {
7708 bool bind_p = decl_context == TYPENAME
7709 || decl_context == FIELD
7710 || decl_context == PARM;
7711 add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7712 }
7713
7714 type = c_build_pointer_type (type);
7715
7716 /* Process type qualifiers (such as const or volatile)
7717 that were given inside the `*'. */
7718 type_quals = declarator->u.pointer_quals;
7719
7720 declarator = declarator->declarator;
7721 break;
7722 }
7723 default:
7724 gcc_unreachable ();
7725 }
7726 if (type != error_mark_node)
7727 C_TYPE_VARIABLY_MODIFIED (type) = varmod || size_varies;
7728 }
7729 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7730 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7731
7732 /* Now TYPE has the actual type, apart from any qualifiers in
7733 TYPE_QUALS. */
7734
7735 /* Warn about address space used for things other than static memory or
7736 pointers. */
7737 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7738 if (!ADDR_SPACE_GENERIC_P (address_space))
7739 {
7740 if (decl_context == NORMAL)
7741 {
7742 switch (storage_class)
7743 {
7744 case csc_auto:
7745 error ("%qs combined with %<auto%> qualifier for %qE",
7746 c_addr_space_name (address_space), name);
7747 break;
7748 case csc_register:
7749 error ("%qs combined with %<register%> qualifier for %qE",
7750 c_addr_space_name (address_space), name);
7751 break;
7752 case csc_none:
7753 if (current_function_scope)
7754 {
7755 error ("%qs specified for auto variable %qE",
7756 c_addr_space_name (address_space), name);
7757 break;
7758 }
7759 break;
7760 case csc_static:
7761 case csc_extern:
7762 case csc_typedef:
7763 break;
7764 default:
7765 gcc_unreachable ();
7766 }
7767 }
7768 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7769 {
7770 if (name)
7771 error ("%qs specified for parameter %qE",
7772 c_addr_space_name (address_space), name);
7773 else
7774 error ("%qs specified for unnamed parameter",
7775 c_addr_space_name (address_space));
7776 }
7777 else if (decl_context == FIELD)
7778 {
7779 if (name)
7780 error ("%qs specified for structure field %qE",
7781 c_addr_space_name (address_space), name);
7782 else
7783 error ("%qs specified for structure field",
7784 c_addr_space_name (address_space));
7785 }
7786 }
7787
7788 /* Check the type and width of a bit-field. */
7789 if (bitfield)
7790 {
7791 check_bitfield_type_and_width (loc, &type, width, name);
7792 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7793 atomic types are permitted for bit-fields; we have no code to
7794 make bit-field accesses atomic, so disallow them. */
7795 if (type_quals & TYPE_QUAL_ATOMIC)
7796 {
7797 if (name)
7798 error_at (loc, "bit-field %qE has atomic type", name);
7799 else
7800 error_at (loc, "bit-field has atomic type");
7801 type_quals &= ~TYPE_QUAL_ATOMIC;
7802 }
7803 }
7804
7805 /* Reject invalid uses of _Alignas. */
7806 if (declspecs->alignas_p)
7807 {
7808 if (storage_class == csc_typedef)
7809 error_at (loc, "alignment specified for typedef %qE", name);
7810 else if (storage_class == csc_register)
7811 error_at (loc, "alignment specified for %<register%> object %qE",
7812 name);
7813 else if (decl_context == PARM)
7814 {
7815 if (name)
7816 error_at (loc, "alignment specified for parameter %qE", name);
7817 else
7818 error_at (loc, "alignment specified for unnamed parameter");
7819 }
7820 else if (bitfield)
7821 {
7822 if (name)
7823 error_at (loc, "alignment specified for bit-field %qE", name);
7824 else
7825 error_at (loc, "alignment specified for unnamed bit-field");
7826 }
7827 else if (TREE_CODE (type) == FUNCTION_TYPE)
7828 error_at (loc, "alignment specified for function %qE", name);
7829 else if (declspecs->align_log != -1 && TYPE_P (type))
7830 {
7831 alignas_align = 1U << declspecs->align_log;
7832 if (alignas_align < min_align_of_type (type))
7833 {
7834 if (name)
7835 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7836 "alignment of %qE", name);
7837 else
7838 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7839 "alignment of unnamed field");
7840 alignas_align = 0;
7841 }
7842 }
7843 }
7844
7845 /* If this is declaring a typedef name, return a TYPE_DECL. */
7846
7847 if (storage_class == csc_typedef)
7848 {
7849 tree decl;
7850 if ((type_quals & TYPE_QUAL_ATOMIC)
7851 && TREE_CODE (type) == FUNCTION_TYPE)
7852 {
7853 error_at (loc,
7854 "%<_Atomic%>-qualified function type");
7855 type_quals &= ~TYPE_QUAL_ATOMIC;
7856 }
7857 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7858 && type_quals)
7859 pedwarn (loc, OPT_Wpedantic,
7860 "ISO C forbids qualified function types");
7861 if (type_quals)
7862 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7863 orig_qual_indirect);
7864 decl = build_decl (declarator->id_loc,
7865 TYPE_DECL, declarator->u.id.id, type);
7866 if (declspecs->explicit_signed_p)
7867 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7868 if (declspecs->inline_p)
7869 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7870 if (declspecs->noreturn_p)
7871 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7872
7873 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7874 {
7875 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7876
7877 if (b != NULL
7878 && b->decl != NULL_TREE
7879 && (B_IN_CURRENT_SCOPE (b)
7880 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7881 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7882 {
7883 auto_diagnostic_group d;
7884 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7885 ("using %qD as both a typedef and a tag is "
7886 "invalid in C++"), decl)
7887 && b->locus != UNKNOWN_LOCATION)
7888 inform (b->locus, "originally defined here");
7889 }
7890 }
7891
7892 return decl;
7893 }
7894
7895 /* If this is a type name (such as, in a cast or sizeof),
7896 compute the type and return it now. */
7897
7898 if (decl_context == TYPENAME)
7899 {
7900 /* Note that the grammar rejects storage classes in typenames
7901 and fields. */
7902 gcc_assert (storage_class == csc_none && !threadp
7903 && !declspecs->inline_p && !declspecs->noreturn_p);
7904 if ((type_quals & TYPE_QUAL_ATOMIC)
7905 && TREE_CODE (type) == FUNCTION_TYPE)
7906 {
7907 error_at (loc,
7908 "%<_Atomic%>-qualified function type");
7909 type_quals &= ~TYPE_QUAL_ATOMIC;
7910 }
7911 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7912 && type_quals)
7913 pedwarn (loc, OPT_Wpedantic,
7914 "ISO C forbids const or volatile function types");
7915 if (type_quals)
7916 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7917 orig_qual_indirect);
7918 return type;
7919 }
7920
7921 if (pedantic && decl_context == FIELD
7922 && c_type_variably_modified_p (type))
7923 {
7924 /* C99 6.7.2.1p8 */
7925 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7926 "have a variably modified type");
7927 }
7928
7929 /* Aside from typedefs and type names (handle above),
7930 `void' at top level (not within pointer)
7931 is allowed only in public variables.
7932 We don't complain about parms either, but that is because
7933 a better error message can be made later. */
7934
7935 if (VOID_TYPE_P (type) && decl_context != PARM
7936 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7937 && (storage_class == csc_extern
7938 || (current_scope == file_scope
7939 && !(storage_class == csc_static
7940 || storage_class == csc_register)))))
7941 {
7942 error_at (loc, "variable or field %qE declared void", name);
7943 type = integer_type_node;
7944 }
7945
7946 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7947 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7948
7949 {
7950 tree decl;
7951
7952 if (decl_context == PARM)
7953 {
7954 tree promoted_type;
7955 bool array_parameter_p = false;
7956
7957 /* A parameter declared as an array of T is really a pointer to T.
7958 One declared as a function is really a pointer to a function. */
7959
7960 if (TREE_CODE (type) == ARRAY_TYPE)
7961 {
7962 /* Transfer const-ness of array into that of type pointed to. */
7963 type = TREE_TYPE (type);
7964 if (orig_qual_type != NULL_TREE)
7965 {
7966 if (orig_qual_indirect == 0)
7967 orig_qual_type = TREE_TYPE (orig_qual_type);
7968 else
7969 orig_qual_indirect--;
7970 }
7971 if (type_quals)
7972 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7973 orig_qual_indirect);
7974
7975 /* The pointed-to type may need a decl expr (see above). */
7976 if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7977 {
7978 bool bind_p = decl_context == TYPENAME
7979 || decl_context == FIELD
7980 || decl_context == PARM;
7981 add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7982 }
7983
7984 type = c_build_pointer_type (type);
7985 type_quals = array_ptr_quals;
7986 if (type_quals)
7987 type = c_build_qualified_type (type, type_quals);
7988
7989 /* We don't yet implement attributes in this context. */
7990 if (array_ptr_attrs != NULL_TREE)
7991 warning_at (loc, OPT_Wattributes,
7992 "attributes in parameter array declarator ignored");
7993
7994 size_varies = false;
7995 array_parameter_p = true;
7996 }
7997 else if (TREE_CODE (type) == FUNCTION_TYPE)
7998 {
7999 if (type_quals & TYPE_QUAL_ATOMIC)
8000 {
8001 error_at (loc,
8002 "%<_Atomic%>-qualified function type");
8003 type_quals &= ~TYPE_QUAL_ATOMIC;
8004 }
8005 else if (type_quals)
8006 pedwarn (loc, OPT_Wpedantic,
8007 "ISO C forbids qualified function types");
8008 if (type_quals)
8009 type = c_build_qualified_type (type, type_quals);
8010 type = c_build_pointer_type (type);
8011 type_quals = TYPE_UNQUALIFIED;
8012 }
8013 else if (type_quals)
8014 type = c_build_qualified_type (type, type_quals);
8015
8016 decl = build_decl (declarator->id_loc,
8017 PARM_DECL, declarator->u.id.id, type);
8018 if (size_varies)
8019 C_DECL_VARIABLE_SIZE (decl) = 1;
8020 C_ARRAY_PARAMETER (decl) = array_parameter_p;
8021
8022 /* Compute the type actually passed in the parmlist,
8023 for the case where there is no prototype.
8024 (For example, shorts and chars are passed as ints.)
8025 When there is a prototype, this is overridden later. */
8026
8027 if (type == error_mark_node)
8028 promoted_type = type;
8029 else
8030 promoted_type = c_type_promotes_to (type);
8031
8032 DECL_ARG_TYPE (decl) = promoted_type;
8033 if (declspecs->inline_p)
8034 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8035 if (declspecs->noreturn_p)
8036 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8037 }
8038 else if (decl_context == FIELD)
8039 {
8040 /* Note that the grammar rejects storage classes in typenames
8041 and fields. */
8042 gcc_assert (storage_class == csc_none && !threadp
8043 && !declspecs->inline_p && !declspecs->noreturn_p);
8044
8045 /* Structure field. It may not be a function. */
8046
8047 if (TREE_CODE (type) == FUNCTION_TYPE)
8048 {
8049 error_at (loc, "field %qE declared as a function", name);
8050 type = build_pointer_type (type);
8051 }
8052 else if (TREE_CODE (type) != ERROR_MARK
8053 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8054 {
8055 if (name)
8056 error_at (loc, "field %qE has incomplete type", name);
8057 else
8058 error_at (loc, "unnamed field has incomplete type");
8059 type = error_mark_node;
8060 }
8061 else if (TREE_CODE (type) == ARRAY_TYPE
8062 && TYPE_DOMAIN (type) == NULL_TREE)
8063 {
8064 /* We have a flexible array member through a typedef.
8065 Set suitable range. Whether this is a correct position
8066 for a flexible array member will be determined elsewhere. */
8067 if (!in_system_header_at (input_location))
8068 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8069 "support flexible array members");
8070 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8071 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8072 NULL_TREE);
8073 if (orig_qual_indirect == 0)
8074 orig_qual_type = NULL_TREE;
8075 }
8076 if (type != error_mark_node
8077 && !verify_type_context (loc, TCTX_FIELD, type))
8078 type = error_mark_node;
8079
8080 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8081 orig_qual_indirect);
8082 decl = build_decl (declarator->id_loc,
8083 FIELD_DECL, declarator->u.id.id, type);
8084 DECL_NONADDRESSABLE_P (decl) = bitfield;
8085 if (bitfield && !declarator->u.id.id)
8086 DECL_PADDING_P (decl) = 1;
8087
8088 if (size_varies)
8089 C_DECL_VARIABLE_SIZE (decl) = 1;
8090 }
8091 else if (TREE_CODE (type) == FUNCTION_TYPE)
8092 {
8093 if (storage_class == csc_register || threadp || constexprp)
8094 {
8095 error_at (loc, "invalid storage class for function %qE", name);
8096 }
8097 else if (current_scope != file_scope)
8098 {
8099 /* Function declaration not at file scope. Storage
8100 classes other than `extern' are not allowed, C99
8101 6.7.1p5, and `extern' makes no difference. However,
8102 GCC allows 'auto', perhaps with 'inline', to support
8103 nested functions. */
8104 if (storage_class == csc_auto)
8105 pedwarn (loc, OPT_Wpedantic,
8106 "invalid storage class for function %qE", name);
8107 else if (storage_class == csc_static)
8108 {
8109 error_at (loc, "invalid storage class for function %qE", name);
8110 if (funcdef_flag)
8111 storage_class = declspecs->storage_class = csc_none;
8112 else
8113 return NULL_TREE;
8114 }
8115 }
8116
8117 decl = build_decl (declarator->id_loc,
8118 FUNCTION_DECL, declarator->u.id.id, type);
8119 decl = build_decl_attribute_variant (decl, decl_attr);
8120
8121 if (type_quals & TYPE_QUAL_ATOMIC)
8122 {
8123 error_at (loc,
8124 "%<_Atomic%>-qualified function type");
8125 type_quals &= ~TYPE_QUAL_ATOMIC;
8126 }
8127 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8128 pedwarn (loc, OPT_Wpedantic,
8129 "ISO C forbids qualified function types");
8130
8131 /* Every function declaration is an external reference
8132 (DECL_EXTERNAL) except for those which are not at file
8133 scope and are explicitly declared "auto". This is
8134 forbidden by standard C (C99 6.7.1p5) and is interpreted by
8135 GCC to signify a forward declaration of a nested function. */
8136 if (storage_class == csc_auto && current_scope != file_scope)
8137 DECL_EXTERNAL (decl) = 0;
8138 /* In C99, a function which is declared 'inline' with 'extern'
8139 is not an external reference (which is confusing). It
8140 means that the later definition of the function must be output
8141 in this file, C99 6.7.4p6. In GNU C89, a function declared
8142 'extern inline' is an external reference. */
8143 else if (declspecs->inline_p && storage_class != csc_static)
8144 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8145 == flag_gnu89_inline);
8146 else
8147 DECL_EXTERNAL (decl) = !initialized;
8148
8149 /* Record absence of global scope for `static' or `auto'. */
8150 TREE_PUBLIC (decl)
8151 = !(storage_class == csc_static || storage_class == csc_auto);
8152
8153 /* For a function definition, record the argument information
8154 block where store_parm_decls will look for it. */
8155 if (funcdef_flag)
8156 current_function_arg_info = arg_info;
8157
8158 if (declspecs->default_int_p)
8159 C_FUNCTION_IMPLICIT_INT (decl) = 1;
8160
8161 /* Record presence of `inline' and `_Noreturn', if it is
8162 reasonable. */
8163 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8164 {
8165 if (declspecs->inline_p)
8166 pedwarn (loc, 0, "cannot inline function %<main%>");
8167 if (declspecs->noreturn_p)
8168 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8169 }
8170 else
8171 {
8172 if (declspecs->inline_p)
8173 /* Record that the function is declared `inline'. */
8174 DECL_DECLARED_INLINE_P (decl) = 1;
8175 if (declspecs->noreturn_p)
8176 {
8177 if (flag_isoc99)
8178 pedwarn_c99 (loc, OPT_Wpedantic,
8179 "ISO C99 does not support %<_Noreturn%>");
8180 else
8181 pedwarn_c99 (loc, OPT_Wpedantic,
8182 "ISO C90 does not support %<_Noreturn%>");
8183 TREE_THIS_VOLATILE (decl) = 1;
8184 }
8185 }
8186
8187 /* C99 6.2.2p7: It is invalid (compile-time undefined
8188 behavior) to create an 'extern' declaration for a
8189 function if there is a global declaration that is
8190 'static' and the global declaration is not visible.
8191 (If the static declaration _is_ currently visible,
8192 the 'extern' declaration is taken to refer to that decl.) */
8193 if (!initialized
8194 && TREE_PUBLIC (decl)
8195 && current_scope != file_scope)
8196 {
8197 tree global_decl = identifier_global_value (declarator->u.id.id);
8198 tree visible_decl = lookup_name (declarator->u.id.id);
8199
8200 if (global_decl
8201 && global_decl != visible_decl
8202 && VAR_OR_FUNCTION_DECL_P (global_decl)
8203 && !TREE_PUBLIC (global_decl))
8204 error_at (loc, "function previously declared %<static%> "
8205 "redeclared %<extern%>");
8206 }
8207 }
8208 else
8209 {
8210 /* It's a variable. */
8211 /* An uninitialized decl with `extern' is a reference. */
8212 int extern_ref = !initialized && storage_class == csc_extern;
8213
8214 if (constexprp)
8215 {
8216 /* The type of a constexpr variable must not be variably
8217 modified, volatile, atomic or restrict qualified or
8218 have a member with such a qualifier. const
8219 qualification is implicitly added, and, at file scope,
8220 has internal linkage. */
8221 if (c_type_variably_modified_p (type))
8222 error_at (loc, "%<constexpr%> object has variably modified "
8223 "type");
8224 if (type_quals
8225 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8226 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8227 else
8228 {
8229 tree type_no_array = strip_array_types (type);
8230 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8231 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8232 error_at (loc, "invalid qualifiers for field of "
8233 "%<constexpr%> object");
8234 }
8235 type_quals |= TYPE_QUAL_CONST;
8236 if (current_scope == file_scope)
8237 storage_class = csc_static;
8238 }
8239
8240 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8241 orig_qual_indirect);
8242
8243 /* C99 6.2.2p7: It is invalid (compile-time undefined
8244 behavior) to create an 'extern' declaration for a
8245 variable if there is a global declaration that is
8246 'static' and the global declaration is not visible.
8247 (If the static declaration _is_ currently visible,
8248 the 'extern' declaration is taken to refer to that decl.) */
8249 if (extern_ref && current_scope != file_scope)
8250 {
8251 tree global_decl = identifier_global_value (declarator->u.id.id);
8252 tree visible_decl = lookup_name (declarator->u.id.id);
8253
8254 if (global_decl
8255 && global_decl != visible_decl
8256 && VAR_P (global_decl)
8257 && !TREE_PUBLIC (global_decl))
8258 error_at (loc, "variable previously declared %<static%> "
8259 "redeclared %<extern%>");
8260 }
8261
8262 decl = build_decl (declarator->id_loc,
8263 VAR_DECL, declarator->u.id.id, type);
8264 if (size_varies)
8265 C_DECL_VARIABLE_SIZE (decl) = 1;
8266 if (constexprp)
8267 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8268
8269 if (declspecs->inline_p)
8270 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8271 if (declspecs->noreturn_p)
8272 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8273
8274 /* At file scope, an initialized extern declaration may follow
8275 a static declaration. In that case, DECL_EXTERNAL will be
8276 reset later in start_decl. */
8277 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8278
8279 /* At file scope, the presence of a `static' or `register' storage
8280 class specifier, or the absence of all storage class specifiers
8281 makes this declaration a definition (perhaps tentative). Also,
8282 the absence of `static' makes it public. */
8283 if (current_scope == file_scope)
8284 {
8285 TREE_PUBLIC (decl) = storage_class != csc_static;
8286 TREE_STATIC (decl) = !extern_ref;
8287 }
8288 /* Not at file scope, only `static' makes a static definition. */
8289 else
8290 {
8291 TREE_STATIC (decl) = (storage_class == csc_static);
8292 TREE_PUBLIC (decl) = extern_ref;
8293 }
8294
8295 if (threadp)
8296 set_decl_tls_model (decl, decl_default_tls_model (decl));
8297 }
8298
8299 if ((storage_class == csc_extern
8300 || (storage_class == csc_none
8301 && TREE_CODE (type) == FUNCTION_TYPE
8302 && !funcdef_flag))
8303 && c_type_variably_modified_p (type))
8304 {
8305 /* C99 6.7.5.2p2 */
8306 if (TREE_CODE (type) == FUNCTION_TYPE)
8307 error_at (loc, "non-nested function with variably modified type");
8308 else
8309 error_at (loc, "object with variably modified type must have "
8310 "no linkage");
8311 }
8312
8313 /* For nested functions disqualify ones taking VLAs by value
8314 from inlining since the middle-end cannot deal with this.
8315 ??? We should arrange for those to be passed by reference
8316 with emitting the copy on the caller side in the frontend. */
8317 if (storage_class == csc_none
8318 && TREE_CODE (type) == FUNCTION_TYPE)
8319 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8320 {
8321 tree arg = TREE_VALUE (al);
8322 if (arg != error_mark_node
8323 && C_TYPE_VARIABLE_SIZE (arg))
8324 {
8325 DECL_UNINLINABLE (decl) = 1;
8326 break;
8327 }
8328 }
8329
8330 /* Record `register' declaration for warnings on &
8331 and in case doing stupid register allocation. */
8332
8333 if (storage_class == csc_register)
8334 {
8335 C_DECL_REGISTER (decl) = 1;
8336 DECL_REGISTER (decl) = 1;
8337 }
8338
8339 /* Record constancy and volatility. */
8340 c_apply_type_quals_to_decl (type_quals, decl);
8341
8342 /* Apply _Alignas specifiers. */
8343 if (alignas_align)
8344 {
8345 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8346 DECL_USER_ALIGN (decl) = 1;
8347 }
8348
8349 /* If a type has volatile components, it should be stored in memory.
8350 Otherwise, the fact that those components are volatile
8351 will be ignored, and would even crash the compiler.
8352 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8353 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8354 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8355 || TREE_CODE (decl) == RESULT_DECL))
8356 {
8357 /* It is not an error for a structure with volatile fields to
8358 be declared register, but reset DECL_REGISTER since it
8359 cannot actually go in a register. */
8360 int was_reg = C_DECL_REGISTER (decl);
8361 C_DECL_REGISTER (decl) = 0;
8362 DECL_REGISTER (decl) = 0;
8363 c_mark_addressable (decl);
8364 C_DECL_REGISTER (decl) = was_reg;
8365 }
8366
8367 /* This is the earliest point at which we might know the assembler
8368 name of a variable. Thus, if it's known before this, die horribly. */
8369 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8370 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8371
8372 if (warn_cxx_compat
8373 && VAR_P (decl)
8374 && TREE_PUBLIC (decl)
8375 && TREE_STATIC (decl)
8376 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8377 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8378 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8379 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8380 ("non-local variable %qD with anonymous type is "
8381 "questionable in C++"),
8382 decl);
8383
8384 return decl;
8385 }
8386 }
8387 \f
8388 /* Decode the parameter-list info for a function type or function definition.
8389 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8390 if there is an identifier list instead of a parameter decl list).
8391 These two functions are separate because when a function returns
8392 or receives functions then each is called multiple times but the order
8393 of calls is different. The last call to `grokparms' is always the one
8394 that contains the formal parameter names of a function definition.
8395
8396 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8397
8398 FUNCDEF_FLAG is true for a function definition, false for
8399 a mere declaration. A nonempty identifier-list gets an error message
8400 when FUNCDEF_FLAG is false. */
8401
8402 static tree
8403 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8404 {
8405 tree arg_types = arg_info->types;
8406
8407 if (funcdef_flag && arg_info->had_vla_unspec)
8408 {
8409 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8410 /* C99 6.7.5.2p4 */
8411 error ("%<[*]%> not allowed in other than function prototype scope");
8412 }
8413
8414 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8415 && !in_system_header_at (input_location))
8416 warning (OPT_Wstrict_prototypes,
8417 "function declaration isn%'t a prototype");
8418
8419 if (arg_types == error_mark_node)
8420 /* Don't set TYPE_ARG_TYPES in this case. */
8421 return NULL_TREE;
8422
8423 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8424 {
8425 if (!funcdef_flag)
8426 {
8427 permerror_opt (input_location,
8428 OPT_Wdeclaration_missing_parameter_type,
8429 "parameter names (without types) in "
8430 "function declaration");
8431 arg_info->parms = NULL_TREE;
8432 }
8433 else
8434 arg_info->parms = arg_info->types;
8435
8436 arg_info->types = NULL_TREE;
8437 return NULL_TREE;
8438 }
8439 else
8440 {
8441 tree parm, type, typelt;
8442 unsigned int parmno;
8443
8444 /* In C23, convert () to (void). */
8445 if (flag_isoc23
8446 && !arg_types
8447 && !arg_info->parms
8448 && !arg_info->no_named_args_stdarg_p)
8449 arg_types = arg_info->types = void_list_node;
8450
8451 /* If there is a parameter of incomplete type in a definition,
8452 this is an error. In a declaration this is valid, and a
8453 struct or union type may be completed later, before any calls
8454 or definition of the function. In the case where the tag was
8455 first declared within the parameter list, a warning has
8456 already been given. If a parameter has void type, then
8457 however the function cannot be defined or called, so
8458 warn. */
8459
8460 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8461 parm;
8462 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8463 {
8464 type = TREE_VALUE (typelt);
8465 if (type == error_mark_node)
8466 continue;
8467
8468 if (!COMPLETE_TYPE_P (type))
8469 {
8470 if (funcdef_flag)
8471 {
8472 if (DECL_NAME (parm))
8473 error_at (input_location,
8474 "parameter %u (%q+D) has incomplete type",
8475 parmno, parm);
8476 else
8477 error_at (DECL_SOURCE_LOCATION (parm),
8478 "parameter %u has incomplete type",
8479 parmno);
8480
8481 TREE_VALUE (typelt) = error_mark_node;
8482 TREE_TYPE (parm) = error_mark_node;
8483 arg_types = NULL_TREE;
8484 }
8485 else if (VOID_TYPE_P (type))
8486 {
8487 if (DECL_NAME (parm))
8488 warning_at (input_location, 0,
8489 "parameter %u (%q+D) has void type",
8490 parmno, parm);
8491 else
8492 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8493 "parameter %u has void type",
8494 parmno);
8495 }
8496 }
8497
8498 if (DECL_NAME (parm) && TREE_USED (parm))
8499 warn_if_shadowing (parm);
8500 }
8501 return arg_types;
8502 }
8503 }
8504
8505 /* Allocate and initialize a c_arg_info structure from the parser's
8506 obstack. */
8507
8508 struct c_arg_info *
8509 build_arg_info (void)
8510 {
8511 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8512 ret->parms = NULL_TREE;
8513 ret->tags = NULL;
8514 ret->types = NULL_TREE;
8515 ret->others = NULL_TREE;
8516 ret->pending_sizes = NULL;
8517 ret->had_vla_unspec = 0;
8518 ret->no_named_args_stdarg_p = 0;
8519 return ret;
8520 }
8521
8522 /* Take apart the current scope and return a c_arg_info structure with
8523 info on a parameter list just parsed.
8524
8525 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8526
8527 ELLIPSIS being true means the argument list ended in '...' so don't
8528 append a sentinel (void_list_node) to the end of the type-list.
8529
8530 EXPR is NULL or an expression that needs to be evaluated for the
8531 side effects of array size expressions in the parameters. */
8532
8533 struct c_arg_info *
8534 get_parm_info (bool ellipsis, tree expr)
8535 {
8536 struct c_binding *b = current_scope->bindings;
8537 struct c_arg_info *arg_info = build_arg_info ();
8538
8539 tree parms = NULL_TREE;
8540 vec<c_arg_tag, va_gc> *tags = NULL;
8541 tree types = NULL_TREE;
8542 tree others = NULL_TREE;
8543
8544 bool gave_void_only_once_err = false;
8545
8546 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8547
8548 /* The bindings in this scope must not get put into a block.
8549 We will take care of deleting the binding nodes. */
8550 current_scope->bindings = 0;
8551
8552 /* This function is only called if there was *something* on the
8553 parameter list. */
8554 gcc_assert (b);
8555
8556 /* A parameter list consisting solely of 'void' indicates that the
8557 function takes no arguments. But if the 'void' is qualified
8558 (by 'const' or 'volatile'), or has a storage class specifier
8559 ('register'), then the behavior is undefined; issue an error.
8560 Typedefs for 'void' are OK (see DR#157). */
8561 if (b->prev == 0 /* one binding */
8562 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8563 && !DECL_NAME (b->decl) /* anonymous */
8564 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8565 {
8566 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8567 || C_DECL_REGISTER (b->decl))
8568 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8569
8570 /* There cannot be an ellipsis. */
8571 if (ellipsis)
8572 error_at (b->locus, "%<void%> must be the only parameter");
8573
8574 arg_info->types = void_list_node;
8575 return arg_info;
8576 }
8577
8578 if (!ellipsis)
8579 types = void_list_node;
8580
8581 /* Break up the bindings list into parms, tags, types, and others;
8582 apply sanity checks; purge the name-to-decl bindings. */
8583 while (b)
8584 {
8585 tree decl = b->decl;
8586 tree type = TREE_TYPE (decl);
8587 c_arg_tag tag;
8588 const char *keyword;
8589
8590 switch (TREE_CODE (decl))
8591 {
8592 case PARM_DECL:
8593 if (b->id)
8594 {
8595 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8596 I_SYMBOL_BINDING (b->id) = b->shadowed;
8597 }
8598
8599 /* Check for forward decls that never got their actual decl. */
8600 if (TREE_ASM_WRITTEN (decl))
8601 error_at (b->locus,
8602 "parameter %q+D has just a forward declaration", decl);
8603 /* Check for (..., void, ...) and issue an error. */
8604 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8605 {
8606 if (!gave_void_only_once_err)
8607 {
8608 error_at (b->locus, "%<void%> must be the only parameter");
8609 gave_void_only_once_err = true;
8610 }
8611 }
8612 else
8613 {
8614 /* Valid parameter, add it to the list. */
8615 DECL_CHAIN (decl) = parms;
8616 parms = decl;
8617
8618 /* Since there is a prototype, args are passed in their
8619 declared types. The back end may override this later. */
8620 DECL_ARG_TYPE (decl) = type;
8621 types = tree_cons (0, type, types);
8622 }
8623 break;
8624
8625 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8626 case UNION_TYPE: keyword = "union"; goto tag;
8627 case RECORD_TYPE: keyword = "struct"; goto tag;
8628 tag:
8629 /* Types may not have tag-names, in which case the type
8630 appears in the bindings list with b->id NULL. */
8631 if (b->id)
8632 {
8633 gcc_assert (I_TAG_BINDING (b->id) == b);
8634 I_TAG_BINDING (b->id) = b->shadowed;
8635 }
8636
8637 /* Warn about any struct, union or enum tags defined in a
8638 parameter list. The scope of such types is limited to
8639 the parameter list, which is rarely if ever desirable
8640 (it's impossible to call such a function with type-
8641 correct arguments). An anonymous union parm type is
8642 meaningful as a GNU extension, so don't warn for that. */
8643 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8644 {
8645 if (b->id)
8646 {
8647 /* The %s will be one of 'struct', 'union', or 'enum'. */
8648 if (!flag_isoc23)
8649 warning_at (b->locus, 0,
8650 "%<%s %E%> declared inside parameter list"
8651 " will not be visible outside of this definition or"
8652 " declaration", keyword, b->id);
8653 }
8654 else
8655 /* The %s will be one of 'struct', 'union', or 'enum'. */
8656 warning_at (b->locus, 0,
8657 "anonymous %s declared inside parameter list"
8658 " will not be visible outside of this definition or"
8659 " declaration", keyword);
8660 }
8661
8662 tag.id = b->id;
8663 tag.type = decl;
8664 vec_safe_push (tags, tag);
8665 break;
8666
8667 case FUNCTION_DECL:
8668 /* FUNCTION_DECLs appear when there is an implicit function
8669 declaration in the parameter list. */
8670 gcc_assert (b->nested || seen_error ());
8671 goto set_shadowed;
8672
8673 case CONST_DECL:
8674 case TYPE_DECL:
8675 /* CONST_DECLs appear here when we have an embedded enum,
8676 and TYPE_DECLs appear here when we have an embedded struct
8677 or union. No warnings for this - we already warned about the
8678 type itself. */
8679
8680 /* When we reinsert this decl in the function body, we need
8681 to reconstruct whether it was marked as nested. */
8682 gcc_assert (!b->nested);
8683 DECL_CHAIN (decl) = others;
8684 others = decl;
8685 /* fall through */
8686
8687 case ERROR_MARK:
8688 set_shadowed:
8689 /* error_mark_node appears here when we have an undeclared
8690 variable. Just throw it away. */
8691 if (b->id)
8692 {
8693 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8694 I_SYMBOL_BINDING (b->id) = b->shadowed;
8695 }
8696 break;
8697
8698 /* Other things that might be encountered. */
8699 case LABEL_DECL:
8700 case VAR_DECL:
8701 default:
8702 gcc_unreachable ();
8703 }
8704
8705 b = free_binding_and_advance (b);
8706 }
8707
8708 arg_info->parms = parms;
8709 arg_info->tags = tags;
8710 arg_info->types = types;
8711 arg_info->others = others;
8712 arg_info->pending_sizes = expr;
8713 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8714 return arg_info;
8715 }
8716 \f
8717 /* Get the struct, enum or union (CODE says which) with tag NAME.
8718 Define the tag as a forward-reference with location LOC if it is
8719 not defined. HAVE_STD_ATTRS says whether any standard attributes
8720 were present after the struct, union or enum keyword; ATTRS are the
8721 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8722 whether an enum type specifier (": specifier-qualifier-list") is
8723 present; if so, this is called before that specifier is parsed, so
8724 that the tag is in scope for that specifier. Return a c_typespec
8725 structure for the type specifier. */
8726
8727 struct c_typespec
8728 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8729 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8730 {
8731 struct c_typespec ret;
8732 tree ref;
8733 location_t refloc;
8734
8735 ret.expr = NULL_TREE;
8736 ret.expr_const_operands = true;
8737 ret.has_enum_type_specifier = has_enum_type_specifier;
8738
8739 /* If a cross reference is requested, look up the type already
8740 defined for this tag and return it. If an enum type specifier is
8741 present, only a definition in the current scope is relevant. */
8742
8743 ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8744
8745 /* If the visble type is still being defined, see if there is
8746 an earlier definition (which may be complete). We do not
8747 have to loop because nested redefinitions are not allowed. */
8748 if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8749 {
8750 tree vis = previous_tag (ref);
8751 if (vis)
8752 ref = vis;
8753 }
8754
8755 /* If this is the right type of tag, return what we found.
8756 (This reference will be shadowed by shadow_tag later if appropriate.)
8757 If this is the wrong type of tag, do not return it. If it was the
8758 wrong type in the same scope, we will have had an error
8759 message already; if in a different scope and declaring
8760 a name, pending_xref_error will give an error message; but if in a
8761 different scope and not declaring a name, this tag should
8762 shadow the previous declaration of a different type of tag, and
8763 this would not work properly if we return the reference found.
8764 (For example, with "struct foo" in an outer scope, "union foo;"
8765 must shadow that tag with a new one of union type.) */
8766 ret.kind = (ref
8767 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8768 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8769 if (ref && TREE_CODE (ref) == code)
8770 {
8771 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8772 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8773 && loc != UNKNOWN_LOCATION
8774 && warn_cxx_compat)
8775 {
8776 auto_diagnostic_group d;
8777 switch (code)
8778 {
8779 case ENUMERAL_TYPE:
8780 if (warning_at (loc, OPT_Wc___compat,
8781 ("enum type defined in struct or union "
8782 "is not visible in C++")))
8783 inform (refloc, "enum type defined here");
8784 break;
8785 case RECORD_TYPE:
8786 if (warning_at (loc, OPT_Wc___compat,
8787 ("struct defined in struct or union "
8788 "is not visible in C++")))
8789 inform (refloc, "struct defined here");
8790 break;
8791 case UNION_TYPE:
8792 if (warning_at (loc, OPT_Wc___compat,
8793 ("union defined in struct or union "
8794 "is not visible in C++")))
8795 inform (refloc, "union defined here");
8796 break;
8797 default:
8798 gcc_unreachable();
8799 }
8800 }
8801
8802 ret.spec = ref;
8803 return ret;
8804 }
8805
8806 /* If no such tag is yet defined, create a forward-reference node
8807 and record it as the "definition".
8808 When a real declaration of this type is found,
8809 the forward-reference will be altered into a real type. */
8810
8811 ref = make_node (code);
8812 if (code == ENUMERAL_TYPE)
8813 {
8814 /* Give the type a default layout like unsigned int
8815 to avoid crashing if it does not get defined. */
8816 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8817 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8818 TYPE_USER_ALIGN (ref) = 0;
8819 TYPE_UNSIGNED (ref) = 1;
8820 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8821 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8822 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8823 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8824 }
8825
8826 pushtag (loc, name, ref);
8827 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8828 if (in_underspecified_init)
8829 error_at (loc, "%qT declared in underspecified object initializer",
8830 ref);
8831
8832 ret.spec = ref;
8833 return ret;
8834 }
8835
8836 /* Get the struct, enum or union (CODE says which) with tag NAME.
8837 Define the tag as a forward-reference if it is not defined.
8838 Return a tree for the type. */
8839
8840 tree
8841 xref_tag (enum tree_code code, tree name)
8842 {
8843 return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8844 false).spec;
8845 }
8846 \f
8847 /* Make sure that the tag NAME is defined *in the current scope*
8848 at least as a forward reference.
8849 LOC is the location of the struct's definition.
8850 CODE says which kind of tag NAME ought to be.
8851
8852 This stores the current value of the file static STRUCT_PARSE_INFO
8853 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8854 new c_struct_parse_info structure. The old value of
8855 STRUCT_PARSE_INFO is restored in finish_struct. */
8856
8857 tree
8858 start_struct (location_t loc, enum tree_code code, tree name,
8859 class c_struct_parse_info **enclosing_struct_parse_info)
8860 {
8861 /* If there is already a tag defined at this scope
8862 (as a forward reference), just return it. */
8863
8864 tree ref = NULL_TREE;
8865 location_t refloc = UNKNOWN_LOCATION;
8866
8867 if (name != NULL_TREE)
8868 ref = lookup_tag (code, name, true, &refloc);
8869
8870 /* For C23, even if we already have a completed definition,
8871 we do not use it. We will check for consistency later.
8872 If we are in a nested redefinition the type is not
8873 complete. We will then detect this below. */
8874 if (flag_isoc23 && ref && TYPE_SIZE (ref))
8875 ref = NULL_TREE;
8876
8877 if (ref && TREE_CODE (ref) == code)
8878 {
8879 if (TYPE_STUB_DECL (ref))
8880 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8881
8882 if (TYPE_SIZE (ref))
8883 {
8884 auto_diagnostic_group d;
8885 if (code == UNION_TYPE)
8886 error_at (loc, "redefinition of %<union %E%>", name);
8887 else
8888 error_at (loc, "redefinition of %<struct %E%>", name);
8889 if (refloc != UNKNOWN_LOCATION)
8890 inform (refloc, "originally defined here");
8891 /* Don't create structures using a name already in use. */
8892 ref = NULL_TREE;
8893 }
8894 else if (C_TYPE_BEING_DEFINED (ref))
8895 {
8896 if (code == UNION_TYPE)
8897 error_at (loc, "nested redefinition of %<union %E%>", name);
8898 else
8899 error_at (loc, "nested redefinition of %<struct %E%>", name);
8900 /* Don't bother to report "originally defined here" for a
8901 nested redefinition; the original definition should be
8902 obvious. */
8903 /* Don't create structures that contain themselves. */
8904 ref = NULL_TREE;
8905 }
8906 }
8907
8908 /* Otherwise create a forward-reference just so the tag is in scope. */
8909
8910 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8911 {
8912 ref = make_node (code);
8913 pushtag (loc, name, ref);
8914 }
8915
8916 C_TYPE_BEING_DEFINED (ref) = 1;
8917 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8918 TYPE_PACKED (v) = flag_pack_struct;
8919
8920 *enclosing_struct_parse_info = struct_parse_info;
8921 struct_parse_info = new c_struct_parse_info ();
8922
8923 /* FIXME: This will issue a warning for a use of a type defined
8924 within a statement expr used within sizeof, et. al. This is not
8925 terribly serious as C++ doesn't permit statement exprs within
8926 sizeof anyhow. */
8927 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8928 warning_at (loc, OPT_Wc___compat,
8929 "defining type in %qs expression is invalid in C++",
8930 (in_sizeof
8931 ? "sizeof"
8932 : (in_typeof ? "typeof" : "alignof")));
8933
8934 if (in_underspecified_init)
8935 error_at (loc, "%qT defined in underspecified object initializer", ref);
8936
8937 return ref;
8938 }
8939
8940 /* Process the specs, declarator and width (NULL if omitted)
8941 of a structure component, returning a FIELD_DECL node.
8942 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8943 DECL_ATTRS is as for grokdeclarator.
8944
8945 LOC is the location of the structure component.
8946
8947 This is done during the parsing of the struct declaration.
8948 The FIELD_DECL nodes are chained together and the lot of them
8949 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8950
8951 tree
8952 grokfield (location_t loc,
8953 struct c_declarator *declarator, struct c_declspecs *declspecs,
8954 tree width, tree *decl_attrs, tree *expr)
8955 {
8956 tree value;
8957
8958 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8959 && width == NULL_TREE)
8960 {
8961 /* This is an unnamed decl.
8962
8963 If we have something of the form "union { list } ;" then this
8964 is the anonymous union extension. Similarly for struct.
8965
8966 If this is something of the form "struct foo;", then
8967 If MS or Plan 9 extensions are enabled, this is handled as
8968 an anonymous struct.
8969 Otherwise this is a forward declaration of a structure tag.
8970
8971 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8972 If foo names a structure or union without a tag, then this
8973 is an anonymous struct (this is permitted by C11).
8974 If MS or Plan 9 extensions are enabled and foo names a
8975 structure, then again this is an anonymous struct.
8976 Otherwise this is an error.
8977
8978 Oh what a horrid tangled web we weave. I wonder if MS consciously
8979 took this from Plan 9 or if it was an accident of implementation
8980 that took root before someone noticed the bug... */
8981
8982 tree type = declspecs->type;
8983 bool ok = false;
8984
8985 if (RECORD_OR_UNION_TYPE_P (type)
8986 && (flag_ms_extensions
8987 || flag_plan9_extensions
8988 || !declspecs->typedef_p))
8989 {
8990 if (flag_ms_extensions || flag_plan9_extensions)
8991 ok = true;
8992 else if (TYPE_NAME (type) == NULL)
8993 ok = true;
8994 else
8995 ok = false;
8996 }
8997 if (!ok)
8998 {
8999 pedwarn (loc, 0, "declaration does not declare anything");
9000 return NULL_TREE;
9001 }
9002 if (flag_isoc99)
9003 pedwarn_c99 (loc, OPT_Wpedantic,
9004 "ISO C99 doesn%'t support unnamed structs/unions");
9005 else
9006 pedwarn_c99 (loc, OPT_Wpedantic,
9007 "ISO C90 doesn%'t support unnamed structs/unions");
9008 }
9009
9010 value = grokdeclarator (declarator, declspecs, FIELD, false,
9011 width ? &width : NULL, decl_attrs, expr, NULL,
9012 DEPRECATED_NORMAL);
9013
9014 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9015 DECL_INITIAL (value) = width;
9016 if (width)
9017 SET_DECL_C_BIT_FIELD (value);
9018
9019 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9020 {
9021 /* If we currently have a binding for this field, set the
9022 in_struct field in the binding, so that we warn about lookups
9023 which find it. */
9024 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9025 if (b != NULL)
9026 {
9027 /* If the in_struct field is not yet set, push it on a list
9028 to be cleared when this struct is finished. */
9029 if (!b->in_struct)
9030 {
9031 struct_parse_info->fields.safe_push (b);
9032 b->in_struct = 1;
9033 }
9034 }
9035 }
9036
9037 return value;
9038 }
9039 \f
9040 /* Subroutine of detect_field_duplicates: return whether X and Y,
9041 which are both fields in the same struct, have duplicate field
9042 names. */
9043
9044 static bool
9045 is_duplicate_field (tree x, tree y)
9046 {
9047 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9048 return true;
9049
9050 /* When using -fplan9-extensions, an anonymous field whose name is a
9051 typedef can duplicate a field name. */
9052 if (flag_plan9_extensions
9053 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9054 {
9055 tree xt, xn, yt, yn;
9056
9057 xt = TREE_TYPE (x);
9058 if (DECL_NAME (x) != NULL_TREE)
9059 xn = DECL_NAME (x);
9060 else if (RECORD_OR_UNION_TYPE_P (xt)
9061 && TYPE_NAME (xt) != NULL_TREE
9062 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9063 xn = DECL_NAME (TYPE_NAME (xt));
9064 else
9065 xn = NULL_TREE;
9066
9067 yt = TREE_TYPE (y);
9068 if (DECL_NAME (y) != NULL_TREE)
9069 yn = DECL_NAME (y);
9070 else if (RECORD_OR_UNION_TYPE_P (yt)
9071 && TYPE_NAME (yt) != NULL_TREE
9072 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9073 yn = DECL_NAME (TYPE_NAME (yt));
9074 else
9075 yn = NULL_TREE;
9076
9077 if (xn != NULL_TREE && xn == yn)
9078 return true;
9079 }
9080
9081 return false;
9082 }
9083
9084 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9085 to HTAB, giving errors for any duplicates. */
9086
9087 static void
9088 detect_field_duplicates_hash (tree fieldlist,
9089 hash_table<nofree_ptr_hash <tree_node> > *htab)
9090 {
9091 tree x, y;
9092 tree_node **slot;
9093
9094 for (x = fieldlist; x ; x = DECL_CHAIN (x))
9095 if ((y = DECL_NAME (x)) != NULL_TREE)
9096 {
9097 slot = htab->find_slot (y, INSERT);
9098 if (*slot)
9099 {
9100 error ("duplicate member %q+D", x);
9101 DECL_NAME (x) = NULL_TREE;
9102 }
9103 *slot = y;
9104 }
9105 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9106 {
9107 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9108
9109 /* When using -fplan9-extensions, an anonymous field whose
9110 name is a typedef can duplicate a field name. */
9111 if (flag_plan9_extensions
9112 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9113 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9114 {
9115 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9116 slot = htab->find_slot (xn, INSERT);
9117 if (*slot)
9118 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9119 *slot = xn;
9120 }
9121 }
9122 }
9123
9124 /* Generate an error for any duplicate field names in FIELDLIST. Munge
9125 the list such that this does not present a problem later. */
9126
9127 static void
9128 detect_field_duplicates (tree fieldlist)
9129 {
9130 tree x, y;
9131 int timeout = 10;
9132
9133 /* If the struct is the list of instance variables of an Objective-C
9134 class, then we need to check all the instance variables of
9135 superclasses when checking for duplicates (since you can't have
9136 an instance variable in a subclass with the same name as an
9137 instance variable in a superclass). We pass on this job to the
9138 Objective-C compiler. objc_detect_field_duplicates() will return
9139 false if we are not checking the list of instance variables and
9140 the C frontend should proceed with the standard field duplicate
9141 checks. If we are checking the list of instance variables, the
9142 ObjC frontend will do the check, emit the errors if needed, and
9143 then return true. */
9144 if (c_dialect_objc ())
9145 if (objc_detect_field_duplicates (false))
9146 return;
9147
9148 /* First, see if there are more than "a few" fields.
9149 This is trivially true if there are zero or one fields. */
9150 if (!fieldlist || !DECL_CHAIN (fieldlist))
9151 return;
9152 x = fieldlist;
9153 do {
9154 timeout--;
9155 if (DECL_NAME (x) == NULL_TREE
9156 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9157 timeout = 0;
9158 x = DECL_CHAIN (x);
9159 } while (timeout > 0 && x);
9160
9161 /* If there were "few" fields and no anonymous structures or unions,
9162 avoid the overhead of allocating a hash table. Instead just do
9163 the nested traversal thing. */
9164 if (timeout > 0)
9165 {
9166 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9167 /* When using -fplan9-extensions, we can have duplicates
9168 between typedef names and fields. */
9169 if (DECL_NAME (x)
9170 || (flag_plan9_extensions
9171 && DECL_NAME (x) == NULL_TREE
9172 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9173 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9174 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9175 {
9176 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9177 if (is_duplicate_field (y, x))
9178 {
9179 error ("duplicate member %q+D", x);
9180 DECL_NAME (x) = NULL_TREE;
9181 }
9182 }
9183 }
9184 else
9185 {
9186 hash_table<nofree_ptr_hash <tree_node> > htab (37);
9187 detect_field_duplicates_hash (fieldlist, &htab);
9188 }
9189 }
9190
9191 /* Finish up struct info used by -Wc++-compat. */
9192
9193 static void
9194 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9195 location_t record_loc)
9196 {
9197 unsigned int ix;
9198 tree x;
9199 struct c_binding *b;
9200
9201 if (fieldlist == NULL_TREE)
9202 {
9203 if (code == RECORD_TYPE)
9204 warning_at (record_loc, OPT_Wc___compat,
9205 "empty struct has size 0 in C, size 1 in C++");
9206 else
9207 warning_at (record_loc, OPT_Wc___compat,
9208 "empty union has size 0 in C, size 1 in C++");
9209 }
9210
9211 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9212 the current struct. We do this now at the end of the struct
9213 because the flag is used to issue visibility warnings, and we
9214 only want to issue those warnings if the type is referenced
9215 outside of the struct declaration. */
9216 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9217 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9218
9219 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9220 typedefs used when declaring fields in this struct. If the name
9221 of any of the fields is also a typedef name then the struct would
9222 not parse in C++, because the C++ lookup rules say that the
9223 typedef name would be looked up in the context of the struct, and
9224 would thus be the field rather than the typedef. */
9225 if (!struct_parse_info->typedefs_seen.is_empty ()
9226 && fieldlist != NULL_TREE)
9227 {
9228 /* Use a hash_set<tree> using the name of the typedef. We can use
9229 a hash_set<tree> because identifiers are interned. */
9230 hash_set<tree> tset;
9231
9232 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9233 tset.add (DECL_NAME (x));
9234
9235 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9236 {
9237 if (DECL_NAME (x) != NULL_TREE
9238 && tset.contains (DECL_NAME (x)))
9239 {
9240 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9241 ("using %qD as both field and typedef name is "
9242 "invalid in C++"),
9243 x);
9244 /* FIXME: It would be nice to report the location where
9245 the typedef name is used. */
9246 }
9247 }
9248 }
9249
9250 /* For each field which has a binding and which was not defined in
9251 an enclosing struct, clear the in_struct field. */
9252 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9253 b->in_struct = 0;
9254 }
9255
9256 /* Function to help qsort sort FIELD_DECLs by name order. */
9257
9258 static int
9259 field_decl_cmp (const void *x_p, const void *y_p)
9260 {
9261 const tree *const x = (const tree *) x_p;
9262 const tree *const y = (const tree *) y_p;
9263
9264 if (DECL_NAME (*x) == DECL_NAME (*y))
9265 /* A nontype is "greater" than a type. */
9266 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9267 if (DECL_NAME (*x) == NULL_TREE)
9268 return -1;
9269 if (DECL_NAME (*y) == NULL_TREE)
9270 return 1;
9271 if (DECL_NAME (*x) < DECL_NAME (*y))
9272 return -1;
9273 return 1;
9274 }
9275
9276 /* If this structure or union completes the type of any previous
9277 variable declaration, lay it out and output its rtl. */
9278 static void
9279 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9280 {
9281 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9282 {
9283 tree decl = TREE_VALUE (x);
9284 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9285 layout_array_type (TREE_TYPE (decl));
9286 if (TREE_CODE (decl) != TYPE_DECL)
9287 {
9288 relayout_decl (decl);
9289 if (c_dialect_objc ())
9290 objc_check_decl (decl);
9291 rest_of_decl_compilation (decl, toplevel, 0);
9292 }
9293 }
9294 }
9295
9296 /* Determine whether the FIELD_DECL X is a flexible array member according to
9297 the following info:
9298 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9299 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9300 or "[1]";
9301 C. flag_strict_flex_arrays;
9302 D. the attribute strict_flex_array that is attached to the field
9303 if presenting.
9304 Return TRUE when it's a flexible array member, FALSE otherwise. */
9305
9306 static bool
9307 is_flexible_array_member_p (bool is_last_field,
9308 tree x)
9309 {
9310 /* If not the last field, return false. */
9311 if (!is_last_field)
9312 return false;
9313
9314 /* If not an array field, return false. */
9315 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9316 return false;
9317
9318 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9319 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9320 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9321
9322 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9323
9324 switch (strict_flex_array_level)
9325 {
9326 case 0:
9327 /* Default, all trailing arrays are flexible array members. */
9328 return true;
9329 case 1:
9330 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9331 if (is_one_element_array)
9332 return true;
9333 /* FALLTHROUGH. */
9334 case 2:
9335 /* Level 2: all "[0]", and "[]" are flexible array members. */
9336 if (is_zero_length_array)
9337 return true;
9338 /* FALLTHROUGH. */
9339 case 3:
9340 /* Level 3: Only "[]" are flexible array members. */
9341 if (is_flexible_array)
9342 return true;
9343 break;
9344 default:
9345 gcc_unreachable ();
9346 }
9347 return false;
9348 }
9349
9350
9351 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9352 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9353 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9354 ATTRIBUTES are attributes to be applied to the structure.
9355
9356 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9357 the struct was started. */
9358
9359 tree
9360 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9361 class c_struct_parse_info *enclosing_struct_parse_info,
9362 tree *expr)
9363 {
9364 tree x;
9365 bool toplevel = file_scope == current_scope;
9366
9367 /* If this type was previously laid out as a forward reference,
9368 make sure we lay it out again. */
9369
9370 TYPE_SIZE (t) = NULL_TREE;
9371
9372 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9373
9374 if (pedantic)
9375 {
9376 for (x = fieldlist; x; x = DECL_CHAIN (x))
9377 {
9378 if (DECL_NAME (x) != NULL_TREE)
9379 break;
9380 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9381 break;
9382 }
9383
9384 if (x == NULL_TREE)
9385 {
9386 if (TREE_CODE (t) == UNION_TYPE)
9387 {
9388 if (fieldlist)
9389 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9390 else
9391 pedwarn (loc, OPT_Wpedantic, "union has no members");
9392 }
9393 else
9394 {
9395 if (fieldlist)
9396 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9397 else
9398 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9399 }
9400 }
9401 }
9402
9403 /* Install struct as DECL_CONTEXT of each field decl.
9404 Also process specified field sizes, found in the DECL_INITIAL,
9405 storing 0 there after the type has been changed to precision equal
9406 to its width, rather than the precision of the specified standard
9407 type. (Correct layout requires the original type to have been preserved
9408 until now.) */
9409
9410 bool saw_named_field = false;
9411 for (x = fieldlist; x; x = DECL_CHAIN (x))
9412 {
9413 /* Whether this field is the last field of the structure or union.
9414 for UNION, any field is the last field of it. */
9415 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9416 || (TREE_CODE (t) == UNION_TYPE);
9417
9418 if (TREE_TYPE (x) == error_mark_node)
9419 continue;
9420
9421 DECL_CONTEXT (x) = t;
9422
9423 tree t1 = strip_array_types (TREE_TYPE (x));
9424 /* If any field is const, the structure type is pseudo-const. */
9425 if (TREE_READONLY (x))
9426 C_TYPE_FIELDS_READONLY (t) = 1;
9427 else
9428 {
9429 /* A field that is pseudo-const makes the structure likewise. */
9430 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9431 C_TYPE_FIELDS_READONLY (t) = 1;
9432 }
9433
9434 /* Any field that is volatile means variables of this type must be
9435 treated in some ways as volatile. */
9436 if (TREE_THIS_VOLATILE (x))
9437 {
9438 C_TYPE_FIELDS_VOLATILE (t) = 1;
9439 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9440 }
9441
9442 /* Any field that is volatile, restrict-qualified or atomic
9443 means the type cannot be used for a constexpr object. */
9444 if (TYPE_QUALS (t1)
9445 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9446 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9447 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9448 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9449
9450 /* Any field of nominal variable size implies structure is too. */
9451 if (C_DECL_VARIABLE_SIZE (x))
9452 C_TYPE_VARIABLE_SIZE (t) = 1;
9453
9454 /* If any field is variably modified, record this fact. */
9455 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9456 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9457
9458 if (DECL_C_BIT_FIELD (x))
9459 {
9460 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9461 DECL_SIZE (x) = bitsize_int (width);
9462 DECL_BIT_FIELD (x) = 1;
9463 }
9464
9465 if (TYPE_PACKED (t)
9466 && (DECL_BIT_FIELD (x)
9467 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9468 DECL_PACKED (x) = 1;
9469
9470 /* Detect flexible array member in an invalid context. */
9471 if (flexible_array_member_type_p (TREE_TYPE (x)))
9472 {
9473 if (TREE_CODE (t) == UNION_TYPE)
9474 {
9475 error_at (DECL_SOURCE_LOCATION (x),
9476 "flexible array member in union");
9477 TREE_TYPE (x) = error_mark_node;
9478 }
9479 else if (!is_last_field)
9480 {
9481 error_at (DECL_SOURCE_LOCATION (x),
9482 "flexible array member not at end of struct");
9483 TREE_TYPE (x) = error_mark_node;
9484 }
9485 else if (!saw_named_field)
9486 {
9487 error_at (DECL_SOURCE_LOCATION (x),
9488 "flexible array member in a struct with no named "
9489 "members");
9490 TREE_TYPE (x) = error_mark_node;
9491 }
9492 }
9493
9494 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9495 && flexible_array_type_p (TREE_TYPE (x)))
9496 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9497 "invalid use of structure with flexible array member");
9498
9499 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9500 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9501
9502 /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9503 when x is an array and is the last field. */
9504 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9505 TYPE_INCLUDES_FLEXARRAY (t)
9506 = is_last_field && flexible_array_member_type_p (TREE_TYPE (x));
9507 /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9508 when x is an union or record and is the last field. */
9509 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9510 TYPE_INCLUDES_FLEXARRAY (t)
9511 = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9512
9513 if (warn_flex_array_member_not_at_end
9514 && !is_last_field
9515 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9516 && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9517 warning_at (DECL_SOURCE_LOCATION (x),
9518 OPT_Wflex_array_member_not_at_end,
9519 "structure containing a flexible array member"
9520 " is not at the end of another structure");
9521
9522 if (DECL_NAME (x)
9523 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9524 saw_named_field = true;
9525 }
9526
9527 detect_field_duplicates (fieldlist);
9528
9529 /* Now we have the nearly final fieldlist. Record it,
9530 then lay out the structure or union (including the fields). */
9531
9532 TYPE_FIELDS (t) = fieldlist;
9533
9534 maybe_apply_pragma_scalar_storage_order (t);
9535
9536 layout_type (t);
9537
9538 if (TYPE_SIZE_UNIT (t)
9539 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9540 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9541 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9542 error ("type %qT is too large", t);
9543
9544 /* Give bit-fields their proper types and rewrite the type of array fields
9545 with scalar component if the enclosing type has reverse storage order. */
9546 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9547 {
9548 if (TREE_CODE (field) == FIELD_DECL
9549 && DECL_INITIAL (field)
9550 && TREE_TYPE (field) != error_mark_node)
9551 {
9552 unsigned HOST_WIDE_INT width
9553 = tree_to_uhwi (DECL_INITIAL (field));
9554 tree type = TREE_TYPE (field);
9555 if (width != TYPE_PRECISION (type))
9556 {
9557 if (TREE_CODE (type) == BITINT_TYPE
9558 && (width > 1 || TYPE_UNSIGNED (type)))
9559 TREE_TYPE (field)
9560 = build_bitint_type (width, TYPE_UNSIGNED (type));
9561 else
9562 TREE_TYPE (field)
9563 = c_build_bitfield_integer_type (width,
9564 TYPE_UNSIGNED (type));
9565 if (tree attr = c_hardbool_type_attr (type))
9566 decl_attributes (&TREE_TYPE (field),
9567 copy_list (attr),
9568 0, NULL_TREE);
9569 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9570 }
9571 DECL_INITIAL (field) = NULL_TREE;
9572 }
9573 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9574 && TREE_CODE (field) == FIELD_DECL
9575 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9576 {
9577 tree ftype = TREE_TYPE (field);
9578 tree ctype = strip_array_types (ftype);
9579 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9580 {
9581 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9582 tree *typep = &fmain_type;
9583 do {
9584 *typep = build_distinct_type_copy (*typep);
9585 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9586 typep = &TREE_TYPE (*typep);
9587 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9588 TREE_TYPE (field)
9589 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9590 }
9591 }
9592
9593 /* Warn on problematic type punning for storage order purposes. */
9594 if (TREE_CODE (t) == UNION_TYPE
9595 && TREE_CODE (field) == FIELD_DECL
9596 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9597 {
9598 tree ftype = TREE_TYPE (field);
9599 if (TREE_CODE (ftype) == ARRAY_TYPE)
9600 ftype = strip_array_types (ftype);
9601 if (RECORD_OR_UNION_TYPE_P (ftype)
9602 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9603 != TYPE_REVERSE_STORAGE_ORDER (t))
9604 warning_at (DECL_SOURCE_LOCATION (field),
9605 OPT_Wscalar_storage_order,
9606 "type punning toggles scalar storage order");
9607 }
9608 }
9609
9610 /* Now we have the truly final field list.
9611 Store it in this type and in the variants. */
9612
9613 TYPE_FIELDS (t) = fieldlist;
9614
9615 /* If there are lots of fields, sort so we can look through them fast.
9616 We arbitrarily consider 16 or more elts to be "a lot". */
9617
9618 {
9619 int len = 0;
9620
9621 for (x = fieldlist; x; x = DECL_CHAIN (x))
9622 {
9623 if (len > 15 || DECL_NAME (x) == NULL)
9624 break;
9625 len += 1;
9626 }
9627
9628 if (len > 15)
9629 {
9630 tree *field_array;
9631 struct lang_type *space;
9632 struct sorted_fields_type *space2;
9633
9634 len += list_length (x);
9635
9636 /* Use the same allocation policy here that make_node uses, to
9637 ensure that this lives as long as the rest of the struct decl.
9638 All decls in an inline function need to be saved. */
9639
9640 space = ggc_cleared_alloc<struct lang_type> ();
9641 space2 = (sorted_fields_type *) ggc_internal_alloc
9642 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9643
9644 len = 0;
9645 space->s = space2;
9646 field_array = &space2->elts[0];
9647 for (x = fieldlist; x; x = DECL_CHAIN (x))
9648 {
9649 field_array[len++] = x;
9650
9651 /* If there is anonymous struct or union, break out of the loop. */
9652 if (DECL_NAME (x) == NULL)
9653 break;
9654 }
9655 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9656 if (x == NULL)
9657 {
9658 TYPE_LANG_SPECIFIC (t) = space;
9659 TYPE_LANG_SPECIFIC (t)->s->len = len;
9660 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9661 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9662 }
9663 }
9664 }
9665
9666 /* If this was supposed to be a transparent union, but we can't
9667 make it one, warn and turn off the flag. */
9668 if (TREE_CODE (t) == UNION_TYPE
9669 && TYPE_TRANSPARENT_AGGR (t)
9670 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9671 {
9672 TYPE_TRANSPARENT_AGGR (t) = 0;
9673 warning_at (loc, 0, "union cannot be made transparent");
9674 }
9675
9676 /* Check for consistency with previous definition. */
9677 if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9678 {
9679 tree vistype = previous_tag (t);
9680 if (vistype
9681 && TREE_CODE (vistype) == TREE_CODE (t)
9682 && !C_TYPE_BEING_DEFINED (vistype))
9683 {
9684 TYPE_STUB_DECL (vistype) = TYPE_STUB_DECL (t);
9685 if (c_type_variably_modified_p (t))
9686 error ("redefinition of struct or union %qT with variably "
9687 "modified type", t);
9688 else if (!comptypes_same_p (t, vistype))
9689 error ("redefinition of struct or union %qT", t);
9690 }
9691 }
9692
9693 C_TYPE_BEING_DEFINED (t) = 0;
9694
9695 /* Set type canonical based on equivalence class. */
9696 if (flag_isoc23)
9697 {
9698 if (NULL == c_struct_htab)
9699 c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9700
9701 hashval_t hash = c_struct_hasher::hash (t);
9702
9703 tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9704 if (*e)
9705 TYPE_CANONICAL (t) = *e;
9706 else
9707 {
9708 TYPE_CANONICAL (t) = t;
9709 *e = t;
9710 }
9711 }
9712
9713 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9714 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9715 {
9716 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9717 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9718 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9719 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9720 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9721 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9722 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9723 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9724 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9725 }
9726
9727 /* Update type location to the one of the definition, instead of e.g.
9728 a forward declaration. */
9729 if (TYPE_STUB_DECL (t))
9730 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9731
9732 /* Finish debugging output for this type. */
9733 rest_of_type_compilation (t, toplevel);
9734
9735 finish_incomplete_vars (incomplete_vars, toplevel);
9736
9737 /* Make sure a DECL_EXPR is created for structs with VLA members.
9738 Because we do not know the context, we always pass expr
9739 to force creation of a BIND_EXPR which is required in some
9740 contexts. */
9741 if (c_type_variably_modified_p (t))
9742 add_decl_expr (loc, t, expr, false);
9743
9744 if (warn_cxx_compat)
9745 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9746
9747 if (NULL != enclosing_struct_parse_info)
9748 {
9749 delete struct_parse_info;
9750
9751 struct_parse_info = enclosing_struct_parse_info;
9752
9753 /* If this struct is defined inside a struct, add it to
9754 struct_types. */
9755 if (warn_cxx_compat
9756 && struct_parse_info != NULL
9757 && !in_sizeof && !in_typeof && !in_alignof)
9758 struct_parse_info->struct_types.safe_push (t);
9759 }
9760
9761 return t;
9762 }
9763
9764 static struct {
9765 gt_pointer_operator new_value;
9766 void *cookie;
9767 } resort_data;
9768
9769 /* This routine compares two fields like field_decl_cmp but using the
9770 pointer operator in resort_data. */
9771
9772 static int
9773 resort_field_decl_cmp (const void *x_p, const void *y_p)
9774 {
9775 const tree *const x = (const tree *) x_p;
9776 const tree *const y = (const tree *) y_p;
9777
9778 if (DECL_NAME (*x) == DECL_NAME (*y))
9779 /* A nontype is "greater" than a type. */
9780 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9781 if (DECL_NAME (*x) == NULL_TREE)
9782 return -1;
9783 if (DECL_NAME (*y) == NULL_TREE)
9784 return 1;
9785 {
9786 tree d1 = DECL_NAME (*x);
9787 tree d2 = DECL_NAME (*y);
9788 resort_data.new_value (&d1, &d1, resort_data.cookie);
9789 resort_data.new_value (&d2, &d2, resort_data.cookie);
9790 if (d1 < d2)
9791 return -1;
9792 }
9793 return 1;
9794 }
9795
9796 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9797
9798 void
9799 resort_sorted_fields (void *obj,
9800 void * ARG_UNUSED (orig_obj),
9801 gt_pointer_operator new_value,
9802 void *cookie)
9803 {
9804 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9805 resort_data.new_value = new_value;
9806 resort_data.cookie = cookie;
9807 qsort (&sf->elts[0], sf->len, sizeof (tree),
9808 resort_field_decl_cmp);
9809 }
9810
9811 /* Lay out the type T, and its element type, and so on. */
9812
9813 static void
9814 layout_array_type (tree t)
9815 {
9816 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9817 layout_array_type (TREE_TYPE (t));
9818 layout_type (t);
9819 }
9820 \f
9821 /* Begin compiling the definition of an enumeration type.
9822 NAME is its name (or null if anonymous).
9823 LOC is the enum's location.
9824 FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9825 definition.
9826 Returns the type object, as yet incomplete.
9827 Also records info about it so that build_enumerator
9828 may be used to declare the individual values as they are read. */
9829
9830 tree
9831 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9832 tree fixed_underlying_type, bool potential_nesting_p)
9833 {
9834 tree enumtype = NULL_TREE;
9835 location_t enumloc = UNKNOWN_LOCATION;
9836
9837 /* If this is the real definition for a previous forward reference,
9838 fill in the contents in the same object that used to be the
9839 forward reference. */
9840
9841 if (name != NULL_TREE)
9842 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9843
9844 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
9845 {
9846 /* If the type is currently being defined or if we have seen an
9847 incomplete version which is now complete, this is a nested
9848 redefinition. The later happens if the redefinition occurs
9849 inside the enum specifier itself. */
9850 if (C_TYPE_BEING_DEFINED (enumtype)
9851 || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
9852 error_at (loc, "nested redefinition of %<enum %E%>", name);
9853
9854 /* For C23 we allow redefinitions. We set to zero and check for
9855 consistency later. */
9856 if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
9857 enumtype = NULL_TREE;
9858 }
9859
9860 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9861 {
9862 enumtype = make_node (ENUMERAL_TYPE);
9863 TYPE_SIZE (enumtype) = NULL_TREE;
9864 pushtag (loc, name, enumtype);
9865 if (fixed_underlying_type != NULL_TREE)
9866 {
9867 /* For an enum definition with a fixed underlying type, the
9868 type is complete during the definition and the
9869 enumeration constants have that type. If there was a
9870 tag, the type was completed in c_parser_enum_specifier.
9871 If not, it must be completed here. */
9872 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9873 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9874 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9875 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9876 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9877 TYPE_SIZE (enumtype) = NULL_TREE;
9878 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9879 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9880 layout_type (enumtype);
9881 }
9882 }
9883 /* Update type location to the one of the definition, instead of e.g.
9884 a forward declaration. */
9885 else if (TYPE_STUB_DECL (enumtype))
9886 {
9887 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9888 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9889 }
9890
9891 C_TYPE_BEING_DEFINED (enumtype) = 1;
9892
9893 if (TYPE_VALUES (enumtype) != NULL_TREE)
9894 {
9895 /* This enum is a named one that has been declared already. */
9896 auto_diagnostic_group d;
9897 error_at (loc, "redeclaration of %<enum %E%>", name);
9898 if (enumloc != UNKNOWN_LOCATION)
9899 inform (enumloc, "originally defined here");
9900
9901 /* Completely replace its old definition.
9902 The old enumerators remain defined, however. */
9903 TYPE_VALUES (enumtype) = NULL_TREE;
9904 }
9905
9906 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9907 && fixed_underlying_type == NULL_TREE)
9908 error_at (loc, "%<enum%> declared with but defined without "
9909 "fixed underlying type");
9910
9911 the_enum->enum_next_value = integer_zero_node;
9912 the_enum->enum_type = enumtype;
9913 the_enum->enum_overflow = 0;
9914
9915 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9916 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9917 TYPE_PACKED (v) = 1;
9918
9919 /* FIXME: This will issue a warning for a use of a type defined
9920 within sizeof in a statement expr. This is not terribly serious
9921 as C++ doesn't permit statement exprs within sizeof anyhow. */
9922 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9923 warning_at (loc, OPT_Wc___compat,
9924 "defining type in %qs expression is invalid in C++",
9925 (in_sizeof
9926 ? "sizeof"
9927 : (in_typeof ? "typeof" : "alignof")));
9928
9929 if (in_underspecified_init)
9930 error_at (loc, "%qT defined in underspecified object initializer",
9931 enumtype);
9932
9933 return enumtype;
9934 }
9935
9936 /* After processing and defining all the values of an enumeration type,
9937 install their decls in the enumeration type and finish it off.
9938 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9939 and ATTRIBUTES are the specified attributes.
9940 Returns ENUMTYPE. */
9941
9942 tree
9943 finish_enum (tree enumtype, tree values, tree attributes)
9944 {
9945 tree pair, tem;
9946 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9947 int precision;
9948 signop sign;
9949 bool toplevel = (file_scope == current_scope);
9950 struct lang_type *lt;
9951
9952 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9953
9954 /* Calculate the maximum value of any enumerator in this type. */
9955
9956 if (values == error_mark_node)
9957 minnode = maxnode = integer_zero_node;
9958 else
9959 {
9960 minnode = maxnode = TREE_VALUE (values);
9961 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9962 {
9963 tree value = TREE_VALUE (pair);
9964 if (tree_int_cst_lt (maxnode, value))
9965 maxnode = value;
9966 if (tree_int_cst_lt (value, minnode))
9967 minnode = value;
9968 }
9969 }
9970
9971 /* Construct the final type of this enumeration. It is the same
9972 as one of the integral types - the narrowest one that fits, except
9973 that normally we only go as narrow as int - and signed iff any of
9974 the values are negative. */
9975 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9976 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9977 tree_int_cst_min_precision (maxnode, sign));
9978
9979 bool wider_than_int =
9980 (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
9981 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
9982
9983
9984 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9985 {
9986 /* If the precision of the type was specified with an attribute and it
9987 was too small, give an error. Otherwise, use it. */
9988 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9989 {
9990 if (precision > TYPE_PRECISION (enumtype))
9991 {
9992 TYPE_PRECISION (enumtype) = 0;
9993 error ("specified mode too small for enumerated values");
9994 }
9995 else
9996 precision = TYPE_PRECISION (enumtype);
9997 }
9998 else
9999 TYPE_PRECISION (enumtype) = 0;
10000
10001 if (TYPE_PACKED (enumtype)
10002 || precision > TYPE_PRECISION (integer_type_node)
10003 || TYPE_PRECISION (enumtype))
10004 {
10005 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10006 if (tem == NULL)
10007 {
10008 /* This should only occur when both signed and unsigned
10009 values of maximum precision occur among the
10010 enumerators. */
10011 pedwarn (input_location, 0,
10012 "enumeration values exceed range of largest integer");
10013 tem = widest_integer_literal_type_node;
10014 }
10015 else if (precision > TYPE_PRECISION (intmax_type_node)
10016 && !tree_int_cst_lt (minnode,
10017 TYPE_MIN_VALUE (intmax_type_node))
10018 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10019 maxnode))
10020 pedwarn (input_location, OPT_Wpedantic,
10021 "enumeration values exceed range of %qs",
10022 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10023 }
10024 else
10025 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10026
10027 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10028 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10029 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10030 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10031 TYPE_SIZE (enumtype) = NULL_TREE;
10032 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10033 ENUM_UNDERLYING_TYPE (enumtype) =
10034 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10035
10036 layout_type (enumtype);
10037 }
10038
10039 if (values != error_mark_node)
10040 {
10041 /* Change the type of the enumerators to be the enum type. We
10042 need to do this irrespective of the size of the enum, for
10043 proper type checking. Replace the DECL_INITIALs of the
10044 enumerators, and the value slots of the list, with copies
10045 that have the enum type; they cannot be modified in place
10046 because they may be shared (e.g. integer_zero_node) Finally,
10047 change the purpose slots to point to the names of the decls. */
10048 for (pair = values; pair; pair = TREE_CHAIN (pair))
10049 {
10050 tree enu = TREE_PURPOSE (pair);
10051 tree ini = DECL_INITIAL (enu);
10052
10053 TREE_TYPE (enu) = enumtype;
10054
10055 /* Before C23, the ISO C Standard mandates enumerators to
10056 have type int, even though the underlying type of an enum
10057 type is unspecified. However, C23 allows enumerators of
10058 any integer type, and if an enumeration has any
10059 enumerators wider than int, all enumerators have the
10060 enumerated type after it is parsed. Any enumerators that
10061 fit in int are given type int in build_enumerator (which
10062 is the correct type while the enumeration is being
10063 parsed), so no conversions are needed here if all
10064 enumerators fit in int. If the enum has a fixed
10065 underlying type, the correct type was also given in
10066 build_enumerator. */
10067 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10068 ini = convert (enumtype, ini);
10069
10070 DECL_INITIAL (enu) = ini;
10071 TREE_PURPOSE (pair) = DECL_NAME (enu);
10072 /* To match the C++ FE, store the CONST_DECL rather than just its
10073 value. */
10074 TREE_VALUE (pair) = enu;
10075 }
10076
10077 TYPE_VALUES (enumtype) = values;
10078 }
10079
10080 /* Record the min/max values so that we can warn about bit-field
10081 enumerations that are too small for the values. */
10082 lt = ggc_cleared_alloc<struct lang_type> ();
10083 lt->enum_min = minnode;
10084 lt->enum_max = maxnode;
10085 TYPE_LANG_SPECIFIC (enumtype) = lt;
10086
10087 /* Fix up all variant types of this enum type. */
10088 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10089 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10090 {
10091 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10092 if (tem == enumtype)
10093 continue;
10094 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10095 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10096 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10097 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10098 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10099 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10100 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10101 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10102 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10103 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10104 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10105 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10106 }
10107
10108 /* Finish debugging output for this type. */
10109 rest_of_type_compilation (enumtype, toplevel);
10110
10111 finish_incomplete_vars (incomplete_vars, toplevel);
10112
10113 /* If this enum is defined inside a struct, add it to
10114 struct_types. */
10115 if (warn_cxx_compat
10116 && struct_parse_info != NULL
10117 && !in_sizeof && !in_typeof && !in_alignof)
10118 struct_parse_info->struct_types.safe_push (enumtype);
10119
10120 /* Check for consistency with previous definition */
10121 if (flag_isoc23)
10122 {
10123 tree vistype = previous_tag (enumtype);
10124 if (vistype
10125 && TREE_CODE (vistype) == TREE_CODE (enumtype)
10126 && !C_TYPE_BEING_DEFINED (vistype))
10127 {
10128 TYPE_STUB_DECL (vistype) = TYPE_STUB_DECL (enumtype);
10129 if (!comptypes_same_p (enumtype, vistype))
10130 error("conflicting redefinition of enum %qT", enumtype);
10131 }
10132 }
10133
10134 C_TYPE_BEING_DEFINED (enumtype) = 0;
10135
10136 return enumtype;
10137 }
10138
10139 /* Build and install a CONST_DECL for one value of the
10140 current enumeration type (one that was begun with start_enum).
10141 DECL_LOC is the location of the enumerator.
10142 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10143 Return a tree-list containing the CONST_DECL and its value.
10144 Assignment of sequential values by default is handled here. */
10145
10146 tree
10147 build_enumerator (location_t decl_loc, location_t loc,
10148 struct c_enum_contents *the_enum, tree name, tree value)
10149 {
10150 tree decl;
10151
10152 /* Validate and default VALUE. */
10153
10154 if (value != NULL_TREE)
10155 {
10156 /* Don't issue more errors for error_mark_node (i.e. an
10157 undeclared identifier) - just ignore the value expression. */
10158 if (value == error_mark_node)
10159 value = NULL_TREE;
10160 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10161 {
10162 error_at (loc, "enumerator value for %qE is not an integer constant",
10163 name);
10164 value = NULL_TREE;
10165 }
10166 else
10167 {
10168 if (TREE_CODE (value) != INTEGER_CST)
10169 {
10170 value = c_fully_fold (value, false, NULL);
10171 if (TREE_CODE (value) == INTEGER_CST)
10172 pedwarn (loc, OPT_Wpedantic,
10173 "enumerator value for %qE is not an integer "
10174 "constant expression", name);
10175 }
10176 if (TREE_CODE (value) != INTEGER_CST)
10177 {
10178 error ("enumerator value for %qE is not an integer constant",
10179 name);
10180 value = NULL_TREE;
10181 }
10182 else
10183 {
10184 value = default_conversion (value);
10185 constant_expression_warning (value);
10186 }
10187 }
10188 }
10189
10190 /* Default based on previous value. */
10191 /* It should no longer be possible to have NON_LVALUE_EXPR
10192 in the default. */
10193 if (value == NULL_TREE)
10194 {
10195 value = the_enum->enum_next_value;
10196 if (the_enum->enum_overflow)
10197 error_at (loc, "overflow in enumeration values");
10198 }
10199 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10200 {
10201 /* Enumeration constants must fit in the fixed underlying type. */
10202 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10203 error_at (loc,
10204 "enumerator value outside the range of underlying type");
10205 /* Enumeration constants for an enum with fixed underlying type
10206 have the enum type, both inside and outside the
10207 definition. */
10208 value = convert (the_enum->enum_type, value);
10209 }
10210 else
10211 {
10212 /* Even though the underlying type of an enum is unspecified, the
10213 type of enumeration constants is explicitly defined as int
10214 (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10215 GCC allows such types for older standards as an extension. */
10216 bool warned_range = false;
10217 if (!int_fits_type_p (value,
10218 (TYPE_UNSIGNED (TREE_TYPE (value))
10219 ? uintmax_type_node
10220 : intmax_type_node)))
10221 /* GCC does not consider its types larger than intmax_t to be
10222 extended integer types (although C23 would permit such types to
10223 be considered extended integer types if all the features
10224 required by <stdint.h> and <inttypes.h> macros, such as support
10225 for integer constants and I/O, were present), so diagnose if
10226 such a wider type is used. (If the wider type arose from a
10227 constant of such a type, that will also have been diagnosed,
10228 but this is the only diagnostic in the case where it arises
10229 from choosing a wider type automatically when adding 1
10230 overflows.) */
10231 warned_range = pedwarn (loc, OPT_Wpedantic,
10232 "enumerator value outside the range of %qs",
10233 (TYPE_UNSIGNED (TREE_TYPE (value))
10234 ? "uintmax_t"
10235 : "intmax_t"));
10236 if (!warned_range && !int_fits_type_p (value, integer_type_node))
10237 pedwarn_c11 (loc, OPT_Wpedantic,
10238 "ISO C restricts enumerator values to range of %<int%> "
10239 "before C23");
10240
10241 /* The ISO C Standard mandates enumerators to have type int before
10242 C23, even though the underlying type of an enum type is
10243 unspecified. C23 allows enumerators of any integer type. During
10244 the parsing of the enumeration, C23 specifies that constants
10245 representable in int have type int, constants not representable
10246 in int have the type of the given expression if any, and
10247 constants not representable in int and derived by adding 1 to the
10248 previous constant have the type of that constant unless the
10249 addition would overflow or wraparound, in which case a wider type
10250 of the same signedness is chosen automatically; after the
10251 enumeration is parsed, all the constants have the type of the
10252 enumeration if any do not fit in int. */
10253 if (int_fits_type_p (value, integer_type_node))
10254 value = convert (integer_type_node, value);
10255 }
10256
10257 /* Set basis for default for next value. */
10258 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10259 {
10260 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10261 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10262 /* A value of 2 following a value of 1 overflows bool, but we
10263 cannot carry out addition directly on bool without
10264 promotion, and converting the result of arithmetic in a
10265 wider type back to bool would not produce the right result
10266 for this overflow check. */
10267 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10268 else
10269 the_enum->enum_next_value
10270 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10271 PLUS_EXPR, convert (underlying_type, value),
10272 convert (underlying_type, integer_one_node),
10273 false);
10274 }
10275 else
10276 the_enum->enum_next_value
10277 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10278 PLUS_EXPR, value, integer_one_node, false);
10279 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10280 if (the_enum->enum_overflow
10281 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10282 {
10283 /* Choose a wider type with the same signedness if
10284 available. */
10285 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10286 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10287 tree new_type = (unsignedp
10288 ? long_unsigned_type_node
10289 : long_integer_type_node);
10290 if (prec > TYPE_PRECISION (new_type))
10291 new_type = (unsignedp
10292 ? long_long_unsigned_type_node
10293 : long_long_integer_type_node);
10294 if (prec > TYPE_PRECISION (new_type))
10295 new_type = (unsignedp
10296 ? widest_unsigned_literal_type_node
10297 : widest_integer_literal_type_node);
10298 if (prec <= TYPE_PRECISION (new_type))
10299 {
10300 the_enum->enum_overflow = false;
10301 the_enum->enum_next_value
10302 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10303 PLUS_EXPR, convert (new_type, value),
10304 integer_one_node, false);
10305 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10306 }
10307 }
10308
10309 /* Now create a declaration for the enum value name. */
10310
10311 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10312 DECL_INITIAL (decl) = value;
10313 DECL_CONTEXT (decl) = the_enum->enum_type;
10314 pushdecl (decl);
10315
10316 return tree_cons (decl, value, NULL_TREE);
10317 }
10318
10319 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10320
10321 tree
10322 c_simulate_enum_decl (location_t loc, const char *name,
10323 vec<string_int_pair> *values_ptr)
10324 {
10325 location_t saved_loc = input_location;
10326 input_location = loc;
10327
10328 struct c_enum_contents the_enum;
10329 tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10330 NULL_TREE, false);
10331
10332 tree value_chain = NULL_TREE;
10333 string_int_pair *value;
10334 vec<string_int_pair> values = *values_ptr;
10335 unsigned int i;
10336 FOR_EACH_VEC_ELT (values, i, value)
10337 {
10338 tree decl = build_enumerator (loc, loc, &the_enum,
10339 get_identifier (value->first),
10340 build_int_cst (integer_type_node,
10341 value->second));
10342 TREE_CHAIN (decl) = value_chain;
10343 value_chain = decl;
10344 }
10345
10346 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10347
10348 input_location = saved_loc;
10349 return enumtype;
10350 }
10351
10352 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10353
10354 tree
10355 c_simulate_record_decl (location_t loc, const char *name,
10356 array_slice<const tree> fields)
10357 {
10358 location_t saved_loc = input_location;
10359 input_location = loc;
10360
10361 class c_struct_parse_info *struct_info;
10362 tree ident = get_identifier (name);
10363 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10364
10365 for (unsigned int i = 0; i < fields.size (); ++i)
10366 {
10367 DECL_FIELD_CONTEXT (fields[i]) = type;
10368 if (i > 0)
10369 DECL_CHAIN (fields[i - 1]) = fields[i];
10370 }
10371
10372 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10373
10374 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10375 set_underlying_type (decl);
10376 lang_hooks.decls.pushdecl (decl);
10377
10378 input_location = saved_loc;
10379 return type;
10380 }
10381 \f
10382 /* Create the FUNCTION_DECL for a function definition.
10383 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10384 the declaration; they describe the function's name and the type it returns,
10385 but twisted together in a fashion that parallels the syntax of C.
10386
10387 This function creates a binding context for the function body
10388 as well as setting up the FUNCTION_DECL in current_function_decl.
10389
10390 Returns true on success. If the DECLARATOR is not suitable for a function
10391 (it defines a datum instead), we return false to report a parse error. */
10392
10393 bool
10394 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10395 tree attributes)
10396 {
10397 tree decl1, old_decl;
10398 tree restype, resdecl;
10399 location_t loc;
10400 location_t result_loc;
10401 tree expr = NULL;
10402
10403 current_function_returns_value = 0; /* Assume, until we see it does. */
10404 current_function_returns_null = 0;
10405 current_function_returns_abnormally = 0;
10406 warn_about_return_type = 0;
10407 c_switch_stack = NULL;
10408
10409 /* Indicate no valid break/continue context. */
10410 in_statement = 0;
10411
10412 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10413 &attributes, &expr, NULL, DEPRECATED_NORMAL);
10414 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10415
10416 /* If the declarator is not suitable for a function definition,
10417 cause a syntax error. */
10418 if (decl1 == NULL_TREE
10419 || TREE_CODE (decl1) != FUNCTION_DECL)
10420 return false;
10421
10422 /* Nested functions may have variably modified (return) type.
10423 Make sure the size expression is evaluated at this point. */
10424 if (expr && !current_scope->parm_flag)
10425 add_stmt (fold_convert (void_type_node, expr));
10426
10427 loc = DECL_SOURCE_LOCATION (decl1);
10428
10429 /* A nested function is not global. */
10430 if (current_function_decl != NULL_TREE)
10431 TREE_PUBLIC (decl1) = 0;
10432
10433 c_decl_attributes (&decl1, attributes, 0);
10434
10435 if (DECL_DECLARED_INLINE_P (decl1)
10436 && DECL_UNINLINABLE (decl1)
10437 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10438 warning_at (loc, OPT_Wattributes,
10439 "inline function %qD given attribute %qs",
10440 decl1, "noinline");
10441
10442 /* Handle gnu_inline attribute. */
10443 if (declspecs->inline_p
10444 && !flag_gnu89_inline
10445 && TREE_CODE (decl1) == FUNCTION_DECL
10446 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10447 || current_function_decl))
10448 {
10449 if (declspecs->storage_class != csc_static)
10450 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10451 }
10452
10453 announce_function (decl1);
10454
10455 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10456 {
10457 error_at (loc, "return type is an incomplete type");
10458 /* Make it return void instead. */
10459 TREE_TYPE (decl1)
10460 = build_function_type (void_type_node,
10461 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10462 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10463 }
10464
10465 if (warn_about_return_type)
10466 permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10467 : (warn_return_type > 0 ? OPT_Wreturn_type
10468 : OPT_Wimplicit_int),
10469 "return type defaults to %<int%>");
10470
10471 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10472 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10473 DECL_INITIAL (decl1) = error_mark_node;
10474
10475 /* If this definition isn't a prototype and we had a prototype declaration
10476 before, copy the arg type info from that prototype. */
10477 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10478 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10479 old_decl = NULL_TREE;
10480
10481 current_function_prototype_locus = UNKNOWN_LOCATION;
10482 current_function_prototype_built_in = false;
10483 current_function_prototype_arg_types = NULL_TREE;
10484 tree newtype = TREE_TYPE (decl1);
10485 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10486 if (!prototype_p (newtype))
10487 {
10488 tree oldrt = TREE_TYPE (oldtype);
10489 tree newrt = TREE_TYPE (newtype);
10490 if (old_decl != NULL_TREE
10491 && TREE_CODE (oldtype) == FUNCTION_TYPE
10492 && comptypes (oldrt, newrt))
10493 {
10494 if (stdarg_p (oldtype))
10495 {
10496 auto_diagnostic_group d;
10497 warning_at (loc, 0, "%q+D defined as variadic function "
10498 "without prototype", decl1);
10499 locate_old_decl (old_decl);
10500 }
10501 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10502 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10503 current_function_prototype_built_in
10504 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10505 current_function_prototype_arg_types
10506 = TYPE_ARG_TYPES (newtype);
10507 }
10508 if (TREE_PUBLIC (decl1))
10509 {
10510 /* If there is an external prototype declaration of this
10511 function, record its location but do not copy information
10512 to this decl. This may be an invisible declaration
10513 (built-in or in a scope which has finished) or simply
10514 have more refined argument types than any declaration
10515 found above. */
10516 struct c_binding *b;
10517 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10518 if (B_IN_SCOPE (b, external_scope))
10519 break;
10520 if (b)
10521 {
10522 tree ext_decl, ext_type;
10523 ext_decl = b->decl;
10524 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10525 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10526 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10527 TREE_TYPE (ext_type)))
10528 {
10529 current_function_prototype_locus
10530 = DECL_SOURCE_LOCATION (ext_decl);
10531 current_function_prototype_built_in
10532 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10533 current_function_prototype_arg_types
10534 = TYPE_ARG_TYPES (ext_type);
10535 }
10536 }
10537 }
10538 }
10539
10540 /* Optionally warn of old-fashioned def with no previous prototype. */
10541 if (warn_strict_prototypes
10542 && old_decl != error_mark_node
10543 && !prototype_p (TREE_TYPE (decl1))
10544 && C_DECL_ISNT_PROTOTYPE (old_decl))
10545 warning_at (loc, OPT_Wstrict_prototypes,
10546 "function declaration isn%'t a prototype");
10547 /* Optionally warn of any global def with no previous prototype. */
10548 else if (warn_missing_prototypes
10549 && old_decl != error_mark_node
10550 && TREE_PUBLIC (decl1)
10551 && !MAIN_NAME_P (DECL_NAME (decl1))
10552 && C_DECL_ISNT_PROTOTYPE (old_decl)
10553 && !DECL_DECLARED_INLINE_P (decl1))
10554 warning_at (loc, OPT_Wmissing_prototypes,
10555 "no previous prototype for %qD", decl1);
10556 /* Optionally warn of any def with no previous prototype
10557 if the function has already been used. */
10558 else if (warn_missing_prototypes
10559 && old_decl != NULL_TREE
10560 && old_decl != error_mark_node
10561 && TREE_USED (old_decl)
10562 && !prototype_p (TREE_TYPE (old_decl)))
10563 warning_at (loc, OPT_Wmissing_prototypes,
10564 "%qD was used with no prototype before its definition", decl1);
10565 /* Optionally warn of any global def with no previous declaration. */
10566 else if (warn_missing_declarations
10567 && TREE_PUBLIC (decl1)
10568 && old_decl == NULL_TREE
10569 && !MAIN_NAME_P (DECL_NAME (decl1))
10570 && !DECL_DECLARED_INLINE_P (decl1))
10571 warning_at (loc, OPT_Wmissing_declarations,
10572 "no previous declaration for %qD",
10573 decl1);
10574 /* Optionally warn of any def with no previous declaration
10575 if the function has already been used. */
10576 else if (warn_missing_declarations
10577 && old_decl != NULL_TREE
10578 && old_decl != error_mark_node
10579 && TREE_USED (old_decl)
10580 && C_DECL_IMPLICIT (old_decl))
10581 warning_at (loc, OPT_Wmissing_declarations,
10582 "%qD was used with no declaration before its definition", decl1);
10583
10584 /* This function exists in static storage.
10585 (This does not mean `static' in the C sense!) */
10586 TREE_STATIC (decl1) = 1;
10587
10588 /* This is the earliest point at which we might know the assembler
10589 name of the function. Thus, if it's set before this, die horribly. */
10590 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10591
10592 /* If #pragma weak was used, mark the decl weak now. */
10593 if (current_scope == file_scope)
10594 maybe_apply_pragma_weak (decl1);
10595
10596 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10597 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10598 {
10599 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10600 != integer_type_node)
10601 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10602 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10603 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10604 decl1);
10605
10606 check_main_parameter_types (decl1);
10607
10608 if (!TREE_PUBLIC (decl1))
10609 pedwarn (loc, OPT_Wmain,
10610 "%qD is normally a non-static function", decl1);
10611 }
10612
10613 tree parms = current_function_arg_info->parms;
10614 if (old_decl)
10615 {
10616 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10617 warn_parm_array_mismatch (origloc, old_decl, parms);
10618 }
10619
10620 /* Record the decl so that the function name is defined.
10621 If we already have a decl for this name, and it is a FUNCTION_DECL,
10622 use the old decl. */
10623
10624 current_function_decl = pushdecl (decl1);
10625
10626 if (tree access = build_attr_access_from_parms (parms, false))
10627 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10628 old_decl);
10629
10630 push_scope ();
10631 declare_parm_level ();
10632
10633 /* Set the result decl source location to the location of the typespec. */
10634 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10635 ? loc : declspecs->locations[cdw_typespec]);
10636 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10637 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10638 DECL_ARTIFICIAL (resdecl) = 1;
10639 DECL_IGNORED_P (resdecl) = 1;
10640 DECL_RESULT (current_function_decl) = resdecl;
10641
10642 start_fname_decls ();
10643
10644 return true;
10645 }
10646 \f
10647 /* Subroutine of store_parm_decls which handles new-style function
10648 definitions (prototype format). The parms already have decls, so we
10649 need only record them as in effect and complain if any redundant
10650 old-style parm decls were written. */
10651 static void
10652 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10653 {
10654 tree decl;
10655 c_arg_tag *tag;
10656 unsigned ix;
10657
10658 if (current_scope->bindings)
10659 {
10660 error_at (DECL_SOURCE_LOCATION (fndecl),
10661 "old-style parameter declarations in prototyped "
10662 "function definition");
10663
10664 /* Get rid of the old-style declarations. */
10665 pop_scope ();
10666 push_scope ();
10667 }
10668 /* Don't issue this warning for nested functions, and don't issue this
10669 warning if we got here because ARG_INFO_TYPES was error_mark_node
10670 (this happens when a function definition has just an ellipsis in
10671 its parameter list). */
10672 else if (!in_system_header_at (input_location)
10673 && !current_function_scope
10674 && arg_info->types != error_mark_node)
10675 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10676 "traditional C rejects ISO C style function definitions");
10677
10678 /* Now make all the parameter declarations visible in the function body.
10679 We can bypass most of the grunt work of pushdecl. */
10680 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10681 {
10682 DECL_CONTEXT (decl) = current_function_decl;
10683 if (DECL_NAME (decl))
10684 {
10685 bind (DECL_NAME (decl), decl, current_scope,
10686 /*invisible=*/false, /*nested=*/false,
10687 UNKNOWN_LOCATION);
10688 if (!TREE_USED (decl))
10689 warn_if_shadowing (decl);
10690 }
10691 else
10692 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10693 "ISO C does not support omitting parameter names in "
10694 "function definitions before C23");
10695 }
10696
10697 /* Record the parameter list in the function declaration. */
10698 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10699
10700 /* Now make all the ancillary declarations visible, likewise. */
10701 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10702 {
10703 DECL_CONTEXT (decl) = current_function_decl;
10704 if (DECL_NAME (decl))
10705 bind (DECL_NAME (decl), decl, current_scope,
10706 /*invisible=*/false,
10707 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10708 UNKNOWN_LOCATION);
10709 }
10710
10711 /* And all the tag declarations. */
10712 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10713 if (tag->id)
10714 bind (tag->id, tag->type, current_scope,
10715 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10716 }
10717
10718 /* Subroutine of store_parm_decls which handles old-style function
10719 definitions (separate parameter list and declarations). */
10720
10721 static void
10722 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10723 {
10724 struct c_binding *b;
10725 tree parm, decl, last;
10726 tree parmids = arg_info->parms;
10727 hash_set<tree> seen_args;
10728
10729 if (!in_system_header_at (input_location))
10730 {
10731 if (flag_isoc23)
10732 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10733 OPT_Wold_style_definition, "old-style function definition");
10734 else
10735 warning_at (DECL_SOURCE_LOCATION (fndecl),
10736 OPT_Wold_style_definition,
10737 "old-style function definition");
10738 }
10739
10740 if (current_scope->had_vla_unspec)
10741 error ("%<[*]%> not allowed in other than function prototype scope");
10742
10743 /* Match each formal parameter name with its declaration. Save each
10744 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10745 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10746 {
10747 if (TREE_VALUE (parm) == NULL_TREE)
10748 {
10749 error_at (DECL_SOURCE_LOCATION (fndecl),
10750 "parameter name missing from parameter list");
10751 TREE_PURPOSE (parm) = NULL_TREE;
10752 continue;
10753 }
10754
10755 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10756 if (b && B_IN_CURRENT_SCOPE (b))
10757 {
10758 decl = b->decl;
10759 /* Skip erroneous parameters. */
10760 if (decl == error_mark_node)
10761 continue;
10762 /* If we got something other than a PARM_DECL it is an error. */
10763 if (TREE_CODE (decl) != PARM_DECL)
10764 {
10765 error_at (DECL_SOURCE_LOCATION (decl),
10766 "%qD declared as a non-parameter", decl);
10767 continue;
10768 }
10769 /* If the declaration is already marked, we have a duplicate
10770 name. Complain and ignore the duplicate. */
10771 else if (seen_args.contains (decl))
10772 {
10773 error_at (DECL_SOURCE_LOCATION (decl),
10774 "multiple parameters named %qD", decl);
10775 TREE_PURPOSE (parm) = NULL_TREE;
10776 continue;
10777 }
10778 /* If the declaration says "void", complain and turn it into
10779 an int. */
10780 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10781 {
10782 error_at (DECL_SOURCE_LOCATION (decl),
10783 "parameter %qD declared with void type", decl);
10784 TREE_TYPE (decl) = integer_type_node;
10785 DECL_ARG_TYPE (decl) = integer_type_node;
10786 layout_decl (decl, 0);
10787 }
10788 warn_if_shadowing (decl);
10789 }
10790 /* If no declaration found, default to int. */
10791 else
10792 {
10793 /* FIXME diagnostics: This should be the location of the argument,
10794 not the FNDECL. E.g., for an old-style declaration
10795
10796 int f10(v) { blah; }
10797
10798 We should use the location of the V, not the F10.
10799 Unfortunately, the V is an IDENTIFIER_NODE which has no
10800 location. In the future we need locations for c_arg_info
10801 entries.
10802
10803 See gcc.dg/Wshadow-3.c for an example of this problem. */
10804 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10805 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10806 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10807 pushdecl (decl);
10808 warn_if_shadowing (decl);
10809
10810 if (flag_isoc99)
10811 permerror_opt (DECL_SOURCE_LOCATION (decl),
10812 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10813 decl);
10814 else
10815 warning_at (DECL_SOURCE_LOCATION (decl),
10816 OPT_Wmissing_parameter_type,
10817 "type of %qD defaults to %<int%>", decl);
10818 }
10819
10820 TREE_PURPOSE (parm) = decl;
10821 seen_args.add (decl);
10822 }
10823
10824 /* Now examine the parms chain for incomplete declarations
10825 and declarations with no corresponding names. */
10826
10827 for (b = current_scope->bindings; b; b = b->prev)
10828 {
10829 parm = b->decl;
10830 if (TREE_CODE (parm) != PARM_DECL)
10831 continue;
10832
10833 if (TREE_TYPE (parm) != error_mark_node
10834 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10835 {
10836 error_at (DECL_SOURCE_LOCATION (parm),
10837 "parameter %qD has incomplete type", parm);
10838 TREE_TYPE (parm) = error_mark_node;
10839 }
10840
10841 if (!seen_args.contains (parm))
10842 {
10843 error_at (DECL_SOURCE_LOCATION (parm),
10844 "declaration for parameter %qD but no such parameter",
10845 parm);
10846
10847 /* Pretend the parameter was not missing.
10848 This gets us to a standard state and minimizes
10849 further error messages. */
10850 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10851 }
10852 }
10853
10854 /* Chain the declarations together in the order of the list of
10855 names. Store that chain in the function decl, replacing the
10856 list of names. Update the current scope to match. */
10857 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10858
10859 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10860 if (TREE_PURPOSE (parm))
10861 break;
10862 if (parm && TREE_PURPOSE (parm))
10863 {
10864 last = TREE_PURPOSE (parm);
10865 DECL_ARGUMENTS (fndecl) = last;
10866
10867 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10868 if (TREE_PURPOSE (parm))
10869 {
10870 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10871 last = TREE_PURPOSE (parm);
10872 }
10873 DECL_CHAIN (last) = NULL_TREE;
10874 }
10875
10876 /* If there was a previous prototype,
10877 set the DECL_ARG_TYPE of each argument according to
10878 the type previously specified, and report any mismatches. */
10879
10880 if (current_function_prototype_arg_types)
10881 {
10882 tree type;
10883 for (parm = DECL_ARGUMENTS (fndecl),
10884 type = current_function_prototype_arg_types;
10885 parm || (type != NULL_TREE
10886 && TREE_VALUE (type) != error_mark_node
10887 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10888 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10889 {
10890 if (parm == NULL_TREE
10891 || type == NULL_TREE
10892 || (TREE_VALUE (type) != error_mark_node
10893 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10894 {
10895 if (current_function_prototype_built_in)
10896 warning_at (DECL_SOURCE_LOCATION (fndecl),
10897 0, "number of arguments doesn%'t match "
10898 "built-in prototype");
10899 else
10900 {
10901 /* FIXME diagnostics: This should be the location of
10902 FNDECL, but there is bug when a prototype is
10903 declared inside function context, but defined
10904 outside of it (e.g., gcc.dg/pr15698-2.c). In
10905 which case FNDECL gets the location of the
10906 prototype, not the definition. */
10907 error_at (input_location,
10908 "number of arguments doesn%'t match prototype");
10909
10910 error_at (current_function_prototype_locus,
10911 "prototype declaration");
10912 }
10913 break;
10914 }
10915 /* Type for passing arg must be consistent with that
10916 declared for the arg. ISO C says we take the unqualified
10917 type for parameters declared with qualified type. */
10918 if (TREE_TYPE (parm) != error_mark_node
10919 && TREE_VALUE (type) != error_mark_node
10920 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10921 != TYPE_ATOMIC (TREE_VALUE (type)))
10922 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10923 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10924 {
10925 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10926 == TYPE_ATOMIC (TREE_VALUE (type)))
10927 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10928 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10929 {
10930 /* Adjust argument to match prototype. E.g. a previous
10931 `int foo(float);' prototype causes
10932 `int foo(x) float x; {...}' to be treated like
10933 `int foo(float x) {...}'. This is particularly
10934 useful for argument types like uid_t. */
10935 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10936
10937 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10938 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10939 && (TYPE_PRECISION (TREE_TYPE (parm))
10940 < TYPE_PRECISION (integer_type_node)))
10941 DECL_ARG_TYPE (parm)
10942 = c_type_promotes_to (TREE_TYPE (parm));
10943
10944 /* ??? Is it possible to get here with a
10945 built-in prototype or will it always have
10946 been diagnosed as conflicting with an
10947 old-style definition and discarded? */
10948 if (current_function_prototype_built_in)
10949 warning_at (DECL_SOURCE_LOCATION (parm),
10950 OPT_Wpedantic, "promoted argument %qD "
10951 "doesn%'t match built-in prototype", parm);
10952 else
10953 {
10954 pedwarn (DECL_SOURCE_LOCATION (parm),
10955 OPT_Wpedantic, "promoted argument %qD "
10956 "doesn%'t match prototype", parm);
10957 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10958 "prototype declaration");
10959 }
10960 }
10961 else
10962 {
10963 if (current_function_prototype_built_in)
10964 warning_at (DECL_SOURCE_LOCATION (parm),
10965 0, "argument %qD doesn%'t match "
10966 "built-in prototype", parm);
10967 else
10968 {
10969 error_at (DECL_SOURCE_LOCATION (parm),
10970 "argument %qD doesn%'t match prototype", parm);
10971 error_at (current_function_prototype_locus,
10972 "prototype declaration");
10973 }
10974 }
10975 }
10976 }
10977 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10978 }
10979
10980 /* Otherwise, create a prototype that would match. */
10981
10982 else
10983 {
10984 tree actual = NULL_TREE, last = NULL_TREE, type;
10985
10986 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10987 {
10988 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10989 if (last)
10990 TREE_CHAIN (last) = type;
10991 else
10992 actual = type;
10993 last = type;
10994 }
10995 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10996 if (last)
10997 TREE_CHAIN (last) = type;
10998 else
10999 actual = type;
11000
11001 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11002 of the type of this function, but we need to avoid having this
11003 affect the types of other similarly-typed functions, so we must
11004 first force the generation of an identical (but separate) type
11005 node for the relevant function type. The new node we create
11006 will be a variant of the main variant of the original function
11007 type. */
11008
11009 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11010
11011 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11012 }
11013 }
11014
11015 /* Store parameter declarations passed in ARG_INFO into the current
11016 function declaration. */
11017
11018 void
11019 store_parm_decls_from (struct c_arg_info *arg_info)
11020 {
11021 current_function_arg_info = arg_info;
11022 store_parm_decls ();
11023 }
11024
11025 /* Called by walk_tree to look for and update context-less labels
11026 or labels with context in the parent function. */
11027
11028 static tree
11029 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11030 {
11031 tree ctx = static_cast<tree>(data);
11032 if (TREE_CODE (*tp) == LABEL_EXPR
11033 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11034 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11035 {
11036 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11037 *walk_subtrees = 0;
11038 }
11039
11040 return NULL_TREE;
11041 }
11042
11043 /* Store the parameter declarations into the current function declaration.
11044 This is called after parsing the parameter declarations, before
11045 digesting the body of the function.
11046
11047 For an old-style definition, construct a prototype out of the old-style
11048 parameter declarations and inject it into the function's type. */
11049
11050 void
11051 store_parm_decls (void)
11052 {
11053 tree fndecl = current_function_decl;
11054 bool proto;
11055
11056 /* The argument information block for FNDECL. */
11057 struct c_arg_info *arg_info = current_function_arg_info;
11058 current_function_arg_info = 0;
11059
11060 /* True if this definition is written with a prototype. In C23, an
11061 empty argument list was converted to (void) in grokparms; in
11062 older C standard versions, it does not give the function a type
11063 with a prototype for future calls. */
11064 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11065
11066 if (proto)
11067 store_parm_decls_newstyle (fndecl, arg_info);
11068 else
11069 store_parm_decls_oldstyle (fndecl, arg_info);
11070
11071 /* The next call to push_scope will be a function body. */
11072
11073 next_is_function_body = true;
11074
11075 /* Write a record describing this function definition to the prototypes
11076 file (if requested). */
11077
11078 gen_aux_info_record (fndecl, 1, 0, proto);
11079
11080 /* Initialize the RTL code for the function. */
11081 allocate_struct_function (fndecl, false);
11082
11083 if (warn_unused_local_typedefs)
11084 cfun->language = ggc_cleared_alloc<language_function> ();
11085
11086 /* Begin the statement tree for this function. */
11087 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11088
11089 /* ??? Insert the contents of the pending sizes list into the function
11090 to be evaluated. The only reason left to have this is
11091 void foo(int n, int array[n++])
11092 because we throw away the array type in favor of a pointer type, and
11093 thus won't naturally see the SAVE_EXPR containing the increment. All
11094 other pending sizes would be handled by gimplify_parameters. */
11095 if (arg_info->pending_sizes)
11096 {
11097 /* In very special circumstances, e.g. for code like
11098 _Atomic int i = 5;
11099 void f (int a[i += 2]) {}
11100 we need to execute the atomic assignment on function entry.
11101 But in this case, it is not just a straight store, it has the
11102 op= form, which means that build_atomic_assign has generated
11103 gotos, labels, etc. Because at that time the function decl
11104 for F has not been created yet, those labels do not have any
11105 function context. But we have the fndecl now, so update the
11106 labels accordingly. gimplify_expr would crash otherwise.
11107 Or with nested functions the labels could be created with parent
11108 function's context, while when the statement is emitted at the
11109 start of the nested function, it needs the nested function's
11110 context. */
11111 walk_tree_without_duplicates (&arg_info->pending_sizes,
11112 set_labels_context_r, fndecl);
11113 add_stmt (arg_info->pending_sizes);
11114 }
11115 }
11116
11117 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11118 c_finish_omp_declare_simd for function prototypes. No diagnostics
11119 should be done. */
11120
11121 void
11122 temp_store_parm_decls (tree fndecl, tree parms)
11123 {
11124 push_scope ();
11125 for (tree p = parms; p; p = DECL_CHAIN (p))
11126 {
11127 DECL_CONTEXT (p) = fndecl;
11128 if (DECL_NAME (p))
11129 bind (DECL_NAME (p), p, current_scope,
11130 /*invisible=*/false, /*nested=*/false,
11131 UNKNOWN_LOCATION);
11132 }
11133 }
11134
11135 /* Undo what temp_store_parm_decls did. */
11136
11137 void
11138 temp_pop_parm_decls (void)
11139 {
11140 /* Clear all bindings in this temporary scope, so that
11141 pop_scope doesn't create a BLOCK. */
11142 struct c_binding *b = current_scope->bindings;
11143 current_scope->bindings = NULL;
11144 for (; b; b = free_binding_and_advance (b))
11145 {
11146 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11147 || b->decl == error_mark_node);
11148 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11149 I_SYMBOL_BINDING (b->id) = b->shadowed;
11150 if (b->shadowed && b->shadowed->u.type)
11151 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11152 }
11153 pop_scope ();
11154 }
11155 \f
11156
11157 /* Finish up a function declaration and compile that function
11158 all the way to assembler language output. Then free the storage
11159 for the function definition.
11160
11161 This is called after parsing the body of the function definition. */
11162
11163 void
11164 finish_function (location_t end_loc)
11165 {
11166 tree fndecl = current_function_decl;
11167
11168 if (c_dialect_objc ())
11169 objc_finish_function ();
11170
11171 if (TREE_CODE (fndecl) == FUNCTION_DECL
11172 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
11173 {
11174 tree args = DECL_ARGUMENTS (fndecl);
11175 for (; args; args = DECL_CHAIN (args))
11176 {
11177 tree type = TREE_TYPE (args);
11178 if (INTEGRAL_TYPE_P (type)
11179 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11180 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
11181 }
11182 }
11183
11184 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11185 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11186
11187 /* Must mark the RESULT_DECL as being in this function. */
11188
11189 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11190 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11191
11192 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11193 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11194 == integer_type_node && flag_isoc99)
11195 {
11196 /* Hack. We don't want the middle-end to warn that this return
11197 is unreachable, so we mark its location as special. Using
11198 UNKNOWN_LOCATION has the problem that it gets clobbered in
11199 annotate_one_with_locus. A cleaner solution might be to
11200 ensure ! should_carry_locus_p (stmt), but that needs a flag.
11201 */
11202 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11203 }
11204
11205 /* Tie off the statement tree for this function. */
11206 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11207
11208 finish_fname_decls ();
11209
11210 /* Complain if there's no return statement only if option specified on
11211 command line. */
11212 if (warn_return_type > 0
11213 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11214 && !current_function_returns_value && !current_function_returns_null
11215 /* Don't complain if we are no-return. */
11216 && !current_function_returns_abnormally
11217 /* Don't complain if we are declared noreturn. */
11218 && !TREE_THIS_VOLATILE (fndecl)
11219 /* Don't warn for main(). */
11220 && !MAIN_NAME_P (DECL_NAME (fndecl))
11221 /* Or if they didn't actually specify a return type. */
11222 && !C_FUNCTION_IMPLICIT_INT (fndecl)
11223 /* Normally, with -Wreturn-type, flow will complain, but we might
11224 optimize out static functions. */
11225 && !TREE_PUBLIC (fndecl)
11226 && targetm.warn_func_return (fndecl)
11227 && warning (OPT_Wreturn_type,
11228 "no return statement in function returning non-void"))
11229 suppress_warning (fndecl, OPT_Wreturn_type);
11230
11231 /* Complain about parameters that are only set, but never otherwise used. */
11232 if (warn_unused_but_set_parameter)
11233 {
11234 tree decl;
11235
11236 for (decl = DECL_ARGUMENTS (fndecl);
11237 decl;
11238 decl = DECL_CHAIN (decl))
11239 if (TREE_USED (decl)
11240 && TREE_CODE (decl) == PARM_DECL
11241 && !DECL_READ_P (decl)
11242 && DECL_NAME (decl)
11243 && !DECL_ARTIFICIAL (decl)
11244 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
11245 warning_at (DECL_SOURCE_LOCATION (decl),
11246 OPT_Wunused_but_set_parameter,
11247 "parameter %qD set but not used", decl);
11248 }
11249
11250 /* Complain about locally defined typedefs that are not used in this
11251 function. */
11252 maybe_warn_unused_local_typedefs ();
11253
11254 /* Possibly warn about unused parameters. */
11255 if (warn_unused_parameter)
11256 do_warn_unused_parameter (fndecl);
11257
11258 /* Store the end of the function, so that we get good line number
11259 info for the epilogue. */
11260 cfun->function_end_locus = end_loc;
11261
11262 /* Finalize the ELF visibility for the function. */
11263 c_determine_visibility (fndecl);
11264
11265 /* For GNU C extern inline functions disregard inline limits. */
11266 if (DECL_EXTERNAL (fndecl)
11267 && DECL_DECLARED_INLINE_P (fndecl)
11268 && (flag_gnu89_inline
11269 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11270 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11271
11272 /* Genericize before inlining. Delay genericizing nested functions
11273 until their parent function is genericized. Since finalizing
11274 requires GENERIC, delay that as well. */
11275
11276 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11277 && !undef_nested_function)
11278 {
11279 if (!decl_function_context (fndecl))
11280 {
11281 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11282 c_genericize (fndecl);
11283
11284 /* ??? Objc emits functions after finalizing the compilation unit.
11285 This should be cleaned up later and this conditional removed. */
11286 if (symtab->global_info_ready)
11287 {
11288 cgraph_node::add_new_function (fndecl, false);
11289 return;
11290 }
11291 cgraph_node::finalize_function (fndecl, false);
11292 }
11293 else
11294 {
11295 /* Register this function with cgraph just far enough to get it
11296 added to our parent's nested function list. Handy, since the
11297 C front end doesn't have such a list. */
11298 (void) cgraph_node::get_create (fndecl);
11299 }
11300 }
11301
11302 if (!decl_function_context (fndecl))
11303 undef_nested_function = false;
11304
11305 if (cfun->language != NULL)
11306 {
11307 ggc_free (cfun->language);
11308 cfun->language = NULL;
11309 }
11310
11311 /* We're leaving the context of this function, so zap cfun.
11312 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11313 tree_rest_of_compilation. */
11314 set_cfun (NULL);
11315 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11316 current_function_decl = NULL;
11317 }
11318 \f
11319 /* Check the declarations given in a for-loop for satisfying the C99
11320 constraints. If exactly one such decl is found, return it. LOC is
11321 the location of the opening parenthesis of the for loop. The last
11322 parameter allows you to control the "for loop initial declarations
11323 are only allowed in C99 mode". Normally, you should pass
11324 flag_isoc99 as that parameter. But in some cases (Objective-C
11325 foreach loop, for example) we want to run the checks in this
11326 function even if not in C99 mode, so we allow the caller to turn
11327 off the error about not being in C99 mode.
11328 */
11329
11330 tree
11331 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11332 {
11333 struct c_binding *b;
11334 tree one_decl = NULL_TREE;
11335 int n_decls = 0;
11336
11337 if (!turn_off_iso_c99_error)
11338 {
11339 static bool hint = true;
11340 /* If we get here, declarations have been used in a for loop without
11341 the C99 for loop scope. This doesn't make much sense, so don't
11342 allow it. */
11343 auto_diagnostic_group d;
11344 error_at (loc, "%<for%> loop initial declarations "
11345 "are only allowed in C99 or C11 mode");
11346 if (hint)
11347 {
11348 inform (loc,
11349 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11350 "%<-std=gnu11%> to compile your code");
11351 hint = false;
11352 }
11353 return NULL_TREE;
11354 }
11355 else
11356 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11357 "initial declarations");
11358
11359 /* C99 subclause 6.8.5 paragraph 3:
11360
11361 [#3] The declaration part of a for statement shall only
11362 declare identifiers for objects having storage class auto or
11363 register.
11364
11365 It isn't clear whether, in this sentence, "identifiers" binds to
11366 "shall only declare" or to "objects" - that is, whether all identifiers
11367 declared must be identifiers for objects, or whether the restriction
11368 only applies to those that are. (A question on this in comp.std.c
11369 in November 2000 received no answer.) We implement the strictest
11370 interpretation, to avoid creating an extension which later causes
11371 problems.
11372
11373 This constraint was removed in C23. */
11374
11375 for (b = current_scope->bindings; b; b = b->prev)
11376 {
11377 tree id = b->id;
11378 tree decl = b->decl;
11379
11380 if (!id)
11381 continue;
11382
11383 switch (TREE_CODE (decl))
11384 {
11385 case VAR_DECL:
11386 {
11387 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11388 if (TREE_STATIC (decl))
11389 pedwarn_c11 (decl_loc, OPT_Wpedantic,
11390 "declaration of static variable %qD in %<for%> "
11391 "loop initial declaration", decl);
11392 else if (DECL_EXTERNAL (decl))
11393 pedwarn_c11 (decl_loc, OPT_Wpedantic,
11394 "declaration of %<extern%> variable %qD in %<for%> "
11395 "loop initial declaration", decl);
11396 }
11397 break;
11398
11399 case RECORD_TYPE:
11400 pedwarn_c11 (loc, OPT_Wpedantic,
11401 "%<struct %E%> declared in %<for%> loop initial "
11402 "declaration", id);
11403 break;
11404 case UNION_TYPE:
11405 pedwarn_c11 (loc, OPT_Wpedantic,
11406 "%<union %E%> declared in %<for%> loop initial "
11407 "declaration",
11408 id);
11409 break;
11410 case ENUMERAL_TYPE:
11411 pedwarn_c11 (loc, OPT_Wpedantic,
11412 "%<enum %E%> declared in %<for%> loop "
11413 "initial declaration", id);
11414 break;
11415 default:
11416 pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11417 "%qD in %<for%> loop initial declaration", decl);
11418 }
11419
11420 n_decls++;
11421 one_decl = decl;
11422 }
11423
11424 return n_decls == 1 ? one_decl : NULL_TREE;
11425 }
11426 \f
11427 /* Save and reinitialize the variables
11428 used during compilation of a C function. */
11429
11430 void
11431 c_push_function_context (void)
11432 {
11433 struct language_function *p = cfun->language;
11434 /* cfun->language might have been already allocated by the use of
11435 -Wunused-local-typedefs. In that case, just re-use it. */
11436 if (p == NULL)
11437 cfun->language = p = ggc_cleared_alloc<language_function> ();
11438
11439 p->base.x_stmt_tree = c_stmt_tree;
11440 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11441 p->x_in_statement = in_statement;
11442 p->x_switch_stack = c_switch_stack;
11443 p->arg_info = current_function_arg_info;
11444 p->returns_value = current_function_returns_value;
11445 p->returns_null = current_function_returns_null;
11446 p->returns_abnormally = current_function_returns_abnormally;
11447 p->warn_about_return_type = warn_about_return_type;
11448
11449 push_function_context ();
11450 }
11451
11452 /* Restore the variables used during compilation of a C function. */
11453
11454 void
11455 c_pop_function_context (void)
11456 {
11457 struct language_function *p;
11458
11459 pop_function_context ();
11460 p = cfun->language;
11461
11462 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11463 used to store data throughout the life time of the current cfun,
11464 So don't deallocate it. */
11465 if (!warn_unused_local_typedefs)
11466 cfun->language = NULL;
11467
11468 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11469 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11470 {
11471 /* Stop pointing to the local nodes about to be freed. */
11472 /* But DECL_INITIAL must remain nonzero so we know this
11473 was an actual function definition. */
11474 DECL_INITIAL (current_function_decl) = error_mark_node;
11475 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11476 }
11477
11478 c_stmt_tree = p->base.x_stmt_tree;
11479 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11480 in_statement = p->x_in_statement;
11481 c_switch_stack = p->x_switch_stack;
11482 current_function_arg_info = p->arg_info;
11483 current_function_returns_value = p->returns_value;
11484 current_function_returns_null = p->returns_null;
11485 current_function_returns_abnormally = p->returns_abnormally;
11486 warn_about_return_type = p->warn_about_return_type;
11487 }
11488
11489 /* The functions below are required for functionality of doing
11490 function at once processing in the C front end. Currently these
11491 functions are not called from anywhere in the C front end, but as
11492 these changes continue, that will change. */
11493
11494 /* Returns the stmt_tree (if any) to which statements are currently
11495 being added. If there is no active statement-tree, NULL is
11496 returned. */
11497
11498 stmt_tree
11499 current_stmt_tree (void)
11500 {
11501 return &c_stmt_tree;
11502 }
11503
11504 /* Return the global value of T as a symbol. */
11505
11506 tree
11507 identifier_global_value (tree t)
11508 {
11509 struct c_binding *b;
11510
11511 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11512 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11513 return b->decl;
11514
11515 return NULL_TREE;
11516 }
11517
11518 /* Return the global value of tag T as a symbol. */
11519
11520 tree
11521 identifier_global_tag (tree t)
11522 {
11523 struct c_binding *b;
11524
11525 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11526 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11527 return b->decl;
11528
11529 return NULL_TREE;
11530 }
11531
11532 /* Returns true if NAME refers to a built-in function or function-like
11533 operator. */
11534
11535 bool
11536 names_builtin_p (const char *name)
11537 {
11538 tree id = get_identifier (name);
11539 if (tree decl = identifier_global_value (id))
11540 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11541
11542 /* Also detect common reserved C words that aren't strictly built-in
11543 functions. */
11544 switch (C_RID_CODE (id))
11545 {
11546 case RID_BUILTIN_ASSOC_BARRIER:
11547 case RID_BUILTIN_CONVERTVECTOR:
11548 case RID_BUILTIN_HAS_ATTRIBUTE:
11549 case RID_BUILTIN_SHUFFLE:
11550 case RID_BUILTIN_SHUFFLEVECTOR:
11551 case RID_BUILTIN_STDC:
11552 case RID_CHOOSE_EXPR:
11553 case RID_OFFSETOF:
11554 case RID_TYPES_COMPATIBLE_P:
11555 return true;
11556 default:
11557 break;
11558 }
11559
11560 return false;
11561 }
11562
11563 /* In C, the only C-linkage public declaration is at file scope. */
11564
11565 tree
11566 c_linkage_bindings (tree name)
11567 {
11568 return identifier_global_value (name);
11569 }
11570
11571 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11572 otherwise the name is found in ridpointers from RID_INDEX. */
11573
11574 void
11575 record_builtin_type (enum rid rid_index, const char *name, tree type)
11576 {
11577 tree id, decl;
11578 if (name == 0)
11579 id = ridpointers[(int) rid_index];
11580 else
11581 id = get_identifier (name);
11582 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11583 pushdecl (decl);
11584 if (debug_hooks->type_decl)
11585 debug_hooks->type_decl (decl, false);
11586 }
11587
11588 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11589
11590 struct c_parm *
11591 build_c_parm (struct c_declspecs *specs, tree attrs,
11592 struct c_declarator *declarator,
11593 location_t loc)
11594 {
11595 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11596 ret->specs = specs;
11597 ret->attrs = attrs;
11598 ret->declarator = declarator;
11599 ret->loc = loc;
11600 return ret;
11601 }
11602
11603 /* Return a declarator with nested attributes. TARGET is the inner
11604 declarator to which these attributes apply. ATTRS are the
11605 attributes. */
11606
11607 struct c_declarator *
11608 build_attrs_declarator (tree attrs, struct c_declarator *target)
11609 {
11610 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11611 ret->kind = cdk_attrs;
11612 ret->declarator = target;
11613 ret->u.attrs = attrs;
11614 return ret;
11615 }
11616
11617 /* Return a declarator for a function with arguments specified by ARGS
11618 and return type specified by TARGET. */
11619
11620 struct c_declarator *
11621 build_function_declarator (struct c_arg_info *args,
11622 struct c_declarator *target)
11623 {
11624 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11625 ret->kind = cdk_function;
11626 ret->declarator = target;
11627 ret->u.arg_info = args;
11628 return ret;
11629 }
11630
11631 /* Return a declarator for the identifier IDENT (which may be
11632 NULL_TREE for an abstract declarator). */
11633
11634 struct c_declarator *
11635 build_id_declarator (tree ident)
11636 {
11637 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11638 ret->kind = cdk_id;
11639 ret->declarator = 0;
11640 ret->u.id.id = ident;
11641 ret->u.id.attrs = NULL_TREE;
11642 /* Default value - may get reset to a more precise location. */
11643 ret->id_loc = input_location;
11644 return ret;
11645 }
11646
11647 /* Return something to represent absolute declarators containing a *.
11648 TARGET is the absolute declarator that the * contains.
11649 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11650 to apply to the pointer type. */
11651
11652 struct c_declarator *
11653 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11654 struct c_declarator *target)
11655 {
11656 tree attrs;
11657 int quals = 0;
11658 struct c_declarator *itarget = target;
11659 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11660 if (type_quals_attrs)
11661 {
11662 attrs = type_quals_attrs->attrs;
11663 quals = quals_from_declspecs (type_quals_attrs);
11664 if (attrs != NULL_TREE)
11665 itarget = build_attrs_declarator (attrs, target);
11666 }
11667 ret->kind = cdk_pointer;
11668 ret->declarator = itarget;
11669 ret->u.pointer_quals = quals;
11670 return ret;
11671 }
11672
11673 /* Return a pointer to a structure for an empty list of declaration
11674 specifiers. */
11675
11676 struct c_declspecs *
11677 build_null_declspecs (void)
11678 {
11679 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11680 memset (ret, 0, sizeof *ret);
11681 ret->align_log = -1;
11682 ret->typespec_word = cts_none;
11683 ret->storage_class = csc_none;
11684 ret->expr_const_operands = true;
11685 ret->typespec_kind = ctsk_none;
11686 ret->address_space = ADDR_SPACE_GENERIC;
11687 return ret;
11688 }
11689
11690 /* Add the address space ADDRSPACE to the declaration specifiers
11691 SPECS, returning SPECS. */
11692
11693 struct c_declspecs *
11694 declspecs_add_addrspace (location_t location,
11695 struct c_declspecs *specs, addr_space_t as)
11696 {
11697 specs->non_sc_seen_p = true;
11698 specs->declspecs_seen_p = true;
11699 specs->non_std_attrs_seen_p = true;
11700
11701 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11702 && specs->address_space != as)
11703 error ("incompatible address space qualifiers %qs and %qs",
11704 c_addr_space_name (as),
11705 c_addr_space_name (specs->address_space));
11706 else
11707 {
11708 specs->address_space = as;
11709 specs->locations[cdw_address_space] = location;
11710 }
11711 return specs;
11712 }
11713
11714 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11715 returning SPECS. */
11716
11717 struct c_declspecs *
11718 declspecs_add_qual (location_t loc,
11719 struct c_declspecs *specs, tree qual)
11720 {
11721 enum rid i;
11722 bool dupe = false;
11723 specs->non_sc_seen_p = true;
11724 specs->declspecs_seen_p = true;
11725 specs->non_std_attrs_seen_p = true;
11726 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11727 && C_IS_RESERVED_WORD (qual));
11728 i = C_RID_CODE (qual);
11729 location_t prev_loc = UNKNOWN_LOCATION;
11730 switch (i)
11731 {
11732 case RID_CONST:
11733 dupe = specs->const_p;
11734 specs->const_p = true;
11735 prev_loc = specs->locations[cdw_const];
11736 specs->locations[cdw_const] = loc;
11737 break;
11738 case RID_VOLATILE:
11739 dupe = specs->volatile_p;
11740 specs->volatile_p = true;
11741 prev_loc = specs->locations[cdw_volatile];
11742 specs->locations[cdw_volatile] = loc;
11743 break;
11744 case RID_RESTRICT:
11745 dupe = specs->restrict_p;
11746 specs->restrict_p = true;
11747 prev_loc = specs->locations[cdw_restrict];
11748 specs->locations[cdw_restrict] = loc;
11749 break;
11750 case RID_ATOMIC:
11751 dupe = specs->atomic_p;
11752 specs->atomic_p = true;
11753 prev_loc = specs->locations[cdw_atomic];
11754 specs->locations[cdw_atomic] = loc;
11755 break;
11756 default:
11757 gcc_unreachable ();
11758 }
11759 if (dupe)
11760 {
11761 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11762 "duplicate %qE declaration specifier", qual);
11763 if (!warned
11764 && warn_duplicate_decl_specifier
11765 && prev_loc >= RESERVED_LOCATION_COUNT
11766 && !from_macro_expansion_at (prev_loc)
11767 && !from_macro_expansion_at (loc))
11768 warning_at (loc, OPT_Wduplicate_decl_specifier,
11769 "duplicate %qE declaration specifier", qual);
11770 }
11771 return specs;
11772 }
11773
11774 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11775 returning SPECS. */
11776
11777 struct c_declspecs *
11778 declspecs_add_type (location_t loc, struct c_declspecs *specs,
11779 struct c_typespec spec)
11780 {
11781 tree type = spec.spec;
11782 specs->non_sc_seen_p = true;
11783 specs->declspecs_seen_p = true;
11784 specs->non_std_attrs_seen_p = true;
11785 specs->typespec_kind = spec.kind;
11786 if (TREE_DEPRECATED (type))
11787 specs->deprecated_p = true;
11788 if (TREE_UNAVAILABLE (type))
11789 specs->unavailable_p = true;
11790
11791 /* As a type specifier is present, "auto" must be used as a storage
11792 class specifier, not for type deduction. */
11793 if (specs->c23_auto_p)
11794 {
11795 specs->c23_auto_p = false;
11796 if (specs->storage_class != csc_none)
11797 error ("multiple storage classes in declaration specifiers");
11798 else if (specs->thread_p)
11799 error ("%qs used with %<auto%>",
11800 specs->thread_gnu_p ? "__thread" : "_Thread_local");
11801 else if (specs->constexpr_p)
11802 /* auto may only be used with another storage class specifier,
11803 such as constexpr, if the type is inferred. */
11804 error ("%<auto%> used with %<constexpr%>");
11805 else
11806 specs->storage_class = csc_auto;
11807 }
11808
11809 /* Handle type specifier keywords. */
11810 if (TREE_CODE (type) == IDENTIFIER_NODE
11811 && C_IS_RESERVED_WORD (type)
11812 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11813 {
11814 enum rid i = C_RID_CODE (type);
11815 if (specs->type)
11816 {
11817 error_at (loc, "two or more data types in declaration specifiers");
11818 return specs;
11819 }
11820 if ((int) i <= (int) RID_LAST_MODIFIER)
11821 {
11822 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11823 bool dupe = false;
11824 switch (i)
11825 {
11826 case RID_LONG:
11827 if (specs->long_long_p)
11828 {
11829 error_at (loc, "%<long long long%> is too long for GCC");
11830 break;
11831 }
11832 if (specs->long_p)
11833 {
11834 if (specs->typespec_word == cts_double)
11835 {
11836 error_at (loc,
11837 ("both %<long long%> and %<double%> in "
11838 "declaration specifiers"));
11839 break;
11840 }
11841 pedwarn_c90 (loc, OPT_Wlong_long,
11842 "ISO C90 does not support %<long long%>");
11843 specs->long_long_p = 1;
11844 specs->locations[cdw_long_long] = loc;
11845 break;
11846 }
11847 if (specs->short_p)
11848 error_at (loc,
11849 ("both %<long%> and %<short%> in "
11850 "declaration specifiers"));
11851 else if (specs->typespec_word == cts_auto_type)
11852 error_at (loc,
11853 ("both %<long%> and %<__auto_type%> in "
11854 "declaration specifiers"));
11855 else if (specs->typespec_word == cts_void)
11856 error_at (loc,
11857 ("both %<long%> and %<void%> in "
11858 "declaration specifiers"));
11859 else if (specs->typespec_word == cts_int_n)
11860 error_at (loc,
11861 ("both %<long%> and %<__int%d%> in "
11862 "declaration specifiers"),
11863 int_n_data[specs->u.int_n_idx].bitsize);
11864 else if (specs->typespec_word == cts_bool)
11865 error_at (loc,
11866 ("both %<long%> and %<_Bool%> in "
11867 "declaration specifiers"));
11868 else if (specs->typespec_word == cts_bitint)
11869 error_at (loc,
11870 ("both %<long%> and %<_BitInt%> in "
11871 "declaration specifiers"));
11872 else if (specs->typespec_word == cts_char)
11873 error_at (loc,
11874 ("both %<long%> and %<char%> in "
11875 "declaration specifiers"));
11876 else if (specs->typespec_word == cts_float)
11877 error_at (loc,
11878 ("both %<long%> and %<float%> in "
11879 "declaration specifiers"));
11880 else if (specs->typespec_word == cts_floatn_nx)
11881 error_at (loc,
11882 ("both %<long%> and %<_Float%d%s%> in "
11883 "declaration specifiers"),
11884 floatn_nx_types[specs->u.floatn_nx_idx].n,
11885 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11886 ? "x"
11887 : ""));
11888 else if (specs->typespec_word == cts_dfloat32)
11889 error_at (loc,
11890 ("both %<long%> and %<_Decimal32%> in "
11891 "declaration specifiers"));
11892 else if (specs->typespec_word == cts_dfloat64)
11893 error_at (loc,
11894 ("both %<long%> and %<_Decimal64%> in "
11895 "declaration specifiers"));
11896 else if (specs->typespec_word == cts_dfloat128)
11897 error_at (loc,
11898 ("both %<long%> and %<_Decimal128%> in "
11899 "declaration specifiers"));
11900 else
11901 {
11902 specs->long_p = true;
11903 specs->locations[cdw_long] = loc;
11904 }
11905 break;
11906 case RID_SHORT:
11907 dupe = specs->short_p;
11908 if (specs->long_p)
11909 error_at (loc,
11910 ("both %<long%> and %<short%> in "
11911 "declaration specifiers"));
11912 else if (specs->typespec_word == cts_auto_type)
11913 error_at (loc,
11914 ("both %<short%> and %<__auto_type%> in "
11915 "declaration specifiers"));
11916 else if (specs->typespec_word == cts_void)
11917 error_at (loc,
11918 ("both %<short%> and %<void%> in "
11919 "declaration specifiers"));
11920 else if (specs->typespec_word == cts_int_n)
11921 error_at (loc,
11922 ("both %<short%> and %<__int%d%> in "
11923 "declaration specifiers"),
11924 int_n_data[specs->u.int_n_idx].bitsize);
11925 else if (specs->typespec_word == cts_bool)
11926 error_at (loc,
11927 ("both %<short%> and %<_Bool%> in "
11928 "declaration specifiers"));
11929 else if (specs->typespec_word == cts_bitint)
11930 error_at (loc,
11931 ("both %<short%> and %<_BitInt%> in "
11932 "declaration specifiers"));
11933 else if (specs->typespec_word == cts_char)
11934 error_at (loc,
11935 ("both %<short%> and %<char%> in "
11936 "declaration specifiers"));
11937 else if (specs->typespec_word == cts_float)
11938 error_at (loc,
11939 ("both %<short%> and %<float%> in "
11940 "declaration specifiers"));
11941 else if (specs->typespec_word == cts_double)
11942 error_at (loc,
11943 ("both %<short%> and %<double%> in "
11944 "declaration specifiers"));
11945 else if (specs->typespec_word == cts_floatn_nx)
11946 error_at (loc,
11947 ("both %<short%> and %<_Float%d%s%> in "
11948 "declaration specifiers"),
11949 floatn_nx_types[specs->u.floatn_nx_idx].n,
11950 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11951 ? "x"
11952 : ""));
11953 else if (specs->typespec_word == cts_dfloat32)
11954 error_at (loc,
11955 ("both %<short%> and %<_Decimal32%> in "
11956 "declaration specifiers"));
11957 else if (specs->typespec_word == cts_dfloat64)
11958 error_at (loc,
11959 ("both %<short%> and %<_Decimal64%> in "
11960 "declaration specifiers"));
11961 else if (specs->typespec_word == cts_dfloat128)
11962 error_at (loc,
11963 ("both %<short%> and %<_Decimal128%> in "
11964 "declaration specifiers"));
11965 else
11966 {
11967 specs->short_p = true;
11968 specs->locations[cdw_short] = loc;
11969 }
11970 break;
11971 case RID_SIGNED:
11972 dupe = specs->signed_p;
11973 if (specs->unsigned_p)
11974 error_at (loc,
11975 ("both %<signed%> and %<unsigned%> in "
11976 "declaration specifiers"));
11977 else if (specs->typespec_word == cts_auto_type)
11978 error_at (loc,
11979 ("both %<signed%> and %<__auto_type%> in "
11980 "declaration specifiers"));
11981 else if (specs->typespec_word == cts_void)
11982 error_at (loc,
11983 ("both %<signed%> and %<void%> in "
11984 "declaration specifiers"));
11985 else if (specs->typespec_word == cts_bool)
11986 error_at (loc,
11987 ("both %<signed%> and %<_Bool%> in "
11988 "declaration specifiers"));
11989 else if (specs->typespec_word == cts_float)
11990 error_at (loc,
11991 ("both %<signed%> and %<float%> in "
11992 "declaration specifiers"));
11993 else if (specs->typespec_word == cts_double)
11994 error_at (loc,
11995 ("both %<signed%> and %<double%> in "
11996 "declaration specifiers"));
11997 else if (specs->typespec_word == cts_floatn_nx)
11998 error_at (loc,
11999 ("both %<signed%> and %<_Float%d%s%> in "
12000 "declaration specifiers"),
12001 floatn_nx_types[specs->u.floatn_nx_idx].n,
12002 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12003 ? "x"
12004 : ""));
12005 else if (specs->typespec_word == cts_dfloat32)
12006 error_at (loc,
12007 ("both %<signed%> and %<_Decimal32%> in "
12008 "declaration specifiers"));
12009 else if (specs->typespec_word == cts_dfloat64)
12010 error_at (loc,
12011 ("both %<signed%> and %<_Decimal64%> in "
12012 "declaration specifiers"));
12013 else if (specs->typespec_word == cts_dfloat128)
12014 error_at (loc,
12015 ("both %<signed%> and %<_Decimal128%> in "
12016 "declaration specifiers"));
12017 else
12018 {
12019 specs->signed_p = true;
12020 specs->locations[cdw_signed] = loc;
12021 }
12022 break;
12023 case RID_UNSIGNED:
12024 dupe = specs->unsigned_p;
12025 if (specs->signed_p)
12026 error_at (loc,
12027 ("both %<signed%> and %<unsigned%> in "
12028 "declaration specifiers"));
12029 else if (specs->typespec_word == cts_auto_type)
12030 error_at (loc,
12031 ("both %<unsigned%> and %<__auto_type%> in "
12032 "declaration specifiers"));
12033 else if (specs->typespec_word == cts_void)
12034 error_at (loc,
12035 ("both %<unsigned%> and %<void%> in "
12036 "declaration specifiers"));
12037 else if (specs->typespec_word == cts_bool)
12038 error_at (loc,
12039 ("both %<unsigned%> and %<_Bool%> in "
12040 "declaration specifiers"));
12041 else if (specs->typespec_word == cts_float)
12042 error_at (loc,
12043 ("both %<unsigned%> and %<float%> in "
12044 "declaration specifiers"));
12045 else if (specs->typespec_word == cts_double)
12046 error_at (loc,
12047 ("both %<unsigned%> and %<double%> in "
12048 "declaration specifiers"));
12049 else if (specs->typespec_word == cts_floatn_nx)
12050 error_at (loc,
12051 ("both %<unsigned%> and %<_Float%d%s%> in "
12052 "declaration specifiers"),
12053 floatn_nx_types[specs->u.floatn_nx_idx].n,
12054 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12055 ? "x"
12056 : ""));
12057 else if (specs->typespec_word == cts_dfloat32)
12058 error_at (loc,
12059 ("both %<unsigned%> and %<_Decimal32%> in "
12060 "declaration specifiers"));
12061 else if (specs->typespec_word == cts_dfloat64)
12062 error_at (loc,
12063 ("both %<unsigned%> and %<_Decimal64%> in "
12064 "declaration specifiers"));
12065 else if (specs->typespec_word == cts_dfloat128)
12066 error_at (loc,
12067 ("both %<unsigned%> and %<_Decimal128%> in "
12068 "declaration specifiers"));
12069 else
12070 {
12071 specs->unsigned_p = true;
12072 specs->locations[cdw_unsigned] = loc;
12073 }
12074 break;
12075 case RID_COMPLEX:
12076 dupe = specs->complex_p;
12077 if (!in_system_header_at (loc))
12078 pedwarn_c90 (loc, OPT_Wpedantic,
12079 "ISO C90 does not support complex types");
12080 if (specs->typespec_word == cts_auto_type)
12081 error_at (loc,
12082 ("both %<complex%> and %<__auto_type%> in "
12083 "declaration specifiers"));
12084 else if (specs->typespec_word == cts_void)
12085 error_at (loc,
12086 ("both %<complex%> and %<void%> in "
12087 "declaration specifiers"));
12088 else if (specs->typespec_word == cts_bool)
12089 error_at (loc,
12090 ("both %<complex%> and %<_Bool%> in "
12091 "declaration specifiers"));
12092 else if (specs->typespec_word == cts_bitint)
12093 error_at (loc,
12094 ("both %<complex%> and %<_BitInt%> in "
12095 "declaration specifiers"));
12096 else if (specs->typespec_word == cts_dfloat32)
12097 error_at (loc,
12098 ("both %<complex%> and %<_Decimal32%> in "
12099 "declaration specifiers"));
12100 else if (specs->typespec_word == cts_dfloat64)
12101 error_at (loc,
12102 ("both %<complex%> and %<_Decimal64%> in "
12103 "declaration specifiers"));
12104 else if (specs->typespec_word == cts_dfloat128)
12105 error_at (loc,
12106 ("both %<complex%> and %<_Decimal128%> in "
12107 "declaration specifiers"));
12108 else if (specs->typespec_word == cts_fract)
12109 error_at (loc,
12110 ("both %<complex%> and %<_Fract%> in "
12111 "declaration specifiers"));
12112 else if (specs->typespec_word == cts_accum)
12113 error_at (loc,
12114 ("both %<complex%> and %<_Accum%> in "
12115 "declaration specifiers"));
12116 else if (specs->saturating_p)
12117 error_at (loc,
12118 ("both %<complex%> and %<_Sat%> in "
12119 "declaration specifiers"));
12120 else
12121 {
12122 specs->complex_p = true;
12123 specs->locations[cdw_complex] = loc;
12124 }
12125 break;
12126 case RID_SAT:
12127 dupe = specs->saturating_p;
12128 pedwarn (loc, OPT_Wpedantic,
12129 "ISO C does not support saturating types");
12130 if (specs->typespec_word == cts_int_n)
12131 {
12132 error_at (loc,
12133 ("both %<_Sat%> and %<__int%d%> in "
12134 "declaration specifiers"),
12135 int_n_data[specs->u.int_n_idx].bitsize);
12136 }
12137 else if (specs->typespec_word == cts_auto_type)
12138 error_at (loc,
12139 ("both %<_Sat%> and %<__auto_type%> in "
12140 "declaration specifiers"));
12141 else if (specs->typespec_word == cts_void)
12142 error_at (loc,
12143 ("both %<_Sat%> and %<void%> in "
12144 "declaration specifiers"));
12145 else if (specs->typespec_word == cts_bool)
12146 error_at (loc,
12147 ("both %<_Sat%> and %<_Bool%> in "
12148 "declaration specifiers"));
12149 else if (specs->typespec_word == cts_bitint)
12150 error_at (loc,
12151 ("both %<_Sat%> and %<_BitInt%> in "
12152 "declaration specifiers"));
12153 else if (specs->typespec_word == cts_char)
12154 error_at (loc,
12155 ("both %<_Sat%> and %<char%> in "
12156 "declaration specifiers"));
12157 else if (specs->typespec_word == cts_int)
12158 error_at (loc,
12159 ("both %<_Sat%> and %<int%> in "
12160 "declaration specifiers"));
12161 else if (specs->typespec_word == cts_float)
12162 error_at (loc,
12163 ("both %<_Sat%> and %<float%> in "
12164 "declaration specifiers"));
12165 else if (specs->typespec_word == cts_double)
12166 error_at (loc,
12167 ("both %<_Sat%> and %<double%> in "
12168 "declaration specifiers"));
12169 else if (specs->typespec_word == cts_floatn_nx)
12170 error_at (loc,
12171 ("both %<_Sat%> and %<_Float%d%s%> in "
12172 "declaration specifiers"),
12173 floatn_nx_types[specs->u.floatn_nx_idx].n,
12174 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12175 ? "x"
12176 : ""));
12177 else if (specs->typespec_word == cts_dfloat32)
12178 error_at (loc,
12179 ("both %<_Sat%> and %<_Decimal32%> in "
12180 "declaration specifiers"));
12181 else if (specs->typespec_word == cts_dfloat64)
12182 error_at (loc,
12183 ("both %<_Sat%> and %<_Decimal64%> in "
12184 "declaration specifiers"));
12185 else if (specs->typespec_word == cts_dfloat128)
12186 error_at (loc,
12187 ("both %<_Sat%> and %<_Decimal128%> in "
12188 "declaration specifiers"));
12189 else if (specs->complex_p)
12190 error_at (loc,
12191 ("both %<_Sat%> and %<complex%> in "
12192 "declaration specifiers"));
12193 else
12194 {
12195 specs->saturating_p = true;
12196 specs->locations[cdw_saturating] = loc;
12197 }
12198 break;
12199 default:
12200 gcc_unreachable ();
12201 }
12202
12203 if (dupe)
12204 error_at (loc, "duplicate %qE", type);
12205
12206 return specs;
12207 }
12208 else
12209 {
12210 /* "void", "_Bool", "char", "int", "float", "double",
12211 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12212 "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12213 "__auto_type". */
12214 if (specs->typespec_word != cts_none)
12215 {
12216 error_at (loc,
12217 "two or more data types in declaration specifiers");
12218 return specs;
12219 }
12220 switch (i)
12221 {
12222 case RID_AUTO_TYPE:
12223 if (specs->long_p)
12224 error_at (loc,
12225 ("both %<long%> and %<__auto_type%> in "
12226 "declaration specifiers"));
12227 else if (specs->short_p)
12228 error_at (loc,
12229 ("both %<short%> and %<__auto_type%> in "
12230 "declaration specifiers"));
12231 else if (specs->signed_p)
12232 error_at (loc,
12233 ("both %<signed%> and %<__auto_type%> in "
12234 "declaration specifiers"));
12235 else if (specs->unsigned_p)
12236 error_at (loc,
12237 ("both %<unsigned%> and %<__auto_type%> in "
12238 "declaration specifiers"));
12239 else if (specs->complex_p)
12240 error_at (loc,
12241 ("both %<complex%> and %<__auto_type%> in "
12242 "declaration specifiers"));
12243 else if (specs->saturating_p)
12244 error_at (loc,
12245 ("both %<_Sat%> and %<__auto_type%> in "
12246 "declaration specifiers"));
12247 else
12248 {
12249 specs->typespec_word = cts_auto_type;
12250 specs->locations[cdw_typespec] = loc;
12251 }
12252 return specs;
12253 case RID_INT_N_0:
12254 case RID_INT_N_1:
12255 case RID_INT_N_2:
12256 case RID_INT_N_3:
12257 specs->u.int_n_idx = i - RID_INT_N_0;
12258 if (!in_system_header_at (input_location)
12259 /* If the INT_N type ends in "__", and so is of the format
12260 "__intN__", don't pedwarn. */
12261 && (strncmp (IDENTIFIER_POINTER (type)
12262 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12263 pedwarn (loc, OPT_Wpedantic,
12264 "ISO C does not support %<__int%d%> types",
12265 int_n_data[specs->u.int_n_idx].bitsize);
12266
12267 if (specs->long_p)
12268 error_at (loc,
12269 ("both %<__int%d%> and %<long%> in "
12270 "declaration specifiers"),
12271 int_n_data[specs->u.int_n_idx].bitsize);
12272 else if (specs->saturating_p)
12273 error_at (loc,
12274 ("both %<_Sat%> and %<__int%d%> in "
12275 "declaration specifiers"),
12276 int_n_data[specs->u.int_n_idx].bitsize);
12277 else if (specs->short_p)
12278 error_at (loc,
12279 ("both %<__int%d%> and %<short%> in "
12280 "declaration specifiers"),
12281 int_n_data[specs->u.int_n_idx].bitsize);
12282 else if (! int_n_enabled_p[specs->u.int_n_idx])
12283 {
12284 specs->typespec_word = cts_int_n;
12285 error_at (loc,
12286 "%<__int%d%> is not supported on this target",
12287 int_n_data[specs->u.int_n_idx].bitsize);
12288 }
12289 else
12290 {
12291 specs->typespec_word = cts_int_n;
12292 specs->locations[cdw_typespec] = loc;
12293 }
12294 return specs;
12295 case RID_VOID:
12296 if (specs->long_p)
12297 error_at (loc,
12298 ("both %<long%> and %<void%> in "
12299 "declaration specifiers"));
12300 else if (specs->short_p)
12301 error_at (loc,
12302 ("both %<short%> and %<void%> in "
12303 "declaration specifiers"));
12304 else if (specs->signed_p)
12305 error_at (loc,
12306 ("both %<signed%> and %<void%> in "
12307 "declaration specifiers"));
12308 else if (specs->unsigned_p)
12309 error_at (loc,
12310 ("both %<unsigned%> and %<void%> in "
12311 "declaration specifiers"));
12312 else if (specs->complex_p)
12313 error_at (loc,
12314 ("both %<complex%> and %<void%> in "
12315 "declaration specifiers"));
12316 else if (specs->saturating_p)
12317 error_at (loc,
12318 ("both %<_Sat%> and %<void%> in "
12319 "declaration specifiers"));
12320 else
12321 {
12322 specs->typespec_word = cts_void;
12323 specs->locations[cdw_typespec] = loc;
12324 }
12325 return specs;
12326 case RID_BOOL:
12327 if (!in_system_header_at (loc))
12328 pedwarn_c90 (loc, OPT_Wpedantic,
12329 "ISO C90 does not support boolean types");
12330 if (specs->long_p)
12331 error_at (loc,
12332 ("both %<long%> and %<_Bool%> in "
12333 "declaration specifiers"));
12334 else if (specs->short_p)
12335 error_at (loc,
12336 ("both %<short%> and %<_Bool%> in "
12337 "declaration specifiers"));
12338 else if (specs->signed_p)
12339 error_at (loc,
12340 ("both %<signed%> and %<_Bool%> in "
12341 "declaration specifiers"));
12342 else if (specs->unsigned_p)
12343 error_at (loc,
12344 ("both %<unsigned%> and %<_Bool%> in "
12345 "declaration specifiers"));
12346 else if (specs->complex_p)
12347 error_at (loc,
12348 ("both %<complex%> and %<_Bool%> in "
12349 "declaration specifiers"));
12350 else if (specs->saturating_p)
12351 error_at (loc,
12352 ("both %<_Sat%> and %<_Bool%> in "
12353 "declaration specifiers"));
12354 else
12355 {
12356 specs->typespec_word = cts_bool;
12357 specs->locations[cdw_typespec] = loc;
12358 }
12359 return specs;
12360 case RID_CHAR:
12361 if (specs->long_p)
12362 error_at (loc,
12363 ("both %<long%> and %<char%> in "
12364 "declaration specifiers"));
12365 else if (specs->short_p)
12366 error_at (loc,
12367 ("both %<short%> and %<char%> in "
12368 "declaration specifiers"));
12369 else if (specs->saturating_p)
12370 error_at (loc,
12371 ("both %<_Sat%> and %<char%> in "
12372 "declaration specifiers"));
12373 else
12374 {
12375 specs->typespec_word = cts_char;
12376 specs->locations[cdw_typespec] = loc;
12377 }
12378 return specs;
12379 case RID_INT:
12380 if (specs->saturating_p)
12381 error_at (loc,
12382 ("both %<_Sat%> and %<int%> in "
12383 "declaration specifiers"));
12384 else
12385 {
12386 specs->typespec_word = cts_int;
12387 specs->locations[cdw_typespec] = loc;
12388 }
12389 return specs;
12390 case RID_FLOAT:
12391 if (specs->long_p)
12392 error_at (loc,
12393 ("both %<long%> and %<float%> in "
12394 "declaration specifiers"));
12395 else if (specs->short_p)
12396 error_at (loc,
12397 ("both %<short%> and %<float%> in "
12398 "declaration specifiers"));
12399 else if (specs->signed_p)
12400 error_at (loc,
12401 ("both %<signed%> and %<float%> in "
12402 "declaration specifiers"));
12403 else if (specs->unsigned_p)
12404 error_at (loc,
12405 ("both %<unsigned%> and %<float%> in "
12406 "declaration specifiers"));
12407 else if (specs->saturating_p)
12408 error_at (loc,
12409 ("both %<_Sat%> and %<float%> in "
12410 "declaration specifiers"));
12411 else
12412 {
12413 specs->typespec_word = cts_float;
12414 specs->locations[cdw_typespec] = loc;
12415 }
12416 return specs;
12417 case RID_DOUBLE:
12418 if (specs->long_long_p)
12419 error_at (loc,
12420 ("both %<long long%> and %<double%> in "
12421 "declaration specifiers"));
12422 else if (specs->short_p)
12423 error_at (loc,
12424 ("both %<short%> and %<double%> in "
12425 "declaration specifiers"));
12426 else if (specs->signed_p)
12427 error_at (loc,
12428 ("both %<signed%> and %<double%> in "
12429 "declaration specifiers"));
12430 else if (specs->unsigned_p)
12431 error_at (loc,
12432 ("both %<unsigned%> and %<double%> in "
12433 "declaration specifiers"));
12434 else if (specs->saturating_p)
12435 error_at (loc,
12436 ("both %<_Sat%> and %<double%> in "
12437 "declaration specifiers"));
12438 else
12439 {
12440 specs->typespec_word = cts_double;
12441 specs->locations[cdw_typespec] = loc;
12442 }
12443 return specs;
12444 CASE_RID_FLOATN_NX:
12445 specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12446 if (!in_system_header_at (input_location))
12447 pedwarn_c11 (loc, OPT_Wpedantic,
12448 "ISO C does not support the %<_Float%d%s%> type"
12449 " before C23",
12450 floatn_nx_types[specs->u.floatn_nx_idx].n,
12451 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12452 ? "x"
12453 : ""));
12454
12455 if (specs->long_p)
12456 error_at (loc,
12457 ("both %<long%> and %<_Float%d%s%> in "
12458 "declaration specifiers"),
12459 floatn_nx_types[specs->u.floatn_nx_idx].n,
12460 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12461 ? "x"
12462 : ""));
12463 else if (specs->short_p)
12464 error_at (loc,
12465 ("both %<short%> and %<_Float%d%s%> in "
12466 "declaration specifiers"),
12467 floatn_nx_types[specs->u.floatn_nx_idx].n,
12468 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12469 ? "x"
12470 : ""));
12471 else if (specs->signed_p)
12472 error_at (loc,
12473 ("both %<signed%> and %<_Float%d%s%> in "
12474 "declaration specifiers"),
12475 floatn_nx_types[specs->u.floatn_nx_idx].n,
12476 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12477 ? "x"
12478 : ""));
12479 else if (specs->unsigned_p)
12480 error_at (loc,
12481 ("both %<unsigned%> and %<_Float%d%s%> in "
12482 "declaration specifiers"),
12483 floatn_nx_types[specs->u.floatn_nx_idx].n,
12484 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12485 ? "x"
12486 : ""));
12487 else if (specs->saturating_p)
12488 error_at (loc,
12489 ("both %<_Sat%> and %<_Float%d%s%> in "
12490 "declaration specifiers"),
12491 floatn_nx_types[specs->u.floatn_nx_idx].n,
12492 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12493 ? "x"
12494 : ""));
12495 else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12496 {
12497 specs->typespec_word = cts_floatn_nx;
12498 error_at (loc,
12499 "%<_Float%d%s%> is not supported on this target",
12500 floatn_nx_types[specs->u.floatn_nx_idx].n,
12501 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12502 ? "x"
12503 : ""));
12504 }
12505 else
12506 {
12507 specs->typespec_word = cts_floatn_nx;
12508 specs->locations[cdw_typespec] = loc;
12509 }
12510 return specs;
12511 case RID_DFLOAT32:
12512 case RID_DFLOAT64:
12513 case RID_DFLOAT128:
12514 {
12515 const char *str;
12516 if (i == RID_DFLOAT32)
12517 str = "_Decimal32";
12518 else if (i == RID_DFLOAT64)
12519 str = "_Decimal64";
12520 else
12521 str = "_Decimal128";
12522 if (specs->long_long_p)
12523 error_at (loc,
12524 ("both %<long long%> and %qs in "
12525 "declaration specifiers"),
12526 str);
12527 if (specs->long_p)
12528 error_at (loc,
12529 ("both %<long%> and %qs in "
12530 "declaration specifiers"),
12531 str);
12532 else if (specs->short_p)
12533 error_at (loc,
12534 ("both %<short%> and %qs in "
12535 "declaration specifiers"),
12536 str);
12537 else if (specs->signed_p)
12538 error_at (loc,
12539 ("both %<signed%> and %qs in "
12540 "declaration specifiers"),
12541 str);
12542 else if (specs->unsigned_p)
12543 error_at (loc,
12544 ("both %<unsigned%> and %qs in "
12545 "declaration specifiers"),
12546 str);
12547 else if (specs->complex_p)
12548 error_at (loc,
12549 ("both %<complex%> and %qs in "
12550 "declaration specifiers"),
12551 str);
12552 else if (specs->saturating_p)
12553 error_at (loc,
12554 ("both %<_Sat%> and %qs in "
12555 "declaration specifiers"),
12556 str);
12557 else if (i == RID_DFLOAT32)
12558 specs->typespec_word = cts_dfloat32;
12559 else if (i == RID_DFLOAT64)
12560 specs->typespec_word = cts_dfloat64;
12561 else
12562 specs->typespec_word = cts_dfloat128;
12563 specs->locations[cdw_typespec] = loc;
12564 }
12565 if (!targetm.decimal_float_supported_p ())
12566 error_at (loc,
12567 ("decimal floating-point not supported "
12568 "for this target"));
12569 pedwarn_c11 (loc, OPT_Wpedantic,
12570 "ISO C does not support decimal floating-point "
12571 "before C23");
12572 return specs;
12573 case RID_FRACT:
12574 case RID_ACCUM:
12575 {
12576 const char *str;
12577 if (i == RID_FRACT)
12578 str = "_Fract";
12579 else
12580 str = "_Accum";
12581 if (specs->complex_p)
12582 error_at (loc,
12583 ("both %<complex%> and %qs in "
12584 "declaration specifiers"),
12585 str);
12586 else if (i == RID_FRACT)
12587 specs->typespec_word = cts_fract;
12588 else
12589 specs->typespec_word = cts_accum;
12590 specs->locations[cdw_typespec] = loc;
12591 }
12592 if (!targetm.fixed_point_supported_p ())
12593 error_at (loc,
12594 "fixed-point types not supported for this target");
12595 pedwarn (loc, OPT_Wpedantic,
12596 "ISO C does not support fixed-point types");
12597 return specs;
12598 case RID_BITINT:
12599 if (specs->long_p)
12600 error_at (loc,
12601 ("both %<long%> and %<_BitInt%> in "
12602 "declaration specifiers"));
12603 else if (specs->short_p)
12604 error_at (loc,
12605 ("both %<short%> and %<_BitInt%> in "
12606 "declaration specifiers"));
12607 else if (specs->complex_p)
12608 error_at (loc,
12609 ("both %<complex%> and %<_BitInt%> in "
12610 "declaration specifiers"));
12611 else if (specs->saturating_p)
12612 error_at (loc,
12613 ("both %<_Sat%> and %<_BitInt%> in "
12614 "declaration specifiers"));
12615 else
12616 {
12617 specs->typespec_word = cts_bitint;
12618 specs->locations[cdw_typespec] = loc;
12619 specs->u.bitint_prec = -1;
12620 if (error_operand_p (spec.expr))
12621 return specs;
12622 if (TREE_CODE (spec.expr) != INTEGER_CST
12623 || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12624 {
12625 error_at (loc, "%<_BitInt%> argument is not an integer "
12626 "constant expression");
12627 return specs;
12628 }
12629 if (tree_int_cst_sgn (spec.expr) <= 0)
12630 {
12631 error_at (loc, "%<_BitInt%> argument %qE is not a "
12632 "positive integer constant expression",
12633 spec.expr);
12634 return specs;
12635 }
12636 if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12637 {
12638 error_at (loc, "%<_BitInt%> argument %qE is larger than "
12639 "%<BITINT_MAXWIDTH%> %qd",
12640 spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12641 return specs;
12642 }
12643 specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12644 struct bitint_info info;
12645 if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12646 &info))
12647 {
12648 sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12649 "this target", specs->u.bitint_prec);
12650 specs->u.bitint_prec = -1;
12651 return specs;
12652 }
12653 }
12654 return specs;
12655 default:
12656 /* ObjC reserved word "id", handled below. */
12657 break;
12658 }
12659 }
12660 }
12661
12662 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12663 form of ObjC type, cases such as "int" and "long" being handled
12664 above), a TYPE (struct, union, enum and typeof specifiers) or an
12665 ERROR_MARK. In none of these cases may there have previously
12666 been any type specifiers. */
12667 if (specs->type || specs->typespec_word != cts_none
12668 || specs->long_p || specs->short_p || specs->signed_p
12669 || specs->unsigned_p || specs->complex_p)
12670 error_at (loc, "two or more data types in declaration specifiers");
12671 else if (TREE_CODE (type) == TYPE_DECL)
12672 {
12673 specs->type = TREE_TYPE (type);
12674 if (TREE_TYPE (type) != error_mark_node)
12675 {
12676 specs->decl_attr = DECL_ATTRIBUTES (type);
12677 specs->typedef_p = true;
12678 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12679 specs->locations[cdw_typedef] = loc;
12680
12681 /* If this typedef name is defined in a struct, then a C++
12682 lookup would return a different value. */
12683 if (warn_cxx_compat
12684 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12685 warning_at (loc, OPT_Wc___compat,
12686 "C++ lookup of %qD would return a field, not a type",
12687 type);
12688
12689 /* If we are parsing a struct, record that a struct field
12690 used a typedef. */
12691 if (warn_cxx_compat && struct_parse_info != NULL)
12692 struct_parse_info->typedefs_seen.safe_push (type);
12693 }
12694 }
12695 else if (TREE_CODE (type) == IDENTIFIER_NODE)
12696 {
12697 tree t = lookup_name (type);
12698 if (!t || TREE_CODE (t) != TYPE_DECL)
12699 error_at (loc, "%qE fails to be a typedef or built in type", type);
12700 else if (TREE_TYPE (t) == error_mark_node)
12701 ;
12702 else
12703 {
12704 specs->type = TREE_TYPE (t);
12705 specs->locations[cdw_typespec] = loc;
12706 }
12707 }
12708 else
12709 {
12710 if (TREE_CODE (type) != ERROR_MARK)
12711 {
12712 if (spec.kind == ctsk_typeof)
12713 {
12714 specs->typedef_p = true;
12715 specs->locations[cdw_typedef] = loc;
12716 }
12717 if (spec.expr)
12718 {
12719 if (specs->expr)
12720 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12721 specs->expr, spec.expr);
12722 else
12723 specs->expr = spec.expr;
12724 specs->expr_const_operands &= spec.expr_const_operands;
12725 }
12726 }
12727 specs->type = type;
12728 if (spec.has_enum_type_specifier
12729 && spec.kind != ctsk_tagdef)
12730 specs->enum_type_specifier_ref_p = true;
12731 }
12732
12733 return specs;
12734 }
12735
12736 /* Add the storage class specifier or function specifier SCSPEC to the
12737 declaration specifiers SPECS, returning SPECS. */
12738
12739 struct c_declspecs *
12740 declspecs_add_scspec (location_t loc,
12741 struct c_declspecs *specs,
12742 tree scspec)
12743 {
12744 enum rid i;
12745 enum c_storage_class n = csc_none;
12746 bool dupe = false;
12747 specs->declspecs_seen_p = true;
12748 specs->non_std_attrs_seen_p = true;
12749 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12750 && C_IS_RESERVED_WORD (scspec));
12751 i = C_RID_CODE (scspec);
12752 if (specs->non_sc_seen_p)
12753 warning (OPT_Wold_style_declaration,
12754 "%qE is not at beginning of declaration", scspec);
12755 switch (i)
12756 {
12757 case RID_INLINE:
12758 /* C99 permits duplicate inline. Although of doubtful utility,
12759 it seems simplest to permit it in gnu89 mode as well, as
12760 there is also little utility in maintaining this as a
12761 difference between gnu89 and C99 inline. */
12762 dupe = false;
12763 specs->inline_p = true;
12764 specs->locations[cdw_inline] = loc;
12765 break;
12766 case RID_NORETURN:
12767 /* Duplicate _Noreturn is permitted. */
12768 dupe = false;
12769 specs->noreturn_p = true;
12770 specs->locations[cdw_noreturn] = loc;
12771 break;
12772 case RID_THREAD:
12773 dupe = specs->thread_p;
12774 if (specs->storage_class == csc_auto)
12775 error ("%qE used with %<auto%>", scspec);
12776 else if (specs->storage_class == csc_register)
12777 error ("%qE used with %<register%>", scspec);
12778 else if (specs->storage_class == csc_typedef)
12779 error ("%qE used with %<typedef%>", scspec);
12780 else if (specs->constexpr_p)
12781 error ("%qE used with %<constexpr%>", scspec);
12782 else
12783 {
12784 specs->thread_p = true;
12785 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12786 "__thread") == 0);
12787 /* A diagnostic is not required for the use of this
12788 identifier in the implementation namespace; only diagnose
12789 it for the C11 spelling because of existing code using
12790 the other spelling. */
12791 if (!specs->thread_gnu_p)
12792 {
12793 if (flag_isoc99)
12794 pedwarn_c99 (loc, OPT_Wpedantic,
12795 "ISO C99 does not support %qE", scspec);
12796 else
12797 pedwarn_c99 (loc, OPT_Wpedantic,
12798 "ISO C90 does not support %qE", scspec);
12799 }
12800 specs->locations[cdw_thread] = loc;
12801 }
12802 break;
12803 case RID_AUTO:
12804 if (flag_isoc23
12805 && specs->typespec_kind == ctsk_none
12806 && specs->storage_class != csc_typedef)
12807 {
12808 /* "auto" potentially used for type deduction. */
12809 if (specs->c23_auto_p)
12810 error ("duplicate %qE", scspec);
12811 specs->c23_auto_p = true;
12812 return specs;
12813 }
12814 n = csc_auto;
12815 /* auto may only be used with another storage class specifier,
12816 such as constexpr, if the type is inferred. */
12817 if (specs->constexpr_p)
12818 error ("%qE used with %<constexpr%>", scspec);
12819 break;
12820 case RID_EXTERN:
12821 n = csc_extern;
12822 /* Diagnose "__thread extern". */
12823 if (specs->thread_p && specs->thread_gnu_p)
12824 error ("%<__thread%> before %<extern%>");
12825 break;
12826 case RID_REGISTER:
12827 n = csc_register;
12828 break;
12829 case RID_STATIC:
12830 n = csc_static;
12831 /* Diagnose "__thread static". */
12832 if (specs->thread_p && specs->thread_gnu_p)
12833 error ("%<__thread%> before %<static%>");
12834 break;
12835 case RID_TYPEDEF:
12836 n = csc_typedef;
12837 if (specs->c23_auto_p)
12838 {
12839 error ("%<typedef%> used with %<auto%>");
12840 specs->c23_auto_p = false;
12841 }
12842 break;
12843 case RID_CONSTEXPR:
12844 dupe = specs->constexpr_p;
12845 if (specs->storage_class == csc_extern)
12846 error ("%qE used with %<extern%>", scspec);
12847 else if (specs->storage_class == csc_typedef)
12848 error ("%qE used with %<typedef%>", scspec);
12849 else if (specs->storage_class == csc_auto)
12850 /* auto may only be used with another storage class specifier,
12851 such as constexpr, if the type is inferred. */
12852 error ("%qE used with %<auto%>", scspec);
12853 else if (specs->thread_p)
12854 error ("%qE used with %qs", scspec,
12855 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12856 else
12857 specs->constexpr_p = true;
12858 break;
12859 default:
12860 gcc_unreachable ();
12861 }
12862 if (n != csc_none && n == specs->storage_class)
12863 dupe = true;
12864 if (dupe)
12865 {
12866 if (i == RID_THREAD)
12867 error ("duplicate %<_Thread_local%> or %<__thread%>");
12868 else
12869 error ("duplicate %qE", scspec);
12870 }
12871 if (n != csc_none)
12872 {
12873 if (specs->storage_class != csc_none && n != specs->storage_class)
12874 {
12875 error ("multiple storage classes in declaration specifiers");
12876 }
12877 else
12878 {
12879 specs->storage_class = n;
12880 specs->locations[cdw_storage_class] = loc;
12881 if (n != csc_extern && n != csc_static && specs->thread_p)
12882 {
12883 error ("%qs used with %qE",
12884 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12885 scspec);
12886 specs->thread_p = false;
12887 }
12888 if (n != csc_auto && n != csc_register && n != csc_static
12889 && specs->constexpr_p)
12890 {
12891 error ("%<constexpr%> used with %qE", scspec);
12892 specs->constexpr_p = false;
12893 }
12894 }
12895 }
12896 return specs;
12897 }
12898
12899 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12900 returning SPECS. */
12901
12902 struct c_declspecs *
12903 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12904 {
12905 specs->attrs = chainon (attrs, specs->attrs);
12906 specs->locations[cdw_attributes] = loc;
12907 specs->declspecs_seen_p = true;
12908 /* In the case of standard attributes at the start of the
12909 declaration, the caller will reset this. */
12910 specs->non_std_attrs_seen_p = true;
12911 return specs;
12912 }
12913
12914 /* Add an _Alignas specifier (expression ALIGN, or type whose
12915 alignment is ALIGN) to the declaration specifiers SPECS, returning
12916 SPECS. */
12917 struct c_declspecs *
12918 declspecs_add_alignas (location_t loc,
12919 struct c_declspecs *specs, tree align)
12920 {
12921 specs->alignas_p = true;
12922 specs->locations[cdw_alignas] = loc;
12923 if (align == error_mark_node)
12924 return specs;
12925
12926 /* Only accept the alignment if it's valid and greater than
12927 the current one. Zero is invalid but by C11 required to
12928 be silently ignored. */
12929 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12930 if (align_log > specs->align_log)
12931 specs->align_log = align_log;
12932 return specs;
12933 }
12934
12935 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12936 specifiers with any other type specifier to determine the resulting
12937 type. This is where ISO C checks on complex types are made, since
12938 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12939 double". Also apply postfix standard attributes to modify the type. */
12940
12941 struct c_declspecs *
12942 finish_declspecs (struct c_declspecs *specs)
12943 {
12944 /* If a type was specified as a whole, we have no modifiers and are
12945 done. */
12946 if (specs->type != NULL_TREE)
12947 {
12948 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12949 && !specs->signed_p && !specs->unsigned_p
12950 && !specs->complex_p && !specs->c23_auto_p);
12951
12952 /* Set a dummy type. */
12953 if (TREE_CODE (specs->type) == ERROR_MARK)
12954 specs->type = integer_type_node;
12955 goto handle_postfix_attrs;
12956 }
12957
12958 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12959 has been specified, treat it as "int" unless "_Complex" is
12960 present and there are no other specifiers. If we just have
12961 "_Complex", it is equivalent to "_Complex double", but e.g.
12962 "_Complex short" is equivalent to "_Complex short int". */
12963 if (specs->typespec_word == cts_none)
12964 {
12965 if (specs->saturating_p)
12966 {
12967 error_at (specs->locations[cdw_saturating],
12968 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12969 if (!targetm.fixed_point_supported_p ())
12970 error_at (specs->locations[cdw_saturating],
12971 "fixed-point types not supported for this target");
12972 specs->typespec_word = cts_fract;
12973 }
12974 else if (specs->long_p || specs->short_p
12975 || specs->signed_p || specs->unsigned_p)
12976 {
12977 specs->typespec_word = cts_int;
12978 }
12979 else if (specs->complex_p)
12980 {
12981 specs->typespec_word = cts_double;
12982 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12983 "ISO C does not support plain %<complex%> meaning "
12984 "%<double complex%>");
12985 }
12986 else if (specs->c23_auto_p)
12987 {
12988 /* Type to be filled in later, including applying postfix
12989 attributes. This warning only actually appears for
12990 -Wc11-c23-compat in C23 mode; in older modes, there may
12991 be a warning or pedwarn for implicit "int" instead, or
12992 other errors for use of auto at file scope. */
12993 pedwarn_c11 (input_location, OPT_Wpedantic,
12994 "ISO C does not support %<auto%> type deduction "
12995 "before C23");
12996 return specs;
12997 }
12998 else
12999 {
13000 specs->typespec_word = cts_int;
13001 specs->default_int_p = true;
13002 /* We don't diagnose this here because grokdeclarator will
13003 give more specific diagnostics according to whether it is
13004 a function definition. */
13005 }
13006 }
13007
13008 /* If "signed" was specified, record this to distinguish "int" and
13009 "signed int" in the case of a bit-field with
13010 -funsigned-bitfields. */
13011 specs->explicit_signed_p = specs->signed_p;
13012
13013 /* Now compute the actual type. */
13014 gcc_assert (!specs->c23_auto_p);
13015 switch (specs->typespec_word)
13016 {
13017 case cts_auto_type:
13018 gcc_assert (!specs->long_p && !specs->short_p
13019 && !specs->signed_p && !specs->unsigned_p
13020 && !specs->complex_p);
13021 /* Type to be filled in later. */
13022 if (specs->postfix_attrs)
13023 error ("%<__auto_type%> followed by %<[[]]%> attributes");
13024 break;
13025 case cts_void:
13026 gcc_assert (!specs->long_p && !specs->short_p
13027 && !specs->signed_p && !specs->unsigned_p
13028 && !specs->complex_p);
13029 specs->type = void_type_node;
13030 break;
13031 case cts_bool:
13032 gcc_assert (!specs->long_p && !specs->short_p
13033 && !specs->signed_p && !specs->unsigned_p
13034 && !specs->complex_p);
13035 specs->type = boolean_type_node;
13036 break;
13037 case cts_char:
13038 gcc_assert (!specs->long_p && !specs->short_p);
13039 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13040 if (specs->signed_p)
13041 specs->type = signed_char_type_node;
13042 else if (specs->unsigned_p)
13043 specs->type = unsigned_char_type_node;
13044 else
13045 specs->type = char_type_node;
13046 if (specs->complex_p)
13047 {
13048 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13049 "ISO C does not support complex integer types");
13050 specs->type = build_complex_type (specs->type);
13051 }
13052 break;
13053 case cts_int_n:
13054 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13055 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13056 if (! int_n_enabled_p[specs->u.int_n_idx])
13057 specs->type = integer_type_node;
13058 else
13059 specs->type = (specs->unsigned_p
13060 ? int_n_trees[specs->u.int_n_idx].unsigned_type
13061 : int_n_trees[specs->u.int_n_idx].signed_type);
13062 if (specs->complex_p)
13063 {
13064 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13065 "ISO C does not support complex integer types");
13066 specs->type = build_complex_type (specs->type);
13067 }
13068 break;
13069 case cts_int:
13070 gcc_assert (!(specs->long_p && specs->short_p));
13071 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13072 if (specs->long_long_p)
13073 specs->type = (specs->unsigned_p
13074 ? long_long_unsigned_type_node
13075 : long_long_integer_type_node);
13076 else if (specs->long_p)
13077 specs->type = (specs->unsigned_p
13078 ? long_unsigned_type_node
13079 : long_integer_type_node);
13080 else if (specs->short_p)
13081 specs->type = (specs->unsigned_p
13082 ? short_unsigned_type_node
13083 : short_integer_type_node);
13084 else
13085 specs->type = (specs->unsigned_p
13086 ? unsigned_type_node
13087 : integer_type_node);
13088 if (specs->complex_p)
13089 {
13090 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13091 "ISO C does not support complex integer types");
13092 specs->type = build_complex_type (specs->type);
13093 }
13094 break;
13095 case cts_float:
13096 gcc_assert (!specs->long_p && !specs->short_p
13097 && !specs->signed_p && !specs->unsigned_p);
13098 specs->type = (specs->complex_p
13099 ? complex_float_type_node
13100 : float_type_node);
13101 break;
13102 case cts_double:
13103 gcc_assert (!specs->long_long_p && !specs->short_p
13104 && !specs->signed_p && !specs->unsigned_p);
13105 if (specs->long_p)
13106 {
13107 specs->type = (specs->complex_p
13108 ? complex_long_double_type_node
13109 : long_double_type_node);
13110 }
13111 else
13112 {
13113 specs->type = (specs->complex_p
13114 ? complex_double_type_node
13115 : double_type_node);
13116 }
13117 break;
13118 case cts_floatn_nx:
13119 gcc_assert (!specs->long_p && !specs->short_p
13120 && !specs->signed_p && !specs->unsigned_p);
13121 if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13122 specs->type = integer_type_node;
13123 else if (specs->complex_p)
13124 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13125 else
13126 specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13127 break;
13128 case cts_dfloat32:
13129 case cts_dfloat64:
13130 case cts_dfloat128:
13131 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13132 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13133 if (!targetm.decimal_float_supported_p ())
13134 specs->type = integer_type_node;
13135 else if (specs->typespec_word == cts_dfloat32)
13136 specs->type = dfloat32_type_node;
13137 else if (specs->typespec_word == cts_dfloat64)
13138 specs->type = dfloat64_type_node;
13139 else
13140 specs->type = dfloat128_type_node;
13141 break;
13142 case cts_fract:
13143 gcc_assert (!specs->complex_p);
13144 if (!targetm.fixed_point_supported_p ())
13145 specs->type = integer_type_node;
13146 else if (specs->saturating_p)
13147 {
13148 if (specs->long_long_p)
13149 specs->type = specs->unsigned_p
13150 ? sat_unsigned_long_long_fract_type_node
13151 : sat_long_long_fract_type_node;
13152 else if (specs->long_p)
13153 specs->type = specs->unsigned_p
13154 ? sat_unsigned_long_fract_type_node
13155 : sat_long_fract_type_node;
13156 else if (specs->short_p)
13157 specs->type = specs->unsigned_p
13158 ? sat_unsigned_short_fract_type_node
13159 : sat_short_fract_type_node;
13160 else
13161 specs->type = specs->unsigned_p
13162 ? sat_unsigned_fract_type_node
13163 : sat_fract_type_node;
13164 }
13165 else
13166 {
13167 if (specs->long_long_p)
13168 specs->type = specs->unsigned_p
13169 ? unsigned_long_long_fract_type_node
13170 : long_long_fract_type_node;
13171 else if (specs->long_p)
13172 specs->type = specs->unsigned_p
13173 ? unsigned_long_fract_type_node
13174 : long_fract_type_node;
13175 else if (specs->short_p)
13176 specs->type = specs->unsigned_p
13177 ? unsigned_short_fract_type_node
13178 : short_fract_type_node;
13179 else
13180 specs->type = specs->unsigned_p
13181 ? unsigned_fract_type_node
13182 : fract_type_node;
13183 }
13184 break;
13185 case cts_accum:
13186 gcc_assert (!specs->complex_p);
13187 if (!targetm.fixed_point_supported_p ())
13188 specs->type = integer_type_node;
13189 else if (specs->saturating_p)
13190 {
13191 if (specs->long_long_p)
13192 specs->type = specs->unsigned_p
13193 ? sat_unsigned_long_long_accum_type_node
13194 : sat_long_long_accum_type_node;
13195 else if (specs->long_p)
13196 specs->type = specs->unsigned_p
13197 ? sat_unsigned_long_accum_type_node
13198 : sat_long_accum_type_node;
13199 else if (specs->short_p)
13200 specs->type = specs->unsigned_p
13201 ? sat_unsigned_short_accum_type_node
13202 : sat_short_accum_type_node;
13203 else
13204 specs->type = specs->unsigned_p
13205 ? sat_unsigned_accum_type_node
13206 : sat_accum_type_node;
13207 }
13208 else
13209 {
13210 if (specs->long_long_p)
13211 specs->type = specs->unsigned_p
13212 ? unsigned_long_long_accum_type_node
13213 : long_long_accum_type_node;
13214 else if (specs->long_p)
13215 specs->type = specs->unsigned_p
13216 ? unsigned_long_accum_type_node
13217 : long_accum_type_node;
13218 else if (specs->short_p)
13219 specs->type = specs->unsigned_p
13220 ? unsigned_short_accum_type_node
13221 : short_accum_type_node;
13222 else
13223 specs->type = specs->unsigned_p
13224 ? unsigned_accum_type_node
13225 : accum_type_node;
13226 }
13227 break;
13228 case cts_bitint:
13229 gcc_assert (!specs->long_p && !specs->short_p
13230 && !specs->complex_p);
13231 if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13232 {
13233 error_at (specs->locations[cdw_typespec],
13234 "%<signed _BitInt%> argument must be at least 2");
13235 specs->type = integer_type_node;
13236 break;
13237 }
13238 if (specs->u.bitint_prec == -1)
13239 specs->type = integer_type_node;
13240 else
13241 {
13242 pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13243 "ISO C does not support %<%s_BitInt(%d)%> before C23",
13244 specs->unsigned_p ? "unsigned "
13245 : specs->signed_p ? "signed " : "",
13246 specs->u.bitint_prec);
13247 specs->type = build_bitint_type (specs->u.bitint_prec,
13248 specs->unsigned_p);
13249 }
13250 break;
13251 default:
13252 gcc_unreachable ();
13253 }
13254 handle_postfix_attrs:
13255 if (specs->type != NULL)
13256 {
13257 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
13258 decl_attributes (&specs->type, specs->postfix_attrs, 0);
13259 specs->postfix_attrs = NULL_TREE;
13260 }
13261
13262 return specs;
13263 }
13264
13265 /* Perform final processing on one file scope's declarations (or the
13266 external scope's declarations), GLOBALS. */
13267
13268 static void
13269 c_write_global_declarations_1 (tree globals)
13270 {
13271 tree decl;
13272 bool reconsider;
13273
13274 /* Process the decls in the order they were written. */
13275 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13276 {
13277 /* Check for used but undefined static functions using the C
13278 standard's definition of "used", and set TREE_NO_WARNING so
13279 that check_global_declaration doesn't repeat the check. */
13280 if (TREE_CODE (decl) == FUNCTION_DECL
13281 && DECL_INITIAL (decl) == NULL_TREE
13282 && DECL_EXTERNAL (decl)
13283 && !TREE_PUBLIC (decl))
13284 {
13285 if (C_DECL_USED (decl))
13286 {
13287 /* TODO: Add OPT_Wundefined-inline. */
13288 if (pedwarn (input_location, 0, "%q+F used but never defined",
13289 decl))
13290 suppress_warning (decl /* OPT_Wundefined-inline. */);
13291 }
13292 /* For -Wunused-function warn about unused static prototypes. */
13293 else if (warn_unused_function
13294 && ! DECL_ARTIFICIAL (decl)
13295 && ! warning_suppressed_p (decl, OPT_Wunused_function))
13296 {
13297 if (warning (OPT_Wunused_function,
13298 "%q+F declared %<static%> but never defined",
13299 decl))
13300 suppress_warning (decl, OPT_Wunused_function);
13301 }
13302 }
13303
13304 wrapup_global_declaration_1 (decl);
13305 }
13306
13307 do
13308 {
13309 reconsider = false;
13310 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13311 reconsider |= wrapup_global_declaration_2 (decl);
13312 }
13313 while (reconsider);
13314 }
13315
13316 /* Preserve the external declarations scope across a garbage collect. */
13317 static GTY(()) tree ext_block;
13318
13319 /* Collect all references relevant to SOURCE_FILE. */
13320
13321 static void
13322 collect_all_refs (const char *source_file)
13323 {
13324 tree t;
13325 unsigned i;
13326
13327 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13328 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13329
13330 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13331 }
13332
13333 /* Collect source file references at global level. */
13334
13335 static void
13336 collect_source_refs (void)
13337 {
13338 tree t;
13339 tree decls;
13340 tree decl;
13341 unsigned i;
13342
13343 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13344 {
13345 decls = DECL_INITIAL (t);
13346 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13347 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13348 collect_source_ref (DECL_SOURCE_FILE (decl));
13349 }
13350
13351 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13352 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13353 collect_source_ref (DECL_SOURCE_FILE (decl));
13354 }
13355
13356 /* Free attribute access data that are not needed by the middle end. */
13357
13358 static void
13359 free_attr_access_data ()
13360 {
13361 struct cgraph_node *n;
13362
13363 /* Iterate over all functions declared in the translation unit. */
13364 FOR_EACH_FUNCTION (n)
13365 {
13366 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13367 if (tree attrs = DECL_ATTRIBUTES (parm))
13368 attr_access::free_lang_data (attrs);
13369
13370 tree fntype = TREE_TYPE (n->decl);
13371 if (!fntype || fntype == error_mark_node)
13372 continue;
13373 tree attrs = TYPE_ATTRIBUTES (fntype);
13374 if (!attrs)
13375 continue;
13376
13377 attr_access::free_lang_data (attrs);
13378 }
13379 }
13380
13381 /* Perform any final parser cleanups and generate initial debugging
13382 information. */
13383
13384 void
13385 c_parse_final_cleanups (void)
13386 {
13387 tree t;
13388 unsigned i;
13389
13390 /* We don't want to do this if generating a PCH. */
13391 if (pch_file)
13392 return;
13393
13394 timevar_stop (TV_PHASE_PARSING);
13395 timevar_start (TV_PHASE_DEFERRED);
13396
13397 /* Do the Objective-C stuff. This is where all the Objective-C
13398 module stuff gets generated (symtab, class/protocol/selector
13399 lists etc). */
13400 if (c_dialect_objc ())
13401 objc_write_global_declarations ();
13402
13403 /* Close the external scope. */
13404 ext_block = pop_scope ();
13405 external_scope = 0;
13406 gcc_assert (!current_scope);
13407
13408 /* Handle -fdump-ada-spec[-slim]. */
13409 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13410 {
13411 /* Build a table of files to generate specs for */
13412 collect_source_ref (main_input_filename);
13413 if (!flag_dump_ada_spec_slim)
13414 collect_source_refs ();
13415
13416 dump_ada_specs (collect_all_refs, NULL);
13417 }
13418
13419 /* Process all file scopes in this compilation, and the external_scope,
13420 through wrapup_global_declarations. */
13421 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13422 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13423 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13424
13425 if (!in_lto_p)
13426 free_attr_access_data ();
13427
13428 timevar_stop (TV_PHASE_DEFERRED);
13429 timevar_start (TV_PHASE_PARSING);
13430
13431 ext_block = NULL;
13432 }
13433
13434 /* Register reserved keyword WORD as qualifier for address space AS. */
13435
13436 void
13437 c_register_addr_space (const char *word, addr_space_t as)
13438 {
13439 int rid = RID_FIRST_ADDR_SPACE + as;
13440 tree id;
13441
13442 /* Address space qualifiers are only supported
13443 in C with GNU extensions enabled. */
13444 if (c_dialect_objc () || flag_no_asm)
13445 return;
13446
13447 id = get_identifier (word);
13448 C_SET_RID_CODE (id, rid);
13449 C_IS_RESERVED_WORD (id) = 1;
13450 ridpointers [rid] = id;
13451 }
13452
13453 /* Return identifier to look up for omp declare reduction. */
13454
13455 tree
13456 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13457 {
13458 const char *p = NULL;
13459 switch (reduction_code)
13460 {
13461 case PLUS_EXPR: p = "+"; break;
13462 case MULT_EXPR: p = "*"; break;
13463 case MINUS_EXPR: p = "-"; break;
13464 case BIT_AND_EXPR: p = "&"; break;
13465 case BIT_XOR_EXPR: p = "^"; break;
13466 case BIT_IOR_EXPR: p = "|"; break;
13467 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13468 case TRUTH_ORIF_EXPR: p = "||"; break;
13469 case MIN_EXPR: p = "min"; break;
13470 case MAX_EXPR: p = "max"; break;
13471 default:
13472 break;
13473 }
13474
13475 if (p == NULL)
13476 {
13477 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13478 return error_mark_node;
13479 p = IDENTIFIER_POINTER (reduction_id);
13480 }
13481
13482 const char prefix[] = "omp declare reduction ";
13483 size_t lenp = sizeof (prefix);
13484 size_t len = strlen (p);
13485 char *name = XALLOCAVEC (char, lenp + len);
13486 memcpy (name, prefix, lenp - 1);
13487 memcpy (name + lenp - 1, p, len + 1);
13488 return get_identifier (name);
13489 }
13490
13491 /* Lookup REDUCTION_ID in the current scope, or create an artificial
13492 VAR_DECL, bind it into the current scope and return it. */
13493
13494 tree
13495 c_omp_reduction_decl (tree reduction_id)
13496 {
13497 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13498 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13499 return b->decl;
13500
13501 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13502 reduction_id, integer_type_node);
13503 DECL_ARTIFICIAL (decl) = 1;
13504 DECL_EXTERNAL (decl) = 1;
13505 TREE_STATIC (decl) = 1;
13506 TREE_PUBLIC (decl) = 0;
13507 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13508 return decl;
13509 }
13510
13511 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13512
13513 tree
13514 c_omp_reduction_lookup (tree reduction_id, tree type)
13515 {
13516 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13517 while (b)
13518 {
13519 tree t;
13520 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13521 if (comptypes (TREE_PURPOSE (t), type))
13522 return TREE_VALUE (t);
13523 b = b->shadowed;
13524 }
13525 return error_mark_node;
13526 }
13527
13528 /* Helper function called via walk_tree, to diagnose invalid
13529 #pragma omp declare reduction combiners or initializers. */
13530
13531 tree
13532 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13533 {
13534 tree *vars = (tree *) data;
13535 if (SSA_VAR_P (*tp)
13536 && !DECL_ARTIFICIAL (*tp)
13537 && *tp != vars[0]
13538 && *tp != vars[1])
13539 {
13540 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13541 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13542 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13543 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13544 *tp);
13545 else
13546 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13547 "to variable %qD which is not %<omp_priv%> nor "
13548 "%<omp_orig%>",
13549 *tp);
13550 return *tp;
13551 }
13552 return NULL_TREE;
13553 }
13554
13555
13556 bool
13557 c_check_in_current_scope (tree decl)
13558 {
13559 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13560 return b != NULL && B_IN_CURRENT_SCOPE (b);
13561 }
13562
13563 #include "gt-c-c-decl.h"