1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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. */
69enum 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
83enum deprecated_states {
84 DEPRECATED_NORMAL,
85 DEPRECATED_SUPPRESS,
86 UNAVAILABLE_DEPRECATED_SUPPRESS
87};
88
89
90/* Nonzero if we have seen an invalid cross reference
91 to a struct, union, or enum, but not yet printed the message. */
92tree pending_invalid_xref;
93
94/* File and line to appear in the eventual error message. */
95location_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
101static location_t current_function_prototype_locus;
102
103/* Whether this prototype was built-in. */
104
105static bool current_function_prototype_built_in;
106
107/* The argument type information of this prototype. */
108
109static tree current_function_prototype_arg_types;
110
111/* The argument information structure for the function currently being
112 defined. */
113
114static 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. */
119struct obstack parser_obstack;
120
121/* The current statement tree. */
122
123static 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. */
127unsigned char in_statement;
128
129/* A list of decls to be made automatically visible in each file scope. */
130static 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
135int 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
140int 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
145int 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
150static 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
155static bool undef_nested_function;
156
157/* Vector of implicit "omp declare target" attributes to be added into
158 the attribute lists. */
159vec<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. */
164vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
165
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
202struct 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
227struct 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. */
235extern 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. */
239void (*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
246static inline struct c_binding* *
247i_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
275static inline struct c_binding **
276i_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
304static inline struct c_binding **
305i_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. */
329struct GTY (()) c_tree_token_vec {
330 struct tree_base base;
331 vec<c_token, va_gc> *tokens;
332};
333
334STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
335STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
336 == offsetof (c_tree_token_vec_struct, tokens));
337
338/* The resulting tree type. */
339
340union 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. */
351size_t
352c_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
374struct 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
398struct 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
405typedef 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
410struct 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
472struct 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
531static 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
537static GTY(()) struct c_scope *current_function_scope;
538
539/* The C file scope. This is reset for each input translation unit. */
540
541static 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
546static GTY(()) struct c_scope *external_scope;
547
548/* A chain of c_scope structures awaiting reuse. */
549
550static GTY((deletable)) struct c_scope *scope_freelist;
551
552/* A chain of c_binding structures awaiting reuse. */
553
554static 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
583struct 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. */
602static GTY(()) struct c_inline_static *c_inline_statics;
603
604/* True means unconditionally make a BLOCK for the next scope pushed. */
605
606static 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
612static bool next_is_function_body;
613
614/* A vector of pointers to c_binding structures. */
615
616typedef struct c_binding *c_binding_ptr;
617
618/* Information that we keep for a struct or union while it is being
619 parsed. */
620
621class c_struct_parse_info
622{
623public:
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. */
639struct 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. */
646hashval_t
647c_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. */
658bool
659c_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. */
665static 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. */
669static class c_struct_parse_info *struct_parse_info;
670
671/* Forward declarations. */
672static tree lookup_name_in_scope (tree, struct c_scope *);
673static tree c_make_fname_decl (location_t, tree, int);
674static tree grokdeclarator (const struct c_declarator *,
675 struct c_declspecs *,
676 enum decl_context, bool, tree *, tree *, tree *,
677 bool *, enum deprecated_states);
678static tree grokparms (struct c_arg_info *, bool);
679static void layout_array_type (tree);
680static const char *header_for_builtin_fn (tree);
681
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
686tree
687add_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
709/* Build a pointer type using the default pointer mode. */
710
711static tree
712c_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
726/* Return true if we will want to say something if a goto statement
727 crosses DECL. */
728
729static bool
730decl_jump_unsafe (tree decl)
731{
732 if (error_operand_p (t: 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 (attr_name: "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
764
765void
766c_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 (stream: file, format: "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. */
794void
795c_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. */
803static void
804bind (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. */
869static struct c_binding *
870free_binding_and_advance (struct c_binding *b)
871{
872 struct c_binding *prev = b->prev;
873
874 memset (s: b, c: 0, n: 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. */
883static void
884bind_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, decl: 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
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
903void
904c_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
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
928void
929record_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
945static void
946check_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
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
974static void
975set_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
994static bool
995update_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
1013/* The Objective-C front-end often needs to determine the current scope. */
1014
1015void *
1016objc_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
1024void
1025objc_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
1045bool
1046global_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
1054bool
1055old_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
1062void
1063keep_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
1070void
1071set_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
1078void
1079clear_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
1086bool
1087float_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
1094void
1095declare_parm_level (void)
1096{
1097 current_scope->parm_flag = true;
1098}
1099
1100void
1101push_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
1170static void
1171update_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, p: &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 (decl: b1->decl))
1211 vec_safe_push(v&: label_vars->decls_in_scope, obj: 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, p: &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
1236tree
1237pop_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 (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 (attr_name: "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 (s: scope, c: 0, n: sizeof (struct c_scope));
1479 scope->outer = scope_freelist;
1480 scope_freelist = scope;
1481
1482 return block;
1483}
1484
1485void
1486push_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, scope: file_scope,
1500 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1501}
1502
1503void
1504pop_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
1535/* Whether we are curently inside the initializer for an
1536 underspecified object definition (C23 auto or constexpr). */
1537static 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. */
1547unsigned int
1548start_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, invisible: false, nested: false, locus: 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. */
1581void
1582finish_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 (b: *p);
1601 }
1602 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1603}
1604
1605/* Adjust the bindings for the start of a statement expression. */
1606
1607void
1608c_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
1640void
1641c_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
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
1694static void
1695pushtag (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, decl: type, scope: current_scope, /*invisible=*/false, /*nested=*/false, locus: 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
1741void
1742c_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
1754void
1755c_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, scope: external_scope, invisible: true, nested: false, locus: 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, invisible: false, nested, locus: loc);
1784}
1785
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
1794static const unsigned builtin_structptr_type_count
1795 = ARRAY_SIZE (builtin_structptr_types);
1796
1797static 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
1803static bool
1804types_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
1817static tree
1818match_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 (t1: oldrettype, t2: 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 (t1: oldtype, t2: 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 (attr_name: "transaction_safe", list: oldattrs);
1920 tree newattrs = TYPE_ATTRIBUTES (newtype);
1921 tree newtsafe = lookup_attribute (attr_name: "transaction_safe", list: 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. */
1934static void
1935diagnose_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 (t: 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. */
1976static bool
1977validate_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. */
2049static void
2050locate_old_decl (tree decl)
2051{
2052 if (TREE_CODE (decl) == FUNCTION_DECL
2053 && fndecl_built_in_p (node: 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 same scope if such a
2073 declaration exists. */
2074static tree
2075previous_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
2100static bool
2101diagnose_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 (node: 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 (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 (decl: olddecl);
2155 }
2156 }
2157 else
2158 {
2159 auto_diagnostic_group d;
2160 error ("redeclaration of enumerator %q+D", newdecl);
2161 locate_old_decl (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 (node: olddecl, klass: 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 strict: &mismatch_expect,
2188 argno: &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 (as: 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 (as: new_addr), newdecl);
2296 else
2297 error ("conflicting named address spaces (%s vs %s) "
2298 "for %q+D",
2299 c_addr_space_name (as: new_addr),
2300 c_addr_space_name (as: 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 (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 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 (node: olddecl, name1: 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 (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 (t: newtype))
2356 {
2357 error ("redefinition of typedef %q+D with variably modified type",
2358 newdecl);
2359 locate_old_decl (decl: olddecl);
2360 }
2361 else if (pedwarn_c99 (input_location, opt: OPT_Wpedantic,
2362 "redefinition of typedef %q+D", newdecl))
2363 locate_old_decl (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 (node: 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 (attr_name: "gnu_inline",
2424 DECL_ATTRIBUTES (olddecl)))
2425 && (!DECL_DECLARED_INLINE_P (newdecl)
2426 || !lookup_attribute (attr_name: "gnu_inline",
2427 DECL_ATTRIBUTES (newdecl)))))
2428 {
2429 auto_diagnostic_group d;
2430 error ("redefinition of %q+D", newdecl);
2431 locate_old_decl (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 (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 (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 (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 (attr_name: "gnu_inline",
2499 DECL_ATTRIBUTES (newdecl)) != NULL;
2500 bool olda = lookup_attribute (attr_name: "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 (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 (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 (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 (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 (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 (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 (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 (node: newdecl)
2682 && fndecl_built_in_p (node: 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 (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
2711static void
2712merge_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 (attr_name: "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 (node: 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 (decl: 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, implicit_p: true);
3019 break;
3020 default:
3021 if (builtin_decl_explicit_p (fncode))
3022 set_builtin_decl_declared_p (fncode, declared_p: 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 (attr_name: "common", DECL_ATTRIBUTES (newdecl))
3079 && !lookup_attribute (attr_name: "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 (dest: (char *) olddecl + sizeof (struct tree_common),
3093 src: (char *) newdecl + sizeof (struct tree_common),
3094 n: 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 (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3104 src: (char *) newdecl + sizeof (struct tree_decl_common),
3105 n: 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 (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3130 src: (char *) newdecl + sizeof (struct tree_decl_common),
3131 n: tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3132 break;
3133
3134 default:
3135
3136 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3137 src: (char *) newdecl + sizeof (struct tree_decl_common),
3138 n: 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
3162static bool
3163duplicate_decls (tree newdecl, tree olddecl)
3164{
3165 tree newtype = NULL, oldtype = NULL;
3166
3167 if (!diagnose_mismatched_decls (newdecl, olddecl, newtypep: &newtype, oldtypep: &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 (decl: newdecl);
3198 if (snode)
3199 snode->remove ();
3200 }
3201 ggc_free (newdecl);
3202 return true;
3203}
3204
3205
3206/* Check whether decl-node NEW_DECL shadows an existing declaration. */
3207static void
3208warn_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 (node: 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
3312tree
3313pushdecl (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, decl: 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 (newdecl: x, olddecl: 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 (node: 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 (newdecl: x, olddecl: 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, decl: 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 (newdecl: x, olddecl: 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, decl: x, scope: 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 (new_decl: 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, decl: 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
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
3568static void
3569implicit_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 (new_content: 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 (new_content: 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 (node: 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 (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
3641static const char *
3642header_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 (decl: 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
3839tree
3840implicitly_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 (node: decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3866 {
3867 bind (name: functionid, decl, scope: 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, id: functionid, olddecl: decl);
3884 C_DECL_IMPLICIT (decl) = 1;
3885 }
3886 if (fndecl_built_in_p (node: 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 (fndecl: 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 (name: functionid, decl, scope: 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, id: functionid, olddecl: 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 (x: 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. */
3963void
3964undeclared_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 (new_content: 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 (new_content: 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 (name: id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4015 UNKNOWN_LOCATION);
4016}
4017
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
4022static tree
4023make_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 (p: &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
4045tree
4046lookup_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 (location: input_location, name, defining: false, p_label_vars: &label_vars);
4074
4075 /* Ordinary labels go in the current function scope. */
4076 bind_label (name, label, scope: 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
4084static void
4085warn_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 (attr_name: "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
4105tree
4106lookup_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 (p: &g->goto_bindings, defining: true);
4135 vec_safe_push (v&: label_vars->gotos, obj: 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 (goto_loc: 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
4164tree
4165declare_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 (decl: b->decl);
4178
4179 /* Just use the previous declaration. */
4180 return b->decl;
4181 }
4182
4183 label = make_label (location: input_location, name, defining: false, p_label_vars: &label_vars);
4184 C_DECLARED_LABEL_FLAG (label) = 1;
4185
4186 /* Declared labels go in the current scope. */
4187 bind_label (name, label, scope: 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
4195static void
4196check_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 (decl: b->decl))
4216 warn_about_goto (goto_loc: g->loc, label, decl: 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 (decl: b->decl))
4236 warn_about_goto (goto_loc: g->loc, label, decl: 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 (v: label_vars->gotos, size: 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
4260tree
4261define_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 (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 (p: &label_vars->label_bindings, defining: 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, defining: true, p_label_vars: &label_vars);
4300
4301 /* Ordinary labels go in the current function scope. */
4302 bind_label (name, label, scope: current_function_scope, label_vars);
4303 }
4304
4305 if (!in_system_header_at (loc: 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
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
4319struct c_spot_bindings *
4320c_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 (p: switch_bindings, defining: true);
4326 return switch_bindings;
4327}
4328
4329void
4330c_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
4340bool
4341c_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 (decl: 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 (attr_name: "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
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
4416static tree
4417lookup_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
4464bool
4465tag_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
4479void
4480pending_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
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
4494tree
4495lookup_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
4508static tree
4509lookup_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
4537name_hint
4538lookup_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 (str: 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 (candidate: 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 (best_candidate: macro_as_identifier,
4630 best_distance: bmm.get_best_distance (),
4631 best_candidate_len: 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 (keyword: 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 (candidate: 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
4659/* Handle the standard [[nodiscard]] attribute. */
4660
4661static tree
4662handle_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
4687static tree
4688handle_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. */
4706static 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 { .name: "_Noreturn", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4711 .handler: handle_std_noreturn_attribute, NULL },
4712 { .name: "deprecated", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4713 .handler: handle_deprecated_attribute, NULL },
4714 { .name: "fallthrough", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4715 .handler: handle_fallthrough_attribute, NULL },
4716 { .name: "maybe_unused", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4717 .handler: handle_unused_attribute, NULL },
4718 { .name: "nodiscard", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4719 .handler: handle_nodiscard_attribute, NULL },
4720 { .name: "noreturn", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4721 .handler: handle_std_noreturn_attribute, NULL }
4722};
4723
4724const scoped_attribute_specs std_attribute_table =
4725{
4726 .ns: nullptr, .attributes: { 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
4734void
4735c_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 (x: 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
4786static tree
4787c_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 (s: 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 (name: id, decl, scope: 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
4831tree
4832c_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 (name: id, decl, scope: 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
4857tree
4858c_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 (name: id, decl, scope: 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
4883tree
4884c_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 (x: 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
4895void
4896c_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
4913tree
4914c_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
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
4937void
4938shadow_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. */
4945void
4946shadow_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, thislevel_only: true, NULL);
5050
5051 if (t == NULL_TREE)
5052 {
5053 t = make_node (code);
5054 if (flag_isoc23 && code != ENUMERAL_TYPE)
5055 SET_TYPE_STRUCTURAL_EQUALITY (t);
5056 pushtag (loc: input_location, name, type: t);
5057 }
5058 }
5059 }
5060 else
5061 {
5062 if (warned != 1 && !in_system_header_at (loc: input_location))
5063 {
5064 pedwarn (input_location, 0,
5065 "useless type name in empty declaration");
5066 warned = 1;
5067 }
5068 }
5069 }
5070 else if (warned != 1 && !in_system_header_at (loc: input_location)
5071 && declspecs->typedef_p)
5072 {
5073 pedwarn (input_location, 0, "useless type name in empty declaration");
5074 warned = 1;
5075 }
5076
5077 pending_invalid_xref = NULL_TREE;
5078
5079 if (declspecs->inline_p)
5080 {
5081 error ("%<inline%> in empty declaration");
5082 warned = 1;
5083 }
5084
5085 if (declspecs->noreturn_p)
5086 {
5087 error ("%<_Noreturn%> in empty declaration");
5088 warned = 1;
5089 }
5090
5091 if (declspecs->constexpr_p)
5092 {
5093 error ("%<constexpr%> in empty declaration");
5094 warned = 1;
5095 }
5096
5097 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5098 {
5099 error ("%<auto%> in file-scope empty declaration");
5100 warned = 1;
5101 }
5102
5103 if (current_scope == file_scope && declspecs->storage_class == csc_register)
5104 {
5105 error ("%<register%> in file-scope empty declaration");
5106 warned = 1;
5107 }
5108
5109 if (declspecs->enum_type_specifier_ref_p && !warned)
5110 {
5111 if (declspecs->storage_class != csc_none)
5112 {
5113 error ("storage class specifier in empty declaration with %<enum%> "
5114 "underlying type");
5115 warned = 1;
5116 }
5117 else if (declspecs->thread_p)
5118 {
5119 error ("%qs in empty declaration with %<enum%> underlying type",
5120 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5121 warned = 1;
5122 }
5123 else if (declspecs->const_p
5124 || declspecs->volatile_p
5125 || declspecs->atomic_p
5126 || declspecs->restrict_p
5127 || declspecs->address_space)
5128 {
5129 error ("type qualifier in empty declaration with %<enum%> "
5130 "underlying type");
5131 warned = 1;
5132 }
5133 else if (declspecs->alignas_p)
5134 {
5135 error ("%<alignas%> in empty declaration with %<enum%> "
5136 "underlying type");
5137 warned = 1;
5138 }
5139 }
5140
5141 if (!warned && !in_system_header_at (loc: input_location)
5142 && declspecs->storage_class != csc_none)
5143 {
5144 warning (0, "useless storage class specifier in empty declaration");
5145 warned = 2;
5146 }
5147
5148 if (!warned && !in_system_header_at (loc: input_location) && declspecs->thread_p)
5149 {
5150 warning (0, "useless %qs in empty declaration",
5151 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5152 warned = 2;
5153 }
5154
5155 if (!warned
5156 && !in_system_header_at (loc: input_location)
5157 && (declspecs->const_p
5158 || declspecs->volatile_p
5159 || declspecs->atomic_p
5160 || declspecs->restrict_p
5161 || declspecs->address_space))
5162 {
5163 warning (0, "useless type qualifier in empty declaration");
5164 warned = 2;
5165 }
5166
5167 if (!warned && !in_system_header_at (loc: input_location)
5168 && declspecs->alignas_p)
5169 {
5170 warning (0, "useless %<_Alignas%> in empty declaration");
5171 warned = 2;
5172 }
5173
5174 if (found_tag
5175 && warned == 2
5176 && (declspecs->typespec_kind == ctsk_tagref_attrs
5177 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5178 {
5179 /* Standard attributes after the "struct" or "union" keyword are
5180 only permitted when the contents of the type are defined, or
5181 in the form "struct-or-union attribute-specifier-sequence
5182 identifier;". If the ';' was not present, attributes were
5183 diagnosed in the parser. Here, ensure that any other useless
5184 elements of the declaration result in a pedwarn, not just a
5185 warning. Forward declarations of enum types are not part of
5186 standard C, but handle them the same. */
5187 pedwarn (input_location, 0,
5188 "invalid use of attributes in empty declaration");
5189 warned = 1;
5190 }
5191
5192 if (warned != 1)
5193 {
5194 if (declspecs->declspecs_seen_p
5195 && !declspecs->non_std_attrs_seen_p)
5196 /* An attribute declaration (but not a fallthrough attribute
5197 declaration, which was handled separately); warn if there
5198 are any attributes being ignored (but not if the attributes
5199 were empty). */
5200 c_warn_unused_attributes (attrs: declspecs->attrs);
5201 else if (!found_tag)
5202 pedwarn (input_location, 0, "empty declaration");
5203 }
5204}
5205
5206
5207/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5208 bits. SPECS represents declaration specifiers that the grammar
5209 only permits to contain type qualifiers and attributes. */
5210
5211int
5212quals_from_declspecs (const struct c_declspecs *specs)
5213{
5214 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5215 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5216 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5217 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5218 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5219 gcc_assert (!specs->type
5220 && !specs->decl_attr
5221 && specs->typespec_word == cts_none
5222 && specs->storage_class == csc_none
5223 && !specs->typedef_p
5224 && !specs->explicit_signed_p
5225 && !specs->deprecated_p
5226 && !specs->unavailable_p
5227 && !specs->long_p
5228 && !specs->long_long_p
5229 && !specs->short_p
5230 && !specs->signed_p
5231 && !specs->unsigned_p
5232 && !specs->complex_p
5233 && !specs->inline_p
5234 && !specs->noreturn_p
5235 && !specs->thread_p);
5236 return quals;
5237}
5238
5239/* Construct an array declarator. LOC is the location of the
5240 beginning of the array (usually the opening brace). EXPR is the
5241 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5242 inside the [] (to be applied to the pointer to which a parameter
5243 array is converted). STATIC_P is true if "static" is inside the
5244 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5245 VLA of unspecified length which is nevertheless a complete type,
5246 false otherwise. The field for the contained declarator is left to
5247 be filled in by set_array_declarator_inner. */
5248
5249struct c_declarator *
5250build_array_declarator (location_t loc,
5251 tree expr, struct c_declspecs *quals, bool static_p,
5252 bool vla_unspec_p)
5253{
5254 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5255 struct c_declarator);
5256 declarator->id_loc = loc;
5257 declarator->kind = cdk_array;
5258 declarator->declarator = 0;
5259 declarator->u.array.dimen = expr;
5260 if (quals)
5261 {
5262 declarator->u.array.attrs = quals->attrs;
5263 declarator->u.array.quals = quals_from_declspecs (specs: quals);
5264 }
5265 else
5266 {
5267 declarator->u.array.attrs = NULL_TREE;
5268 declarator->u.array.quals = 0;
5269 }
5270 declarator->u.array.static_p = static_p;
5271 declarator->u.array.vla_unspec_p = vla_unspec_p;
5272 if (static_p || quals != NULL)
5273 pedwarn_c90 (loc, opt: OPT_Wpedantic,
5274 "ISO C90 does not support %<static%> or type "
5275 "qualifiers in parameter array declarators");
5276 if (vla_unspec_p)
5277 pedwarn_c90 (loc, opt: OPT_Wpedantic,
5278 "ISO C90 does not support %<[*]%> array declarators");
5279 if (vla_unspec_p)
5280 {
5281 if (!current_scope->parm_flag)
5282 {
5283 /* C99 6.7.5.2p4 */
5284 error_at (loc, "%<[*]%> not allowed in other than "
5285 "function prototype scope");
5286 declarator->u.array.vla_unspec_p = false;
5287 return NULL;
5288 }
5289 current_scope->had_vla_unspec = true;
5290 }
5291 return declarator;
5292}
5293
5294/* Set the contained declarator of an array declarator. DECL is the
5295 declarator, as constructed by build_array_declarator; INNER is what
5296 appears on the left of the []. */
5297
5298struct c_declarator *
5299set_array_declarator_inner (struct c_declarator *decl,
5300 struct c_declarator *inner)
5301{
5302 decl->declarator = inner;
5303 return decl;
5304}
5305
5306/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5307static bool
5308flexible_array_member_type_p (const_tree type)
5309{
5310 if (TREE_CODE (type) == ARRAY_TYPE
5311 && TYPE_SIZE (type) == NULL_TREE
5312 && TYPE_DOMAIN (type) != NULL_TREE
5313 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5314 return true;
5315
5316 return false;
5317}
5318
5319/* Determine whether TYPE is a one-element array type "[1]". */
5320static bool
5321one_element_array_type_p (const_tree type)
5322{
5323 if (TREE_CODE (type) != ARRAY_TYPE)
5324 return false;
5325 return integer_zerop (array_type_nelts (type));
5326}
5327
5328/* Determine whether TYPE is a zero-length array type "[0]". */
5329static bool
5330zero_length_array_type_p (const_tree type)
5331{
5332 if (TREE_CODE (type) == ARRAY_TYPE)
5333 if (tree type_size = TYPE_SIZE_UNIT (type))
5334 if ((integer_zerop (type_size))
5335 && TYPE_DOMAIN (type) != NULL_TREE
5336 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5337 return true;
5338 return false;
5339}
5340
5341/* INIT is a constructor that forms DECL's initializer. If the final
5342 element initializes a flexible array field, add the size of that
5343 initializer to DECL's size. */
5344
5345static void
5346add_flexible_array_elts_to_size (tree decl, tree init)
5347{
5348 tree elt, type;
5349
5350 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5351 return;
5352
5353 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5354 type = TREE_TYPE (elt);
5355 if (flexible_array_member_type_p (type))
5356 {
5357 complete_array_type (&type, elt, false);
5358 DECL_SIZE (decl)
5359 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5360 DECL_SIZE_UNIT (decl)
5361 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5362 }
5363}
5364
5365/* Decode a "typename", such as "int **", returning a ..._TYPE node.
5366 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5367 before the type name, and set *EXPR_CONST_OPERANDS, if
5368 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5369 appear in a constant expression. */
5370
5371tree
5372groktypename (struct c_type_name *type_name, tree *expr,
5373 bool *expr_const_operands)
5374{
5375 tree type;
5376 tree attrs = type_name->specs->attrs;
5377
5378 type_name->specs->attrs = NULL_TREE;
5379
5380 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5381 false, NULL, &attrs, expr, expr_const_operands,
5382 DEPRECATED_NORMAL);
5383
5384 /* Apply attributes. */
5385 attrs = c_warn_type_attributes (attrs);
5386 decl_attributes (&type, attrs, 0);
5387
5388 return type;
5389}
5390
5391/* Looks up the most recent pushed declaration corresponding to DECL. */
5392
5393static tree
5394lookup_last_decl (tree decl)
5395{
5396 tree last_decl = lookup_name (DECL_NAME (decl));
5397 if (!last_decl)
5398 last_decl = lookup_name_in_scope (DECL_NAME (decl), scope: external_scope);
5399 return last_decl;
5400}
5401
5402/* Wrapper for decl_attributes that adds some implicit attributes
5403 to VAR_DECLs or FUNCTION_DECLs. */
5404
5405static tree
5406c_decl_attributes (tree *node, tree attributes, int flags)
5407{
5408 /* Add implicit "omp declare target" attribute if requested. */
5409 if (vec_safe_length (v: current_omp_declare_target_attribute)
5410 && ((VAR_P (*node) && is_global_var (t: *node))
5411 || TREE_CODE (*node) == FUNCTION_DECL))
5412 {
5413 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5414 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5415 NULL_TREE, attributes);
5416 else
5417 {
5418 attributes = tree_cons (get_identifier ("omp declare target"),
5419 NULL_TREE, attributes);
5420 attributes = tree_cons (get_identifier ("omp declare target block"),
5421 NULL_TREE, attributes);
5422 }
5423 if (TREE_CODE (*node) == FUNCTION_DECL)
5424 {
5425 int device_type
5426 = current_omp_declare_target_attribute->last ().device_type;
5427 device_type = MAX (device_type, 0);
5428 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5429 && !lookup_attribute (attr_name: "omp declare target host", list: attributes))
5430 attributes
5431 = tree_cons (get_identifier ("omp declare target host"),
5432 NULL_TREE, attributes);
5433 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5434 && !lookup_attribute (attr_name: "omp declare target nohost", list: attributes))
5435 attributes
5436 = tree_cons (get_identifier ("omp declare target nohost"),
5437 NULL_TREE, attributes);
5438
5439 int indirect
5440 = current_omp_declare_target_attribute->last ().indirect;
5441 if (indirect && !lookup_attribute (attr_name: "omp declare target indirect",
5442 list: attributes))
5443 attributes
5444 = tree_cons (get_identifier ("omp declare target indirect"),
5445 NULL_TREE, attributes);
5446 }
5447 }
5448
5449 if (flag_openmp || flag_openmp_simd)
5450 {
5451 bool diagnosed = false;
5452 for (tree *pa = &attributes; *pa; )
5453 {
5454 if (is_attribute_namespace_p (attr_ns: "omp", attr: *pa))
5455 {
5456 tree name = get_attribute_name (*pa);
5457 if (is_attribute_p (attr_name: "directive", ident: name)
5458 || is_attribute_p (attr_name: "sequence", ident: name)
5459 || is_attribute_p (attr_name: "decl", ident: name))
5460 {
5461 const char *p = NULL;
5462 if (TREE_VALUE (*pa) == NULL_TREE)
5463 p = IDENTIFIER_POINTER (name);
5464 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5465 {
5466 tree d = TREE_VALUE (a);
5467 gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5468 if (TREE_PUBLIC (d)
5469 && (VAR_P (*node)
5470 || TREE_CODE (*node) == FUNCTION_DECL)
5471 && c_maybe_parse_omp_decl (*node, d))
5472 continue;
5473 p = TREE_PUBLIC (d) ? "decl" : "directive";
5474 }
5475 if (p && !diagnosed)
5476 {
5477 error ("%<omp::%s%> not allowed to be specified in "
5478 "this context", p);
5479 diagnosed = true;
5480 }
5481 if (p)
5482 {
5483 *pa = TREE_CHAIN (*pa);
5484 continue;
5485 }
5486 }
5487 }
5488 pa = &TREE_CHAIN (*pa);
5489 }
5490 }
5491
5492 /* Look up the current declaration with all the attributes merged
5493 so far so that attributes on the current declaration that's
5494 about to be pushed that conflict with the former can be detected,
5495 diagnosed, and rejected as appropriate. */
5496 tree last_decl = lookup_last_decl (decl: *node);
5497 return decl_attributes (node, attributes, flags, last_decl);
5498}
5499
5500
5501/* Decode a declarator in an ordinary declaration or data definition.
5502 This is called as soon as the type information and variable name
5503 have been parsed, before parsing the initializer if any.
5504 Here we create the ..._DECL node, fill in its type,
5505 and (if DO_PUSH) put it on the list of decls for the current context.
5506 When nonnull, set *LASTLOC to the location of the prior declaration
5507 of the same entity if one exists.
5508 The ..._DECL node is returned as the value.
5509
5510 Exception: for arrays where the length is not specified,
5511 the type is left null, to be filled in by `finish_decl'.
5512
5513 Function definitions do not come here; they go to start_function
5514 instead. However, external and forward declarations of functions
5515 do go through here. Structure field declarations are done by
5516 grokfield and not through here. */
5517
5518tree
5519start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5520 bool initialized, tree attributes, bool do_push /* = true */,
5521 location_t *lastloc /* = NULL */)
5522{
5523 tree decl;
5524 tree old_decl;
5525 tree tem;
5526 tree expr = NULL_TREE;
5527 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5528
5529 /* An object declared as __attribute__((unavailable)) suppresses
5530 warnings and errors from __attribute__((deprecated/unavailable))
5531 components.
5532 An object declared as __attribute__((deprecated)) suppresses
5533 warnings of uses of other deprecated items. */
5534 if (lookup_attribute (attr_name: "unavailable", list: attributes))
5535 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5536 else if (lookup_attribute (attr_name: "deprecated", list: attributes))
5537 deprecated_state = DEPRECATED_SUPPRESS;
5538
5539 decl = grokdeclarator (declarator, declspecs,
5540 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5541 deprecated_state);
5542 if (!decl || decl == error_mark_node)
5543 return NULL_TREE;
5544
5545 old_decl = lookup_last_decl (decl);
5546
5547 if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5548 if (lastdecl != error_mark_node)
5549 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5550
5551 /* Make sure the size expression is evaluated at this point. */
5552 if (expr && !current_scope->parm_flag)
5553 add_stmt (fold_convert (void_type_node, expr));
5554
5555 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5556 && TREE_PUBLIC (decl))
5557 warning (OPT_Wmain, "%q+D is usually a function", decl);
5558
5559 if (warn_missing_variable_declarations && VAR_P (decl)
5560 && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5561 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5562 "no previous declaration for %qD", decl);
5563
5564 if (initialized)
5565 /* Is it valid for this decl to have an initializer at all?
5566 If not, set INITIALIZED to zero, which will indirectly
5567 tell 'finish_decl' to ignore the initializer once it is parsed. */
5568 switch (TREE_CODE (decl))
5569 {
5570 case TYPE_DECL:
5571 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5572 initialized = false;
5573 break;
5574
5575 case FUNCTION_DECL:
5576 error ("function %qD is initialized like a variable", decl);
5577 initialized = false;
5578 break;
5579
5580 case PARM_DECL:
5581 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5582 error ("parameter %qD is initialized", decl);
5583 initialized = false;
5584 break;
5585
5586 default:
5587 /* Don't allow initializations for incomplete types except for
5588 arrays which might be completed by the initialization. */
5589
5590 /* This can happen if the array size is an undefined macro.
5591 We already gave a warning, so we don't need another one. */
5592 if (TREE_TYPE (decl) == error_mark_node)
5593 initialized = false;
5594 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5595 {
5596 /* A complete type is ok if size is fixed. If the size is
5597 variable, an empty initializer is OK and nonempty
5598 initializers will be diagnosed in the parser. */
5599 }
5600 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5601 {
5602 error ("variable %qD has initializer but incomplete type", decl);
5603 initialized = false;
5604 }
5605 }
5606
5607 if (initialized)
5608 {
5609 if (current_scope == file_scope)
5610 TREE_STATIC (decl) = 1;
5611
5612 /* Tell 'pushdecl' this is an initialized decl
5613 even though we don't yet have the initializer expression.
5614 Also tell 'finish_decl' it may store the real initializer. */
5615 DECL_INITIAL (decl) = error_mark_node;
5616 }
5617
5618 /* If this is a function declaration, write a record describing it to the
5619 prototypes file (if requested). */
5620
5621 if (TREE_CODE (decl) == FUNCTION_DECL)
5622 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5623
5624 /* ANSI specifies that a tentative definition which is not merged with
5625 a non-tentative definition behaves exactly like a definition with an
5626 initializer equal to zero. (Section 3.7.2)
5627
5628 -fno-common gives strict ANSI behavior, though this tends to break
5629 a large body of code that grew up without this rule.
5630
5631 Thread-local variables are never common, since there's no entrenched
5632 body of code to break, and it allows more efficient variable references
5633 in the presence of dynamic linking. */
5634
5635 if (VAR_P (decl)
5636 && !initialized
5637 && TREE_PUBLIC (decl)
5638 && !DECL_THREAD_LOCAL_P (decl)
5639 && !flag_no_common)
5640 DECL_COMMON (decl) = 1;
5641
5642 /* Set attributes here so if duplicate decl, will have proper attributes. */
5643 c_decl_attributes (node: &decl, attributes, flags: 0);
5644
5645 /* Handle gnu_inline attribute. */
5646 if (declspecs->inline_p
5647 && !flag_gnu89_inline
5648 && TREE_CODE (decl) == FUNCTION_DECL
5649 && (lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (decl))
5650 || current_function_decl))
5651 {
5652 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5653 ;
5654 else if (declspecs->storage_class != csc_static)
5655 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5656 }
5657
5658 if (TREE_CODE (decl) == FUNCTION_DECL
5659 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5660 {
5661 struct c_declarator *ce = declarator;
5662
5663 if (ce->kind == cdk_pointer)
5664 ce = declarator->declarator;
5665 if (ce->kind == cdk_function)
5666 {
5667 tree args = ce->u.arg_info->parms;
5668 for (; args; args = DECL_CHAIN (args))
5669 {
5670 tree type = TREE_TYPE (args);
5671 if (type && INTEGRAL_TYPE_P (type)
5672 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5673 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5674 }
5675 }
5676 }
5677
5678 if (TREE_CODE (decl) == FUNCTION_DECL
5679 && DECL_DECLARED_INLINE_P (decl)
5680 && DECL_UNINLINABLE (decl)
5681 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl)))
5682 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5683 decl, "noinline");
5684
5685 /* C99 6.7.4p3: An inline definition of a function with external
5686 linkage shall not contain a definition of a modifiable object
5687 with static storage duration... */
5688 if (VAR_P (decl)
5689 && current_scope != file_scope
5690 && TREE_STATIC (decl)
5691 && !TREE_READONLY (decl)
5692 && DECL_DECLARED_INLINE_P (current_function_decl)
5693 && DECL_EXTERNAL (current_function_decl))
5694 record_inline_static (loc: input_location, func: current_function_decl,
5695 decl, type: csi_modifiable);
5696
5697 if (c_dialect_objc ()
5698 && VAR_OR_FUNCTION_DECL_P (decl))
5699 objc_check_global_decl (decl);
5700
5701 /* Add this decl to the current scope.
5702 TEM may equal DECL or it may be a previous decl of the same name. */
5703 if (do_push)
5704 {
5705 tem = pushdecl (x: decl);
5706
5707 if (initialized && DECL_EXTERNAL (tem))
5708 {
5709 DECL_EXTERNAL (tem) = 0;
5710 TREE_STATIC (tem) = 1;
5711 }
5712
5713 return tem;
5714 }
5715 else
5716 return decl;
5717}
5718
5719/* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5720 DECL or the non-array element type if DECL is an uninitialized array.
5721 If that type has a const member, diagnose this. */
5722
5723static void
5724diagnose_uninitialized_cst_member (tree decl, tree type)
5725{
5726 tree field;
5727 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5728 {
5729 tree field_type;
5730 if (TREE_CODE (field) != FIELD_DECL)
5731 continue;
5732 field_type = strip_array_types (TREE_TYPE (field));
5733
5734 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5735 {
5736 auto_diagnostic_group d;
5737 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5738 "uninitialized const member in %qT is invalid in C++",
5739 strip_array_types (TREE_TYPE (decl))))
5740 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5741 }
5742
5743 if (RECORD_OR_UNION_TYPE_P (field_type))
5744 diagnose_uninitialized_cst_member (decl, type: field_type);
5745 }
5746}
5747
5748/* Finish processing of a declaration;
5749 install its initial value.
5750 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5751 If the length of an array type is not known before,
5752 it must be determined now, from the initial value, or it is an error.
5753
5754 INIT_LOC is the location of the initial value. */
5755
5756void
5757finish_decl (tree decl, location_t init_loc, tree init,
5758 tree origtype, tree asmspec_tree)
5759{
5760 tree type;
5761 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5762 const char *asmspec = 0;
5763
5764 /* If a name was specified, get the string. */
5765 if (VAR_OR_FUNCTION_DECL_P (decl)
5766 && DECL_FILE_SCOPE_P (decl))
5767 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5768 if (asmspec_tree)
5769 asmspec = TREE_STRING_POINTER (asmspec_tree);
5770
5771 if (VAR_P (decl)
5772 && TREE_STATIC (decl)
5773 && global_bindings_p ())
5774 /* So decl is a global variable. Record the types it uses
5775 so that we can decide later to emit debug info for them. */
5776 record_types_used_by_current_var_decl (decl);
5777
5778 /* If `start_decl' didn't like having an initialization, ignore it now. */
5779 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5780 init = NULL_TREE;
5781
5782 /* Don't crash if parm is initialized. */
5783 if (TREE_CODE (decl) == PARM_DECL)
5784 init = NULL_TREE;
5785
5786 if (init)
5787 store_init_value (init_loc, decl, init, origtype);
5788
5789 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5790 || TREE_CODE (decl) == FIELD_DECL))
5791 objc_check_decl (decl);
5792
5793 type = TREE_TYPE (decl);
5794
5795 /* Deduce size of array from initialization, if not already known.
5796 This is only needed for an initialization in the current scope;
5797 it must not be done for a file-scope initialization of a
5798 declaration with external linkage, redeclared in an inner scope
5799 with the outer declaration shadowed in an intermediate scope. */
5800 if (TREE_CODE (type) == ARRAY_TYPE
5801 && TYPE_DOMAIN (type) == NULL_TREE
5802 && TREE_CODE (decl) != TYPE_DECL
5803 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5804 {
5805 bool do_default
5806 = (TREE_STATIC (decl)
5807 /* Even if pedantic, an external linkage array
5808 may have incomplete type at first. */
5809 ? pedantic && !TREE_PUBLIC (decl)
5810 : !DECL_EXTERNAL (decl));
5811 int failure
5812 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5813 do_default);
5814
5815 /* Get the completed type made by complete_array_type. */
5816 type = TREE_TYPE (decl);
5817
5818 switch (failure)
5819 {
5820 case 1:
5821 error ("initializer fails to determine size of %q+D", decl);
5822 break;
5823
5824 case 2:
5825 if (do_default)
5826 error ("array size missing in %q+D", decl);
5827 break;
5828
5829 case 3:
5830 error ("zero or negative size array %q+D", decl);
5831 break;
5832
5833 case 0:
5834 /* For global variables, update the copy of the type that
5835 exists in the binding. */
5836 if (TREE_PUBLIC (decl))
5837 {
5838 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5839 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5840 b_ext = b_ext->shadowed;
5841 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5842 {
5843 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5844 b_ext->u.type = composite_type (b_ext->u.type, type);
5845 else
5846 b_ext->u.type = type;
5847 }
5848 }
5849 break;
5850
5851 default:
5852 gcc_unreachable ();
5853 }
5854
5855 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5856 TREE_TYPE (DECL_INITIAL (decl)) = type;
5857
5858 relayout_decl (decl);
5859 }
5860
5861 /* Look for braced array initializers for character arrays and
5862 recursively convert them into STRING_CSTs. */
5863 if (tree init = DECL_INITIAL (decl))
5864 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5865
5866 if (VAR_P (decl))
5867 {
5868 if (init && TREE_CODE (init) == CONSTRUCTOR)
5869 add_flexible_array_elts_to_size (decl, init);
5870
5871 complete_flexible_array_elts (DECL_INITIAL (decl));
5872
5873 if (is_global_var (t: decl))
5874 {
5875 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5876 ? TCTX_THREAD_STORAGE
5877 : TCTX_STATIC_STORAGE);
5878 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5879 TREE_TYPE (decl) = error_mark_node;
5880 }
5881
5882 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5883 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5884 layout_decl (decl, 0);
5885
5886 if (DECL_SIZE (decl) == NULL_TREE
5887 /* Don't give an error if we already gave one earlier. */
5888 && TREE_TYPE (decl) != error_mark_node
5889 && (TREE_STATIC (decl)
5890 /* A static variable with an incomplete type
5891 is an error if it is initialized.
5892 Also if it is not file scope.
5893 Also if it is thread-local (in C23).
5894 Otherwise, let it through, but if it is not `extern'
5895 then it may cause an error message later. */
5896 ? (DECL_INITIAL (decl) != NULL_TREE
5897 || !DECL_FILE_SCOPE_P (decl)
5898 || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5899 /* An automatic variable with an incomplete type
5900 is an error. */
5901 : !DECL_EXTERNAL (decl)))
5902 {
5903 error ("storage size of %q+D isn%'t known", decl);
5904 TREE_TYPE (decl) = error_mark_node;
5905 }
5906
5907 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5908 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5909 && DECL_SIZE (decl) == NULL_TREE
5910 && TREE_STATIC (decl))
5911 incomplete_record_decls.safe_push (obj: decl);
5912
5913 if (is_global_var (t: decl)
5914 && DECL_SIZE (decl) != NULL_TREE
5915 && TREE_TYPE (decl) != error_mark_node)
5916 {
5917 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5918 constant_expression_warning (DECL_SIZE (decl));
5919 else
5920 {
5921 error ("storage size of %q+D isn%'t constant", decl);
5922 TREE_TYPE (decl) = error_mark_node;
5923 }
5924 }
5925
5926 if (TREE_USED (type))
5927 {
5928 TREE_USED (decl) = 1;
5929 DECL_READ_P (decl) = 1;
5930 }
5931 }
5932
5933 /* If this is a function and an assembler name is specified, reset DECL_RTL
5934 so we can give it its new name. Also, update builtin_decl if it
5935 was a normal built-in. */
5936 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5937 {
5938 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5939 set_builtin_user_assembler_name (decl, asmspec);
5940 set_user_assembler_name (decl, asmspec);
5941 }
5942
5943 /* If #pragma weak was used, mark the decl weak now. */
5944 maybe_apply_pragma_weak (decl);
5945
5946 /* Output the assembler code and/or RTL code for variables and functions,
5947 unless the type is an undefined structure or union.
5948 If not, it will get done when the type is completed. */
5949
5950 if (VAR_OR_FUNCTION_DECL_P (decl))
5951 {
5952 /* Determine the ELF visibility. */
5953 if (TREE_PUBLIC (decl))
5954 c_determine_visibility (decl);
5955
5956 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5957 if (c_dialect_objc ())
5958 objc_check_decl (decl);
5959
5960 if (asmspec)
5961 {
5962 /* If this is not a static variable, issue a warning.
5963 It doesn't make any sense to give an ASMSPEC for an
5964 ordinary, non-register local variable. Historically,
5965 GCC has accepted -- but ignored -- the ASMSPEC in
5966 this case. */
5967 if (!DECL_FILE_SCOPE_P (decl)
5968 && VAR_P (decl)
5969 && !C_DECL_REGISTER (decl)
5970 && !TREE_STATIC (decl))
5971 warning (0, "ignoring %<asm%> specifier for non-static local "
5972 "variable %q+D", decl);
5973 else
5974 set_user_assembler_name (decl, asmspec);
5975 }
5976
5977 if (DECL_FILE_SCOPE_P (decl))
5978 {
5979 if (DECL_INITIAL (decl) == NULL_TREE
5980 || DECL_INITIAL (decl) == error_mark_node)
5981 /* Don't output anything
5982 when a tentative file-scope definition is seen.
5983 But at end of compilation, do output code for them. */
5984 DECL_DEFER_OUTPUT (decl) = 1;
5985 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5986 DECL_HARD_REGISTER (decl) = 1;
5987 rest_of_decl_compilation (decl, true, 0);
5988
5989 if (TREE_CODE (decl) == FUNCTION_DECL)
5990 {
5991 tree parms = DECL_ARGUMENTS (decl);
5992 const bool builtin = fndecl_built_in_p (node: decl);
5993 if (tree access = build_attr_access_from_parms (parms, !builtin))
5994 decl_attributes (&decl, access, 0);
5995 }
5996 }
5997 else
5998 {
5999 /* In conjunction with an ASMSPEC, the `register'
6000 keyword indicates that we should place the variable
6001 in a particular register. */
6002 if (asmspec && C_DECL_REGISTER (decl))
6003 {
6004 DECL_HARD_REGISTER (decl) = 1;
6005 /* This cannot be done for a structure with volatile
6006 fields, on which DECL_REGISTER will have been
6007 reset. */
6008 if (!DECL_REGISTER (decl))
6009 error ("cannot put object with volatile field into register");
6010 }
6011
6012 if (TREE_CODE (decl) != FUNCTION_DECL)
6013 {
6014 /* If we're building a variable sized type, and we might be
6015 reachable other than via the top of the current binding
6016 level, then create a new BIND_EXPR so that we deallocate
6017 the object at the right time. */
6018 /* Note that DECL_SIZE can be null due to errors. */
6019 if (DECL_SIZE (decl)
6020 && !TREE_CONSTANT (DECL_SIZE (decl))
6021 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6022 {
6023 tree bind;
6024 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6025 TREE_SIDE_EFFECTS (bind) = 1;
6026 add_stmt (t: bind);
6027 BIND_EXPR_BODY (bind) = push_stmt_list ();
6028 }
6029 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl),
6030 DECL_EXPR, decl));
6031 }
6032 }
6033
6034
6035 if (!DECL_FILE_SCOPE_P (decl))
6036 {
6037 /* Recompute the RTL of a local array now
6038 if it used to be an incomplete type. */
6039 if (was_incomplete && !is_global_var (t: decl))
6040 {
6041 /* If we used it already as memory, it must stay in memory. */
6042 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6043 /* If it's still incomplete now, no init will save it. */
6044 if (DECL_SIZE (decl) == NULL_TREE)
6045 DECL_INITIAL (decl) = NULL_TREE;
6046 }
6047 }
6048 }
6049
6050 if (TREE_CODE (decl) == TYPE_DECL)
6051 {
6052 if (!DECL_FILE_SCOPE_P (decl)
6053 && c_type_variably_modified_p (TREE_TYPE (decl)))
6054 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6055
6056 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6057 }
6058
6059 /* Install a cleanup (aka destructor) if one was given. */
6060 if (VAR_P (decl) && !TREE_STATIC (decl))
6061 {
6062 tree attr = lookup_attribute (attr_name: "cleanup", DECL_ATTRIBUTES (decl));
6063 if (attr)
6064 {
6065 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6066 tree cleanup_decl = lookup_name (name: cleanup_id);
6067 tree cleanup;
6068 vec<tree, va_gc> *v;
6069
6070 /* Build "cleanup(&decl)" for the destructor. */
6071 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6072 vec_alloc (v, nelems: 1);
6073 v->quick_push (obj: cleanup);
6074 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6075 vNULL, cleanup_decl, v, NULL);
6076 vec_free (v);
6077
6078 /* Don't warn about decl unused; the cleanup uses it. */
6079 TREE_USED (decl) = 1;
6080 TREE_USED (cleanup_decl) = 1;
6081 DECL_READ_P (decl) = 1;
6082
6083 push_cleanup (decl, cleanup, false);
6084 }
6085 }
6086
6087 if (warn_cxx_compat
6088 && VAR_P (decl)
6089 && !DECL_EXTERNAL (decl)
6090 && DECL_INITIAL (decl) == NULL_TREE)
6091 {
6092 type = strip_array_types (type);
6093 if (TREE_READONLY (decl))
6094 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6095 "uninitialized %<const %D%> is invalid in C++", decl);
6096 else if (RECORD_OR_UNION_TYPE_P (type)
6097 && C_TYPE_FIELDS_READONLY (type))
6098 diagnose_uninitialized_cst_member (decl, type);
6099 }
6100
6101 if (flag_openmp
6102 && VAR_P (decl)
6103 && lookup_attribute (attr_name: "omp declare target implicit",
6104 DECL_ATTRIBUTES (decl)))
6105 {
6106 DECL_ATTRIBUTES (decl)
6107 = remove_attribute ("omp declare target implicit",
6108 DECL_ATTRIBUTES (decl));
6109 if (!omp_mappable_type (TREE_TYPE (decl)))
6110 error ("%q+D in declare target directive does not have mappable type",
6111 decl);
6112 else if (!lookup_attribute (attr_name: "omp declare target",
6113 DECL_ATTRIBUTES (decl))
6114 && !lookup_attribute (attr_name: "omp declare target link",
6115 DECL_ATTRIBUTES (decl)))
6116 {
6117 DECL_ATTRIBUTES (decl)
6118 = tree_cons (get_identifier ("omp declare target"),
6119 NULL_TREE, DECL_ATTRIBUTES (decl));
6120 symtab_node *node = symtab_node::get (decl);
6121 if (node != NULL)
6122 {
6123 node->offloadable = 1;
6124 if (ENABLE_OFFLOADING)
6125 {
6126 g->have_offload = true;
6127 if (is_a <varpool_node *> (p: node))
6128 vec_safe_push (v&: offload_vars, obj: decl);
6129 }
6130 }
6131 }
6132 }
6133
6134 /* This is the last point we can lower alignment so give the target the
6135 chance to do so. */
6136 if (VAR_P (decl)
6137 && !is_global_var (t: decl)
6138 && !DECL_HARD_REGISTER (decl))
6139 targetm.lower_local_decl_alignment (decl);
6140
6141 invoke_plugin_callbacks (event: PLUGIN_FINISH_DECL, gcc_data: decl);
6142}
6143
6144/* Given a parsed parameter declaration, decode it into a PARM_DECL.
6145 EXPR is NULL or a pointer to an expression that needs to be
6146 evaluated for the side effects of array size expressions in the
6147 parameters. */
6148
6149tree
6150grokparm (const struct c_parm *parm, tree *expr)
6151{
6152 tree attrs = parm->attrs;
6153 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6154 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6155
6156 decl_attributes (&decl, attrs, 0);
6157
6158 return decl;
6159}
6160
6161/* Return attribute "arg spec" corresponding to an array/VLA parameter
6162 described by PARM, concatenated onto attributes ATTRS.
6163 The spec consists of one dollar symbol for each specified variable
6164 bound, one asterisk for each unspecified variable bound, followed
6165 by at most one specification of the most significant bound of
6166 an ordinary array parameter. For ordinary arrays the specification
6167 is either the constant bound itself, or the space character for
6168 an array with an unspecified bound (the [] form). Finally, a chain
6169 of specified variable bounds is appended to the spec, starting with
6170 the most significant bound. For example, the PARM T a[2][m][3][n]
6171 will produce __attribute__((arg spec ("[$$2]", m, n)).
6172 For T a typedef for an array with variable bounds, the bounds are
6173 included in the specification in the expected order.
6174 No "arg spec" is created for parameters of pointer types, making
6175 a distinction between T(*)[N] (or, equivalently, T[][N]) and
6176 the T[M][N] form, all of which have the same type and are represented
6177 the same, but only the last of which gets an "arg spec" describing
6178 the most significant bound M. */
6179
6180static tree
6181get_parm_array_spec (const struct c_parm *parm, tree attrs)
6182{
6183 /* The attribute specification string, minor bound first. */
6184 std::string spec;
6185
6186 /* A list of VLA variable bounds, major first, or null if unspecified
6187 or not a VLA. */
6188 tree vbchain = NULL_TREE;
6189 /* True for a pointer parameter. */
6190 bool pointer = false;
6191 /* True for an ordinary array with an unpecified bound. */
6192 bool nobound = false;
6193
6194 /* Create a string representation for the bounds of the array/VLA. */
6195 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6196 {
6197 next = pd->declarator;
6198 while (next && next->kind == cdk_attrs)
6199 next = next->declarator;
6200
6201 /* Remember if a pointer has been seen to avoid storing the constant
6202 bound. */
6203 if (pd->kind == cdk_pointer)
6204 pointer = true;
6205
6206 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6207 && (!next || next->kind == cdk_id))
6208 {
6209 /* Do nothing for the common case of a pointer. The fact that
6210 the parameter is one can be deduced from the absence of
6211 an arg spec for it. */
6212 return attrs;
6213 }
6214
6215 if (pd->kind == cdk_id)
6216 {
6217 if (pointer
6218 || !parm->specs->type
6219 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6220 || !TYPE_DOMAIN (parm->specs->type)
6221 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6222 continue;
6223
6224 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6225 if (!vbchain
6226 && TREE_CODE (max) == INTEGER_CST)
6227 {
6228 /* Extract the upper bound from a parameter of an array type
6229 unless the parameter is an ordinary array of unspecified
6230 bound in which case a next iteration of the loop will
6231 exit. */
6232 if (spec.empty () || spec.end ()[-1] != ' ')
6233 {
6234 if (!tree_fits_shwi_p (max))
6235 continue;
6236
6237 /* The upper bound is the value of the largest valid
6238 index. */
6239 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6240 char buf[40];
6241 sprintf (s: buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6242 spec += buf;
6243 }
6244 continue;
6245 }
6246
6247 /* For a VLA typedef, create a list of its variable bounds and
6248 append it in the expected order to VBCHAIN. */
6249 tree tpbnds = NULL_TREE;
6250 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6251 type = TREE_TYPE (type))
6252 {
6253 tree nelts = array_type_nelts (type);
6254 if (error_operand_p (t: nelts))
6255 return attrs;
6256 if (TREE_CODE (nelts) != INTEGER_CST)
6257 {
6258 /* Each variable VLA bound is represented by the dollar
6259 sign. */
6260 spec += "$";
6261 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
6262 }
6263 }
6264 tpbnds = nreverse (tpbnds);
6265 vbchain = chainon (vbchain, tpbnds);
6266 continue;
6267 }
6268
6269 if (pd->kind != cdk_array)
6270 continue;
6271
6272 if (pd->u.array.vla_unspec_p)
6273 {
6274 /* Each unspecified bound is represented by a star. There
6275 can be any number of these in a declaration (but none in
6276 a definition). */
6277 spec += '*';
6278 continue;
6279 }
6280
6281 tree nelts = pd->u.array.dimen;
6282 if (!nelts)
6283 {
6284 /* Ordinary array of unspecified size. There can be at most
6285 one for the most significant bound. Exit on the next
6286 iteration which determines whether or not PARM is declared
6287 as a pointer or an array. */
6288 nobound = true;
6289 continue;
6290 }
6291
6292 if (pd->u.array.static_p)
6293 spec += 's';
6294
6295 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6296 /* Avoid invalid NELTS. */
6297 return attrs;
6298
6299 STRIP_NOPS (nelts);
6300 nelts = c_fully_fold (nelts, false, nullptr);
6301 if (TREE_CODE (nelts) == INTEGER_CST)
6302 {
6303 /* Skip all constant bounds except the most significant one.
6304 The interior ones are included in the array type. */
6305 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6306 continue;
6307
6308 if (!tree_fits_uhwi_p (nelts))
6309 /* Bail completely on invalid bounds. */
6310 return attrs;
6311
6312 char buf[40];
6313 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6314 sprintf (s: buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6315 spec += buf;
6316 break;
6317 }
6318
6319 /* Each variable VLA bound is represented by a dollar sign. */
6320 spec += "$";
6321 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6322 }
6323
6324 if (spec.empty () && !nobound)
6325 return attrs;
6326
6327 spec.insert (pos: 0, s: "[");
6328 if (nobound)
6329 /* Ordinary array of unspecified bound is represented by a space.
6330 It must be last in the spec. */
6331 spec += ' ';
6332 spec += ']';
6333
6334 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6335 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6336 tree name = get_identifier ("arg spec");
6337 return tree_cons (name, args, attrs);
6338}
6339
6340/* Given a parsed parameter declaration, decode it into a PARM_DECL
6341 and push that on the current scope. EXPR is a pointer to an
6342 expression that needs to be evaluated for the side effects of array
6343 size expressions in the parameters. */
6344
6345void
6346push_parm_decl (const struct c_parm *parm, tree *expr)
6347{
6348 tree attrs = parm->attrs;
6349 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6350 &attrs, expr, NULL, DEPRECATED_NORMAL);
6351 if (decl && DECL_P (decl))
6352 DECL_SOURCE_LOCATION (decl) = parm->loc;
6353
6354 attrs = get_parm_array_spec (parm, attrs);
6355 decl_attributes (&decl, attrs, 0);
6356
6357 decl = pushdecl (x: decl);
6358
6359 finish_decl (decl, init_loc: input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6360}
6361
6362/* Mark all the parameter declarations to date as forward decls.
6363 Also diagnose use of this extension. */
6364
6365void
6366mark_forward_parm_decls (void)
6367{
6368 struct c_binding *b;
6369
6370 if (pedantic && !current_scope->warned_forward_parm_decls)
6371 {
6372 pedwarn (input_location, OPT_Wpedantic,
6373 "ISO C forbids forward parameter declarations");
6374 current_scope->warned_forward_parm_decls = true;
6375 }
6376
6377 for (b = current_scope->bindings; b; b = b->prev)
6378 if (TREE_CODE (b->decl) == PARM_DECL)
6379 TREE_ASM_WRITTEN (b->decl) = 1;
6380}
6381
6382/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6383 literal, which may be an incomplete array type completed by the
6384 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6385 literal. NON_CONST is true if the initializers contain something
6386 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6387 it is the (valid) alignment for this compound literal, as specified
6388 with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6389 compound literal. */
6390
6391tree
6392build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6393 unsigned int alignas_align,
6394 struct c_declspecs *scspecs)
6395{
6396 /* We do not use start_decl here because we have a type, not a declarator;
6397 and do not use finish_decl because the decl should be stored inside
6398 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6399 tree decl;
6400 tree complit;
6401 tree stmt;
6402 bool threadp = scspecs ? scspecs->thread_p : false;
6403 enum c_storage_class storage_class = (scspecs
6404 ? scspecs->storage_class
6405 : csc_none);
6406
6407 if (type == error_mark_node
6408 || init == error_mark_node)
6409 return error_mark_node;
6410
6411 if (current_scope == file_scope && storage_class == csc_register)
6412 {
6413 error_at (loc, "file-scope compound literal specifies %<register%>");
6414 storage_class = csc_none;
6415 }
6416
6417 if (current_scope != file_scope && threadp && storage_class == csc_none)
6418 {
6419 error_at (loc, "compound literal implicitly auto and declared %qs",
6420 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6421 threadp = false;
6422 }
6423
6424 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6425 DECL_EXTERNAL (decl) = 0;
6426 TREE_PUBLIC (decl) = 0;
6427 TREE_STATIC (decl) = (current_scope == file_scope
6428 || storage_class == csc_static);
6429 DECL_CONTEXT (decl) = current_function_decl;
6430 TREE_USED (decl) = 1;
6431 DECL_READ_P (decl) = 1;
6432 DECL_ARTIFICIAL (decl) = 1;
6433 DECL_IGNORED_P (decl) = 1;
6434 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6435 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6436 TREE_TYPE (decl) = type;
6437 if (threadp)
6438 set_decl_tls_model (decl, decl_default_tls_model (decl));
6439 if (storage_class == csc_register)
6440 {
6441 C_DECL_REGISTER (decl) = 1;
6442 DECL_REGISTER (decl) = 1;
6443 }
6444 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6445 if (alignas_align)
6446 {
6447 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6448 DECL_USER_ALIGN (decl) = 1;
6449 }
6450 store_init_value (loc, decl, init, NULL_TREE);
6451 if (current_scope != file_scope
6452 && TREE_STATIC (decl)
6453 && !TREE_READONLY (decl)
6454 && DECL_DECLARED_INLINE_P (current_function_decl)
6455 && DECL_EXTERNAL (current_function_decl))
6456 record_inline_static (loc: input_location, func: current_function_decl,
6457 decl, type: csi_modifiable);
6458
6459 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6460 {
6461 int failure = complete_array_type (&TREE_TYPE (decl),
6462 DECL_INITIAL (decl), true);
6463 /* If complete_array_type returns 3, it means that the
6464 initial value of the compound literal is empty. Allow it. */
6465 gcc_assert (failure == 0 || failure == 3);
6466
6467 type = TREE_TYPE (decl);
6468 TREE_TYPE (DECL_INITIAL (decl)) = type;
6469 }
6470
6471 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6472 {
6473 c_incomplete_type_error (loc, NULL_TREE, type);
6474 return error_mark_node;
6475 }
6476
6477 if (TREE_STATIC (decl)
6478 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6479 return error_mark_node;
6480
6481 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6482 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6483 TREE_SIDE_EFFECTS (complit) = 1;
6484
6485 layout_decl (decl, 0);
6486
6487 if (TREE_STATIC (decl))
6488 {
6489 /* This decl needs a name for the assembler output. */
6490 set_compound_literal_name (decl);
6491 DECL_DEFER_OUTPUT (decl) = 1;
6492 DECL_COMDAT (decl) = 1;
6493 pushdecl (x: decl);
6494 rest_of_decl_compilation (decl, 1, 0);
6495 }
6496 else if (current_function_decl && !current_scope->parm_flag)
6497 pushdecl (x: decl);
6498
6499 if (non_const)
6500 {
6501 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6502 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6503 }
6504
6505 return complit;
6506}
6507
6508/* Check the type of a compound literal. Here we just check that it
6509 is valid for C++. */
6510
6511void
6512check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6513{
6514 if (warn_cxx_compat
6515 && (type_name->specs->typespec_kind == ctsk_tagdef
6516 || type_name->specs->typespec_kind == ctsk_tagfirstref
6517 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6518 warning_at (loc, OPT_Wc___compat,
6519 "defining a type in a compound literal is invalid in C++");
6520}
6521
6522/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6523 replacing with appropriate values if they are invalid. */
6524
6525static void
6526check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6527 tree orig_name)
6528{
6529 tree type_mv;
6530 unsigned int max_width;
6531 unsigned HOST_WIDE_INT w;
6532 const char *name = (orig_name
6533 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6534 : _("<anonymous>"));
6535
6536 /* Detect and ignore out of range field width and process valid
6537 field widths. */
6538 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6539 {
6540 error_at (loc, "bit-field %qs width not an integer constant", name);
6541 *width = integer_one_node;
6542 }
6543 else
6544 {
6545 if (TREE_CODE (*width) != INTEGER_CST)
6546 {
6547 *width = c_fully_fold (*width, false, NULL);
6548 if (TREE_CODE (*width) == INTEGER_CST)
6549 pedwarn (loc, OPT_Wpedantic,
6550 "bit-field %qs width not an integer constant expression",
6551 name);
6552 }
6553 if (TREE_CODE (*width) != INTEGER_CST)
6554 {
6555 error_at (loc, "bit-field %qs width not an integer constant", name);
6556 *width = integer_one_node;
6557 }
6558 constant_expression_warning (*width);
6559 if (tree_int_cst_sgn (*width) < 0)
6560 {
6561 error_at (loc, "negative width in bit-field %qs", name);
6562 *width = integer_one_node;
6563 }
6564 else if (integer_zerop (*width) && orig_name)
6565 {
6566 error_at (loc, "zero width for bit-field %qs", name);
6567 *width = integer_one_node;
6568 }
6569 }
6570
6571 /* Detect invalid bit-field type. */
6572 if (TREE_CODE (*type) != INTEGER_TYPE
6573 && TREE_CODE (*type) != BOOLEAN_TYPE
6574 && TREE_CODE (*type) != ENUMERAL_TYPE
6575 && TREE_CODE (*type) != BITINT_TYPE)
6576 {
6577 error_at (loc, "bit-field %qs has invalid type", name);
6578 *type = unsigned_type_node;
6579 }
6580
6581 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6582 {
6583 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6584 name);
6585 *type = unsigned_type_node;
6586 }
6587
6588 type_mv = TYPE_MAIN_VARIANT (*type);
6589 if (!in_system_header_at (loc: input_location)
6590 && type_mv != integer_type_node
6591 && type_mv != unsigned_type_node
6592 && type_mv != boolean_type_node)
6593 pedwarn_c90 (loc, opt: OPT_Wpedantic,
6594 "type of bit-field %qs is a GCC extension", name);
6595
6596 max_width = TYPE_PRECISION (*type);
6597
6598 if (compare_tree_int (*width, max_width) > 0)
6599 {
6600 error_at (loc, "width of %qs exceeds its type", name);
6601 w = max_width;
6602 *width = build_int_cst (integer_type_node, w);
6603 }
6604 else
6605 w = tree_to_uhwi (*width);
6606
6607 /* Truncation of hardbool false and true representation values is always safe:
6608 either the values remain different, or we'll report a problem when creating
6609 the narrower type. */
6610 if (c_hardbool_type_attr (type: *type))
6611 return;
6612
6613 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6614 {
6615 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6616 if (!lt
6617 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6618 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6619 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6620 }
6621}
6622
6623
6624
6625/* Print warning about variable length array if necessary. */
6626
6627static void
6628warn_variable_length_array (tree name, tree size)
6629{
6630 if (TREE_CONSTANT (size))
6631 {
6632 if (name)
6633 pedwarn_c90 (input_location, opt: OPT_Wvla,
6634 "ISO C90 forbids array %qE whose size "
6635 "cannot be evaluated", name);
6636 else
6637 pedwarn_c90 (input_location, opt: OPT_Wvla, "ISO C90 forbids array "
6638 "whose size cannot be evaluated");
6639 }
6640 else
6641 {
6642 if (name)
6643 pedwarn_c90 (input_location, opt: OPT_Wvla,
6644 "ISO C90 forbids variable length array %qE", name);
6645 else
6646 pedwarn_c90 (input_location, opt: OPT_Wvla, "ISO C90 forbids variable "
6647 "length array");
6648 }
6649}
6650
6651/* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6652 considering only those c_declspec_words found in LIST, which
6653 must be terminated by cdw_number_of_elements. */
6654
6655static location_t
6656smallest_type_quals_location (const location_t *locations,
6657 const c_declspec_word *list)
6658{
6659 location_t loc = UNKNOWN_LOCATION;
6660 while (*list != cdw_number_of_elements)
6661 {
6662 location_t newloc = locations[*list];
6663 if (loc == UNKNOWN_LOCATION
6664 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6665 loc = newloc;
6666 list++;
6667 }
6668
6669 return loc;
6670}
6671
6672
6673/* We attach an artificial TYPE_DECL to pointed-to type
6674 and arrange for it to be included in a DECL_EXPR. This
6675 forces the sizes evaluation at a safe point and ensures it
6676 is not deferred until e.g. within a deeper conditional context.
6677
6678 PARM contexts have no enclosing statement list that
6679 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6680 instead, and add it to the list of expressions that
6681 need to be evaluated.
6682
6683 TYPENAME contexts do have an enclosing statement list,
6684 but it would be incorrect to use it, as the size should
6685 only be evaluated if the containing expression is
6686 evaluated. We might also be in the middle of an
6687 expression with side effects on the pointed-to type size
6688 "arguments" prior to the pointer declaration point and
6689 the fake TYPE_DECL in the enclosing context would force
6690 the size evaluation prior to the side effects. We therefore
6691 use BIND_EXPRs in TYPENAME contexts too. */
6692static void
6693add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6694{
6695 tree bind = NULL_TREE;
6696 if (expr)
6697 {
6698 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6699 NULL_TREE);
6700 TREE_SIDE_EFFECTS (bind) = 1;
6701 BIND_EXPR_BODY (bind) = push_stmt_list ();
6702 push_scope ();
6703 }
6704
6705 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6706 pushdecl (x: decl);
6707 DECL_ARTIFICIAL (decl) = 1;
6708 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6709 if (set_name_p)
6710 TYPE_NAME (type) = decl;
6711
6712 if (bind)
6713 {
6714 pop_scope ();
6715 BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6716 if (*expr)
6717 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6718 else
6719 *expr = bind;
6720 }
6721}
6722
6723/* Given declspecs and a declarator,
6724 determine the name and type of the object declared
6725 and construct a ..._DECL node for it.
6726 (In one case we can return a ..._TYPE node instead.
6727 For invalid input we sometimes return NULL_TREE.)
6728
6729 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6730
6731 DECL_CONTEXT says which syntactic context this declaration is in:
6732 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6733 FUNCDEF for a function definition. Like NORMAL but a few different
6734 error messages in each case. Return value may be zero meaning
6735 this definition is too screwy to try to parse.
6736 PARM for a parameter declaration (either within a function prototype
6737 or before a function body). Make a PARM_DECL, or return void_type_node.
6738 TYPENAME if for a typename (in a cast or sizeof).
6739 Don't make a DECL node; just return the ..._TYPE node.
6740 FIELD for a struct or union field; make a FIELD_DECL.
6741 INITIALIZED is true if the decl has an initializer.
6742 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6743 representing the width of the bit-field.
6744 DECL_ATTRS points to the list of attributes that should be added to this
6745 decl. Any nested attributes that belong on the decl itself will be
6746 added to this list.
6747 If EXPR is not NULL, any expressions that need to be evaluated as
6748 part of evaluating variably modified types will be stored in *EXPR.
6749 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6750 set to indicate whether operands in *EXPR can be used in constant
6751 expressions.
6752 DEPRECATED_STATE is a deprecated_states value indicating whether
6753 deprecation/unavailability warnings should be suppressed.
6754
6755 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6756 It may also be so in the PARM case, for a prototype where the
6757 argument type is specified but not the name.
6758
6759 This function is where the complicated C meanings of `static'
6760 and `extern' are interpreted. */
6761
6762static tree
6763grokdeclarator (const struct c_declarator *declarator,
6764 struct c_declspecs *declspecs,
6765 enum decl_context decl_context, bool initialized, tree *width,
6766 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6767 enum deprecated_states deprecated_state)
6768{
6769 tree type = declspecs->type;
6770 bool threadp = declspecs->thread_p;
6771 bool constexprp = declspecs->constexpr_p;
6772 enum c_storage_class storage_class = declspecs->storage_class;
6773 int constp;
6774 int restrictp;
6775 int volatilep;
6776 int atomicp;
6777 int type_quals = TYPE_UNQUALIFIED;
6778 tree name = NULL_TREE;
6779 bool funcdef_flag = false;
6780 bool funcdef_syntax = false;
6781 bool size_varies = false;
6782 tree decl_attr = declspecs->decl_attr;
6783 int array_ptr_quals = TYPE_UNQUALIFIED;
6784 tree array_ptr_attrs = NULL_TREE;
6785 bool array_parm_static = false;
6786 bool array_parm_vla_unspec_p = false;
6787 tree returned_attrs = NULL_TREE;
6788 tree decl_id_attrs = NULL_TREE;
6789 bool bitfield = width != NULL;
6790 tree element_type;
6791 tree orig_qual_type = NULL;
6792 size_t orig_qual_indirect = 0;
6793 struct c_arg_info *arg_info = 0;
6794 addr_space_t as1, as2, address_space;
6795 location_t loc = UNKNOWN_LOCATION;
6796 tree expr_dummy;
6797 bool expr_const_operands_dummy;
6798 enum c_declarator_kind first_non_attr_kind;
6799 unsigned int alignas_align = 0;
6800
6801 if (type == NULL_TREE)
6802 {
6803 /* This can occur for auto on a parameter in C23 mode. Set a
6804 dummy type here so subsequent code can give diagnostics for
6805 this case. */
6806 gcc_assert (declspecs->c23_auto_p);
6807 gcc_assert (decl_context == PARM);
6808 type = declspecs->type = integer_type_node;
6809 }
6810 if (TREE_CODE (type) == ERROR_MARK)
6811 return error_mark_node;
6812 if (expr == NULL)
6813 {
6814 expr = &expr_dummy;
6815 expr_dummy = NULL_TREE;
6816 }
6817 if (expr_const_operands == NULL)
6818 expr_const_operands = &expr_const_operands_dummy;
6819
6820 if (declspecs->expr)
6821 {
6822 if (*expr)
6823 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6824 declspecs->expr);
6825 else
6826 *expr = declspecs->expr;
6827 }
6828 *expr_const_operands = declspecs->expr_const_operands;
6829
6830 if (decl_context == FUNCDEF)
6831 funcdef_flag = true, decl_context = NORMAL;
6832
6833 /* Look inside a declarator for the name being declared
6834 and get it as an IDENTIFIER_NODE, for an error message. */
6835 {
6836 const struct c_declarator *decl = declarator;
6837
6838 first_non_attr_kind = cdk_attrs;
6839 while (decl)
6840 switch (decl->kind)
6841 {
6842 case cdk_array:
6843 loc = decl->id_loc;
6844 /* FALL THRU. */
6845
6846 case cdk_function:
6847 case cdk_pointer:
6848 funcdef_syntax = (decl->kind == cdk_function);
6849 if (first_non_attr_kind == cdk_attrs)
6850 first_non_attr_kind = decl->kind;
6851 decl = decl->declarator;
6852 break;
6853
6854 case cdk_attrs:
6855 decl = decl->declarator;
6856 break;
6857
6858 case cdk_id:
6859 loc = decl->id_loc;
6860 if (decl->u.id.id)
6861 name = decl->u.id.id;
6862 decl_id_attrs = decl->u.id.attrs;
6863 if (first_non_attr_kind == cdk_attrs)
6864 first_non_attr_kind = decl->kind;
6865 decl = 0;
6866 break;
6867
6868 default:
6869 gcc_unreachable ();
6870 }
6871 if (name == NULL_TREE)
6872 {
6873 gcc_assert (decl_context == PARM
6874 || decl_context == TYPENAME
6875 || (decl_context == FIELD
6876 && declarator->kind == cdk_id));
6877 gcc_assert (!initialized);
6878 }
6879 }
6880
6881 /* An enum type specifier (": specifier-qualifier-list") may only be
6882 specified when the enum is being defined or in an empty
6883 declaration of the form "enum identifier enum-type-specifier;".
6884 Except for the case of an empty declaration that has additional
6885 declaration specifiers, all invalid contexts (declarations that
6886 aren't empty, type names, parameter declarations, member
6887 declarations) pass through grokdeclarator. */
6888 if (declspecs->enum_type_specifier_ref_p)
6889 error_at (loc, "%<enum%> underlying type may not be specified here");
6890
6891 /* A function definition's declarator must have the form of
6892 a function declarator. */
6893
6894 if (funcdef_flag && !funcdef_syntax)
6895 return NULL_TREE;
6896
6897 /* If this looks like a function definition, make it one,
6898 even if it occurs where parms are expected.
6899 Then store_parm_decls will reject it and not use it as a parm. */
6900 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6901 decl_context = PARM;
6902
6903 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6904 {
6905 if (declspecs->unavailable_p)
6906 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6907 else if (declspecs->deprecated_p
6908 && deprecated_state != DEPRECATED_SUPPRESS)
6909 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6910 }
6911
6912 if ((decl_context == NORMAL || decl_context == FIELD)
6913 && current_scope == file_scope
6914 && c_type_variably_modified_p (t: type))
6915 {
6916 if (name)
6917 error_at (loc, "variably modified %qE at file scope", name);
6918 else
6919 error_at (loc, "variably modified field at file scope");
6920 type = integer_type_node;
6921 }
6922
6923 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6924
6925 /* Diagnose defaulting to "int". */
6926
6927 if (declspecs->default_int_p)
6928 {
6929 /* Issue a warning if this is an ISO C 99 program or if
6930 -Wreturn-type and this is a function, or if -Wimplicit;
6931 prefer the former warning since it is more explicit. */
6932 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6933 && funcdef_flag)
6934 warn_about_return_type = 1;
6935 else
6936 {
6937 if (name)
6938 permerror_opt (loc, OPT_Wimplicit_int,
6939 "type defaults to %<int%> in declaration "
6940 "of %qE", name);
6941 else
6942 permerror_opt (loc, OPT_Wimplicit_int,
6943 "type defaults to %<int%> in type name");
6944 }
6945 }
6946
6947 /* Adjust the type if a bit-field is being declared,
6948 -funsigned-bitfields applied and the type is not explicitly
6949 "signed". */
6950 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6951 && TREE_CODE (type) == INTEGER_TYPE)
6952 type = c_common_unsigned_type (type);
6953
6954 /* Figure out the type qualifiers for the declaration. There are
6955 two ways a declaration can become qualified. One is something
6956 like `const int i' where the `const' is explicit. Another is
6957 something like `typedef const int CI; CI i' where the type of the
6958 declaration contains the `const'. A third possibility is that
6959 there is a type qualifier on the element type of a typedefed
6960 array type, in which case we should extract that qualifier so
6961 that c_apply_type_quals_to_decl receives the full list of
6962 qualifiers to work with (C90 is not entirely clear about whether
6963 duplicate qualifiers should be diagnosed in this case, but it
6964 seems most appropriate to do so). */
6965 element_type = strip_array_types (type);
6966 constp = declspecs->const_p + TYPE_READONLY (element_type);
6967 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6968 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6969 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6970 as1 = declspecs->address_space;
6971 as2 = TYPE_ADDR_SPACE (element_type);
6972 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6973
6974 if (constp > 1)
6975 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<const%>");
6976 if (restrictp > 1)
6977 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<restrict%>");
6978 if (volatilep > 1)
6979 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<volatile%>");
6980 if (atomicp > 1)
6981 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<_Atomic%>");
6982
6983 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6984 error_at (loc, "conflicting named address spaces (%s vs %s)",
6985 c_addr_space_name (as: as1), c_addr_space_name (as: as2));
6986
6987 if ((TREE_CODE (type) == ARRAY_TYPE
6988 || first_non_attr_kind == cdk_array)
6989 && TYPE_QUALS (element_type))
6990 {
6991 orig_qual_type = type;
6992 type = TYPE_MAIN_VARIANT (type);
6993 }
6994 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6995 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6996 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6997 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6998 | ENCODE_QUAL_ADDR_SPACE (address_space));
6999 if (type_quals != TYPE_QUALS (element_type))
7000 orig_qual_type = NULL_TREE;
7001
7002 /* Applying the _Atomic qualifier to an array type (through the use
7003 of typedefs or typeof) must be detected here. If the qualifier
7004 is introduced later, any appearance of applying it to an array is
7005 actually applying it to an element of that array. */
7006 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7007 error_at (loc, "%<_Atomic%>-qualified array type");
7008
7009 /* Warn about storage classes that are invalid for certain
7010 kinds of declarations (parameters, typenames, etc.). */
7011
7012 if (funcdef_flag
7013 && (threadp
7014 || constexprp
7015 || storage_class == csc_auto
7016 || storage_class == csc_register
7017 || storage_class == csc_typedef))
7018 {
7019 if (storage_class == csc_auto)
7020 pedwarn (loc,
7021 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7022 "function definition declared %<auto%>");
7023 if (storage_class == csc_register)
7024 error_at (loc, "function definition declared %<register%>");
7025 if (storage_class == csc_typedef)
7026 error_at (loc, "function definition declared %<typedef%>");
7027 if (threadp)
7028 error_at (loc, "function definition declared %qs",
7029 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7030 threadp = false;
7031 /* The parser ensures a constexpr function definition never
7032 reaches here. */
7033 gcc_assert (!constexprp);
7034 if (storage_class == csc_auto
7035 || storage_class == csc_register
7036 || storage_class == csc_typedef)
7037 storage_class = csc_none;
7038 }
7039 else if (decl_context != NORMAL && (storage_class != csc_none
7040 || threadp
7041 || constexprp
7042 || declspecs->c23_auto_p))
7043 {
7044 if (decl_context == PARM
7045 && storage_class == csc_register
7046 && !constexprp
7047 && !declspecs->c23_auto_p)
7048 ;
7049 else
7050 {
7051 switch (decl_context)
7052 {
7053 case FIELD:
7054 if (name)
7055 error_at (loc, "storage class specified for structure "
7056 "field %qE", name);
7057 else
7058 error_at (loc, "storage class specified for structure field");
7059 break;
7060 case PARM:
7061 if (name)
7062 error_at (loc, "storage class specified for parameter %qE",
7063 name);
7064 else
7065 error_at (loc, "storage class specified for unnamed parameter");
7066 break;
7067 default:
7068 error_at (loc, "storage class specified for typename");
7069 break;
7070 }
7071 storage_class = csc_none;
7072 threadp = false;
7073 constexprp = false;
7074 }
7075 }
7076 else if (storage_class == csc_extern
7077 && initialized
7078 && !funcdef_flag)
7079 {
7080 /* 'extern' with initialization is invalid if not at file scope. */
7081 if (current_scope == file_scope)
7082 {
7083 /* It is fine to have 'extern const' when compiling at C
7084 and C++ intersection. */
7085 if (!(warn_cxx_compat && constp))
7086 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7087 name);
7088 }
7089 else
7090 error_at (loc, "%qE has both %<extern%> and initializer", name);
7091 }
7092 else if (current_scope == file_scope)
7093 {
7094 if (storage_class == csc_auto)
7095 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7096 name);
7097 if (pedantic && storage_class == csc_register)
7098 pedwarn (input_location, OPT_Wpedantic,
7099 "file-scope declaration of %qE specifies %<register%>", name);
7100 }
7101 else
7102 {
7103 if (storage_class == csc_extern && funcdef_flag)
7104 error_at (loc, "nested function %qE declared %<extern%>", name);
7105 else if (threadp && storage_class == csc_none)
7106 {
7107 error_at (loc, "function-scope %qE implicitly auto and declared "
7108 "%qs", name,
7109 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7110 threadp = false;
7111 }
7112 }
7113
7114 /* Now figure out the structure of the declarator proper.
7115 Descend through it, creating more complex types, until we reach
7116 the declared identifier (or NULL_TREE, in an absolute declarator).
7117 At each stage we maintain an unqualified version of the type
7118 together with any qualifiers that should be applied to it with
7119 c_build_qualified_type; this way, array types including
7120 multidimensional array types are first built up in unqualified
7121 form and then the qualified form is created with
7122 TYPE_MAIN_VARIANT pointing to the unqualified form. */
7123
7124 while (declarator && declarator->kind != cdk_id)
7125 {
7126 if (type == error_mark_node)
7127 {
7128 declarator = declarator->declarator;
7129 continue;
7130 }
7131
7132 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7133 a cdk_pointer (for *...),
7134 a cdk_function (for ...(...)),
7135 a cdk_attrs (for nested attributes),
7136 or a cdk_id (for the name being declared
7137 or the place in an absolute declarator
7138 where the name was omitted).
7139 For the last case, we have just exited the loop.
7140
7141 At this point, TYPE is the type of elements of an array,
7142 or for a function to return, or for a pointer to point to.
7143 After this sequence of ifs, TYPE is the type of the
7144 array or function or pointer, and DECLARATOR has had its
7145 outermost layer removed. */
7146
7147 if (array_ptr_quals != TYPE_UNQUALIFIED
7148 || array_ptr_attrs != NULL_TREE
7149 || array_parm_static)
7150 {
7151 /* Only the innermost declarator (making a parameter be of
7152 array type which is converted to pointer type)
7153 may have static or type qualifiers. */
7154 error_at (loc, "static or type qualifiers in non-parameter array declarator");
7155 array_ptr_quals = TYPE_UNQUALIFIED;
7156 array_ptr_attrs = NULL_TREE;
7157 array_parm_static = false;
7158 }
7159
7160 bool varmod = C_TYPE_VARIABLY_MODIFIED (type);
7161
7162 switch (declarator->kind)
7163 {
7164 case cdk_attrs:
7165 {
7166 /* A declarator with embedded attributes. */
7167 tree attrs = declarator->u.attrs;
7168 const struct c_declarator *inner_decl;
7169 int attr_flags = 0;
7170 declarator = declarator->declarator;
7171 /* Standard attribute syntax precisely defines what entity
7172 an attribute in each position appertains to, so only
7173 apply laxity about positioning to GNU attribute syntax.
7174 Standard attributes applied to a function or array
7175 declarator apply exactly to that type; standard
7176 attributes applied to the identifier apply to the
7177 declaration rather than to the type, and are specified
7178 using a cdk_id declarator rather than using
7179 cdk_attrs. */
7180 inner_decl = declarator;
7181 while (inner_decl->kind == cdk_attrs)
7182 inner_decl = inner_decl->declarator;
7183 if (!cxx11_attribute_p (attrs))
7184 {
7185 if (inner_decl->kind == cdk_id)
7186 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7187 else if (inner_decl->kind == cdk_function)
7188 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7189 else if (inner_decl->kind == cdk_array)
7190 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7191 }
7192 attrs = c_warn_type_attributes (attrs);
7193 returned_attrs = decl_attributes (&type,
7194 chainon (returned_attrs, attrs),
7195 attr_flags);
7196 break;
7197 }
7198 case cdk_array:
7199 {
7200 tree itype = NULL_TREE;
7201 tree size = declarator->u.array.dimen;
7202 /* The index is a signed object `sizetype' bits wide. */
7203 tree index_type = c_common_signed_type (sizetype);
7204
7205 array_ptr_quals = declarator->u.array.quals;
7206 array_ptr_attrs = declarator->u.array.attrs;
7207 array_parm_static = declarator->u.array.static_p;
7208 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7209
7210 declarator = declarator->declarator;
7211
7212 /* Check for some types that there cannot be arrays of. */
7213
7214 if (VOID_TYPE_P (type))
7215 {
7216 if (name)
7217 error_at (loc, "declaration of %qE as array of voids", name);
7218 else
7219 error_at (loc, "declaration of type name as array of voids");
7220 type = error_mark_node;
7221 }
7222
7223 if (TREE_CODE (type) == FUNCTION_TYPE)
7224 {
7225 if (name)
7226 error_at (loc, "declaration of %qE as array of functions",
7227 name);
7228 else
7229 error_at (loc, "declaration of type name as array of "
7230 "functions");
7231 type = error_mark_node;
7232 }
7233
7234 if (pedantic && !in_system_header_at (loc: input_location)
7235 && flexible_array_type_p (type))
7236 pedwarn (loc, OPT_Wpedantic,
7237 "invalid use of structure with flexible array member");
7238
7239 if (size == error_mark_node)
7240 type = error_mark_node;
7241
7242 if (type == error_mark_node)
7243 continue;
7244
7245 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7246 {
7247 type = error_mark_node;
7248 continue;
7249 }
7250
7251 /* If size was specified, set ITYPE to a range-type for
7252 that size. Otherwise, ITYPE remains null. finish_decl
7253 may figure it out from an initial value. */
7254
7255 if (size)
7256 {
7257 bool size_maybe_const = true;
7258 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7259 && !TREE_OVERFLOW (size));
7260 bool this_size_varies = false;
7261
7262 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7263 lvalue. */
7264 STRIP_TYPE_NOPS (size);
7265
7266 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7267 {
7268 if (name)
7269 error_at (loc, "size of array %qE has non-integer type",
7270 name);
7271 else
7272 error_at (loc,
7273 "size of unnamed array has non-integer type");
7274 size = integer_one_node;
7275 size_int_const = true;
7276 }
7277 /* This can happen with enum forward declaration. */
7278 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7279 {
7280 if (name)
7281 error_at (loc, "size of array %qE has incomplete type",
7282 name);
7283 else
7284 error_at (loc, "size of unnamed array has incomplete "
7285 "type");
7286 size = integer_one_node;
7287 size_int_const = true;
7288 }
7289
7290 size = c_fully_fold (size, false, &size_maybe_const);
7291
7292 if (pedantic && size_maybe_const && integer_zerop (size))
7293 {
7294 if (name)
7295 pedwarn (loc, OPT_Wpedantic,
7296 "ISO C forbids zero-size array %qE", name);
7297 else
7298 pedwarn (loc, OPT_Wpedantic,
7299 "ISO C forbids zero-size array");
7300 }
7301
7302 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7303 {
7304 constant_expression_warning (size);
7305 if (tree_int_cst_sgn (size) < 0)
7306 {
7307 if (name)
7308 error_at (loc, "size of array %qE is negative", name);
7309 else
7310 error_at (loc, "size of unnamed array is negative");
7311 size = integer_one_node;
7312 size_int_const = true;
7313 }
7314 /* Handle a size folded to an integer constant but
7315 not an integer constant expression. */
7316 if (!size_int_const)
7317 {
7318 /* If this is a file scope declaration of an
7319 ordinary identifier, this is invalid code;
7320 diagnosing it here and not subsequently
7321 treating the type as variable-length avoids
7322 more confusing diagnostics later. */
7323 if ((decl_context == NORMAL || decl_context == FIELD)
7324 && current_scope == file_scope)
7325 pedwarn (input_location, 0,
7326 "variably modified %qE at file scope",
7327 name);
7328 else
7329 this_size_varies = size_varies = true;
7330 warn_variable_length_array (name, size);
7331 }
7332 }
7333 else if ((decl_context == NORMAL || decl_context == FIELD)
7334 && current_scope == file_scope)
7335 {
7336 error_at (loc, "variably modified %qE at file scope", name);
7337 size = integer_one_node;
7338 }
7339 else
7340 {
7341 /* Make sure the array size remains visibly
7342 nonconstant even if it is (eg) a const variable
7343 with known value. */
7344 this_size_varies = size_varies = true;
7345 warn_variable_length_array (name, size);
7346 if (sanitize_flags_p (flag: SANITIZE_VLA)
7347 && current_function_decl != NULL_TREE
7348 && decl_context == NORMAL)
7349 {
7350 /* Evaluate the array size only once. */
7351 size = save_expr (size);
7352 size = c_fully_fold (size, false, NULL);
7353 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7354 ubsan_instrument_vla (loc, size),
7355 size);
7356 }
7357 }
7358
7359 if (integer_zerop (size) && !this_size_varies)
7360 {
7361 /* A zero-length array cannot be represented with
7362 an unsigned index type, which is what we'll
7363 get with build_index_type. Create an
7364 open-ended range instead. */
7365 itype = build_range_type (sizetype, size, NULL_TREE);
7366 }
7367 else
7368 {
7369 /* Arrange for the SAVE_EXPR on the inside of the
7370 MINUS_EXPR, which allows the -1 to get folded
7371 with the +1 that happens when building TYPE_SIZE. */
7372 if (size_varies)
7373 size = save_expr (size);
7374 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7375 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7376 integer_zero_node, size);
7377
7378 /* Compute the maximum valid index, that is, size
7379 - 1. Do the calculation in index_type, so that
7380 if it is a variable the computations will be
7381 done in the proper mode. */
7382 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7383 convert (index_type, size),
7384 convert (index_type,
7385 size_one_node));
7386
7387 /* The above overflows when size does not fit
7388 in index_type.
7389 ??? While a size of INT_MAX+1 technically shouldn't
7390 cause an overflow (because we subtract 1), handling
7391 this case seems like an unnecessary complication. */
7392 if (TREE_CODE (size) == INTEGER_CST
7393 && !int_fits_type_p (size, index_type))
7394 {
7395 if (name)
7396 error_at (loc, "size of array %qE is too large",
7397 name);
7398 else
7399 error_at (loc, "size of unnamed array is too large");
7400 type = error_mark_node;
7401 continue;
7402 }
7403
7404 itype = build_index_type (itype);
7405 }
7406 if (this_size_varies)
7407 {
7408 if (TREE_SIDE_EFFECTS (size))
7409 {
7410 if (*expr)
7411 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7412 *expr, size);
7413 else
7414 *expr = size;
7415 }
7416 *expr_const_operands &= size_maybe_const;
7417 }
7418 }
7419 else if (decl_context == FIELD)
7420 {
7421 bool flexible_array_member = false;
7422 if (array_parm_vla_unspec_p)
7423 /* Field names can in fact have function prototype
7424 scope so [*] is disallowed here through making
7425 the field variably modified, not through being
7426 something other than a declaration with function
7427 prototype scope. */
7428 size_varies = true;
7429 else
7430 {
7431 const struct c_declarator *t = declarator;
7432 while (t->kind == cdk_attrs)
7433 t = t->declarator;
7434 flexible_array_member = (t->kind == cdk_id);
7435 }
7436 if (flexible_array_member
7437 && !in_system_header_at (loc: input_location))
7438 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not "
7439 "support flexible array members");
7440
7441 /* ISO C99 Flexible array members are effectively
7442 identical to GCC's zero-length array extension. */
7443 if (flexible_array_member || array_parm_vla_unspec_p)
7444 itype = build_range_type (sizetype, size_zero_node,
7445 NULL_TREE);
7446 }
7447 else if (decl_context == PARM)
7448 {
7449 if (array_parm_vla_unspec_p)
7450 {
7451 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7452 size_varies = true;
7453 }
7454 }
7455 else if (decl_context == TYPENAME)
7456 {
7457 if (array_parm_vla_unspec_p)
7458 {
7459 /* C99 6.7.5.2p4 */
7460 warning (0, "%<[*]%> not in a declaration");
7461 /* We use this to avoid messing up with incomplete
7462 array types of the same type, that would
7463 otherwise be modified below. */
7464 itype = build_range_type (sizetype, size_zero_node,
7465 NULL_TREE);
7466 size_varies = true;
7467 }
7468 }
7469
7470 /* Complain about arrays of incomplete types. */
7471 if (!COMPLETE_TYPE_P (type))
7472 {
7473 auto_diagnostic_group d;
7474 error_at (loc, "array type has incomplete element type %qT",
7475 type);
7476 /* See if we can be more helpful. */
7477 if (TREE_CODE (type) == ARRAY_TYPE)
7478 {
7479 if (name)
7480 inform (loc, "declaration of %qE as multidimensional "
7481 "array must have bounds for all dimensions "
7482 "except the first", name);
7483 else
7484 inform (loc, "declaration of multidimensional array "
7485 "must have bounds for all dimensions except "
7486 "the first");
7487 }
7488 type = error_mark_node;
7489 }
7490 else
7491 /* When itype is NULL, a shared incomplete array type is
7492 returned for all array of a given type. Elsewhere we
7493 make sure we don't complete that type before copying
7494 it, but here we want to make sure we don't ever
7495 modify the shared type, so we gcc_assert (itype)
7496 below. */
7497 {
7498 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7499 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7500 type = build_qualified_type (type,
7501 ENCODE_QUAL_ADDR_SPACE (as));
7502
7503 type = build_array_type (type, itype);
7504 }
7505
7506 if (type != error_mark_node)
7507 {
7508 if (size_varies)
7509 {
7510 /* It is ok to modify type here even if itype is
7511 NULL: if size_varies, we're in a
7512 multi-dimensional array and the inner type has
7513 variable size, so the enclosing shared array type
7514 must too. */
7515 if (size && TREE_CODE (size) == INTEGER_CST)
7516 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7517 C_TYPE_VARIABLE_SIZE (type) = 1;
7518 }
7519
7520 /* The GCC extension for zero-length arrays differs from
7521 ISO flexible array members in that sizeof yields
7522 zero. */
7523 if (size && integer_zerop (size))
7524 {
7525 gcc_assert (itype);
7526 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7527 TYPE_SIZE (type) = bitsize_zero_node;
7528 TYPE_SIZE_UNIT (type) = size_zero_node;
7529 SET_TYPE_STRUCTURAL_EQUALITY (type);
7530 }
7531 if (array_parm_vla_unspec_p)
7532 {
7533 gcc_assert (itype);
7534 /* The type is complete. C99 6.7.5.2p4 */
7535 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7536 TYPE_SIZE (type) = bitsize_zero_node;
7537 TYPE_SIZE_UNIT (type) = size_zero_node;
7538 SET_TYPE_STRUCTURAL_EQUALITY (type);
7539 }
7540
7541 if (!valid_array_size_p (loc, type, name))
7542 type = error_mark_node;
7543 }
7544
7545 if (decl_context != PARM
7546 && (array_ptr_quals != TYPE_UNQUALIFIED
7547 || array_ptr_attrs != NULL_TREE
7548 || array_parm_static))
7549 {
7550 error_at (loc, "static or type qualifiers in non-parameter "
7551 "array declarator");
7552 array_ptr_quals = TYPE_UNQUALIFIED;
7553 array_ptr_attrs = NULL_TREE;
7554 array_parm_static = false;
7555 }
7556 orig_qual_indirect++;
7557 break;
7558 }
7559 case cdk_function:
7560 {
7561 /* Say it's a definition only for the declarator closest
7562 to the identifier, apart possibly from some
7563 attributes. */
7564 bool really_funcdef = false;
7565 tree arg_types;
7566 orig_qual_type = NULL_TREE;
7567 if (funcdef_flag)
7568 {
7569 const struct c_declarator *t = declarator->declarator;
7570 while (t->kind == cdk_attrs)
7571 t = t->declarator;
7572 really_funcdef = (t->kind == cdk_id);
7573 }
7574
7575 /* Declaring a function type. Make sure we have a valid
7576 type for the function to return. */
7577 if (type == error_mark_node)
7578 continue;
7579
7580 size_varies = false;
7581
7582 /* Warn about some types functions can't return. */
7583 if (TREE_CODE (type) == FUNCTION_TYPE)
7584 {
7585 if (name)
7586 error_at (loc, "%qE declared as function returning a "
7587 "function", name);
7588 else
7589 error_at (loc, "type name declared as function "
7590 "returning a function");
7591 type = integer_type_node;
7592 }
7593 if (TREE_CODE (type) == ARRAY_TYPE)
7594 {
7595 if (name)
7596 error_at (loc, "%qE declared as function returning an array",
7597 name);
7598 else
7599 error_at (loc, "type name declared as function returning "
7600 "an array");
7601 type = integer_type_node;
7602 }
7603
7604 /* Construct the function type and go to the next
7605 inner layer of declarator. */
7606 arg_info = declarator->u.arg_info;
7607 arg_types = grokparms (arg_info, really_funcdef);
7608
7609 /* Type qualifiers before the return type of the function
7610 qualify the return type, not the function type. */
7611 if (type_quals)
7612 {
7613 const enum c_declspec_word ignored_quals_list[] =
7614 {
7615 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7616 cdw_atomic, cdw_number_of_elements
7617 };
7618 location_t specs_loc
7619 = smallest_type_quals_location (locations: declspecs->locations,
7620 list: ignored_quals_list);
7621 if (specs_loc == UNKNOWN_LOCATION)
7622 specs_loc = declspecs->locations[cdw_typedef];
7623 if (specs_loc == UNKNOWN_LOCATION)
7624 specs_loc = loc;
7625
7626 /* Type qualifiers on a function return type are
7627 normally permitted by the standard but have no
7628 effect, so give a warning at -Wreturn-type.
7629 Qualifiers on a void return type are banned on
7630 function definitions in ISO C; GCC used to used
7631 them for noreturn functions. The resolution of C11
7632 DR#423 means qualifiers (other than _Atomic) are
7633 actually removed from the return type when
7634 determining the function type. For C23, _Atomic is
7635 removed as well. */
7636 int quals_used = type_quals;
7637 if (flag_isoc23)
7638 quals_used = 0;
7639 else if (flag_isoc11)
7640 quals_used &= TYPE_QUAL_ATOMIC;
7641 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7642 pedwarn (specs_loc, 0,
7643 "function definition has qualified void "
7644 "return type");
7645 else
7646 warning_at (specs_loc, OPT_Wignored_qualifiers,
7647 "type qualifiers ignored on function "
7648 "return type");
7649
7650 /* Ensure an error for restrict on invalid types; the
7651 DR#423 resolution is not entirely clear about
7652 this. */
7653 if (flag_isoc11
7654 && (type_quals & TYPE_QUAL_RESTRICT)
7655 && (!POINTER_TYPE_P (type)
7656 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7657 error_at (loc, "invalid use of %<restrict%>");
7658 type = c_build_qualified_type (type, quals_used);
7659 }
7660 type_quals = TYPE_UNQUALIFIED;
7661
7662 type = build_function_type (type, arg_types,
7663 arg_info->no_named_args_stdarg_p);
7664 declarator = declarator->declarator;
7665
7666 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7667 the formal parameter list of this FUNCTION_TYPE to point to
7668 the FUNCTION_TYPE node itself. */
7669 {
7670 c_arg_tag *tag;
7671 unsigned ix;
7672
7673 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7674 TYPE_CONTEXT (tag->type) = type;
7675 }
7676 break;
7677 }
7678 case cdk_pointer:
7679 {
7680 /* Merge any constancy or volatility into the target type
7681 for the pointer. */
7682 if ((type_quals & TYPE_QUAL_ATOMIC)
7683 && TREE_CODE (type) == FUNCTION_TYPE)
7684 {
7685 error_at (loc,
7686 "%<_Atomic%>-qualified function type");
7687 type_quals &= ~TYPE_QUAL_ATOMIC;
7688 }
7689 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7690 && type_quals)
7691 pedwarn (loc, OPT_Wpedantic,
7692 "ISO C forbids qualified function types");
7693 if (type_quals)
7694 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7695 orig_qual_indirect);
7696 orig_qual_type = NULL_TREE;
7697 size_varies = false;
7698
7699 /* When the pointed-to type involves components of variable size,
7700 care must be taken to ensure that the size evaluation code is
7701 emitted early enough to dominate all the possible later uses
7702 and late enough for the variables on which it depends to have
7703 been assigned.
7704
7705 This is expected to happen automatically when the pointed-to
7706 type has a name/declaration of it's own, but special attention
7707 is required if the type is anonymous. */
7708 if (!TYPE_NAME (type) && c_type_variably_modified_p (t: type))
7709 {
7710 bool bind_p = decl_context == TYPENAME
7711 || decl_context == FIELD
7712 || decl_context == PARM;
7713 add_decl_expr (loc, type, expr: bind_p ? expr : NULL, set_name_p: true);
7714 }
7715
7716 type = c_build_pointer_type (to_type: type);
7717
7718 /* Process type qualifiers (such as const or volatile)
7719 that were given inside the `*'. */
7720 type_quals = declarator->u.pointer_quals;
7721
7722 declarator = declarator->declarator;
7723 break;
7724 }
7725 default:
7726 gcc_unreachable ();
7727 }
7728 if (type != error_mark_node)
7729 C_TYPE_VARIABLY_MODIFIED (type) = varmod || size_varies;
7730 }
7731 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7732 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7733
7734 /* Now TYPE has the actual type, apart from any qualifiers in
7735 TYPE_QUALS. */
7736
7737 /* Warn about address space used for things other than static memory or
7738 pointers. */
7739 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7740 if (!ADDR_SPACE_GENERIC_P (address_space))
7741 {
7742 if (decl_context == NORMAL)
7743 {
7744 switch (storage_class)
7745 {
7746 case csc_auto:
7747 error ("%qs combined with %<auto%> qualifier for %qE",
7748 c_addr_space_name (as: address_space), name);
7749 break;
7750 case csc_register:
7751 error ("%qs combined with %<register%> qualifier for %qE",
7752 c_addr_space_name (as: address_space), name);
7753 break;
7754 case csc_none:
7755 if (current_function_scope)
7756 {
7757 error ("%qs specified for auto variable %qE",
7758 c_addr_space_name (as: address_space), name);
7759 break;
7760 }
7761 break;
7762 case csc_static:
7763 case csc_extern:
7764 case csc_typedef:
7765 break;
7766 default:
7767 gcc_unreachable ();
7768 }
7769 }
7770 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7771 {
7772 if (name)
7773 error ("%qs specified for parameter %qE",
7774 c_addr_space_name (as: address_space), name);
7775 else
7776 error ("%qs specified for unnamed parameter",
7777 c_addr_space_name (as: address_space));
7778 }
7779 else if (decl_context == FIELD)
7780 {
7781 if (name)
7782 error ("%qs specified for structure field %qE",
7783 c_addr_space_name (as: address_space), name);
7784 else
7785 error ("%qs specified for structure field",
7786 c_addr_space_name (as: address_space));
7787 }
7788 }
7789
7790 /* Check the type and width of a bit-field. */
7791 if (bitfield)
7792 {
7793 check_bitfield_type_and_width (loc, type: &type, width, orig_name: name);
7794 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7795 atomic types are permitted for bit-fields; we have no code to
7796 make bit-field accesses atomic, so disallow them. */
7797 if (type_quals & TYPE_QUAL_ATOMIC)
7798 {
7799 if (name)
7800 error_at (loc, "bit-field %qE has atomic type", name);
7801 else
7802 error_at (loc, "bit-field has atomic type");
7803 type_quals &= ~TYPE_QUAL_ATOMIC;
7804 }
7805 }
7806
7807 /* Reject invalid uses of _Alignas. */
7808 if (declspecs->alignas_p)
7809 {
7810 if (storage_class == csc_typedef)
7811 error_at (loc, "alignment specified for typedef %qE", name);
7812 else if (storage_class == csc_register)
7813 error_at (loc, "alignment specified for %<register%> object %qE",
7814 name);
7815 else if (decl_context == PARM)
7816 {
7817 if (name)
7818 error_at (loc, "alignment specified for parameter %qE", name);
7819 else
7820 error_at (loc, "alignment specified for unnamed parameter");
7821 }
7822 else if (bitfield)
7823 {
7824 if (name)
7825 error_at (loc, "alignment specified for bit-field %qE", name);
7826 else
7827 error_at (loc, "alignment specified for unnamed bit-field");
7828 }
7829 else if (TREE_CODE (type) == FUNCTION_TYPE)
7830 error_at (loc, "alignment specified for function %qE", name);
7831 else if (declspecs->align_log != -1 && TYPE_P (type))
7832 {
7833 alignas_align = 1U << declspecs->align_log;
7834 if (alignas_align < min_align_of_type (type))
7835 {
7836 if (name)
7837 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7838 "alignment of %qE", name);
7839 else
7840 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7841 "alignment of unnamed field");
7842 alignas_align = 0;
7843 }
7844 }
7845 }
7846
7847 /* If this is declaring a typedef name, return a TYPE_DECL. */
7848
7849 if (storage_class == csc_typedef)
7850 {
7851 tree decl;
7852 if ((type_quals & TYPE_QUAL_ATOMIC)
7853 && TREE_CODE (type) == FUNCTION_TYPE)
7854 {
7855 error_at (loc,
7856 "%<_Atomic%>-qualified function type");
7857 type_quals &= ~TYPE_QUAL_ATOMIC;
7858 }
7859 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7860 && type_quals)
7861 pedwarn (loc, OPT_Wpedantic,
7862 "ISO C forbids qualified function types");
7863 if (type_quals)
7864 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7865 orig_qual_indirect);
7866 decl = build_decl (declarator->id_loc,
7867 TYPE_DECL, declarator->u.id.id, type);
7868 if (declspecs->explicit_signed_p)
7869 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7870 if (declspecs->inline_p)
7871 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7872 if (declspecs->noreturn_p)
7873 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7874
7875 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7876 {
7877 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7878
7879 if (b != NULL
7880 && b->decl != NULL_TREE
7881 && (B_IN_CURRENT_SCOPE (b)
7882 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7883 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7884 {
7885 auto_diagnostic_group d;
7886 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7887 ("using %qD as both a typedef and a tag is "
7888 "invalid in C++"), decl)
7889 && b->locus != UNKNOWN_LOCATION)
7890 inform (b->locus, "originally defined here");
7891 }
7892 }
7893
7894 return decl;
7895 }
7896
7897 /* If this is a type name (such as, in a cast or sizeof),
7898 compute the type and return it now. */
7899
7900 if (decl_context == TYPENAME)
7901 {
7902 /* Note that the grammar rejects storage classes in typenames
7903 and fields. */
7904 gcc_assert (storage_class == csc_none && !threadp
7905 && !declspecs->inline_p && !declspecs->noreturn_p);
7906 if ((type_quals & TYPE_QUAL_ATOMIC)
7907 && TREE_CODE (type) == FUNCTION_TYPE)
7908 {
7909 error_at (loc,
7910 "%<_Atomic%>-qualified function type");
7911 type_quals &= ~TYPE_QUAL_ATOMIC;
7912 }
7913 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7914 && type_quals)
7915 pedwarn (loc, OPT_Wpedantic,
7916 "ISO C forbids const or volatile function types");
7917 if (type_quals)
7918 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7919 orig_qual_indirect);
7920 return type;
7921 }
7922
7923 if (pedantic && decl_context == FIELD
7924 && c_type_variably_modified_p (t: type))
7925 {
7926 /* C99 6.7.2.1p8 */
7927 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7928 "have a variably modified type");
7929 }
7930
7931 /* Aside from typedefs and type names (handle above),
7932 `void' at top level (not within pointer)
7933 is allowed only in public variables.
7934 We don't complain about parms either, but that is because
7935 a better error message can be made later. */
7936
7937 if (VOID_TYPE_P (type) && decl_context != PARM
7938 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7939 && (storage_class == csc_extern
7940 || (current_scope == file_scope
7941 && !(storage_class == csc_static
7942 || storage_class == csc_register)))))
7943 {
7944 error_at (loc, "variable or field %qE declared void", name);
7945 type = integer_type_node;
7946 }
7947
7948 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7949 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7950
7951 {
7952 tree decl;
7953
7954 if (decl_context == PARM)
7955 {
7956 tree promoted_type;
7957 bool array_parameter_p = false;
7958
7959 /* A parameter declared as an array of T is really a pointer to T.
7960 One declared as a function is really a pointer to a function. */
7961
7962 if (TREE_CODE (type) == ARRAY_TYPE)
7963 {
7964 /* Transfer const-ness of array into that of type pointed to. */
7965 type = TREE_TYPE (type);
7966 if (orig_qual_type != NULL_TREE)
7967 {
7968 if (orig_qual_indirect == 0)
7969 orig_qual_type = TREE_TYPE (orig_qual_type);
7970 else
7971 orig_qual_indirect--;
7972 }
7973 if (type_quals)
7974 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7975 orig_qual_indirect);
7976
7977 /* The pointed-to type may need a decl expr (see above). */
7978 if (!TYPE_NAME (type) && c_type_variably_modified_p (t: type))
7979 {
7980 bool bind_p = decl_context == TYPENAME
7981 || decl_context == FIELD
7982 || decl_context == PARM;
7983 add_decl_expr (loc, type, expr: bind_p ? expr : NULL, set_name_p: true);
7984 }
7985
7986 type = c_build_pointer_type (to_type: type);
7987 type_quals = array_ptr_quals;
7988 if (type_quals)
7989 type = c_build_qualified_type (type, type_quals);
7990
7991 /* We don't yet implement attributes in this context. */
7992 if (array_ptr_attrs != NULL_TREE)
7993 warning_at (loc, OPT_Wattributes,
7994 "attributes in parameter array declarator ignored");
7995
7996 size_varies = false;
7997 array_parameter_p = true;
7998 }
7999 else if (TREE_CODE (type) == FUNCTION_TYPE)
8000 {
8001 if (type_quals & TYPE_QUAL_ATOMIC)
8002 {
8003 error_at (loc,
8004 "%<_Atomic%>-qualified function type");
8005 type_quals &= ~TYPE_QUAL_ATOMIC;
8006 }
8007 else if (type_quals)
8008 pedwarn (loc, OPT_Wpedantic,
8009 "ISO C forbids qualified function types");
8010 if (type_quals)
8011 type = c_build_qualified_type (type, type_quals);
8012 type = c_build_pointer_type (to_type: type);
8013 type_quals = TYPE_UNQUALIFIED;
8014 }
8015 else if (type_quals)
8016 type = c_build_qualified_type (type, type_quals);
8017
8018 decl = build_decl (declarator->id_loc,
8019 PARM_DECL, declarator->u.id.id, type);
8020 if (size_varies)
8021 C_DECL_VARIABLE_SIZE (decl) = 1;
8022 C_ARRAY_PARAMETER (decl) = array_parameter_p;
8023
8024 /* Compute the type actually passed in the parmlist,
8025 for the case where there is no prototype.
8026 (For example, shorts and chars are passed as ints.)
8027 When there is a prototype, this is overridden later. */
8028
8029 if (type == error_mark_node)
8030 promoted_type = type;
8031 else
8032 promoted_type = c_type_promotes_to (type);
8033
8034 DECL_ARG_TYPE (decl) = promoted_type;
8035 if (declspecs->inline_p)
8036 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8037 if (declspecs->noreturn_p)
8038 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8039 }
8040 else if (decl_context == FIELD)
8041 {
8042 /* Note that the grammar rejects storage classes in typenames
8043 and fields. */
8044 gcc_assert (storage_class == csc_none && !threadp
8045 && !declspecs->inline_p && !declspecs->noreturn_p);
8046
8047 /* Structure field. It may not be a function. */
8048
8049 if (TREE_CODE (type) == FUNCTION_TYPE)
8050 {
8051 error_at (loc, "field %qE declared as a function", name);
8052 type = build_pointer_type (type);
8053 }
8054 else if (TREE_CODE (type) != ERROR_MARK
8055 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8056 {
8057 if (name)
8058 error_at (loc, "field %qE has incomplete type", name);
8059 else
8060 error_at (loc, "unnamed field has incomplete type");
8061 type = error_mark_node;
8062 }
8063 else if (TREE_CODE (type) == ARRAY_TYPE
8064 && TYPE_DOMAIN (type) == NULL_TREE)
8065 {
8066 /* We have a flexible array member through a typedef.
8067 Set suitable range. Whether this is a correct position
8068 for a flexible array member will be determined elsewhere. */
8069 if (!in_system_header_at (loc: input_location))
8070 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not "
8071 "support flexible array members");
8072 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8073 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8074 NULL_TREE);
8075 if (orig_qual_indirect == 0)
8076 orig_qual_type = NULL_TREE;
8077 }
8078 if (type != error_mark_node
8079 && !verify_type_context (loc, TCTX_FIELD, type))
8080 type = error_mark_node;
8081
8082 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8083 orig_qual_indirect);
8084 decl = build_decl (declarator->id_loc,
8085 FIELD_DECL, declarator->u.id.id, type);
8086 DECL_NONADDRESSABLE_P (decl) = bitfield;
8087 if (bitfield && !declarator->u.id.id)
8088 DECL_PADDING_P (decl) = 1;
8089
8090 if (size_varies)
8091 C_DECL_VARIABLE_SIZE (decl) = 1;
8092 }
8093 else if (TREE_CODE (type) == FUNCTION_TYPE)
8094 {
8095 if (storage_class == csc_register || threadp || constexprp)
8096 {
8097 error_at (loc, "invalid storage class for function %qE", name);
8098 }
8099 else if (current_scope != file_scope)
8100 {
8101 /* Function declaration not at file scope. Storage
8102 classes other than `extern' are not allowed, C99
8103 6.7.1p5, and `extern' makes no difference. However,
8104 GCC allows 'auto', perhaps with 'inline', to support
8105 nested functions. */
8106 if (storage_class == csc_auto)
8107 pedwarn (loc, OPT_Wpedantic,
8108 "invalid storage class for function %qE", name);
8109 else if (storage_class == csc_static)
8110 {
8111 error_at (loc, "invalid storage class for function %qE", name);
8112 if (funcdef_flag)
8113 storage_class = declspecs->storage_class = csc_none;
8114 else
8115 return NULL_TREE;
8116 }
8117 }
8118
8119 decl = build_decl (declarator->id_loc,
8120 FUNCTION_DECL, declarator->u.id.id, type);
8121 decl = build_decl_attribute_variant (decl, decl_attr);
8122
8123 if (type_quals & TYPE_QUAL_ATOMIC)
8124 {
8125 error_at (loc,
8126 "%<_Atomic%>-qualified function type");
8127 type_quals &= ~TYPE_QUAL_ATOMIC;
8128 }
8129 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8130 pedwarn (loc, OPT_Wpedantic,
8131 "ISO C forbids qualified function types");
8132
8133 /* Every function declaration is an external reference
8134 (DECL_EXTERNAL) except for those which are not at file
8135 scope and are explicitly declared "auto". This is
8136 forbidden by standard C (C99 6.7.1p5) and is interpreted by
8137 GCC to signify a forward declaration of a nested function. */
8138 if (storage_class == csc_auto && current_scope != file_scope)
8139 DECL_EXTERNAL (decl) = 0;
8140 /* In C99, a function which is declared 'inline' with 'extern'
8141 is not an external reference (which is confusing). It
8142 means that the later definition of the function must be output
8143 in this file, C99 6.7.4p6. In GNU C89, a function declared
8144 'extern inline' is an external reference. */
8145 else if (declspecs->inline_p && storage_class != csc_static)
8146 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8147 == flag_gnu89_inline);
8148 else
8149 DECL_EXTERNAL (decl) = !initialized;
8150
8151 /* Record absence of global scope for `static' or `auto'. */
8152 TREE_PUBLIC (decl)
8153 = !(storage_class == csc_static || storage_class == csc_auto);
8154
8155 /* For a function definition, record the argument information
8156 block where store_parm_decls will look for it. */
8157 if (funcdef_flag)
8158 current_function_arg_info = arg_info;
8159
8160 if (declspecs->default_int_p)
8161 C_FUNCTION_IMPLICIT_INT (decl) = 1;
8162
8163 /* Record presence of `inline' and `_Noreturn', if it is
8164 reasonable. */
8165 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8166 {
8167 if (declspecs->inline_p)
8168 pedwarn (loc, 0, "cannot inline function %<main%>");
8169 if (declspecs->noreturn_p)
8170 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8171 }
8172 else
8173 {
8174 if (declspecs->inline_p)
8175 /* Record that the function is declared `inline'. */
8176 DECL_DECLARED_INLINE_P (decl) = 1;
8177 if (declspecs->noreturn_p)
8178 {
8179 if (flag_isoc99)
8180 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8181 "ISO C99 does not support %<_Noreturn%>");
8182 else
8183 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8184 "ISO C90 does not support %<_Noreturn%>");
8185 TREE_THIS_VOLATILE (decl) = 1;
8186 }
8187 }
8188
8189 /* C99 6.2.2p7: It is invalid (compile-time undefined
8190 behavior) to create an 'extern' declaration for a
8191 function if there is a global declaration that is
8192 'static' and the global declaration is not visible.
8193 (If the static declaration _is_ currently visible,
8194 the 'extern' declaration is taken to refer to that decl.) */
8195 if (!initialized
8196 && TREE_PUBLIC (decl)
8197 && current_scope != file_scope)
8198 {
8199 tree global_decl = identifier_global_value (declarator->u.id.id);
8200 tree visible_decl = lookup_name (name: declarator->u.id.id);
8201
8202 if (global_decl
8203 && global_decl != visible_decl
8204 && VAR_OR_FUNCTION_DECL_P (global_decl)
8205 && !TREE_PUBLIC (global_decl))
8206 error_at (loc, "function previously declared %<static%> "
8207 "redeclared %<extern%>");
8208 }
8209 }
8210 else
8211 {
8212 /* It's a variable. */
8213 /* An uninitialized decl with `extern' is a reference. */
8214 int extern_ref = !initialized && storage_class == csc_extern;
8215
8216 if (constexprp)
8217 {
8218 /* The type of a constexpr variable must not be variably
8219 modified, volatile, atomic or restrict qualified or
8220 have a member with such a qualifier. const
8221 qualification is implicitly added, and, at file scope,
8222 has internal linkage. */
8223 if (c_type_variably_modified_p (t: type))
8224 error_at (loc, "%<constexpr%> object has variably modified "
8225 "type");
8226 if (type_quals
8227 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8228 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8229 else
8230 {
8231 tree type_no_array = strip_array_types (type);
8232 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8233 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8234 error_at (loc, "invalid qualifiers for field of "
8235 "%<constexpr%> object");
8236 }
8237 type_quals |= TYPE_QUAL_CONST;
8238 if (current_scope == file_scope)
8239 storage_class = csc_static;
8240 }
8241
8242 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8243 orig_qual_indirect);
8244
8245 /* C99 6.2.2p7: It is invalid (compile-time undefined
8246 behavior) to create an 'extern' declaration for a
8247 variable if there is a global declaration that is
8248 'static' and the global declaration is not visible.
8249 (If the static declaration _is_ currently visible,
8250 the 'extern' declaration is taken to refer to that decl.) */
8251 if (extern_ref && current_scope != file_scope)
8252 {
8253 tree global_decl = identifier_global_value (declarator->u.id.id);
8254 tree visible_decl = lookup_name (name: declarator->u.id.id);
8255
8256 if (global_decl
8257 && global_decl != visible_decl
8258 && VAR_P (global_decl)
8259 && !TREE_PUBLIC (global_decl))
8260 error_at (loc, "variable previously declared %<static%> "
8261 "redeclared %<extern%>");
8262 }
8263
8264 decl = build_decl (declarator->id_loc,
8265 VAR_DECL, declarator->u.id.id, type);
8266 if (size_varies)
8267 C_DECL_VARIABLE_SIZE (decl) = 1;
8268 if (constexprp)
8269 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8270
8271 if (declspecs->inline_p)
8272 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8273 if (declspecs->noreturn_p)
8274 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8275
8276 /* At file scope, an initialized extern declaration may follow
8277 a static declaration. In that case, DECL_EXTERNAL will be
8278 reset later in start_decl. */
8279 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8280
8281 /* At file scope, the presence of a `static' or `register' storage
8282 class specifier, or the absence of all storage class specifiers
8283 makes this declaration a definition (perhaps tentative). Also,
8284 the absence of `static' makes it public. */
8285 if (current_scope == file_scope)
8286 {
8287 TREE_PUBLIC (decl) = storage_class != csc_static;
8288 TREE_STATIC (decl) = !extern_ref;
8289 }
8290 /* Not at file scope, only `static' makes a static definition. */
8291 else
8292 {
8293 TREE_STATIC (decl) = (storage_class == csc_static);
8294 TREE_PUBLIC (decl) = extern_ref;
8295 }
8296
8297 if (threadp)
8298 set_decl_tls_model (decl, decl_default_tls_model (decl));
8299 }
8300
8301 if ((storage_class == csc_extern
8302 || (storage_class == csc_none
8303 && TREE_CODE (type) == FUNCTION_TYPE
8304 && !funcdef_flag))
8305 && c_type_variably_modified_p (t: type))
8306 {
8307 /* C99 6.7.5.2p2 */
8308 if (TREE_CODE (type) == FUNCTION_TYPE)
8309 error_at (loc, "non-nested function with variably modified type");
8310 else
8311 error_at (loc, "object with variably modified type must have "
8312 "no linkage");
8313 }
8314
8315 /* For nested functions disqualify ones taking VLAs by value
8316 from inlining since the middle-end cannot deal with this.
8317 ??? We should arrange for those to be passed by reference
8318 with emitting the copy on the caller side in the frontend. */
8319 if (storage_class == csc_none
8320 && TREE_CODE (type) == FUNCTION_TYPE)
8321 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8322 {
8323 tree arg = TREE_VALUE (al);
8324 if (arg != error_mark_node
8325 && C_TYPE_VARIABLE_SIZE (arg))
8326 {
8327 DECL_UNINLINABLE (decl) = 1;
8328 break;
8329 }
8330 }
8331
8332 /* Record `register' declaration for warnings on &
8333 and in case doing stupid register allocation. */
8334
8335 if (storage_class == csc_register)
8336 {
8337 C_DECL_REGISTER (decl) = 1;
8338 DECL_REGISTER (decl) = 1;
8339 }
8340
8341 /* Record constancy and volatility. */
8342 c_apply_type_quals_to_decl (type_quals, decl);
8343
8344 /* Apply _Alignas specifiers. */
8345 if (alignas_align)
8346 {
8347 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8348 DECL_USER_ALIGN (decl) = 1;
8349 }
8350
8351 /* If a type has volatile components, it should be stored in memory.
8352 Otherwise, the fact that those components are volatile
8353 will be ignored, and would even crash the compiler.
8354 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8355 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8356 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8357 || TREE_CODE (decl) == RESULT_DECL))
8358 {
8359 /* It is not an error for a structure with volatile fields to
8360 be declared register, but reset DECL_REGISTER since it
8361 cannot actually go in a register. */
8362 int was_reg = C_DECL_REGISTER (decl);
8363 C_DECL_REGISTER (decl) = 0;
8364 DECL_REGISTER (decl) = 0;
8365 c_mark_addressable (decl);
8366 C_DECL_REGISTER (decl) = was_reg;
8367 }
8368
8369 /* This is the earliest point at which we might know the assembler
8370 name of a variable. Thus, if it's known before this, die horribly. */
8371 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8372 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8373
8374 if (warn_cxx_compat
8375 && VAR_P (decl)
8376 && TREE_PUBLIC (decl)
8377 && TREE_STATIC (decl)
8378 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8379 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8380 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8381 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8382 ("non-local variable %qD with anonymous type is "
8383 "questionable in C++"),
8384 decl);
8385
8386 return decl;
8387 }
8388}
8389
8390/* Decode the parameter-list info for a function type or function definition.
8391 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8392 if there is an identifier list instead of a parameter decl list).
8393 These two functions are separate because when a function returns
8394 or receives functions then each is called multiple times but the order
8395 of calls is different. The last call to `grokparms' is always the one
8396 that contains the formal parameter names of a function definition.
8397
8398 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8399
8400 FUNCDEF_FLAG is true for a function definition, false for
8401 a mere declaration. A nonempty identifier-list gets an error message
8402 when FUNCDEF_FLAG is false. */
8403
8404static tree
8405grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8406{
8407 tree arg_types = arg_info->types;
8408
8409 if (funcdef_flag && arg_info->had_vla_unspec)
8410 {
8411 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8412 /* C99 6.7.5.2p4 */
8413 error ("%<[*]%> not allowed in other than function prototype scope");
8414 }
8415
8416 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8417 && !in_system_header_at (loc: input_location))
8418 warning (OPT_Wstrict_prototypes,
8419 "function declaration isn%'t a prototype");
8420
8421 if (arg_types == error_mark_node)
8422 /* Don't set TYPE_ARG_TYPES in this case. */
8423 return NULL_TREE;
8424
8425 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8426 {
8427 if (!funcdef_flag)
8428 {
8429 permerror_opt (input_location,
8430 OPT_Wdeclaration_missing_parameter_type,
8431 "parameter names (without types) in "
8432 "function declaration");
8433 arg_info->parms = NULL_TREE;
8434 }
8435 else
8436 arg_info->parms = arg_info->types;
8437
8438 arg_info->types = NULL_TREE;
8439 return NULL_TREE;
8440 }
8441 else
8442 {
8443 tree parm, type, typelt;
8444 unsigned int parmno;
8445
8446 /* In C23, convert () to (void). */
8447 if (flag_isoc23
8448 && !arg_types
8449 && !arg_info->parms
8450 && !arg_info->no_named_args_stdarg_p)
8451 arg_types = arg_info->types = void_list_node;
8452
8453 /* If there is a parameter of incomplete type in a definition,
8454 this is an error. In a declaration this is valid, and a
8455 struct or union type may be completed later, before any calls
8456 or definition of the function. In the case where the tag was
8457 first declared within the parameter list, a warning has
8458 already been given. If a parameter has void type, then
8459 however the function cannot be defined or called, so
8460 warn. */
8461
8462 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8463 parm;
8464 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8465 {
8466 type = TREE_VALUE (typelt);
8467 if (type == error_mark_node)
8468 continue;
8469
8470 if (!COMPLETE_TYPE_P (type))
8471 {
8472 if (funcdef_flag)
8473 {
8474 if (DECL_NAME (parm))
8475 error_at (input_location,
8476 "parameter %u (%q+D) has incomplete type",
8477 parmno, parm);
8478 else
8479 error_at (DECL_SOURCE_LOCATION (parm),
8480 "parameter %u has incomplete type",
8481 parmno);
8482
8483 TREE_VALUE (typelt) = error_mark_node;
8484 TREE_TYPE (parm) = error_mark_node;
8485 arg_types = NULL_TREE;
8486 }
8487 else if (VOID_TYPE_P (type))
8488 {
8489 if (DECL_NAME (parm))
8490 warning_at (input_location, 0,
8491 "parameter %u (%q+D) has void type",
8492 parmno, parm);
8493 else
8494 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8495 "parameter %u has void type",
8496 parmno);
8497 }
8498 }
8499
8500 if (DECL_NAME (parm) && TREE_USED (parm))
8501 warn_if_shadowing (new_decl: parm);
8502 }
8503 return arg_types;
8504 }
8505}
8506
8507/* Allocate and initialize a c_arg_info structure from the parser's
8508 obstack. */
8509
8510struct c_arg_info *
8511build_arg_info (void)
8512{
8513 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8514 ret->parms = NULL_TREE;
8515 ret->tags = NULL;
8516 ret->types = NULL_TREE;
8517 ret->others = NULL_TREE;
8518 ret->pending_sizes = NULL;
8519 ret->had_vla_unspec = 0;
8520 ret->no_named_args_stdarg_p = 0;
8521 return ret;
8522}
8523
8524/* Take apart the current scope and return a c_arg_info structure with
8525 info on a parameter list just parsed.
8526
8527 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8528
8529 ELLIPSIS being true means the argument list ended in '...' so don't
8530 append a sentinel (void_list_node) to the end of the type-list.
8531
8532 EXPR is NULL or an expression that needs to be evaluated for the
8533 side effects of array size expressions in the parameters. */
8534
8535struct c_arg_info *
8536get_parm_info (bool ellipsis, tree expr)
8537{
8538 struct c_binding *b = current_scope->bindings;
8539 struct c_arg_info *arg_info = build_arg_info ();
8540
8541 tree parms = NULL_TREE;
8542 vec<c_arg_tag, va_gc> *tags = NULL;
8543 tree types = NULL_TREE;
8544 tree others = NULL_TREE;
8545
8546 bool gave_void_only_once_err = false;
8547
8548 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8549
8550 /* The bindings in this scope must not get put into a block.
8551 We will take care of deleting the binding nodes. */
8552 current_scope->bindings = 0;
8553
8554 /* This function is only called if there was *something* on the
8555 parameter list. */
8556 gcc_assert (b);
8557
8558 /* A parameter list consisting solely of 'void' indicates that the
8559 function takes no arguments. But if the 'void' is qualified
8560 (by 'const' or 'volatile'), or has a storage class specifier
8561 ('register'), then the behavior is undefined; issue an error.
8562 Typedefs for 'void' are OK (see DR#157). */
8563 if (b->prev == 0 /* one binding */
8564 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8565 && !DECL_NAME (b->decl) /* anonymous */
8566 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8567 {
8568 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8569 || C_DECL_REGISTER (b->decl))
8570 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8571
8572 /* There cannot be an ellipsis. */
8573 if (ellipsis)
8574 error_at (b->locus, "%<void%> must be the only parameter");
8575
8576 arg_info->types = void_list_node;
8577 return arg_info;
8578 }
8579
8580 if (!ellipsis)
8581 types = void_list_node;
8582
8583 /* Break up the bindings list into parms, tags, types, and others;
8584 apply sanity checks; purge the name-to-decl bindings. */
8585 while (b)
8586 {
8587 tree decl = b->decl;
8588 tree type = TREE_TYPE (decl);
8589 c_arg_tag tag;
8590 const char *keyword;
8591
8592 switch (TREE_CODE (decl))
8593 {
8594 case PARM_DECL:
8595 if (b->id)
8596 {
8597 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8598 I_SYMBOL_BINDING (b->id) = b->shadowed;
8599 }
8600
8601 /* Check for forward decls that never got their actual decl. */
8602 if (TREE_ASM_WRITTEN (decl))
8603 error_at (b->locus,
8604 "parameter %q+D has just a forward declaration", decl);
8605 /* Check for (..., void, ...) and issue an error. */
8606 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8607 {
8608 if (!gave_void_only_once_err)
8609 {
8610 error_at (b->locus, "%<void%> must be the only parameter");
8611 gave_void_only_once_err = true;
8612 }
8613 }
8614 else
8615 {
8616 /* Valid parameter, add it to the list. */
8617 DECL_CHAIN (decl) = parms;
8618 parms = decl;
8619
8620 /* Since there is a prototype, args are passed in their
8621 declared types. The back end may override this later. */
8622 DECL_ARG_TYPE (decl) = type;
8623 types = tree_cons (0, type, types);
8624 }
8625 break;
8626
8627 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8628 case UNION_TYPE: keyword = "union"; goto tag;
8629 case RECORD_TYPE: keyword = "struct"; goto tag;
8630 tag:
8631 /* Types may not have tag-names, in which case the type
8632 appears in the bindings list with b->id NULL. */
8633 if (b->id)
8634 {
8635 gcc_assert (I_TAG_BINDING (b->id) == b);
8636 I_TAG_BINDING (b->id) = b->shadowed;
8637 }
8638
8639 /* Warn about any struct, union or enum tags defined in a
8640 parameter list. The scope of such types is limited to
8641 the parameter list, which is rarely if ever desirable
8642 (it's impossible to call such a function with type-
8643 correct arguments). An anonymous union parm type is
8644 meaningful as a GNU extension, so don't warn for that. */
8645 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8646 {
8647 if (b->id)
8648 {
8649 /* The %s will be one of 'struct', 'union', or 'enum'. */
8650 if (!flag_isoc23)
8651 warning_at (b->locus, 0,
8652 "%<%s %E%> declared inside parameter list"
8653 " will not be visible outside of this definition or"
8654 " declaration", keyword, b->id);
8655 }
8656 else
8657 /* The %s will be one of 'struct', 'union', or 'enum'. */
8658 warning_at (b->locus, 0,
8659 "anonymous %s declared inside parameter list"
8660 " will not be visible outside of this definition or"
8661 " declaration", keyword);
8662 }
8663
8664 tag.id = b->id;
8665 tag.type = decl;
8666 vec_safe_push (v&: tags, obj: tag);
8667 break;
8668
8669 case FUNCTION_DECL:
8670 /* FUNCTION_DECLs appear when there is an implicit function
8671 declaration in the parameter list. */
8672 gcc_assert (b->nested || seen_error ());
8673 goto set_shadowed;
8674
8675 case CONST_DECL:
8676 case TYPE_DECL:
8677 /* CONST_DECLs appear here when we have an embedded enum,
8678 and TYPE_DECLs appear here when we have an embedded struct
8679 or union. No warnings for this - we already warned about the
8680 type itself. */
8681
8682 /* When we reinsert this decl in the function body, we need
8683 to reconstruct whether it was marked as nested. */
8684 gcc_assert (!b->nested);
8685 DECL_CHAIN (decl) = others;
8686 others = decl;
8687 /* fall through */
8688
8689 case ERROR_MARK:
8690 set_shadowed:
8691 /* error_mark_node appears here when we have an undeclared
8692 variable. Just throw it away. */
8693 if (b->id)
8694 {
8695 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8696 I_SYMBOL_BINDING (b->id) = b->shadowed;
8697 }
8698 break;
8699
8700 /* Other things that might be encountered. */
8701 case LABEL_DECL:
8702 case VAR_DECL:
8703 default:
8704 gcc_unreachable ();
8705 }
8706
8707 b = free_binding_and_advance (b);
8708 }
8709
8710 arg_info->parms = parms;
8711 arg_info->tags = tags;
8712 arg_info->types = types;
8713 arg_info->others = others;
8714 arg_info->pending_sizes = expr;
8715 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8716 return arg_info;
8717}
8718
8719/* Get the struct, enum or union (CODE says which) with tag NAME.
8720 Define the tag as a forward-reference with location LOC if it is
8721 not defined. HAVE_STD_ATTRS says whether any standard attributes
8722 were present after the struct, union or enum keyword; ATTRS are the
8723 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8724 whether an enum type specifier (": specifier-qualifier-list") is
8725 present; if so, this is called before that specifier is parsed, so
8726 that the tag is in scope for that specifier. Return a c_typespec
8727 structure for the type specifier. */
8728
8729struct c_typespec
8730parser_xref_tag (location_t loc, enum tree_code code, tree name,
8731 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8732{
8733 struct c_typespec ret;
8734 tree ref;
8735 location_t refloc;
8736
8737 ret.expr = NULL_TREE;
8738 ret.expr_const_operands = true;
8739 ret.has_enum_type_specifier = has_enum_type_specifier;
8740
8741 /* If a cross reference is requested, look up the type already
8742 defined for this tag and return it. If an enum type specifier is
8743 present, only a definition in the current scope is relevant. */
8744
8745 ref = lookup_tag (code, name, thislevel_only: has_enum_type_specifier, ploc: &refloc);
8746
8747 /* If the visble type is still being defined, see if there is
8748 an earlier definition (which may be complete). We do not
8749 have to loop because nested redefinitions are not allowed. */
8750 if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8751 {
8752 tree vis = previous_tag (type: ref);
8753 if (vis)
8754 ref = vis;
8755 }
8756
8757 /* If this is the right type of tag, return what we found.
8758 (This reference will be shadowed by shadow_tag later if appropriate.)
8759 If this is the wrong type of tag, do not return it. If it was the
8760 wrong type in the same scope, we will have had an error
8761 message already; if in a different scope and declaring
8762 a name, pending_xref_error will give an error message; but if in a
8763 different scope and not declaring a name, this tag should
8764 shadow the previous declaration of a different type of tag, and
8765 this would not work properly if we return the reference found.
8766 (For example, with "struct foo" in an outer scope, "union foo;"
8767 must shadow that tag with a new one of union type.) */
8768 ret.kind = (ref
8769 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8770 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8771 if (ref && TREE_CODE (ref) == code)
8772 {
8773 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8774 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8775 && loc != UNKNOWN_LOCATION
8776 && warn_cxx_compat)
8777 {
8778 auto_diagnostic_group d;
8779 switch (code)
8780 {
8781 case ENUMERAL_TYPE:
8782 if (warning_at (loc, OPT_Wc___compat,
8783 ("enum type defined in struct or union "
8784 "is not visible in C++")))
8785 inform (refloc, "enum type defined here");
8786 break;
8787 case RECORD_TYPE:
8788 if (warning_at (loc, OPT_Wc___compat,
8789 ("struct defined in struct or union "
8790 "is not visible in C++")))
8791 inform (refloc, "struct defined here");
8792 break;
8793 case UNION_TYPE:
8794 if (warning_at (loc, OPT_Wc___compat,
8795 ("union defined in struct or union "
8796 "is not visible in C++")))
8797 inform (refloc, "union defined here");
8798 break;
8799 default:
8800 gcc_unreachable();
8801 }
8802 }
8803
8804 ret.spec = ref;
8805 return ret;
8806 }
8807
8808 /* If no such tag is yet defined, create a forward-reference node
8809 and record it as the "definition".
8810 When a real declaration of this type is found,
8811 the forward-reference will be altered into a real type. */
8812
8813 ref = make_node (code);
8814 if (flag_isoc23 && code != ENUMERAL_TYPE)
8815 SET_TYPE_STRUCTURAL_EQUALITY (ref);
8816 if (code == ENUMERAL_TYPE)
8817 {
8818 /* Give the type a default layout like unsigned int
8819 to avoid crashing if it does not get defined. */
8820 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8821 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8822 TYPE_USER_ALIGN (ref) = 0;
8823 TYPE_UNSIGNED (ref) = 1;
8824 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8825 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8826 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8827 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8828 }
8829
8830 pushtag (loc, name, type: ref);
8831 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8832 if (in_underspecified_init)
8833 error_at (loc, "%qT declared in underspecified object initializer",
8834 ref);
8835
8836 ret.spec = ref;
8837 return ret;
8838}
8839
8840/* Get the struct, enum or union (CODE says which) with tag NAME.
8841 Define the tag as a forward-reference if it is not defined.
8842 Return a tree for the type. */
8843
8844tree
8845xref_tag (enum tree_code code, tree name)
8846{
8847 return parser_xref_tag (loc: input_location, code, name, have_std_attrs: false, NULL_TREE,
8848 has_enum_type_specifier: false).spec;
8849}
8850
8851/* Make sure that the tag NAME is defined *in the current scope*
8852 at least as a forward reference.
8853 LOC is the location of the struct's definition.
8854 CODE says which kind of tag NAME ought to be.
8855
8856 This stores the current value of the file static STRUCT_PARSE_INFO
8857 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8858 new c_struct_parse_info structure. The old value of
8859 STRUCT_PARSE_INFO is restored in finish_struct. */
8860
8861tree
8862start_struct (location_t loc, enum tree_code code, tree name,
8863 class c_struct_parse_info **enclosing_struct_parse_info)
8864{
8865 /* If there is already a tag defined at this scope
8866 (as a forward reference), just return it. */
8867
8868 tree ref = NULL_TREE;
8869 location_t refloc = UNKNOWN_LOCATION;
8870
8871 if (name != NULL_TREE)
8872 ref = lookup_tag (code, name, thislevel_only: true, ploc: &refloc);
8873
8874 /* For C23, even if we already have a completed definition,
8875 we do not use it. We will check for consistency later.
8876 If we are in a nested redefinition the type is not
8877 complete. We will then detect this below. */
8878 if (flag_isoc23 && ref && TYPE_SIZE (ref))
8879 ref = NULL_TREE;
8880
8881 if (ref && TREE_CODE (ref) == code)
8882 {
8883 if (TYPE_STUB_DECL (ref))
8884 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8885
8886 if (TYPE_SIZE (ref))
8887 {
8888 auto_diagnostic_group d;
8889 if (code == UNION_TYPE)
8890 error_at (loc, "redefinition of %<union %E%>", name);
8891 else
8892 error_at (loc, "redefinition of %<struct %E%>", name);
8893 if (refloc != UNKNOWN_LOCATION)
8894 inform (refloc, "originally defined here");
8895 /* Don't create structures using a name already in use. */
8896 ref = NULL_TREE;
8897 }
8898 else if (C_TYPE_BEING_DEFINED (ref))
8899 {
8900 if (code == UNION_TYPE)
8901 error_at (loc, "nested redefinition of %<union %E%>", name);
8902 else
8903 error_at (loc, "nested redefinition of %<struct %E%>", name);
8904 /* Don't bother to report "originally defined here" for a
8905 nested redefinition; the original definition should be
8906 obvious. */
8907 /* Don't create structures that contain themselves. */
8908 ref = NULL_TREE;
8909 }
8910 }
8911
8912 /* Otherwise create a forward-reference just so the tag is in scope. */
8913
8914 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8915 {
8916 ref = make_node (code);
8917 if (flag_isoc23)
8918 SET_TYPE_STRUCTURAL_EQUALITY (ref);
8919 pushtag (loc, name, type: ref);
8920 }
8921
8922 C_TYPE_BEING_DEFINED (ref) = 1;
8923 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8924 TYPE_PACKED (v) = flag_pack_struct;
8925
8926 *enclosing_struct_parse_info = struct_parse_info;
8927 struct_parse_info = new c_struct_parse_info ();
8928
8929 /* FIXME: This will issue a warning for a use of a type defined
8930 within a statement expr used within sizeof, et. al. This is not
8931 terribly serious as C++ doesn't permit statement exprs within
8932 sizeof anyhow. */
8933 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8934 warning_at (loc, OPT_Wc___compat,
8935 "defining type in %qs expression is invalid in C++",
8936 (in_sizeof
8937 ? "sizeof"
8938 : (in_typeof ? "typeof" : "alignof")));
8939
8940 if (in_underspecified_init)
8941 error_at (loc, "%qT defined in underspecified object initializer", ref);
8942
8943 return ref;
8944}
8945
8946/* Process the specs, declarator and width (NULL if omitted)
8947 of a structure component, returning a FIELD_DECL node.
8948 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8949 DECL_ATTRS is as for grokdeclarator.
8950
8951 LOC is the location of the structure component.
8952
8953 This is done during the parsing of the struct declaration.
8954 The FIELD_DECL nodes are chained together and the lot of them
8955 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8956
8957tree
8958grokfield (location_t loc,
8959 struct c_declarator *declarator, struct c_declspecs *declspecs,
8960 tree width, tree *decl_attrs, tree *expr)
8961{
8962 tree value;
8963
8964 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8965 && width == NULL_TREE)
8966 {
8967 /* This is an unnamed decl.
8968
8969 If we have something of the form "union { list } ;" then this
8970 is the anonymous union extension. Similarly for struct.
8971
8972 If this is something of the form "struct foo;", then
8973 If MS or Plan 9 extensions are enabled, this is handled as
8974 an anonymous struct.
8975 Otherwise this is a forward declaration of a structure tag.
8976
8977 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8978 If foo names a structure or union without a tag, then this
8979 is an anonymous struct (this is permitted by C11).
8980 If MS or Plan 9 extensions are enabled and foo names a
8981 structure, then again this is an anonymous struct.
8982 Otherwise this is an error.
8983
8984 Oh what a horrid tangled web we weave. I wonder if MS consciously
8985 took this from Plan 9 or if it was an accident of implementation
8986 that took root before someone noticed the bug... */
8987
8988 tree type = declspecs->type;
8989 bool ok = false;
8990
8991 if (RECORD_OR_UNION_TYPE_P (type)
8992 && (flag_ms_extensions
8993 || flag_plan9_extensions
8994 || !declspecs->typedef_p))
8995 {
8996 if (flag_ms_extensions || flag_plan9_extensions)
8997 ok = true;
8998 else if (TYPE_NAME (type) == NULL)
8999 ok = true;
9000 else
9001 ok = false;
9002 }
9003 if (!ok)
9004 {
9005 pedwarn (loc, 0, "declaration does not declare anything");
9006 return NULL_TREE;
9007 }
9008 if (flag_isoc99)
9009 pedwarn_c99 (loc, opt: OPT_Wpedantic,
9010 "ISO C99 doesn%'t support unnamed structs/unions");
9011 else
9012 pedwarn_c99 (loc, opt: OPT_Wpedantic,
9013 "ISO C90 doesn%'t support unnamed structs/unions");
9014 }
9015
9016 value = grokdeclarator (declarator, declspecs, decl_context: FIELD, initialized: false,
9017 width: width ? &width : NULL, decl_attrs, expr, NULL,
9018 deprecated_state: DEPRECATED_NORMAL);
9019
9020 finish_decl (decl: value, init_loc: loc, NULL_TREE, NULL_TREE, NULL_TREE);
9021 DECL_INITIAL (value) = width;
9022 if (width)
9023 SET_DECL_C_BIT_FIELD (value);
9024
9025 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9026 {
9027 /* If we currently have a binding for this field, set the
9028 in_struct field in the binding, so that we warn about lookups
9029 which find it. */
9030 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9031 if (b != NULL)
9032 {
9033 /* If the in_struct field is not yet set, push it on a list
9034 to be cleared when this struct is finished. */
9035 if (!b->in_struct)
9036 {
9037 struct_parse_info->fields.safe_push (obj: b);
9038 b->in_struct = 1;
9039 }
9040 }
9041 }
9042
9043 return value;
9044}
9045
9046/* Subroutine of detect_field_duplicates: return whether X and Y,
9047 which are both fields in the same struct, have duplicate field
9048 names. */
9049
9050static bool
9051is_duplicate_field (tree x, tree y)
9052{
9053 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9054 return true;
9055
9056 /* When using -fplan9-extensions, an anonymous field whose name is a
9057 typedef can duplicate a field name. */
9058 if (flag_plan9_extensions
9059 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9060 {
9061 tree xt, xn, yt, yn;
9062
9063 xt = TREE_TYPE (x);
9064 if (DECL_NAME (x) != NULL_TREE)
9065 xn = DECL_NAME (x);
9066 else if (RECORD_OR_UNION_TYPE_P (xt)
9067 && TYPE_NAME (xt) != NULL_TREE
9068 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9069 xn = DECL_NAME (TYPE_NAME (xt));
9070 else
9071 xn = NULL_TREE;
9072
9073 yt = TREE_TYPE (y);
9074 if (DECL_NAME (y) != NULL_TREE)
9075 yn = DECL_NAME (y);
9076 else if (RECORD_OR_UNION_TYPE_P (yt)
9077 && TYPE_NAME (yt) != NULL_TREE
9078 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9079 yn = DECL_NAME (TYPE_NAME (yt));
9080 else
9081 yn = NULL_TREE;
9082
9083 if (xn != NULL_TREE && xn == yn)
9084 return true;
9085 }
9086
9087 return false;
9088}
9089
9090/* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9091 to HTAB, giving errors for any duplicates. */
9092
9093static void
9094detect_field_duplicates_hash (tree fieldlist,
9095 hash_table<nofree_ptr_hash <tree_node> > *htab)
9096{
9097 tree x, y;
9098 tree_node **slot;
9099
9100 for (x = fieldlist; x ; x = DECL_CHAIN (x))
9101 if ((y = DECL_NAME (x)) != NULL_TREE)
9102 {
9103 slot = htab->find_slot (value: y, insert: INSERT);
9104 if (*slot)
9105 {
9106 error ("duplicate member %q+D", x);
9107 DECL_NAME (x) = NULL_TREE;
9108 }
9109 *slot = y;
9110 }
9111 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9112 {
9113 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9114
9115 /* When using -fplan9-extensions, an anonymous field whose
9116 name is a typedef can duplicate a field name. */
9117 if (flag_plan9_extensions
9118 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9119 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9120 {
9121 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9122 slot = htab->find_slot (value: xn, insert: INSERT);
9123 if (*slot)
9124 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9125 *slot = xn;
9126 }
9127 }
9128}
9129
9130/* Generate an error for any duplicate field names in FIELDLIST. Munge
9131 the list such that this does not present a problem later. */
9132
9133static void
9134detect_field_duplicates (tree fieldlist)
9135{
9136 tree x, y;
9137 int timeout = 10;
9138
9139 /* If the struct is the list of instance variables of an Objective-C
9140 class, then we need to check all the instance variables of
9141 superclasses when checking for duplicates (since you can't have
9142 an instance variable in a subclass with the same name as an
9143 instance variable in a superclass). We pass on this job to the
9144 Objective-C compiler. objc_detect_field_duplicates() will return
9145 false if we are not checking the list of instance variables and
9146 the C frontend should proceed with the standard field duplicate
9147 checks. If we are checking the list of instance variables, the
9148 ObjC frontend will do the check, emit the errors if needed, and
9149 then return true. */
9150 if (c_dialect_objc ())
9151 if (objc_detect_field_duplicates (false))
9152 return;
9153
9154 /* First, see if there are more than "a few" fields.
9155 This is trivially true if there are zero or one fields. */
9156 if (!fieldlist || !DECL_CHAIN (fieldlist))
9157 return;
9158 x = fieldlist;
9159 do {
9160 timeout--;
9161 if (DECL_NAME (x) == NULL_TREE
9162 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9163 timeout = 0;
9164 x = DECL_CHAIN (x);
9165 } while (timeout > 0 && x);
9166
9167 /* If there were "few" fields and no anonymous structures or unions,
9168 avoid the overhead of allocating a hash table. Instead just do
9169 the nested traversal thing. */
9170 if (timeout > 0)
9171 {
9172 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9173 /* When using -fplan9-extensions, we can have duplicates
9174 between typedef names and fields. */
9175 if (DECL_NAME (x)
9176 || (flag_plan9_extensions
9177 && DECL_NAME (x) == NULL_TREE
9178 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9179 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9180 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9181 {
9182 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9183 if (is_duplicate_field (x: y, y: x))
9184 {
9185 error ("duplicate member %q+D", x);
9186 DECL_NAME (x) = NULL_TREE;
9187 }
9188 }
9189 }
9190 else
9191 {
9192 hash_table<nofree_ptr_hash <tree_node> > htab (37);
9193 detect_field_duplicates_hash (fieldlist, htab: &htab);
9194 }
9195}
9196
9197/* Finish up struct info used by -Wc++-compat. */
9198
9199static void
9200warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9201 location_t record_loc)
9202{
9203 unsigned int ix;
9204 tree x;
9205 struct c_binding *b;
9206
9207 if (fieldlist == NULL_TREE)
9208 {
9209 if (code == RECORD_TYPE)
9210 warning_at (record_loc, OPT_Wc___compat,
9211 "empty struct has size 0 in C, size 1 in C++");
9212 else
9213 warning_at (record_loc, OPT_Wc___compat,
9214 "empty union has size 0 in C, size 1 in C++");
9215 }
9216
9217 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9218 the current struct. We do this now at the end of the struct
9219 because the flag is used to issue visibility warnings, and we
9220 only want to issue those warnings if the type is referenced
9221 outside of the struct declaration. */
9222 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9223 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9224
9225 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9226 typedefs used when declaring fields in this struct. If the name
9227 of any of the fields is also a typedef name then the struct would
9228 not parse in C++, because the C++ lookup rules say that the
9229 typedef name would be looked up in the context of the struct, and
9230 would thus be the field rather than the typedef. */
9231 if (!struct_parse_info->typedefs_seen.is_empty ()
9232 && fieldlist != NULL_TREE)
9233 {
9234 /* Use a hash_set<tree> using the name of the typedef. We can use
9235 a hash_set<tree> because identifiers are interned. */
9236 hash_set<tree> tset;
9237
9238 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9239 tset.add (DECL_NAME (x));
9240
9241 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9242 {
9243 if (DECL_NAME (x) != NULL_TREE
9244 && tset.contains (DECL_NAME (x)))
9245 {
9246 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9247 ("using %qD as both field and typedef name is "
9248 "invalid in C++"),
9249 x);
9250 /* FIXME: It would be nice to report the location where
9251 the typedef name is used. */
9252 }
9253 }
9254 }
9255
9256 /* For each field which has a binding and which was not defined in
9257 an enclosing struct, clear the in_struct field. */
9258 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9259 b->in_struct = 0;
9260}
9261
9262/* Function to help qsort sort FIELD_DECLs by name order. */
9263
9264static int
9265field_decl_cmp (const void *x_p, const void *y_p)
9266{
9267 const tree *const x = (const tree *) x_p;
9268 const tree *const y = (const tree *) y_p;
9269
9270 if (DECL_NAME (*x) == DECL_NAME (*y))
9271 /* A nontype is "greater" than a type. */
9272 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9273 if (DECL_NAME (*x) == NULL_TREE)
9274 return -1;
9275 if (DECL_NAME (*y) == NULL_TREE)
9276 return 1;
9277 if (DECL_NAME (*x) < DECL_NAME (*y))
9278 return -1;
9279 return 1;
9280}
9281
9282/* If this structure or union completes the type of any previous
9283 variable declaration, lay it out and output its rtl. */
9284static void
9285finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9286{
9287 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9288 {
9289 tree decl = TREE_VALUE (x);
9290 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9291 layout_array_type (TREE_TYPE (decl));
9292 if (TREE_CODE (decl) != TYPE_DECL)
9293 {
9294 relayout_decl (decl);
9295 if (c_dialect_objc ())
9296 objc_check_decl (decl);
9297 rest_of_decl_compilation (decl, toplevel, 0);
9298 }
9299 }
9300}
9301
9302/* Determine whether the FIELD_DECL X is a flexible array member according to
9303 the following info:
9304 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9305 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9306 or "[1]";
9307 C. flag_strict_flex_arrays;
9308 D. the attribute strict_flex_array that is attached to the field
9309 if presenting.
9310 Return TRUE when it's a flexible array member, FALSE otherwise. */
9311
9312static bool
9313is_flexible_array_member_p (bool is_last_field,
9314 tree x)
9315{
9316 /* If not the last field, return false. */
9317 if (!is_last_field)
9318 return false;
9319
9320 /* If not an array field, return false. */
9321 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9322 return false;
9323
9324 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9325 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9326 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9327
9328 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9329
9330 switch (strict_flex_array_level)
9331 {
9332 case 0:
9333 /* Default, all trailing arrays are flexible array members. */
9334 return true;
9335 case 1:
9336 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9337 if (is_one_element_array)
9338 return true;
9339 /* FALLTHROUGH. */
9340 case 2:
9341 /* Level 2: all "[0]", and "[]" are flexible array members. */
9342 if (is_zero_length_array)
9343 return true;
9344 /* FALLTHROUGH. */
9345 case 3:
9346 /* Level 3: Only "[]" are flexible array members. */
9347 if (is_flexible_array)
9348 return true;
9349 break;
9350 default:
9351 gcc_unreachable ();
9352 }
9353 return false;
9354}
9355
9356/* Recompute TYPE_CANONICAL for variants of the type including qualified
9357 versions of the type and related pointer types after an aggregate type
9358 has been finalized.
9359 Will not update array types, pointers to array types, function
9360 types and other derived types created while the type was still
9361 incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9362
9363static void
9364c_update_type_canonical (tree t)
9365{
9366 for (tree x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9367 {
9368 if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9369 {
9370 if (TYPE_QUALS (x) == TYPE_QUALS (t))
9371 TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9372 else if (TYPE_CANONICAL (t) != t
9373 || check_qualified_type (x, t, TYPE_QUALS (x)))
9374 TYPE_CANONICAL (x)
9375 = build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9376 else
9377 TYPE_CANONICAL (x) = x;
9378 }
9379 else if (x != t)
9380 continue;
9381 for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9382 {
9383 if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9384 continue;
9385 if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9386 TYPE_CANONICAL (p)
9387 = build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9388 false);
9389 else
9390 TYPE_CANONICAL (p) = p;
9391 c_update_type_canonical (t: p);
9392 }
9393 }
9394}
9395
9396/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9397 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9398 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9399 ATTRIBUTES are attributes to be applied to the structure.
9400
9401 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9402 the struct was started. */
9403
9404tree
9405finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9406 class c_struct_parse_info *enclosing_struct_parse_info,
9407 tree *expr)
9408{
9409 tree x;
9410 bool toplevel = file_scope == current_scope;
9411
9412 /* If this type was previously laid out as a forward reference,
9413 make sure we lay it out again. */
9414
9415 TYPE_SIZE (t) = NULL_TREE;
9416
9417 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9418
9419 if (pedantic)
9420 {
9421 for (x = fieldlist; x; x = DECL_CHAIN (x))
9422 {
9423 if (DECL_NAME (x) != NULL_TREE)
9424 break;
9425 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9426 break;
9427 }
9428
9429 if (x == NULL_TREE)
9430 {
9431 if (TREE_CODE (t) == UNION_TYPE)
9432 {
9433 if (fieldlist)
9434 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9435 else
9436 pedwarn (loc, OPT_Wpedantic, "union has no members");
9437 }
9438 else
9439 {
9440 if (fieldlist)
9441 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9442 else
9443 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9444 }
9445 }
9446 }
9447
9448 /* Install struct as DECL_CONTEXT of each field decl.
9449 Also process specified field sizes, found in the DECL_INITIAL,
9450 storing 0 there after the type has been changed to precision equal
9451 to its width, rather than the precision of the specified standard
9452 type. (Correct layout requires the original type to have been preserved
9453 until now.) */
9454
9455 bool saw_named_field = false;
9456 for (x = fieldlist; x; x = DECL_CHAIN (x))
9457 {
9458 /* Whether this field is the last field of the structure or union.
9459 for UNION, any field is the last field of it. */
9460 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9461 || (TREE_CODE (t) == UNION_TYPE);
9462
9463 if (TREE_TYPE (x) == error_mark_node)
9464 continue;
9465
9466 DECL_CONTEXT (x) = t;
9467
9468 tree t1 = strip_array_types (TREE_TYPE (x));
9469 /* If any field is const, the structure type is pseudo-const. */
9470 if (TREE_READONLY (x))
9471 C_TYPE_FIELDS_READONLY (t) = 1;
9472 else
9473 {
9474 /* A field that is pseudo-const makes the structure likewise. */
9475 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9476 C_TYPE_FIELDS_READONLY (t) = 1;
9477 }
9478
9479 /* Any field that is volatile means variables of this type must be
9480 treated in some ways as volatile. */
9481 if (TREE_THIS_VOLATILE (x))
9482 {
9483 C_TYPE_FIELDS_VOLATILE (t) = 1;
9484 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9485 }
9486
9487 /* Any field that is volatile, restrict-qualified or atomic
9488 means the type cannot be used for a constexpr object. */
9489 if (TYPE_QUALS (t1)
9490 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9491 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9492 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9493 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9494
9495 /* Any field of nominal variable size implies structure is too. */
9496 if (C_DECL_VARIABLE_SIZE (x))
9497 C_TYPE_VARIABLE_SIZE (t) = 1;
9498
9499 /* If any field is variably modified, record this fact. */
9500 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9501 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9502
9503 if (DECL_C_BIT_FIELD (x))
9504 {
9505 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9506 DECL_SIZE (x) = bitsize_int (width);
9507 DECL_BIT_FIELD (x) = 1;
9508 }
9509
9510 if (TYPE_PACKED (t)
9511 && (DECL_BIT_FIELD (x)
9512 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9513 DECL_PACKED (x) = 1;
9514
9515 /* Detect flexible array member in an invalid context. */
9516 if (flexible_array_member_type_p (TREE_TYPE (x)))
9517 {
9518 if (TREE_CODE (t) == UNION_TYPE)
9519 {
9520 error_at (DECL_SOURCE_LOCATION (x),
9521 "flexible array member in union");
9522 TREE_TYPE (x) = error_mark_node;
9523 }
9524 else if (!is_last_field)
9525 {
9526 error_at (DECL_SOURCE_LOCATION (x),
9527 "flexible array member not at end of struct");
9528 TREE_TYPE (x) = error_mark_node;
9529 }
9530 else if (!saw_named_field)
9531 {
9532 error_at (DECL_SOURCE_LOCATION (x),
9533 "flexible array member in a struct with no named "
9534 "members");
9535 TREE_TYPE (x) = error_mark_node;
9536 }
9537 }
9538
9539 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9540 && flexible_array_type_p (TREE_TYPE (x)))
9541 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9542 "invalid use of structure with flexible array member");
9543
9544 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9545 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9546
9547 /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9548 when x is an array and is the last field. */
9549 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9550 TYPE_INCLUDES_FLEXARRAY (t)
9551 = is_last_field && flexible_array_member_type_p (TREE_TYPE (x));
9552 /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9553 when x is an union or record and is the last field. */
9554 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9555 TYPE_INCLUDES_FLEXARRAY (t)
9556 = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9557
9558 if (warn_flex_array_member_not_at_end
9559 && !is_last_field
9560 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9561 && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9562 warning_at (DECL_SOURCE_LOCATION (x),
9563 OPT_Wflex_array_member_not_at_end,
9564 "structure containing a flexible array member"
9565 " is not at the end of another structure");
9566
9567 if (DECL_NAME (x)
9568 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9569 saw_named_field = true;
9570 }
9571
9572 detect_field_duplicates (fieldlist);
9573
9574 /* Now we have the nearly final fieldlist. Record it,
9575 then lay out the structure or union (including the fields). */
9576
9577 TYPE_FIELDS (t) = fieldlist;
9578
9579 maybe_apply_pragma_scalar_storage_order (t);
9580
9581 layout_type (t);
9582
9583 if (TYPE_SIZE_UNIT (t)
9584 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9585 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9586 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9587 error ("type %qT is too large", t);
9588
9589 /* Give bit-fields their proper types and rewrite the type of array fields
9590 with scalar component if the enclosing type has reverse storage order. */
9591 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9592 {
9593 if (TREE_CODE (field) == FIELD_DECL
9594 && DECL_INITIAL (field)
9595 && TREE_TYPE (field) != error_mark_node)
9596 {
9597 unsigned HOST_WIDE_INT width
9598 = tree_to_uhwi (DECL_INITIAL (field));
9599 tree type = TREE_TYPE (field);
9600 if (width != TYPE_PRECISION (type))
9601 {
9602 if (TREE_CODE (type) == BITINT_TYPE
9603 && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9604 TREE_TYPE (field)
9605 = build_bitint_type (width, TYPE_UNSIGNED (type));
9606 else
9607 TREE_TYPE (field)
9608 = c_build_bitfield_integer_type (width,
9609 TYPE_UNSIGNED (type));
9610 if (tree attr = c_hardbool_type_attr (type))
9611 decl_attributes (&TREE_TYPE (field),
9612 copy_list (attr),
9613 0, NULL_TREE);
9614 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9615 }
9616 DECL_INITIAL (field) = NULL_TREE;
9617 }
9618 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9619 && TREE_CODE (field) == FIELD_DECL
9620 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9621 {
9622 tree ftype = TREE_TYPE (field);
9623 tree ctype = strip_array_types (type: ftype);
9624 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9625 {
9626 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9627 tree *typep = &fmain_type;
9628 do {
9629 *typep = build_distinct_type_copy (*typep);
9630 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9631 typep = &TREE_TYPE (*typep);
9632 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9633 TREE_TYPE (field)
9634 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9635 }
9636 }
9637
9638 /* Warn on problematic type punning for storage order purposes. */
9639 if (TREE_CODE (t) == UNION_TYPE
9640 && TREE_CODE (field) == FIELD_DECL
9641 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9642 {
9643 tree ftype = TREE_TYPE (field);
9644 if (TREE_CODE (ftype) == ARRAY_TYPE)
9645 ftype = strip_array_types (type: ftype);
9646 if (RECORD_OR_UNION_TYPE_P (ftype)
9647 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9648 != TYPE_REVERSE_STORAGE_ORDER (t))
9649 warning_at (DECL_SOURCE_LOCATION (field),
9650 OPT_Wscalar_storage_order,
9651 "type punning toggles scalar storage order");
9652 }
9653 }
9654
9655 /* Now we have the truly final field list.
9656 Store it in this type and in the variants. */
9657
9658 TYPE_FIELDS (t) = fieldlist;
9659
9660 /* If there are lots of fields, sort so we can look through them fast.
9661 We arbitrarily consider 16 or more elts to be "a lot". */
9662
9663 {
9664 int len = 0;
9665
9666 for (x = fieldlist; x; x = DECL_CHAIN (x))
9667 {
9668 if (len > 15 || DECL_NAME (x) == NULL)
9669 break;
9670 len += 1;
9671 }
9672
9673 if (len > 15)
9674 {
9675 tree *field_array;
9676 struct lang_type *space;
9677 struct sorted_fields_type *space2;
9678
9679 len += list_length (x);
9680
9681 /* Use the same allocation policy here that make_node uses, to
9682 ensure that this lives as long as the rest of the struct decl.
9683 All decls in an inline function need to be saved. */
9684
9685 space = ggc_cleared_alloc<struct lang_type> ();
9686 space2 = (sorted_fields_type *) ggc_internal_alloc
9687 (s: sizeof (struct sorted_fields_type) + len * sizeof (tree));
9688
9689 len = 0;
9690 space->s = space2;
9691 field_array = &space2->elts[0];
9692 for (x = fieldlist; x; x = DECL_CHAIN (x))
9693 {
9694 field_array[len++] = x;
9695
9696 /* If there is anonymous struct or union, break out of the loop. */
9697 if (DECL_NAME (x) == NULL)
9698 break;
9699 }
9700 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9701 if (x == NULL)
9702 {
9703 TYPE_LANG_SPECIFIC (t) = space;
9704 TYPE_LANG_SPECIFIC (t)->s->len = len;
9705 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9706 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9707 }
9708 }
9709 }
9710
9711 /* If this was supposed to be a transparent union, but we can't
9712 make it one, warn and turn off the flag. */
9713 if (TREE_CODE (t) == UNION_TYPE
9714 && TYPE_TRANSPARENT_AGGR (t)
9715 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9716 {
9717 TYPE_TRANSPARENT_AGGR (t) = 0;
9718 warning_at (loc, 0, "union cannot be made transparent");
9719 }
9720
9721 /* Check for consistency with previous definition. */
9722 if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9723 {
9724 tree vistype = previous_tag (type: t);
9725 if (vistype
9726 && TREE_CODE (vistype) == TREE_CODE (t)
9727 && !C_TYPE_BEING_DEFINED (vistype))
9728 {
9729 TYPE_STUB_DECL (vistype) = TYPE_STUB_DECL (t);
9730 if (c_type_variably_modified_p (t))
9731 error ("redefinition of struct or union %qT with variably "
9732 "modified type", t);
9733 else if (!comptypes_same_p (t, vistype))
9734 error ("redefinition of struct or union %qT", t);
9735 }
9736 }
9737
9738 C_TYPE_BEING_DEFINED (t) = 0;
9739
9740 /* Set type canonical based on equivalence class. */
9741 if (flag_isoc23)
9742 {
9743 if (c_struct_htab == NULL)
9744 c_struct_htab = hash_table<c_struct_hasher>::create_ggc (n: 61);
9745
9746 hashval_t hash = c_struct_hasher::hash (type: t);
9747
9748 gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9749 tree *e = c_struct_htab->find_slot_with_hash (comparable: t, hash, insert: INSERT);
9750 if (*e)
9751 TYPE_CANONICAL (t) = *e;
9752 else
9753 {
9754 TYPE_CANONICAL (t) = t;
9755 *e = t;
9756 }
9757 c_update_type_canonical (t);
9758 }
9759
9760 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9761 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9762 {
9763 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9764 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9765 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9766 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9767 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9768 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9769 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9770 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9771 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9772 }
9773
9774 /* Update type location to the one of the definition, instead of e.g.
9775 a forward declaration. */
9776 if (TYPE_STUB_DECL (t))
9777 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9778
9779 /* Finish debugging output for this type. */
9780 rest_of_type_compilation (t, toplevel);
9781
9782 finish_incomplete_vars (incomplete_vars, toplevel);
9783
9784 /* Make sure a DECL_EXPR is created for structs with VLA members.
9785 Because we do not know the context, we always pass expr
9786 to force creation of a BIND_EXPR which is required in some
9787 contexts. */
9788 if (c_type_variably_modified_p (t))
9789 add_decl_expr (loc, type: t, expr, set_name_p: false);
9790
9791 if (warn_cxx_compat)
9792 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), record_loc: loc);
9793
9794 if (NULL != enclosing_struct_parse_info)
9795 {
9796 delete struct_parse_info;
9797
9798 struct_parse_info = enclosing_struct_parse_info;
9799
9800 /* If this struct is defined inside a struct, add it to
9801 struct_types. */
9802 if (warn_cxx_compat
9803 && struct_parse_info != NULL
9804 && !in_sizeof && !in_typeof && !in_alignof)
9805 struct_parse_info->struct_types.safe_push (obj: t);
9806 }
9807
9808 return t;
9809}
9810
9811static struct {
9812 gt_pointer_operator new_value;
9813 void *cookie;
9814} resort_data;
9815
9816/* This routine compares two fields like field_decl_cmp but using the
9817pointer operator in resort_data. */
9818
9819static int
9820resort_field_decl_cmp (const void *x_p, const void *y_p)
9821{
9822 const tree *const x = (const tree *) x_p;
9823 const tree *const y = (const tree *) y_p;
9824
9825 if (DECL_NAME (*x) == DECL_NAME (*y))
9826 /* A nontype is "greater" than a type. */
9827 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9828 if (DECL_NAME (*x) == NULL_TREE)
9829 return -1;
9830 if (DECL_NAME (*y) == NULL_TREE)
9831 return 1;
9832 {
9833 tree d1 = DECL_NAME (*x);
9834 tree d2 = DECL_NAME (*y);
9835 resort_data.new_value (&d1, &d1, resort_data.cookie);
9836 resort_data.new_value (&d2, &d2, resort_data.cookie);
9837 if (d1 < d2)
9838 return -1;
9839 }
9840 return 1;
9841}
9842
9843/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9844
9845void
9846resort_sorted_fields (void *obj,
9847 void * ARG_UNUSED (orig_obj),
9848 gt_pointer_operator new_value,
9849 void *cookie)
9850{
9851 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9852 resort_data.new_value = new_value;
9853 resort_data.cookie = cookie;
9854 qsort (&sf->elts[0], sf->len, sizeof (tree),
9855 resort_field_decl_cmp);
9856}
9857
9858/* Lay out the type T, and its element type, and so on. */
9859
9860static void
9861layout_array_type (tree t)
9862{
9863 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9864 layout_array_type (TREE_TYPE (t));
9865 layout_type (t);
9866}
9867
9868/* Begin compiling the definition of an enumeration type.
9869 NAME is its name (or null if anonymous).
9870 LOC is the enum's location.
9871 FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9872 definition.
9873 Returns the type object, as yet incomplete.
9874 Also records info about it so that build_enumerator
9875 may be used to declare the individual values as they are read. */
9876
9877tree
9878start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9879 tree fixed_underlying_type, bool potential_nesting_p)
9880{
9881 tree enumtype = NULL_TREE;
9882 location_t enumloc = UNKNOWN_LOCATION;
9883
9884 /* If this is the real definition for a previous forward reference,
9885 fill in the contents in the same object that used to be the
9886 forward reference. */
9887
9888 if (name != NULL_TREE)
9889 enumtype = lookup_tag (code: ENUMERAL_TYPE, name, thislevel_only: true, ploc: &enumloc);
9890
9891 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
9892 {
9893 /* If the type is currently being defined or if we have seen an
9894 incomplete version which is now complete, this is a nested
9895 redefinition. The later happens if the redefinition occurs
9896 inside the enum specifier itself. */
9897 if (C_TYPE_BEING_DEFINED (enumtype)
9898 || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
9899 error_at (loc, "nested redefinition of %<enum %E%>", name);
9900
9901 /* For C23 we allow redefinitions. We set to zero and check for
9902 consistency later. */
9903 if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
9904 enumtype = NULL_TREE;
9905 }
9906
9907 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9908 {
9909 enumtype = make_node (ENUMERAL_TYPE);
9910 TYPE_SIZE (enumtype) = NULL_TREE;
9911 pushtag (loc, name, type: enumtype);
9912 if (fixed_underlying_type != NULL_TREE)
9913 {
9914 /* For an enum definition with a fixed underlying type, the
9915 type is complete during the definition and the
9916 enumeration constants have that type. If there was a
9917 tag, the type was completed in c_parser_enum_specifier.
9918 If not, it must be completed here. */
9919 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9920 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9921 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9922 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9923 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9924 TYPE_SIZE (enumtype) = NULL_TREE;
9925 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9926 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9927 layout_type (enumtype);
9928 }
9929 }
9930 /* Update type location to the one of the definition, instead of e.g.
9931 a forward declaration. */
9932 else if (TYPE_STUB_DECL (enumtype))
9933 {
9934 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9935 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9936 }
9937
9938 C_TYPE_BEING_DEFINED (enumtype) = 1;
9939
9940 if (TYPE_VALUES (enumtype) != NULL_TREE)
9941 {
9942 /* This enum is a named one that has been declared already. */
9943 auto_diagnostic_group d;
9944 error_at (loc, "redeclaration of %<enum %E%>", name);
9945 if (enumloc != UNKNOWN_LOCATION)
9946 inform (enumloc, "originally defined here");
9947
9948 /* Completely replace its old definition.
9949 The old enumerators remain defined, however. */
9950 TYPE_VALUES (enumtype) = NULL_TREE;
9951 }
9952
9953 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9954 && fixed_underlying_type == NULL_TREE)
9955 {
9956 error_at (loc, "%<enum%> declared with but defined without "
9957 "fixed underlying type");
9958 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
9959 }
9960
9961 the_enum->enum_next_value = integer_zero_node;
9962 the_enum->enum_type = enumtype;
9963 the_enum->enum_overflow = 0;
9964
9965 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9966 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9967 TYPE_PACKED (v) = 1;
9968
9969 /* FIXME: This will issue a warning for a use of a type defined
9970 within sizeof in a statement expr. This is not terribly serious
9971 as C++ doesn't permit statement exprs within sizeof anyhow. */
9972 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9973 warning_at (loc, OPT_Wc___compat,
9974 "defining type in %qs expression is invalid in C++",
9975 (in_sizeof
9976 ? "sizeof"
9977 : (in_typeof ? "typeof" : "alignof")));
9978
9979 if (in_underspecified_init)
9980 error_at (loc, "%qT defined in underspecified object initializer",
9981 enumtype);
9982
9983 return enumtype;
9984}
9985
9986/* After processing and defining all the values of an enumeration type,
9987 install their decls in the enumeration type and finish it off.
9988 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9989 and ATTRIBUTES are the specified attributes.
9990 Returns ENUMTYPE. */
9991
9992tree
9993finish_enum (tree enumtype, tree values, tree attributes)
9994{
9995 tree pair, tem;
9996 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9997 int precision;
9998 signop sign;
9999 bool toplevel = (file_scope == current_scope);
10000 struct lang_type *lt;
10001
10002 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10003
10004 /* Calculate the maximum value of any enumerator in this type. */
10005
10006 if (values == error_mark_node)
10007 minnode = maxnode = integer_zero_node;
10008 else
10009 {
10010 minnode = maxnode = TREE_VALUE (values);
10011 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10012 {
10013 tree value = TREE_VALUE (pair);
10014 if (tree_int_cst_lt (t1: maxnode, t2: value))
10015 maxnode = value;
10016 if (tree_int_cst_lt (t1: value, t2: minnode))
10017 minnode = value;
10018 }
10019 }
10020
10021 /* Construct the final type of this enumeration. It is the same
10022 as one of the integral types - the narrowest one that fits, except
10023 that normally we only go as narrow as int - and signed iff any of
10024 the values are negative. */
10025 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10026 precision = MAX (tree_int_cst_min_precision (minnode, sign),
10027 tree_int_cst_min_precision (maxnode, sign));
10028
10029 bool wider_than_int =
10030 (tree_int_cst_lt (t1: minnode, TYPE_MIN_VALUE (integer_type_node))
10031 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), t2: maxnode));
10032
10033
10034 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10035 {
10036 /* If the precision of the type was specified with an attribute and it
10037 was too small, give an error. Otherwise, use it. */
10038 if (TYPE_PRECISION (enumtype) && lookup_attribute (attr_name: "mode", list: attributes))
10039 {
10040 if (precision > TYPE_PRECISION (enumtype))
10041 {
10042 TYPE_PRECISION (enumtype) = 0;
10043 error ("specified mode too small for enumerated values");
10044 }
10045 else
10046 precision = TYPE_PRECISION (enumtype);
10047 }
10048 else
10049 TYPE_PRECISION (enumtype) = 0;
10050
10051 if (TYPE_PACKED (enumtype)
10052 || precision > TYPE_PRECISION (integer_type_node)
10053 || TYPE_PRECISION (enumtype))
10054 {
10055 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10056 if (tem == NULL)
10057 {
10058 /* This should only occur when both signed and unsigned
10059 values of maximum precision occur among the
10060 enumerators. */
10061 pedwarn (input_location, 0,
10062 "enumeration values exceed range of largest integer");
10063 tem = widest_integer_literal_type_node;
10064 }
10065 else if (precision > TYPE_PRECISION (intmax_type_node)
10066 && !tree_int_cst_lt (t1: minnode,
10067 TYPE_MIN_VALUE (intmax_type_node))
10068 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10069 t2: maxnode))
10070 pedwarn (input_location, OPT_Wpedantic,
10071 "enumeration values exceed range of %qs",
10072 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10073 }
10074 else
10075 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10076
10077 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10078 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10079 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10080 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10081 TYPE_SIZE (enumtype) = NULL_TREE;
10082 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10083 ENUM_UNDERLYING_TYPE (enumtype) =
10084 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10085
10086 layout_type (enumtype);
10087 }
10088
10089 if (values != error_mark_node)
10090 {
10091 /* Change the type of the enumerators to be the enum type. We
10092 need to do this irrespective of the size of the enum, for
10093 proper type checking. Replace the DECL_INITIALs of the
10094 enumerators, and the value slots of the list, with copies
10095 that have the enum type; they cannot be modified in place
10096 because they may be shared (e.g. integer_zero_node) Finally,
10097 change the purpose slots to point to the names of the decls. */
10098 for (pair = values; pair; pair = TREE_CHAIN (pair))
10099 {
10100 tree enu = TREE_PURPOSE (pair);
10101 tree ini = DECL_INITIAL (enu);
10102
10103 TREE_TYPE (enu) = enumtype;
10104
10105 /* Before C23, the ISO C Standard mandates enumerators to
10106 have type int, even though the underlying type of an enum
10107 type is unspecified. However, C23 allows enumerators of
10108 any integer type, and if an enumeration has any
10109 enumerators wider than int, all enumerators have the
10110 enumerated type after it is parsed. Any enumerators that
10111 fit in int are given type int in build_enumerator (which
10112 is the correct type while the enumeration is being
10113 parsed), so no conversions are needed here if all
10114 enumerators fit in int. If the enum has a fixed
10115 underlying type, the correct type was also given in
10116 build_enumerator. */
10117 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10118 ini = convert (enumtype, ini);
10119
10120 DECL_INITIAL (enu) = ini;
10121 TREE_PURPOSE (pair) = DECL_NAME (enu);
10122 /* To match the C++ FE, store the CONST_DECL rather than just its
10123 value. */
10124 TREE_VALUE (pair) = enu;
10125 }
10126
10127 TYPE_VALUES (enumtype) = values;
10128 }
10129
10130 /* Record the min/max values so that we can warn about bit-field
10131 enumerations that are too small for the values. */
10132 lt = ggc_cleared_alloc<struct lang_type> ();
10133 lt->enum_min = minnode;
10134 lt->enum_max = maxnode;
10135 TYPE_LANG_SPECIFIC (enumtype) = lt;
10136
10137 /* Fix up all variant types of this enum type. */
10138 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10139 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10140 {
10141 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10142 if (tem == enumtype)
10143 continue;
10144 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10145 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10146 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10147 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10148 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10149 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10150 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10151 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10152 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10153 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10154 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10155 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10156 }
10157
10158 /* Finish debugging output for this type. */
10159 rest_of_type_compilation (enumtype, toplevel);
10160
10161 finish_incomplete_vars (incomplete_vars, toplevel);
10162
10163 /* If this enum is defined inside a struct, add it to
10164 struct_types. */
10165 if (warn_cxx_compat
10166 && struct_parse_info != NULL
10167 && !in_sizeof && !in_typeof && !in_alignof)
10168 struct_parse_info->struct_types.safe_push (obj: enumtype);
10169
10170 /* Check for consistency with previous definition */
10171 if (flag_isoc23)
10172 {
10173 tree vistype = previous_tag (type: enumtype);
10174 if (vistype
10175 && TREE_CODE (vistype) == TREE_CODE (enumtype)
10176 && !C_TYPE_BEING_DEFINED (vistype))
10177 {
10178 TYPE_STUB_DECL (vistype) = TYPE_STUB_DECL (enumtype);
10179 if (!comptypes_same_p (enumtype, vistype))
10180 error("conflicting redefinition of enum %qT", enumtype);
10181 }
10182 }
10183
10184 C_TYPE_BEING_DEFINED (enumtype) = 0;
10185
10186 return enumtype;
10187}
10188
10189/* Build and install a CONST_DECL for one value of the
10190 current enumeration type (one that was begun with start_enum).
10191 DECL_LOC is the location of the enumerator.
10192 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10193 Return a tree-list containing the CONST_DECL and its value.
10194 Assignment of sequential values by default is handled here. */
10195
10196tree
10197build_enumerator (location_t decl_loc, location_t loc,
10198 struct c_enum_contents *the_enum, tree name, tree value)
10199{
10200 tree decl;
10201
10202 /* Validate and default VALUE. */
10203
10204 if (value != NULL_TREE)
10205 {
10206 /* Don't issue more errors for error_mark_node (i.e. an
10207 undeclared identifier) - just ignore the value expression. */
10208 if (value == error_mark_node)
10209 value = NULL_TREE;
10210 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10211 {
10212 error_at (loc, "enumerator value for %qE is not an integer constant",
10213 name);
10214 value = NULL_TREE;
10215 }
10216 else
10217 {
10218 if (TREE_CODE (value) != INTEGER_CST)
10219 {
10220 value = c_fully_fold (value, false, NULL);
10221 if (TREE_CODE (value) == INTEGER_CST)
10222 pedwarn (loc, OPT_Wpedantic,
10223 "enumerator value for %qE is not an integer "
10224 "constant expression", name);
10225 }
10226 if (TREE_CODE (value) != INTEGER_CST)
10227 {
10228 error ("enumerator value for %qE is not an integer constant",
10229 name);
10230 value = NULL_TREE;
10231 }
10232 else
10233 {
10234 value = default_conversion (value);
10235 constant_expression_warning (value);
10236 }
10237 }
10238 }
10239
10240 /* Default based on previous value. */
10241 /* It should no longer be possible to have NON_LVALUE_EXPR
10242 in the default. */
10243 if (value == NULL_TREE)
10244 {
10245 value = the_enum->enum_next_value;
10246 if (the_enum->enum_overflow)
10247 error_at (loc, "overflow in enumeration values");
10248 }
10249 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10250 {
10251 /* Enumeration constants must fit in the fixed underlying type. */
10252 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10253 error_at (loc,
10254 "enumerator value outside the range of underlying type");
10255 /* Enumeration constants for an enum with fixed underlying type
10256 have the enum type, both inside and outside the
10257 definition. */
10258 value = convert (the_enum->enum_type, value);
10259 }
10260 else
10261 {
10262 /* Even though the underlying type of an enum is unspecified, the
10263 type of enumeration constants is explicitly defined as int
10264 (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10265 GCC allows such types for older standards as an extension. */
10266 bool warned_range = false;
10267 if (!int_fits_type_p (value,
10268 (TYPE_UNSIGNED (TREE_TYPE (value))
10269 ? uintmax_type_node
10270 : intmax_type_node)))
10271 /* GCC does not consider its types larger than intmax_t to be
10272 extended integer types (although C23 would permit such types to
10273 be considered extended integer types if all the features
10274 required by <stdint.h> and <inttypes.h> macros, such as support
10275 for integer constants and I/O, were present), so diagnose if
10276 such a wider type is used. (If the wider type arose from a
10277 constant of such a type, that will also have been diagnosed,
10278 but this is the only diagnostic in the case where it arises
10279 from choosing a wider type automatically when adding 1
10280 overflows.) */
10281 warned_range = pedwarn (loc, OPT_Wpedantic,
10282 "enumerator value outside the range of %qs",
10283 (TYPE_UNSIGNED (TREE_TYPE (value))
10284 ? "uintmax_t"
10285 : "intmax_t"));
10286 if (!warned_range && !int_fits_type_p (value, integer_type_node))
10287 pedwarn_c11 (loc, opt: OPT_Wpedantic,
10288 "ISO C restricts enumerator values to range of %<int%> "
10289 "before C23");
10290
10291 /* The ISO C Standard mandates enumerators to have type int before
10292 C23, even though the underlying type of an enum type is
10293 unspecified. C23 allows enumerators of any integer type. During
10294 the parsing of the enumeration, C23 specifies that constants
10295 representable in int have type int, constants not representable
10296 in int have the type of the given expression if any, and
10297 constants not representable in int and derived by adding 1 to the
10298 previous constant have the type of that constant unless the
10299 addition would overflow or wraparound, in which case a wider type
10300 of the same signedness is chosen automatically; after the
10301 enumeration is parsed, all the constants have the type of the
10302 enumeration if any do not fit in int. */
10303 if (int_fits_type_p (value, integer_type_node))
10304 value = convert (integer_type_node, value);
10305 }
10306
10307 /* Set basis for default for next value. */
10308 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10309 {
10310 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10311 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10312 /* A value of 2 following a value of 1 overflows bool, but we
10313 cannot carry out addition directly on bool without
10314 promotion, and converting the result of arithmetic in a
10315 wider type back to bool would not produce the right result
10316 for this overflow check. */
10317 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10318 else
10319 the_enum->enum_next_value
10320 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10321 PLUS_EXPR, convert (underlying_type, value),
10322 convert (underlying_type, integer_one_node),
10323 false);
10324 }
10325 else
10326 the_enum->enum_next_value
10327 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10328 PLUS_EXPR, value, integer_one_node, false);
10329 the_enum->enum_overflow = tree_int_cst_lt (t1: the_enum->enum_next_value, t2: value);
10330 if (the_enum->enum_overflow
10331 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10332 {
10333 /* Choose a wider type with the same signedness if
10334 available. */
10335 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10336 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10337 tree new_type = (unsignedp
10338 ? long_unsigned_type_node
10339 : long_integer_type_node);
10340 if (prec > TYPE_PRECISION (new_type))
10341 new_type = (unsignedp
10342 ? long_long_unsigned_type_node
10343 : long_long_integer_type_node);
10344 if (prec > TYPE_PRECISION (new_type))
10345 new_type = (unsignedp
10346 ? widest_unsigned_literal_type_node
10347 : widest_integer_literal_type_node);
10348 if (prec <= TYPE_PRECISION (new_type))
10349 {
10350 the_enum->enum_overflow = false;
10351 the_enum->enum_next_value
10352 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10353 PLUS_EXPR, convert (new_type, value),
10354 integer_one_node, false);
10355 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10356 }
10357 }
10358
10359 /* Now create a declaration for the enum value name. */
10360
10361 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10362 DECL_INITIAL (decl) = value;
10363 DECL_CONTEXT (decl) = the_enum->enum_type;
10364 pushdecl (x: decl);
10365
10366 return tree_cons (decl, value, NULL_TREE);
10367}
10368
10369/* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10370
10371tree
10372c_simulate_enum_decl (location_t loc, const char *name,
10373 vec<string_int_pair> *values_ptr)
10374{
10375 location_t saved_loc = input_location;
10376 input_location = loc;
10377
10378 struct c_enum_contents the_enum;
10379 tree enumtype = start_enum (loc, the_enum: &the_enum, get_identifier (name),
10380 NULL_TREE, potential_nesting_p: false);
10381
10382 tree value_chain = NULL_TREE;
10383 string_int_pair *value;
10384 vec<string_int_pair> values = *values_ptr;
10385 unsigned int i;
10386 FOR_EACH_VEC_ELT (values, i, value)
10387 {
10388 tree decl = build_enumerator (decl_loc: loc, loc, the_enum: &the_enum,
10389 get_identifier (value->first),
10390 value: build_int_cst (integer_type_node,
10391 value->second));
10392 TREE_CHAIN (decl) = value_chain;
10393 value_chain = decl;
10394 }
10395
10396 finish_enum (enumtype, values: nreverse (value_chain), NULL_TREE);
10397
10398 input_location = saved_loc;
10399 return enumtype;
10400}
10401
10402/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10403
10404tree
10405c_simulate_record_decl (location_t loc, const char *name,
10406 array_slice<const tree> fields)
10407{
10408 location_t saved_loc = input_location;
10409 input_location = loc;
10410
10411 class c_struct_parse_info *struct_info;
10412 tree ident = get_identifier (name);
10413 tree type = start_struct (loc, code: RECORD_TYPE, name: ident, enclosing_struct_parse_info: &struct_info);
10414
10415 for (unsigned int i = 0; i < fields.size (); ++i)
10416 {
10417 DECL_FIELD_CONTEXT (fields[i]) = type;
10418 if (i > 0)
10419 DECL_CHAIN (fields[i - 1]) = fields[i];
10420 }
10421
10422 finish_struct (loc, t: type, fieldlist: fields[0], NULL_TREE, enclosing_struct_parse_info: struct_info);
10423
10424 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10425 set_underlying_type (decl);
10426 lang_hooks.decls.pushdecl (decl);
10427
10428 input_location = saved_loc;
10429 return type;
10430}
10431
10432/* Create the FUNCTION_DECL for a function definition.
10433 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10434 the declaration; they describe the function's name and the type it returns,
10435 but twisted together in a fashion that parallels the syntax of C.
10436
10437 This function creates a binding context for the function body
10438 as well as setting up the FUNCTION_DECL in current_function_decl.
10439
10440 Returns true on success. If the DECLARATOR is not suitable for a function
10441 (it defines a datum instead), we return false to report a parse error. */
10442
10443bool
10444start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10445 tree attributes)
10446{
10447 tree decl1, old_decl;
10448 tree restype, resdecl;
10449 location_t loc;
10450 location_t result_loc;
10451 tree expr = NULL;
10452
10453 current_function_returns_value = 0; /* Assume, until we see it does. */
10454 current_function_returns_null = 0;
10455 current_function_returns_abnormally = 0;
10456 warn_about_return_type = 0;
10457 c_switch_stack = NULL;
10458
10459 /* Indicate no valid break/continue context. */
10460 in_statement = 0;
10461
10462 decl1 = grokdeclarator (declarator, declspecs, decl_context: FUNCDEF, initialized: true, NULL,
10463 decl_attrs: &attributes, expr: &expr, NULL, deprecated_state: DEPRECATED_NORMAL);
10464 invoke_plugin_callbacks (event: PLUGIN_START_PARSE_FUNCTION, gcc_data: decl1);
10465
10466 /* If the declarator is not suitable for a function definition,
10467 cause a syntax error. */
10468 if (decl1 == NULL_TREE
10469 || TREE_CODE (decl1) != FUNCTION_DECL)
10470 return false;
10471
10472 /* Nested functions may have variably modified (return) type.
10473 Make sure the size expression is evaluated at this point. */
10474 if (expr && !current_scope->parm_flag)
10475 add_stmt (fold_convert (void_type_node, expr));
10476
10477 loc = DECL_SOURCE_LOCATION (decl1);
10478
10479 /* A nested function is not global. */
10480 if (current_function_decl != NULL_TREE)
10481 TREE_PUBLIC (decl1) = 0;
10482
10483 c_decl_attributes (node: &decl1, attributes, flags: 0);
10484
10485 if (DECL_DECLARED_INLINE_P (decl1)
10486 && DECL_UNINLINABLE (decl1)
10487 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl1)))
10488 warning_at (loc, OPT_Wattributes,
10489 "inline function %qD given attribute %qs",
10490 decl1, "noinline");
10491
10492 /* Handle gnu_inline attribute. */
10493 if (declspecs->inline_p
10494 && !flag_gnu89_inline
10495 && TREE_CODE (decl1) == FUNCTION_DECL
10496 && (lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (decl1))
10497 || current_function_decl))
10498 {
10499 if (declspecs->storage_class != csc_static)
10500 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10501 }
10502
10503 announce_function (decl1);
10504
10505 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10506 {
10507 error_at (loc, "return type is an incomplete type");
10508 /* Make it return void instead. */
10509 TREE_TYPE (decl1)
10510 = build_function_type (void_type_node,
10511 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10512 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10513 }
10514
10515 if (warn_about_return_type)
10516 permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10517 : (warn_return_type > 0 ? OPT_Wreturn_type
10518 : OPT_Wimplicit_int),
10519 "return type defaults to %<int%>");
10520
10521 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10522 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10523 DECL_INITIAL (decl1) = error_mark_node;
10524
10525 /* If this definition isn't a prototype and we had a prototype declaration
10526 before, copy the arg type info from that prototype. */
10527 old_decl = lookup_name_in_scope (DECL_NAME (decl1), scope: current_scope);
10528 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10529 old_decl = NULL_TREE;
10530
10531 current_function_prototype_locus = UNKNOWN_LOCATION;
10532 current_function_prototype_built_in = false;
10533 current_function_prototype_arg_types = NULL_TREE;
10534 tree newtype = TREE_TYPE (decl1);
10535 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10536 if (!prototype_p (newtype))
10537 {
10538 tree oldrt = TREE_TYPE (oldtype);
10539 tree newrt = TREE_TYPE (newtype);
10540 if (old_decl != NULL_TREE
10541 && TREE_CODE (oldtype) == FUNCTION_TYPE
10542 && comptypes (oldrt, newrt))
10543 {
10544 if (stdarg_p (oldtype))
10545 {
10546 auto_diagnostic_group d;
10547 warning_at (loc, 0, "%q+D defined as variadic function "
10548 "without prototype", decl1);
10549 locate_old_decl (decl: old_decl);
10550 }
10551 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10552 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10553 current_function_prototype_built_in
10554 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10555 current_function_prototype_arg_types
10556 = TYPE_ARG_TYPES (newtype);
10557 }
10558 if (TREE_PUBLIC (decl1))
10559 {
10560 /* If there is an external prototype declaration of this
10561 function, record its location but do not copy information
10562 to this decl. This may be an invisible declaration
10563 (built-in or in a scope which has finished) or simply
10564 have more refined argument types than any declaration
10565 found above. */
10566 struct c_binding *b;
10567 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10568 if (B_IN_SCOPE (b, external_scope))
10569 break;
10570 if (b)
10571 {
10572 tree ext_decl, ext_type;
10573 ext_decl = b->decl;
10574 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10575 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10576 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10577 TREE_TYPE (ext_type)))
10578 {
10579 current_function_prototype_locus
10580 = DECL_SOURCE_LOCATION (ext_decl);
10581 current_function_prototype_built_in
10582 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10583 current_function_prototype_arg_types
10584 = TYPE_ARG_TYPES (ext_type);
10585 }
10586 }
10587 }
10588 }
10589
10590 /* Optionally warn of old-fashioned def with no previous prototype. */
10591 if (warn_strict_prototypes
10592 && old_decl != error_mark_node
10593 && !prototype_p (TREE_TYPE (decl1))
10594 && C_DECL_ISNT_PROTOTYPE (old_decl))
10595 warning_at (loc, OPT_Wstrict_prototypes,
10596 "function declaration isn%'t a prototype");
10597 /* Optionally warn of any global def with no previous prototype. */
10598 else if (warn_missing_prototypes
10599 && old_decl != error_mark_node
10600 && TREE_PUBLIC (decl1)
10601 && !MAIN_NAME_P (DECL_NAME (decl1))
10602 && C_DECL_ISNT_PROTOTYPE (old_decl)
10603 && !DECL_DECLARED_INLINE_P (decl1))
10604 warning_at (loc, OPT_Wmissing_prototypes,
10605 "no previous prototype for %qD", decl1);
10606 /* Optionally warn of any def with no previous prototype
10607 if the function has already been used. */
10608 else if (warn_missing_prototypes
10609 && old_decl != NULL_TREE
10610 && old_decl != error_mark_node
10611 && TREE_USED (old_decl)
10612 && !prototype_p (TREE_TYPE (old_decl)))
10613 warning_at (loc, OPT_Wmissing_prototypes,
10614 "%qD was used with no prototype before its definition", decl1);
10615 /* Optionally warn of any global def with no previous declaration. */
10616 else if (warn_missing_declarations
10617 && TREE_PUBLIC (decl1)
10618 && old_decl == NULL_TREE
10619 && !MAIN_NAME_P (DECL_NAME (decl1))
10620 && !DECL_DECLARED_INLINE_P (decl1))
10621 warning_at (loc, OPT_Wmissing_declarations,
10622 "no previous declaration for %qD",
10623 decl1);
10624 /* Optionally warn of any def with no previous declaration
10625 if the function has already been used. */
10626 else if (warn_missing_declarations
10627 && old_decl != NULL_TREE
10628 && old_decl != error_mark_node
10629 && TREE_USED (old_decl)
10630 && C_DECL_IMPLICIT (old_decl))
10631 warning_at (loc, OPT_Wmissing_declarations,
10632 "%qD was used with no declaration before its definition", decl1);
10633
10634 /* This function exists in static storage.
10635 (This does not mean `static' in the C sense!) */
10636 TREE_STATIC (decl1) = 1;
10637
10638 /* This is the earliest point at which we might know the assembler
10639 name of the function. Thus, if it's set before this, die horribly. */
10640 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10641
10642 /* If #pragma weak was used, mark the decl weak now. */
10643 if (current_scope == file_scope)
10644 maybe_apply_pragma_weak (decl1);
10645
10646 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10647 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10648 {
10649 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10650 != integer_type_node)
10651 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10652 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10653 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10654 decl1);
10655
10656 check_main_parameter_types (decl: decl1);
10657
10658 if (!TREE_PUBLIC (decl1))
10659 pedwarn (loc, OPT_Wmain,
10660 "%qD is normally a non-static function", decl1);
10661 }
10662
10663 tree parms = current_function_arg_info->parms;
10664 if (old_decl)
10665 {
10666 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10667 warn_parm_array_mismatch (origloc, old_decl, parms);
10668 }
10669
10670 /* Record the decl so that the function name is defined.
10671 If we already have a decl for this name, and it is a FUNCTION_DECL,
10672 use the old decl. */
10673
10674 current_function_decl = pushdecl (x: decl1);
10675
10676 if (tree access = build_attr_access_from_parms (parms, false))
10677 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10678 old_decl);
10679
10680 push_scope ();
10681 declare_parm_level ();
10682
10683 /* Set the result decl source location to the location of the typespec. */
10684 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10685 ? loc : declspecs->locations[cdw_typespec]);
10686 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10687 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10688 DECL_ARTIFICIAL (resdecl) = 1;
10689 DECL_IGNORED_P (resdecl) = 1;
10690 DECL_RESULT (current_function_decl) = resdecl;
10691
10692 start_fname_decls ();
10693
10694 return true;
10695}
10696
10697/* Subroutine of store_parm_decls which handles new-style function
10698 definitions (prototype format). The parms already have decls, so we
10699 need only record them as in effect and complain if any redundant
10700 old-style parm decls were written. */
10701static void
10702store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10703{
10704 tree decl;
10705 c_arg_tag *tag;
10706 unsigned ix;
10707
10708 if (current_scope->bindings)
10709 {
10710 error_at (DECL_SOURCE_LOCATION (fndecl),
10711 "old-style parameter declarations in prototyped "
10712 "function definition");
10713
10714 /* Get rid of the old-style declarations. */
10715 pop_scope ();
10716 push_scope ();
10717 }
10718 /* Don't issue this warning for nested functions, and don't issue this
10719 warning if we got here because ARG_INFO_TYPES was error_mark_node
10720 (this happens when a function definition has just an ellipsis in
10721 its parameter list). */
10722 else if (!in_system_header_at (loc: input_location)
10723 && !current_function_scope
10724 && arg_info->types != error_mark_node)
10725 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10726 "traditional C rejects ISO C style function definitions");
10727
10728 /* Now make all the parameter declarations visible in the function body.
10729 We can bypass most of the grunt work of pushdecl. */
10730 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10731 {
10732 DECL_CONTEXT (decl) = current_function_decl;
10733 if (DECL_NAME (decl))
10734 {
10735 bind (DECL_NAME (decl), decl, scope: current_scope,
10736 /*invisible=*/false, /*nested=*/false,
10737 UNKNOWN_LOCATION);
10738 if (!TREE_USED (decl))
10739 warn_if_shadowing (new_decl: decl);
10740 }
10741 else
10742 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), opt: OPT_Wpedantic,
10743 "ISO C does not support omitting parameter names in "
10744 "function definitions before C23");
10745 }
10746
10747 /* Record the parameter list in the function declaration. */
10748 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10749
10750 /* Now make all the ancillary declarations visible, likewise. */
10751 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10752 {
10753 DECL_CONTEXT (decl) = current_function_decl;
10754 if (DECL_NAME (decl))
10755 bind (DECL_NAME (decl), decl, scope: current_scope,
10756 /*invisible=*/false,
10757 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10758 UNKNOWN_LOCATION);
10759 }
10760
10761 /* And all the tag declarations. */
10762 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10763 if (tag->id)
10764 bind (name: tag->id, decl: tag->type, scope: current_scope,
10765 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10766}
10767
10768/* Subroutine of store_parm_decls which handles old-style function
10769 definitions (separate parameter list and declarations). */
10770
10771static void
10772store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10773{
10774 struct c_binding *b;
10775 tree parm, decl, last;
10776 tree parmids = arg_info->parms;
10777 hash_set<tree> seen_args;
10778
10779 if (!in_system_header_at (loc: input_location))
10780 {
10781 if (flag_isoc23)
10782 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10783 OPT_Wold_style_definition, "old-style function definition");
10784 else
10785 warning_at (DECL_SOURCE_LOCATION (fndecl),
10786 OPT_Wold_style_definition,
10787 "old-style function definition");
10788 }
10789
10790 if (current_scope->had_vla_unspec)
10791 error ("%<[*]%> not allowed in other than function prototype scope");
10792
10793 /* Match each formal parameter name with its declaration. Save each
10794 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10795 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10796 {
10797 if (TREE_VALUE (parm) == NULL_TREE)
10798 {
10799 error_at (DECL_SOURCE_LOCATION (fndecl),
10800 "parameter name missing from parameter list");
10801 TREE_PURPOSE (parm) = NULL_TREE;
10802 continue;
10803 }
10804
10805 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10806 if (b && B_IN_CURRENT_SCOPE (b))
10807 {
10808 decl = b->decl;
10809 /* Skip erroneous parameters. */
10810 if (decl == error_mark_node)
10811 continue;
10812 /* If we got something other than a PARM_DECL it is an error. */
10813 if (TREE_CODE (decl) != PARM_DECL)
10814 {
10815 error_at (DECL_SOURCE_LOCATION (decl),
10816 "%qD declared as a non-parameter", decl);
10817 continue;
10818 }
10819 /* If the declaration is already marked, we have a duplicate
10820 name. Complain and ignore the duplicate. */
10821 else if (seen_args.contains (k: decl))
10822 {
10823 error_at (DECL_SOURCE_LOCATION (decl),
10824 "multiple parameters named %qD", decl);
10825 TREE_PURPOSE (parm) = NULL_TREE;
10826 continue;
10827 }
10828 /* If the declaration says "void", complain and turn it into
10829 an int. */
10830 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10831 {
10832 error_at (DECL_SOURCE_LOCATION (decl),
10833 "parameter %qD declared with void type", decl);
10834 TREE_TYPE (decl) = integer_type_node;
10835 DECL_ARG_TYPE (decl) = integer_type_node;
10836 layout_decl (decl, 0);
10837 }
10838 warn_if_shadowing (new_decl: decl);
10839 }
10840 /* If no declaration found, default to int. */
10841 else
10842 {
10843 /* FIXME diagnostics: This should be the location of the argument,
10844 not the FNDECL. E.g., for an old-style declaration
10845
10846 int f10(v) { blah; }
10847
10848 We should use the location of the V, not the F10.
10849 Unfortunately, the V is an IDENTIFIER_NODE which has no
10850 location. In the future we need locations for c_arg_info
10851 entries.
10852
10853 See gcc.dg/Wshadow-3.c for an example of this problem. */
10854 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10855 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10856 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10857 pushdecl (x: decl);
10858 warn_if_shadowing (new_decl: decl);
10859
10860 if (flag_isoc99)
10861 permerror_opt (DECL_SOURCE_LOCATION (decl),
10862 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10863 decl);
10864 else
10865 warning_at (DECL_SOURCE_LOCATION (decl),
10866 OPT_Wmissing_parameter_type,
10867 "type of %qD defaults to %<int%>", decl);
10868 }
10869
10870 TREE_PURPOSE (parm) = decl;
10871 seen_args.add (k: decl);
10872 }
10873
10874 /* Now examine the parms chain for incomplete declarations
10875 and declarations with no corresponding names. */
10876
10877 for (b = current_scope->bindings; b; b = b->prev)
10878 {
10879 parm = b->decl;
10880 if (TREE_CODE (parm) != PARM_DECL)
10881 continue;
10882
10883 if (TREE_TYPE (parm) != error_mark_node
10884 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10885 {
10886 error_at (DECL_SOURCE_LOCATION (parm),
10887 "parameter %qD has incomplete type", parm);
10888 TREE_TYPE (parm) = error_mark_node;
10889 }
10890
10891 if (!seen_args.contains (k: parm))
10892 {
10893 error_at (DECL_SOURCE_LOCATION (parm),
10894 "declaration for parameter %qD but no such parameter",
10895 parm);
10896
10897 /* Pretend the parameter was not missing.
10898 This gets us to a standard state and minimizes
10899 further error messages. */
10900 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10901 }
10902 }
10903
10904 /* Chain the declarations together in the order of the list of
10905 names. Store that chain in the function decl, replacing the
10906 list of names. Update the current scope to match. */
10907 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10908
10909 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10910 if (TREE_PURPOSE (parm))
10911 break;
10912 if (parm && TREE_PURPOSE (parm))
10913 {
10914 last = TREE_PURPOSE (parm);
10915 DECL_ARGUMENTS (fndecl) = last;
10916
10917 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10918 if (TREE_PURPOSE (parm))
10919 {
10920 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10921 last = TREE_PURPOSE (parm);
10922 }
10923 DECL_CHAIN (last) = NULL_TREE;
10924 }
10925
10926 /* If there was a previous prototype,
10927 set the DECL_ARG_TYPE of each argument according to
10928 the type previously specified, and report any mismatches. */
10929
10930 if (current_function_prototype_arg_types)
10931 {
10932 tree type;
10933 for (parm = DECL_ARGUMENTS (fndecl),
10934 type = current_function_prototype_arg_types;
10935 parm || (type != NULL_TREE
10936 && TREE_VALUE (type) != error_mark_node
10937 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10938 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10939 {
10940 if (parm == NULL_TREE
10941 || type == NULL_TREE
10942 || (TREE_VALUE (type) != error_mark_node
10943 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10944 {
10945 if (current_function_prototype_built_in)
10946 warning_at (DECL_SOURCE_LOCATION (fndecl),
10947 0, "number of arguments doesn%'t match "
10948 "built-in prototype");
10949 else
10950 {
10951 /* FIXME diagnostics: This should be the location of
10952 FNDECL, but there is bug when a prototype is
10953 declared inside function context, but defined
10954 outside of it (e.g., gcc.dg/pr15698-2.c). In
10955 which case FNDECL gets the location of the
10956 prototype, not the definition. */
10957 error_at (input_location,
10958 "number of arguments doesn%'t match prototype");
10959
10960 error_at (current_function_prototype_locus,
10961 "prototype declaration");
10962 }
10963 break;
10964 }
10965 /* Type for passing arg must be consistent with that
10966 declared for the arg. ISO C says we take the unqualified
10967 type for parameters declared with qualified type. */
10968 if (TREE_TYPE (parm) != error_mark_node
10969 && TREE_VALUE (type) != error_mark_node
10970 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10971 != TYPE_ATOMIC (TREE_VALUE (type)))
10972 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10973 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10974 {
10975 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10976 == TYPE_ATOMIC (TREE_VALUE (type)))
10977 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10978 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10979 {
10980 /* Adjust argument to match prototype. E.g. a previous
10981 `int foo(float);' prototype causes
10982 `int foo(x) float x; {...}' to be treated like
10983 `int foo(float x) {...}'. This is particularly
10984 useful for argument types like uid_t. */
10985 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10986
10987 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10988 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10989 && (TYPE_PRECISION (TREE_TYPE (parm))
10990 < TYPE_PRECISION (integer_type_node)))
10991 DECL_ARG_TYPE (parm)
10992 = c_type_promotes_to (TREE_TYPE (parm));
10993
10994 /* ??? Is it possible to get here with a
10995 built-in prototype or will it always have
10996 been diagnosed as conflicting with an
10997 old-style definition and discarded? */
10998 if (current_function_prototype_built_in)
10999 warning_at (DECL_SOURCE_LOCATION (parm),
11000 OPT_Wpedantic, "promoted argument %qD "
11001 "doesn%'t match built-in prototype", parm);
11002 else
11003 {
11004 pedwarn (DECL_SOURCE_LOCATION (parm),
11005 OPT_Wpedantic, "promoted argument %qD "
11006 "doesn%'t match prototype", parm);
11007 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11008 "prototype declaration");
11009 }
11010 }
11011 else
11012 {
11013 if (current_function_prototype_built_in)
11014 warning_at (DECL_SOURCE_LOCATION (parm),
11015 0, "argument %qD doesn%'t match "
11016 "built-in prototype", parm);
11017 else
11018 {
11019 error_at (DECL_SOURCE_LOCATION (parm),
11020 "argument %qD doesn%'t match prototype", parm);
11021 error_at (current_function_prototype_locus,
11022 "prototype declaration");
11023 }
11024 }
11025 }
11026 }
11027 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11028 }
11029
11030 /* Otherwise, create a prototype that would match. */
11031
11032 else
11033 {
11034 tree actual = NULL_TREE, last = NULL_TREE, type;
11035
11036 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11037 {
11038 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11039 if (last)
11040 TREE_CHAIN (last) = type;
11041 else
11042 actual = type;
11043 last = type;
11044 }
11045 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11046 if (last)
11047 TREE_CHAIN (last) = type;
11048 else
11049 actual = type;
11050
11051 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11052 of the type of this function, but we need to avoid having this
11053 affect the types of other similarly-typed functions, so we must
11054 first force the generation of an identical (but separate) type
11055 node for the relevant function type. The new node we create
11056 will be a variant of the main variant of the original function
11057 type. */
11058
11059 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11060
11061 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11062 }
11063}
11064
11065/* Store parameter declarations passed in ARG_INFO into the current
11066 function declaration. */
11067
11068void
11069store_parm_decls_from (struct c_arg_info *arg_info)
11070{
11071 current_function_arg_info = arg_info;
11072 store_parm_decls ();
11073}
11074
11075/* Called by walk_tree to look for and update context-less labels
11076 or labels with context in the parent function. */
11077
11078static tree
11079set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11080{
11081 tree ctx = static_cast<tree>(data);
11082 if (TREE_CODE (*tp) == LABEL_EXPR
11083 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11084 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11085 {
11086 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11087 *walk_subtrees = 0;
11088 }
11089
11090 return NULL_TREE;
11091}
11092
11093/* Store the parameter declarations into the current function declaration.
11094 This is called after parsing the parameter declarations, before
11095 digesting the body of the function.
11096
11097 For an old-style definition, construct a prototype out of the old-style
11098 parameter declarations and inject it into the function's type. */
11099
11100void
11101store_parm_decls (void)
11102{
11103 tree fndecl = current_function_decl;
11104 bool proto;
11105
11106 /* The argument information block for FNDECL. */
11107 struct c_arg_info *arg_info = current_function_arg_info;
11108 current_function_arg_info = 0;
11109
11110 /* True if this definition is written with a prototype. In C23, an
11111 empty argument list was converted to (void) in grokparms; in
11112 older C standard versions, it does not give the function a type
11113 with a prototype for future calls. */
11114 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11115
11116 if (proto)
11117 store_parm_decls_newstyle (fndecl, arg_info);
11118 else
11119 store_parm_decls_oldstyle (fndecl, arg_info);
11120
11121 /* The next call to push_scope will be a function body. */
11122
11123 next_is_function_body = true;
11124
11125 /* Write a record describing this function definition to the prototypes
11126 file (if requested). */
11127
11128 gen_aux_info_record (fndecl, 1, 0, proto);
11129
11130 /* Initialize the RTL code for the function. */
11131 allocate_struct_function (fndecl, false);
11132
11133 if (warn_unused_local_typedefs)
11134 cfun->language = ggc_cleared_alloc<language_function> ();
11135
11136 /* Begin the statement tree for this function. */
11137 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11138
11139 /* ??? Insert the contents of the pending sizes list into the function
11140 to be evaluated. The only reason left to have this is
11141 void foo(int n, int array[n++])
11142 because we throw away the array type in favor of a pointer type, and
11143 thus won't naturally see the SAVE_EXPR containing the increment. All
11144 other pending sizes would be handled by gimplify_parameters. */
11145 if (arg_info->pending_sizes)
11146 {
11147 /* In very special circumstances, e.g. for code like
11148 _Atomic int i = 5;
11149 void f (int a[i += 2]) {}
11150 we need to execute the atomic assignment on function entry.
11151 But in this case, it is not just a straight store, it has the
11152 op= form, which means that build_atomic_assign has generated
11153 gotos, labels, etc. Because at that time the function decl
11154 for F has not been created yet, those labels do not have any
11155 function context. But we have the fndecl now, so update the
11156 labels accordingly. gimplify_expr would crash otherwise.
11157 Or with nested functions the labels could be created with parent
11158 function's context, while when the statement is emitted at the
11159 start of the nested function, it needs the nested function's
11160 context. */
11161 walk_tree_without_duplicates (&arg_info->pending_sizes,
11162 set_labels_context_r, fndecl);
11163 add_stmt (t: arg_info->pending_sizes);
11164 }
11165}
11166
11167/* Store PARM_DECLs in PARMS into scope temporarily. Used for
11168 c_finish_omp_declare_simd for function prototypes. No diagnostics
11169 should be done. */
11170
11171void
11172temp_store_parm_decls (tree fndecl, tree parms)
11173{
11174 push_scope ();
11175 for (tree p = parms; p; p = DECL_CHAIN (p))
11176 {
11177 DECL_CONTEXT (p) = fndecl;
11178 if (DECL_NAME (p))
11179 bind (DECL_NAME (p), decl: p, scope: current_scope,
11180 /*invisible=*/false, /*nested=*/false,
11181 UNKNOWN_LOCATION);
11182 }
11183}
11184
11185/* Undo what temp_store_parm_decls did. */
11186
11187void
11188temp_pop_parm_decls (void)
11189{
11190 /* Clear all bindings in this temporary scope, so that
11191 pop_scope doesn't create a BLOCK. */
11192 struct c_binding *b = current_scope->bindings;
11193 current_scope->bindings = NULL;
11194 for (; b; b = free_binding_and_advance (b))
11195 {
11196 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11197 || b->decl == error_mark_node);
11198 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11199 I_SYMBOL_BINDING (b->id) = b->shadowed;
11200 if (b->shadowed && b->shadowed->u.type)
11201 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11202 }
11203 pop_scope ();
11204}
11205
11206
11207/* Finish up a function declaration and compile that function
11208 all the way to assembler language output. Then free the storage
11209 for the function definition.
11210
11211 This is called after parsing the body of the function definition. */
11212
11213void
11214finish_function (location_t end_loc)
11215{
11216 tree fndecl = current_function_decl;
11217
11218 if (c_dialect_objc ())
11219 objc_finish_function ();
11220
11221 if (TREE_CODE (fndecl) == FUNCTION_DECL
11222 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
11223 {
11224 tree args = DECL_ARGUMENTS (fndecl);
11225 for (; args; args = DECL_CHAIN (args))
11226 {
11227 tree type = TREE_TYPE (args);
11228 if (INTEGRAL_TYPE_P (type)
11229 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11230 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
11231 }
11232 }
11233
11234 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11235 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11236
11237 /* Must mark the RESULT_DECL as being in this function. */
11238
11239 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11240 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11241
11242 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11243 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11244 == integer_type_node && flag_isoc99)
11245 {
11246 /* Hack. We don't want the middle-end to warn that this return
11247 is unreachable, so we mark its location as special. Using
11248 UNKNOWN_LOCATION has the problem that it gets clobbered in
11249 annotate_one_with_locus. A cleaner solution might be to
11250 ensure ! should_carry_locus_p (stmt), but that needs a flag.
11251 */
11252 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11253 }
11254
11255 /* Tie off the statement tree for this function. */
11256 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11257
11258 finish_fname_decls ();
11259
11260 /* Complain if there's no return statement only if option specified on
11261 command line. */
11262 if (warn_return_type > 0
11263 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11264 && !current_function_returns_value && !current_function_returns_null
11265 /* Don't complain if we are no-return. */
11266 && !current_function_returns_abnormally
11267 /* Don't complain if we are declared noreturn. */
11268 && !TREE_THIS_VOLATILE (fndecl)
11269 /* Don't warn for main(). */
11270 && !MAIN_NAME_P (DECL_NAME (fndecl))
11271 /* Or if they didn't actually specify a return type. */
11272 && !C_FUNCTION_IMPLICIT_INT (fndecl)
11273 /* Normally, with -Wreturn-type, flow will complain, but we might
11274 optimize out static functions. */
11275 && !TREE_PUBLIC (fndecl)
11276 && targetm.warn_func_return (fndecl)
11277 && warning (OPT_Wreturn_type,
11278 "no return statement in function returning non-void"))
11279 suppress_warning (fndecl, OPT_Wreturn_type);
11280
11281 /* Complain about parameters that are only set, but never otherwise used. */
11282 if (warn_unused_but_set_parameter)
11283 {
11284 tree decl;
11285
11286 for (decl = DECL_ARGUMENTS (fndecl);
11287 decl;
11288 decl = DECL_CHAIN (decl))
11289 if (TREE_USED (decl)
11290 && TREE_CODE (decl) == PARM_DECL
11291 && !DECL_READ_P (decl)
11292 && DECL_NAME (decl)
11293 && !DECL_ARTIFICIAL (decl)
11294 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
11295 warning_at (DECL_SOURCE_LOCATION (decl),
11296 OPT_Wunused_but_set_parameter,
11297 "parameter %qD set but not used", decl);
11298 }
11299
11300 /* Complain about locally defined typedefs that are not used in this
11301 function. */
11302 maybe_warn_unused_local_typedefs ();
11303
11304 /* Possibly warn about unused parameters. */
11305 if (warn_unused_parameter)
11306 do_warn_unused_parameter (fndecl);
11307
11308 /* Store the end of the function, so that we get good line number
11309 info for the epilogue. */
11310 cfun->function_end_locus = end_loc;
11311
11312 /* Finalize the ELF visibility for the function. */
11313 c_determine_visibility (fndecl);
11314
11315 /* For GNU C extern inline functions disregard inline limits. */
11316 if (DECL_EXTERNAL (fndecl)
11317 && DECL_DECLARED_INLINE_P (fndecl)
11318 && (flag_gnu89_inline
11319 || lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (fndecl))))
11320 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11321
11322 /* Genericize before inlining. Delay genericizing nested functions
11323 until their parent function is genericized. Since finalizing
11324 requires GENERIC, delay that as well. */
11325
11326 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11327 && !undef_nested_function)
11328 {
11329 if (!decl_function_context (fndecl))
11330 {
11331 invoke_plugin_callbacks (event: PLUGIN_PRE_GENERICIZE, gcc_data: fndecl);
11332 c_genericize (fndecl);
11333
11334 /* ??? Objc emits functions after finalizing the compilation unit.
11335 This should be cleaned up later and this conditional removed. */
11336 if (symtab->global_info_ready)
11337 {
11338 cgraph_node::add_new_function (fndecl, lowered: false);
11339 return;
11340 }
11341 cgraph_node::finalize_function (fndecl, false);
11342 }
11343 else
11344 {
11345 /* Register this function with cgraph just far enough to get it
11346 added to our parent's nested function list. Handy, since the
11347 C front end doesn't have such a list. */
11348 (void) cgraph_node::get_create (fndecl);
11349 }
11350 }
11351
11352 if (!decl_function_context (fndecl))
11353 undef_nested_function = false;
11354
11355 if (cfun->language != NULL)
11356 {
11357 ggc_free (cfun->language);
11358 cfun->language = NULL;
11359 }
11360
11361 /* We're leaving the context of this function, so zap cfun.
11362 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11363 tree_rest_of_compilation. */
11364 set_cfun (NULL);
11365 invoke_plugin_callbacks (event: PLUGIN_FINISH_PARSE_FUNCTION, gcc_data: current_function_decl);
11366 current_function_decl = NULL;
11367}
11368
11369/* Check the declarations given in a for-loop for satisfying the C99
11370 constraints. If exactly one such decl is found, return it. LOC is
11371 the location of the opening parenthesis of the for loop. The last
11372 parameter allows you to control the "for loop initial declarations
11373 are only allowed in C99 mode". Normally, you should pass
11374 flag_isoc99 as that parameter. But in some cases (Objective-C
11375 foreach loop, for example) we want to run the checks in this
11376 function even if not in C99 mode, so we allow the caller to turn
11377 off the error about not being in C99 mode.
11378*/
11379
11380tree
11381check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11382{
11383 struct c_binding *b;
11384 tree one_decl = NULL_TREE;
11385 int n_decls = 0;
11386
11387 if (!turn_off_iso_c99_error)
11388 {
11389 static bool hint = true;
11390 /* If we get here, declarations have been used in a for loop without
11391 the C99 for loop scope. This doesn't make much sense, so don't
11392 allow it. */
11393 auto_diagnostic_group d;
11394 error_at (loc, "%<for%> loop initial declarations "
11395 "are only allowed in C99 or C11 mode");
11396 if (hint)
11397 {
11398 inform (loc,
11399 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11400 "%<-std=gnu11%> to compile your code");
11401 hint = false;
11402 }
11403 return NULL_TREE;
11404 }
11405 else
11406 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11407 "initial declarations");
11408
11409 /* C99 subclause 6.8.5 paragraph 3:
11410
11411 [#3] The declaration part of a for statement shall only
11412 declare identifiers for objects having storage class auto or
11413 register.
11414
11415 It isn't clear whether, in this sentence, "identifiers" binds to
11416 "shall only declare" or to "objects" - that is, whether all identifiers
11417 declared must be identifiers for objects, or whether the restriction
11418 only applies to those that are. (A question on this in comp.std.c
11419 in November 2000 received no answer.) We implement the strictest
11420 interpretation, to avoid creating an extension which later causes
11421 problems.
11422
11423 This constraint was removed in C23. */
11424
11425 for (b = current_scope->bindings; b; b = b->prev)
11426 {
11427 tree id = b->id;
11428 tree decl = b->decl;
11429
11430 if (!id)
11431 continue;
11432
11433 switch (TREE_CODE (decl))
11434 {
11435 case VAR_DECL:
11436 {
11437 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11438 if (TREE_STATIC (decl))
11439 pedwarn_c11 (decl_loc, opt: OPT_Wpedantic,
11440 "declaration of static variable %qD in %<for%> "
11441 "loop initial declaration", decl);
11442 else if (DECL_EXTERNAL (decl))
11443 pedwarn_c11 (decl_loc, opt: OPT_Wpedantic,
11444 "declaration of %<extern%> variable %qD in %<for%> "
11445 "loop initial declaration", decl);
11446 }
11447 break;
11448
11449 case RECORD_TYPE:
11450 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11451 "%<struct %E%> declared in %<for%> loop initial "
11452 "declaration", id);
11453 break;
11454 case UNION_TYPE:
11455 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11456 "%<union %E%> declared in %<for%> loop initial "
11457 "declaration",
11458 id);
11459 break;
11460 case ENUMERAL_TYPE:
11461 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11462 "%<enum %E%> declared in %<for%> loop "
11463 "initial declaration", id);
11464 break;
11465 default:
11466 pedwarn_c11 (loc, opt: OPT_Wpedantic, "declaration of non-variable "
11467 "%qD in %<for%> loop initial declaration", decl);
11468 }
11469
11470 n_decls++;
11471 one_decl = decl;
11472 }
11473
11474 return n_decls == 1 ? one_decl : NULL_TREE;
11475}
11476
11477/* Save and reinitialize the variables
11478 used during compilation of a C function. */
11479
11480void
11481c_push_function_context (void)
11482{
11483 struct language_function *p = cfun->language;
11484 /* cfun->language might have been already allocated by the use of
11485 -Wunused-local-typedefs. In that case, just re-use it. */
11486 if (p == NULL)
11487 cfun->language = p = ggc_cleared_alloc<language_function> ();
11488
11489 p->base.x_stmt_tree = c_stmt_tree;
11490 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (src: c_stmt_tree.x_cur_stmt_list);
11491 p->x_in_statement = in_statement;
11492 p->x_switch_stack = c_switch_stack;
11493 p->arg_info = current_function_arg_info;
11494 p->returns_value = current_function_returns_value;
11495 p->returns_null = current_function_returns_null;
11496 p->returns_abnormally = current_function_returns_abnormally;
11497 p->warn_about_return_type = warn_about_return_type;
11498
11499 push_function_context ();
11500}
11501
11502/* Restore the variables used during compilation of a C function. */
11503
11504void
11505c_pop_function_context (void)
11506{
11507 struct language_function *p;
11508
11509 pop_function_context ();
11510 p = cfun->language;
11511
11512 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11513 used to store data throughout the life time of the current cfun,
11514 So don't deallocate it. */
11515 if (!warn_unused_local_typedefs)
11516 cfun->language = NULL;
11517
11518 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11519 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11520 {
11521 /* Stop pointing to the local nodes about to be freed. */
11522 /* But DECL_INITIAL must remain nonzero so we know this
11523 was an actual function definition. */
11524 DECL_INITIAL (current_function_decl) = error_mark_node;
11525 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11526 }
11527
11528 c_stmt_tree = p->base.x_stmt_tree;
11529 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11530 in_statement = p->x_in_statement;
11531 c_switch_stack = p->x_switch_stack;
11532 current_function_arg_info = p->arg_info;
11533 current_function_returns_value = p->returns_value;
11534 current_function_returns_null = p->returns_null;
11535 current_function_returns_abnormally = p->returns_abnormally;
11536 warn_about_return_type = p->warn_about_return_type;
11537}
11538
11539/* The functions below are required for functionality of doing
11540 function at once processing in the C front end. Currently these
11541 functions are not called from anywhere in the C front end, but as
11542 these changes continue, that will change. */
11543
11544/* Returns the stmt_tree (if any) to which statements are currently
11545 being added. If there is no active statement-tree, NULL is
11546 returned. */
11547
11548stmt_tree
11549current_stmt_tree (void)
11550{
11551 return &c_stmt_tree;
11552}
11553
11554/* Return the global value of T as a symbol. */
11555
11556tree
11557identifier_global_value (tree t)
11558{
11559 struct c_binding *b;
11560
11561 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11562 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11563 return b->decl;
11564
11565 return NULL_TREE;
11566}
11567
11568/* Return the global value of tag T as a symbol. */
11569
11570tree
11571identifier_global_tag (tree t)
11572{
11573 struct c_binding *b;
11574
11575 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11576 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11577 return b->decl;
11578
11579 return NULL_TREE;
11580}
11581
11582/* Returns true if NAME refers to a built-in function or function-like
11583 operator. */
11584
11585bool
11586names_builtin_p (const char *name)
11587{
11588 tree id = get_identifier (name);
11589 if (tree decl = identifier_global_value (t: id))
11590 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11591
11592 /* Also detect common reserved C words that aren't strictly built-in
11593 functions. */
11594 switch (C_RID_CODE (id))
11595 {
11596 case RID_BUILTIN_ASSOC_BARRIER:
11597 case RID_BUILTIN_CONVERTVECTOR:
11598 case RID_BUILTIN_HAS_ATTRIBUTE:
11599 case RID_BUILTIN_SHUFFLE:
11600 case RID_BUILTIN_SHUFFLEVECTOR:
11601 case RID_BUILTIN_STDC:
11602 case RID_CHOOSE_EXPR:
11603 case RID_OFFSETOF:
11604 case RID_TYPES_COMPATIBLE_P:
11605 return true;
11606 default:
11607 break;
11608 }
11609
11610 return false;
11611}
11612
11613/* In C, the only C-linkage public declaration is at file scope. */
11614
11615tree
11616c_linkage_bindings (tree name)
11617{
11618 return identifier_global_value (t: name);
11619}
11620
11621/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11622 otherwise the name is found in ridpointers from RID_INDEX. */
11623
11624void
11625record_builtin_type (enum rid rid_index, const char *name, tree type)
11626{
11627 tree id, decl;
11628 if (name == 0)
11629 id = ridpointers[(int) rid_index];
11630 else
11631 id = get_identifier (name);
11632 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11633 pushdecl (x: decl);
11634 if (debug_hooks->type_decl)
11635 debug_hooks->type_decl (decl, false);
11636}
11637
11638/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11639
11640struct c_parm *
11641build_c_parm (struct c_declspecs *specs, tree attrs,
11642 struct c_declarator *declarator,
11643 location_t loc)
11644{
11645 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11646 ret->specs = specs;
11647 ret->attrs = attrs;
11648 ret->declarator = declarator;
11649 ret->loc = loc;
11650 return ret;
11651}
11652
11653/* Return a declarator with nested attributes. TARGET is the inner
11654 declarator to which these attributes apply. ATTRS are the
11655 attributes. */
11656
11657struct c_declarator *
11658build_attrs_declarator (tree attrs, struct c_declarator *target)
11659{
11660 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11661 ret->kind = cdk_attrs;
11662 ret->declarator = target;
11663 ret->u.attrs = attrs;
11664 return ret;
11665}
11666
11667/* Return a declarator for a function with arguments specified by ARGS
11668 and return type specified by TARGET. */
11669
11670struct c_declarator *
11671build_function_declarator (struct c_arg_info *args,
11672 struct c_declarator *target)
11673{
11674 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11675 ret->kind = cdk_function;
11676 ret->declarator = target;
11677 ret->u.arg_info = args;
11678 return ret;
11679}
11680
11681/* Return a declarator for the identifier IDENT (which may be
11682 NULL_TREE for an abstract declarator). */
11683
11684struct c_declarator *
11685build_id_declarator (tree ident)
11686{
11687 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11688 ret->kind = cdk_id;
11689 ret->declarator = 0;
11690 ret->u.id.id = ident;
11691 ret->u.id.attrs = NULL_TREE;
11692 /* Default value - may get reset to a more precise location. */
11693 ret->id_loc = input_location;
11694 return ret;
11695}
11696
11697/* Return something to represent absolute declarators containing a *.
11698 TARGET is the absolute declarator that the * contains.
11699 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11700 to apply to the pointer type. */
11701
11702struct c_declarator *
11703make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11704 struct c_declarator *target)
11705{
11706 tree attrs;
11707 int quals = 0;
11708 struct c_declarator *itarget = target;
11709 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11710 if (type_quals_attrs)
11711 {
11712 attrs = type_quals_attrs->attrs;
11713 quals = quals_from_declspecs (specs: type_quals_attrs);
11714 if (attrs != NULL_TREE)
11715 itarget = build_attrs_declarator (attrs, target);
11716 }
11717 ret->kind = cdk_pointer;
11718 ret->declarator = itarget;
11719 ret->u.pointer_quals = quals;
11720 return ret;
11721}
11722
11723/* Return a pointer to a structure for an empty list of declaration
11724 specifiers. */
11725
11726struct c_declspecs *
11727build_null_declspecs (void)
11728{
11729 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11730 memset (s: ret, c: 0, n: sizeof *ret);
11731 ret->align_log = -1;
11732 ret->typespec_word = cts_none;
11733 ret->storage_class = csc_none;
11734 ret->expr_const_operands = true;
11735 ret->typespec_kind = ctsk_none;
11736 ret->address_space = ADDR_SPACE_GENERIC;
11737 return ret;
11738}
11739
11740/* Add the address space ADDRSPACE to the declaration specifiers
11741 SPECS, returning SPECS. */
11742
11743struct c_declspecs *
11744declspecs_add_addrspace (location_t location,
11745 struct c_declspecs *specs, addr_space_t as)
11746{
11747 specs->non_sc_seen_p = true;
11748 specs->declspecs_seen_p = true;
11749 specs->non_std_attrs_seen_p = true;
11750
11751 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11752 && specs->address_space != as)
11753 error ("incompatible address space qualifiers %qs and %qs",
11754 c_addr_space_name (as),
11755 c_addr_space_name (as: specs->address_space));
11756 else
11757 {
11758 specs->address_space = as;
11759 specs->locations[cdw_address_space] = location;
11760 }
11761 return specs;
11762}
11763
11764/* Add the type qualifier QUAL to the declaration specifiers SPECS,
11765 returning SPECS. */
11766
11767struct c_declspecs *
11768declspecs_add_qual (location_t loc,
11769 struct c_declspecs *specs, tree qual)
11770{
11771 enum rid i;
11772 bool dupe = false;
11773 specs->non_sc_seen_p = true;
11774 specs->declspecs_seen_p = true;
11775 specs->non_std_attrs_seen_p = true;
11776 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11777 && C_IS_RESERVED_WORD (qual));
11778 i = C_RID_CODE (qual);
11779 location_t prev_loc = UNKNOWN_LOCATION;
11780 switch (i)
11781 {
11782 case RID_CONST:
11783 dupe = specs->const_p;
11784 specs->const_p = true;
11785 prev_loc = specs->locations[cdw_const];
11786 specs->locations[cdw_const] = loc;
11787 break;
11788 case RID_VOLATILE:
11789 dupe = specs->volatile_p;
11790 specs->volatile_p = true;
11791 prev_loc = specs->locations[cdw_volatile];
11792 specs->locations[cdw_volatile] = loc;
11793 break;
11794 case RID_RESTRICT:
11795 dupe = specs->restrict_p;
11796 specs->restrict_p = true;
11797 prev_loc = specs->locations[cdw_restrict];
11798 specs->locations[cdw_restrict] = loc;
11799 break;
11800 case RID_ATOMIC:
11801 dupe = specs->atomic_p;
11802 specs->atomic_p = true;
11803 prev_loc = specs->locations[cdw_atomic];
11804 specs->locations[cdw_atomic] = loc;
11805 break;
11806 default:
11807 gcc_unreachable ();
11808 }
11809 if (dupe)
11810 {
11811 bool warned = pedwarn_c90 (loc, opt: OPT_Wpedantic,
11812 "duplicate %qE declaration specifier", qual);
11813 if (!warned
11814 && warn_duplicate_decl_specifier
11815 && prev_loc >= RESERVED_LOCATION_COUNT
11816 && !from_macro_expansion_at (loc: prev_loc)
11817 && !from_macro_expansion_at (loc))
11818 warning_at (loc, OPT_Wduplicate_decl_specifier,
11819 "duplicate %qE declaration specifier", qual);
11820 }
11821 return specs;
11822}
11823
11824/* Add the type specifier TYPE to the declaration specifiers SPECS,
11825 returning SPECS. */
11826
11827struct c_declspecs *
11828declspecs_add_type (location_t loc, struct c_declspecs *specs,
11829 struct c_typespec spec)
11830{
11831 tree type = spec.spec;
11832 specs->non_sc_seen_p = true;
11833 specs->declspecs_seen_p = true;
11834 specs->non_std_attrs_seen_p = true;
11835 specs->typespec_kind = spec.kind;
11836 if (TREE_DEPRECATED (type))
11837 specs->deprecated_p = true;
11838 if (TREE_UNAVAILABLE (type))
11839 specs->unavailable_p = true;
11840
11841 /* As a type specifier is present, "auto" must be used as a storage
11842 class specifier, not for type deduction. */
11843 if (specs->c23_auto_p)
11844 {
11845 specs->c23_auto_p = false;
11846 if (specs->storage_class != csc_none)
11847 error ("multiple storage classes in declaration specifiers");
11848 else if (specs->thread_p)
11849 error ("%qs used with %<auto%>",
11850 specs->thread_gnu_p ? "__thread" : "_Thread_local");
11851 else if (specs->constexpr_p)
11852 /* auto may only be used with another storage class specifier,
11853 such as constexpr, if the type is inferred. */
11854 error ("%<auto%> used with %<constexpr%>");
11855 else
11856 specs->storage_class = csc_auto;
11857 }
11858
11859 /* Handle type specifier keywords. */
11860 if (TREE_CODE (type) == IDENTIFIER_NODE
11861 && C_IS_RESERVED_WORD (type)
11862 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11863 {
11864 enum rid i = C_RID_CODE (type);
11865 if (specs->type)
11866 {
11867 error_at (loc, "two or more data types in declaration specifiers");
11868 return specs;
11869 }
11870 if ((int) i <= (int) RID_LAST_MODIFIER)
11871 {
11872 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11873 bool dupe = false;
11874 switch (i)
11875 {
11876 case RID_LONG:
11877 if (specs->long_long_p)
11878 {
11879 error_at (loc, "%<long long long%> is too long for GCC");
11880 break;
11881 }
11882 if (specs->long_p)
11883 {
11884 if (specs->typespec_word == cts_double)
11885 {
11886 error_at (loc,
11887 ("both %<long long%> and %<double%> in "
11888 "declaration specifiers"));
11889 break;
11890 }
11891 pedwarn_c90 (loc, opt: OPT_Wlong_long,
11892 "ISO C90 does not support %<long long%>");
11893 specs->long_long_p = 1;
11894 specs->locations[cdw_long_long] = loc;
11895 break;
11896 }
11897 if (specs->short_p)
11898 error_at (loc,
11899 ("both %<long%> and %<short%> in "
11900 "declaration specifiers"));
11901 else if (specs->typespec_word == cts_auto_type)
11902 error_at (loc,
11903 ("both %<long%> and %<__auto_type%> in "
11904 "declaration specifiers"));
11905 else if (specs->typespec_word == cts_void)
11906 error_at (loc,
11907 ("both %<long%> and %<void%> in "
11908 "declaration specifiers"));
11909 else if (specs->typespec_word == cts_int_n)
11910 error_at (loc,
11911 ("both %<long%> and %<__int%d%> in "
11912 "declaration specifiers"),
11913 int_n_data[specs->u.int_n_idx].bitsize);
11914 else if (specs->typespec_word == cts_bool)
11915 error_at (loc,
11916 ("both %<long%> and %<_Bool%> in "
11917 "declaration specifiers"));
11918 else if (specs->typespec_word == cts_bitint)
11919 error_at (loc,
11920 ("both %<long%> and %<_BitInt%> in "
11921 "declaration specifiers"));
11922 else if (specs->typespec_word == cts_char)
11923 error_at (loc,
11924 ("both %<long%> and %<char%> in "
11925 "declaration specifiers"));
11926 else if (specs->typespec_word == cts_float)
11927 error_at (loc,
11928 ("both %<long%> and %<float%> in "
11929 "declaration specifiers"));
11930 else if (specs->typespec_word == cts_floatn_nx)
11931 error_at (loc,
11932 ("both %<long%> and %<_Float%d%s%> in "
11933 "declaration specifiers"),
11934 floatn_nx_types[specs->u.floatn_nx_idx].n,
11935 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11936 ? "x"
11937 : ""));
11938 else if (specs->typespec_word == cts_dfloat32)
11939 error_at (loc,
11940 ("both %<long%> and %<_Decimal32%> in "
11941 "declaration specifiers"));
11942 else if (specs->typespec_word == cts_dfloat64)
11943 error_at (loc,
11944 ("both %<long%> and %<_Decimal64%> in "
11945 "declaration specifiers"));
11946 else if (specs->typespec_word == cts_dfloat128)
11947 error_at (loc,
11948 ("both %<long%> and %<_Decimal128%> in "
11949 "declaration specifiers"));
11950 else
11951 {
11952 specs->long_p = true;
11953 specs->locations[cdw_long] = loc;
11954 }
11955 break;
11956 case RID_SHORT:
11957 dupe = specs->short_p;
11958 if (specs->long_p)
11959 error_at (loc,
11960 ("both %<long%> and %<short%> in "
11961 "declaration specifiers"));
11962 else if (specs->typespec_word == cts_auto_type)
11963 error_at (loc,
11964 ("both %<short%> and %<__auto_type%> in "
11965 "declaration specifiers"));
11966 else if (specs->typespec_word == cts_void)
11967 error_at (loc,
11968 ("both %<short%> and %<void%> in "
11969 "declaration specifiers"));
11970 else if (specs->typespec_word == cts_int_n)
11971 error_at (loc,
11972 ("both %<short%> and %<__int%d%> in "
11973 "declaration specifiers"),
11974 int_n_data[specs->u.int_n_idx].bitsize);
11975 else if (specs->typespec_word == cts_bool)
11976 error_at (loc,
11977 ("both %<short%> and %<_Bool%> in "
11978 "declaration specifiers"));
11979 else if (specs->typespec_word == cts_bitint)
11980 error_at (loc,
11981 ("both %<short%> and %<_BitInt%> in "
11982 "declaration specifiers"));
11983 else if (specs->typespec_word == cts_char)
11984 error_at (loc,
11985 ("both %<short%> and %<char%> in "
11986 "declaration specifiers"));
11987 else if (specs->typespec_word == cts_float)
11988 error_at (loc,
11989 ("both %<short%> and %<float%> in "
11990 "declaration specifiers"));
11991 else if (specs->typespec_word == cts_double)
11992 error_at (loc,
11993 ("both %<short%> and %<double%> in "
11994 "declaration specifiers"));
11995 else if (specs->typespec_word == cts_floatn_nx)
11996 error_at (loc,
11997 ("both %<short%> and %<_Float%d%s%> in "
11998 "declaration specifiers"),
11999 floatn_nx_types[specs->u.floatn_nx_idx].n,
12000 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12001 ? "x"
12002 : ""));
12003 else if (specs->typespec_word == cts_dfloat32)
12004 error_at (loc,
12005 ("both %<short%> and %<_Decimal32%> in "
12006 "declaration specifiers"));
12007 else if (specs->typespec_word == cts_dfloat64)
12008 error_at (loc,
12009 ("both %<short%> and %<_Decimal64%> in "
12010 "declaration specifiers"));
12011 else if (specs->typespec_word == cts_dfloat128)
12012 error_at (loc,
12013 ("both %<short%> and %<_Decimal128%> in "
12014 "declaration specifiers"));
12015 else
12016 {
12017 specs->short_p = true;
12018 specs->locations[cdw_short] = loc;
12019 }
12020 break;
12021 case RID_SIGNED:
12022 dupe = specs->signed_p;
12023 if (specs->unsigned_p)
12024 error_at (loc,
12025 ("both %<signed%> and %<unsigned%> in "
12026 "declaration specifiers"));
12027 else if (specs->typespec_word == cts_auto_type)
12028 error_at (loc,
12029 ("both %<signed%> and %<__auto_type%> in "
12030 "declaration specifiers"));
12031 else if (specs->typespec_word == cts_void)
12032 error_at (loc,
12033 ("both %<signed%> and %<void%> in "
12034 "declaration specifiers"));
12035 else if (specs->typespec_word == cts_bool)
12036 error_at (loc,
12037 ("both %<signed%> and %<_Bool%> in "
12038 "declaration specifiers"));
12039 else if (specs->typespec_word == cts_float)
12040 error_at (loc,
12041 ("both %<signed%> and %<float%> in "
12042 "declaration specifiers"));
12043 else if (specs->typespec_word == cts_double)
12044 error_at (loc,
12045 ("both %<signed%> and %<double%> in "
12046 "declaration specifiers"));
12047 else if (specs->typespec_word == cts_floatn_nx)
12048 error_at (loc,
12049 ("both %<signed%> and %<_Float%d%s%> in "
12050 "declaration specifiers"),
12051 floatn_nx_types[specs->u.floatn_nx_idx].n,
12052 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12053 ? "x"
12054 : ""));
12055 else if (specs->typespec_word == cts_dfloat32)
12056 error_at (loc,
12057 ("both %<signed%> and %<_Decimal32%> in "
12058 "declaration specifiers"));
12059 else if (specs->typespec_word == cts_dfloat64)
12060 error_at (loc,
12061 ("both %<signed%> and %<_Decimal64%> in "
12062 "declaration specifiers"));
12063 else if (specs->typespec_word == cts_dfloat128)
12064 error_at (loc,
12065 ("both %<signed%> and %<_Decimal128%> in "
12066 "declaration specifiers"));
12067 else
12068 {
12069 specs->signed_p = true;
12070 specs->locations[cdw_signed] = loc;
12071 }
12072 break;
12073 case RID_UNSIGNED:
12074 dupe = specs->unsigned_p;
12075 if (specs->signed_p)
12076 error_at (loc,
12077 ("both %<signed%> and %<unsigned%> in "
12078 "declaration specifiers"));
12079 else if (specs->typespec_word == cts_auto_type)
12080 error_at (loc,
12081 ("both %<unsigned%> and %<__auto_type%> in "
12082 "declaration specifiers"));
12083 else if (specs->typespec_word == cts_void)
12084 error_at (loc,
12085 ("both %<unsigned%> and %<void%> in "
12086 "declaration specifiers"));
12087 else if (specs->typespec_word == cts_bool)
12088 error_at (loc,
12089 ("both %<unsigned%> and %<_Bool%> in "
12090 "declaration specifiers"));
12091 else if (specs->typespec_word == cts_float)
12092 error_at (loc,
12093 ("both %<unsigned%> and %<float%> in "
12094 "declaration specifiers"));
12095 else if (specs->typespec_word == cts_double)
12096 error_at (loc,
12097 ("both %<unsigned%> and %<double%> in "
12098 "declaration specifiers"));
12099 else if (specs->typespec_word == cts_floatn_nx)
12100 error_at (loc,
12101 ("both %<unsigned%> and %<_Float%d%s%> in "
12102 "declaration specifiers"),
12103 floatn_nx_types[specs->u.floatn_nx_idx].n,
12104 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12105 ? "x"
12106 : ""));
12107 else if (specs->typespec_word == cts_dfloat32)
12108 error_at (loc,
12109 ("both %<unsigned%> and %<_Decimal32%> in "
12110 "declaration specifiers"));
12111 else if (specs->typespec_word == cts_dfloat64)
12112 error_at (loc,
12113 ("both %<unsigned%> and %<_Decimal64%> in "
12114 "declaration specifiers"));
12115 else if (specs->typespec_word == cts_dfloat128)
12116 error_at (loc,
12117 ("both %<unsigned%> and %<_Decimal128%> in "
12118 "declaration specifiers"));
12119 else
12120 {
12121 specs->unsigned_p = true;
12122 specs->locations[cdw_unsigned] = loc;
12123 }
12124 break;
12125 case RID_COMPLEX:
12126 dupe = specs->complex_p;
12127 if (!in_system_header_at (loc))
12128 pedwarn_c90 (loc, opt: OPT_Wpedantic,
12129 "ISO C90 does not support complex types");
12130 if (specs->typespec_word == cts_auto_type)
12131 error_at (loc,
12132 ("both %<complex%> and %<__auto_type%> in "
12133 "declaration specifiers"));
12134 else if (specs->typespec_word == cts_void)
12135 error_at (loc,
12136 ("both %<complex%> and %<void%> in "
12137 "declaration specifiers"));
12138 else if (specs->typespec_word == cts_bool)
12139 error_at (loc,
12140 ("both %<complex%> and %<_Bool%> in "
12141 "declaration specifiers"));
12142 else if (specs->typespec_word == cts_bitint)
12143 error_at (loc,
12144 ("both %<complex%> and %<_BitInt%> in "
12145 "declaration specifiers"));
12146 else if (specs->typespec_word == cts_dfloat32)
12147 error_at (loc,
12148 ("both %<complex%> and %<_Decimal32%> in "
12149 "declaration specifiers"));
12150 else if (specs->typespec_word == cts_dfloat64)
12151 error_at (loc,
12152 ("both %<complex%> and %<_Decimal64%> in "
12153 "declaration specifiers"));
12154 else if (specs->typespec_word == cts_dfloat128)
12155 error_at (loc,
12156 ("both %<complex%> and %<_Decimal128%> in "
12157 "declaration specifiers"));
12158 else if (specs->typespec_word == cts_fract)
12159 error_at (loc,
12160 ("both %<complex%> and %<_Fract%> in "
12161 "declaration specifiers"));
12162 else if (specs->typespec_word == cts_accum)
12163 error_at (loc,
12164 ("both %<complex%> and %<_Accum%> in "
12165 "declaration specifiers"));
12166 else if (specs->saturating_p)
12167 error_at (loc,
12168 ("both %<complex%> and %<_Sat%> in "
12169 "declaration specifiers"));
12170 else
12171 {
12172 specs->complex_p = true;
12173 specs->locations[cdw_complex] = loc;
12174 }
12175 break;
12176 case RID_SAT:
12177 dupe = specs->saturating_p;
12178 pedwarn (loc, OPT_Wpedantic,
12179 "ISO C does not support saturating types");
12180 if (specs->typespec_word == cts_int_n)
12181 {
12182 error_at (loc,
12183 ("both %<_Sat%> and %<__int%d%> in "
12184 "declaration specifiers"),
12185 int_n_data[specs->u.int_n_idx].bitsize);
12186 }
12187 else if (specs->typespec_word == cts_auto_type)
12188 error_at (loc,
12189 ("both %<_Sat%> and %<__auto_type%> in "
12190 "declaration specifiers"));
12191 else if (specs->typespec_word == cts_void)
12192 error_at (loc,
12193 ("both %<_Sat%> and %<void%> in "
12194 "declaration specifiers"));
12195 else if (specs->typespec_word == cts_bool)
12196 error_at (loc,
12197 ("both %<_Sat%> and %<_Bool%> in "
12198 "declaration specifiers"));
12199 else if (specs->typespec_word == cts_bitint)
12200 error_at (loc,
12201 ("both %<_Sat%> and %<_BitInt%> in "
12202 "declaration specifiers"));
12203 else if (specs->typespec_word == cts_char)
12204 error_at (loc,
12205 ("both %<_Sat%> and %<char%> in "
12206 "declaration specifiers"));
12207 else if (specs->typespec_word == cts_int)
12208 error_at (loc,
12209 ("both %<_Sat%> and %<int%> in "
12210 "declaration specifiers"));
12211 else if (specs->typespec_word == cts_float)
12212 error_at (loc,
12213 ("both %<_Sat%> and %<float%> in "
12214 "declaration specifiers"));
12215 else if (specs->typespec_word == cts_double)
12216 error_at (loc,
12217 ("both %<_Sat%> and %<double%> in "
12218 "declaration specifiers"));
12219 else if (specs->typespec_word == cts_floatn_nx)
12220 error_at (loc,
12221 ("both %<_Sat%> and %<_Float%d%s%> in "
12222 "declaration specifiers"),
12223 floatn_nx_types[specs->u.floatn_nx_idx].n,
12224 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12225 ? "x"
12226 : ""));
12227 else if (specs->typespec_word == cts_dfloat32)
12228 error_at (loc,
12229 ("both %<_Sat%> and %<_Decimal32%> in "
12230 "declaration specifiers"));
12231 else if (specs->typespec_word == cts_dfloat64)
12232 error_at (loc,
12233 ("both %<_Sat%> and %<_Decimal64%> in "
12234 "declaration specifiers"));
12235 else if (specs->typespec_word == cts_dfloat128)
12236 error_at (loc,
12237 ("both %<_Sat%> and %<_Decimal128%> in "
12238 "declaration specifiers"));
12239 else if (specs->complex_p)
12240 error_at (loc,
12241 ("both %<_Sat%> and %<complex%> in "
12242 "declaration specifiers"));
12243 else
12244 {
12245 specs->saturating_p = true;
12246 specs->locations[cdw_saturating] = loc;
12247 }
12248 break;
12249 default:
12250 gcc_unreachable ();
12251 }
12252
12253 if (dupe)
12254 error_at (loc, "duplicate %qE", type);
12255
12256 return specs;
12257 }
12258 else
12259 {
12260 /* "void", "_Bool", "char", "int", "float", "double",
12261 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12262 "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12263 "__auto_type". */
12264 if (specs->typespec_word != cts_none)
12265 {
12266 error_at (loc,
12267 "two or more data types in declaration specifiers");
12268 return specs;
12269 }
12270 switch (i)
12271 {
12272 case RID_AUTO_TYPE:
12273 if (specs->long_p)
12274 error_at (loc,
12275 ("both %<long%> and %<__auto_type%> in "
12276 "declaration specifiers"));
12277 else if (specs->short_p)
12278 error_at (loc,
12279 ("both %<short%> and %<__auto_type%> in "
12280 "declaration specifiers"));
12281 else if (specs->signed_p)
12282 error_at (loc,
12283 ("both %<signed%> and %<__auto_type%> in "
12284 "declaration specifiers"));
12285 else if (specs->unsigned_p)
12286 error_at (loc,
12287 ("both %<unsigned%> and %<__auto_type%> in "
12288 "declaration specifiers"));
12289 else if (specs->complex_p)
12290 error_at (loc,
12291 ("both %<complex%> and %<__auto_type%> in "
12292 "declaration specifiers"));
12293 else if (specs->saturating_p)
12294 error_at (loc,
12295 ("both %<_Sat%> and %<__auto_type%> in "
12296 "declaration specifiers"));
12297 else
12298 {
12299 specs->typespec_word = cts_auto_type;
12300 specs->locations[cdw_typespec] = loc;
12301 }
12302 return specs;
12303 case RID_INT_N_0:
12304 case RID_INT_N_1:
12305 case RID_INT_N_2:
12306 case RID_INT_N_3:
12307 specs->u.int_n_idx = i - RID_INT_N_0;
12308 if (!in_system_header_at (loc: input_location)
12309 /* If the INT_N type ends in "__", and so is of the format
12310 "__intN__", don't pedwarn. */
12311 && (strncmp (IDENTIFIER_POINTER (type)
12312 + (IDENTIFIER_LENGTH (type) - 2), s2: "__", n: 2) != 0))
12313 pedwarn (loc, OPT_Wpedantic,
12314 "ISO C does not support %<__int%d%> types",
12315 int_n_data[specs->u.int_n_idx].bitsize);
12316
12317 if (specs->long_p)
12318 error_at (loc,
12319 ("both %<__int%d%> and %<long%> in "
12320 "declaration specifiers"),
12321 int_n_data[specs->u.int_n_idx].bitsize);
12322 else if (specs->saturating_p)
12323 error_at (loc,
12324 ("both %<_Sat%> and %<__int%d%> in "
12325 "declaration specifiers"),
12326 int_n_data[specs->u.int_n_idx].bitsize);
12327 else if (specs->short_p)
12328 error_at (loc,
12329 ("both %<__int%d%> and %<short%> in "
12330 "declaration specifiers"),
12331 int_n_data[specs->u.int_n_idx].bitsize);
12332 else if (! int_n_enabled_p[specs->u.int_n_idx])
12333 {
12334 specs->typespec_word = cts_int_n;
12335 error_at (loc,
12336 "%<__int%d%> is not supported on this target",
12337 int_n_data[specs->u.int_n_idx].bitsize);
12338 }
12339 else
12340 {
12341 specs->typespec_word = cts_int_n;
12342 specs->locations[cdw_typespec] = loc;
12343 }
12344 return specs;
12345 case RID_VOID:
12346 if (specs->long_p)
12347 error_at (loc,
12348 ("both %<long%> and %<void%> in "
12349 "declaration specifiers"));
12350 else if (specs->short_p)
12351 error_at (loc,
12352 ("both %<short%> and %<void%> in "
12353 "declaration specifiers"));
12354 else if (specs->signed_p)
12355 error_at (loc,
12356 ("both %<signed%> and %<void%> in "
12357 "declaration specifiers"));
12358 else if (specs->unsigned_p)
12359 error_at (loc,
12360 ("both %<unsigned%> and %<void%> in "
12361 "declaration specifiers"));
12362 else if (specs->complex_p)
12363 error_at (loc,
12364 ("both %<complex%> and %<void%> in "
12365 "declaration specifiers"));
12366 else if (specs->saturating_p)
12367 error_at (loc,
12368 ("both %<_Sat%> and %<void%> in "
12369 "declaration specifiers"));
12370 else
12371 {
12372 specs->typespec_word = cts_void;
12373 specs->locations[cdw_typespec] = loc;
12374 }
12375 return specs;
12376 case RID_BOOL:
12377 if (!in_system_header_at (loc))
12378 pedwarn_c90 (loc, opt: OPT_Wpedantic,
12379 "ISO C90 does not support boolean types");
12380 if (specs->long_p)
12381 error_at (loc,
12382 ("both %<long%> and %<_Bool%> in "
12383 "declaration specifiers"));
12384 else if (specs->short_p)
12385 error_at (loc,
12386 ("both %<short%> and %<_Bool%> in "
12387 "declaration specifiers"));
12388 else if (specs->signed_p)
12389 error_at (loc,
12390 ("both %<signed%> and %<_Bool%> in "
12391 "declaration specifiers"));
12392 else if (specs->unsigned_p)
12393 error_at (loc,
12394 ("both %<unsigned%> and %<_Bool%> in "
12395 "declaration specifiers"));
12396 else if (specs->complex_p)
12397 error_at (loc,
12398 ("both %<complex%> and %<_Bool%> in "
12399 "declaration specifiers"));
12400 else if (specs->saturating_p)
12401 error_at (loc,
12402 ("both %<_Sat%> and %<_Bool%> in "
12403 "declaration specifiers"));
12404 else
12405 {
12406 specs->typespec_word = cts_bool;
12407 specs->locations[cdw_typespec] = loc;
12408 }
12409 return specs;
12410 case RID_CHAR:
12411 if (specs->long_p)
12412 error_at (loc,
12413 ("both %<long%> and %<char%> in "
12414 "declaration specifiers"));
12415 else if (specs->short_p)
12416 error_at (loc,
12417 ("both %<short%> and %<char%> in "
12418 "declaration specifiers"));
12419 else if (specs->saturating_p)
12420 error_at (loc,
12421 ("both %<_Sat%> and %<char%> in "
12422 "declaration specifiers"));
12423 else
12424 {
12425 specs->typespec_word = cts_char;
12426 specs->locations[cdw_typespec] = loc;
12427 }
12428 return specs;
12429 case RID_INT:
12430 if (specs->saturating_p)
12431 error_at (loc,
12432 ("both %<_Sat%> and %<int%> in "
12433 "declaration specifiers"));
12434 else
12435 {
12436 specs->typespec_word = cts_int;
12437 specs->locations[cdw_typespec] = loc;
12438 }
12439 return specs;
12440 case RID_FLOAT:
12441 if (specs->long_p)
12442 error_at (loc,
12443 ("both %<long%> and %<float%> in "
12444 "declaration specifiers"));
12445 else if (specs->short_p)
12446 error_at (loc,
12447 ("both %<short%> and %<float%> in "
12448 "declaration specifiers"));
12449 else if (specs->signed_p)
12450 error_at (loc,
12451 ("both %<signed%> and %<float%> in "
12452 "declaration specifiers"));
12453 else if (specs->unsigned_p)
12454 error_at (loc,
12455 ("both %<unsigned%> and %<float%> in "
12456 "declaration specifiers"));
12457 else if (specs->saturating_p)
12458 error_at (loc,
12459 ("both %<_Sat%> and %<float%> in "
12460 "declaration specifiers"));
12461 else
12462 {
12463 specs->typespec_word = cts_float;
12464 specs->locations[cdw_typespec] = loc;
12465 }
12466 return specs;
12467 case RID_DOUBLE:
12468 if (specs->long_long_p)
12469 error_at (loc,
12470 ("both %<long long%> and %<double%> in "
12471 "declaration specifiers"));
12472 else if (specs->short_p)
12473 error_at (loc,
12474 ("both %<short%> and %<double%> in "
12475 "declaration specifiers"));
12476 else if (specs->signed_p)
12477 error_at (loc,
12478 ("both %<signed%> and %<double%> in "
12479 "declaration specifiers"));
12480 else if (specs->unsigned_p)
12481 error_at (loc,
12482 ("both %<unsigned%> and %<double%> in "
12483 "declaration specifiers"));
12484 else if (specs->saturating_p)
12485 error_at (loc,
12486 ("both %<_Sat%> and %<double%> in "
12487 "declaration specifiers"));
12488 else
12489 {
12490 specs->typespec_word = cts_double;
12491 specs->locations[cdw_typespec] = loc;
12492 }
12493 return specs;
12494 CASE_RID_FLOATN_NX:
12495 specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12496 if (!in_system_header_at (loc: input_location))
12497 pedwarn_c11 (loc, opt: OPT_Wpedantic,
12498 "ISO C does not support the %<_Float%d%s%> type"
12499 " before C23",
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 if (specs->long_p)
12506 error_at (loc,
12507 ("both %<long%> and %<_Float%d%s%> in "
12508 "declaration specifiers"),
12509 floatn_nx_types[specs->u.floatn_nx_idx].n,
12510 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12511 ? "x"
12512 : ""));
12513 else if (specs->short_p)
12514 error_at (loc,
12515 ("both %<short%> and %<_Float%d%s%> in "
12516 "declaration specifiers"),
12517 floatn_nx_types[specs->u.floatn_nx_idx].n,
12518 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12519 ? "x"
12520 : ""));
12521 else if (specs->signed_p)
12522 error_at (loc,
12523 ("both %<signed%> and %<_Float%d%s%> in "
12524 "declaration specifiers"),
12525 floatn_nx_types[specs->u.floatn_nx_idx].n,
12526 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12527 ? "x"
12528 : ""));
12529 else if (specs->unsigned_p)
12530 error_at (loc,
12531 ("both %<unsigned%> and %<_Float%d%s%> in "
12532 "declaration specifiers"),
12533 floatn_nx_types[specs->u.floatn_nx_idx].n,
12534 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12535 ? "x"
12536 : ""));
12537 else if (specs->saturating_p)
12538 error_at (loc,
12539 ("both %<_Sat%> and %<_Float%d%s%> in "
12540 "declaration specifiers"),
12541 floatn_nx_types[specs->u.floatn_nx_idx].n,
12542 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12543 ? "x"
12544 : ""));
12545 else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12546 {
12547 specs->typespec_word = cts_floatn_nx;
12548 error_at (loc,
12549 "%<_Float%d%s%> is not supported on this target",
12550 floatn_nx_types[specs->u.floatn_nx_idx].n,
12551 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12552 ? "x"
12553 : ""));
12554 }
12555 else
12556 {
12557 specs->typespec_word = cts_floatn_nx;
12558 specs->locations[cdw_typespec] = loc;
12559 }
12560 return specs;
12561 case RID_DFLOAT32:
12562 case RID_DFLOAT64:
12563 case RID_DFLOAT128:
12564 {
12565 const char *str;
12566 if (i == RID_DFLOAT32)
12567 str = "_Decimal32";
12568 else if (i == RID_DFLOAT64)
12569 str = "_Decimal64";
12570 else
12571 str = "_Decimal128";
12572 if (specs->long_long_p)
12573 error_at (loc,
12574 ("both %<long long%> and %qs in "
12575 "declaration specifiers"),
12576 str);
12577 if (specs->long_p)
12578 error_at (loc,
12579 ("both %<long%> and %qs in "
12580 "declaration specifiers"),
12581 str);
12582 else if (specs->short_p)
12583 error_at (loc,
12584 ("both %<short%> and %qs in "
12585 "declaration specifiers"),
12586 str);
12587 else if (specs->signed_p)
12588 error_at (loc,
12589 ("both %<signed%> and %qs in "
12590 "declaration specifiers"),
12591 str);
12592 else if (specs->unsigned_p)
12593 error_at (loc,
12594 ("both %<unsigned%> and %qs in "
12595 "declaration specifiers"),
12596 str);
12597 else if (specs->complex_p)
12598 error_at (loc,
12599 ("both %<complex%> and %qs in "
12600 "declaration specifiers"),
12601 str);
12602 else if (specs->saturating_p)
12603 error_at (loc,
12604 ("both %<_Sat%> and %qs in "
12605 "declaration specifiers"),
12606 str);
12607 else if (i == RID_DFLOAT32)
12608 specs->typespec_word = cts_dfloat32;
12609 else if (i == RID_DFLOAT64)
12610 specs->typespec_word = cts_dfloat64;
12611 else
12612 specs->typespec_word = cts_dfloat128;
12613 specs->locations[cdw_typespec] = loc;
12614 }
12615 if (!targetm.decimal_float_supported_p ())
12616 error_at (loc,
12617 ("decimal floating-point not supported "
12618 "for this target"));
12619 pedwarn_c11 (loc, opt: OPT_Wpedantic,
12620 "ISO C does not support decimal floating-point "
12621 "before C23");
12622 return specs;
12623 case RID_FRACT:
12624 case RID_ACCUM:
12625 {
12626 const char *str;
12627 if (i == RID_FRACT)
12628 str = "_Fract";
12629 else
12630 str = "_Accum";
12631 if (specs->complex_p)
12632 error_at (loc,
12633 ("both %<complex%> and %qs in "
12634 "declaration specifiers"),
12635 str);
12636 else if (i == RID_FRACT)
12637 specs->typespec_word = cts_fract;
12638 else
12639 specs->typespec_word = cts_accum;
12640 specs->locations[cdw_typespec] = loc;
12641 }
12642 if (!targetm.fixed_point_supported_p ())
12643 error_at (loc,
12644 "fixed-point types not supported for this target");
12645 pedwarn (loc, OPT_Wpedantic,
12646 "ISO C does not support fixed-point types");
12647 return specs;
12648 case RID_BITINT:
12649 if (specs->long_p)
12650 error_at (loc,
12651 ("both %<long%> and %<_BitInt%> in "
12652 "declaration specifiers"));
12653 else if (specs->short_p)
12654 error_at (loc,
12655 ("both %<short%> and %<_BitInt%> in "
12656 "declaration specifiers"));
12657 else if (specs->complex_p)
12658 error_at (loc,
12659 ("both %<complex%> and %<_BitInt%> in "
12660 "declaration specifiers"));
12661 else if (specs->saturating_p)
12662 error_at (loc,
12663 ("both %<_Sat%> and %<_BitInt%> in "
12664 "declaration specifiers"));
12665 else
12666 {
12667 specs->typespec_word = cts_bitint;
12668 specs->locations[cdw_typespec] = loc;
12669 specs->u.bitint_prec = -1;
12670 if (error_operand_p (t: spec.expr))
12671 return specs;
12672 if (TREE_CODE (spec.expr) != INTEGER_CST
12673 || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12674 {
12675 error_at (loc, "%<_BitInt%> argument is not an integer "
12676 "constant expression");
12677 return specs;
12678 }
12679 if (tree_int_cst_sgn (spec.expr) <= 0)
12680 {
12681 error_at (loc, "%<_BitInt%> argument %qE is not a "
12682 "positive integer constant expression",
12683 spec.expr);
12684 return specs;
12685 }
12686 if (wi::to_widest (t: spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12687 {
12688 error_at (loc, "%<_BitInt%> argument %qE is larger than "
12689 "%<BITINT_MAXWIDTH%> %qd",
12690 spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12691 return specs;
12692 }
12693 specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12694 struct bitint_info info;
12695 if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12696 &info))
12697 {
12698 sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12699 "this target", specs->u.bitint_prec);
12700 specs->u.bitint_prec = -1;
12701 return specs;
12702 }
12703 }
12704 return specs;
12705 default:
12706 /* ObjC reserved word "id", handled below. */
12707 break;
12708 }
12709 }
12710 }
12711
12712 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12713 form of ObjC type, cases such as "int" and "long" being handled
12714 above), a TYPE (struct, union, enum and typeof specifiers) or an
12715 ERROR_MARK. In none of these cases may there have previously
12716 been any type specifiers. */
12717 if (specs->type || specs->typespec_word != cts_none
12718 || specs->long_p || specs->short_p || specs->signed_p
12719 || specs->unsigned_p || specs->complex_p)
12720 error_at (loc, "two or more data types in declaration specifiers");
12721 else if (TREE_CODE (type) == TYPE_DECL)
12722 {
12723 specs->type = TREE_TYPE (type);
12724 if (TREE_TYPE (type) != error_mark_node)
12725 {
12726 specs->decl_attr = DECL_ATTRIBUTES (type);
12727 specs->typedef_p = true;
12728 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12729 specs->locations[cdw_typedef] = loc;
12730
12731 /* If this typedef name is defined in a struct, then a C++
12732 lookup would return a different value. */
12733 if (warn_cxx_compat
12734 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12735 warning_at (loc, OPT_Wc___compat,
12736 "C++ lookup of %qD would return a field, not a type",
12737 type);
12738
12739 /* If we are parsing a struct, record that a struct field
12740 used a typedef. */
12741 if (warn_cxx_compat && struct_parse_info != NULL)
12742 struct_parse_info->typedefs_seen.safe_push (obj: type);
12743 }
12744 }
12745 else if (TREE_CODE (type) == IDENTIFIER_NODE)
12746 {
12747 tree t = lookup_name (name: type);
12748 if (!t || TREE_CODE (t) != TYPE_DECL)
12749 error_at (loc, "%qE fails to be a typedef or built in type", type);
12750 else if (TREE_TYPE (t) == error_mark_node)
12751 ;
12752 else
12753 {
12754 specs->type = TREE_TYPE (t);
12755 specs->locations[cdw_typespec] = loc;
12756 }
12757 }
12758 else
12759 {
12760 if (TREE_CODE (type) != ERROR_MARK)
12761 {
12762 if (spec.kind == ctsk_typeof)
12763 {
12764 specs->typedef_p = true;
12765 specs->locations[cdw_typedef] = loc;
12766 }
12767 if (spec.expr)
12768 {
12769 if (specs->expr)
12770 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12771 specs->expr, spec.expr);
12772 else
12773 specs->expr = spec.expr;
12774 specs->expr_const_operands &= spec.expr_const_operands;
12775 }
12776 }
12777 specs->type = type;
12778 if (spec.has_enum_type_specifier
12779 && spec.kind != ctsk_tagdef)
12780 specs->enum_type_specifier_ref_p = true;
12781 }
12782
12783 return specs;
12784}
12785
12786/* Add the storage class specifier or function specifier SCSPEC to the
12787 declaration specifiers SPECS, returning SPECS. */
12788
12789struct c_declspecs *
12790declspecs_add_scspec (location_t loc,
12791 struct c_declspecs *specs,
12792 tree scspec)
12793{
12794 enum rid i;
12795 enum c_storage_class n = csc_none;
12796 bool dupe = false;
12797 specs->declspecs_seen_p = true;
12798 specs->non_std_attrs_seen_p = true;
12799 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12800 && C_IS_RESERVED_WORD (scspec));
12801 i = C_RID_CODE (scspec);
12802 if (specs->non_sc_seen_p)
12803 warning (OPT_Wold_style_declaration,
12804 "%qE is not at beginning of declaration", scspec);
12805 switch (i)
12806 {
12807 case RID_INLINE:
12808 /* C99 permits duplicate inline. Although of doubtful utility,
12809 it seems simplest to permit it in gnu89 mode as well, as
12810 there is also little utility in maintaining this as a
12811 difference between gnu89 and C99 inline. */
12812 dupe = false;
12813 specs->inline_p = true;
12814 specs->locations[cdw_inline] = loc;
12815 break;
12816 case RID_NORETURN:
12817 /* Duplicate _Noreturn is permitted. */
12818 dupe = false;
12819 specs->noreturn_p = true;
12820 specs->locations[cdw_noreturn] = loc;
12821 break;
12822 case RID_THREAD:
12823 dupe = specs->thread_p;
12824 if (specs->storage_class == csc_auto)
12825 error ("%qE used with %<auto%>", scspec);
12826 else if (specs->storage_class == csc_register)
12827 error ("%qE used with %<register%>", scspec);
12828 else if (specs->storage_class == csc_typedef)
12829 error ("%qE used with %<typedef%>", scspec);
12830 else if (specs->constexpr_p)
12831 error ("%qE used with %<constexpr%>", scspec);
12832 else
12833 {
12834 specs->thread_p = true;
12835 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12836 s2: "__thread") == 0);
12837 /* A diagnostic is not required for the use of this
12838 identifier in the implementation namespace; only diagnose
12839 it for the C11 spelling because of existing code using
12840 the other spelling. */
12841 if (!specs->thread_gnu_p)
12842 {
12843 if (flag_isoc99)
12844 pedwarn_c99 (loc, opt: OPT_Wpedantic,
12845 "ISO C99 does not support %qE", scspec);
12846 else
12847 pedwarn_c99 (loc, opt: OPT_Wpedantic,
12848 "ISO C90 does not support %qE", scspec);
12849 }
12850 specs->locations[cdw_thread] = loc;
12851 }
12852 break;
12853 case RID_AUTO:
12854 if (flag_isoc23
12855 && specs->typespec_kind == ctsk_none
12856 && specs->storage_class != csc_typedef)
12857 {
12858 /* "auto" potentially used for type deduction. */
12859 if (specs->c23_auto_p)
12860 error ("duplicate %qE", scspec);
12861 specs->c23_auto_p = true;
12862 return specs;
12863 }
12864 n = csc_auto;
12865 /* auto may only be used with another storage class specifier,
12866 such as constexpr, if the type is inferred. */
12867 if (specs->constexpr_p)
12868 error ("%qE used with %<constexpr%>", scspec);
12869 break;
12870 case RID_EXTERN:
12871 n = csc_extern;
12872 /* Diagnose "__thread extern". */
12873 if (specs->thread_p && specs->thread_gnu_p)
12874 error ("%<__thread%> before %<extern%>");
12875 break;
12876 case RID_REGISTER:
12877 n = csc_register;
12878 break;
12879 case RID_STATIC:
12880 n = csc_static;
12881 /* Diagnose "__thread static". */
12882 if (specs->thread_p && specs->thread_gnu_p)
12883 error ("%<__thread%> before %<static%>");
12884 break;
12885 case RID_TYPEDEF:
12886 n = csc_typedef;
12887 if (specs->c23_auto_p)
12888 {
12889 error ("%<typedef%> used with %<auto%>");
12890 specs->c23_auto_p = false;
12891 }
12892 break;
12893 case RID_CONSTEXPR:
12894 dupe = specs->constexpr_p;
12895 if (specs->storage_class == csc_extern)
12896 error ("%qE used with %<extern%>", scspec);
12897 else if (specs->storage_class == csc_typedef)
12898 error ("%qE used with %<typedef%>", scspec);
12899 else if (specs->storage_class == csc_auto)
12900 /* auto may only be used with another storage class specifier,
12901 such as constexpr, if the type is inferred. */
12902 error ("%qE used with %<auto%>", scspec);
12903 else if (specs->thread_p)
12904 error ("%qE used with %qs", scspec,
12905 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12906 else
12907 specs->constexpr_p = true;
12908 break;
12909 default:
12910 gcc_unreachable ();
12911 }
12912 if (n != csc_none && n == specs->storage_class)
12913 dupe = true;
12914 if (dupe)
12915 {
12916 if (i == RID_THREAD)
12917 error ("duplicate %<_Thread_local%> or %<__thread%>");
12918 else
12919 error ("duplicate %qE", scspec);
12920 }
12921 if (n != csc_none)
12922 {
12923 if (specs->storage_class != csc_none && n != specs->storage_class)
12924 {
12925 error ("multiple storage classes in declaration specifiers");
12926 }
12927 else
12928 {
12929 specs->storage_class = n;
12930 specs->locations[cdw_storage_class] = loc;
12931 if (n != csc_extern && n != csc_static && specs->thread_p)
12932 {
12933 error ("%qs used with %qE",
12934 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12935 scspec);
12936 specs->thread_p = false;
12937 }
12938 if (n != csc_auto && n != csc_register && n != csc_static
12939 && specs->constexpr_p)
12940 {
12941 error ("%<constexpr%> used with %qE", scspec);
12942 specs->constexpr_p = false;
12943 }
12944 }
12945 }
12946 return specs;
12947}
12948
12949/* Add the attributes ATTRS to the declaration specifiers SPECS,
12950 returning SPECS. */
12951
12952struct c_declspecs *
12953declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12954{
12955 specs->attrs = chainon (attrs, specs->attrs);
12956 specs->locations[cdw_attributes] = loc;
12957 specs->declspecs_seen_p = true;
12958 /* In the case of standard attributes at the start of the
12959 declaration, the caller will reset this. */
12960 specs->non_std_attrs_seen_p = true;
12961 return specs;
12962}
12963
12964/* Add an _Alignas specifier (expression ALIGN, or type whose
12965 alignment is ALIGN) to the declaration specifiers SPECS, returning
12966 SPECS. */
12967struct c_declspecs *
12968declspecs_add_alignas (location_t loc,
12969 struct c_declspecs *specs, tree align)
12970{
12971 specs->alignas_p = true;
12972 specs->locations[cdw_alignas] = loc;
12973 if (align == error_mark_node)
12974 return specs;
12975
12976 /* Only accept the alignment if it's valid and greater than
12977 the current one. Zero is invalid but by C11 required to
12978 be silently ignored. */
12979 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12980 if (align_log > specs->align_log)
12981 specs->align_log = align_log;
12982 return specs;
12983}
12984
12985/* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12986 specifiers with any other type specifier to determine the resulting
12987 type. This is where ISO C checks on complex types are made, since
12988 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12989 double". Also apply postfix standard attributes to modify the type. */
12990
12991struct c_declspecs *
12992finish_declspecs (struct c_declspecs *specs)
12993{
12994 /* If a type was specified as a whole, we have no modifiers and are
12995 done. */
12996 if (specs->type != NULL_TREE)
12997 {
12998 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12999 && !specs->signed_p && !specs->unsigned_p
13000 && !specs->complex_p && !specs->c23_auto_p);
13001
13002 /* Set a dummy type. */
13003 if (TREE_CODE (specs->type) == ERROR_MARK)
13004 specs->type = integer_type_node;
13005 goto handle_postfix_attrs;
13006 }
13007
13008 /* If none of "void", "_Bool", "char", "int", "float" or "double"
13009 has been specified, treat it as "int" unless "_Complex" is
13010 present and there are no other specifiers. If we just have
13011 "_Complex", it is equivalent to "_Complex double", but e.g.
13012 "_Complex short" is equivalent to "_Complex short int". */
13013 if (specs->typespec_word == cts_none)
13014 {
13015 if (specs->saturating_p)
13016 {
13017 error_at (specs->locations[cdw_saturating],
13018 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13019 if (!targetm.fixed_point_supported_p ())
13020 error_at (specs->locations[cdw_saturating],
13021 "fixed-point types not supported for this target");
13022 specs->typespec_word = cts_fract;
13023 }
13024 else if (specs->long_p || specs->short_p
13025 || specs->signed_p || specs->unsigned_p)
13026 {
13027 specs->typespec_word = cts_int;
13028 }
13029 else if (specs->complex_p)
13030 {
13031 specs->typespec_word = cts_double;
13032 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13033 "ISO C does not support plain %<complex%> meaning "
13034 "%<double complex%>");
13035 }
13036 else if (specs->c23_auto_p)
13037 {
13038 /* Type to be filled in later, including applying postfix
13039 attributes. This warning only actually appears for
13040 -Wc11-c23-compat in C23 mode; in older modes, there may
13041 be a warning or pedwarn for implicit "int" instead, or
13042 other errors for use of auto at file scope. */
13043 pedwarn_c11 (input_location, opt: OPT_Wpedantic,
13044 "ISO C does not support %<auto%> type deduction "
13045 "before C23");
13046 return specs;
13047 }
13048 else
13049 {
13050 specs->typespec_word = cts_int;
13051 specs->default_int_p = true;
13052 /* We don't diagnose this here because grokdeclarator will
13053 give more specific diagnostics according to whether it is
13054 a function definition. */
13055 }
13056 }
13057
13058 /* If "signed" was specified, record this to distinguish "int" and
13059 "signed int" in the case of a bit-field with
13060 -funsigned-bitfields. */
13061 specs->explicit_signed_p = specs->signed_p;
13062
13063 /* Now compute the actual type. */
13064 gcc_assert (!specs->c23_auto_p);
13065 switch (specs->typespec_word)
13066 {
13067 case cts_auto_type:
13068 gcc_assert (!specs->long_p && !specs->short_p
13069 && !specs->signed_p && !specs->unsigned_p
13070 && !specs->complex_p);
13071 /* Type to be filled in later. */
13072 if (specs->postfix_attrs)
13073 error ("%<__auto_type%> followed by %<[[]]%> attributes");
13074 break;
13075 case cts_void:
13076 gcc_assert (!specs->long_p && !specs->short_p
13077 && !specs->signed_p && !specs->unsigned_p
13078 && !specs->complex_p);
13079 specs->type = void_type_node;
13080 break;
13081 case cts_bool:
13082 gcc_assert (!specs->long_p && !specs->short_p
13083 && !specs->signed_p && !specs->unsigned_p
13084 && !specs->complex_p);
13085 specs->type = boolean_type_node;
13086 break;
13087 case cts_char:
13088 gcc_assert (!specs->long_p && !specs->short_p);
13089 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13090 if (specs->signed_p)
13091 specs->type = signed_char_type_node;
13092 else if (specs->unsigned_p)
13093 specs->type = unsigned_char_type_node;
13094 else
13095 specs->type = char_type_node;
13096 if (specs->complex_p)
13097 {
13098 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13099 "ISO C does not support complex integer types");
13100 specs->type = build_complex_type (specs->type);
13101 }
13102 break;
13103 case cts_int_n:
13104 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13105 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13106 if (! int_n_enabled_p[specs->u.int_n_idx])
13107 specs->type = integer_type_node;
13108 else
13109 specs->type = (specs->unsigned_p
13110 ? int_n_trees[specs->u.int_n_idx].unsigned_type
13111 : int_n_trees[specs->u.int_n_idx].signed_type);
13112 if (specs->complex_p)
13113 {
13114 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13115 "ISO C does not support complex integer types");
13116 specs->type = build_complex_type (specs->type);
13117 }
13118 break;
13119 case cts_int:
13120 gcc_assert (!(specs->long_p && specs->short_p));
13121 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13122 if (specs->long_long_p)
13123 specs->type = (specs->unsigned_p
13124 ? long_long_unsigned_type_node
13125 : long_long_integer_type_node);
13126 else if (specs->long_p)
13127 specs->type = (specs->unsigned_p
13128 ? long_unsigned_type_node
13129 : long_integer_type_node);
13130 else if (specs->short_p)
13131 specs->type = (specs->unsigned_p
13132 ? short_unsigned_type_node
13133 : short_integer_type_node);
13134 else
13135 specs->type = (specs->unsigned_p
13136 ? unsigned_type_node
13137 : integer_type_node);
13138 if (specs->complex_p)
13139 {
13140 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13141 "ISO C does not support complex integer types");
13142 specs->type = build_complex_type (specs->type);
13143 }
13144 break;
13145 case cts_float:
13146 gcc_assert (!specs->long_p && !specs->short_p
13147 && !specs->signed_p && !specs->unsigned_p);
13148 specs->type = (specs->complex_p
13149 ? complex_float_type_node
13150 : float_type_node);
13151 break;
13152 case cts_double:
13153 gcc_assert (!specs->long_long_p && !specs->short_p
13154 && !specs->signed_p && !specs->unsigned_p);
13155 if (specs->long_p)
13156 {
13157 specs->type = (specs->complex_p
13158 ? complex_long_double_type_node
13159 : long_double_type_node);
13160 }
13161 else
13162 {
13163 specs->type = (specs->complex_p
13164 ? complex_double_type_node
13165 : double_type_node);
13166 }
13167 break;
13168 case cts_floatn_nx:
13169 gcc_assert (!specs->long_p && !specs->short_p
13170 && !specs->signed_p && !specs->unsigned_p);
13171 if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13172 specs->type = integer_type_node;
13173 else if (specs->complex_p)
13174 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13175 else
13176 specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13177 break;
13178 case cts_dfloat32:
13179 case cts_dfloat64:
13180 case cts_dfloat128:
13181 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13182 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13183 if (!targetm.decimal_float_supported_p ())
13184 specs->type = integer_type_node;
13185 else if (specs->typespec_word == cts_dfloat32)
13186 specs->type = dfloat32_type_node;
13187 else if (specs->typespec_word == cts_dfloat64)
13188 specs->type = dfloat64_type_node;
13189 else
13190 specs->type = dfloat128_type_node;
13191 break;
13192 case cts_fract:
13193 gcc_assert (!specs->complex_p);
13194 if (!targetm.fixed_point_supported_p ())
13195 specs->type = integer_type_node;
13196 else if (specs->saturating_p)
13197 {
13198 if (specs->long_long_p)
13199 specs->type = specs->unsigned_p
13200 ? sat_unsigned_long_long_fract_type_node
13201 : sat_long_long_fract_type_node;
13202 else if (specs->long_p)
13203 specs->type = specs->unsigned_p
13204 ? sat_unsigned_long_fract_type_node
13205 : sat_long_fract_type_node;
13206 else if (specs->short_p)
13207 specs->type = specs->unsigned_p
13208 ? sat_unsigned_short_fract_type_node
13209 : sat_short_fract_type_node;
13210 else
13211 specs->type = specs->unsigned_p
13212 ? sat_unsigned_fract_type_node
13213 : sat_fract_type_node;
13214 }
13215 else
13216 {
13217 if (specs->long_long_p)
13218 specs->type = specs->unsigned_p
13219 ? unsigned_long_long_fract_type_node
13220 : long_long_fract_type_node;
13221 else if (specs->long_p)
13222 specs->type = specs->unsigned_p
13223 ? unsigned_long_fract_type_node
13224 : long_fract_type_node;
13225 else if (specs->short_p)
13226 specs->type = specs->unsigned_p
13227 ? unsigned_short_fract_type_node
13228 : short_fract_type_node;
13229 else
13230 specs->type = specs->unsigned_p
13231 ? unsigned_fract_type_node
13232 : fract_type_node;
13233 }
13234 break;
13235 case cts_accum:
13236 gcc_assert (!specs->complex_p);
13237 if (!targetm.fixed_point_supported_p ())
13238 specs->type = integer_type_node;
13239 else if (specs->saturating_p)
13240 {
13241 if (specs->long_long_p)
13242 specs->type = specs->unsigned_p
13243 ? sat_unsigned_long_long_accum_type_node
13244 : sat_long_long_accum_type_node;
13245 else if (specs->long_p)
13246 specs->type = specs->unsigned_p
13247 ? sat_unsigned_long_accum_type_node
13248 : sat_long_accum_type_node;
13249 else if (specs->short_p)
13250 specs->type = specs->unsigned_p
13251 ? sat_unsigned_short_accum_type_node
13252 : sat_short_accum_type_node;
13253 else
13254 specs->type = specs->unsigned_p
13255 ? sat_unsigned_accum_type_node
13256 : sat_accum_type_node;
13257 }
13258 else
13259 {
13260 if (specs->long_long_p)
13261 specs->type = specs->unsigned_p
13262 ? unsigned_long_long_accum_type_node
13263 : long_long_accum_type_node;
13264 else if (specs->long_p)
13265 specs->type = specs->unsigned_p
13266 ? unsigned_long_accum_type_node
13267 : long_accum_type_node;
13268 else if (specs->short_p)
13269 specs->type = specs->unsigned_p
13270 ? unsigned_short_accum_type_node
13271 : short_accum_type_node;
13272 else
13273 specs->type = specs->unsigned_p
13274 ? unsigned_accum_type_node
13275 : accum_type_node;
13276 }
13277 break;
13278 case cts_bitint:
13279 gcc_assert (!specs->long_p && !specs->short_p
13280 && !specs->complex_p);
13281 if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13282 {
13283 error_at (specs->locations[cdw_typespec],
13284 "%<signed _BitInt%> argument must be at least 2");
13285 specs->type = integer_type_node;
13286 break;
13287 }
13288 if (specs->u.bitint_prec == -1)
13289 specs->type = integer_type_node;
13290 else
13291 {
13292 pedwarn_c11 (specs->locations[cdw_typespec], opt: OPT_Wpedantic,
13293 "ISO C does not support %<%s_BitInt(%d)%> before C23",
13294 specs->unsigned_p ? "unsigned "
13295 : specs->signed_p ? "signed " : "",
13296 specs->u.bitint_prec);
13297 specs->type = build_bitint_type (specs->u.bitint_prec,
13298 specs->unsigned_p);
13299 }
13300 break;
13301 default:
13302 gcc_unreachable ();
13303 }
13304 handle_postfix_attrs:
13305 if (specs->type != NULL)
13306 {
13307 specs->postfix_attrs = c_warn_type_attributes (attrs: specs->postfix_attrs);
13308 decl_attributes (&specs->type, specs->postfix_attrs, 0);
13309 specs->postfix_attrs = NULL_TREE;
13310 }
13311
13312 return specs;
13313}
13314
13315/* Perform final processing on one file scope's declarations (or the
13316 external scope's declarations), GLOBALS. */
13317
13318static void
13319c_write_global_declarations_1 (tree globals)
13320{
13321 tree decl;
13322 bool reconsider;
13323
13324 /* Process the decls in the order they were written. */
13325 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13326 {
13327 /* Check for used but undefined static functions using the C
13328 standard's definition of "used", and set TREE_NO_WARNING so
13329 that check_global_declaration doesn't repeat the check. */
13330 if (TREE_CODE (decl) == FUNCTION_DECL
13331 && DECL_INITIAL (decl) == NULL_TREE
13332 && DECL_EXTERNAL (decl)
13333 && !TREE_PUBLIC (decl))
13334 {
13335 if (C_DECL_USED (decl))
13336 {
13337 /* TODO: Add OPT_Wundefined-inline. */
13338 if (pedwarn (input_location, 0, "%q+F used but never defined",
13339 decl))
13340 suppress_warning (decl /* OPT_Wundefined-inline. */);
13341 }
13342 /* For -Wunused-function warn about unused static prototypes. */
13343 else if (warn_unused_function
13344 && ! DECL_ARTIFICIAL (decl)
13345 && ! warning_suppressed_p (decl, OPT_Wunused_function))
13346 {
13347 if (warning (OPT_Wunused_function,
13348 "%q+F declared %<static%> but never defined",
13349 decl))
13350 suppress_warning (decl, OPT_Wunused_function);
13351 }
13352 }
13353
13354 wrapup_global_declaration_1 (decl);
13355 }
13356
13357 do
13358 {
13359 reconsider = false;
13360 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13361 reconsider |= wrapup_global_declaration_2 (decl);
13362 }
13363 while (reconsider);
13364}
13365
13366/* Preserve the external declarations scope across a garbage collect. */
13367static GTY(()) tree ext_block;
13368
13369/* Collect all references relevant to SOURCE_FILE. */
13370
13371static void
13372collect_all_refs (const char *source_file)
13373{
13374 tree t;
13375 unsigned i;
13376
13377 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13378 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13379
13380 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13381}
13382
13383/* Collect source file references at global level. */
13384
13385static void
13386collect_source_refs (void)
13387{
13388 tree t;
13389 tree decls;
13390 tree decl;
13391 unsigned i;
13392
13393 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13394 {
13395 decls = DECL_INITIAL (t);
13396 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13397 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13398 collect_source_ref (DECL_SOURCE_FILE (decl));
13399 }
13400
13401 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13402 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13403 collect_source_ref (DECL_SOURCE_FILE (decl));
13404}
13405
13406/* Free attribute access data that are not needed by the middle end. */
13407
13408static void
13409free_attr_access_data ()
13410{
13411 struct cgraph_node *n;
13412
13413 /* Iterate over all functions declared in the translation unit. */
13414 FOR_EACH_FUNCTION (n)
13415 {
13416 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13417 if (tree attrs = DECL_ATTRIBUTES (parm))
13418 attr_access::free_lang_data (attrs);
13419
13420 tree fntype = TREE_TYPE (n->decl);
13421 if (!fntype || fntype == error_mark_node)
13422 continue;
13423 tree attrs = TYPE_ATTRIBUTES (fntype);
13424 if (!attrs)
13425 continue;
13426
13427 attr_access::free_lang_data (attrs);
13428 }
13429}
13430
13431/* Perform any final parser cleanups and generate initial debugging
13432 information. */
13433
13434void
13435c_parse_final_cleanups (void)
13436{
13437 tree t;
13438 unsigned i;
13439
13440 /* We don't want to do this if generating a PCH. */
13441 if (pch_file)
13442 return;
13443
13444 timevar_stop (TV_PHASE_PARSING);
13445 timevar_start (TV_PHASE_DEFERRED);
13446
13447 /* Do the Objective-C stuff. This is where all the Objective-C
13448 module stuff gets generated (symtab, class/protocol/selector
13449 lists etc). */
13450 if (c_dialect_objc ())
13451 objc_write_global_declarations ();
13452
13453 /* Close the external scope. */
13454 ext_block = pop_scope ();
13455 external_scope = 0;
13456 gcc_assert (!current_scope);
13457
13458 /* Handle -fdump-ada-spec[-slim]. */
13459 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13460 {
13461 /* Build a table of files to generate specs for */
13462 collect_source_ref (main_input_filename);
13463 if (!flag_dump_ada_spec_slim)
13464 collect_source_refs ();
13465
13466 dump_ada_specs (collect_all_refs, NULL);
13467 }
13468
13469 /* Process all file scopes in this compilation, and the external_scope,
13470 through wrapup_global_declarations. */
13471 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13472 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13473 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13474
13475 if (!in_lto_p)
13476 free_attr_access_data ();
13477
13478 timevar_stop (TV_PHASE_DEFERRED);
13479 timevar_start (TV_PHASE_PARSING);
13480
13481 ext_block = NULL;
13482}
13483
13484/* Register reserved keyword WORD as qualifier for address space AS. */
13485
13486void
13487c_register_addr_space (const char *word, addr_space_t as)
13488{
13489 int rid = RID_FIRST_ADDR_SPACE + as;
13490 tree id;
13491
13492 /* Address space qualifiers are only supported
13493 in C with GNU extensions enabled. */
13494 if (c_dialect_objc () || flag_no_asm)
13495 return;
13496
13497 id = get_identifier (word);
13498 C_SET_RID_CODE (id, rid);
13499 C_IS_RESERVED_WORD (id) = 1;
13500 ridpointers [rid] = id;
13501}
13502
13503/* Return identifier to look up for omp declare reduction. */
13504
13505tree
13506c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13507{
13508 const char *p = NULL;
13509 switch (reduction_code)
13510 {
13511 case PLUS_EXPR: p = "+"; break;
13512 case MULT_EXPR: p = "*"; break;
13513 case MINUS_EXPR: p = "-"; break;
13514 case BIT_AND_EXPR: p = "&"; break;
13515 case BIT_XOR_EXPR: p = "^"; break;
13516 case BIT_IOR_EXPR: p = "|"; break;
13517 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13518 case TRUTH_ORIF_EXPR: p = "||"; break;
13519 case MIN_EXPR: p = "min"; break;
13520 case MAX_EXPR: p = "max"; break;
13521 default:
13522 break;
13523 }
13524
13525 if (p == NULL)
13526 {
13527 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13528 return error_mark_node;
13529 p = IDENTIFIER_POINTER (reduction_id);
13530 }
13531
13532 const char prefix[] = "omp declare reduction ";
13533 size_t lenp = sizeof (prefix);
13534 size_t len = strlen (s: p);
13535 char *name = XALLOCAVEC (char, lenp + len);
13536 memcpy (dest: name, src: prefix, n: lenp - 1);
13537 memcpy (dest: name + lenp - 1, src: p, n: len + 1);
13538 return get_identifier (name);
13539}
13540
13541/* Lookup REDUCTION_ID in the current scope, or create an artificial
13542 VAR_DECL, bind it into the current scope and return it. */
13543
13544tree
13545c_omp_reduction_decl (tree reduction_id)
13546{
13547 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13548 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13549 return b->decl;
13550
13551 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13552 reduction_id, integer_type_node);
13553 DECL_ARTIFICIAL (decl) = 1;
13554 DECL_EXTERNAL (decl) = 1;
13555 TREE_STATIC (decl) = 1;
13556 TREE_PUBLIC (decl) = 0;
13557 bind (name: reduction_id, decl, scope: current_scope, invisible: true, nested: false, BUILTINS_LOCATION);
13558 return decl;
13559}
13560
13561/* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13562
13563tree
13564c_omp_reduction_lookup (tree reduction_id, tree type)
13565{
13566 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13567 while (b)
13568 {
13569 tree t;
13570 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13571 if (comptypes (TREE_PURPOSE (t), type))
13572 return TREE_VALUE (t);
13573 b = b->shadowed;
13574 }
13575 return error_mark_node;
13576}
13577
13578/* Helper function called via walk_tree, to diagnose invalid
13579 #pragma omp declare reduction combiners or initializers. */
13580
13581tree
13582c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13583{
13584 tree *vars = (tree *) data;
13585 if (SSA_VAR_P (*tp)
13586 && !DECL_ARTIFICIAL (*tp)
13587 && *tp != vars[0]
13588 && *tp != vars[1])
13589 {
13590 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13591 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), s2: "omp_out") == 0)
13592 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13593 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13594 *tp);
13595 else
13596 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13597 "to variable %qD which is not %<omp_priv%> nor "
13598 "%<omp_orig%>",
13599 *tp);
13600 return *tp;
13601 }
13602 return NULL_TREE;
13603}
13604
13605
13606bool
13607c_check_in_current_scope (tree decl)
13608{
13609 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13610 return b != NULL && B_IN_CURRENT_SCOPE (b);
13611}
13612
13613#include "gt-c-c-decl.h"
13614

source code of gcc/c/c-decl.cc