1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2023 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/* Information for the struct or union currently being parsed, or
638 NULL if not parsing a struct or union. */
639static class c_struct_parse_info *struct_parse_info;
640
641/* Forward declarations. */
642static tree lookup_name_in_scope (tree, struct c_scope *);
643static tree c_make_fname_decl (location_t, tree, int);
644static tree grokdeclarator (const struct c_declarator *,
645 struct c_declspecs *,
646 enum decl_context, bool, tree *, tree *, tree *,
647 bool *, enum deprecated_states);
648static tree grokparms (struct c_arg_info *, bool);
649static void layout_array_type (tree);
650static void warn_defaults_to (location_t, int, const char *, ...)
651 ATTRIBUTE_GCC_DIAG(3,4);
652static const char *header_for_builtin_fn (tree);
653
654/* T is a statement. Add it to the statement-tree. This is the
655 C/ObjC version--C++ has a slightly different version of this
656 function. */
657
658tree
659add_stmt (tree t)
660{
661 enum tree_code code = TREE_CODE (t);
662
663 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
664 {
665 if (!EXPR_HAS_LOCATION (t))
666 SET_EXPR_LOCATION (t, input_location);
667 }
668
669 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
670 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
671
672 /* Add T to the statement-tree. Non-side-effect statements need to be
673 recorded during statement expressions. */
674 if (!building_stmt_list_p ())
675 push_stmt_list ();
676 append_to_statement_list_force (t, &cur_stmt_list);
677
678 return t;
679}
680
681/* Build a pointer type using the default pointer mode. */
682
683static tree
684c_build_pointer_type (tree to_type)
685{
686 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
687 : TYPE_ADDR_SPACE (to_type);
688 machine_mode pointer_mode;
689
690 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
691 pointer_mode = targetm.addr_space.pointer_mode (as);
692 else
693 pointer_mode = c_default_pointer_mode;
694 return build_pointer_type_for_mode (to_type, pointer_mode, false);
695}
696
697
698/* Return true if we will want to say something if a goto statement
699 crosses DECL. */
700
701static bool
702decl_jump_unsafe (tree decl)
703{
704 if (error_operand_p (t: decl))
705 return false;
706
707 /* Don't warn for compound literals. If a goto statement crosses
708 their initialization, it should cross also all the places where
709 the complit is used or where the complit address might be saved
710 into some variable, so code after the label to which goto jumps
711 should not be able to refer to the compound literal. */
712 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
713 return false;
714
715 if (flag_openmp
716 && VAR_P (decl)
717 && lookup_attribute (attr_name: "omp allocate", DECL_ATTRIBUTES (decl)))
718 return true;
719
720 /* Always warn about crossing variably modified types. */
721 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
722 && c_type_variably_modified_p (TREE_TYPE (decl)))
723 return true;
724
725 /* Otherwise, only warn if -Wgoto-misses-init and this is an
726 initialized automatic decl. */
727 if (warn_jump_misses_init
728 && VAR_P (decl)
729 && !TREE_STATIC (decl)
730 && DECL_INITIAL (decl) != NULL_TREE)
731 return true;
732
733 return false;
734}
735
736
737void
738c_print_identifier (FILE *file, tree node, int indent)
739{
740 void (*save) (enum c_oracle_request, tree identifier);
741
742 /* Temporarily hide any binding oracle. Without this, calls to
743 debug_tree from the debugger will end up calling into the oracle,
744 making for a confusing debug session. As the oracle isn't needed
745 here for normal operation, it's simplest to suppress it. */
746 save = c_binding_oracle;
747 c_binding_oracle = NULL;
748
749 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
750 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
751 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
752 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
753 {
754 tree rid = ridpointers[C_RID_CODE (node)];
755 indent_to (file, indent + 4);
756 fprintf (stream: file, format: "rid " HOST_PTR_PRINTF " \"%s\"",
757 (void *) rid, IDENTIFIER_POINTER (rid));
758 }
759
760 c_binding_oracle = save;
761}
762
763/* Establish that the scope contains declarations that are sensitive to
764 jumps that cross a binding. Together with decl_jump_unsafe, this is
765 used to diagnose such jumps. */
766void
767c_mark_decl_jump_unsafe_in_current_scope ()
768{
769 current_scope->has_jump_unsafe_decl = 1;
770}
771
772/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
773 which may be any of several kinds of DECL or TYPE or error_mark_node,
774 in the scope SCOPE. */
775static void
776bind (tree name, tree decl, struct c_scope *scope, bool invisible,
777 bool nested, location_t locus)
778{
779 struct c_binding *b, **here;
780
781 if (binding_freelist)
782 {
783 b = binding_freelist;
784 binding_freelist = b->prev;
785 }
786 else
787 b = ggc_alloc<c_binding> ();
788
789 b->shadowed = 0;
790 b->decl = decl;
791 b->id = name;
792 b->depth = scope->depth;
793 b->invisible = invisible;
794 b->nested = nested;
795 b->inner_comp = 0;
796 b->in_struct = 0;
797 b->locus = locus;
798
799 b->u.type = NULL;
800
801 b->prev = scope->bindings;
802 scope->bindings = b;
803
804 if (decl_jump_unsafe (decl))
805 scope->has_jump_unsafe_decl = 1;
806
807 if (!name)
808 return;
809
810 switch (TREE_CODE (decl))
811 {
812 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
813 case ENUMERAL_TYPE:
814 case UNION_TYPE:
815 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
816 case VAR_DECL:
817 case FUNCTION_DECL:
818 case TYPE_DECL:
819 case CONST_DECL:
820 case PARM_DECL:
821 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
822
823 default:
824 gcc_unreachable ();
825 }
826
827 /* Locate the appropriate place in the chain of shadowed decls
828 to insert this binding. Normally, scope == current_scope and
829 this does nothing. */
830 while (*here && (*here)->depth > scope->depth)
831 here = &(*here)->shadowed;
832
833 b->shadowed = *here;
834 *here = b;
835}
836
837/* Clear the binding structure B, stick it on the binding_freelist,
838 and return the former value of b->prev. This is used by pop_scope
839 and get_parm_info to iterate destructively over all the bindings
840 from a given scope. */
841static struct c_binding *
842free_binding_and_advance (struct c_binding *b)
843{
844 struct c_binding *prev = b->prev;
845
846 memset (s: b, c: 0, n: sizeof (struct c_binding));
847 b->prev = binding_freelist;
848 binding_freelist = b;
849
850 return prev;
851}
852
853/* Bind a label. Like bind, but skip fields which aren't used for
854 labels, and add the LABEL_VARS value. */
855static void
856bind_label (tree name, tree label, struct c_scope *scope,
857 struct c_label_vars *label_vars)
858{
859 struct c_binding *b;
860
861 bind (name, decl: label, scope, /*invisible=*/false, /*nested=*/false,
862 UNKNOWN_LOCATION);
863
864 scope->has_label_bindings = true;
865
866 b = scope->bindings;
867 gcc_assert (b->decl == label);
868 label_vars->shadowed = b->u.label;
869 b->u.label = label_vars;
870}
871
872/* Hook called at end of compilation to assume 1 elt
873 for a file-scope tentative array defn that wasn't complete before. */
874
875void
876c_finish_incomplete_decl (tree decl)
877{
878 if (VAR_P (decl))
879 {
880 tree type = TREE_TYPE (decl);
881 if (type != error_mark_node
882 && TREE_CODE (type) == ARRAY_TYPE
883 && !DECL_EXTERNAL (decl)
884 && TYPE_DOMAIN (type) == NULL_TREE)
885 {
886 warning_at (DECL_SOURCE_LOCATION (decl),
887 0, "array %q+D assumed to have one element", decl);
888
889 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
890
891 relayout_decl (decl);
892 }
893 }
894}
895
896/* Record that inline function FUNC contains a reference (location
897 LOC) to static DECL (file-scope or function-local according to
898 TYPE). */
899
900void
901record_inline_static (location_t loc, tree func, tree decl,
902 enum c_inline_static_type type)
903{
904 c_inline_static *csi = ggc_alloc<c_inline_static> ();
905 csi->location = loc;
906 csi->function = func;
907 csi->static_decl = decl;
908 csi->type = type;
909 csi->next = c_inline_statics;
910 c_inline_statics = csi;
911}
912
913/* Check for references to static declarations in inline functions at
914 the end of the translation unit and diagnose them if the functions
915 are still inline definitions. */
916
917static void
918check_inline_statics (void)
919{
920 struct c_inline_static *csi;
921 for (csi = c_inline_statics; csi; csi = csi->next)
922 {
923 if (DECL_EXTERNAL (csi->function))
924 switch (csi->type)
925 {
926 case csi_internal:
927 pedwarn (csi->location, 0,
928 "%qD is static but used in inline function %qD "
929 "which is not static", csi->static_decl, csi->function);
930 break;
931 case csi_modifiable:
932 pedwarn (csi->location, 0,
933 "%q+D is static but declared in inline function %qD "
934 "which is not static", csi->static_decl, csi->function);
935 break;
936 default:
937 gcc_unreachable ();
938 }
939 }
940 c_inline_statics = NULL;
941}
942
943/* Fill in a c_spot_bindings structure. If DEFINING is true, set it
944 for the current state, otherwise set it to uninitialized. */
945
946static void
947set_spot_bindings (struct c_spot_bindings *p, bool defining)
948{
949 if (defining)
950 {
951 p->scope = current_scope;
952 p->bindings_in_scope = current_scope->bindings;
953 }
954 else
955 {
956 p->scope = NULL;
957 p->bindings_in_scope = NULL;
958 }
959 p->stmt_exprs = 0;
960 p->left_stmt_expr = false;
961}
962
963/* Update spot bindings P as we pop out of SCOPE. Return true if we
964 should push decls for a label. */
965
966static bool
967update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
968{
969 if (p->scope != scope)
970 {
971 /* This label or goto is defined in some other scope, or it is a
972 label which is not yet defined. There is nothing to
973 update. */
974 return false;
975 }
976
977 /* Adjust the spot bindings to refer to the bindings already defined
978 in the enclosing scope. */
979 p->scope = scope->outer;
980 p->bindings_in_scope = p->scope->bindings;
981
982 return true;
983}
984
985/* The Objective-C front-end often needs to determine the current scope. */
986
987void *
988objc_get_current_scope (void)
989{
990 return current_scope;
991}
992
993/* The following function is used only by Objective-C. It needs to live here
994 because it accesses the innards of c_scope. */
995
996void
997objc_mark_locals_volatile (void *enclosing_blk)
998{
999 struct c_scope *scope;
1000 struct c_binding *b;
1001
1002 for (scope = current_scope;
1003 scope && scope != enclosing_blk;
1004 scope = scope->outer)
1005 {
1006 for (b = scope->bindings; b; b = b->prev)
1007 objc_volatilize_decl (b->decl);
1008
1009 /* Do not climb up past the current function. */
1010 if (scope->function_body)
1011 break;
1012 }
1013}
1014
1015/* Return true if we are in the global binding level. */
1016
1017bool
1018global_bindings_p (void)
1019{
1020 return current_scope == file_scope;
1021}
1022
1023/* Return true if we're declaring parameters in an old-style function
1024 declaration. */
1025
1026bool
1027old_style_parameter_scope (void)
1028{
1029 /* If processing parameters and there is no function statement list, we
1030 * have an old-style function declaration. */
1031 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1032}
1033
1034void
1035keep_next_level (void)
1036{
1037 keep_next_level_flag = true;
1038}
1039
1040/* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1041
1042void
1043set_float_const_decimal64 (void)
1044{
1045 current_scope->float_const_decimal64 = true;
1046}
1047
1048/* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1049
1050void
1051clear_float_const_decimal64 (void)
1052{
1053 current_scope->float_const_decimal64 = false;
1054}
1055
1056/* Return nonzero if an unsuffixed float constant is _Decimal64. */
1057
1058bool
1059float_const_decimal64_p (void)
1060{
1061 return current_scope->float_const_decimal64;
1062}
1063
1064/* Identify this scope as currently being filled with parameters. */
1065
1066void
1067declare_parm_level (void)
1068{
1069 current_scope->parm_flag = true;
1070}
1071
1072void
1073push_scope (void)
1074{
1075 if (next_is_function_body)
1076 {
1077 /* This is the transition from the parameters to the top level
1078 of the function body. These are the same scope
1079 (C99 6.2.1p4,6) so we do not push another scope structure.
1080 next_is_function_body is set only by store_parm_decls, which
1081 in turn is called when and only when we are about to
1082 encounter the opening curly brace for the function body.
1083
1084 The outermost block of a function always gets a BLOCK node,
1085 because the debugging output routines expect that each
1086 function has at least one BLOCK. */
1087 current_scope->parm_flag = false;
1088 current_scope->function_body = true;
1089 current_scope->keep = true;
1090 current_scope->outer_function = current_function_scope;
1091 current_function_scope = current_scope;
1092
1093 keep_next_level_flag = false;
1094 next_is_function_body = false;
1095
1096 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1097 if (current_scope->outer)
1098 current_scope->float_const_decimal64
1099 = current_scope->outer->float_const_decimal64;
1100 else
1101 current_scope->float_const_decimal64 = false;
1102 }
1103 else
1104 {
1105 struct c_scope *scope;
1106 if (scope_freelist)
1107 {
1108 scope = scope_freelist;
1109 scope_freelist = scope->outer;
1110 }
1111 else
1112 scope = ggc_cleared_alloc<c_scope> ();
1113
1114 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1115 if (current_scope)
1116 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1117 else
1118 scope->float_const_decimal64 = false;
1119
1120 scope->keep = keep_next_level_flag;
1121 scope->outer = current_scope;
1122 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1123
1124 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1125 possible. */
1126 if (current_scope && scope->depth == 0)
1127 {
1128 scope->depth--;
1129 sorry ("GCC supports only %u nested scopes", scope->depth);
1130 }
1131
1132 current_scope = scope;
1133 keep_next_level_flag = false;
1134 }
1135}
1136
1137/* This is called when we are leaving SCOPE. For each label defined
1138 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1139 These are the decls whose initialization will be skipped by a goto
1140 later in the function. */
1141
1142static void
1143update_label_decls (struct c_scope *scope)
1144{
1145 struct c_scope *s;
1146
1147 s = scope;
1148 while (s != NULL)
1149 {
1150 if (s->has_label_bindings)
1151 {
1152 struct c_binding *b;
1153
1154 for (b = s->bindings; b != NULL; b = b->prev)
1155 {
1156 struct c_label_vars *label_vars;
1157 struct c_binding *b1;
1158 bool hjud;
1159 unsigned int ix;
1160 struct c_goto_bindings *g;
1161
1162 if (TREE_CODE (b->decl) != LABEL_DECL)
1163 continue;
1164 label_vars = b->u.label;
1165
1166 b1 = label_vars->label_bindings.bindings_in_scope;
1167 if (label_vars->label_bindings.scope == NULL)
1168 hjud = false;
1169 else
1170 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1171 if (update_spot_bindings (scope, p: &label_vars->label_bindings))
1172 {
1173 /* This label is defined in this scope. */
1174 if (hjud)
1175 {
1176 for (; b1 != NULL; b1 = b1->prev)
1177 {
1178 /* A goto from later in the function to this
1179 label will never see the initialization
1180 of B1, if any. Save it to issue a
1181 warning if needed. */
1182 if (decl_jump_unsafe (decl: b1->decl))
1183 vec_safe_push(v&: label_vars->decls_in_scope, obj: b1->decl);
1184 }
1185 }
1186 }
1187
1188 /* Update the bindings of any goto statements associated
1189 with this label. */
1190 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1191 update_spot_bindings (scope, p: &g->goto_bindings);
1192 }
1193 }
1194
1195 /* Don't search beyond the current function. */
1196 if (s == current_function_scope)
1197 break;
1198
1199 s = s->outer;
1200 }
1201}
1202
1203/* Exit a scope. Restore the state of the identifier-decl mappings
1204 that were in effect when this scope was entered. Return a BLOCK
1205 node containing all the DECLs in this scope that are of interest
1206 to debug info generation. */
1207
1208tree
1209pop_scope (void)
1210{
1211 struct c_scope *scope = current_scope;
1212 tree block, context, p;
1213 struct c_binding *b;
1214
1215 bool functionbody = scope->function_body;
1216 bool keep = functionbody || scope->keep || scope->bindings;
1217
1218 update_label_decls (scope);
1219
1220 /* If appropriate, create a BLOCK to record the decls for the life
1221 of this function. */
1222 block = NULL_TREE;
1223 if (keep)
1224 {
1225 block = make_node (BLOCK);
1226 BLOCK_SUBBLOCKS (block) = scope->blocks;
1227 TREE_USED (block) = 1;
1228
1229 /* In each subblock, record that this is its superior. */
1230 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1231 BLOCK_SUPERCONTEXT (p) = block;
1232
1233 BLOCK_VARS (block) = NULL_TREE;
1234 }
1235
1236 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1237 scope must be set so that they point to the appropriate
1238 construct, i.e. either to the current FUNCTION_DECL node, or
1239 else to the BLOCK node we just constructed.
1240
1241 Note that for tagged types whose scope is just the formal
1242 parameter list for some function type specification, we can't
1243 properly set their TYPE_CONTEXTs here, because we don't have a
1244 pointer to the appropriate FUNCTION_TYPE node readily available
1245 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1246 type nodes get set in `grokdeclarator' as soon as we have created
1247 the FUNCTION_TYPE node which will represent the "scope" for these
1248 "parameter list local" tagged types. */
1249 if (scope->function_body)
1250 context = current_function_decl;
1251 else if (scope == file_scope)
1252 {
1253 tree file_decl
1254 = build_translation_unit_decl (get_identifier (main_input_filename));
1255 context = file_decl;
1256 debug_hooks->register_main_translation_unit (file_decl);
1257 }
1258 else
1259 context = block;
1260
1261 /* Clear all bindings in this scope. */
1262 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1263 {
1264 p = b->decl;
1265 switch (TREE_CODE (p))
1266 {
1267 case LABEL_DECL:
1268 /* Warnings for unused labels, errors for undefined labels. */
1269 if (TREE_USED (p) && !DECL_INITIAL (p))
1270 {
1271 error ("label %q+D used but not defined", p);
1272 DECL_INITIAL (p) = error_mark_node;
1273 }
1274 else
1275 warn_for_unused_label (label: p);
1276
1277 /* Labels go in BLOCK_VARS. */
1278 DECL_CHAIN (p) = BLOCK_VARS (block);
1279 BLOCK_VARS (block) = p;
1280 gcc_assert (I_LABEL_BINDING (b->id) == b);
1281 I_LABEL_BINDING (b->id) = b->shadowed;
1282
1283 /* Also pop back to the shadowed label_vars. */
1284 release_tree_vector (b->u.label->decls_in_scope);
1285 b->u.label = b->u.label->shadowed;
1286 break;
1287
1288 case ENUMERAL_TYPE:
1289 case UNION_TYPE:
1290 case RECORD_TYPE:
1291
1292 /* Types may not have tag-names, in which case the type
1293 appears in the bindings list with b->id NULL. */
1294 if (b->id)
1295 {
1296 gcc_assert (I_TAG_BINDING (b->id) == b);
1297 I_TAG_BINDING (b->id) = b->shadowed;
1298 }
1299 break;
1300
1301 case FUNCTION_DECL:
1302 /* Propagate TREE_ADDRESSABLE from nested functions to their
1303 containing functions. */
1304 if (!TREE_ASM_WRITTEN (p)
1305 && DECL_INITIAL (p) != NULL_TREE
1306 && TREE_ADDRESSABLE (p)
1307 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1308 && DECL_ABSTRACT_ORIGIN (p) != p)
1309 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1310 if (!TREE_PUBLIC (p)
1311 && !DECL_INITIAL (p)
1312 && !b->nested
1313 && scope != file_scope
1314 && scope != external_scope)
1315 {
1316 error ("nested function %q+D declared but never defined", p);
1317 undef_nested_function = true;
1318 }
1319 else if (DECL_DECLARED_INLINE_P (p)
1320 && TREE_PUBLIC (p)
1321 && !DECL_INITIAL (p))
1322 {
1323 /* C99 6.7.4p6: "a function with external linkage... declared
1324 with an inline function specifier ... shall also be defined
1325 in the same translation unit." */
1326 if (!flag_gnu89_inline
1327 && !lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (p))
1328 && scope == external_scope)
1329 pedwarn (input_location, 0,
1330 "inline function %q+D declared but never defined", p);
1331 DECL_EXTERNAL (p) = 1;
1332 }
1333
1334 goto common_symbol;
1335
1336 case VAR_DECL:
1337 /* Warnings for unused variables. */
1338 if ((!TREE_USED (p) || !DECL_READ_P (p))
1339 && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1340 && !DECL_IN_SYSTEM_HEADER (p)
1341 && DECL_NAME (p)
1342 && !DECL_ARTIFICIAL (p)
1343 && scope != file_scope
1344 && scope != external_scope)
1345 {
1346 if (!TREE_USED (p))
1347 {
1348 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1349 suppress_warning (p, OPT_Wunused_variable);
1350 }
1351 else if (DECL_CONTEXT (p) == current_function_decl)
1352 warning_at (DECL_SOURCE_LOCATION (p),
1353 OPT_Wunused_but_set_variable,
1354 "variable %qD set but not used", p);
1355 }
1356
1357 if (b->inner_comp)
1358 {
1359 error ("type of array %q+D completed incompatibly with"
1360 " implicit initialization", p);
1361 }
1362
1363 /* Fall through. */
1364 case TYPE_DECL:
1365 case CONST_DECL:
1366 common_symbol:
1367 /* All of these go in BLOCK_VARS, but only if this is the
1368 binding in the home scope. */
1369 if (!b->nested)
1370 {
1371 DECL_CHAIN (p) = BLOCK_VARS (block);
1372 BLOCK_VARS (block) = p;
1373 }
1374 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1375 {
1376 /* For block local externs add a special
1377 DECL_EXTERNAL decl for debug info generation. */
1378 tree extp = copy_node (p);
1379
1380 DECL_EXTERNAL (extp) = 1;
1381 TREE_STATIC (extp) = 0;
1382 TREE_PUBLIC (extp) = 1;
1383 DECL_INITIAL (extp) = NULL_TREE;
1384 DECL_LANG_SPECIFIC (extp) = NULL;
1385 DECL_CONTEXT (extp) = current_function_decl;
1386 if (TREE_CODE (p) == FUNCTION_DECL)
1387 {
1388 DECL_RESULT (extp) = NULL_TREE;
1389 DECL_SAVED_TREE (extp) = NULL_TREE;
1390 DECL_STRUCT_FUNCTION (extp) = NULL;
1391 }
1392 if (b->locus != UNKNOWN_LOCATION)
1393 DECL_SOURCE_LOCATION (extp) = b->locus;
1394 DECL_CHAIN (extp) = BLOCK_VARS (block);
1395 BLOCK_VARS (block) = extp;
1396 }
1397 /* If this is the file scope set DECL_CONTEXT of each decl to
1398 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1399 work. */
1400 if (scope == file_scope)
1401 DECL_CONTEXT (p) = context;
1402
1403 gcc_fallthrough ();
1404 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1405 already been put there by store_parm_decls. Unused-
1406 parameter warnings are handled by function.cc.
1407 error_mark_node obviously does not go in BLOCK_VARS and
1408 does not get unused-variable warnings. */
1409 case PARM_DECL:
1410 case ERROR_MARK:
1411 /* It is possible for a decl not to have a name. We get
1412 here with b->id NULL in this case. */
1413 if (b->id)
1414 {
1415 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1416 I_SYMBOL_BINDING (b->id) = b->shadowed;
1417 if (b->shadowed && b->shadowed->u.type)
1418 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1419 }
1420 break;
1421
1422 default:
1423 gcc_unreachable ();
1424 }
1425 }
1426
1427
1428 /* Dispose of the block that we just made inside some higher level. */
1429 if ((scope->function_body || scope == file_scope) && context)
1430 {
1431 DECL_INITIAL (context) = block;
1432 BLOCK_SUPERCONTEXT (block) = context;
1433 }
1434 else if (scope->outer)
1435 {
1436 if (block)
1437 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1438 /* If we did not make a block for the scope just exited, any
1439 blocks made for inner scopes must be carried forward so they
1440 will later become subblocks of something else. */
1441 else if (scope->blocks)
1442 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1443 }
1444
1445 /* Pop the current scope, and free the structure for reuse. */
1446 current_scope = scope->outer;
1447 if (scope->function_body)
1448 current_function_scope = scope->outer_function;
1449
1450 memset (s: scope, c: 0, n: sizeof (struct c_scope));
1451 scope->outer = scope_freelist;
1452 scope_freelist = scope;
1453
1454 return block;
1455}
1456
1457void
1458push_file_scope (void)
1459{
1460 tree decl;
1461
1462 if (file_scope)
1463 return;
1464
1465 push_scope ();
1466 file_scope = current_scope;
1467
1468 start_fname_decls ();
1469
1470 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1471 bind (DECL_NAME (decl), decl, scope: file_scope,
1472 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1473}
1474
1475void
1476pop_file_scope (void)
1477{
1478 /* In case there were missing closebraces, get us back to the global
1479 binding level. */
1480 while (current_scope != file_scope)
1481 pop_scope ();
1482
1483 /* __FUNCTION__ is defined at file scope (""). This
1484 call may not be necessary as my tests indicate it
1485 still works without it. */
1486 finish_fname_decls ();
1487
1488 check_inline_statics ();
1489
1490 /* This is the point to write out a PCH if we're doing that.
1491 In that case we do not want to do anything else. */
1492 if (pch_file)
1493 {
1494 c_common_write_pch ();
1495 /* Ensure even the callers don't try to finalize the CU. */
1496 flag_syntax_only = 1;
1497 return;
1498 }
1499
1500 /* Pop off the file scope and close this translation unit. */
1501 pop_scope ();
1502 file_scope = 0;
1503
1504 maybe_apply_pending_pragma_weaks ();
1505}
1506
1507/* Whether we are curently inside the initializer for an
1508 underspecified object definition (C23 auto or constexpr). */
1509static bool in_underspecified_init;
1510
1511/* Start an underspecified object definition for NAME at LOC. This
1512 means that NAME is shadowed inside its initializer, so neither the
1513 definition being initialized, nor any definition from an outer
1514 scope, may be referenced during that initializer. Return state to
1515 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1516 underspecified object is a (constexpr) compound literal; there is
1517 no shadowing in that case, but all the other restrictions on
1518 underspecified object definitions still apply. */
1519unsigned int
1520start_underspecified_init (location_t loc, tree name)
1521{
1522 bool prev = in_underspecified_init;
1523 bool ok;
1524 if (name == NULL_TREE)
1525 ok = true;
1526 else
1527 {
1528 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1529 C_DECL_UNDERSPECIFIED (decl) = 1;
1530 struct c_scope *scope = current_scope;
1531 struct c_binding *b = I_SYMBOL_BINDING (name);
1532 if (b && B_IN_SCOPE (b, scope))
1533 {
1534 error_at (loc, "underspecified declaration of %qE, which is already "
1535 "declared in this scope", name);
1536 ok = false;
1537 }
1538 else
1539 {
1540 bind (name, decl, scope, invisible: false, nested: false, locus: loc);
1541 ok = true;
1542 }
1543 }
1544 in_underspecified_init = true;
1545 return ok | (prev << 1);
1546}
1547
1548/* Finish an underspecified object definition for NAME, before that
1549 name is bound to the real declaration instead of a placeholder.
1550 PREV_STATE is the value returned by the call to
1551 start_underspecified_init. If NAME is NULL_TREE, this means a
1552 compound literal, as for start_underspecified_init. */
1553void
1554finish_underspecified_init (tree name, unsigned int prev_state)
1555{
1556 if (name != NULL_TREE && (prev_state & 1))
1557 {
1558 /* A VAR_DECL was bound to the name to shadow any previous
1559 declarations for the name; remove that binding now. */
1560 struct c_scope *scope = current_scope;
1561 struct c_binding *b = I_SYMBOL_BINDING (name);
1562 gcc_assert (b);
1563 gcc_assert (B_IN_SCOPE (b, scope));
1564 gcc_assert (VAR_P (b->decl));
1565 gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1566 I_SYMBOL_BINDING (name) = b->shadowed;
1567 /* In erroneous cases there may be other bindings added to this
1568 scope during the initializer. */
1569 struct c_binding **p = &scope->bindings;
1570 while (*p != b)
1571 p = &((*p)->prev);
1572 *p = free_binding_and_advance (b: *p);
1573 }
1574 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1575}
1576
1577/* Adjust the bindings for the start of a statement expression. */
1578
1579void
1580c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1581{
1582 struct c_scope *scope;
1583
1584 for (scope = current_scope; scope != NULL; scope = scope->outer)
1585 {
1586 struct c_binding *b;
1587
1588 if (!scope->has_label_bindings)
1589 continue;
1590
1591 for (b = scope->bindings; b != NULL; b = b->prev)
1592 {
1593 struct c_label_vars *label_vars;
1594 unsigned int ix;
1595 struct c_goto_bindings *g;
1596
1597 if (TREE_CODE (b->decl) != LABEL_DECL)
1598 continue;
1599 label_vars = b->u.label;
1600 ++label_vars->label_bindings.stmt_exprs;
1601 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1602 ++g->goto_bindings.stmt_exprs;
1603 }
1604 }
1605
1606 if (switch_bindings != NULL)
1607 ++switch_bindings->stmt_exprs;
1608}
1609
1610/* Adjust the bindings for the end of a statement expression. */
1611
1612void
1613c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1614{
1615 struct c_scope *scope;
1616
1617 for (scope = current_scope; scope != NULL; scope = scope->outer)
1618 {
1619 struct c_binding *b;
1620
1621 if (!scope->has_label_bindings)
1622 continue;
1623
1624 for (b = scope->bindings; b != NULL; b = b->prev)
1625 {
1626 struct c_label_vars *label_vars;
1627 unsigned int ix;
1628 struct c_goto_bindings *g;
1629
1630 if (TREE_CODE (b->decl) != LABEL_DECL)
1631 continue;
1632 label_vars = b->u.label;
1633 --label_vars->label_bindings.stmt_exprs;
1634 if (label_vars->label_bindings.stmt_exprs < 0)
1635 {
1636 label_vars->label_bindings.left_stmt_expr = true;
1637 label_vars->label_bindings.stmt_exprs = 0;
1638 }
1639 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1640 {
1641 --g->goto_bindings.stmt_exprs;
1642 if (g->goto_bindings.stmt_exprs < 0)
1643 {
1644 g->goto_bindings.left_stmt_expr = true;
1645 g->goto_bindings.stmt_exprs = 0;
1646 }
1647 }
1648 }
1649 }
1650
1651 if (switch_bindings != NULL)
1652 {
1653 --switch_bindings->stmt_exprs;
1654 gcc_assert (switch_bindings->stmt_exprs >= 0);
1655 }
1656}
1657
1658/* Push a definition or a declaration of struct, union or enum tag "name".
1659 "type" should be the type node.
1660 We assume that the tag "name" is not already defined, and has a location
1661 of LOC.
1662
1663 Note that the definition may really be just a forward reference.
1664 In that case, the TYPE_SIZE will be zero. */
1665
1666static void
1667pushtag (location_t loc, tree name, tree type)
1668{
1669 /* Record the identifier as the type's name if it has none. */
1670 if (name && !TYPE_NAME (type))
1671 TYPE_NAME (type) = name;
1672 bind (name, decl: type, scope: current_scope, /*invisible=*/false, /*nested=*/false, locus: loc);
1673
1674 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1675 tagged type we just added to the current scope. This fake
1676 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1677 to output a representation of a tagged type, and it also gives
1678 us a convenient place to record the "scope start" address for the
1679 tagged type. */
1680
1681 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1682 TYPE_DECL, NULL_TREE, type));
1683
1684 /* An approximation for now, so we can tell this is a function-scope tag.
1685 This will be updated in pop_scope. */
1686 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1687
1688 if (warn_cxx_compat && name != NULL_TREE)
1689 {
1690 struct c_binding *b = I_SYMBOL_BINDING (name);
1691
1692 if (b != NULL
1693 && b->decl != NULL_TREE
1694 && TREE_CODE (b->decl) == TYPE_DECL
1695 && (B_IN_CURRENT_SCOPE (b)
1696 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1697 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1698 != TYPE_MAIN_VARIANT (type)))
1699 {
1700 auto_diagnostic_group d;
1701 if (warning_at (loc, OPT_Wc___compat,
1702 ("using %qD as both a typedef and a tag is "
1703 "invalid in C++"), b->decl)
1704 && b->locus != UNKNOWN_LOCATION)
1705 inform (b->locus, "originally defined here");
1706 }
1707 }
1708}
1709
1710/* An exported interface to pushtag. This is used by the gdb plugin's
1711 binding oracle to introduce a new tag binding. */
1712
1713void
1714c_pushtag (location_t loc, tree name, tree type)
1715{
1716 pushtag (loc, name, type);
1717}
1718
1719/* An exported interface to bind a declaration. LOC is the location
1720 to use. DECL is the declaration to bind. The decl's name is used
1721 to determine how it is bound. If DECL is a VAR_DECL, then
1722 IS_GLOBAL determines whether the decl is put into the global (file
1723 and external) scope or the current function's scope; if DECL is not
1724 a VAR_DECL then it is always put into the file scope. */
1725
1726void
1727c_bind (location_t loc, tree decl, bool is_global)
1728{
1729 struct c_scope *scope;
1730 bool nested = false;
1731
1732 if (!VAR_P (decl) || current_function_scope == NULL)
1733 {
1734 /* Types and functions are always considered to be global. */
1735 scope = file_scope;
1736 DECL_EXTERNAL (decl) = 1;
1737 TREE_PUBLIC (decl) = 1;
1738 }
1739 else if (is_global)
1740 {
1741 /* Also bind it into the external scope. */
1742 bind (DECL_NAME (decl), decl, scope: external_scope, invisible: true, nested: false, locus: loc);
1743 nested = true;
1744 scope = file_scope;
1745 DECL_EXTERNAL (decl) = 1;
1746 TREE_PUBLIC (decl) = 1;
1747 }
1748 else
1749 {
1750 DECL_CONTEXT (decl) = current_function_decl;
1751 TREE_PUBLIC (decl) = 0;
1752 scope = current_function_scope;
1753 }
1754
1755 bind (DECL_NAME (decl), decl, scope, invisible: false, nested, locus: loc);
1756}
1757
1758
1759/* Stores the first FILE*, const struct tm* etc. argument type (whatever
1760 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1761 to the builtin_structptr_types array. Subsequent declarations of such
1762 built-ins are expected to refer to it rather than to fileptr_type_node,
1763 etc. which is just void* (or to any other type).
1764 Used only by match_builtin_function_types. */
1765
1766static const unsigned builtin_structptr_type_count
1767 = ARRAY_SIZE (builtin_structptr_types);
1768
1769static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1770
1771/* Returns true if types T1 and T2 representing return types or types
1772 of function arguments are close enough to be considered interchangeable
1773 in redeclarations of built-in functions. */
1774
1775static bool
1776types_close_enough_to_match (tree t1, tree t2)
1777{
1778 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1779 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1780 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1781}
1782
1783/* Subroutine of compare_decls. Allow harmless mismatches in return
1784 and argument types provided that the type modes match. Set *STRICT
1785 and *ARGNO to the expected argument type and number in case of
1786 an argument type mismatch or null and zero otherwise. Return
1787 a unified type given a suitable match, and 0 otherwise. */
1788
1789static tree
1790match_builtin_function_types (tree newtype, tree oldtype,
1791 tree *strict, unsigned *argno)
1792{
1793 *argno = 0;
1794 *strict = NULL_TREE;
1795
1796 /* Accept the return type of the new declaration if it has the same
1797 mode and if they're both pointers or if neither is. */
1798 tree oldrettype = TREE_TYPE (oldtype);
1799 tree newrettype = TREE_TYPE (newtype);
1800
1801 if (!types_close_enough_to_match (t1: oldrettype, t2: newrettype))
1802 return NULL_TREE;
1803
1804 /* Check that the return types are compatible but don't fail if they
1805 are not (e.g., int vs long in ILP32) and just let the caller know. */
1806 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1807 TYPE_MAIN_VARIANT (newrettype)))
1808 *strict = oldrettype;
1809
1810 tree oldargs = TYPE_ARG_TYPES (oldtype);
1811 tree newargs = TYPE_ARG_TYPES (newtype);
1812 tree tryargs = newargs;
1813
1814 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1815 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1816
1817 gcc_checking_assert (nlst == nbst);
1818
1819 for (unsigned i = 1; oldargs || newargs; ++i)
1820 {
1821 if (!oldargs
1822 || !newargs
1823 || !TREE_VALUE (oldargs)
1824 || !TREE_VALUE (newargs))
1825 return NULL_TREE;
1826
1827 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1828 tree newtype = TREE_VALUE (newargs);
1829 if (newtype == error_mark_node)
1830 return NULL_TREE;
1831 newtype = TYPE_MAIN_VARIANT (newtype);
1832
1833 if (!types_close_enough_to_match (t1: oldtype, t2: newtype))
1834 return NULL_TREE;
1835
1836 unsigned j = nbst;
1837 if (POINTER_TYPE_P (oldtype))
1838 /* Iterate over well-known struct types like FILE (whose types
1839 aren't known to us) and compare the pointer to each to
1840 the pointer argument. */
1841 for (j = 0; j < nbst; ++j)
1842 {
1843 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1844 continue;
1845 /* Store the first FILE* etc. argument type (whatever it is), and
1846 expect any subsequent declarations of file I/O etc. built-ins
1847 to refer to it rather than to fileptr_type_node etc. which is
1848 just void* (or const void*). */
1849 if (last_structptr_types[j])
1850 {
1851 if (!comptypes (last_structptr_types[j], newtype))
1852 {
1853 *argno = i;
1854 *strict = last_structptr_types[j];
1855 }
1856 }
1857 else
1858 last_structptr_types[j] = newtype;
1859 break;
1860 }
1861
1862 if (j == nbst && !comptypes (oldtype, newtype))
1863 {
1864 if (POINTER_TYPE_P (oldtype))
1865 {
1866 /* For incompatible pointers, only reject differences in
1867 the unqualified variants of the referenced types but
1868 consider differences in qualifiers as benign (report
1869 those to caller via *STRICT below). */
1870 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1871 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1872 if (!comptypes (oldref, newref))
1873 return NULL_TREE;
1874 }
1875
1876 if (!*strict)
1877 {
1878 *argno = i;
1879 *strict = oldtype;
1880 }
1881 }
1882
1883 oldargs = TREE_CHAIN (oldargs);
1884 newargs = TREE_CHAIN (newargs);
1885 }
1886
1887 tree trytype = build_function_type (newrettype, tryargs);
1888
1889 /* Allow declaration to change transaction_safe attribute. */
1890 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1891 tree oldtsafe = lookup_attribute (attr_name: "transaction_safe", list: oldattrs);
1892 tree newattrs = TYPE_ATTRIBUTES (newtype);
1893 tree newtsafe = lookup_attribute (attr_name: "transaction_safe", list: newattrs);
1894 if (oldtsafe && !newtsafe)
1895 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1896 else if (newtsafe && !oldtsafe)
1897 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1898 NULL_TREE, oldattrs);
1899
1900 return build_type_attribute_variant (trytype, oldattrs);
1901}
1902
1903/* Subroutine of diagnose_mismatched_decls. Check for function type
1904 mismatch involving an empty arglist vs a nonempty one and give clearer
1905 diagnostics. */
1906static void
1907diagnose_arglist_conflict (tree newdecl, tree olddecl,
1908 tree newtype, tree oldtype)
1909{
1910 tree t;
1911
1912 if (TREE_CODE (olddecl) != FUNCTION_DECL
1913 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1914 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1915 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1916 return;
1917
1918 t = TYPE_ARG_TYPES (oldtype);
1919 if (t == NULL_TREE)
1920 t = TYPE_ARG_TYPES (newtype);
1921 for (; t; t = TREE_CHAIN (t))
1922 {
1923 tree type = TREE_VALUE (t);
1924
1925 if (TREE_CHAIN (t) == NULL_TREE
1926 && TYPE_MAIN_VARIANT (type) != void_type_node)
1927 {
1928 inform (input_location, "a parameter list with an ellipsis "
1929 "cannot match an empty parameter name list declaration");
1930 break;
1931 }
1932
1933 if (!error_operand_p (t: type)
1934 && c_type_promotes_to (type) != type)
1935 {
1936 inform (input_location, "an argument type that has a default "
1937 "promotion cannot match an empty parameter name list "
1938 "declaration");
1939 break;
1940 }
1941 }
1942}
1943
1944/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1945 old-style function definition, NEWDECL is a prototype declaration.
1946 Diagnose inconsistencies in the argument list. Returns TRUE if
1947 the prototype is compatible, FALSE if not. */
1948static bool
1949validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1950{
1951 tree newargs, oldargs;
1952 int i;
1953
1954#define END_OF_ARGLIST(t) ((t) == void_type_node)
1955
1956 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1957 newargs = TYPE_ARG_TYPES (newtype);
1958 i = 1;
1959
1960 for (;;)
1961 {
1962 tree oldargtype = TREE_VALUE (oldargs);
1963 tree newargtype = TREE_VALUE (newargs);
1964
1965 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1966 return false;
1967
1968 oldargtype = (TYPE_ATOMIC (oldargtype)
1969 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1970 TYPE_QUAL_ATOMIC)
1971 : TYPE_MAIN_VARIANT (oldargtype));
1972 newargtype = (TYPE_ATOMIC (newargtype)
1973 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1974 TYPE_QUAL_ATOMIC)
1975 : TYPE_MAIN_VARIANT (newargtype));
1976
1977 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1978 break;
1979
1980 /* Reaching the end of just one list means the two decls don't
1981 agree on the number of arguments. */
1982 if (END_OF_ARGLIST (oldargtype))
1983 {
1984 error ("prototype for %q+D declares more arguments "
1985 "than previous old-style definition", newdecl);
1986 return false;
1987 }
1988 else if (END_OF_ARGLIST (newargtype))
1989 {
1990 error ("prototype for %q+D declares fewer arguments "
1991 "than previous old-style definition", newdecl);
1992 return false;
1993 }
1994
1995 /* Type for passing arg must be consistent with that declared
1996 for the arg. */
1997 else if (!comptypes (oldargtype, newargtype))
1998 {
1999 error ("prototype for %q+D declares argument %d"
2000 " with incompatible type",
2001 newdecl, i);
2002 return false;
2003 }
2004
2005 oldargs = TREE_CHAIN (oldargs);
2006 newargs = TREE_CHAIN (newargs);
2007 i++;
2008 }
2009
2010 /* If we get here, no errors were found, but do issue a warning
2011 for this poor-style construct. */
2012 warning (0, "prototype for %q+D follows non-prototype definition",
2013 newdecl);
2014 return true;
2015#undef END_OF_ARGLIST
2016}
2017
2018/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2019 first in a pair of mismatched declarations, using the diagnostic
2020 function DIAG. */
2021static void
2022locate_old_decl (tree decl)
2023{
2024 if (TREE_CODE (decl) == FUNCTION_DECL
2025 && fndecl_built_in_p (node: decl)
2026 && !C_DECL_DECLARED_BUILTIN (decl))
2027 ;
2028 else if (DECL_INITIAL (decl))
2029 inform (input_location,
2030 "previous definition of %q+D with type %qT",
2031 decl, TREE_TYPE (decl));
2032 else if (C_DECL_IMPLICIT (decl))
2033 inform (input_location,
2034 "previous implicit declaration of %q+D with type %qT",
2035 decl, TREE_TYPE (decl));
2036 else
2037 inform (input_location,
2038 "previous declaration of %q+D with type %qT",
2039 decl, TREE_TYPE (decl));
2040}
2041
2042/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2043 Returns true if the caller should proceed to merge the two, false
2044 if OLDDECL should simply be discarded. As a side effect, issues
2045 all necessary diagnostics for invalid or poor-style combinations.
2046 If it returns true, writes the types of NEWDECL and OLDDECL to
2047 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2048 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2049
2050static bool
2051diagnose_mismatched_decls (tree newdecl, tree olddecl,
2052 tree *newtypep, tree *oldtypep)
2053{
2054 tree newtype, oldtype;
2055 bool retval = true;
2056
2057#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2058 && DECL_EXTERNAL (DECL))
2059
2060 /* If we have error_mark_node for either decl or type, just discard
2061 the previous decl - we're in an error cascade already. */
2062 if (olddecl == error_mark_node || newdecl == error_mark_node)
2063 return false;
2064 *oldtypep = oldtype = TREE_TYPE (olddecl);
2065 *newtypep = newtype = TREE_TYPE (newdecl);
2066 if (oldtype == error_mark_node || newtype == error_mark_node)
2067 return false;
2068
2069 /* Two different categories of symbol altogether. This is an error
2070 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2071 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2072 {
2073 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2074 && fndecl_built_in_p (node: olddecl)
2075 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2076 {
2077 auto_diagnostic_group d;
2078 error ("%q+D redeclared as different kind of symbol", newdecl);
2079 locate_old_decl (decl: olddecl);
2080 }
2081 else if (TREE_PUBLIC (newdecl))
2082 warning (OPT_Wbuiltin_declaration_mismatch,
2083 "built-in function %q+D declared as non-function",
2084 newdecl);
2085 else
2086 warning (OPT_Wshadow, "declaration of %q+D shadows "
2087 "a built-in function", newdecl);
2088 return false;
2089 }
2090
2091 /* Enumerators have no linkage, so may only be declared once in a
2092 given scope. */
2093 if (TREE_CODE (olddecl) == CONST_DECL)
2094 {
2095 auto_diagnostic_group d;
2096 error ("redeclaration of enumerator %q+D", newdecl);
2097 locate_old_decl (decl: olddecl);
2098 return false;
2099 }
2100
2101 bool pedwarned = false;
2102 bool warned = false;
2103 bool enum_and_int_p = false;
2104 auto_diagnostic_group d;
2105
2106 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2107 &enum_and_int_p);
2108 if (!comptypes_result)
2109 {
2110 if (TREE_CODE (olddecl) == FUNCTION_DECL
2111 && fndecl_built_in_p (node: olddecl, klass: BUILT_IN_NORMAL)
2112 && !C_DECL_DECLARED_BUILTIN (olddecl))
2113 {
2114 /* Accept "harmless" mismatches in function types such
2115 as missing qualifiers or int vs long when they're the same
2116 size. However, diagnose return and argument types that are
2117 incompatible according to language rules. */
2118 tree mismatch_expect;
2119 unsigned mismatch_argno;
2120
2121 tree trytype = match_builtin_function_types (newtype, oldtype,
2122 strict: &mismatch_expect,
2123 argno: &mismatch_argno);
2124
2125 if (trytype && comptypes (newtype, trytype))
2126 *oldtypep = oldtype = trytype;
2127 else
2128 {
2129 /* If types don't match for a built-in, throw away the
2130 built-in. No point in calling locate_old_decl here, it
2131 won't print anything. */
2132 const char *header = header_for_builtin_fn (olddecl);
2133 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2134 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2135 "conflicting types for built-in function %q+D; "
2136 "expected %qT",
2137 newdecl, oldtype)
2138 && header)
2139 {
2140 /* Suggest the right header to include as the preferred
2141 solution rather than the spelling of the declaration. */
2142 rich_location richloc (line_table, loc);
2143 maybe_add_include_fixit (&richloc, header, true);
2144 inform (&richloc,
2145 "%qD is declared in header %qs", olddecl, header);
2146 }
2147 return false;
2148 }
2149
2150 if (mismatch_expect && extra_warnings)
2151 {
2152 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2153 bool warned = false;
2154 if (mismatch_argno)
2155 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2156 "mismatch in argument %u type of built-in "
2157 "function %qD; expected %qT",
2158 mismatch_argno, newdecl, mismatch_expect);
2159 else
2160 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2161 "mismatch in return type of built-in "
2162 "function %qD; expected %qT",
2163 newdecl, mismatch_expect);
2164 const char *header = header_for_builtin_fn (olddecl);
2165 if (warned && header)
2166 {
2167 rich_location richloc (line_table, newloc);
2168 maybe_add_include_fixit (&richloc, header, true);
2169 inform (&richloc,
2170 "%qD is declared in header %qs", olddecl, header);
2171 }
2172 }
2173 }
2174 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2175 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2176 {
2177 /* A conflicting function declaration for a predeclared
2178 function that isn't actually built in. Objective C uses
2179 these. The new declaration silently overrides everything
2180 but the volatility (i.e. noreturn) indication. See also
2181 below. FIXME: Make Objective C use normal builtins. */
2182 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2183 return false;
2184 }
2185 /* Permit void foo (...) to match int foo (...) if the latter is
2186 the definition and implicit int was used. See
2187 c-torture/compile/920625-2.c. */
2188 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2189 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2190 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2191 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2192 {
2193 pedwarned = pedwarn (input_location, 0,
2194 "conflicting types for %q+D", newdecl);
2195 /* Make sure we keep void as the return type. */
2196 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2197 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2198 }
2199 /* Permit void foo (...) to match an earlier call to foo (...) with
2200 no declared type (thus, implicitly int). */
2201 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2202 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2203 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2204 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2205 {
2206 pedwarned = pedwarn (input_location, 0,
2207 "conflicting types for %q+D; have %qT",
2208 newdecl, newtype);
2209 /* Make sure we keep void as the return type. */
2210 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2211 }
2212 else
2213 {
2214 int new_quals = TYPE_QUALS (newtype);
2215 int old_quals = TYPE_QUALS (oldtype);
2216
2217 if (new_quals != old_quals)
2218 {
2219 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2220 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2221 if (new_addr != old_addr)
2222 {
2223 if (ADDR_SPACE_GENERIC_P (new_addr))
2224 error ("conflicting named address spaces (generic vs %s) "
2225 "for %q+D",
2226 c_addr_space_name (as: old_addr), newdecl);
2227 else if (ADDR_SPACE_GENERIC_P (old_addr))
2228 error ("conflicting named address spaces (%s vs generic) "
2229 "for %q+D",
2230 c_addr_space_name (as: new_addr), newdecl);
2231 else
2232 error ("conflicting named address spaces (%s vs %s) "
2233 "for %q+D",
2234 c_addr_space_name (as: new_addr),
2235 c_addr_space_name (as: old_addr),
2236 newdecl);
2237 }
2238
2239 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2240 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2241 error ("conflicting type qualifiers for %q+D", newdecl);
2242 }
2243 else
2244 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2245 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2246 locate_old_decl (decl: olddecl);
2247 return false;
2248 }
2249 }
2250 /* Warn about enum/integer type mismatches. They are compatible types
2251 (C23 6.7.2.2/5), but may pose portability problems. */
2252 else if (enum_and_int_p
2253 && TREE_CODE (newdecl) != TYPE_DECL
2254 /* Don't warn about about acc_on_device built-in redeclaration,
2255 the built-in is declared with int rather than enum because
2256 the enum isn't intrinsic. */
2257 && !(TREE_CODE (olddecl) == FUNCTION_DECL
2258 && fndecl_built_in_p (node: olddecl, name1: BUILT_IN_ACC_ON_DEVICE)
2259 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2260 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2261 OPT_Wenum_int_mismatch,
2262 "conflicting types for %q+D due to enum/integer "
2263 "mismatch; have %qT", newdecl, newtype);
2264
2265 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2266 but silently ignore the redeclaration if either is in a system
2267 header. (Conflicting redeclarations were handled above.) This
2268 is allowed for C11 if the types are the same, not just
2269 compatible. */
2270 if (TREE_CODE (newdecl) == TYPE_DECL)
2271 {
2272 bool types_different = false;
2273
2274 comptypes_result
2275 = comptypes_check_different_types (oldtype, newtype, &types_different);
2276
2277 if (comptypes_result != 1 || types_different)
2278 {
2279 error ("redefinition of typedef %q+D with different type", newdecl);
2280 locate_old_decl (decl: olddecl);
2281 return false;
2282 }
2283
2284 if (DECL_IN_SYSTEM_HEADER (newdecl)
2285 || DECL_IN_SYSTEM_HEADER (olddecl)
2286 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2287 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2288 return true; /* Allow OLDDECL to continue in use. */
2289
2290 if (c_type_variably_modified_p (t: newtype))
2291 {
2292 error ("redefinition of typedef %q+D with variably modified type",
2293 newdecl);
2294 locate_old_decl (decl: olddecl);
2295 }
2296 else if (pedwarn_c99 (input_location, opt: OPT_Wpedantic,
2297 "redefinition of typedef %q+D", newdecl))
2298 locate_old_decl (decl: olddecl);
2299
2300 return true;
2301 }
2302
2303 /* Function declarations can either be 'static' or 'extern' (no
2304 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2305 can never conflict with each other on account of linkage
2306 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2307 gnu89 mode permits two definitions if one is 'extern inline' and
2308 one is not. The non- extern-inline definition supersedes the
2309 extern-inline definition. */
2310
2311 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2312 {
2313 /* If you declare a built-in function name as static, or
2314 define the built-in with an old-style definition (so we
2315 can't validate the argument list) the built-in definition is
2316 overridden, but optionally warn this was a bad choice of name. */
2317 if (fndecl_built_in_p (node: olddecl)
2318 && !C_DECL_DECLARED_BUILTIN (olddecl))
2319 {
2320 if (!TREE_PUBLIC (newdecl)
2321 || (DECL_INITIAL (newdecl)
2322 && !prototype_p (TREE_TYPE (newdecl))))
2323 {
2324 warning_at (DECL_SOURCE_LOCATION (newdecl),
2325 OPT_Wshadow, "declaration of %qD shadows "
2326 "a built-in function", newdecl);
2327 /* Discard the old built-in function. */
2328 return false;
2329 }
2330
2331 if (!prototype_p (TREE_TYPE (newdecl)))
2332 {
2333 /* Set for built-ins that take no arguments. */
2334 bool func_void_args = false;
2335 if (tree at = TYPE_ARG_TYPES (oldtype))
2336 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2337
2338 if (extra_warnings && !func_void_args)
2339 warning_at (DECL_SOURCE_LOCATION (newdecl),
2340 OPT_Wbuiltin_declaration_mismatch,
2341 "declaration of built-in function %qD without "
2342 "a prototype; expected %qT",
2343 newdecl, TREE_TYPE (olddecl));
2344 }
2345 }
2346
2347 if (DECL_INITIAL (newdecl))
2348 {
2349 if (DECL_INITIAL (olddecl))
2350 {
2351 /* If the new declaration isn't overriding an extern inline
2352 reject the new decl. In c99, no overriding is allowed
2353 in the same translation unit. */
2354 if (!DECL_EXTERN_INLINE (olddecl)
2355 || DECL_EXTERN_INLINE (newdecl)
2356 || (!flag_gnu89_inline
2357 && (!DECL_DECLARED_INLINE_P (olddecl)
2358 || !lookup_attribute (attr_name: "gnu_inline",
2359 DECL_ATTRIBUTES (olddecl)))
2360 && (!DECL_DECLARED_INLINE_P (newdecl)
2361 || !lookup_attribute (attr_name: "gnu_inline",
2362 DECL_ATTRIBUTES (newdecl)))))
2363 {
2364 auto_diagnostic_group d;
2365 error ("redefinition of %q+D", newdecl);
2366 locate_old_decl (decl: olddecl);
2367 return false;
2368 }
2369 }
2370 }
2371 /* If we have a prototype after an old-style function definition,
2372 the argument types must be checked specially. */
2373 else if (DECL_INITIAL (olddecl)
2374 && !prototype_p (oldtype) && prototype_p (newtype)
2375 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2376 {
2377 auto_diagnostic_group d;
2378 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2379 {
2380 locate_old_decl (decl: olddecl);
2381 return false;
2382 }
2383 }
2384 /* A non-static declaration (even an "extern") followed by a
2385 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2386 The same is true for a static forward declaration at block
2387 scope followed by a non-static declaration/definition at file
2388 scope. Static followed by non-static at the same scope is
2389 not undefined behavior, and is the most convenient way to get
2390 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2391 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2392 we do diagnose it if -Wtraditional. */
2393 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2394 {
2395 /* Two exceptions to the rule. If olddecl is an extern
2396 inline, or a predeclared function that isn't actually
2397 built in, newdecl silently overrides olddecl. The latter
2398 occur only in Objective C; see also above. (FIXME: Make
2399 Objective C use normal builtins.) */
2400 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2401 && !DECL_EXTERN_INLINE (olddecl))
2402 {
2403 auto_diagnostic_group d;
2404 error ("static declaration of %q+D follows "
2405 "non-static declaration", newdecl);
2406 locate_old_decl (decl: olddecl);
2407 }
2408 return false;
2409 }
2410 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2411 {
2412 if (DECL_CONTEXT (olddecl))
2413 {
2414 auto_diagnostic_group d;
2415 error ("non-static declaration of %q+D follows "
2416 "static declaration", newdecl);
2417 locate_old_decl (decl: olddecl);
2418 return false;
2419 }
2420 else if (warn_traditional)
2421 {
2422 warned |= warning (OPT_Wtraditional,
2423 "non-static declaration of %q+D "
2424 "follows static declaration", newdecl);
2425 }
2426 }
2427
2428 /* Make sure gnu_inline attribute is either not present, or
2429 present on all inline decls. */
2430 if (DECL_DECLARED_INLINE_P (olddecl)
2431 && DECL_DECLARED_INLINE_P (newdecl))
2432 {
2433 bool newa = lookup_attribute (attr_name: "gnu_inline",
2434 DECL_ATTRIBUTES (newdecl)) != NULL;
2435 bool olda = lookup_attribute (attr_name: "gnu_inline",
2436 DECL_ATTRIBUTES (olddecl)) != NULL;
2437 if (newa != olda)
2438 {
2439 auto_diagnostic_group d;
2440 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2441 newa ? newdecl : olddecl);
2442 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2443 "but not here");
2444 }
2445 }
2446 }
2447 else if (VAR_P (newdecl))
2448 {
2449 /* Only variables can be thread-local, and all declarations must
2450 agree on this property. */
2451 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2452 {
2453 /* Nothing to check. Since OLDDECL is marked threadprivate
2454 and NEWDECL does not have a thread-local attribute, we
2455 will merge the threadprivate attribute into NEWDECL. */
2456 ;
2457 }
2458 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2459 {
2460 auto_diagnostic_group d;
2461 if (DECL_THREAD_LOCAL_P (newdecl))
2462 error ("thread-local declaration of %q+D follows "
2463 "non-thread-local declaration", newdecl);
2464 else
2465 error ("non-thread-local declaration of %q+D follows "
2466 "thread-local declaration", newdecl);
2467
2468 locate_old_decl (decl: olddecl);
2469 return false;
2470 }
2471
2472 /* Multiple initialized definitions are not allowed (6.9p3,5).
2473 For this purpose, C23 makes it clear that thread-local
2474 declarations without extern are definitions, not tentative
2475 definitions, whether or not they have initializers. The
2476 wording before C23 was unclear; literally it would have made
2477 uninitialized thread-local declarations into tentative
2478 definitions only if they also used static, but without saying
2479 explicitly whether or not other cases count as
2480 definitions at all. */
2481 if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2482 || (flag_isoc23
2483 && DECL_THREAD_LOCAL_P (newdecl)
2484 && !DECL_EXTERNAL (newdecl)
2485 && !DECL_EXTERNAL (olddecl)))
2486 {
2487 auto_diagnostic_group d;
2488 error ("redefinition of %q+D", newdecl);
2489 locate_old_decl (decl: olddecl);
2490 return false;
2491 }
2492
2493 /* Objects declared at file scope: if the first declaration had
2494 external linkage (even if it was an external reference) the
2495 second must have external linkage as well, or the behavior is
2496 undefined. If the first declaration had internal linkage, then
2497 the second must too, or else be an external reference (in which
2498 case the composite declaration still has internal linkage).
2499 As for function declarations, we warn about the static-then-
2500 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2501 if (DECL_FILE_SCOPE_P (newdecl)
2502 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2503 {
2504 if (DECL_EXTERNAL (newdecl))
2505 {
2506 if (!DECL_FILE_SCOPE_P (olddecl))
2507 {
2508 auto_diagnostic_group d;
2509 error ("extern declaration of %q+D follows "
2510 "declaration with no linkage", newdecl);
2511 locate_old_decl (decl: olddecl);
2512 return false;
2513 }
2514 else if (warn_traditional)
2515 {
2516 warned |= warning (OPT_Wtraditional,
2517 "non-static declaration of %q+D "
2518 "follows static declaration", newdecl);
2519 }
2520 }
2521 else
2522 {
2523 auto_diagnostic_group d;
2524 if (TREE_PUBLIC (newdecl))
2525 error ("non-static declaration of %q+D follows "
2526 "static declaration", newdecl);
2527 else
2528 error ("static declaration of %q+D follows "
2529 "non-static declaration", newdecl);
2530
2531 locate_old_decl (decl: olddecl);
2532 return false;
2533 }
2534 }
2535 /* Two objects with the same name declared at the same block
2536 scope must both be external references (6.7p3). */
2537 else if (!DECL_FILE_SCOPE_P (newdecl))
2538 {
2539 if (DECL_EXTERNAL (newdecl))
2540 {
2541 /* Extern with initializer at block scope, which will
2542 already have received an error. */
2543 }
2544 else if (DECL_EXTERNAL (olddecl))
2545 {
2546 auto_diagnostic_group d;
2547 error ("declaration of %q+D with no linkage follows "
2548 "extern declaration", newdecl);
2549 locate_old_decl (decl: olddecl);
2550 }
2551 else
2552 {
2553 auto_diagnostic_group d;
2554 error ("redeclaration of %q+D with no linkage", newdecl);
2555 locate_old_decl (decl: olddecl);
2556 }
2557
2558 return false;
2559 }
2560
2561 /* C++ does not permit a decl to appear multiple times at file
2562 scope. */
2563 if (warn_cxx_compat
2564 && DECL_FILE_SCOPE_P (newdecl)
2565 && !DECL_EXTERNAL (newdecl)
2566 && !DECL_EXTERNAL (olddecl))
2567 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2568 OPT_Wc___compat,
2569 ("duplicate declaration of %qD is "
2570 "invalid in C++"),
2571 newdecl);
2572 }
2573
2574 /* warnings */
2575 /* All decls must agree on a visibility. */
2576 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2577 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2578 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2579 {
2580 warned |= warning (0, "redeclaration of %q+D with different visibility "
2581 "(old visibility preserved)", newdecl);
2582 }
2583
2584 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2585 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2586 else /* PARM_DECL, VAR_DECL */
2587 {
2588 /* Redeclaration of a parameter is a constraint violation (this is
2589 not explicitly stated, but follows from C99 6.7p3 [no more than
2590 one declaration of the same identifier with no linkage in the
2591 same scope, except type tags] and 6.2.2p6 [parameters have no
2592 linkage]). We must check for a forward parameter declaration,
2593 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2594 an extension, the mandatory diagnostic for which is handled by
2595 mark_forward_parm_decls. */
2596
2597 if (TREE_CODE (newdecl) == PARM_DECL
2598 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2599 {
2600 auto_diagnostic_group d;
2601 error ("redefinition of parameter %q+D", newdecl);
2602 locate_old_decl (decl: olddecl);
2603 return false;
2604 }
2605 }
2606
2607 /* Optional warning for completely redundant decls. */
2608 if (!warned && !pedwarned
2609 && warn_redundant_decls
2610 /* Don't warn about a function declaration followed by a
2611 definition. */
2612 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2613 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2614 /* Don't warn about redundant redeclarations of builtins. */
2615 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2616 && !fndecl_built_in_p (node: newdecl)
2617 && fndecl_built_in_p (node: olddecl)
2618 && !C_DECL_DECLARED_BUILTIN (olddecl))
2619 /* Don't warn about an extern followed by a definition. */
2620 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2621 /* Don't warn about forward parameter decls. */
2622 && !(TREE_CODE (newdecl) == PARM_DECL
2623 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2624 /* Don't warn about a variable definition following a declaration. */
2625 && !(VAR_P (newdecl)
2626 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2627 {
2628 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2629 newdecl);
2630 }
2631
2632 /* Report location of previous decl/defn. */
2633 if (warned || pedwarned)
2634 locate_old_decl (decl: olddecl);
2635
2636#undef DECL_EXTERN_INLINE
2637
2638 return retval;
2639}
2640
2641/* Subroutine of duplicate_decls. NEWDECL has been found to be
2642 consistent with OLDDECL, but carries new information. Merge the
2643 new information into OLDDECL. This function issues no
2644 diagnostics. */
2645
2646static void
2647merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2648{
2649 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2650 && DECL_INITIAL (newdecl) != NULL_TREE);
2651 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2652 && prototype_p (TREE_TYPE (newdecl)));
2653 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2654 && prototype_p (TREE_TYPE (olddecl)));
2655
2656 /* For real parm decl following a forward decl, rechain the old decl
2657 in its new location and clear TREE_ASM_WRITTEN (it's not a
2658 forward decl anymore). */
2659 if (TREE_CODE (newdecl) == PARM_DECL
2660 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2661 {
2662 struct c_binding *b, **here;
2663
2664 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2665 if ((*here)->decl == olddecl)
2666 goto found;
2667 gcc_unreachable ();
2668
2669 found:
2670 b = *here;
2671 *here = b->prev;
2672 b->prev = current_scope->bindings;
2673 current_scope->bindings = b;
2674
2675 TREE_ASM_WRITTEN (olddecl) = 0;
2676 }
2677
2678 DECL_ATTRIBUTES (newdecl)
2679 = targetm.merge_decl_attributes (olddecl, newdecl);
2680
2681 /* For typedefs use the old type, as the new type's DECL_NAME points
2682 at newdecl, which will be ggc_freed. */
2683 if (TREE_CODE (newdecl) == TYPE_DECL)
2684 {
2685 /* But NEWTYPE might have an attribute, honor that. */
2686 tree tem = newtype;
2687 newtype = oldtype;
2688
2689 if (TYPE_USER_ALIGN (tem))
2690 {
2691 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2692 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2693 TYPE_USER_ALIGN (newtype) = true;
2694 }
2695
2696 /* And remove the new type from the variants list. */
2697 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2698 {
2699 tree remove = TREE_TYPE (newdecl);
2700 if (TYPE_MAIN_VARIANT (remove) == remove)
2701 {
2702 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2703 /* If remove is the main variant, no need to remove that
2704 from the list. One of the DECL_ORIGINAL_TYPE
2705 variants, e.g. created for aligned attribute, might still
2706 refer to the newdecl TYPE_DECL though, so remove that one
2707 in that case. */
2708 if (DECL_ORIGINAL_TYPE (newdecl)
2709 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2710 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2711 t; t = TYPE_MAIN_VARIANT (t))
2712 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2713 {
2714 TYPE_NEXT_VARIANT (t)
2715 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2716 break;
2717 }
2718 }
2719 else
2720 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2721 t = TYPE_NEXT_VARIANT (t))
2722 if (TYPE_NEXT_VARIANT (t) == remove)
2723 {
2724 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2725 break;
2726 }
2727 }
2728 }
2729
2730 /* Merge the data types specified in the two decls. */
2731 TREE_TYPE (newdecl)
2732 = TREE_TYPE (olddecl)
2733 = composite_type (newtype, oldtype);
2734
2735 /* Lay the type out, unless already done. */
2736 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2737 {
2738 if (TREE_TYPE (newdecl) != error_mark_node)
2739 layout_type (TREE_TYPE (newdecl));
2740 if (TREE_CODE (newdecl) != FUNCTION_DECL
2741 && TREE_CODE (newdecl) != TYPE_DECL
2742 && TREE_CODE (newdecl) != CONST_DECL)
2743 layout_decl (newdecl, 0);
2744 }
2745 else
2746 {
2747 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2748 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2749 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2750 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2751 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2752 {
2753 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2754 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2755 }
2756 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2757 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2758 DECL_USER_ALIGN (newdecl) = 1;
2759 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2760 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2761 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2762 DECL_WARN_IF_NOT_ALIGN (olddecl));
2763 }
2764
2765 /* Keep the old rtl since we can safely use it. */
2766 if (HAS_RTL_P (olddecl))
2767 COPY_DECL_RTL (olddecl, newdecl);
2768
2769 /* Merge the type qualifiers. */
2770 if (TREE_READONLY (newdecl))
2771 TREE_READONLY (olddecl) = 1;
2772
2773 if (TREE_THIS_VOLATILE (newdecl))
2774 TREE_THIS_VOLATILE (olddecl) = 1;
2775
2776 /* Merge deprecatedness. */
2777 if (TREE_DEPRECATED (newdecl))
2778 TREE_DEPRECATED (olddecl) = 1;
2779
2780 /* Merge unavailability. */
2781 if (TREE_UNAVAILABLE (newdecl))
2782 TREE_UNAVAILABLE (olddecl) = 1;
2783
2784 /* If a decl is in a system header and the other isn't, keep the one on the
2785 system header. Otherwise, keep source location of definition rather than
2786 declaration and of prototype rather than non-prototype unless that
2787 prototype is built-in. */
2788 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2789 && DECL_IN_SYSTEM_HEADER (olddecl)
2790 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2791 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2792 else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2793 && DECL_IN_SYSTEM_HEADER (newdecl)
2794 && !DECL_IN_SYSTEM_HEADER (olddecl))
2795 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2796 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2797 && DECL_INITIAL (olddecl) != NULL_TREE)
2798 || (old_is_prototype && !new_is_prototype
2799 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2800 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2801
2802 /* Merge the initialization information. */
2803 if (DECL_INITIAL (newdecl) == NULL_TREE)
2804 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2805
2806 /* Merge 'constexpr' information. */
2807 if (VAR_P (olddecl) && VAR_P (newdecl))
2808 {
2809 if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2810 C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2811 else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2812 C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2813 }
2814
2815 /* Merge the threadprivate attribute. */
2816 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2817 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2818
2819 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2820 {
2821 /* Copy the assembler name.
2822 Currently, it can only be defined in the prototype. */
2823 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2824
2825 /* Use visibility of whichever declaration had it specified */
2826 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2827 {
2828 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2829 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2830 }
2831
2832 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2833 {
2834 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2835 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2836 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2837 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2838 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2839 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2840 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2841 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2842 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2843 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2844 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2845 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2846 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2847 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2848 }
2849
2850 /* Merge the storage class information. */
2851 merge_weak (newdecl, olddecl);
2852
2853 /* For functions, static overrides non-static. */
2854 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2855 {
2856 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2857 /* This is since we don't automatically
2858 copy the attributes of NEWDECL into OLDDECL. */
2859 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2860 /* If this clears `static', clear it in the identifier too. */
2861 if (!TREE_PUBLIC (olddecl))
2862 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2863 }
2864 }
2865
2866 /* In c99, 'extern' declaration before (or after) 'inline' means this
2867 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2868 is present. */
2869 if (TREE_CODE (newdecl) == FUNCTION_DECL
2870 && !flag_gnu89_inline
2871 && (DECL_DECLARED_INLINE_P (newdecl)
2872 || DECL_DECLARED_INLINE_P (olddecl))
2873 && (!DECL_DECLARED_INLINE_P (newdecl)
2874 || !DECL_DECLARED_INLINE_P (olddecl)
2875 || !DECL_EXTERNAL (olddecl))
2876 && DECL_EXTERNAL (newdecl)
2877 && !lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (newdecl))
2878 && !current_function_decl)
2879 DECL_EXTERNAL (newdecl) = 0;
2880
2881 /* An inline definition following a static declaration is not
2882 DECL_EXTERNAL. */
2883 if (new_is_definition
2884 && (DECL_DECLARED_INLINE_P (newdecl)
2885 || DECL_DECLARED_INLINE_P (olddecl))
2886 && !TREE_PUBLIC (olddecl))
2887 DECL_EXTERNAL (newdecl) = 0;
2888
2889 if (DECL_EXTERNAL (newdecl))
2890 {
2891 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2892 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2893
2894 /* An extern decl does not override previous storage class. */
2895 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2896 if (!DECL_EXTERNAL (newdecl))
2897 {
2898 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2899 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2900 }
2901 }
2902 else
2903 {
2904 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2905 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2906 }
2907
2908 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2909 {
2910 /* If we're redefining a function previously defined as extern
2911 inline, make sure we emit debug info for the inline before we
2912 throw it away, in case it was inlined into a function that
2913 hasn't been written out yet. */
2914 if (new_is_definition && DECL_INITIAL (olddecl))
2915 /* The new defn must not be inline. */
2916 DECL_UNINLINABLE (newdecl) = 1;
2917 else
2918 {
2919 /* If either decl says `inline', this fn is inline, unless
2920 its definition was passed already. */
2921 if (DECL_DECLARED_INLINE_P (newdecl)
2922 || DECL_DECLARED_INLINE_P (olddecl))
2923 DECL_DECLARED_INLINE_P (newdecl) = 1;
2924
2925 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2926 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2927
2928 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2929 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2930 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2931 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2932 }
2933
2934 if (fndecl_built_in_p (node: olddecl))
2935 {
2936 /* If redeclaring a builtin function, it stays built in.
2937 But it gets tagged as having been declared. */
2938 copy_decl_built_in_function (newdecl, olddecl);
2939 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2940 if (new_is_prototype)
2941 {
2942 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2943 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2944 {
2945 enum built_in_function fncode = DECL_FUNCTION_CODE (decl: newdecl);
2946 switch (fncode)
2947 {
2948 /* If a compatible prototype of these builtin functions
2949 is seen, assume the runtime implements it with the
2950 expected semantics. */
2951 case BUILT_IN_STPCPY:
2952 if (builtin_decl_explicit_p (fncode))
2953 set_builtin_decl_implicit_p (fncode, implicit_p: true);
2954 break;
2955 default:
2956 if (builtin_decl_explicit_p (fncode))
2957 set_builtin_decl_declared_p (fncode, declared_p: true);
2958 break;
2959 }
2960
2961 copy_attributes_to_builtin (newdecl);
2962 }
2963 }
2964 else
2965 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2966 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2967 }
2968
2969 /* Preserve function specific target and optimization options */
2970 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2971 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2972 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2973 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2974
2975 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2976 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2977 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2978 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2979
2980 /* Also preserve various other info from the definition. */
2981 if (!new_is_definition)
2982 {
2983 tree t;
2984 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2985 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2986 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2987 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2988 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2989 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2990 DECL_CONTEXT (t) = newdecl;
2991
2992 /* See if we've got a function to instantiate from. */
2993 if (DECL_SAVED_TREE (olddecl))
2994 DECL_ABSTRACT_ORIGIN (newdecl)
2995 = DECL_ABSTRACT_ORIGIN (olddecl);
2996 }
2997 }
2998
2999 /* Merge the USED information. */
3000 if (TREE_USED (olddecl))
3001 TREE_USED (newdecl) = 1;
3002 else if (TREE_USED (newdecl))
3003 TREE_USED (olddecl) = 1;
3004 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3005 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3006 if (DECL_PRESERVE_P (olddecl))
3007 DECL_PRESERVE_P (newdecl) = 1;
3008 else if (DECL_PRESERVE_P (newdecl))
3009 DECL_PRESERVE_P (olddecl) = 1;
3010
3011 /* Merge DECL_COMMON */
3012 if (VAR_P (olddecl) && VAR_P (newdecl)
3013 && !lookup_attribute (attr_name: "common", DECL_ATTRIBUTES (newdecl))
3014 && !lookup_attribute (attr_name: "nocommon", DECL_ATTRIBUTES (newdecl)))
3015 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3016
3017 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3018 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3019 DECL_ARGUMENTS (if appropriate). */
3020 {
3021 unsigned olddecl_uid = DECL_UID (olddecl);
3022 tree olddecl_context = DECL_CONTEXT (olddecl);
3023 tree olddecl_arguments = NULL;
3024 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3025 olddecl_arguments = DECL_ARGUMENTS (olddecl);
3026
3027 memcpy (dest: (char *) olddecl + sizeof (struct tree_common),
3028 src: (char *) newdecl + sizeof (struct tree_common),
3029 n: sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3030 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3031 switch (TREE_CODE (olddecl))
3032 {
3033 case FUNCTION_DECL:
3034 case VAR_DECL:
3035 {
3036 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3037
3038 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3039 src: (char *) newdecl + sizeof (struct tree_decl_common),
3040 n: tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3041 olddecl->decl_with_vis.symtab_node = snode;
3042
3043 if ((DECL_EXTERNAL (olddecl)
3044 || TREE_PUBLIC (olddecl)
3045 || TREE_STATIC (olddecl))
3046 && DECL_SECTION_NAME (newdecl) != NULL)
3047 set_decl_section_name (olddecl, newdecl);
3048
3049 /* This isn't quite correct for something like
3050 int __thread x attribute ((tls_model ("local-exec")));
3051 extern int __thread x;
3052 as we'll lose the "local-exec" model. */
3053 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3054 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3055 break;
3056 }
3057
3058 case FIELD_DECL:
3059 case PARM_DECL:
3060 case LABEL_DECL:
3061 case RESULT_DECL:
3062 case CONST_DECL:
3063 case TYPE_DECL:
3064 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3065 src: (char *) newdecl + sizeof (struct tree_decl_common),
3066 n: tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3067 break;
3068
3069 default:
3070
3071 memcpy (dest: (char *) olddecl + sizeof (struct tree_decl_common),
3072 src: (char *) newdecl + sizeof (struct tree_decl_common),
3073 n: sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3074 }
3075 DECL_UID (olddecl) = olddecl_uid;
3076 DECL_CONTEXT (olddecl) = olddecl_context;
3077 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3078 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3079 }
3080
3081 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3082 so that encode_section_info has a chance to look at the new decl
3083 flags and attributes. */
3084 if (DECL_RTL_SET_P (olddecl)
3085 && (TREE_CODE (olddecl) == FUNCTION_DECL
3086 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3087 make_decl_rtl (olddecl);
3088}
3089
3090/* Handle when a new declaration NEWDECL has the same name as an old
3091 one OLDDECL in the same binding contour. Prints an error message
3092 if appropriate.
3093
3094 If safely possible, alter OLDDECL to look like NEWDECL, and return
3095 true. Otherwise, return false. */
3096
3097static bool
3098duplicate_decls (tree newdecl, tree olddecl)
3099{
3100 tree newtype = NULL, oldtype = NULL;
3101
3102 if (!diagnose_mismatched_decls (newdecl, olddecl, newtypep: &newtype, oldtypep: &oldtype))
3103 {
3104 /* Avoid `unused variable' and other warnings for OLDDECL. */
3105 suppress_warning (olddecl, OPT_Wunused);
3106 /* If the types are completely different, poison them both with
3107 error_mark_node. */
3108 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3109 && olddecl != error_mark_node
3110 && seen_error ())
3111 {
3112 if (TREE_CODE (olddecl) != FUNCTION_DECL)
3113 TREE_TYPE (olddecl) = error_mark_node;
3114 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3115 TREE_TYPE (newdecl) = error_mark_node;
3116 }
3117 return false;
3118 }
3119
3120 merge_decls (newdecl, olddecl, newtype, oldtype);
3121
3122 /* The NEWDECL will no longer be needed.
3123
3124 Before releasing the node, be sure to remove function from symbol
3125 table that might have been inserted there to record comdat group.
3126 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3127 structure is shared in between NEWDECL and OLDECL. */
3128 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3129 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3130 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3131 {
3132 struct symtab_node *snode = symtab_node::get (decl: newdecl);
3133 if (snode)
3134 snode->remove ();
3135 }
3136 ggc_free (newdecl);
3137 return true;
3138}
3139
3140
3141/* Check whether decl-node NEW_DECL shadows an existing declaration. */
3142static void
3143warn_if_shadowing (tree new_decl)
3144{
3145 struct c_binding *b;
3146
3147 /* Shadow warnings wanted? */
3148 if (!(warn_shadow
3149 || warn_shadow_local
3150 || warn_shadow_compatible_local)
3151 /* No shadow warnings for internally generated vars. */
3152 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3153 return;
3154
3155 /* Is anything being shadowed? Invisible decls do not count. */
3156 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3157 if (b->decl && b->decl != new_decl && !b->invisible
3158 && (b->decl == error_mark_node
3159 || diagnostic_report_warnings_p (global_dc,
3160 DECL_SOURCE_LOCATION (b->decl))))
3161 {
3162 tree old_decl = b->decl;
3163
3164 if (old_decl == error_mark_node)
3165 {
3166 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3167 "non-variable", new_decl);
3168 break;
3169 }
3170
3171 bool warned = false;
3172 auto_diagnostic_group d;
3173 if (TREE_CODE (old_decl) == PARM_DECL)
3174 {
3175 enum opt_code warning_code;
3176
3177 /* If '-Wshadow=compatible-local' is specified without other
3178 -Wshadow= flags, we will warn only when the types of the
3179 shadowing variable (i.e. new_decl) and the shadowed variable
3180 (old_decl) are compatible. */
3181 if (warn_shadow)
3182 warning_code = OPT_Wshadow;
3183 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3184 warning_code = OPT_Wshadow_compatible_local;
3185 else
3186 warning_code = OPT_Wshadow_local;
3187 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3188 "declaration of %qD shadows a parameter",
3189 new_decl);
3190 }
3191 else if (DECL_FILE_SCOPE_P (old_decl))
3192 {
3193 /* Do not warn if a variable shadows a function, unless
3194 the variable is a function or a pointer-to-function. */
3195 if (TREE_CODE (old_decl) == FUNCTION_DECL
3196 && TREE_CODE (new_decl) != FUNCTION_DECL
3197 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3198 continue;
3199
3200 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3201 "declaration of %qD shadows a global "
3202 "declaration",
3203 new_decl);
3204 }
3205 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3206 && fndecl_built_in_p (node: old_decl))
3207 {
3208 warning (OPT_Wshadow, "declaration of %q+D shadows "
3209 "a built-in function", new_decl);
3210 break;
3211 }
3212 else
3213 {
3214 enum opt_code warning_code;
3215
3216 /* If '-Wshadow=compatible-local' is specified without other
3217 -Wshadow= flags, we will warn only when the types of the
3218 shadowing variable (i.e. new_decl) and the shadowed variable
3219 (old_decl) are compatible. */
3220 if (warn_shadow)
3221 warning_code = OPT_Wshadow;
3222 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3223 warning_code = OPT_Wshadow_compatible_local;
3224 else
3225 warning_code = OPT_Wshadow_local;
3226 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3227 "declaration of %qD shadows a previous local",
3228 new_decl);
3229 }
3230
3231 if (warned)
3232 inform (DECL_SOURCE_LOCATION (old_decl),
3233 "shadowed declaration is here");
3234
3235 break;
3236 }
3237}
3238
3239/* Record a decl-node X as belonging to the current lexical scope.
3240 Check for errors (such as an incompatible declaration for the same
3241 name already seen in the same scope).
3242
3243 Returns either X or an old decl for the same name.
3244 If an old decl is returned, it may have been smashed
3245 to agree with what X says. */
3246
3247tree
3248pushdecl (tree x)
3249{
3250 tree name = DECL_NAME (x);
3251 struct c_scope *scope = current_scope;
3252 struct c_binding *b;
3253 bool nested = false;
3254 location_t locus = DECL_SOURCE_LOCATION (x);
3255
3256 /* Must set DECL_CONTEXT for everything not at file scope or
3257 DECL_FILE_SCOPE_P won't work. Local externs don't count
3258 unless they have initializers (which generate code). */
3259 if (current_function_decl
3260 && (!VAR_OR_FUNCTION_DECL_P (x)
3261 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3262 DECL_CONTEXT (x) = current_function_decl;
3263
3264 /* Anonymous decls are just inserted in the scope. */
3265 if (!name)
3266 {
3267 bind (name, decl: x, scope, /*invisible=*/false, /*nested=*/false,
3268 locus);
3269 return x;
3270 }
3271
3272 /* First, see if there is another declaration with the same name in
3273 the current scope. If there is, duplicate_decls may do all the
3274 work for us. If duplicate_decls returns false, that indicates
3275 two incompatible decls in the same scope; we are to silently
3276 replace the old one (duplicate_decls has issued all appropriate
3277 diagnostics). In particular, we should not consider possible
3278 duplicates in the external scope, or shadowing. */
3279 b = I_SYMBOL_BINDING (name);
3280 if (b && B_IN_SCOPE (b, scope))
3281 {
3282 struct c_binding *b_ext, *b_use;
3283 tree type = TREE_TYPE (x);
3284 tree visdecl = b->decl;
3285 tree vistype = TREE_TYPE (visdecl);
3286 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3287 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3288 b->inner_comp = false;
3289 b_use = b;
3290 b_ext = b;
3291 /* If this is an external linkage declaration, we should check
3292 for compatibility with the type in the external scope before
3293 setting the type at this scope based on the visible
3294 information only. */
3295 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3296 {
3297 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3298 b_ext = b_ext->shadowed;
3299 if (b_ext)
3300 {
3301 b_use = b_ext;
3302 if (b_use->u.type)
3303 TREE_TYPE (b_use->decl) = b_use->u.type;
3304 }
3305 }
3306 if (duplicate_decls (newdecl: x, olddecl: b_use->decl))
3307 {
3308 if (b_use != b)
3309 {
3310 /* Save the updated type in the external scope and
3311 restore the proper type for this scope. */
3312 tree thistype;
3313 if (comptypes (vistype, type))
3314 thistype = composite_type (vistype, type);
3315 else
3316 thistype = TREE_TYPE (b_use->decl);
3317 b_use->u.type = TREE_TYPE (b_use->decl);
3318 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3319 && fndecl_built_in_p (node: b_use->decl))
3320 thistype
3321 = build_type_attribute_variant (thistype,
3322 TYPE_ATTRIBUTES
3323 (b_use->u.type));
3324 TREE_TYPE (b_use->decl) = thistype;
3325 }
3326 return b_use->decl;
3327 }
3328 else
3329 goto skip_external_and_shadow_checks;
3330 }
3331
3332 /* All declarations with external linkage, and all external
3333 references, go in the external scope, no matter what scope is
3334 current. However, the binding in that scope is ignored for
3335 purposes of normal name lookup. A separate binding structure is
3336 created in the requested scope; this governs the normal
3337 visibility of the symbol.
3338
3339 The binding in the externals scope is used exclusively for
3340 detecting duplicate declarations of the same object, no matter
3341 what scope they are in; this is what we do here. (C99 6.2.7p2:
3342 All declarations that refer to the same object or function shall
3343 have compatible type; otherwise, the behavior is undefined.)
3344 However, in Objective-C, we also want to detect declarations
3345 conflicting with those of the basic types. */
3346 if ((DECL_EXTERNAL (x) || scope == file_scope)
3347 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3348 {
3349 tree type = TREE_TYPE (x);
3350 tree vistype = NULL_TREE;
3351 tree visdecl = NULL_TREE;
3352 bool type_saved = false;
3353 if (b && !B_IN_EXTERNAL_SCOPE (b)
3354 && VAR_OR_FUNCTION_DECL_P (b->decl)
3355 && DECL_FILE_SCOPE_P (b->decl))
3356 {
3357 visdecl = b->decl;
3358 vistype = TREE_TYPE (visdecl);
3359 }
3360 if (scope != file_scope
3361 && !DECL_IN_SYSTEM_HEADER (x))
3362 warning_at (locus, OPT_Wnested_externs,
3363 "nested extern declaration of %qD", x);
3364
3365 while (b && !B_IN_EXTERNAL_SCOPE (b))
3366 {
3367 /* If this decl might be modified, save its type. This is
3368 done here rather than when the decl is first bound
3369 because the type may change after first binding, through
3370 being completed or through attributes being added. If we
3371 encounter multiple such decls, only the first should have
3372 its type saved; the others will already have had their
3373 proper types saved and the types will not have changed as
3374 their scopes will not have been re-entered. */
3375 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3376 {
3377 b->u.type = TREE_TYPE (b->decl);
3378 type_saved = true;
3379 }
3380 if (B_IN_FILE_SCOPE (b)
3381 && VAR_P (b->decl)
3382 && TREE_STATIC (b->decl)
3383 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3384 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3385 && TREE_CODE (type) == ARRAY_TYPE
3386 && TYPE_DOMAIN (type)
3387 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3388 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3389 {
3390 /* Array type completed in inner scope, which should be
3391 diagnosed if the completion does not have size 1 and
3392 it does not get completed in the file scope. */
3393 b->inner_comp = true;
3394 }
3395 b = b->shadowed;
3396 }
3397
3398 /* If a matching external declaration has been found, set its
3399 type to the composite of all the types of that declaration.
3400 After the consistency checks, it will be reset to the
3401 composite of the visible types only. */
3402 if (b && b->u.type)
3403 TREE_TYPE (b->decl) = b->u.type;
3404
3405 /* the static does not go in the externals scope. */
3406 if (b && duplicate_decls (newdecl: x, olddecl: b->decl))
3407 {
3408 tree thistype;
3409 if (vistype)
3410 {
3411 if (comptypes (vistype, type))
3412 thistype = composite_type (vistype, type);
3413 else
3414 thistype = TREE_TYPE (b->decl);
3415 }
3416 else
3417 thistype = type;
3418 b->u.type = TREE_TYPE (b->decl);
3419 /* Propagate the type attributes to the decl. */
3420 thistype
3421 = build_type_attribute_variant (thistype,
3422 TYPE_ATTRIBUTES (b->u.type));
3423 TREE_TYPE (b->decl) = thistype;
3424 bind (name, decl: b->decl, scope, /*invisible=*/false, /*nested=*/true,
3425 locus);
3426 return b->decl;
3427 }
3428 else if (TREE_PUBLIC (x))
3429 {
3430 if (visdecl && !b && duplicate_decls (newdecl: x, olddecl: visdecl))
3431 {
3432 /* An external declaration at block scope referring to a
3433 visible entity with internal linkage. The composite
3434 type will already be correct for this scope, so we
3435 just need to fall through to make the declaration in
3436 this scope. */
3437 nested = true;
3438 x = visdecl;
3439 }
3440 else
3441 {
3442 bind (name, decl: x, scope: external_scope, /*invisible=*/true,
3443 /*nested=*/false, locus);
3444 nested = true;
3445 }
3446 }
3447 }
3448
3449 if (TREE_CODE (x) != PARM_DECL)
3450 warn_if_shadowing (new_decl: x);
3451
3452 skip_external_and_shadow_checks:
3453 if (TREE_CODE (x) == TYPE_DECL)
3454 {
3455 /* So this is a typedef, set its underlying type. */
3456 set_underlying_type (x);
3457
3458 /* If X is a typedef defined in the current function, record it
3459 for the purpose of implementing the -Wunused-local-typedefs
3460 warning. */
3461 record_locally_defined_typedef (x);
3462 }
3463
3464 bind (name, decl: x, scope, /*invisible=*/false, nested, locus);
3465
3466 /* If x's type is incomplete because it's based on a
3467 structure or union which has not yet been fully declared,
3468 attach it to that structure or union type, so we can go
3469 back and complete the variable declaration later, if the
3470 structure or union gets fully declared.
3471
3472 If the input is erroneous, we can have error_mark in the type
3473 slot (e.g. "f(void a, ...)") - that doesn't count as an
3474 incomplete type. */
3475 if (TREE_TYPE (x) != error_mark_node
3476 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3477 {
3478 tree element = TREE_TYPE (x);
3479
3480 while (TREE_CODE (element) == ARRAY_TYPE)
3481 element = TREE_TYPE (element);
3482 element = TYPE_MAIN_VARIANT (element);
3483
3484 if ((RECORD_OR_UNION_TYPE_P (element)
3485 || TREE_CODE (element) == ENUMERAL_TYPE)
3486 && (TREE_CODE (x) != TYPE_DECL
3487 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3488 && !COMPLETE_TYPE_P (element))
3489 C_TYPE_INCOMPLETE_VARS (element)
3490 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3491 }
3492 return x;
3493}
3494
3495
3496/* Issue a warning about implicit function declaration. ID is the function
3497 identifier, OLDDECL is a declaration of the function in a different scope,
3498 or NULL_TREE. */
3499
3500static void
3501implicit_decl_warning (location_t loc, tree id, tree olddecl)
3502{
3503 if (!warn_implicit_function_declaration)
3504 return;
3505
3506 bool warned;
3507 auto_diagnostic_group d;
3508 name_hint hint;
3509 if (!olddecl)
3510 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3511
3512 if (flag_isoc99)
3513 {
3514 if (const char *suggestion = hint.suggestion ())
3515 {
3516 gcc_rich_location richloc (loc);
3517 richloc.add_fixit_replace (new_content: suggestion);
3518 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3519 "implicit declaration of function %qE;"
3520 " did you mean %qs?",
3521 id, suggestion);
3522 }
3523 else
3524 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3525 "implicit declaration of function %qE", id);
3526 }
3527 else if (const char *suggestion = hint.suggestion ())
3528 {
3529 gcc_rich_location richloc (loc);
3530 richloc.add_fixit_replace (new_content: suggestion);
3531 warned = warning_at
3532 (&richloc, OPT_Wimplicit_function_declaration,
3533 G_("implicit declaration of function %qE; did you mean %qs?"),
3534 id, suggestion);
3535 }
3536 else
3537 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3538 G_("implicit declaration of function %qE"), id);
3539
3540 if (warned)
3541 {
3542 /* Whether the olddecl is an undeclared builtin function.
3543 locate_old_decl will not generate a diagnostic for those,
3544 so in that case we want to look elsewhere. */
3545 bool undeclared_builtin = (olddecl
3546 && TREE_CODE (olddecl) == FUNCTION_DECL
3547 && fndecl_built_in_p (node: olddecl)
3548 && !C_DECL_DECLARED_BUILTIN (olddecl));
3549 if (undeclared_builtin)
3550 {
3551 const char *header = header_for_builtin_fn (olddecl);
3552 if (header)
3553 {
3554 rich_location richloc (line_table, loc);
3555 maybe_add_include_fixit (&richloc, header, true);
3556 inform (&richloc,
3557 "include %qs or provide a declaration of %qE",
3558 header, id);
3559 }
3560 }
3561 else if (olddecl)
3562 locate_old_decl (decl: olddecl);
3563 }
3564
3565 if (!warned)
3566 hint.suppress ();
3567}
3568
3569/* Return the name of the header file that declares built-in function
3570 FNDECL, or null if either we don't know or don't expect to see an
3571 explicit declaration. */
3572
3573static const char *
3574header_for_builtin_fn (tree fndecl)
3575{
3576 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3577 return NULL;
3578
3579 switch (DECL_FUNCTION_CODE (decl: fndecl))
3580 {
3581 CASE_FLT_FN (BUILT_IN_ACOS):
3582 CASE_FLT_FN (BUILT_IN_ACOSH):
3583 CASE_FLT_FN (BUILT_IN_ASIN):
3584 CASE_FLT_FN (BUILT_IN_ASINH):
3585 CASE_FLT_FN (BUILT_IN_ATAN):
3586 CASE_FLT_FN (BUILT_IN_ATANH):
3587 CASE_FLT_FN (BUILT_IN_ATAN2):
3588 CASE_FLT_FN (BUILT_IN_CBRT):
3589 CASE_FLT_FN (BUILT_IN_CEIL):
3590 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3591 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3592 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3593 CASE_FLT_FN (BUILT_IN_COS):
3594 CASE_FLT_FN (BUILT_IN_COSH):
3595 CASE_FLT_FN (BUILT_IN_ERF):
3596 CASE_FLT_FN (BUILT_IN_ERFC):
3597 CASE_FLT_FN (BUILT_IN_EXP):
3598 CASE_FLT_FN (BUILT_IN_EXP2):
3599 CASE_FLT_FN (BUILT_IN_EXPM1):
3600 CASE_FLT_FN (BUILT_IN_FABS):
3601 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3602 CASE_FLT_FN (BUILT_IN_FDIM):
3603 CASE_FLT_FN (BUILT_IN_FLOOR):
3604 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3605 CASE_FLT_FN (BUILT_IN_FMA):
3606 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3607 CASE_FLT_FN (BUILT_IN_FMAX):
3608 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3609 CASE_FLT_FN (BUILT_IN_FMIN):
3610 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3611 CASE_FLT_FN (BUILT_IN_FMOD):
3612 CASE_FLT_FN (BUILT_IN_FREXP):
3613 CASE_FLT_FN (BUILT_IN_HYPOT):
3614 CASE_FLT_FN (BUILT_IN_ILOGB):
3615 CASE_FLT_FN (BUILT_IN_LDEXP):
3616 CASE_FLT_FN (BUILT_IN_LGAMMA):
3617 CASE_FLT_FN (BUILT_IN_LLRINT):
3618 CASE_FLT_FN (BUILT_IN_LLROUND):
3619 CASE_FLT_FN (BUILT_IN_LOG):
3620 CASE_FLT_FN (BUILT_IN_LOG10):
3621 CASE_FLT_FN (BUILT_IN_LOG1P):
3622 CASE_FLT_FN (BUILT_IN_LOG2):
3623 CASE_FLT_FN (BUILT_IN_LOGB):
3624 CASE_FLT_FN (BUILT_IN_LRINT):
3625 CASE_FLT_FN (BUILT_IN_LROUND):
3626 CASE_FLT_FN (BUILT_IN_MODF):
3627 CASE_FLT_FN (BUILT_IN_NAN):
3628 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3629 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3630 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3631 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3632 CASE_FLT_FN (BUILT_IN_POW):
3633 CASE_FLT_FN (BUILT_IN_REMAINDER):
3634 CASE_FLT_FN (BUILT_IN_REMQUO):
3635 CASE_FLT_FN (BUILT_IN_RINT):
3636 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3637 CASE_FLT_FN (BUILT_IN_ROUND):
3638 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3639 CASE_FLT_FN (BUILT_IN_SCALBLN):
3640 CASE_FLT_FN (BUILT_IN_SCALBN):
3641 CASE_FLT_FN (BUILT_IN_SIN):
3642 CASE_FLT_FN (BUILT_IN_SINH):
3643 CASE_FLT_FN (BUILT_IN_SINCOS):
3644 CASE_FLT_FN (BUILT_IN_SQRT):
3645 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3646 CASE_FLT_FN (BUILT_IN_TAN):
3647 CASE_FLT_FN (BUILT_IN_TANH):
3648 CASE_FLT_FN (BUILT_IN_TGAMMA):
3649 CASE_FLT_FN (BUILT_IN_TRUNC):
3650 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3651 case BUILT_IN_ISINF:
3652 case BUILT_IN_ISNAN:
3653 return "<math.h>";
3654 CASE_FLT_FN (BUILT_IN_CABS):
3655 CASE_FLT_FN (BUILT_IN_CACOS):
3656 CASE_FLT_FN (BUILT_IN_CACOSH):
3657 CASE_FLT_FN (BUILT_IN_CARG):
3658 CASE_FLT_FN (BUILT_IN_CASIN):
3659 CASE_FLT_FN (BUILT_IN_CASINH):
3660 CASE_FLT_FN (BUILT_IN_CATAN):
3661 CASE_FLT_FN (BUILT_IN_CATANH):
3662 CASE_FLT_FN (BUILT_IN_CCOS):
3663 CASE_FLT_FN (BUILT_IN_CCOSH):
3664 CASE_FLT_FN (BUILT_IN_CEXP):
3665 CASE_FLT_FN (BUILT_IN_CIMAG):
3666 CASE_FLT_FN (BUILT_IN_CLOG):
3667 CASE_FLT_FN (BUILT_IN_CONJ):
3668 CASE_FLT_FN (BUILT_IN_CPOW):
3669 CASE_FLT_FN (BUILT_IN_CPROJ):
3670 CASE_FLT_FN (BUILT_IN_CREAL):
3671 CASE_FLT_FN (BUILT_IN_CSIN):
3672 CASE_FLT_FN (BUILT_IN_CSINH):
3673 CASE_FLT_FN (BUILT_IN_CSQRT):
3674 CASE_FLT_FN (BUILT_IN_CTAN):
3675 CASE_FLT_FN (BUILT_IN_CTANH):
3676 return "<complex.h>";
3677 case BUILT_IN_MEMCHR:
3678 case BUILT_IN_MEMCMP:
3679 case BUILT_IN_MEMCPY:
3680 case BUILT_IN_MEMMOVE:
3681 case BUILT_IN_MEMSET:
3682 case BUILT_IN_STRCAT:
3683 case BUILT_IN_STRCHR:
3684 case BUILT_IN_STRCMP:
3685 case BUILT_IN_STRCPY:
3686 case BUILT_IN_STRCSPN:
3687 case BUILT_IN_STRLEN:
3688 case BUILT_IN_STRNCAT:
3689 case BUILT_IN_STRNCMP:
3690 case BUILT_IN_STRNCPY:
3691 case BUILT_IN_STRPBRK:
3692 case BUILT_IN_STRRCHR:
3693 case BUILT_IN_STRSPN:
3694 case BUILT_IN_STRSTR:
3695 return "<string.h>";
3696 case BUILT_IN_FPRINTF:
3697 case BUILT_IN_PUTC:
3698 case BUILT_IN_FPUTC:
3699 case BUILT_IN_FPUTS:
3700 case BUILT_IN_FSCANF:
3701 case BUILT_IN_FWRITE:
3702 case BUILT_IN_PRINTF:
3703 case BUILT_IN_PUTCHAR:
3704 case BUILT_IN_PUTS:
3705 case BUILT_IN_SCANF:
3706 case BUILT_IN_SNPRINTF:
3707 case BUILT_IN_SPRINTF:
3708 case BUILT_IN_SSCANF:
3709 case BUILT_IN_VFPRINTF:
3710 case BUILT_IN_VFSCANF:
3711 case BUILT_IN_VPRINTF:
3712 case BUILT_IN_VSCANF:
3713 case BUILT_IN_VSNPRINTF:
3714 case BUILT_IN_VSPRINTF:
3715 case BUILT_IN_VSSCANF:
3716 return "<stdio.h>";
3717 case BUILT_IN_ISALNUM:
3718 case BUILT_IN_ISALPHA:
3719 case BUILT_IN_ISBLANK:
3720 case BUILT_IN_ISCNTRL:
3721 case BUILT_IN_ISDIGIT:
3722 case BUILT_IN_ISGRAPH:
3723 case BUILT_IN_ISLOWER:
3724 case BUILT_IN_ISPRINT:
3725 case BUILT_IN_ISPUNCT:
3726 case BUILT_IN_ISSPACE:
3727 case BUILT_IN_ISUPPER:
3728 case BUILT_IN_ISXDIGIT:
3729 case BUILT_IN_TOLOWER:
3730 case BUILT_IN_TOUPPER:
3731 return "<ctype.h>";
3732 case BUILT_IN_ISWALNUM:
3733 case BUILT_IN_ISWALPHA:
3734 case BUILT_IN_ISWBLANK:
3735 case BUILT_IN_ISWCNTRL:
3736 case BUILT_IN_ISWDIGIT:
3737 case BUILT_IN_ISWGRAPH:
3738 case BUILT_IN_ISWLOWER:
3739 case BUILT_IN_ISWPRINT:
3740 case BUILT_IN_ISWPUNCT:
3741 case BUILT_IN_ISWSPACE:
3742 case BUILT_IN_ISWUPPER:
3743 case BUILT_IN_ISWXDIGIT:
3744 case BUILT_IN_TOWLOWER:
3745 case BUILT_IN_TOWUPPER:
3746 return "<wctype.h>";
3747 case BUILT_IN_ABORT:
3748 case BUILT_IN_ABS:
3749 case BUILT_IN_CALLOC:
3750 case BUILT_IN_EXIT:
3751 case BUILT_IN_FREE:
3752 case BUILT_IN_LABS:
3753 case BUILT_IN_LLABS:
3754 case BUILT_IN_MALLOC:
3755 case BUILT_IN_REALLOC:
3756 case BUILT_IN__EXIT2:
3757 case BUILT_IN_ALIGNED_ALLOC:
3758 return "<stdlib.h>";
3759 case BUILT_IN_IMAXABS:
3760 return "<inttypes.h>";
3761 case BUILT_IN_STRFTIME:
3762 return "<time.h>";
3763 default:
3764 return NULL;
3765 }
3766}
3767
3768/* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3769 function of type int (). */
3770
3771tree
3772implicitly_declare (location_t loc, tree functionid)
3773{
3774 struct c_binding *b;
3775 tree decl = NULL_TREE;
3776 tree asmspec_tree;
3777
3778 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3779 {
3780 if (B_IN_SCOPE (b, external_scope))
3781 {
3782 decl = b->decl;
3783 break;
3784 }
3785 }
3786
3787 if (decl)
3788 {
3789 if (TREE_CODE (decl) != FUNCTION_DECL)
3790 return decl;
3791
3792 /* FIXME: Objective-C has weird not-really-builtin functions
3793 which are supposed to be visible automatically. They wind up
3794 in the external scope because they're pushed before the file
3795 scope gets created. Catch this here and rebind them into the
3796 file scope. */
3797 if (!fndecl_built_in_p (node: decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3798 {
3799 bind (name: functionid, decl, scope: file_scope,
3800 /*invisible=*/false, /*nested=*/true,
3801 DECL_SOURCE_LOCATION (decl));
3802 return decl;
3803 }
3804 else
3805 {
3806 tree newtype = default_function_type;
3807 if (b->u.type)
3808 TREE_TYPE (decl) = b->u.type;
3809 /* Implicit declaration of a function already declared
3810 (somehow) in a different scope, or as a built-in.
3811 If this is the first time this has happened, warn;
3812 then recycle the old declaration but with the new type. */
3813 if (!C_DECL_IMPLICIT (decl))
3814 {
3815 implicit_decl_warning (loc, id: functionid, olddecl: decl);
3816 C_DECL_IMPLICIT (decl) = 1;
3817 }
3818 if (fndecl_built_in_p (node: decl))
3819 {
3820 newtype = build_type_attribute_variant (newtype,
3821 TYPE_ATTRIBUTES
3822 (TREE_TYPE (decl)));
3823 if (!comptypes (newtype, TREE_TYPE (decl)))
3824 {
3825 auto_diagnostic_group d;
3826 bool warned = warning_at (loc,
3827 OPT_Wbuiltin_declaration_mismatch,
3828 "incompatible implicit "
3829 "declaration of built-in "
3830 "function %qD", decl);
3831 /* See if we can hint which header to include. */
3832 const char *header = header_for_builtin_fn (fndecl: decl);
3833 if (header != NULL && warned)
3834 {
3835 rich_location richloc (line_table, loc);
3836 maybe_add_include_fixit (&richloc, header, true);
3837 inform (&richloc,
3838 "include %qs or provide a declaration of %qD",
3839 header, decl);
3840 }
3841 newtype = TREE_TYPE (decl);
3842 }
3843 }
3844 else
3845 {
3846 if (!comptypes (newtype, TREE_TYPE (decl)))
3847 {
3848 auto_diagnostic_group d;
3849 error_at (loc, "incompatible implicit declaration of "
3850 "function %qD", decl);
3851 locate_old_decl (decl);
3852 }
3853 }
3854 b->u.type = TREE_TYPE (decl);
3855 TREE_TYPE (decl) = newtype;
3856 bind (name: functionid, decl, scope: current_scope,
3857 /*invisible=*/false, /*nested=*/true,
3858 DECL_SOURCE_LOCATION (decl));
3859 return decl;
3860 }
3861 }
3862
3863 /* Not seen before. */
3864 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3865 DECL_EXTERNAL (decl) = 1;
3866 TREE_PUBLIC (decl) = 1;
3867 C_DECL_IMPLICIT (decl) = 1;
3868 implicit_decl_warning (loc, id: functionid, olddecl: 0);
3869 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3870 if (asmspec_tree)
3871 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3872
3873 /* C89 says implicit declarations are in the innermost block.
3874 So we record the decl in the standard fashion. */
3875 decl = pushdecl (x: decl);
3876
3877 /* No need to call objc_check_decl here - it's a function type. */
3878 rest_of_decl_compilation (decl, 0, 0);
3879
3880 /* Write a record describing this implicit function declaration
3881 to the prototypes file (if requested). */
3882 gen_aux_info_record (decl, 0, 1, 0);
3883
3884 /* Possibly apply some default attributes to this implicit declaration. */
3885 decl_attributes (&decl, NULL_TREE, 0);
3886
3887 return decl;
3888}
3889
3890/* Issue an error message for a reference to an undeclared variable
3891 ID, including a reference to a builtin outside of function-call
3892 context. Establish a binding of the identifier to error_mark_node
3893 in an appropriate scope, which will suppress further errors for the
3894 same identifier. The error message should be given location LOC. */
3895void
3896undeclared_variable (location_t loc, tree id)
3897{
3898 static bool already = false;
3899 struct c_scope *scope;
3900
3901 auto_diagnostic_group d;
3902 if (current_function_decl == NULL_TREE)
3903 {
3904 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3905 if (const char *suggestion = guessed_id.suggestion ())
3906 {
3907 gcc_rich_location richloc (loc);
3908 richloc.add_fixit_replace (new_content: suggestion);
3909 error_at (&richloc,
3910 "%qE undeclared here (not in a function);"
3911 " did you mean %qs?",
3912 id, suggestion);
3913 }
3914 else
3915 error_at (loc, "%qE undeclared here (not in a function)", id);
3916 scope = current_scope;
3917 }
3918 else
3919 {
3920 if (!objc_diagnose_private_ivar (id))
3921 {
3922 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3923 if (const char *suggestion = guessed_id.suggestion ())
3924 {
3925 gcc_rich_location richloc (loc);
3926 richloc.add_fixit_replace (new_content: suggestion);
3927 error_at (&richloc,
3928 "%qE undeclared (first use in this function);"
3929 " did you mean %qs?",
3930 id, suggestion);
3931 }
3932 else
3933 error_at (loc, "%qE undeclared (first use in this function)", id);
3934 }
3935 if (!already)
3936 {
3937 inform (loc, "each undeclared identifier is reported only"
3938 " once for each function it appears in");
3939 already = true;
3940 }
3941
3942 /* If we are parsing old-style parameter decls, current_function_decl
3943 will be nonnull but current_function_scope will be null. */
3944 scope = current_function_scope ? current_function_scope : current_scope;
3945 }
3946 bind (name: id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3947 UNKNOWN_LOCATION);
3948}
3949
3950/* Subroutine of lookup_label, declare_label, define_label: construct a
3951 LABEL_DECL with all the proper frills. Also create a struct
3952 c_label_vars initialized for the current scope. */
3953
3954static tree
3955make_label (location_t location, tree name, bool defining,
3956 struct c_label_vars **p_label_vars)
3957{
3958 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3959 DECL_CONTEXT (label) = current_function_decl;
3960 SET_DECL_MODE (label, VOIDmode);
3961
3962 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3963 label_vars->shadowed = NULL;
3964 set_spot_bindings (p: &label_vars->label_bindings, defining);
3965 label_vars->decls_in_scope = make_tree_vector ();
3966 label_vars->gotos = NULL;
3967 *p_label_vars = label_vars;
3968
3969 return label;
3970}
3971
3972/* Get the LABEL_DECL corresponding to identifier NAME as a label.
3973 Create one if none exists so far for the current function.
3974 This is called when a label is used in a goto expression or
3975 has its address taken. */
3976
3977tree
3978lookup_label (tree name)
3979{
3980 tree label;
3981 struct c_label_vars *label_vars;
3982
3983 if (current_function_scope == 0)
3984 {
3985 error ("label %qE referenced outside of any function", name);
3986 return NULL_TREE;
3987 }
3988
3989 /* Use a label already defined or ref'd with this name, but not if
3990 it is inherited from a containing function and wasn't declared
3991 using __label__. */
3992 label = I_LABEL_DECL (name);
3993 if (label && (DECL_CONTEXT (label) == current_function_decl
3994 || C_DECLARED_LABEL_FLAG (label)))
3995 {
3996 /* If the label has only been declared, update its apparent
3997 location to point here, for better diagnostics if it
3998 turns out not to have been defined. */
3999 if (DECL_INITIAL (label) == NULL_TREE)
4000 DECL_SOURCE_LOCATION (label) = input_location;
4001 return label;
4002 }
4003
4004 /* No label binding for that identifier; make one. */
4005 label = make_label (location: input_location, name, defining: false, p_label_vars: &label_vars);
4006
4007 /* Ordinary labels go in the current function scope. */
4008 bind_label (name, label, scope: current_function_scope, label_vars);
4009
4010 return label;
4011}
4012
4013/* Issue a warning about DECL for a goto statement at GOTO_LOC going
4014 to LABEL. */
4015
4016static void
4017warn_about_goto (location_t goto_loc, tree label, tree decl)
4018{
4019 auto_diagnostic_group d;
4020 if (c_type_variably_modified_p (TREE_TYPE (decl)))
4021 error_at (goto_loc,
4022 "jump into scope of identifier with variably modified type");
4023 else if (flag_openmp
4024 && lookup_attribute (attr_name: "omp allocate", DECL_ATTRIBUTES (decl)))
4025 error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4026 else
4027 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4028 "jump skips variable initialization"))
4029 return;
4030 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4031 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4032}
4033
4034/* Look up a label because of a goto statement. This is like
4035 lookup_label, but also issues any appropriate warnings. */
4036
4037tree
4038lookup_label_for_goto (location_t loc, tree name)
4039{
4040 tree label;
4041 struct c_label_vars *label_vars;
4042 unsigned int ix;
4043 tree decl;
4044
4045 label = lookup_label (name);
4046 if (label == NULL_TREE)
4047 return NULL_TREE;
4048
4049 /* If we are jumping to a different function, we can't issue any
4050 useful warnings. */
4051 if (DECL_CONTEXT (label) != current_function_decl)
4052 {
4053 gcc_assert (C_DECLARED_LABEL_FLAG (label));
4054 return label;
4055 }
4056
4057 label_vars = I_LABEL_BINDING (name)->u.label;
4058
4059 /* If the label has not yet been defined, then push this goto on a
4060 list for possible later warnings. */
4061 if (label_vars->label_bindings.scope == NULL)
4062 {
4063 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4064
4065 g->loc = loc;
4066 set_spot_bindings (p: &g->goto_bindings, defining: true);
4067 vec_safe_push (v&: label_vars->gotos, obj: g);
4068 return label;
4069 }
4070
4071 /* If there are any decls in label_vars->decls_in_scope, then this
4072 goto has missed the declaration of the decl. This happens for a
4073 case like
4074 int i = 1;
4075 lab:
4076 ...
4077 goto lab;
4078 Issue a warning or error. */
4079 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4080 warn_about_goto (goto_loc: loc, label, decl);
4081
4082 if (label_vars->label_bindings.left_stmt_expr)
4083 {
4084 auto_diagnostic_group d;
4085 error_at (loc, "jump into statement expression");
4086 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4087 }
4088
4089 return label;
4090}
4091
4092/* Make a label named NAME in the current function, shadowing silently
4093 any that may be inherited from containing functions or containing
4094 scopes. This is called for __label__ declarations. */
4095
4096tree
4097declare_label (tree name)
4098{
4099 struct c_binding *b = I_LABEL_BINDING (name);
4100 tree label;
4101 struct c_label_vars *label_vars;
4102
4103 /* Check to make sure that the label hasn't already been declared
4104 at this scope */
4105 if (b && B_IN_CURRENT_SCOPE (b))
4106 {
4107 auto_diagnostic_group d;
4108 error ("duplicate label declaration %qE", name);
4109 locate_old_decl (decl: b->decl);
4110
4111 /* Just use the previous declaration. */
4112 return b->decl;
4113 }
4114
4115 label = make_label (location: input_location, name, defining: false, p_label_vars: &label_vars);
4116 C_DECLARED_LABEL_FLAG (label) = 1;
4117
4118 /* Declared labels go in the current scope. */
4119 bind_label (name, label, scope: current_scope, label_vars);
4120
4121 return label;
4122}
4123
4124/* When we define a label, issue any appropriate warnings if there are
4125 any gotos earlier in the function which jump to this label. */
4126
4127static void
4128check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4129{
4130 unsigned int ix;
4131 struct c_goto_bindings *g;
4132
4133 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4134 {
4135 struct c_binding *b;
4136 struct c_scope *scope;
4137
4138 /* We have a goto to this label. The goto is going forward. In
4139 g->scope, the goto is going to skip any binding which was
4140 defined after g->bindings_in_scope. */
4141 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4142 {
4143 for (b = g->goto_bindings.scope->bindings;
4144 b != g->goto_bindings.bindings_in_scope;
4145 b = b->prev)
4146 {
4147 if (decl_jump_unsafe (decl: b->decl))
4148 warn_about_goto (goto_loc: g->loc, label, decl: b->decl);
4149 }
4150 }
4151
4152 /* We also need to warn about decls defined in any scopes
4153 between the scope of the label and the scope of the goto. */
4154 for (scope = label_vars->label_bindings.scope;
4155 scope != g->goto_bindings.scope;
4156 scope = scope->outer)
4157 {
4158 gcc_assert (scope != NULL);
4159 if (scope->has_jump_unsafe_decl)
4160 {
4161 if (scope == label_vars->label_bindings.scope)
4162 b = label_vars->label_bindings.bindings_in_scope;
4163 else
4164 b = scope->bindings;
4165 for (; b != NULL; b = b->prev)
4166 {
4167 if (decl_jump_unsafe (decl: b->decl))
4168 warn_about_goto (goto_loc: g->loc, label, decl: b->decl);
4169 }
4170 }
4171 }
4172
4173 if (g->goto_bindings.stmt_exprs > 0)
4174 {
4175 auto_diagnostic_group d;
4176 error_at (g->loc, "jump into statement expression");
4177 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4178 label);
4179 }
4180 }
4181
4182 /* Now that the label is defined, we will issue warnings about
4183 subsequent gotos to this label when we see them. */
4184 vec_safe_truncate (v: label_vars->gotos, size: 0);
4185 label_vars->gotos = NULL;
4186}
4187
4188/* Define a label, specifying the location in the source file.
4189 Return the LABEL_DECL node for the label, if the definition is valid.
4190 Otherwise return NULL_TREE. */
4191
4192tree
4193define_label (location_t location, tree name)
4194{
4195 /* Find any preexisting label with this name. It is an error
4196 if that label has already been defined in this function, or
4197 if there is a containing function with a declared label with
4198 the same name. */
4199 tree label = I_LABEL_DECL (name);
4200
4201 if (label
4202 && ((DECL_CONTEXT (label) == current_function_decl
4203 && DECL_INITIAL (label) != NULL_TREE)
4204 || (DECL_CONTEXT (label) != current_function_decl
4205 && C_DECLARED_LABEL_FLAG (label))))
4206 {
4207 auto_diagnostic_group d;
4208 error_at (location, "duplicate label %qD", label);
4209 locate_old_decl (decl: label);
4210 return NULL_TREE;
4211 }
4212 else if (label && DECL_CONTEXT (label) == current_function_decl)
4213 {
4214 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4215
4216 /* The label has been used or declared already in this function,
4217 but not defined. Update its location to point to this
4218 definition. */
4219 DECL_SOURCE_LOCATION (label) = location;
4220 set_spot_bindings (p: &label_vars->label_bindings, defining: true);
4221
4222 /* Issue warnings as required about any goto statements from
4223 earlier in the function. */
4224 check_earlier_gotos (label, label_vars);
4225 }
4226 else
4227 {
4228 struct c_label_vars *label_vars;
4229
4230 /* No label binding for that identifier; make one. */
4231 label = make_label (location, name, defining: true, p_label_vars: &label_vars);
4232
4233 /* Ordinary labels go in the current function scope. */
4234 bind_label (name, label, scope: current_function_scope, label_vars);
4235 }
4236
4237 if (!in_system_header_at (loc: input_location) && lookup_name (name))
4238 warning_at (location, OPT_Wtraditional,
4239 "traditional C lacks a separate namespace "
4240 "for labels, identifier %qE conflicts", name);
4241
4242 /* Mark label as having been defined. */
4243 DECL_INITIAL (label) = error_mark_node;
4244 return label;
4245}
4246
4247/* Get the bindings for a new switch statement. This is used to issue
4248 warnings as appropriate for jumps from the switch to case or
4249 default labels. */
4250
4251struct c_spot_bindings *
4252c_get_switch_bindings (void)
4253{
4254 struct c_spot_bindings *switch_bindings;
4255
4256 switch_bindings = XNEW (struct c_spot_bindings);
4257 set_spot_bindings (p: switch_bindings, defining: true);
4258 return switch_bindings;
4259}
4260
4261void
4262c_release_switch_bindings (struct c_spot_bindings *bindings)
4263{
4264 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4265 XDELETE (bindings);
4266}
4267
4268/* This is called at the point of a case or default label to issue
4269 warnings about decls as needed. It returns true if it found an
4270 error, not just a warning. */
4271
4272bool
4273c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4274 location_t switch_loc, location_t case_loc)
4275{
4276 bool saw_error;
4277 struct c_scope *scope;
4278
4279 saw_error = false;
4280 for (scope = current_scope;
4281 scope != switch_bindings->scope;
4282 scope = scope->outer)
4283 {
4284 struct c_binding *b;
4285
4286 gcc_assert (scope != NULL);
4287
4288 if (!scope->has_jump_unsafe_decl)
4289 continue;
4290
4291 for (b = scope->bindings; b != NULL; b = b->prev)
4292 {
4293 if (decl_jump_unsafe (decl: b->decl))
4294 {
4295 auto_diagnostic_group d;
4296 bool emitted;
4297 if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4298 {
4299 saw_error = true;
4300 error_at (case_loc,
4301 ("switch jumps into scope of identifier with "
4302 "variably modified type"));
4303 emitted = true;
4304 }
4305 else if (flag_openmp
4306 && lookup_attribute (attr_name: "omp allocate",
4307 DECL_ATTRIBUTES (b->decl)))
4308 {
4309 saw_error = true;
4310 error_at (case_loc,
4311 "switch jumps over OpenMP %<allocate%> allocation");
4312 emitted = true;
4313 }
4314 else
4315 emitted
4316 = warning_at (case_loc, OPT_Wjump_misses_init,
4317 "switch jumps over variable initialization");
4318 if (emitted)
4319 {
4320 inform (switch_loc, "switch starts here");
4321 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4322 b->decl);
4323 }
4324 }
4325 }
4326 }
4327
4328 if (switch_bindings->stmt_exprs > 0)
4329 {
4330 saw_error = true;
4331 auto_diagnostic_group d;
4332 error_at (case_loc, "switch jumps into statement expression");
4333 inform (switch_loc, "switch starts here");
4334 }
4335
4336 return saw_error;
4337}
4338
4339/* Given NAME, an IDENTIFIER_NODE,
4340 return the structure (or union or enum) definition for that name.
4341 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4342 CODE says which kind of type the caller wants;
4343 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4344 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4345 location where the tag was defined.
4346 If the wrong kind of type is found, an error is reported. */
4347
4348static tree
4349lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4350 location_t *ploc)
4351{
4352 struct c_binding *b = I_TAG_BINDING (name);
4353 bool thislevel = false;
4354
4355 if (!b || !b->decl)
4356 return NULL_TREE;
4357
4358 /* We only care about whether it's in this level if
4359 thislevel_only was set or it might be a type clash. */
4360 if (thislevel_only || TREE_CODE (b->decl) != code)
4361 {
4362 /* For our purposes, a tag in the external scope is the same as
4363 a tag in the file scope. (Primarily relevant to Objective-C
4364 and its builtin structure tags, which get pushed before the
4365 file scope is created.) */
4366 if (B_IN_CURRENT_SCOPE (b)
4367 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4368 thislevel = true;
4369 }
4370
4371 if (thislevel_only && !thislevel)
4372 return NULL_TREE;
4373
4374 if (TREE_CODE (b->decl) != code)
4375 {
4376 /* Definition isn't the kind we were looking for. */
4377 pending_invalid_xref = name;
4378 pending_invalid_xref_location = input_location;
4379
4380 /* If in the same binding level as a declaration as a tag
4381 of a different type, this must not be allowed to
4382 shadow that tag, so give the error immediately.
4383 (For example, "struct foo; union foo;" is invalid.) */
4384 if (thislevel)
4385 pending_xref_error ();
4386 }
4387
4388 if (ploc != NULL)
4389 *ploc = b->locus;
4390
4391 return b->decl;
4392}
4393
4394/* Return true if a definition exists for NAME with code CODE. */
4395
4396bool
4397tag_exists_p (enum tree_code code, tree name)
4398{
4399 struct c_binding *b = I_TAG_BINDING (name);
4400
4401 if (b == NULL || b->decl == NULL_TREE)
4402 return false;
4403 return TREE_CODE (b->decl) == code;
4404}
4405
4406/* Print an error message now
4407 for a recent invalid struct, union or enum cross reference.
4408 We don't print them immediately because they are not invalid
4409 when used in the `struct foo;' construct for shadowing. */
4410
4411void
4412pending_xref_error (void)
4413{
4414 if (pending_invalid_xref != NULL_TREE)
4415 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4416 pending_invalid_xref);
4417 pending_invalid_xref = NULL_TREE;
4418}
4419
4420
4421/* Look up NAME in the current scope and its superiors
4422 in the namespace of variables, functions and typedefs.
4423 Return a ..._DECL node of some kind representing its definition,
4424 or return NULL_TREE if it is undefined. */
4425
4426tree
4427lookup_name (tree name)
4428{
4429 struct c_binding *b = I_SYMBOL_BINDING (name);
4430 if (b && !b->invisible)
4431 {
4432 maybe_record_typedef_use (b->decl);
4433 return b->decl;
4434 }
4435 return NULL_TREE;
4436}
4437
4438/* Similar to `lookup_name' but look only at the indicated scope. */
4439
4440static tree
4441lookup_name_in_scope (tree name, struct c_scope *scope)
4442{
4443 struct c_binding *b;
4444
4445 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4446 if (B_IN_SCOPE (b, scope))
4447 return b->decl;
4448 return NULL_TREE;
4449}
4450
4451/* Look for the closest match for NAME within the currently valid
4452 scopes.
4453
4454 This finds the identifier with the lowest Levenshtein distance to
4455 NAME. If there are multiple candidates with equal minimal distance,
4456 the first one found is returned. Scopes are searched from innermost
4457 outwards, and within a scope in reverse order of declaration, thus
4458 benefiting candidates "near" to the current scope.
4459
4460 The function also looks for similar macro names to NAME, since a
4461 misspelled macro name will not be expanded, and hence looks like an
4462 identifier to the C frontend.
4463
4464 It also looks for start_typename keywords, to detect "singed" vs "signed"
4465 typos.
4466
4467 Use LOC for any deferred diagnostics. */
4468
4469name_hint
4470lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4471{
4472 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4473
4474 /* First, try some well-known names in the C standard library, in case
4475 the user forgot a #include. */
4476 const char *header_hint
4477 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4478
4479 if (header_hint)
4480 return name_hint (NULL,
4481 new suggest_missing_header (loc,
4482 IDENTIFIER_POINTER (name),
4483 header_hint));
4484
4485 /* Only suggest names reserved for the implementation if NAME begins
4486 with an underscore. */
4487 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4488
4489 best_match<tree, tree> bm (name);
4490
4491 /* Look within currently valid scopes. */
4492 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4493 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4494 {
4495 if (!binding->id || binding->invisible)
4496 continue;
4497 if (binding->decl == error_mark_node)
4498 continue;
4499 /* Don't use bindings from implicitly declared functions,
4500 as they were likely misspellings themselves. */
4501 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4502 if (C_DECL_IMPLICIT (binding->decl))
4503 continue;
4504 /* Don't suggest names that are reserved for use by the
4505 implementation, unless NAME began with an underscore. */
4506 if (!consider_implementation_names)
4507 {
4508 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4509 if (name_reserved_for_implementation_p (str: suggestion_str))
4510 continue;
4511 }
4512 switch (kind)
4513 {
4514 case FUZZY_LOOKUP_TYPENAME:
4515 if (TREE_CODE (binding->decl) != TYPE_DECL)
4516 continue;
4517 break;
4518
4519 case FUZZY_LOOKUP_FUNCTION_NAME:
4520 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4521 {
4522 /* Allow function pointers. */
4523 if ((VAR_P (binding->decl)
4524 || TREE_CODE (binding->decl) == PARM_DECL)
4525 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4526 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4527 == FUNCTION_TYPE))
4528 break;
4529 continue;
4530 }
4531 break;
4532
4533 default:
4534 break;
4535 }
4536 bm.consider (candidate: binding->id);
4537 }
4538
4539 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4540 as:
4541 x = SOME_OTHER_MACRO (y);
4542 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4543 as a misspelled identifier.
4544
4545 Use the best distance so far so that a candidate is only set if
4546 a macro is better than anything so far. This allows early rejection
4547 (without calculating the edit distance) of macro names that must have
4548 distance >= bm.get_best_distance (), and means that we only get a
4549 non-NULL result for best_macro_match if it's better than any of
4550 the identifiers already checked, which avoids needless creation
4551 of identifiers for macro hashnodes. */
4552 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4553 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4554 /* If a macro is the closest so far to NAME, use it, creating an
4555 identifier tree node for it. */
4556 if (best_macro)
4557 {
4558 const char *id = (const char *)best_macro->ident.str;
4559 tree macro_as_identifier
4560 = get_identifier_with_length (id, best_macro->ident.len);
4561 bm.set_best_so_far (best_candidate: macro_as_identifier,
4562 best_distance: bmm.get_best_distance (),
4563 best_candidate_len: bmm.get_best_candidate_length ());
4564 }
4565
4566 /* Try the "start_typename" keywords to detect
4567 "singed" vs "signed" typos. */
4568 if (kind == FUZZY_LOOKUP_TYPENAME)
4569 {
4570 for (unsigned i = 0; i < num_c_common_reswords; i++)
4571 {
4572 const c_common_resword *resword = &c_common_reswords[i];
4573 if (!c_keyword_starts_typename (keyword: resword->rid))
4574 continue;
4575 tree resword_identifier = ridpointers [resword->rid];
4576 if (!resword_identifier)
4577 continue;
4578 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4579 bm.consider (candidate: resword_identifier);
4580 }
4581 }
4582
4583 tree best = bm.get_best_meaningful_candidate ();
4584 if (best)
4585 return name_hint (IDENTIFIER_POINTER (best), NULL);
4586 else
4587 return name_hint (NULL, NULL);
4588}
4589
4590
4591/* Handle the standard [[nodiscard]] attribute. */
4592
4593static tree
4594handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4595 int /*flags*/, bool *no_add_attrs)
4596{
4597 if (TREE_CODE (*node) == FUNCTION_DECL)
4598 {
4599 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4600 warning_at (DECL_SOURCE_LOCATION (*node),
4601 OPT_Wattributes, "%qE attribute applied to %qD with void "
4602 "return type", name, *node);
4603 }
4604 else if (RECORD_OR_UNION_TYPE_P (*node)
4605 || TREE_CODE (*node) == ENUMERAL_TYPE)
4606 /* OK */;
4607 else
4608 {
4609 pedwarn (input_location,
4610 OPT_Wattributes, "%qE attribute can only be applied to "
4611 "functions or to structure, union or enumeration types", name);
4612 *no_add_attrs = true;
4613 }
4614 return NULL_TREE;
4615}
4616
4617/* Handle the standard [[noreturn]] attribute. */
4618
4619static tree
4620handle_std_noreturn_attribute (tree *node, tree name, tree args,
4621 int flags, bool *no_add_attrs)
4622{
4623 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4624 only applies to functions, not function pointers. */
4625 if (TREE_CODE (*node) == FUNCTION_DECL)
4626 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4627 else
4628 {
4629 pedwarn (input_location, OPT_Wattributes,
4630 "standard %qE attribute can only be applied to functions",
4631 name);
4632 *no_add_attrs = true;
4633 return NULL_TREE;
4634 }
4635}
4636
4637/* Table of supported standard (C23) attributes. */
4638const struct attribute_spec std_attribute_table[] =
4639{
4640 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4641 affects_type_identity, handler, exclude } */
4642 { .name: "_Noreturn", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4643 .handler: handle_std_noreturn_attribute, NULL },
4644 { .name: "deprecated", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4645 .handler: handle_deprecated_attribute, NULL },
4646 { .name: "fallthrough", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4647 .handler: handle_fallthrough_attribute, NULL },
4648 { .name: "maybe_unused", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4649 .handler: handle_unused_attribute, NULL },
4650 { .name: "nodiscard", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4651 .handler: handle_nodiscard_attribute, NULL },
4652 { .name: "noreturn", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
4653 .handler: handle_std_noreturn_attribute, NULL },
4654 { NULL, .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false, NULL, NULL }
4655};
4656
4657/* Create the predefined scalar types of C,
4658 and some nodes representing standard constants (0, 1, (void *) 0).
4659 Initialize the global scope.
4660 Make definitions for built-in primitive functions. */
4661
4662void
4663c_init_decl_processing (void)
4664{
4665 location_t save_loc = input_location;
4666
4667 /* Initialize reserved words for parser. */
4668 c_parse_init ();
4669
4670 register_scoped_attributes (std_attribute_table, NULL);
4671
4672 current_function_decl = NULL_TREE;
4673
4674 gcc_obstack_init (&parser_obstack);
4675
4676 /* Make the externals scope. */
4677 push_scope ();
4678 external_scope = current_scope;
4679
4680 /* Declarations from c_common_nodes_and_builtins must not be associated
4681 with this input file, lest we get differences between using and not
4682 using preprocessed headers. */
4683 input_location = BUILTINS_LOCATION;
4684
4685 c_common_nodes_and_builtins ();
4686
4687 /* In C, comparisons and TRUTH_* expressions have type int. */
4688 truthvalue_type_node = integer_type_node;
4689 truthvalue_true_node = integer_one_node;
4690 truthvalue_false_node = integer_zero_node;
4691
4692 /* Even in C99, which has a real boolean type. */
4693 pushdecl (x: build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4694 boolean_type_node));
4695
4696 /* C-specific nullptr initialization. */
4697 record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4698 /* The size and alignment of nullptr_t is the same as for a pointer to
4699 character type. */
4700 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4701
4702 input_location = save_loc;
4703
4704 make_fname_decl = c_make_fname_decl;
4705 start_fname_decls ();
4706}
4707
4708/* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4709 give the decl, NAME is the initialization string and TYPE_DEP
4710 indicates whether NAME depended on the type of the function. As we
4711 don't yet implement delayed emission of static data, we mark the
4712 decl as emitted so it is not placed in the output. Anything using
4713 it must therefore pull out the STRING_CST initializer directly.
4714 FIXME. */
4715
4716static tree
4717c_make_fname_decl (location_t loc, tree id, int type_dep)
4718{
4719 const char *name = fname_as_string (type_dep);
4720 tree decl, type, init;
4721 size_t length = strlen (s: name);
4722
4723 type = build_array_type (char_type_node,
4724 build_index_type (size_int (length)));
4725 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4726
4727 decl = build_decl (loc, VAR_DECL, id, type);
4728
4729 TREE_STATIC (decl) = 1;
4730 TREE_READONLY (decl) = 1;
4731 DECL_ARTIFICIAL (decl) = 1;
4732
4733 init = build_string (length + 1, name);
4734 free (CONST_CAST (char *, name));
4735 TREE_TYPE (init) = type;
4736 DECL_INITIAL (decl) = init;
4737
4738 TREE_USED (decl) = 1;
4739
4740 if (current_function_decl
4741 /* For invalid programs like this:
4742
4743 void foo()
4744 const char* p = __FUNCTION__;
4745
4746 the __FUNCTION__ is believed to appear in K&R style function
4747 parameter declarator. In that case we still don't have
4748 function_scope. */
4749 && current_function_scope)
4750 {
4751 DECL_CONTEXT (decl) = current_function_decl;
4752 bind (name: id, decl, scope: current_function_scope,
4753 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4754 }
4755
4756 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4757
4758 return decl;
4759}
4760
4761tree
4762c_builtin_function (tree decl)
4763{
4764 tree type = TREE_TYPE (decl);
4765 tree id = DECL_NAME (decl);
4766
4767 const char *name = IDENTIFIER_POINTER (id);
4768 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4769
4770 /* Should never be called on a symbol with a preexisting meaning. */
4771 gcc_assert (!I_SYMBOL_BINDING (id));
4772
4773 bind (name: id, decl, scope: external_scope, /*invisible=*/true, /*nested=*/false,
4774 UNKNOWN_LOCATION);
4775
4776 /* Builtins in the implementation namespace are made visible without
4777 needing to be explicitly declared. See push_file_scope. */
4778 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4779 {
4780 DECL_CHAIN (decl) = visible_builtins;
4781 visible_builtins = decl;
4782 }
4783
4784 return decl;
4785}
4786
4787tree
4788c_builtin_function_ext_scope (tree decl)
4789{
4790 tree type = TREE_TYPE (decl);
4791 tree id = DECL_NAME (decl);
4792
4793 const char *name = IDENTIFIER_POINTER (id);
4794 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4795
4796 if (external_scope)
4797 bind (name: id, decl, scope: external_scope, /*invisible=*/false, /*nested=*/false,
4798 UNKNOWN_LOCATION);
4799
4800 /* Builtins in the implementation namespace are made visible without
4801 needing to be explicitly declared. See push_file_scope. */
4802 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4803 {
4804 DECL_CHAIN (decl) = visible_builtins;
4805 visible_builtins = decl;
4806 }
4807
4808 return decl;
4809}
4810
4811/* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4812
4813tree
4814c_simulate_builtin_function_decl (tree decl)
4815{
4816 tree type = TREE_TYPE (decl);
4817 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4818 return pushdecl (x: decl);
4819}
4820
4821/* Warn about attributes in a context where they are unused
4822 (attribute-declarations, except for the "fallthrough" case, and
4823 attributes on statements). */
4824
4825void
4826c_warn_unused_attributes (tree attrs)
4827{
4828 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4829 if (get_attribute_namespace (t) == NULL_TREE)
4830 /* The specifications of standard attributes mean this is a
4831 constraint violation. */
4832 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4833 get_attribute_name (t));
4834 else if (!attribute_ignored_p (t))
4835 warning (OPT_Wattributes, "%qE attribute ignored",
4836 get_attribute_name (t));
4837}
4838
4839/* Warn for standard attributes being applied to a type that is not
4840 being defined, where that is a constraint violation, and return a
4841 list of attributes with them removed. */
4842
4843tree
4844c_warn_type_attributes (tree attrs)
4845{
4846 tree *attr_ptr = &attrs;
4847 while (*attr_ptr)
4848 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4849 {
4850 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4851 get_attribute_name (*attr_ptr));
4852 *attr_ptr = TREE_CHAIN (*attr_ptr);
4853 }
4854 else
4855 attr_ptr = &TREE_CHAIN (*attr_ptr);
4856 return attrs;
4857}
4858
4859/* Called when a declaration is seen that contains no names to declare.
4860 If its type is a reference to a structure, union or enum inherited
4861 from a containing scope, shadow that tag name for the current scope
4862 with a forward reference.
4863 If its type defines a new named structure or union
4864 or defines an enum, it is valid but we need not do anything here.
4865 Otherwise, it is an error. */
4866
4867void
4868shadow_tag (const struct c_declspecs *declspecs)
4869{
4870 shadow_tag_warned (declspecs, 0);
4871}
4872
4873/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4874 but no pedwarn. */
4875void
4876shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4877{
4878 bool found_tag = false;
4879
4880 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4881 {
4882 tree value = declspecs->type;
4883 enum tree_code code = TREE_CODE (value);
4884
4885 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4886 /* Used to test also that TYPE_SIZE (value) != 0.
4887 That caused warning for `struct foo;' at top level in the file. */
4888 {
4889 tree name = TYPE_NAME (value);
4890 tree t;
4891
4892 found_tag = true;
4893
4894 if (declspecs->restrict_p)
4895 {
4896 error ("invalid use of %<restrict%>");
4897 warned = 1;
4898 }
4899
4900 if (in_underspecified_init)
4901 {
4902 /* This can only occur with extensions such as statement
4903 expressions, but is still appropriate as an error to
4904 avoid types declared in such a context escaping to
4905 the type of an auto variable. */
4906 error ("%qT declared in underspecified object initializer",
4907 value);
4908 warned = 1;
4909 }
4910
4911 if (name == NULL_TREE)
4912 {
4913 if (warned != 1 && code != ENUMERAL_TYPE)
4914 /* Empty unnamed enum OK */
4915 {
4916 pedwarn (input_location, 0,
4917 "unnamed struct/union that defines no instances");
4918 warned = 1;
4919 }
4920 }
4921 else if (declspecs->typespec_kind != ctsk_tagdef
4922 && declspecs->typespec_kind != ctsk_tagfirstref
4923 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4924 && declspecs->storage_class != csc_none)
4925 {
4926 if (warned != 1)
4927 pedwarn (input_location, 0,
4928 "empty declaration with storage class specifier "
4929 "does not redeclare tag");
4930 warned = 1;
4931 pending_xref_error ();
4932 }
4933 else if (declspecs->typespec_kind != ctsk_tagdef
4934 && declspecs->typespec_kind != ctsk_tagfirstref
4935 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4936 && (declspecs->const_p
4937 || declspecs->volatile_p
4938 || declspecs->atomic_p
4939 || declspecs->restrict_p
4940 || declspecs->address_space))
4941 {
4942 if (warned != 1)
4943 pedwarn (input_location, 0,
4944 "empty declaration with type qualifier "
4945 "does not redeclare tag");
4946 warned = 1;
4947 pending_xref_error ();
4948 }
4949 else if (declspecs->typespec_kind != ctsk_tagdef
4950 && declspecs->typespec_kind != ctsk_tagfirstref
4951 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4952 && declspecs->alignas_p)
4953 {
4954 if (warned != 1)
4955 pedwarn (input_location, 0,
4956 "empty declaration with %<_Alignas%> "
4957 "does not redeclare tag");
4958 warned = 1;
4959 pending_xref_error ();
4960 }
4961 else if (declspecs->typespec_kind != ctsk_tagdef
4962 && declspecs->typespec_kind != ctsk_tagfirstref
4963 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4964 && code == ENUMERAL_TYPE
4965 && !declspecs->enum_type_specifier_ref_p)
4966 {
4967 bool warned_enum = false;
4968 if (warned != 1)
4969 warned_enum = pedwarn (input_location, OPT_Wpedantic,
4970 "empty declaration of %<enum%> type "
4971 "does not redeclare tag");
4972 if (warned_enum)
4973 warned = 1;
4974 pending_xref_error ();
4975 }
4976 else
4977 {
4978 pending_invalid_xref = NULL_TREE;
4979 t = lookup_tag (code, name, thislevel_only: true, NULL);
4980
4981 if (t == NULL_TREE)
4982 {
4983 t = make_node (code);
4984 pushtag (loc: input_location, name, type: t);
4985 }
4986 }
4987 }
4988 else
4989 {
4990 if (warned != 1 && !in_system_header_at (loc: input_location))
4991 {
4992 pedwarn (input_location, 0,
4993 "useless type name in empty declaration");
4994 warned = 1;
4995 }
4996 }
4997 }
4998 else if (warned != 1 && !in_system_header_at (loc: input_location)
4999 && declspecs->typedef_p)
5000 {
5001 pedwarn (input_location, 0, "useless type name in empty declaration");
5002 warned = 1;
5003 }
5004
5005 pending_invalid_xref = NULL_TREE;
5006
5007 if (declspecs->inline_p)
5008 {
5009 error ("%<inline%> in empty declaration");
5010 warned = 1;
5011 }
5012
5013 if (declspecs->noreturn_p)
5014 {
5015 error ("%<_Noreturn%> in empty declaration");
5016 warned = 1;
5017 }
5018
5019 if (declspecs->constexpr_p)
5020 {
5021 error ("%<constexpr%> in empty declaration");
5022 warned = 1;
5023 }
5024
5025 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5026 {
5027 error ("%<auto%> in file-scope empty declaration");
5028 warned = 1;
5029 }
5030
5031 if (current_scope == file_scope && declspecs->storage_class == csc_register)
5032 {
5033 error ("%<register%> in file-scope empty declaration");
5034 warned = 1;
5035 }
5036
5037 if (declspecs->enum_type_specifier_ref_p && !warned)
5038 {
5039 if (declspecs->storage_class != csc_none)
5040 {
5041 error ("storage class specifier in empty declaration with %<enum%> "
5042 "underlying type");
5043 warned = 1;
5044 }
5045 else if (declspecs->thread_p)
5046 {
5047 error ("%qs in empty declaration with %<enum%> underlying type",
5048 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5049 warned = 1;
5050 }
5051 else if (declspecs->const_p
5052 || declspecs->volatile_p
5053 || declspecs->atomic_p
5054 || declspecs->restrict_p
5055 || declspecs->address_space)
5056 {
5057 error ("type qualifier in empty declaration with %<enum%> "
5058 "underlying type");
5059 warned = 1;
5060 }
5061 else if (declspecs->alignas_p)
5062 {
5063 error ("%<alignas%> in empty declaration with %<enum%> "
5064 "underlying type");
5065 warned = 1;
5066 }
5067 }
5068
5069 if (!warned && !in_system_header_at (loc: input_location)
5070 && declspecs->storage_class != csc_none)
5071 {
5072 warning (0, "useless storage class specifier in empty declaration");
5073 warned = 2;
5074 }
5075
5076 if (!warned && !in_system_header_at (loc: input_location) && declspecs->thread_p)
5077 {
5078 warning (0, "useless %qs in empty declaration",
5079 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5080 warned = 2;
5081 }
5082
5083 if (!warned
5084 && !in_system_header_at (loc: input_location)
5085 && (declspecs->const_p
5086 || declspecs->volatile_p
5087 || declspecs->atomic_p
5088 || declspecs->restrict_p
5089 || declspecs->address_space))
5090 {
5091 warning (0, "useless type qualifier in empty declaration");
5092 warned = 2;
5093 }
5094
5095 if (!warned && !in_system_header_at (loc: input_location)
5096 && declspecs->alignas_p)
5097 {
5098 warning (0, "useless %<_Alignas%> in empty declaration");
5099 warned = 2;
5100 }
5101
5102 if (found_tag
5103 && warned == 2
5104 && (declspecs->typespec_kind == ctsk_tagref_attrs
5105 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5106 {
5107 /* Standard attributes after the "struct" or "union" keyword are
5108 only permitted when the contents of the type are defined, or
5109 in the form "struct-or-union attribute-specifier-sequence
5110 identifier;". If the ';' was not present, attributes were
5111 diagnosed in the parser. Here, ensure that any other useless
5112 elements of the declaration result in a pedwarn, not just a
5113 warning. Forward declarations of enum types are not part of
5114 standard C, but handle them the same. */
5115 pedwarn (input_location, 0,
5116 "invalid use of attributes in empty declaration");
5117 warned = 1;
5118 }
5119
5120 if (warned != 1)
5121 {
5122 if (declspecs->declspecs_seen_p
5123 && !declspecs->non_std_attrs_seen_p)
5124 /* An attribute declaration (but not a fallthrough attribute
5125 declaration, which was handled separately); warn if there
5126 are any attributes being ignored (but not if the attributes
5127 were empty). */
5128 c_warn_unused_attributes (attrs: declspecs->attrs);
5129 else if (!found_tag)
5130 pedwarn (input_location, 0, "empty declaration");
5131 }
5132}
5133
5134
5135/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5136 bits. SPECS represents declaration specifiers that the grammar
5137 only permits to contain type qualifiers and attributes. */
5138
5139int
5140quals_from_declspecs (const struct c_declspecs *specs)
5141{
5142 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5143 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5144 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5145 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5146 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5147 gcc_assert (!specs->type
5148 && !specs->decl_attr
5149 && specs->typespec_word == cts_none
5150 && specs->storage_class == csc_none
5151 && !specs->typedef_p
5152 && !specs->explicit_signed_p
5153 && !specs->deprecated_p
5154 && !specs->unavailable_p
5155 && !specs->long_p
5156 && !specs->long_long_p
5157 && !specs->short_p
5158 && !specs->signed_p
5159 && !specs->unsigned_p
5160 && !specs->complex_p
5161 && !specs->inline_p
5162 && !specs->noreturn_p
5163 && !specs->thread_p);
5164 return quals;
5165}
5166
5167/* Construct an array declarator. LOC is the location of the
5168 beginning of the array (usually the opening brace). EXPR is the
5169 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5170 inside the [] (to be applied to the pointer to which a parameter
5171 array is converted). STATIC_P is true if "static" is inside the
5172 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5173 VLA of unspecified length which is nevertheless a complete type,
5174 false otherwise. The field for the contained declarator is left to
5175 be filled in by set_array_declarator_inner. */
5176
5177struct c_declarator *
5178build_array_declarator (location_t loc,
5179 tree expr, struct c_declspecs *quals, bool static_p,
5180 bool vla_unspec_p)
5181{
5182 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5183 struct c_declarator);
5184 declarator->id_loc = loc;
5185 declarator->kind = cdk_array;
5186 declarator->declarator = 0;
5187 declarator->u.array.dimen = expr;
5188 if (quals)
5189 {
5190 declarator->u.array.attrs = quals->attrs;
5191 declarator->u.array.quals = quals_from_declspecs (specs: quals);
5192 }
5193 else
5194 {
5195 declarator->u.array.attrs = NULL_TREE;
5196 declarator->u.array.quals = 0;
5197 }
5198 declarator->u.array.static_p = static_p;
5199 declarator->u.array.vla_unspec_p = vla_unspec_p;
5200 if (static_p || quals != NULL)
5201 pedwarn_c90 (loc, opt: OPT_Wpedantic,
5202 "ISO C90 does not support %<static%> or type "
5203 "qualifiers in parameter array declarators");
5204 if (vla_unspec_p)
5205 pedwarn_c90 (loc, opt: OPT_Wpedantic,
5206 "ISO C90 does not support %<[*]%> array declarators");
5207 if (vla_unspec_p)
5208 {
5209 if (!current_scope->parm_flag)
5210 {
5211 /* C99 6.7.5.2p4 */
5212 error_at (loc, "%<[*]%> not allowed in other than "
5213 "function prototype scope");
5214 declarator->u.array.vla_unspec_p = false;
5215 return NULL;
5216 }
5217 current_scope->had_vla_unspec = true;
5218 }
5219 return declarator;
5220}
5221
5222/* Set the contained declarator of an array declarator. DECL is the
5223 declarator, as constructed by build_array_declarator; INNER is what
5224 appears on the left of the []. */
5225
5226struct c_declarator *
5227set_array_declarator_inner (struct c_declarator *decl,
5228 struct c_declarator *inner)
5229{
5230 decl->declarator = inner;
5231 return decl;
5232}
5233
5234/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5235static bool
5236flexible_array_member_type_p (const_tree type)
5237{
5238 if (TREE_CODE (type) == ARRAY_TYPE
5239 && TYPE_SIZE (type) == NULL_TREE
5240 && TYPE_DOMAIN (type) != NULL_TREE
5241 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5242 return true;
5243
5244 return false;
5245}
5246
5247/* Determine whether TYPE is a one-element array type "[1]". */
5248static bool
5249one_element_array_type_p (const_tree type)
5250{
5251 if (TREE_CODE (type) != ARRAY_TYPE)
5252 return false;
5253 return integer_zerop (array_type_nelts (type));
5254}
5255
5256/* Determine whether TYPE is a zero-length array type "[0]". */
5257static bool
5258zero_length_array_type_p (const_tree type)
5259{
5260 if (TREE_CODE (type) == ARRAY_TYPE)
5261 if (tree type_size = TYPE_SIZE_UNIT (type))
5262 if ((integer_zerop (type_size))
5263 && TYPE_DOMAIN (type) != NULL_TREE
5264 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5265 return true;
5266 return false;
5267}
5268
5269/* INIT is a constructor that forms DECL's initializer. If the final
5270 element initializes a flexible array field, add the size of that
5271 initializer to DECL's size. */
5272
5273static void
5274add_flexible_array_elts_to_size (tree decl, tree init)
5275{
5276 tree elt, type;
5277
5278 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5279 return;
5280
5281 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5282 type = TREE_TYPE (elt);
5283 if (flexible_array_member_type_p (type))
5284 {
5285 complete_array_type (&type, elt, false);
5286 DECL_SIZE (decl)
5287 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5288 DECL_SIZE_UNIT (decl)
5289 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5290 }
5291}
5292
5293/* Decode a "typename", such as "int **", returning a ..._TYPE node.
5294 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5295 before the type name, and set *EXPR_CONST_OPERANDS, if
5296 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5297 appear in a constant expression. */
5298
5299tree
5300groktypename (struct c_type_name *type_name, tree *expr,
5301 bool *expr_const_operands)
5302{
5303 tree type;
5304 tree attrs = type_name->specs->attrs;
5305
5306 type_name->specs->attrs = NULL_TREE;
5307
5308 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5309 false, NULL, &attrs, expr, expr_const_operands,
5310 DEPRECATED_NORMAL);
5311
5312 /* Apply attributes. */
5313 attrs = c_warn_type_attributes (attrs);
5314 decl_attributes (&type, attrs, 0);
5315
5316 return type;
5317}
5318
5319/* Looks up the most recent pushed declaration corresponding to DECL. */
5320
5321static tree
5322lookup_last_decl (tree decl)
5323{
5324 tree last_decl = lookup_name (DECL_NAME (decl));
5325 if (!last_decl)
5326 last_decl = lookup_name_in_scope (DECL_NAME (decl), scope: external_scope);
5327 return last_decl;
5328}
5329
5330/* Wrapper for decl_attributes that adds some implicit attributes
5331 to VAR_DECLs or FUNCTION_DECLs. */
5332
5333static tree
5334c_decl_attributes (tree *node, tree attributes, int flags)
5335{
5336 /* Add implicit "omp declare target" attribute if requested. */
5337 if (vec_safe_length (v: current_omp_declare_target_attribute)
5338 && ((VAR_P (*node) && is_global_var (t: *node))
5339 || TREE_CODE (*node) == FUNCTION_DECL))
5340 {
5341 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5342 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5343 NULL_TREE, attributes);
5344 else
5345 {
5346 attributes = tree_cons (get_identifier ("omp declare target"),
5347 NULL_TREE, attributes);
5348 attributes = tree_cons (get_identifier ("omp declare target block"),
5349 NULL_TREE, attributes);
5350 }
5351 if (TREE_CODE (*node) == FUNCTION_DECL)
5352 {
5353 int device_type
5354 = current_omp_declare_target_attribute->last ().device_type;
5355 device_type = MAX (device_type, 0);
5356 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5357 && !lookup_attribute (attr_name: "omp declare target host", list: attributes))
5358 attributes
5359 = tree_cons (get_identifier ("omp declare target host"),
5360 NULL_TREE, attributes);
5361 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5362 && !lookup_attribute (attr_name: "omp declare target nohost", list: attributes))
5363 attributes
5364 = tree_cons (get_identifier ("omp declare target nohost"),
5365 NULL_TREE, attributes);
5366
5367 int indirect
5368 = current_omp_declare_target_attribute->last ().indirect;
5369 if (indirect && !lookup_attribute (attr_name: "omp declare target indirect",
5370 list: attributes))
5371 attributes
5372 = tree_cons (get_identifier ("omp declare target indirect"),
5373 NULL_TREE, attributes);
5374 }
5375 }
5376
5377 if (flag_openmp || flag_openmp_simd)
5378 {
5379 bool diagnosed = false;
5380 for (tree *pa = &attributes; *pa; )
5381 {
5382 if (is_attribute_namespace_p (attr_ns: "omp", attr: *pa))
5383 {
5384 tree name = get_attribute_name (*pa);
5385 if (is_attribute_p (attr_name: "directive", ident: name)
5386 || is_attribute_p (attr_name: "sequence", ident: name)
5387 || is_attribute_p (attr_name: "decl", ident: name))
5388 {
5389 const char *p = NULL;
5390 if (TREE_VALUE (*pa) == NULL_TREE)
5391 p = IDENTIFIER_POINTER (name);
5392 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5393 {
5394 tree d = TREE_VALUE (a);
5395 gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5396 if (TREE_PUBLIC (d)
5397 && (VAR_P (*node)
5398 || TREE_CODE (*node) == FUNCTION_DECL)
5399 && c_maybe_parse_omp_decl (*node, d))
5400 continue;
5401 p = TREE_PUBLIC (d) ? "decl" : "directive";
5402 }
5403 if (p && !diagnosed)
5404 {
5405 error ("%<omp::%s%> not allowed to be specified in "
5406 "this context", p);
5407 diagnosed = true;
5408 }
5409 if (p)
5410 {
5411 *pa = TREE_CHAIN (*pa);
5412 continue;
5413 }
5414 }
5415 }
5416 pa = &TREE_CHAIN (*pa);
5417 }
5418 }
5419
5420 /* Look up the current declaration with all the attributes merged
5421 so far so that attributes on the current declaration that's
5422 about to be pushed that conflict with the former can be detected,
5423 diagnosed, and rejected as appropriate. */
5424 tree last_decl = lookup_last_decl (decl: *node);
5425 return decl_attributes (node, attributes, flags, last_decl);
5426}
5427
5428
5429/* Decode a declarator in an ordinary declaration or data definition.
5430 This is called as soon as the type information and variable name
5431 have been parsed, before parsing the initializer if any.
5432 Here we create the ..._DECL node, fill in its type,
5433 and (if DO_PUSH) put it on the list of decls for the current context.
5434 When nonnull, set *LASTLOC to the location of the prior declaration
5435 of the same entity if one exists.
5436 The ..._DECL node is returned as the value.
5437
5438 Exception: for arrays where the length is not specified,
5439 the type is left null, to be filled in by `finish_decl'.
5440
5441 Function definitions do not come here; they go to start_function
5442 instead. However, external and forward declarations of functions
5443 do go through here. Structure field declarations are done by
5444 grokfield and not through here. */
5445
5446tree
5447start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5448 bool initialized, tree attributes, bool do_push /* = true */,
5449 location_t *lastloc /* = NULL */)
5450{
5451 tree decl;
5452 tree old_decl;
5453 tree tem;
5454 tree expr = NULL_TREE;
5455 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5456
5457 /* An object declared as __attribute__((unavailable)) suppresses
5458 warnings and errors from __attribute__((deprecated/unavailable))
5459 components.
5460 An object declared as __attribute__((deprecated)) suppresses
5461 warnings of uses of other deprecated items. */
5462 if (lookup_attribute (attr_name: "unavailable", list: attributes))
5463 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5464 else if (lookup_attribute (attr_name: "deprecated", list: attributes))
5465 deprecated_state = DEPRECATED_SUPPRESS;
5466
5467 decl = grokdeclarator (declarator, declspecs,
5468 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5469 deprecated_state);
5470 if (!decl || decl == error_mark_node)
5471 return NULL_TREE;
5472
5473 old_decl = lookup_last_decl (decl);
5474
5475 if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5476 if (lastdecl != error_mark_node)
5477 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5478
5479 /* Make sure the size expression is evaluated at this point. */
5480 if (expr && !current_scope->parm_flag)
5481 add_stmt (fold_convert (void_type_node, expr));
5482
5483 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5484 && TREE_PUBLIC (decl))
5485 warning (OPT_Wmain, "%q+D is usually a function", decl);
5486
5487 if (warn_missing_variable_declarations && VAR_P (decl)
5488 && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5489 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5490 "no previous declaration for %qD", decl);
5491
5492 if (initialized)
5493 /* Is it valid for this decl to have an initializer at all?
5494 If not, set INITIALIZED to zero, which will indirectly
5495 tell 'finish_decl' to ignore the initializer once it is parsed. */
5496 switch (TREE_CODE (decl))
5497 {
5498 case TYPE_DECL:
5499 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5500 initialized = false;
5501 break;
5502
5503 case FUNCTION_DECL:
5504 error ("function %qD is initialized like a variable", decl);
5505 initialized = false;
5506 break;
5507
5508 case PARM_DECL:
5509 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5510 error ("parameter %qD is initialized", decl);
5511 initialized = false;
5512 break;
5513
5514 default:
5515 /* Don't allow initializations for incomplete types except for
5516 arrays which might be completed by the initialization. */
5517
5518 /* This can happen if the array size is an undefined macro.
5519 We already gave a warning, so we don't need another one. */
5520 if (TREE_TYPE (decl) == error_mark_node)
5521 initialized = false;
5522 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5523 {
5524 /* A complete type is ok if size is fixed. If the size is
5525 variable, an empty initializer is OK and nonempty
5526 initializers will be diagnosed in the parser. */
5527 }
5528 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5529 {
5530 error ("variable %qD has initializer but incomplete type", decl);
5531 initialized = false;
5532 }
5533 }
5534
5535 if (initialized)
5536 {
5537 if (current_scope == file_scope)
5538 TREE_STATIC (decl) = 1;
5539
5540 /* Tell 'pushdecl' this is an initialized decl
5541 even though we don't yet have the initializer expression.
5542 Also tell 'finish_decl' it may store the real initializer. */
5543 DECL_INITIAL (decl) = error_mark_node;
5544 }
5545
5546 /* If this is a function declaration, write a record describing it to the
5547 prototypes file (if requested). */
5548
5549 if (TREE_CODE (decl) == FUNCTION_DECL)
5550 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5551
5552 /* ANSI specifies that a tentative definition which is not merged with
5553 a non-tentative definition behaves exactly like a definition with an
5554 initializer equal to zero. (Section 3.7.2)
5555
5556 -fno-common gives strict ANSI behavior, though this tends to break
5557 a large body of code that grew up without this rule.
5558
5559 Thread-local variables are never common, since there's no entrenched
5560 body of code to break, and it allows more efficient variable references
5561 in the presence of dynamic linking. */
5562
5563 if (VAR_P (decl)
5564 && !initialized
5565 && TREE_PUBLIC (decl)
5566 && !DECL_THREAD_LOCAL_P (decl)
5567 && !flag_no_common)
5568 DECL_COMMON (decl) = 1;
5569
5570 /* Set attributes here so if duplicate decl, will have proper attributes. */
5571 c_decl_attributes (node: &decl, attributes, flags: 0);
5572
5573 /* Handle gnu_inline attribute. */
5574 if (declspecs->inline_p
5575 && !flag_gnu89_inline
5576 && TREE_CODE (decl) == FUNCTION_DECL
5577 && (lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (decl))
5578 || current_function_decl))
5579 {
5580 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5581 ;
5582 else if (declspecs->storage_class != csc_static)
5583 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5584 }
5585
5586 if (TREE_CODE (decl) == FUNCTION_DECL
5587 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5588 {
5589 struct c_declarator *ce = declarator;
5590
5591 if (ce->kind == cdk_pointer)
5592 ce = declarator->declarator;
5593 if (ce->kind == cdk_function)
5594 {
5595 tree args = ce->u.arg_info->parms;
5596 for (; args; args = DECL_CHAIN (args))
5597 {
5598 tree type = TREE_TYPE (args);
5599 if (type && INTEGRAL_TYPE_P (type)
5600 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5601 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5602 }
5603 }
5604 }
5605
5606 if (TREE_CODE (decl) == FUNCTION_DECL
5607 && DECL_DECLARED_INLINE_P (decl)
5608 && DECL_UNINLINABLE (decl)
5609 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl)))
5610 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5611 decl, "noinline");
5612
5613 /* C99 6.7.4p3: An inline definition of a function with external
5614 linkage shall not contain a definition of a modifiable object
5615 with static storage duration... */
5616 if (VAR_P (decl)
5617 && current_scope != file_scope
5618 && TREE_STATIC (decl)
5619 && !TREE_READONLY (decl)
5620 && DECL_DECLARED_INLINE_P (current_function_decl)
5621 && DECL_EXTERNAL (current_function_decl))
5622 record_inline_static (loc: input_location, func: current_function_decl,
5623 decl, type: csi_modifiable);
5624
5625 if (c_dialect_objc ()
5626 && VAR_OR_FUNCTION_DECL_P (decl))
5627 objc_check_global_decl (decl);
5628
5629 /* Add this decl to the current scope.
5630 TEM may equal DECL or it may be a previous decl of the same name. */
5631 if (do_push)
5632 {
5633 tem = pushdecl (x: decl);
5634
5635 if (initialized && DECL_EXTERNAL (tem))
5636 {
5637 DECL_EXTERNAL (tem) = 0;
5638 TREE_STATIC (tem) = 1;
5639 }
5640
5641 return tem;
5642 }
5643 else
5644 return decl;
5645}
5646
5647/* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5648 DECL or the non-array element type if DECL is an uninitialized array.
5649 If that type has a const member, diagnose this. */
5650
5651static void
5652diagnose_uninitialized_cst_member (tree decl, tree type)
5653{
5654 tree field;
5655 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5656 {
5657 tree field_type;
5658 if (TREE_CODE (field) != FIELD_DECL)
5659 continue;
5660 field_type = strip_array_types (TREE_TYPE (field));
5661
5662 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5663 {
5664 auto_diagnostic_group d;
5665 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5666 "uninitialized const member in %qT is invalid in C++",
5667 strip_array_types (TREE_TYPE (decl))))
5668 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5669 }
5670
5671 if (RECORD_OR_UNION_TYPE_P (field_type))
5672 diagnose_uninitialized_cst_member (decl, type: field_type);
5673 }
5674}
5675
5676/* Finish processing of a declaration;
5677 install its initial value.
5678 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5679 If the length of an array type is not known before,
5680 it must be determined now, from the initial value, or it is an error.
5681
5682 INIT_LOC is the location of the initial value. */
5683
5684void
5685finish_decl (tree decl, location_t init_loc, tree init,
5686 tree origtype, tree asmspec_tree)
5687{
5688 tree type;
5689 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5690 const char *asmspec = 0;
5691
5692 /* If a name was specified, get the string. */
5693 if (VAR_OR_FUNCTION_DECL_P (decl)
5694 && DECL_FILE_SCOPE_P (decl))
5695 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5696 if (asmspec_tree)
5697 asmspec = TREE_STRING_POINTER (asmspec_tree);
5698
5699 if (VAR_P (decl)
5700 && TREE_STATIC (decl)
5701 && global_bindings_p ())
5702 /* So decl is a global variable. Record the types it uses
5703 so that we can decide later to emit debug info for them. */
5704 record_types_used_by_current_var_decl (decl);
5705
5706 /* If `start_decl' didn't like having an initialization, ignore it now. */
5707 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5708 init = NULL_TREE;
5709
5710 /* Don't crash if parm is initialized. */
5711 if (TREE_CODE (decl) == PARM_DECL)
5712 init = NULL_TREE;
5713
5714 if (init)
5715 store_init_value (init_loc, decl, init, origtype);
5716
5717 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5718 || TREE_CODE (decl) == FIELD_DECL))
5719 objc_check_decl (decl);
5720
5721 type = TREE_TYPE (decl);
5722
5723 /* Deduce size of array from initialization, if not already known.
5724 This is only needed for an initialization in the current scope;
5725 it must not be done for a file-scope initialization of a
5726 declaration with external linkage, redeclared in an inner scope
5727 with the outer declaration shadowed in an intermediate scope. */
5728 if (TREE_CODE (type) == ARRAY_TYPE
5729 && TYPE_DOMAIN (type) == NULL_TREE
5730 && TREE_CODE (decl) != TYPE_DECL
5731 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5732 {
5733 bool do_default
5734 = (TREE_STATIC (decl)
5735 /* Even if pedantic, an external linkage array
5736 may have incomplete type at first. */
5737 ? pedantic && !TREE_PUBLIC (decl)
5738 : !DECL_EXTERNAL (decl));
5739 int failure
5740 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5741 do_default);
5742
5743 /* Get the completed type made by complete_array_type. */
5744 type = TREE_TYPE (decl);
5745
5746 switch (failure)
5747 {
5748 case 1:
5749 error ("initializer fails to determine size of %q+D", decl);
5750 break;
5751
5752 case 2:
5753 if (do_default)
5754 error ("array size missing in %q+D", decl);
5755 break;
5756
5757 case 3:
5758 error ("zero or negative size array %q+D", decl);
5759 break;
5760
5761 case 0:
5762 /* For global variables, update the copy of the type that
5763 exists in the binding. */
5764 if (TREE_PUBLIC (decl))
5765 {
5766 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5767 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5768 b_ext = b_ext->shadowed;
5769 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5770 {
5771 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5772 b_ext->u.type = composite_type (b_ext->u.type, type);
5773 else
5774 b_ext->u.type = type;
5775 }
5776 }
5777 break;
5778
5779 default:
5780 gcc_unreachable ();
5781 }
5782
5783 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5784 TREE_TYPE (DECL_INITIAL (decl)) = type;
5785
5786 relayout_decl (decl);
5787 }
5788
5789 /* Look for braced array initializers for character arrays and
5790 recursively convert them into STRING_CSTs. */
5791 if (tree init = DECL_INITIAL (decl))
5792 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5793
5794 if (VAR_P (decl))
5795 {
5796 if (init && TREE_CODE (init) == CONSTRUCTOR)
5797 add_flexible_array_elts_to_size (decl, init);
5798
5799 complete_flexible_array_elts (DECL_INITIAL (decl));
5800
5801 if (is_global_var (t: decl))
5802 {
5803 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5804 ? TCTX_THREAD_STORAGE
5805 : TCTX_STATIC_STORAGE);
5806 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5807 TREE_TYPE (decl) = error_mark_node;
5808 }
5809
5810 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5811 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5812 layout_decl (decl, 0);
5813
5814 if (DECL_SIZE (decl) == NULL_TREE
5815 /* Don't give an error if we already gave one earlier. */
5816 && TREE_TYPE (decl) != error_mark_node
5817 && (TREE_STATIC (decl)
5818 /* A static variable with an incomplete type
5819 is an error if it is initialized.
5820 Also if it is not file scope.
5821 Also if it is thread-local (in C23).
5822 Otherwise, let it through, but if it is not `extern'
5823 then it may cause an error message later. */
5824 ? (DECL_INITIAL (decl) != NULL_TREE
5825 || !DECL_FILE_SCOPE_P (decl)
5826 || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5827 /* An automatic variable with an incomplete type
5828 is an error. */
5829 : !DECL_EXTERNAL (decl)))
5830 {
5831 error ("storage size of %q+D isn%'t known", decl);
5832 TREE_TYPE (decl) = error_mark_node;
5833 }
5834
5835 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5836 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5837 && DECL_SIZE (decl) == NULL_TREE
5838 && TREE_STATIC (decl))
5839 incomplete_record_decls.safe_push (obj: decl);
5840
5841 if (is_global_var (t: decl)
5842 && DECL_SIZE (decl) != NULL_TREE
5843 && TREE_TYPE (decl) != error_mark_node)
5844 {
5845 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5846 constant_expression_warning (DECL_SIZE (decl));
5847 else
5848 {
5849 error ("storage size of %q+D isn%'t constant", decl);
5850 TREE_TYPE (decl) = error_mark_node;
5851 }
5852 }
5853
5854 if (TREE_USED (type))
5855 {
5856 TREE_USED (decl) = 1;
5857 DECL_READ_P (decl) = 1;
5858 }
5859 }
5860
5861 /* If this is a function and an assembler name is specified, reset DECL_RTL
5862 so we can give it its new name. Also, update builtin_decl if it
5863 was a normal built-in. */
5864 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5865 {
5866 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5867 set_builtin_user_assembler_name (decl, asmspec);
5868 set_user_assembler_name (decl, asmspec);
5869 }
5870
5871 /* If #pragma weak was used, mark the decl weak now. */
5872 maybe_apply_pragma_weak (decl);
5873
5874 /* Output the assembler code and/or RTL code for variables and functions,
5875 unless the type is an undefined structure or union.
5876 If not, it will get done when the type is completed. */
5877
5878 if (VAR_OR_FUNCTION_DECL_P (decl))
5879 {
5880 /* Determine the ELF visibility. */
5881 if (TREE_PUBLIC (decl))
5882 c_determine_visibility (decl);
5883
5884 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5885 if (c_dialect_objc ())
5886 objc_check_decl (decl);
5887
5888 if (asmspec)
5889 {
5890 /* If this is not a static variable, issue a warning.
5891 It doesn't make any sense to give an ASMSPEC for an
5892 ordinary, non-register local variable. Historically,
5893 GCC has accepted -- but ignored -- the ASMSPEC in
5894 this case. */
5895 if (!DECL_FILE_SCOPE_P (decl)
5896 && VAR_P (decl)
5897 && !C_DECL_REGISTER (decl)
5898 && !TREE_STATIC (decl))
5899 warning (0, "ignoring %<asm%> specifier for non-static local "
5900 "variable %q+D", decl);
5901 else
5902 set_user_assembler_name (decl, asmspec);
5903 }
5904
5905 if (DECL_FILE_SCOPE_P (decl))
5906 {
5907 if (DECL_INITIAL (decl) == NULL_TREE
5908 || DECL_INITIAL (decl) == error_mark_node)
5909 /* Don't output anything
5910 when a tentative file-scope definition is seen.
5911 But at end of compilation, do output code for them. */
5912 DECL_DEFER_OUTPUT (decl) = 1;
5913 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5914 DECL_HARD_REGISTER (decl) = 1;
5915 rest_of_decl_compilation (decl, true, 0);
5916
5917 if (TREE_CODE (decl) == FUNCTION_DECL)
5918 {
5919 tree parms = DECL_ARGUMENTS (decl);
5920 const bool builtin = fndecl_built_in_p (node: decl);
5921 if (tree access = build_attr_access_from_parms (parms, !builtin))
5922 decl_attributes (&decl, access, 0);
5923 }
5924 }
5925 else
5926 {
5927 /* In conjunction with an ASMSPEC, the `register'
5928 keyword indicates that we should place the variable
5929 in a particular register. */
5930 if (asmspec && C_DECL_REGISTER (decl))
5931 {
5932 DECL_HARD_REGISTER (decl) = 1;
5933 /* This cannot be done for a structure with volatile
5934 fields, on which DECL_REGISTER will have been
5935 reset. */
5936 if (!DECL_REGISTER (decl))
5937 error ("cannot put object with volatile field into register");
5938 }
5939
5940 if (TREE_CODE (decl) != FUNCTION_DECL)
5941 {
5942 /* If we're building a variable sized type, and we might be
5943 reachable other than via the top of the current binding
5944 level, then create a new BIND_EXPR so that we deallocate
5945 the object at the right time. */
5946 /* Note that DECL_SIZE can be null due to errors. */
5947 if (DECL_SIZE (decl)
5948 && !TREE_CONSTANT (DECL_SIZE (decl))
5949 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5950 {
5951 tree bind;
5952 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5953 TREE_SIDE_EFFECTS (bind) = 1;
5954 add_stmt (t: bind);
5955 BIND_EXPR_BODY (bind) = push_stmt_list ();
5956 }
5957 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl),
5958 DECL_EXPR, decl));
5959 }
5960 }
5961
5962
5963 if (!DECL_FILE_SCOPE_P (decl))
5964 {
5965 /* Recompute the RTL of a local array now
5966 if it used to be an incomplete type. */
5967 if (was_incomplete && !is_global_var (t: decl))
5968 {
5969 /* If we used it already as memory, it must stay in memory. */
5970 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5971 /* If it's still incomplete now, no init will save it. */
5972 if (DECL_SIZE (decl) == NULL_TREE)
5973 DECL_INITIAL (decl) = NULL_TREE;
5974 }
5975 }
5976 }
5977
5978 if (TREE_CODE (decl) == TYPE_DECL)
5979 {
5980 if (!DECL_FILE_SCOPE_P (decl)
5981 && c_type_variably_modified_p (TREE_TYPE (decl)))
5982 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5983
5984 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5985 }
5986
5987 /* Install a cleanup (aka destructor) if one was given. */
5988 if (VAR_P (decl) && !TREE_STATIC (decl))
5989 {
5990 tree attr = lookup_attribute (attr_name: "cleanup", DECL_ATTRIBUTES (decl));
5991 if (attr)
5992 {
5993 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5994 tree cleanup_decl = lookup_name (name: cleanup_id);
5995 tree cleanup;
5996 vec<tree, va_gc> *v;
5997
5998 /* Build "cleanup(&decl)" for the destructor. */
5999 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6000 vec_alloc (v, nelems: 1);
6001 v->quick_push (obj: cleanup);
6002 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6003 vNULL, cleanup_decl, v, NULL);
6004 vec_free (v);
6005
6006 /* Don't warn about decl unused; the cleanup uses it. */
6007 TREE_USED (decl) = 1;
6008 TREE_USED (cleanup_decl) = 1;
6009 DECL_READ_P (decl) = 1;
6010
6011 push_cleanup (decl, cleanup, false);
6012 }
6013 }
6014
6015 if (warn_cxx_compat
6016 && VAR_P (decl)
6017 && !DECL_EXTERNAL (decl)
6018 && DECL_INITIAL (decl) == NULL_TREE)
6019 {
6020 type = strip_array_types (type);
6021 if (TREE_READONLY (decl))
6022 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6023 "uninitialized %<const %D%> is invalid in C++", decl);
6024 else if (RECORD_OR_UNION_TYPE_P (type)
6025 && C_TYPE_FIELDS_READONLY (type))
6026 diagnose_uninitialized_cst_member (decl, type);
6027 }
6028
6029 if (flag_openmp
6030 && VAR_P (decl)
6031 && lookup_attribute (attr_name: "omp declare target implicit",
6032 DECL_ATTRIBUTES (decl)))
6033 {
6034 DECL_ATTRIBUTES (decl)
6035 = remove_attribute ("omp declare target implicit",
6036 DECL_ATTRIBUTES (decl));
6037 if (!omp_mappable_type (TREE_TYPE (decl)))
6038 error ("%q+D in declare target directive does not have mappable type",
6039 decl);
6040 else if (!lookup_attribute (attr_name: "omp declare target",
6041 DECL_ATTRIBUTES (decl))
6042 && !lookup_attribute (attr_name: "omp declare target link",
6043 DECL_ATTRIBUTES (decl)))
6044 {
6045 DECL_ATTRIBUTES (decl)
6046 = tree_cons (get_identifier ("omp declare target"),
6047 NULL_TREE, DECL_ATTRIBUTES (decl));
6048 symtab_node *node = symtab_node::get (decl);
6049 if (node != NULL)
6050 {
6051 node->offloadable = 1;
6052 if (ENABLE_OFFLOADING)
6053 {
6054 g->have_offload = true;
6055 if (is_a <varpool_node *> (p: node))
6056 vec_safe_push (v&: offload_vars, obj: decl);
6057 }
6058 }
6059 }
6060 }
6061
6062 /* This is the last point we can lower alignment so give the target the
6063 chance to do so. */
6064 if (VAR_P (decl)
6065 && !is_global_var (t: decl)
6066 && !DECL_HARD_REGISTER (decl))
6067 targetm.lower_local_decl_alignment (decl);
6068
6069 invoke_plugin_callbacks (event: PLUGIN_FINISH_DECL, gcc_data: decl);
6070}
6071
6072/* Given a parsed parameter declaration, decode it into a PARM_DECL.
6073 EXPR is NULL or a pointer to an expression that needs to be
6074 evaluated for the side effects of array size expressions in the
6075 parameters. */
6076
6077tree
6078grokparm (const struct c_parm *parm, tree *expr)
6079{
6080 tree attrs = parm->attrs;
6081 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6082 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6083
6084 decl_attributes (&decl, attrs, 0);
6085
6086 return decl;
6087}
6088
6089/* Return attribute "arg spec" corresponding to an array/VLA parameter
6090 described by PARM, concatenated onto attributes ATTRS.
6091 The spec consists of one dollar symbol for each specified variable
6092 bound, one asterisk for each unspecified variable bound, followed
6093 by at most one specification of the most significant bound of
6094 an ordinary array parameter. For ordinary arrays the specification
6095 is either the constant bound itself, or the space character for
6096 an array with an unspecified bound (the [] form). Finally, a chain
6097 of specified variable bounds is appended to the spec, starting with
6098 the most significant bound. For example, the PARM T a[2][m][3][n]
6099 will produce __attribute__((arg spec ("[$$2]", m, n)).
6100 For T a typedef for an array with variable bounds, the bounds are
6101 included in the specification in the expected order.
6102 No "arg spec" is created for parameters of pointer types, making
6103 a distinction between T(*)[N] (or, equivalently, T[][N]) and
6104 the T[M][N] form, all of which have the same type and are represented
6105 the same, but only the last of which gets an "arg spec" describing
6106 the most significant bound M. */
6107
6108static tree
6109get_parm_array_spec (const struct c_parm *parm, tree attrs)
6110{
6111 /* The attribute specification string, minor bound first. */
6112 std::string spec;
6113
6114 /* A list of VLA variable bounds, major first, or null if unspecified
6115 or not a VLA. */
6116 tree vbchain = NULL_TREE;
6117 /* True for a pointer parameter. */
6118 bool pointer = false;
6119 /* True for an ordinary array with an unpecified bound. */
6120 bool nobound = false;
6121
6122 /* Create a string representation for the bounds of the array/VLA. */
6123 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6124 {
6125 next = pd->declarator;
6126 while (next && next->kind == cdk_attrs)
6127 next = next->declarator;
6128
6129 /* Remember if a pointer has been seen to avoid storing the constant
6130 bound. */
6131 if (pd->kind == cdk_pointer)
6132 pointer = true;
6133
6134 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6135 && (!next || next->kind == cdk_id))
6136 {
6137 /* Do nothing for the common case of a pointer. The fact that
6138 the parameter is one can be deduced from the absence of
6139 an arg spec for it. */
6140 return attrs;
6141 }
6142
6143 if (pd->kind == cdk_id)
6144 {
6145 if (pointer
6146 || !parm->specs->type
6147 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6148 || !TYPE_DOMAIN (parm->specs->type)
6149 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6150 continue;
6151
6152 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6153 if (!vbchain
6154 && TREE_CODE (max) == INTEGER_CST)
6155 {
6156 /* Extract the upper bound from a parameter of an array type
6157 unless the parameter is an ordinary array of unspecified
6158 bound in which case a next iteration of the loop will
6159 exit. */
6160 if (spec.empty () || spec.end ()[-1] != ' ')
6161 {
6162 if (!tree_fits_shwi_p (max))
6163 continue;
6164
6165 /* The upper bound is the value of the largest valid
6166 index. */
6167 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6168 char buf[40];
6169 sprintf (s: buf, format: "%lu", (unsigned long)n);
6170 spec += buf;
6171 }
6172 continue;
6173 }
6174
6175 /* For a VLA typedef, create a list of its variable bounds and
6176 append it in the expected order to VBCHAIN. */
6177 tree tpbnds = NULL_TREE;
6178 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6179 type = TREE_TYPE (type))
6180 {
6181 tree nelts = array_type_nelts (type);
6182 if (error_operand_p (t: nelts))
6183 return attrs;
6184 if (TREE_CODE (nelts) != INTEGER_CST)
6185 {
6186 /* Each variable VLA bound is represented by the dollar
6187 sign. */
6188 spec += "$";
6189 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
6190 }
6191 }
6192 tpbnds = nreverse (tpbnds);
6193 vbchain = chainon (vbchain, tpbnds);
6194 continue;
6195 }
6196
6197 if (pd->kind != cdk_array)
6198 continue;
6199
6200 if (pd->u.array.vla_unspec_p)
6201 {
6202 /* Each unspecified bound is represented by a star. There
6203 can be any number of these in a declaration (but none in
6204 a definition). */
6205 spec += '*';
6206 continue;
6207 }
6208
6209 tree nelts = pd->u.array.dimen;
6210 if (!nelts)
6211 {
6212 /* Ordinary array of unspecified size. There can be at most
6213 one for the most significant bound. Exit on the next
6214 iteration which determines whether or not PARM is declared
6215 as a pointer or an array. */
6216 nobound = true;
6217 continue;
6218 }
6219
6220 if (pd->u.array.static_p)
6221 spec += 's';
6222
6223 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6224 /* Avoid invalid NELTS. */
6225 return attrs;
6226
6227 STRIP_NOPS (nelts);
6228 nelts = c_fully_fold (nelts, false, nullptr);
6229 if (TREE_CODE (nelts) == INTEGER_CST)
6230 {
6231 /* Skip all constant bounds except the most significant one.
6232 The interior ones are included in the array type. */
6233 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6234 continue;
6235
6236 if (!tree_fits_uhwi_p (nelts))
6237 /* Bail completely on invalid bounds. */
6238 return attrs;
6239
6240 char buf[40];
6241 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6242 sprintf (s: buf, format: "%llu", (unsigned long long)n);
6243 spec += buf;
6244 break;
6245 }
6246
6247 /* Each variable VLA bound is represented by a dollar sign. */
6248 spec += "$";
6249 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6250 }
6251
6252 if (spec.empty () && !nobound)
6253 return attrs;
6254
6255 spec.insert (pos: 0, s: "[");
6256 if (nobound)
6257 /* Ordinary array of unspecified bound is represented by a space.
6258 It must be last in the spec. */
6259 spec += ' ';
6260 spec += ']';
6261
6262 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6263 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6264 tree name = get_identifier ("arg spec");
6265 return tree_cons (name, args, attrs);
6266}
6267
6268/* Given a parsed parameter declaration, decode it into a PARM_DECL
6269 and push that on the current scope. EXPR is a pointer to an
6270 expression that needs to be evaluated for the side effects of array
6271 size expressions in the parameters. */
6272
6273void
6274push_parm_decl (const struct c_parm *parm, tree *expr)
6275{
6276 tree attrs = parm->attrs;
6277 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6278 &attrs, expr, NULL, DEPRECATED_NORMAL);
6279 if (decl && DECL_P (decl))
6280 DECL_SOURCE_LOCATION (decl) = parm->loc;
6281
6282 attrs = get_parm_array_spec (parm, attrs);
6283 decl_attributes (&decl, attrs, 0);
6284
6285 decl = pushdecl (x: decl);
6286
6287 finish_decl (decl, init_loc: input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6288}
6289
6290/* Mark all the parameter declarations to date as forward decls.
6291 Also diagnose use of this extension. */
6292
6293void
6294mark_forward_parm_decls (void)
6295{
6296 struct c_binding *b;
6297
6298 if (pedantic && !current_scope->warned_forward_parm_decls)
6299 {
6300 pedwarn (input_location, OPT_Wpedantic,
6301 "ISO C forbids forward parameter declarations");
6302 current_scope->warned_forward_parm_decls = true;
6303 }
6304
6305 for (b = current_scope->bindings; b; b = b->prev)
6306 if (TREE_CODE (b->decl) == PARM_DECL)
6307 TREE_ASM_WRITTEN (b->decl) = 1;
6308}
6309
6310/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6311 literal, which may be an incomplete array type completed by the
6312 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6313 literal. NON_CONST is true if the initializers contain something
6314 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6315 it is the (valid) alignment for this compound literal, as specified
6316 with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6317 compound literal. */
6318
6319tree
6320build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6321 unsigned int alignas_align,
6322 struct c_declspecs *scspecs)
6323{
6324 /* We do not use start_decl here because we have a type, not a declarator;
6325 and do not use finish_decl because the decl should be stored inside
6326 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6327 tree decl;
6328 tree complit;
6329 tree stmt;
6330 bool threadp = scspecs ? scspecs->thread_p : false;
6331 enum c_storage_class storage_class = (scspecs
6332 ? scspecs->storage_class
6333 : csc_none);
6334
6335 if (type == error_mark_node
6336 || init == error_mark_node)
6337 return error_mark_node;
6338
6339 if (current_scope == file_scope && storage_class == csc_register)
6340 {
6341 error_at (loc, "file-scope compound literal specifies %<register%>");
6342 storage_class = csc_none;
6343 }
6344
6345 if (current_scope != file_scope && threadp && storage_class == csc_none)
6346 {
6347 error_at (loc, "compound literal implicitly auto and declared %qs",
6348 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6349 threadp = false;
6350 }
6351
6352 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6353 DECL_EXTERNAL (decl) = 0;
6354 TREE_PUBLIC (decl) = 0;
6355 TREE_STATIC (decl) = (current_scope == file_scope
6356 || storage_class == csc_static);
6357 DECL_CONTEXT (decl) = current_function_decl;
6358 TREE_USED (decl) = 1;
6359 DECL_READ_P (decl) = 1;
6360 DECL_ARTIFICIAL (decl) = 1;
6361 DECL_IGNORED_P (decl) = 1;
6362 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6363 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6364 TREE_TYPE (decl) = type;
6365 if (threadp)
6366 set_decl_tls_model (decl, decl_default_tls_model (decl));
6367 if (storage_class == csc_register)
6368 {
6369 C_DECL_REGISTER (decl) = 1;
6370 DECL_REGISTER (decl) = 1;
6371 }
6372 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6373 if (alignas_align)
6374 {
6375 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6376 DECL_USER_ALIGN (decl) = 1;
6377 }
6378 store_init_value (loc, decl, init, NULL_TREE);
6379 if (current_scope != file_scope
6380 && TREE_STATIC (decl)
6381 && !TREE_READONLY (decl)
6382 && DECL_DECLARED_INLINE_P (current_function_decl)
6383 && DECL_EXTERNAL (current_function_decl))
6384 record_inline_static (loc: input_location, func: current_function_decl,
6385 decl, type: csi_modifiable);
6386
6387 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6388 {
6389 int failure = complete_array_type (&TREE_TYPE (decl),
6390 DECL_INITIAL (decl), true);
6391 /* If complete_array_type returns 3, it means that the
6392 initial value of the compound literal is empty. Allow it. */
6393 gcc_assert (failure == 0 || failure == 3);
6394
6395 type = TREE_TYPE (decl);
6396 TREE_TYPE (DECL_INITIAL (decl)) = type;
6397 }
6398
6399 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6400 {
6401 c_incomplete_type_error (loc, NULL_TREE, type);
6402 return error_mark_node;
6403 }
6404
6405 if (TREE_STATIC (decl)
6406 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6407 return error_mark_node;
6408
6409 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6410 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6411 TREE_SIDE_EFFECTS (complit) = 1;
6412
6413 layout_decl (decl, 0);
6414
6415 if (TREE_STATIC (decl))
6416 {
6417 /* This decl needs a name for the assembler output. */
6418 set_compound_literal_name (decl);
6419 DECL_DEFER_OUTPUT (decl) = 1;
6420 DECL_COMDAT (decl) = 1;
6421 pushdecl (x: decl);
6422 rest_of_decl_compilation (decl, 1, 0);
6423 }
6424 else if (current_function_decl && !current_scope->parm_flag)
6425 pushdecl (x: decl);
6426
6427 if (non_const)
6428 {
6429 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6430 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6431 }
6432
6433 return complit;
6434}
6435
6436/* Check the type of a compound literal. Here we just check that it
6437 is valid for C++. */
6438
6439void
6440check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6441{
6442 if (warn_cxx_compat
6443 && (type_name->specs->typespec_kind == ctsk_tagdef
6444 || type_name->specs->typespec_kind == ctsk_tagfirstref
6445 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6446 warning_at (loc, OPT_Wc___compat,
6447 "defining a type in a compound literal is invalid in C++");
6448}
6449
6450/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6451 replacing with appropriate values if they are invalid. */
6452
6453static void
6454check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6455 tree orig_name)
6456{
6457 tree type_mv;
6458 unsigned int max_width;
6459 unsigned HOST_WIDE_INT w;
6460 const char *name = (orig_name
6461 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6462 : _("<anonymous>"));
6463
6464 /* Detect and ignore out of range field width and process valid
6465 field widths. */
6466 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6467 {
6468 error_at (loc, "bit-field %qs width not an integer constant", name);
6469 *width = integer_one_node;
6470 }
6471 else
6472 {
6473 if (TREE_CODE (*width) != INTEGER_CST)
6474 {
6475 *width = c_fully_fold (*width, false, NULL);
6476 if (TREE_CODE (*width) == INTEGER_CST)
6477 pedwarn (loc, OPT_Wpedantic,
6478 "bit-field %qs width not an integer constant expression",
6479 name);
6480 }
6481 if (TREE_CODE (*width) != INTEGER_CST)
6482 {
6483 error_at (loc, "bit-field %qs width not an integer constant", name);
6484 *width = integer_one_node;
6485 }
6486 constant_expression_warning (*width);
6487 if (tree_int_cst_sgn (*width) < 0)
6488 {
6489 error_at (loc, "negative width in bit-field %qs", name);
6490 *width = integer_one_node;
6491 }
6492 else if (integer_zerop (*width) && orig_name)
6493 {
6494 error_at (loc, "zero width for bit-field %qs", name);
6495 *width = integer_one_node;
6496 }
6497 }
6498
6499 /* Detect invalid bit-field type. */
6500 if (TREE_CODE (*type) != INTEGER_TYPE
6501 && TREE_CODE (*type) != BOOLEAN_TYPE
6502 && TREE_CODE (*type) != ENUMERAL_TYPE
6503 && TREE_CODE (*type) != BITINT_TYPE)
6504 {
6505 error_at (loc, "bit-field %qs has invalid type", name);
6506 *type = unsigned_type_node;
6507 }
6508
6509 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6510 {
6511 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6512 name);
6513 *type = unsigned_type_node;
6514 }
6515
6516 type_mv = TYPE_MAIN_VARIANT (*type);
6517 if (!in_system_header_at (loc: input_location)
6518 && type_mv != integer_type_node
6519 && type_mv != unsigned_type_node
6520 && type_mv != boolean_type_node)
6521 pedwarn_c90 (loc, opt: OPT_Wpedantic,
6522 "type of bit-field %qs is a GCC extension", name);
6523
6524 max_width = TYPE_PRECISION (*type);
6525
6526 if (compare_tree_int (*width, max_width) > 0)
6527 {
6528 error_at (loc, "width of %qs exceeds its type", name);
6529 w = max_width;
6530 *width = build_int_cst (integer_type_node, w);
6531 }
6532 else
6533 w = tree_to_uhwi (*width);
6534
6535 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6536 {
6537 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6538 if (!lt
6539 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6540 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6541 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6542 }
6543}
6544
6545
6546
6547/* Print warning about variable length array if necessary. */
6548
6549static void
6550warn_variable_length_array (tree name, tree size)
6551{
6552 if (TREE_CONSTANT (size))
6553 {
6554 if (name)
6555 pedwarn_c90 (input_location, opt: OPT_Wvla,
6556 "ISO C90 forbids array %qE whose size "
6557 "cannot be evaluated", name);
6558 else
6559 pedwarn_c90 (input_location, opt: OPT_Wvla, "ISO C90 forbids array "
6560 "whose size cannot be evaluated");
6561 }
6562 else
6563 {
6564 if (name)
6565 pedwarn_c90 (input_location, opt: OPT_Wvla,
6566 "ISO C90 forbids variable length array %qE", name);
6567 else
6568 pedwarn_c90 (input_location, opt: OPT_Wvla, "ISO C90 forbids variable "
6569 "length array");
6570 }
6571}
6572
6573/* Print warning about defaulting to int if necessary. */
6574
6575static void
6576warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6577{
6578 diagnostic_info diagnostic;
6579 va_list ap;
6580 rich_location richloc (line_table, location);
6581
6582 va_start (ap, gmsgid);
6583 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6584 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6585 diagnostic.option_index = opt;
6586 diagnostic_report_diagnostic (context: global_dc, diagnostic: &diagnostic);
6587 va_end (ap);
6588}
6589
6590/* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6591 considering only those c_declspec_words found in LIST, which
6592 must be terminated by cdw_number_of_elements. */
6593
6594static location_t
6595smallest_type_quals_location (const location_t *locations,
6596 const c_declspec_word *list)
6597{
6598 location_t loc = UNKNOWN_LOCATION;
6599 while (*list != cdw_number_of_elements)
6600 {
6601 location_t newloc = locations[*list];
6602 if (loc == UNKNOWN_LOCATION
6603 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6604 loc = newloc;
6605 list++;
6606 }
6607
6608 return loc;
6609}
6610
6611
6612/* We attach an artificial TYPE_DECL to pointed-to type
6613 and arrange for it to be included in a DECL_EXPR. This
6614 forces the sizes evaluation at a safe point and ensures it
6615 is not deferred until e.g. within a deeper conditional context.
6616
6617 PARM contexts have no enclosing statement list that
6618 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6619 instead, and add it to the list of expressions that
6620 need to be evaluated.
6621
6622 TYPENAME contexts do have an enclosing statement list,
6623 but it would be incorrect to use it, as the size should
6624 only be evaluated if the containing expression is
6625 evaluated. We might also be in the middle of an
6626 expression with side effects on the pointed-to type size
6627 "arguments" prior to the pointer declaration point and
6628 the fake TYPE_DECL in the enclosing context would force
6629 the size evaluation prior to the side effects. We therefore
6630 use BIND_EXPRs in TYPENAME contexts too. */
6631static void
6632add_decl_expr (location_t loc, enum decl_context decl_context, tree type,
6633 tree *expr)
6634{
6635 tree bind = NULL_TREE;
6636 if (decl_context == TYPENAME || decl_context == PARM
6637 || decl_context == FIELD)
6638 {
6639 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6640 NULL_TREE);
6641 TREE_SIDE_EFFECTS (bind) = 1;
6642 BIND_EXPR_BODY (bind) = push_stmt_list ();
6643 push_scope ();
6644 }
6645
6646 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6647 pushdecl (x: decl);
6648 DECL_ARTIFICIAL (decl) = 1;
6649 add_stmt (t: build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6650 TYPE_NAME (type) = decl;
6651
6652 if (bind)
6653 {
6654 pop_scope ();
6655 BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6656 if (*expr)
6657 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6658 else
6659 *expr = bind;
6660 }
6661}
6662
6663/* Given declspecs and a declarator,
6664 determine the name and type of the object declared
6665 and construct a ..._DECL node for it.
6666 (In one case we can return a ..._TYPE node instead.
6667 For invalid input we sometimes return NULL_TREE.)
6668
6669 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6670
6671 DECL_CONTEXT says which syntactic context this declaration is in:
6672 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6673 FUNCDEF for a function definition. Like NORMAL but a few different
6674 error messages in each case. Return value may be zero meaning
6675 this definition is too screwy to try to parse.
6676 PARM for a parameter declaration (either within a function prototype
6677 or before a function body). Make a PARM_DECL, or return void_type_node.
6678 TYPENAME if for a typename (in a cast or sizeof).
6679 Don't make a DECL node; just return the ..._TYPE node.
6680 FIELD for a struct or union field; make a FIELD_DECL.
6681 INITIALIZED is true if the decl has an initializer.
6682 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6683 representing the width of the bit-field.
6684 DECL_ATTRS points to the list of attributes that should be added to this
6685 decl. Any nested attributes that belong on the decl itself will be
6686 added to this list.
6687 If EXPR is not NULL, any expressions that need to be evaluated as
6688 part of evaluating variably modified types will be stored in *EXPR.
6689 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6690 set to indicate whether operands in *EXPR can be used in constant
6691 expressions.
6692 DEPRECATED_STATE is a deprecated_states value indicating whether
6693 deprecation/unavailability warnings should be suppressed.
6694
6695 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6696 It may also be so in the PARM case, for a prototype where the
6697 argument type is specified but not the name.
6698
6699 This function is where the complicated C meanings of `static'
6700 and `extern' are interpreted. */
6701
6702static tree
6703grokdeclarator (const struct c_declarator *declarator,
6704 struct c_declspecs *declspecs,
6705 enum decl_context decl_context, bool initialized, tree *width,
6706 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6707 enum deprecated_states deprecated_state)
6708{
6709 tree type = declspecs->type;
6710 bool threadp = declspecs->thread_p;
6711 bool constexprp = declspecs->constexpr_p;
6712 enum c_storage_class storage_class = declspecs->storage_class;
6713 int constp;
6714 int restrictp;
6715 int volatilep;
6716 int atomicp;
6717 int type_quals = TYPE_UNQUALIFIED;
6718 tree name = NULL_TREE;
6719 bool funcdef_flag = false;
6720 bool funcdef_syntax = false;
6721 bool size_varies = false;
6722 tree decl_attr = declspecs->decl_attr;
6723 int array_ptr_quals = TYPE_UNQUALIFIED;
6724 tree array_ptr_attrs = NULL_TREE;
6725 bool array_parm_static = false;
6726 bool array_parm_vla_unspec_p = false;
6727 tree returned_attrs = NULL_TREE;
6728 tree decl_id_attrs = NULL_TREE;
6729 bool bitfield = width != NULL;
6730 tree element_type;
6731 tree orig_qual_type = NULL;
6732 size_t orig_qual_indirect = 0;
6733 struct c_arg_info *arg_info = 0;
6734 addr_space_t as1, as2, address_space;
6735 location_t loc = UNKNOWN_LOCATION;
6736 tree expr_dummy;
6737 bool expr_const_operands_dummy;
6738 enum c_declarator_kind first_non_attr_kind;
6739 unsigned int alignas_align = 0;
6740
6741 if (type == NULL_TREE)
6742 {
6743 /* This can occur for auto on a parameter in C23 mode. Set a
6744 dummy type here so subsequent code can give diagnostics for
6745 this case. */
6746 gcc_assert (declspecs->c23_auto_p);
6747 gcc_assert (decl_context == PARM);
6748 type = declspecs->type = integer_type_node;
6749 }
6750 if (TREE_CODE (type) == ERROR_MARK)
6751 return error_mark_node;
6752 if (expr == NULL)
6753 {
6754 expr = &expr_dummy;
6755 expr_dummy = NULL_TREE;
6756 }
6757 if (expr_const_operands == NULL)
6758 expr_const_operands = &expr_const_operands_dummy;
6759
6760 if (declspecs->expr)
6761 {
6762 if (*expr)
6763 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6764 declspecs->expr);
6765 else
6766 *expr = declspecs->expr;
6767 }
6768 *expr_const_operands = declspecs->expr_const_operands;
6769
6770 if (decl_context == FUNCDEF)
6771 funcdef_flag = true, decl_context = NORMAL;
6772
6773 /* Look inside a declarator for the name being declared
6774 and get it as an IDENTIFIER_NODE, for an error message. */
6775 {
6776 const struct c_declarator *decl = declarator;
6777
6778 first_non_attr_kind = cdk_attrs;
6779 while (decl)
6780 switch (decl->kind)
6781 {
6782 case cdk_array:
6783 loc = decl->id_loc;
6784 /* FALL THRU. */
6785
6786 case cdk_function:
6787 case cdk_pointer:
6788 funcdef_syntax = (decl->kind == cdk_function);
6789 if (first_non_attr_kind == cdk_attrs)
6790 first_non_attr_kind = decl->kind;
6791 decl = decl->declarator;
6792 break;
6793
6794 case cdk_attrs:
6795 decl = decl->declarator;
6796 break;
6797
6798 case cdk_id:
6799 loc = decl->id_loc;
6800 if (decl->u.id.id)
6801 name = decl->u.id.id;
6802 decl_id_attrs = decl->u.id.attrs;
6803 if (first_non_attr_kind == cdk_attrs)
6804 first_non_attr_kind = decl->kind;
6805 decl = 0;
6806 break;
6807
6808 default:
6809 gcc_unreachable ();
6810 }
6811 if (name == NULL_TREE)
6812 {
6813 gcc_assert (decl_context == PARM
6814 || decl_context == TYPENAME
6815 || (decl_context == FIELD
6816 && declarator->kind == cdk_id));
6817 gcc_assert (!initialized);
6818 }
6819 }
6820
6821 /* An enum type specifier (": specifier-qualifier-list") may only be
6822 specified when the enum is being defined or in an empty
6823 declaration of the form "enum identifier enum-type-specifier;".
6824 Except for the case of an empty declaration that has additional
6825 declaration specifiers, all invalid contexts (declarations that
6826 aren't empty, type names, parameter declarations, member
6827 declarations) pass through grokdeclarator. */
6828 if (declspecs->enum_type_specifier_ref_p)
6829 error_at (loc, "%<enum%> underlying type may not be specified here");
6830
6831 /* A function definition's declarator must have the form of
6832 a function declarator. */
6833
6834 if (funcdef_flag && !funcdef_syntax)
6835 return NULL_TREE;
6836
6837 /* If this looks like a function definition, make it one,
6838 even if it occurs where parms are expected.
6839 Then store_parm_decls will reject it and not use it as a parm. */
6840 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6841 decl_context = PARM;
6842
6843 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6844 {
6845 if (declspecs->unavailable_p)
6846 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6847 else if (declspecs->deprecated_p
6848 && deprecated_state != DEPRECATED_SUPPRESS)
6849 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6850 }
6851
6852 if ((decl_context == NORMAL || decl_context == FIELD)
6853 && current_scope == file_scope
6854 && c_type_variably_modified_p (t: type))
6855 {
6856 if (name)
6857 error_at (loc, "variably modified %qE at file scope", name);
6858 else
6859 error_at (loc, "variably modified field at file scope");
6860 type = integer_type_node;
6861 }
6862
6863 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6864
6865 /* Diagnose defaulting to "int". */
6866
6867 if (declspecs->default_int_p && !in_system_header_at (loc: input_location))
6868 {
6869 /* Issue a warning if this is an ISO C 99 program or if
6870 -Wreturn-type and this is a function, or if -Wimplicit;
6871 prefer the former warning since it is more explicit. */
6872 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6873 && funcdef_flag)
6874 warn_about_return_type = 1;
6875 else
6876 {
6877 if (name)
6878 warn_defaults_to (location: loc, opt: OPT_Wimplicit_int,
6879 gmsgid: "type defaults to %<int%> in declaration "
6880 "of %qE", name);
6881 else
6882 warn_defaults_to (location: loc, opt: OPT_Wimplicit_int,
6883 gmsgid: "type defaults to %<int%> in type name");
6884 }
6885 }
6886
6887 /* Adjust the type if a bit-field is being declared,
6888 -funsigned-bitfields applied and the type is not explicitly
6889 "signed". */
6890 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6891 && TREE_CODE (type) == INTEGER_TYPE)
6892 type = unsigned_type_for (type);
6893
6894 /* Figure out the type qualifiers for the declaration. There are
6895 two ways a declaration can become qualified. One is something
6896 like `const int i' where the `const' is explicit. Another is
6897 something like `typedef const int CI; CI i' where the type of the
6898 declaration contains the `const'. A third possibility is that
6899 there is a type qualifier on the element type of a typedefed
6900 array type, in which case we should extract that qualifier so
6901 that c_apply_type_quals_to_decl receives the full list of
6902 qualifiers to work with (C90 is not entirely clear about whether
6903 duplicate qualifiers should be diagnosed in this case, but it
6904 seems most appropriate to do so). */
6905 element_type = strip_array_types (type);
6906 constp = declspecs->const_p + TYPE_READONLY (element_type);
6907 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6908 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6909 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6910 as1 = declspecs->address_space;
6911 as2 = TYPE_ADDR_SPACE (element_type);
6912 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6913
6914 if (constp > 1)
6915 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<const%>");
6916 if (restrictp > 1)
6917 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<restrict%>");
6918 if (volatilep > 1)
6919 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<volatile%>");
6920 if (atomicp > 1)
6921 pedwarn_c90 (loc, opt: OPT_Wpedantic, "duplicate %<_Atomic%>");
6922
6923 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6924 error_at (loc, "conflicting named address spaces (%s vs %s)",
6925 c_addr_space_name (as: as1), c_addr_space_name (as: as2));
6926
6927 if ((TREE_CODE (type) == ARRAY_TYPE
6928 || first_non_attr_kind == cdk_array)
6929 && TYPE_QUALS (element_type))
6930 {
6931 orig_qual_type = type;
6932 type = TYPE_MAIN_VARIANT (type);
6933 }
6934 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6935 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6936 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6937 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6938 | ENCODE_QUAL_ADDR_SPACE (address_space));
6939 if (type_quals != TYPE_QUALS (element_type))
6940 orig_qual_type = NULL_TREE;
6941
6942 /* Applying the _Atomic qualifier to an array type (through the use
6943 of typedefs or typeof) must be detected here. If the qualifier
6944 is introduced later, any appearance of applying it to an array is
6945 actually applying it to an element of that array. */
6946 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6947 error_at (loc, "%<_Atomic%>-qualified array type");
6948
6949 /* Warn about storage classes that are invalid for certain
6950 kinds of declarations (parameters, typenames, etc.). */
6951
6952 if (funcdef_flag
6953 && (threadp
6954 || constexprp
6955 || storage_class == csc_auto
6956 || storage_class == csc_register
6957 || storage_class == csc_typedef))
6958 {
6959 if (storage_class == csc_auto)
6960 pedwarn (loc,
6961 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6962 "function definition declared %<auto%>");
6963 if (storage_class == csc_register)
6964 error_at (loc, "function definition declared %<register%>");
6965 if (storage_class == csc_typedef)
6966 error_at (loc, "function definition declared %<typedef%>");
6967 if (threadp)
6968 error_at (loc, "function definition declared %qs",
6969 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6970 threadp = false;
6971 /* The parser ensures a constexpr function definition never
6972 reaches here. */
6973 gcc_assert (!constexprp);
6974 if (storage_class == csc_auto
6975 || storage_class == csc_register
6976 || storage_class == csc_typedef)
6977 storage_class = csc_none;
6978 }
6979 else if (decl_context != NORMAL && (storage_class != csc_none
6980 || threadp
6981 || constexprp
6982 || declspecs->c23_auto_p))
6983 {
6984 if (decl_context == PARM
6985 && storage_class == csc_register
6986 && !constexprp
6987 && !declspecs->c23_auto_p)
6988 ;
6989 else
6990 {
6991 switch (decl_context)
6992 {
6993 case FIELD:
6994 if (name)
6995 error_at (loc, "storage class specified for structure "
6996 "field %qE", name);
6997 else
6998 error_at (loc, "storage class specified for structure field");
6999 break;
7000 case PARM:
7001 if (name)
7002 error_at (loc, "storage class specified for parameter %qE",
7003 name);
7004 else
7005 error_at (loc, "storage class specified for unnamed parameter");
7006 break;
7007 default:
7008 error_at (loc, "storage class specified for typename");
7009 break;
7010 }
7011 storage_class = csc_none;
7012 threadp = false;
7013 constexprp = false;
7014 }
7015 }
7016 else if (storage_class == csc_extern
7017 && initialized
7018 && !funcdef_flag)
7019 {
7020 /* 'extern' with initialization is invalid if not at file scope. */
7021 if (current_scope == file_scope)
7022 {
7023 /* It is fine to have 'extern const' when compiling at C
7024 and C++ intersection. */
7025 if (!(warn_cxx_compat && constp))
7026 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7027 name);
7028 }
7029 else
7030 error_at (loc, "%qE has both %<extern%> and initializer", name);
7031 }
7032 else if (current_scope == file_scope)
7033 {
7034 if (storage_class == csc_auto)
7035 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7036 name);
7037 if (pedantic && storage_class == csc_register)
7038 pedwarn (input_location, OPT_Wpedantic,
7039 "file-scope declaration of %qE specifies %<register%>", name);
7040 }
7041 else
7042 {
7043 if (storage_class == csc_extern && funcdef_flag)
7044 error_at (loc, "nested function %qE declared %<extern%>", name);
7045 else if (threadp && storage_class == csc_none)
7046 {
7047 error_at (loc, "function-scope %qE implicitly auto and declared "
7048 "%qs", name,
7049 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7050 threadp = false;
7051 }
7052 }
7053
7054 /* Now figure out the structure of the declarator proper.
7055 Descend through it, creating more complex types, until we reach
7056 the declared identifier (or NULL_TREE, in an absolute declarator).
7057 At each stage we maintain an unqualified version of the type
7058 together with any qualifiers that should be applied to it with
7059 c_build_qualified_type; this way, array types including
7060 multidimensional array types are first built up in unqualified
7061 form and then the qualified form is created with
7062 TYPE_MAIN_VARIANT pointing to the unqualified form. */
7063
7064 while (declarator && declarator->kind != cdk_id)
7065 {
7066 if (type == error_mark_node)
7067 {
7068 declarator = declarator->declarator;
7069 continue;
7070 }
7071
7072 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7073 a cdk_pointer (for *...),
7074 a cdk_function (for ...(...)),
7075 a cdk_attrs (for nested attributes),
7076 or a cdk_id (for the name being declared
7077 or the place in an absolute declarator
7078 where the name was omitted).
7079 For the last case, we have just exited the loop.
7080
7081 At this point, TYPE is the type of elements of an array,
7082 or for a function to return, or for a pointer to point to.
7083 After this sequence of ifs, TYPE is the type of the
7084 array or function or pointer, and DECLARATOR has had its
7085 outermost layer removed. */
7086
7087 if (array_ptr_quals != TYPE_UNQUALIFIED
7088 || array_ptr_attrs != NULL_TREE
7089 || array_parm_static)
7090 {
7091 /* Only the innermost declarator (making a parameter be of
7092 array type which is converted to pointer type)
7093 may have static or type qualifiers. */
7094 error_at (loc, "static or type qualifiers in non-parameter array declarator");
7095 array_ptr_quals = TYPE_UNQUALIFIED;
7096 array_ptr_attrs = NULL_TREE;
7097 array_parm_static = false;
7098 }
7099
7100 bool varmod = C_TYPE_VARIABLY_MODIFIED (type);
7101
7102 switch (declarator->kind)
7103 {
7104 case cdk_attrs:
7105 {
7106 /* A declarator with embedded attributes. */
7107 tree attrs = declarator->u.attrs;
7108 const struct c_declarator *inner_decl;
7109 int attr_flags = 0;
7110 declarator = declarator->declarator;
7111 /* Standard attribute syntax precisely defines what entity
7112 an attribute in each position appertains to, so only
7113 apply laxity about positioning to GNU attribute syntax.
7114 Standard attributes applied to a function or array
7115 declarator apply exactly to that type; standard
7116 attributes applied to the identifier apply to the
7117 declaration rather than to the type, and are specified
7118 using a cdk_id declarator rather than using
7119 cdk_attrs. */
7120 inner_decl = declarator;
7121 while (inner_decl->kind == cdk_attrs)
7122 inner_decl = inner_decl->declarator;
7123 if (!cxx11_attribute_p (attrs))
7124 {
7125 if (inner_decl->kind == cdk_id)
7126 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7127 else if (inner_decl->kind == cdk_function)
7128 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7129 else if (inner_decl->kind == cdk_array)
7130 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7131 }
7132 attrs = c_warn_type_attributes (attrs);
7133 returned_attrs = decl_attributes (&type,
7134 chainon (returned_attrs, attrs),
7135 attr_flags);
7136 break;
7137 }
7138 case cdk_array:
7139 {
7140 tree itype = NULL_TREE;
7141 tree size = declarator->u.array.dimen;
7142 /* The index is a signed object `sizetype' bits wide. */
7143 tree index_type = c_common_signed_type (sizetype);
7144
7145 array_ptr_quals = declarator->u.array.quals;
7146 array_ptr_attrs = declarator->u.array.attrs;
7147 array_parm_static = declarator->u.array.static_p;
7148 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7149
7150 declarator = declarator->declarator;
7151
7152 /* Check for some types that there cannot be arrays of. */
7153
7154 if (VOID_TYPE_P (type))
7155 {
7156 if (name)
7157 error_at (loc, "declaration of %qE as array of voids", name);
7158 else
7159 error_at (loc, "declaration of type name as array of voids");
7160 type = error_mark_node;
7161 }
7162
7163 if (TREE_CODE (type) == FUNCTION_TYPE)
7164 {
7165 if (name)
7166 error_at (loc, "declaration of %qE as array of functions",
7167 name);
7168 else
7169 error_at (loc, "declaration of type name as array of "
7170 "functions");
7171 type = error_mark_node;
7172 }
7173
7174 if (pedantic && !in_system_header_at (loc: input_location)
7175 && flexible_array_type_p (type))
7176 pedwarn (loc, OPT_Wpedantic,
7177 "invalid use of structure with flexible array member");
7178
7179 if (size == error_mark_node)
7180 type = error_mark_node;
7181
7182 if (type == error_mark_node)
7183 continue;
7184
7185 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7186 {
7187 type = error_mark_node;
7188 continue;
7189 }
7190
7191 /* If size was specified, set ITYPE to a range-type for
7192 that size. Otherwise, ITYPE remains null. finish_decl
7193 may figure it out from an initial value. */
7194
7195 if (size)
7196 {
7197 bool size_maybe_const = true;
7198 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7199 && !TREE_OVERFLOW (size));
7200 bool this_size_varies = false;
7201
7202 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7203 lvalue. */
7204 STRIP_TYPE_NOPS (size);
7205
7206 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7207 {
7208 if (name)
7209 error_at (loc, "size of array %qE has non-integer type",
7210 name);
7211 else
7212 error_at (loc,
7213 "size of unnamed array has non-integer type");
7214 size = integer_one_node;
7215 size_int_const = true;
7216 }
7217 /* This can happen with enum forward declaration. */
7218 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7219 {
7220 if (name)
7221 error_at (loc, "size of array %qE has incomplete type",
7222 name);
7223 else
7224 error_at (loc, "size of unnamed array has incomplete "
7225 "type");
7226 size = integer_one_node;
7227 size_int_const = true;
7228 }
7229
7230 size = c_fully_fold (size, false, &size_maybe_const);
7231
7232 if (pedantic && size_maybe_const && integer_zerop (size))
7233 {
7234 if (name)
7235 pedwarn (loc, OPT_Wpedantic,
7236 "ISO C forbids zero-size array %qE", name);
7237 else
7238 pedwarn (loc, OPT_Wpedantic,
7239 "ISO C forbids zero-size array");
7240 }
7241
7242 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7243 {
7244 constant_expression_warning (size);
7245 if (tree_int_cst_sgn (size) < 0)
7246 {
7247 if (name)
7248 error_at (loc, "size of array %qE is negative", name);
7249 else
7250 error_at (loc, "size of unnamed array is negative");
7251 size = integer_one_node;
7252 size_int_const = true;
7253 }
7254 /* Handle a size folded to an integer constant but
7255 not an integer constant expression. */
7256 if (!size_int_const)
7257 {
7258 /* If this is a file scope declaration of an
7259 ordinary identifier, this is invalid code;
7260 diagnosing it here and not subsequently
7261 treating the type as variable-length avoids
7262 more confusing diagnostics later. */
7263 if ((decl_context == NORMAL || decl_context == FIELD)
7264 && current_scope == file_scope)
7265 pedwarn (input_location, 0,
7266 "variably modified %qE at file scope",
7267 name);
7268 else
7269 this_size_varies = size_varies = true;
7270 warn_variable_length_array (name, size);
7271 }
7272 }
7273 else if ((decl_context == NORMAL || decl_context == FIELD)
7274 && current_scope == file_scope)
7275 {
7276 error_at (loc, "variably modified %qE at file scope", name);
7277 size = integer_one_node;
7278 }
7279 else
7280 {
7281 /* Make sure the array size remains visibly
7282 nonconstant even if it is (eg) a const variable
7283 with known value. */
7284 this_size_varies = size_varies = true;
7285 warn_variable_length_array (name, size);
7286 if (sanitize_flags_p (flag: SANITIZE_VLA)
7287 && current_function_decl != NULL_TREE
7288 && decl_context == NORMAL)
7289 {
7290 /* Evaluate the array size only once. */
7291 size = save_expr (size);
7292 size = c_fully_fold (size, false, NULL);
7293 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7294 ubsan_instrument_vla (loc, size),
7295 size);
7296 }
7297 }
7298
7299 if (integer_zerop (size) && !this_size_varies)
7300 {
7301 /* A zero-length array cannot be represented with
7302 an unsigned index type, which is what we'll
7303 get with build_index_type. Create an
7304 open-ended range instead. */
7305 itype = build_range_type (sizetype, size, NULL_TREE);
7306 }
7307 else
7308 {
7309 /* Arrange for the SAVE_EXPR on the inside of the
7310 MINUS_EXPR, which allows the -1 to get folded
7311 with the +1 that happens when building TYPE_SIZE. */
7312 if (size_varies)
7313 size = save_expr (size);
7314 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7315 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7316 integer_zero_node, size);
7317
7318 /* Compute the maximum valid index, that is, size
7319 - 1. Do the calculation in index_type, so that
7320 if it is a variable the computations will be
7321 done in the proper mode. */
7322 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7323 convert (index_type, size),
7324 convert (index_type,
7325 size_one_node));
7326
7327 /* The above overflows when size does not fit
7328 in index_type.
7329 ??? While a size of INT_MAX+1 technically shouldn't
7330 cause an overflow (because we subtract 1), handling
7331 this case seems like an unnecessary complication. */
7332 if (TREE_CODE (size) == INTEGER_CST
7333 && !int_fits_type_p (size, index_type))
7334 {
7335 if (name)
7336 error_at (loc, "size of array %qE is too large",
7337 name);
7338 else
7339 error_at (loc, "size of unnamed array is too large");
7340 type = error_mark_node;
7341 continue;
7342 }
7343
7344 itype = build_index_type (itype);
7345 }
7346 if (this_size_varies)
7347 {
7348 if (TREE_SIDE_EFFECTS (size))
7349 {
7350 if (*expr)
7351 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7352 *expr, size);
7353 else
7354 *expr = size;
7355 }
7356 *expr_const_operands &= size_maybe_const;
7357 }
7358 }
7359 else if (decl_context == FIELD)
7360 {
7361 bool flexible_array_member = false;
7362 if (array_parm_vla_unspec_p)
7363 /* Field names can in fact have function prototype
7364 scope so [*] is disallowed here through making
7365 the field variably modified, not through being
7366 something other than a declaration with function
7367 prototype scope. */
7368 size_varies = true;
7369 else
7370 {
7371 const struct c_declarator *t = declarator;
7372 while (t->kind == cdk_attrs)
7373 t = t->declarator;
7374 flexible_array_member = (t->kind == cdk_id);
7375 }
7376 if (flexible_array_member
7377 && !in_system_header_at (loc: input_location))
7378 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not "
7379 "support flexible array members");
7380
7381 /* ISO C99 Flexible array members are effectively
7382 identical to GCC's zero-length array extension. */
7383 if (flexible_array_member || array_parm_vla_unspec_p)
7384 itype = build_range_type (sizetype, size_zero_node,
7385 NULL_TREE);
7386 }
7387 else if (decl_context == PARM)
7388 {
7389 if (array_parm_vla_unspec_p)
7390 {
7391 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7392 size_varies = true;
7393 }
7394 }
7395 else if (decl_context == TYPENAME)
7396 {
7397 if (array_parm_vla_unspec_p)
7398 {
7399 /* C99 6.7.5.2p4 */
7400 warning (0, "%<[*]%> not in a declaration");
7401 /* We use this to avoid messing up with incomplete
7402 array types of the same type, that would
7403 otherwise be modified below. */
7404 itype = build_range_type (sizetype, size_zero_node,
7405 NULL_TREE);
7406 size_varies = true;
7407 }
7408 }
7409
7410 /* Complain about arrays of incomplete types. */
7411 if (!COMPLETE_TYPE_P (type))
7412 {
7413 auto_diagnostic_group d;
7414 error_at (loc, "array type has incomplete element type %qT",
7415 type);
7416 /* See if we can be more helpful. */
7417 if (TREE_CODE (type) == ARRAY_TYPE)
7418 {
7419 if (name)
7420 inform (loc, "declaration of %qE as multidimensional "
7421 "array must have bounds for all dimensions "
7422 "except the first", name);
7423 else
7424 inform (loc, "declaration of multidimensional array "
7425 "must have bounds for all dimensions except "
7426 "the first");
7427 }
7428 type = error_mark_node;
7429 }
7430 else
7431 /* When itype is NULL, a shared incomplete array type is
7432 returned for all array of a given type. Elsewhere we
7433 make sure we don't complete that type before copying
7434 it, but here we want to make sure we don't ever
7435 modify the shared type, so we gcc_assert (itype)
7436 below. */
7437 {
7438 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7439 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7440 type = build_qualified_type (type,
7441 ENCODE_QUAL_ADDR_SPACE (as));
7442
7443 type = build_array_type (type, itype);
7444 }
7445
7446 if (type != error_mark_node)
7447 {
7448 if (size_varies)
7449 {
7450 /* It is ok to modify type here even if itype is
7451 NULL: if size_varies, we're in a
7452 multi-dimensional array and the inner type has
7453 variable size, so the enclosing shared array type
7454 must too. */
7455 if (size && TREE_CODE (size) == INTEGER_CST)
7456 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7457 C_TYPE_VARIABLE_SIZE (type) = 1;
7458 }
7459
7460 /* The GCC extension for zero-length arrays differs from
7461 ISO flexible array members in that sizeof yields
7462 zero. */
7463 if (size && integer_zerop (size))
7464 {
7465 gcc_assert (itype);
7466 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7467 TYPE_SIZE (type) = bitsize_zero_node;
7468 TYPE_SIZE_UNIT (type) = size_zero_node;
7469 SET_TYPE_STRUCTURAL_EQUALITY (type);
7470 }
7471 if (array_parm_vla_unspec_p)
7472 {
7473 gcc_assert (itype);
7474 /* The type is complete. C99 6.7.5.2p4 */
7475 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7476 TYPE_SIZE (type) = bitsize_zero_node;
7477 TYPE_SIZE_UNIT (type) = size_zero_node;
7478 SET_TYPE_STRUCTURAL_EQUALITY (type);
7479 }
7480
7481 if (!valid_array_size_p (loc, type, name))
7482 type = error_mark_node;
7483 }
7484
7485 if (decl_context != PARM
7486 && (array_ptr_quals != TYPE_UNQUALIFIED
7487 || array_ptr_attrs != NULL_TREE
7488 || array_parm_static))
7489 {
7490 error_at (loc, "static or type qualifiers in non-parameter "
7491 "array declarator");
7492 array_ptr_quals = TYPE_UNQUALIFIED;
7493 array_ptr_attrs = NULL_TREE;
7494 array_parm_static = false;
7495 }
7496 orig_qual_indirect++;
7497 break;
7498 }
7499 case cdk_function:
7500 {
7501 /* Say it's a definition only for the declarator closest
7502 to the identifier, apart possibly from some
7503 attributes. */
7504 bool really_funcdef = false;
7505 tree arg_types;
7506 orig_qual_type = NULL_TREE;
7507 if (funcdef_flag)
7508 {
7509 const struct c_declarator *t = declarator->declarator;
7510 while (t->kind == cdk_attrs)
7511 t = t->declarator;
7512 really_funcdef = (t->kind == cdk_id);
7513 }
7514
7515 /* Declaring a function type. Make sure we have a valid
7516 type for the function to return. */
7517 if (type == error_mark_node)
7518 continue;
7519
7520 size_varies = false;
7521
7522 /* Warn about some types functions can't return. */
7523 if (TREE_CODE (type) == FUNCTION_TYPE)
7524 {
7525 if (name)
7526 error_at (loc, "%qE declared as function returning a "
7527 "function", name);
7528 else
7529 error_at (loc, "type name declared as function "
7530 "returning a function");
7531 type = integer_type_node;
7532 }
7533 if (TREE_CODE (type) == ARRAY_TYPE)
7534 {
7535 if (name)
7536 error_at (loc, "%qE declared as function returning an array",
7537 name);
7538 else
7539 error_at (loc, "type name declared as function returning "
7540 "an array");
7541 type = integer_type_node;
7542 }
7543
7544 /* Construct the function type and go to the next
7545 inner layer of declarator. */
7546 arg_info = declarator->u.arg_info;
7547 arg_types = grokparms (arg_info, really_funcdef);
7548
7549 /* Type qualifiers before the return type of the function
7550 qualify the return type, not the function type. */
7551 if (type_quals)
7552 {
7553 const enum c_declspec_word ignored_quals_list[] =
7554 {
7555 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7556 cdw_atomic, cdw_number_of_elements
7557 };
7558 location_t specs_loc
7559 = smallest_type_quals_location (locations: declspecs->locations,
7560 list: ignored_quals_list);
7561 if (specs_loc == UNKNOWN_LOCATION)
7562 specs_loc = declspecs->locations[cdw_typedef];
7563 if (specs_loc == UNKNOWN_LOCATION)
7564 specs_loc = loc;
7565
7566 /* Type qualifiers on a function return type are
7567 normally permitted by the standard but have no
7568 effect, so give a warning at -Wreturn-type.
7569 Qualifiers on a void return type are banned on
7570 function definitions in ISO C; GCC used to used
7571 them for noreturn functions. The resolution of C11
7572 DR#423 means qualifiers (other than _Atomic) are
7573 actually removed from the return type when
7574 determining the function type. For C23, _Atomic is
7575 removed as well. */
7576 int quals_used = type_quals;
7577 if (flag_isoc23)
7578 quals_used = 0;
7579 else if (flag_isoc11)
7580 quals_used &= TYPE_QUAL_ATOMIC;
7581 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7582 pedwarn (specs_loc, 0,
7583 "function definition has qualified void "
7584 "return type");
7585 else
7586 warning_at (specs_loc, OPT_Wignored_qualifiers,
7587 "type qualifiers ignored on function "
7588 "return type");
7589
7590 /* Ensure an error for restrict on invalid types; the
7591 DR#423 resolution is not entirely clear about
7592 this. */
7593 if (flag_isoc11
7594 && (type_quals & TYPE_QUAL_RESTRICT)
7595 && (!POINTER_TYPE_P (type)
7596 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7597 error_at (loc, "invalid use of %<restrict%>");
7598 type = c_build_qualified_type (type, quals_used);
7599 }
7600 type_quals = TYPE_UNQUALIFIED;
7601
7602 type = build_function_type (type, arg_types,
7603 arg_info->no_named_args_stdarg_p);
7604 declarator = declarator->declarator;
7605
7606 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7607 the formal parameter list of this FUNCTION_TYPE to point to
7608 the FUNCTION_TYPE node itself. */
7609 {
7610 c_arg_tag *tag;
7611 unsigned ix;
7612
7613 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7614 TYPE_CONTEXT (tag->type) = type;
7615 }
7616 break;
7617 }
7618 case cdk_pointer:
7619 {
7620 /* Merge any constancy or volatility into the target type
7621 for the pointer. */
7622 if ((type_quals & TYPE_QUAL_ATOMIC)
7623 && TREE_CODE (type) == FUNCTION_TYPE)
7624 {
7625 error_at (loc,
7626 "%<_Atomic%>-qualified function type");
7627 type_quals &= ~TYPE_QUAL_ATOMIC;
7628 }
7629 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7630 && type_quals)
7631 pedwarn (loc, OPT_Wpedantic,
7632 "ISO C forbids qualified function types");
7633 if (type_quals)
7634 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7635 orig_qual_indirect);
7636 orig_qual_type = NULL_TREE;
7637 size_varies = false;
7638
7639 /* When the pointed-to type involves components of variable size,
7640 care must be taken to ensure that the size evaluation code is
7641 emitted early enough to dominate all the possible later uses
7642 and late enough for the variables on which it depends to have
7643 been assigned.
7644
7645 This is expected to happen automatically when the pointed-to
7646 type has a name/declaration of it's own, but special attention
7647 is required if the type is anonymous. */
7648 if (!TYPE_NAME (type) && c_type_variably_modified_p (t: type))
7649 add_decl_expr (loc, decl_context, type, expr);
7650
7651 type = c_build_pointer_type (to_type: type);
7652
7653 /* Process type qualifiers (such as const or volatile)
7654 that were given inside the `*'. */
7655 type_quals = declarator->u.pointer_quals;
7656
7657 declarator = declarator->declarator;
7658 break;
7659 }
7660 default:
7661 gcc_unreachable ();
7662 }
7663 if (type != error_mark_node)
7664 C_TYPE_VARIABLY_MODIFIED (type) = varmod || size_varies;
7665 }
7666 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7667 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7668
7669 /* Now TYPE has the actual type, apart from any qualifiers in
7670 TYPE_QUALS. */
7671
7672 /* Warn about address space used for things other than static memory or
7673 pointers. */
7674 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7675 if (!ADDR_SPACE_GENERIC_P (address_space))
7676 {
7677 if (decl_context == NORMAL)
7678 {
7679 switch (storage_class)
7680 {
7681 case csc_auto:
7682 error ("%qs combined with %<auto%> qualifier for %qE",
7683 c_addr_space_name (as: address_space), name);
7684 break;
7685 case csc_register:
7686 error ("%qs combined with %<register%> qualifier for %qE",
7687 c_addr_space_name (as: address_space), name);
7688 break;
7689 case csc_none:
7690 if (current_function_scope)
7691 {
7692 error ("%qs specified for auto variable %qE",
7693 c_addr_space_name (as: address_space), name);
7694 break;
7695 }
7696 break;
7697 case csc_static:
7698 case csc_extern:
7699 case csc_typedef:
7700 break;
7701 default:
7702 gcc_unreachable ();
7703 }
7704 }
7705 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7706 {
7707 if (name)
7708 error ("%qs specified for parameter %qE",
7709 c_addr_space_name (as: address_space), name);
7710 else
7711 error ("%qs specified for unnamed parameter",
7712 c_addr_space_name (as: address_space));
7713 }
7714 else if (decl_context == FIELD)
7715 {
7716 if (name)
7717 error ("%qs specified for structure field %qE",
7718 c_addr_space_name (as: address_space), name);
7719 else
7720 error ("%qs specified for structure field",
7721 c_addr_space_name (as: address_space));
7722 }
7723 }
7724
7725 /* Check the type and width of a bit-field. */
7726 if (bitfield)
7727 {
7728 check_bitfield_type_and_width (loc, type: &type, width, orig_name: name);
7729 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7730 atomic types are permitted for bit-fields; we have no code to
7731 make bit-field accesses atomic, so disallow them. */
7732 if (type_quals & TYPE_QUAL_ATOMIC)
7733 {
7734 if (name)
7735 error_at (loc, "bit-field %qE has atomic type", name);
7736 else
7737 error_at (loc, "bit-field has atomic type");
7738 type_quals &= ~TYPE_QUAL_ATOMIC;
7739 }
7740 }
7741
7742 /* Reject invalid uses of _Alignas. */
7743 if (declspecs->alignas_p)
7744 {
7745 if (storage_class == csc_typedef)
7746 error_at (loc, "alignment specified for typedef %qE", name);
7747 else if (storage_class == csc_register)
7748 error_at (loc, "alignment specified for %<register%> object %qE",
7749 name);
7750 else if (decl_context == PARM)
7751 {
7752 if (name)
7753 error_at (loc, "alignment specified for parameter %qE", name);
7754 else
7755 error_at (loc, "alignment specified for unnamed parameter");
7756 }
7757 else if (bitfield)
7758 {
7759 if (name)
7760 error_at (loc, "alignment specified for bit-field %qE", name);
7761 else
7762 error_at (loc, "alignment specified for unnamed bit-field");
7763 }
7764 else if (TREE_CODE (type) == FUNCTION_TYPE)
7765 error_at (loc, "alignment specified for function %qE", name);
7766 else if (declspecs->align_log != -1 && TYPE_P (type))
7767 {
7768 alignas_align = 1U << declspecs->align_log;
7769 if (alignas_align < min_align_of_type (type))
7770 {
7771 if (name)
7772 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7773 "alignment of %qE", name);
7774 else
7775 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7776 "alignment of unnamed field");
7777 alignas_align = 0;
7778 }
7779 }
7780 }
7781
7782 /* If this is declaring a typedef name, return a TYPE_DECL. */
7783
7784 if (storage_class == csc_typedef)
7785 {
7786 tree decl;
7787 if ((type_quals & TYPE_QUAL_ATOMIC)
7788 && TREE_CODE (type) == FUNCTION_TYPE)
7789 {
7790 error_at (loc,
7791 "%<_Atomic%>-qualified function type");
7792 type_quals &= ~TYPE_QUAL_ATOMIC;
7793 }
7794 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7795 && type_quals)
7796 pedwarn (loc, OPT_Wpedantic,
7797 "ISO C forbids qualified function types");
7798 if (type_quals)
7799 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7800 orig_qual_indirect);
7801 decl = build_decl (declarator->id_loc,
7802 TYPE_DECL, declarator->u.id.id, type);
7803 if (declspecs->explicit_signed_p)
7804 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7805 if (declspecs->inline_p)
7806 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7807 if (declspecs->noreturn_p)
7808 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7809
7810 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7811 {
7812 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7813
7814 if (b != NULL
7815 && b->decl != NULL_TREE
7816 && (B_IN_CURRENT_SCOPE (b)
7817 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7818 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7819 {
7820 auto_diagnostic_group d;
7821 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7822 ("using %qD as both a typedef and a tag is "
7823 "invalid in C++"), decl)
7824 && b->locus != UNKNOWN_LOCATION)
7825 inform (b->locus, "originally defined here");
7826 }
7827 }
7828
7829 return decl;
7830 }
7831
7832 /* If this is a type name (such as, in a cast or sizeof),
7833 compute the type and return it now. */
7834
7835 if (decl_context == TYPENAME)
7836 {
7837 /* Note that the grammar rejects storage classes in typenames
7838 and fields. */
7839 gcc_assert (storage_class == csc_none && !threadp
7840 && !declspecs->inline_p && !declspecs->noreturn_p);
7841 if ((type_quals & TYPE_QUAL_ATOMIC)
7842 && TREE_CODE (type) == FUNCTION_TYPE)
7843 {
7844 error_at (loc,
7845 "%<_Atomic%>-qualified function type");
7846 type_quals &= ~TYPE_QUAL_ATOMIC;
7847 }
7848 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7849 && type_quals)
7850 pedwarn (loc, OPT_Wpedantic,
7851 "ISO C forbids const or volatile function types");
7852 if (type_quals)
7853 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7854 orig_qual_indirect);
7855 return type;
7856 }
7857
7858 if (pedantic && decl_context == FIELD
7859 && c_type_variably_modified_p (t: type))
7860 {
7861 /* C99 6.7.2.1p8 */
7862 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7863 "have a variably modified type");
7864 }
7865
7866 /* Aside from typedefs and type names (handle above),
7867 `void' at top level (not within pointer)
7868 is allowed only in public variables.
7869 We don't complain about parms either, but that is because
7870 a better error message can be made later. */
7871
7872 if (VOID_TYPE_P (type) && decl_context != PARM
7873 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7874 && (storage_class == csc_extern
7875 || (current_scope == file_scope
7876 && !(storage_class == csc_static
7877 || storage_class == csc_register)))))
7878 {
7879 error_at (loc, "variable or field %qE declared void", name);
7880 type = integer_type_node;
7881 }
7882
7883 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7884 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7885
7886 {
7887 tree decl;
7888
7889 if (decl_context == PARM)
7890 {
7891 tree promoted_type;
7892 bool array_parameter_p = false;
7893
7894 /* A parameter declared as an array of T is really a pointer to T.
7895 One declared as a function is really a pointer to a function. */
7896
7897 if (TREE_CODE (type) == ARRAY_TYPE)
7898 {
7899 /* Transfer const-ness of array into that of type pointed to. */
7900 type = TREE_TYPE (type);
7901 if (orig_qual_type != NULL_TREE)
7902 {
7903 if (orig_qual_indirect == 0)
7904 orig_qual_type = TREE_TYPE (orig_qual_type);
7905 else
7906 orig_qual_indirect--;
7907 }
7908 if (type_quals)
7909 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7910 orig_qual_indirect);
7911
7912 /* The pointed-to type may need a decl expr (see above). */
7913 if (!TYPE_NAME (type) && c_type_variably_modified_p (t: type))
7914 add_decl_expr (loc, decl_context, type, expr);
7915
7916 type = c_build_pointer_type (to_type: type);
7917 type_quals = array_ptr_quals;
7918 if (type_quals)
7919 type = c_build_qualified_type (type, type_quals);
7920
7921 /* We don't yet implement attributes in this context. */
7922 if (array_ptr_attrs != NULL_TREE)
7923 warning_at (loc, OPT_Wattributes,
7924 "attributes in parameter array declarator ignored");
7925
7926 size_varies = false;
7927 array_parameter_p = true;
7928 }
7929 else if (TREE_CODE (type) == FUNCTION_TYPE)
7930 {
7931 if (type_quals & TYPE_QUAL_ATOMIC)
7932 {
7933 error_at (loc,
7934 "%<_Atomic%>-qualified function type");
7935 type_quals &= ~TYPE_QUAL_ATOMIC;
7936 }
7937 else if (type_quals)
7938 pedwarn (loc, OPT_Wpedantic,
7939 "ISO C forbids qualified function types");
7940 if (type_quals)
7941 type = c_build_qualified_type (type, type_quals);
7942 type = c_build_pointer_type (to_type: type);
7943 type_quals = TYPE_UNQUALIFIED;
7944 }
7945 else if (type_quals)
7946 type = c_build_qualified_type (type, type_quals);
7947
7948 decl = build_decl (declarator->id_loc,
7949 PARM_DECL, declarator->u.id.id, type);
7950 if (size_varies)
7951 C_DECL_VARIABLE_SIZE (decl) = 1;
7952 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7953
7954 /* Compute the type actually passed in the parmlist,
7955 for the case where there is no prototype.
7956 (For example, shorts and chars are passed as ints.)
7957 When there is a prototype, this is overridden later. */
7958
7959 if (type == error_mark_node)
7960 promoted_type = type;
7961 else
7962 promoted_type = c_type_promotes_to (type);
7963
7964 DECL_ARG_TYPE (decl) = promoted_type;
7965 if (declspecs->inline_p)
7966 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7967 if (declspecs->noreturn_p)
7968 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7969 }
7970 else if (decl_context == FIELD)
7971 {
7972 /* Note that the grammar rejects storage classes in typenames
7973 and fields. */
7974 gcc_assert (storage_class == csc_none && !threadp
7975 && !declspecs->inline_p && !declspecs->noreturn_p);
7976
7977 /* Structure field. It may not be a function. */
7978
7979 if (TREE_CODE (type) == FUNCTION_TYPE)
7980 {
7981 error_at (loc, "field %qE declared as a function", name);
7982 type = build_pointer_type (type);
7983 }
7984 else if (TREE_CODE (type) != ERROR_MARK
7985 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7986 {
7987 if (name)
7988 error_at (loc, "field %qE has incomplete type", name);
7989 else
7990 error_at (loc, "unnamed field has incomplete type");
7991 type = error_mark_node;
7992 }
7993 else if (TREE_CODE (type) == ARRAY_TYPE
7994 && TYPE_DOMAIN (type) == NULL_TREE)
7995 {
7996 /* We have a flexible array member through a typedef.
7997 Set suitable range. Whether this is a correct position
7998 for a flexible array member will be determined elsewhere. */
7999 if (!in_system_header_at (loc: input_location))
8000 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not "
8001 "support flexible array members");
8002 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8003 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8004 NULL_TREE);
8005 if (orig_qual_indirect == 0)
8006 orig_qual_type = NULL_TREE;
8007 }
8008 if (type != error_mark_node
8009 && !verify_type_context (loc, TCTX_FIELD, type))
8010 type = error_mark_node;
8011
8012 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8013 orig_qual_indirect);
8014 decl = build_decl (declarator->id_loc,
8015 FIELD_DECL, declarator->u.id.id, type);
8016 DECL_NONADDRESSABLE_P (decl) = bitfield;
8017 if (bitfield && !declarator->u.id.id)
8018 DECL_PADDING_P (decl) = 1;
8019
8020 if (size_varies)
8021 C_DECL_VARIABLE_SIZE (decl) = 1;
8022 }
8023 else if (TREE_CODE (type) == FUNCTION_TYPE)
8024 {
8025 if (storage_class == csc_register || threadp || constexprp)
8026 {
8027 error_at (loc, "invalid storage class for function %qE", name);
8028 }
8029 else if (current_scope != file_scope)
8030 {
8031 /* Function declaration not at file scope. Storage
8032 classes other than `extern' are not allowed, C99
8033 6.7.1p5, and `extern' makes no difference. However,
8034 GCC allows 'auto', perhaps with 'inline', to support
8035 nested functions. */
8036 if (storage_class == csc_auto)
8037 pedwarn (loc, OPT_Wpedantic,
8038 "invalid storage class for function %qE", name);
8039 else if (storage_class == csc_static)
8040 {
8041 error_at (loc, "invalid storage class for function %qE", name);
8042 if (funcdef_flag)
8043 storage_class = declspecs->storage_class = csc_none;
8044 else
8045 return NULL_TREE;
8046 }
8047 }
8048
8049 decl = build_decl (declarator->id_loc,
8050 FUNCTION_DECL, declarator->u.id.id, type);
8051 decl = build_decl_attribute_variant (decl, decl_attr);
8052
8053 if (type_quals & TYPE_QUAL_ATOMIC)
8054 {
8055 error_at (loc,
8056 "%<_Atomic%>-qualified function type");
8057 type_quals &= ~TYPE_QUAL_ATOMIC;
8058 }
8059 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8060 pedwarn (loc, OPT_Wpedantic,
8061 "ISO C forbids qualified function types");
8062
8063 /* Every function declaration is an external reference
8064 (DECL_EXTERNAL) except for those which are not at file
8065 scope and are explicitly declared "auto". This is
8066 forbidden by standard C (C99 6.7.1p5) and is interpreted by
8067 GCC to signify a forward declaration of a nested function. */
8068 if (storage_class == csc_auto && current_scope != file_scope)
8069 DECL_EXTERNAL (decl) = 0;
8070 /* In C99, a function which is declared 'inline' with 'extern'
8071 is not an external reference (which is confusing). It
8072 means that the later definition of the function must be output
8073 in this file, C99 6.7.4p6. In GNU C89, a function declared
8074 'extern inline' is an external reference. */
8075 else if (declspecs->inline_p && storage_class != csc_static)
8076 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8077 == flag_gnu89_inline);
8078 else
8079 DECL_EXTERNAL (decl) = !initialized;
8080
8081 /* Record absence of global scope for `static' or `auto'. */
8082 TREE_PUBLIC (decl)
8083 = !(storage_class == csc_static || storage_class == csc_auto);
8084
8085 /* For a function definition, record the argument information
8086 block where store_parm_decls will look for it. */
8087 if (funcdef_flag)
8088 current_function_arg_info = arg_info;
8089
8090 if (declspecs->default_int_p)
8091 C_FUNCTION_IMPLICIT_INT (decl) = 1;
8092
8093 /* Record presence of `inline' and `_Noreturn', if it is
8094 reasonable. */
8095 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8096 {
8097 if (declspecs->inline_p)
8098 pedwarn (loc, 0, "cannot inline function %<main%>");
8099 if (declspecs->noreturn_p)
8100 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8101 }
8102 else
8103 {
8104 if (declspecs->inline_p)
8105 /* Record that the function is declared `inline'. */
8106 DECL_DECLARED_INLINE_P (decl) = 1;
8107 if (declspecs->noreturn_p)
8108 {
8109 if (flag_isoc99)
8110 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8111 "ISO C99 does not support %<_Noreturn%>");
8112 else
8113 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8114 "ISO C90 does not support %<_Noreturn%>");
8115 TREE_THIS_VOLATILE (decl) = 1;
8116 }
8117 }
8118
8119 /* C99 6.2.2p7: It is invalid (compile-time undefined
8120 behavior) to create an 'extern' declaration for a
8121 function if there is a global declaration that is
8122 'static' and the global declaration is not visible.
8123 (If the static declaration _is_ currently visible,
8124 the 'extern' declaration is taken to refer to that decl.) */
8125 if (!initialized
8126 && TREE_PUBLIC (decl)
8127 && current_scope != file_scope)
8128 {
8129 tree global_decl = identifier_global_value (declarator->u.id.id);
8130 tree visible_decl = lookup_name (name: declarator->u.id.id);
8131
8132 if (global_decl
8133 && global_decl != visible_decl
8134 && VAR_OR_FUNCTION_DECL_P (global_decl)
8135 && !TREE_PUBLIC (global_decl))
8136 error_at (loc, "function previously declared %<static%> "
8137 "redeclared %<extern%>");
8138 }
8139 }
8140 else
8141 {
8142 /* It's a variable. */
8143 /* An uninitialized decl with `extern' is a reference. */
8144 int extern_ref = !initialized && storage_class == csc_extern;
8145
8146 if (constexprp)
8147 {
8148 /* The type of a constexpr variable must not be variably
8149 modified, volatile, atomic or restrict qualified or
8150 have a member with such a qualifier. const
8151 qualification is implicitly added, and, at file scope,
8152 has internal linkage. */
8153 if (c_type_variably_modified_p (t: type))
8154 error_at (loc, "%<constexpr%> object has variably modified "
8155 "type");
8156 if (type_quals
8157 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8158 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8159 else
8160 {
8161 tree type_no_array = strip_array_types (type);
8162 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8163 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8164 error_at (loc, "invalid qualifiers for field of "
8165 "%<constexpr%> object");
8166 }
8167 type_quals |= TYPE_QUAL_CONST;
8168 if (current_scope == file_scope)
8169 storage_class = csc_static;
8170 }
8171
8172 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8173 orig_qual_indirect);
8174
8175 /* C99 6.2.2p7: It is invalid (compile-time undefined
8176 behavior) to create an 'extern' declaration for a
8177 variable if there is a global declaration that is
8178 'static' and the global declaration is not visible.
8179 (If the static declaration _is_ currently visible,
8180 the 'extern' declaration is taken to refer to that decl.) */
8181 if (extern_ref && current_scope != file_scope)
8182 {
8183 tree global_decl = identifier_global_value (declarator->u.id.id);
8184 tree visible_decl = lookup_name (name: declarator->u.id.id);
8185
8186 if (global_decl
8187 && global_decl != visible_decl
8188 && VAR_P (global_decl)
8189 && !TREE_PUBLIC (global_decl))
8190 error_at (loc, "variable previously declared %<static%> "
8191 "redeclared %<extern%>");
8192 }
8193
8194 decl = build_decl (declarator->id_loc,
8195 VAR_DECL, declarator->u.id.id, type);
8196 if (size_varies)
8197 C_DECL_VARIABLE_SIZE (decl) = 1;
8198 if (constexprp)
8199 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8200
8201 if (declspecs->inline_p)
8202 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8203 if (declspecs->noreturn_p)
8204 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8205
8206 /* At file scope, an initialized extern declaration may follow
8207 a static declaration. In that case, DECL_EXTERNAL will be
8208 reset later in start_decl. */
8209 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8210
8211 /* At file scope, the presence of a `static' or `register' storage
8212 class specifier, or the absence of all storage class specifiers
8213 makes this declaration a definition (perhaps tentative). Also,
8214 the absence of `static' makes it public. */
8215 if (current_scope == file_scope)
8216 {
8217 TREE_PUBLIC (decl) = storage_class != csc_static;
8218 TREE_STATIC (decl) = !extern_ref;
8219 }
8220 /* Not at file scope, only `static' makes a static definition. */
8221 else
8222 {
8223 TREE_STATIC (decl) = (storage_class == csc_static);
8224 TREE_PUBLIC (decl) = extern_ref;
8225 }
8226
8227 if (threadp)
8228 set_decl_tls_model (decl, decl_default_tls_model (decl));
8229 }
8230
8231 if ((storage_class == csc_extern
8232 || (storage_class == csc_none
8233 && TREE_CODE (type) == FUNCTION_TYPE
8234 && !funcdef_flag))
8235 && c_type_variably_modified_p (t: type))
8236 {
8237 /* C99 6.7.5.2p2 */
8238 if (TREE_CODE (type) == FUNCTION_TYPE)
8239 error_at (loc, "non-nested function with variably modified type");
8240 else
8241 error_at (loc, "object with variably modified type must have "
8242 "no linkage");
8243 }
8244
8245 /* For nested functions disqualify ones taking VLAs by value
8246 from inlining since the middle-end cannot deal with this.
8247 ??? We should arrange for those to be passed by reference
8248 with emitting the copy on the caller side in the frontend. */
8249 if (storage_class == csc_none
8250 && TREE_CODE (type) == FUNCTION_TYPE)
8251 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8252 {
8253 tree arg = TREE_VALUE (al);
8254 if (arg != error_mark_node
8255 && C_TYPE_VARIABLE_SIZE (arg))
8256 {
8257 DECL_UNINLINABLE (decl) = 1;
8258 break;
8259 }
8260 }
8261
8262 /* Record `register' declaration for warnings on &
8263 and in case doing stupid register allocation. */
8264
8265 if (storage_class == csc_register)
8266 {
8267 C_DECL_REGISTER (decl) = 1;
8268 DECL_REGISTER (decl) = 1;
8269 }
8270
8271 /* Record constancy and volatility. */
8272 c_apply_type_quals_to_decl (type_quals, decl);
8273
8274 /* Apply _Alignas specifiers. */
8275 if (alignas_align)
8276 {
8277 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8278 DECL_USER_ALIGN (decl) = 1;
8279 }
8280
8281 /* If a type has volatile components, it should be stored in memory.
8282 Otherwise, the fact that those components are volatile
8283 will be ignored, and would even crash the compiler.
8284 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8285 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8286 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8287 || TREE_CODE (decl) == RESULT_DECL))
8288 {
8289 /* It is not an error for a structure with volatile fields to
8290 be declared register, but reset DECL_REGISTER since it
8291 cannot actually go in a register. */
8292 int was_reg = C_DECL_REGISTER (decl);
8293 C_DECL_REGISTER (decl) = 0;
8294 DECL_REGISTER (decl) = 0;
8295 c_mark_addressable (decl);
8296 C_DECL_REGISTER (decl) = was_reg;
8297 }
8298
8299 /* This is the earliest point at which we might know the assembler
8300 name of a variable. Thus, if it's known before this, die horribly. */
8301 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8302 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8303
8304 if (warn_cxx_compat
8305 && VAR_P (decl)
8306 && TREE_PUBLIC (decl)
8307 && TREE_STATIC (decl)
8308 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8309 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8310 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8311 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8312 ("non-local variable %qD with anonymous type is "
8313 "questionable in C++"),
8314 decl);
8315
8316 return decl;
8317 }
8318}
8319
8320/* Decode the parameter-list info for a function type or function definition.
8321 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8322 if there is an identifier list instead of a parameter decl list).
8323 These two functions are separate because when a function returns
8324 or receives functions then each is called multiple times but the order
8325 of calls is different. The last call to `grokparms' is always the one
8326 that contains the formal parameter names of a function definition.
8327
8328 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8329
8330 FUNCDEF_FLAG is true for a function definition, false for
8331 a mere declaration. A nonempty identifier-list gets an error message
8332 when FUNCDEF_FLAG is false. */
8333
8334static tree
8335grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8336{
8337 tree arg_types = arg_info->types;
8338
8339 if (funcdef_flag && arg_info->had_vla_unspec)
8340 {
8341 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8342 /* C99 6.7.5.2p4 */
8343 error ("%<[*]%> not allowed in other than function prototype scope");
8344 }
8345
8346 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8347 && !in_system_header_at (loc: input_location))
8348 warning (OPT_Wstrict_prototypes,
8349 "function declaration isn%'t a prototype");
8350
8351 if (arg_types == error_mark_node)
8352 /* Don't set TYPE_ARG_TYPES in this case. */
8353 return NULL_TREE;
8354
8355 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8356 {
8357 if (!funcdef_flag)
8358 {
8359 pedwarn (input_location, 0, "parameter names (without types) in "
8360 "function declaration");
8361 arg_info->parms = NULL_TREE;
8362 }
8363 else
8364 arg_info->parms = arg_info->types;
8365
8366 arg_info->types = NULL_TREE;
8367 return NULL_TREE;
8368 }
8369 else
8370 {
8371 tree parm, type, typelt;
8372 unsigned int parmno;
8373
8374 /* In C23, convert () to (void). */
8375 if (flag_isoc23
8376 && !arg_types
8377 && !arg_info->parms
8378 && !arg_info->no_named_args_stdarg_p)
8379 arg_types = arg_info->types = void_list_node;
8380
8381 /* If there is a parameter of incomplete type in a definition,
8382 this is an error. In a declaration this is valid, and a
8383 struct or union type may be completed later, before any calls
8384 or definition of the function. In the case where the tag was
8385 first declared within the parameter list, a warning has
8386 already been given. If a parameter has void type, then
8387 however the function cannot be defined or called, so
8388 warn. */
8389
8390 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8391 parm;
8392 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8393 {
8394 type = TREE_VALUE (typelt);
8395 if (type == error_mark_node)
8396 continue;
8397
8398 if (!COMPLETE_TYPE_P (type))
8399 {
8400 if (funcdef_flag)
8401 {
8402 if (DECL_NAME (parm))
8403 error_at (input_location,
8404 "parameter %u (%q+D) has incomplete type",
8405 parmno, parm);
8406 else
8407 error_at (DECL_SOURCE_LOCATION (parm),
8408 "parameter %u has incomplete type",
8409 parmno);
8410
8411 TREE_VALUE (typelt) = error_mark_node;
8412 TREE_TYPE (parm) = error_mark_node;
8413 arg_types = NULL_TREE;
8414 }
8415 else if (VOID_TYPE_P (type))
8416 {
8417 if (DECL_NAME (parm))
8418 warning_at (input_location, 0,
8419 "parameter %u (%q+D) has void type",
8420 parmno, parm);
8421 else
8422 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8423 "parameter %u has void type",
8424 parmno);
8425 }
8426 }
8427
8428 if (DECL_NAME (parm) && TREE_USED (parm))
8429 warn_if_shadowing (new_decl: parm);
8430 }
8431 return arg_types;
8432 }
8433}
8434
8435/* Allocate and initialize a c_arg_info structure from the parser's
8436 obstack. */
8437
8438struct c_arg_info *
8439build_arg_info (void)
8440{
8441 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8442 ret->parms = NULL_TREE;
8443 ret->tags = NULL;
8444 ret->types = NULL_TREE;
8445 ret->others = NULL_TREE;
8446 ret->pending_sizes = NULL;
8447 ret->had_vla_unspec = 0;
8448 ret->no_named_args_stdarg_p = 0;
8449 return ret;
8450}
8451
8452/* Take apart the current scope and return a c_arg_info structure with
8453 info on a parameter list just parsed.
8454
8455 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8456
8457 ELLIPSIS being true means the argument list ended in '...' so don't
8458 append a sentinel (void_list_node) to the end of the type-list.
8459
8460 EXPR is NULL or an expression that needs to be evaluated for the
8461 side effects of array size expressions in the parameters. */
8462
8463struct c_arg_info *
8464get_parm_info (bool ellipsis, tree expr)
8465{
8466 struct c_binding *b = current_scope->bindings;
8467 struct c_arg_info *arg_info = build_arg_info ();
8468
8469 tree parms = NULL_TREE;
8470 vec<c_arg_tag, va_gc> *tags = NULL;
8471 tree types = NULL_TREE;
8472 tree others = NULL_TREE;
8473
8474 bool gave_void_only_once_err = false;
8475
8476 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8477
8478 /* The bindings in this scope must not get put into a block.
8479 We will take care of deleting the binding nodes. */
8480 current_scope->bindings = 0;
8481
8482 /* This function is only called if there was *something* on the
8483 parameter list. */
8484 gcc_assert (b);
8485
8486 /* A parameter list consisting solely of 'void' indicates that the
8487 function takes no arguments. But if the 'void' is qualified
8488 (by 'const' or 'volatile'), or has a storage class specifier
8489 ('register'), then the behavior is undefined; issue an error.
8490 Typedefs for 'void' are OK (see DR#157). */
8491 if (b->prev == 0 /* one binding */
8492 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8493 && !DECL_NAME (b->decl) /* anonymous */
8494 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8495 {
8496 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8497 || C_DECL_REGISTER (b->decl))
8498 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8499
8500 /* There cannot be an ellipsis. */
8501 if (ellipsis)
8502 error_at (b->locus, "%<void%> must be the only parameter");
8503
8504 arg_info->types = void_list_node;
8505 return arg_info;
8506 }
8507
8508 if (!ellipsis)
8509 types = void_list_node;
8510
8511 /* Break up the bindings list into parms, tags, types, and others;
8512 apply sanity checks; purge the name-to-decl bindings. */
8513 while (b)
8514 {
8515 tree decl = b->decl;
8516 tree type = TREE_TYPE (decl);
8517 c_arg_tag tag;
8518 const char *keyword;
8519
8520 switch (TREE_CODE (decl))
8521 {
8522 case PARM_DECL:
8523 if (b->id)
8524 {
8525 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8526 I_SYMBOL_BINDING (b->id) = b->shadowed;
8527 }
8528
8529 /* Check for forward decls that never got their actual decl. */
8530 if (TREE_ASM_WRITTEN (decl))
8531 error_at (b->locus,
8532 "parameter %q+D has just a forward declaration", decl);
8533 /* Check for (..., void, ...) and issue an error. */
8534 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8535 {
8536 if (!gave_void_only_once_err)
8537 {
8538 error_at (b->locus, "%<void%> must be the only parameter");
8539 gave_void_only_once_err = true;
8540 }
8541 }
8542 else
8543 {
8544 /* Valid parameter, add it to the list. */
8545 DECL_CHAIN (decl) = parms;
8546 parms = decl;
8547
8548 /* Since there is a prototype, args are passed in their
8549 declared types. The back end may override this later. */
8550 DECL_ARG_TYPE (decl) = type;
8551 types = tree_cons (0, type, types);
8552 }
8553 break;
8554
8555 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8556 case UNION_TYPE: keyword = "union"; goto tag;
8557 case RECORD_TYPE: keyword = "struct"; goto tag;
8558 tag:
8559 /* Types may not have tag-names, in which case the type
8560 appears in the bindings list with b->id NULL. */
8561 if (b->id)
8562 {
8563 gcc_assert (I_TAG_BINDING (b->id) == b);
8564 I_TAG_BINDING (b->id) = b->shadowed;
8565 }
8566
8567 /* Warn about any struct, union or enum tags defined in a
8568 parameter list. The scope of such types is limited to
8569 the parameter list, which is rarely if ever desirable
8570 (it's impossible to call such a function with type-
8571 correct arguments). An anonymous union parm type is
8572 meaningful as a GNU extension, so don't warn for that. */
8573 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8574 {
8575 if (b->id)
8576 /* The %s will be one of 'struct', 'union', or 'enum'. */
8577 warning_at (b->locus, 0,
8578 "%<%s %E%> declared inside parameter list"
8579 " will not be visible outside of this definition or"
8580 " declaration", keyword, b->id);
8581 else
8582 /* The %s will be one of 'struct', 'union', or 'enum'. */
8583 warning_at (b->locus, 0,
8584 "anonymous %s declared inside parameter list"
8585 " will not be visible outside of this definition or"
8586 " declaration", keyword);
8587 }
8588
8589 tag.id = b->id;
8590 tag.type = decl;
8591 vec_safe_push (v&: tags, obj: tag);
8592 break;
8593
8594 case FUNCTION_DECL:
8595 /* FUNCTION_DECLs appear when there is an implicit function
8596 declaration in the parameter list. */
8597 gcc_assert (b->nested || seen_error ());
8598 goto set_shadowed;
8599
8600 case CONST_DECL:
8601 case TYPE_DECL:
8602 /* CONST_DECLs appear here when we have an embedded enum,
8603 and TYPE_DECLs appear here when we have an embedded struct
8604 or union. No warnings for this - we already warned about the
8605 type itself. */
8606
8607 /* When we reinsert this decl in the function body, we need
8608 to reconstruct whether it was marked as nested. */
8609 gcc_assert (!b->nested);
8610 DECL_CHAIN (decl) = others;
8611 others = decl;
8612 /* fall through */
8613
8614 case ERROR_MARK:
8615 set_shadowed:
8616 /* error_mark_node appears here when we have an undeclared
8617 variable. Just throw it away. */
8618 if (b->id)
8619 {
8620 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8621 I_SYMBOL_BINDING (b->id) = b->shadowed;
8622 }
8623 break;
8624
8625 /* Other things that might be encountered. */
8626 case LABEL_DECL:
8627 case VAR_DECL:
8628 default:
8629 gcc_unreachable ();
8630 }
8631
8632 b = free_binding_and_advance (b);
8633 }
8634
8635 arg_info->parms = parms;
8636 arg_info->tags = tags;
8637 arg_info->types = types;
8638 arg_info->others = others;
8639 arg_info->pending_sizes = expr;
8640 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8641 return arg_info;
8642}
8643
8644/* Get the struct, enum or union (CODE says which) with tag NAME.
8645 Define the tag as a forward-reference with location LOC if it is
8646 not defined. HAVE_STD_ATTRS says whether any standard attributes
8647 were present after the struct, union or enum keyword; ATTRS are the
8648 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8649 whether an enum type specifier (": specifier-qualifier-list") is
8650 present; if so, this is called before that specifier is parsed, so
8651 that the tag is in scope for that specifier. Return a c_typespec
8652 structure for the type specifier. */
8653
8654struct c_typespec
8655parser_xref_tag (location_t loc, enum tree_code code, tree name,
8656 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8657{
8658 struct c_typespec ret;
8659 tree ref;
8660 location_t refloc;
8661
8662 ret.expr = NULL_TREE;
8663 ret.expr_const_operands = true;
8664 ret.has_enum_type_specifier = has_enum_type_specifier;
8665
8666 /* If a cross reference is requested, look up the type already
8667 defined for this tag and return it. If an enum type specifier is
8668 present, only a definition in the current scope is relevant. */
8669
8670 ref = lookup_tag (code, name, thislevel_only: has_enum_type_specifier, ploc: &refloc);
8671 /* If this is the right type of tag, return what we found.
8672 (This reference will be shadowed by shadow_tag later if appropriate.)
8673 If this is the wrong type of tag, do not return it. If it was the
8674 wrong type in the same scope, we will have had an error
8675 message already; if in a different scope and declaring
8676 a name, pending_xref_error will give an error message; but if in a
8677 different scope and not declaring a name, this tag should
8678 shadow the previous declaration of a different type of tag, and
8679 this would not work properly if we return the reference found.
8680 (For example, with "struct foo" in an outer scope, "union foo;"
8681 must shadow that tag with a new one of union type.) */
8682 ret.kind = (ref
8683 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8684 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8685 if (ref && TREE_CODE (ref) == code)
8686 {
8687 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8688 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8689 && loc != UNKNOWN_LOCATION
8690 && warn_cxx_compat)
8691 {
8692 auto_diagnostic_group d;
8693 switch (code)
8694 {
8695 case ENUMERAL_TYPE:
8696 if (warning_at (loc, OPT_Wc___compat,
8697 ("enum type defined in struct or union "
8698 "is not visible in C++")))
8699 inform (refloc, "enum type defined here");
8700 break;
8701 case RECORD_TYPE:
8702 if (warning_at (loc, OPT_Wc___compat,
8703 ("struct defined in struct or union "
8704 "is not visible in C++")))
8705 inform (refloc, "struct defined here");
8706 break;
8707 case UNION_TYPE:
8708 if (warning_at (loc, OPT_Wc___compat,
8709 ("union defined in struct or union "
8710 "is not visible in C++")))
8711 inform (refloc, "union defined here");
8712 break;
8713 default:
8714 gcc_unreachable();
8715 }
8716 }
8717
8718 ret.spec = ref;
8719 return ret;
8720 }
8721
8722 /* If no such tag is yet defined, create a forward-reference node
8723 and record it as the "definition".
8724 When a real declaration of this type is found,
8725 the forward-reference will be altered into a real type. */
8726
8727 ref = make_node (code);
8728 if (code == ENUMERAL_TYPE)
8729 {
8730 /* Give the type a default layout like unsigned int
8731 to avoid crashing if it does not get defined. */
8732 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8733 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8734 TYPE_USER_ALIGN (ref) = 0;
8735 TYPE_UNSIGNED (ref) = 1;
8736 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8737 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8738 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8739 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8740 }
8741
8742 pushtag (loc, name, type: ref);
8743 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8744 if (in_underspecified_init)
8745 error_at (loc, "%qT declared in underspecified object initializer",
8746 ref);
8747
8748 ret.spec = ref;
8749 return ret;
8750}
8751
8752/* Get the struct, enum or union (CODE says which) with tag NAME.
8753 Define the tag as a forward-reference if it is not defined.
8754 Return a tree for the type. */
8755
8756tree
8757xref_tag (enum tree_code code, tree name)
8758{
8759 return parser_xref_tag (loc: input_location, code, name, have_std_attrs: false, NULL_TREE,
8760 has_enum_type_specifier: false).spec;
8761}
8762
8763/* Make sure that the tag NAME is defined *in the current scope*
8764 at least as a forward reference.
8765 LOC is the location of the struct's definition.
8766 CODE says which kind of tag NAME ought to be.
8767
8768 This stores the current value of the file static STRUCT_PARSE_INFO
8769 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8770 new c_struct_parse_info structure. The old value of
8771 STRUCT_PARSE_INFO is restored in finish_struct. */
8772
8773tree
8774start_struct (location_t loc, enum tree_code code, tree name,
8775 class c_struct_parse_info **enclosing_struct_parse_info)
8776{
8777 /* If there is already a tag defined at this scope
8778 (as a forward reference), just return it. */
8779
8780 tree ref = NULL_TREE;
8781 location_t refloc = UNKNOWN_LOCATION;
8782
8783 if (name != NULL_TREE)
8784 ref = lookup_tag (code, name, thislevel_only: true, ploc: &refloc);
8785 if (ref && TREE_CODE (ref) == code)
8786 {
8787 if (TYPE_STUB_DECL (ref))
8788 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8789
8790 if (TYPE_SIZE (ref))
8791 {
8792 auto_diagnostic_group d;
8793 if (code == UNION_TYPE)
8794 error_at (loc, "redefinition of %<union %E%>", name);
8795 else
8796 error_at (loc, "redefinition of %<struct %E%>", name);
8797 if (refloc != UNKNOWN_LOCATION)
8798 inform (refloc, "originally defined here");
8799 /* Don't create structures using a name already in use. */
8800 ref = NULL_TREE;
8801 }
8802 else if (C_TYPE_BEING_DEFINED (ref))
8803 {
8804 if (code == UNION_TYPE)
8805 error_at (loc, "nested redefinition of %<union %E%>", name);
8806 else
8807 error_at (loc, "nested redefinition of %<struct %E%>", name);
8808 /* Don't bother to report "originally defined here" for a
8809 nested redefinition; the original definition should be
8810 obvious. */
8811 /* Don't create structures that contain themselves. */
8812 ref = NULL_TREE;
8813 }
8814 }
8815
8816 /* Otherwise create a forward-reference just so the tag is in scope. */
8817
8818 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8819 {
8820 ref = make_node (code);
8821 pushtag (loc, name, type: ref);
8822 }
8823
8824 C_TYPE_BEING_DEFINED (ref) = 1;
8825 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8826 TYPE_PACKED (v) = flag_pack_struct;
8827
8828 *enclosing_struct_parse_info = struct_parse_info;
8829 struct_parse_info = new c_struct_parse_info ();
8830
8831 /* FIXME: This will issue a warning for a use of a type defined
8832 within a statement expr used within sizeof, et. al. This is not
8833 terribly serious as C++ doesn't permit statement exprs within
8834 sizeof anyhow. */
8835 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8836 warning_at (loc, OPT_Wc___compat,
8837 "defining type in %qs expression is invalid in C++",
8838 (in_sizeof
8839 ? "sizeof"
8840 : (in_typeof ? "typeof" : "alignof")));
8841
8842 if (in_underspecified_init)
8843 error_at (loc, "%qT defined in underspecified object initializer", ref);
8844
8845 return ref;
8846}
8847
8848/* Process the specs, declarator and width (NULL if omitted)
8849 of a structure component, returning a FIELD_DECL node.
8850 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8851 DECL_ATTRS is as for grokdeclarator.
8852
8853 LOC is the location of the structure component.
8854
8855 This is done during the parsing of the struct declaration.
8856 The FIELD_DECL nodes are chained together and the lot of them
8857 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8858
8859tree
8860grokfield (location_t loc,
8861 struct c_declarator *declarator, struct c_declspecs *declspecs,
8862 tree width, tree *decl_attrs, tree *expr)
8863{
8864 tree value;
8865
8866 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8867 && width == NULL_TREE)
8868 {
8869 /* This is an unnamed decl.
8870
8871 If we have something of the form "union { list } ;" then this
8872 is the anonymous union extension. Similarly for struct.
8873
8874 If this is something of the form "struct foo;", then
8875 If MS or Plan 9 extensions are enabled, this is handled as
8876 an anonymous struct.
8877 Otherwise this is a forward declaration of a structure tag.
8878
8879 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8880 If foo names a structure or union without a tag, then this
8881 is an anonymous struct (this is permitted by C11).
8882 If MS or Plan 9 extensions are enabled and foo names a
8883 structure, then again this is an anonymous struct.
8884 Otherwise this is an error.
8885
8886 Oh what a horrid tangled web we weave. I wonder if MS consciously
8887 took this from Plan 9 or if it was an accident of implementation
8888 that took root before someone noticed the bug... */
8889
8890 tree type = declspecs->type;
8891 bool ok = false;
8892
8893 if (RECORD_OR_UNION_TYPE_P (type)
8894 && (flag_ms_extensions
8895 || flag_plan9_extensions
8896 || !declspecs->typedef_p))
8897 {
8898 if (flag_ms_extensions || flag_plan9_extensions)
8899 ok = true;
8900 else if (TYPE_NAME (type) == NULL)
8901 ok = true;
8902 else
8903 ok = false;
8904 }
8905 if (!ok)
8906 {
8907 pedwarn (loc, 0, "declaration does not declare anything");
8908 return NULL_TREE;
8909 }
8910 if (flag_isoc99)
8911 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8912 "ISO C99 doesn%'t support unnamed structs/unions");
8913 else
8914 pedwarn_c99 (loc, opt: OPT_Wpedantic,
8915 "ISO C90 doesn%'t support unnamed structs/unions");
8916 }
8917
8918 value = grokdeclarator (declarator, declspecs, decl_context: FIELD, initialized: false,
8919 width: width ? &width : NULL, decl_attrs, expr, NULL,
8920 deprecated_state: DEPRECATED_NORMAL);
8921
8922 finish_decl (decl: value, init_loc: loc, NULL_TREE, NULL_TREE, NULL_TREE);
8923 DECL_INITIAL (value) = width;
8924 if (width)
8925 SET_DECL_C_BIT_FIELD (value);
8926
8927 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8928 {
8929 /* If we currently have a binding for this field, set the
8930 in_struct field in the binding, so that we warn about lookups
8931 which find it. */
8932 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8933 if (b != NULL)
8934 {
8935 /* If the in_struct field is not yet set, push it on a list
8936 to be cleared when this struct is finished. */
8937 if (!b->in_struct)
8938 {
8939 struct_parse_info->fields.safe_push (obj: b);
8940 b->in_struct = 1;
8941 }
8942 }
8943 }
8944
8945 return value;
8946}
8947
8948/* Subroutine of detect_field_duplicates: return whether X and Y,
8949 which are both fields in the same struct, have duplicate field
8950 names. */
8951
8952static bool
8953is_duplicate_field (tree x, tree y)
8954{
8955 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8956 return true;
8957
8958 /* When using -fplan9-extensions, an anonymous field whose name is a
8959 typedef can duplicate a field name. */
8960 if (flag_plan9_extensions
8961 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8962 {
8963 tree xt, xn, yt, yn;
8964
8965 xt = TREE_TYPE (x);
8966 if (DECL_NAME (x) != NULL_TREE)
8967 xn = DECL_NAME (x);
8968 else if (RECORD_OR_UNION_TYPE_P (xt)
8969 && TYPE_NAME (xt) != NULL_TREE
8970 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8971 xn = DECL_NAME (TYPE_NAME (xt));
8972 else
8973 xn = NULL_TREE;
8974
8975 yt = TREE_TYPE (y);
8976 if (DECL_NAME (y) != NULL_TREE)
8977 yn = DECL_NAME (y);
8978 else if (RECORD_OR_UNION_TYPE_P (yt)
8979 && TYPE_NAME (yt) != NULL_TREE
8980 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8981 yn = DECL_NAME (TYPE_NAME (yt));
8982 else
8983 yn = NULL_TREE;
8984
8985 if (xn != NULL_TREE && xn == yn)
8986 return true;
8987 }
8988
8989 return false;
8990}
8991
8992/* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8993 to HTAB, giving errors for any duplicates. */
8994
8995static void
8996detect_field_duplicates_hash (tree fieldlist,
8997 hash_table<nofree_ptr_hash <tree_node> > *htab)
8998{
8999 tree x, y;
9000 tree_node **slot;
9001
9002 for (x = fieldlist; x ; x = DECL_CHAIN (x))
9003 if ((y = DECL_NAME (x)) != NULL_TREE)
9004 {
9005 slot = htab->find_slot (value: y, insert: INSERT);
9006 if (*slot)
9007 {
9008 error ("duplicate member %q+D", x);
9009 DECL_NAME (x) = NULL_TREE;
9010 }
9011 *slot = y;
9012 }
9013 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9014 {
9015 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9016
9017 /* When using -fplan9-extensions, an anonymous field whose
9018 name is a typedef can duplicate a field name. */
9019 if (flag_plan9_extensions
9020 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9021 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9022 {
9023 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9024 slot = htab->find_slot (value: xn, insert: INSERT);
9025 if (*slot)
9026 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9027 *slot = xn;
9028 }
9029 }
9030}
9031
9032/* Generate an error for any duplicate field names in FIELDLIST. Munge
9033 the list such that this does not present a problem later. */
9034
9035static void
9036detect_field_duplicates (tree fieldlist)
9037{
9038 tree x, y;
9039 int timeout = 10;
9040
9041 /* If the struct is the list of instance variables of an Objective-C
9042 class, then we need to check all the instance variables of
9043 superclasses when checking for duplicates (since you can't have
9044 an instance variable in a subclass with the same name as an
9045 instance variable in a superclass). We pass on this job to the
9046 Objective-C compiler. objc_detect_field_duplicates() will return
9047 false if we are not checking the list of instance variables and
9048 the C frontend should proceed with the standard field duplicate
9049 checks. If we are checking the list of instance variables, the
9050 ObjC frontend will do the check, emit the errors if needed, and
9051 then return true. */
9052 if (c_dialect_objc ())
9053 if (objc_detect_field_duplicates (false))
9054 return;
9055
9056 /* First, see if there are more than "a few" fields.
9057 This is trivially true if there are zero or one fields. */
9058 if (!fieldlist || !DECL_CHAIN (fieldlist))
9059 return;
9060 x = fieldlist;
9061 do {
9062 timeout--;
9063 if (DECL_NAME (x) == NULL_TREE
9064 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9065 timeout = 0;
9066 x = DECL_CHAIN (x);
9067 } while (timeout > 0 && x);
9068
9069 /* If there were "few" fields and no anonymous structures or unions,
9070 avoid the overhead of allocating a hash table. Instead just do
9071 the nested traversal thing. */
9072 if (timeout > 0)
9073 {
9074 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9075 /* When using -fplan9-extensions, we can have duplicates
9076 between typedef names and fields. */
9077 if (DECL_NAME (x)
9078 || (flag_plan9_extensions
9079 && DECL_NAME (x) == NULL_TREE
9080 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9081 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9082 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9083 {
9084 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9085 if (is_duplicate_field (x: y, y: x))
9086 {
9087 error ("duplicate member %q+D", x);
9088 DECL_NAME (x) = NULL_TREE;
9089 }
9090 }
9091 }
9092 else
9093 {
9094 hash_table<nofree_ptr_hash <tree_node> > htab (37);
9095 detect_field_duplicates_hash (fieldlist, htab: &htab);
9096 }
9097}
9098
9099/* Finish up struct info used by -Wc++-compat. */
9100
9101static void
9102warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9103 location_t record_loc)
9104{
9105 unsigned int ix;
9106 tree x;
9107 struct c_binding *b;
9108
9109 if (fieldlist == NULL_TREE)
9110 {
9111 if (code == RECORD_TYPE)
9112 warning_at (record_loc, OPT_Wc___compat,
9113 "empty struct has size 0 in C, size 1 in C++");
9114 else
9115 warning_at (record_loc, OPT_Wc___compat,
9116 "empty union has size 0 in C, size 1 in C++");
9117 }
9118
9119 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9120 the current struct. We do this now at the end of the struct
9121 because the flag is used to issue visibility warnings, and we
9122 only want to issue those warnings if the type is referenced
9123 outside of the struct declaration. */
9124 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9125 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9126
9127 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9128 typedefs used when declaring fields in this struct. If the name
9129 of any of the fields is also a typedef name then the struct would
9130 not parse in C++, because the C++ lookup rules say that the
9131 typedef name would be looked up in the context of the struct, and
9132 would thus be the field rather than the typedef. */
9133 if (!struct_parse_info->typedefs_seen.is_empty ()
9134 && fieldlist != NULL_TREE)
9135 {
9136 /* Use a hash_set<tree> using the name of the typedef. We can use
9137 a hash_set<tree> because identifiers are interned. */
9138 hash_set<tree> tset;
9139
9140 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9141 tset.add (DECL_NAME (x));
9142
9143 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9144 {
9145 if (DECL_NAME (x) != NULL_TREE
9146 && tset.contains (DECL_NAME (x)))
9147 {
9148 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9149 ("using %qD as both field and typedef name is "
9150 "invalid in C++"),
9151 x);
9152 /* FIXME: It would be nice to report the location where
9153 the typedef name is used. */
9154 }
9155 }
9156 }
9157
9158 /* For each field which has a binding and which was not defined in
9159 an enclosing struct, clear the in_struct field. */
9160 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9161 b->in_struct = 0;
9162}
9163
9164/* Function to help qsort sort FIELD_DECLs by name order. */
9165
9166static int
9167field_decl_cmp (const void *x_p, const void *y_p)
9168{
9169 const tree *const x = (const tree *) x_p;
9170 const tree *const y = (const tree *) y_p;
9171
9172 if (DECL_NAME (*x) == DECL_NAME (*y))
9173 /* A nontype is "greater" than a type. */
9174 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9175 if (DECL_NAME (*x) == NULL_TREE)
9176 return -1;
9177 if (DECL_NAME (*y) == NULL_TREE)
9178 return 1;
9179 if (DECL_NAME (*x) < DECL_NAME (*y))
9180 return -1;
9181 return 1;
9182}
9183
9184/* If this structure or union completes the type of any previous
9185 variable declaration, lay it out and output its rtl. */
9186static void
9187finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9188{
9189 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9190 {
9191 tree decl = TREE_VALUE (x);
9192 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9193 layout_array_type (TREE_TYPE (decl));
9194 if (TREE_CODE (decl) != TYPE_DECL)
9195 {
9196 relayout_decl (decl);
9197 if (c_dialect_objc ())
9198 objc_check_decl (decl);
9199 rest_of_decl_compilation (decl, toplevel, 0);
9200 }
9201 }
9202}
9203
9204/* Determine whether the FIELD_DECL X is a flexible array member according to
9205 the following info:
9206 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9207 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9208 or "[1]";
9209 C. flag_strict_flex_arrays;
9210 D. the attribute strict_flex_array that is attached to the field
9211 if presenting.
9212 Return TRUE when it's a flexible array member, FALSE otherwise. */
9213
9214static bool
9215is_flexible_array_member_p (bool is_last_field,
9216 tree x)
9217{
9218 /* If not the last field, return false. */
9219 if (!is_last_field)
9220 return false;
9221
9222 /* If not an array field, return false. */
9223 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9224 return false;
9225
9226 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9227 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9228 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9229
9230 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9231
9232 switch (strict_flex_array_level)
9233 {
9234 case 0:
9235 /* Default, all trailing arrays are flexible array members. */
9236 return true;
9237 case 1:
9238 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9239 if (is_one_element_array)
9240 return true;
9241 /* FALLTHROUGH. */
9242 case 2:
9243 /* Level 2: all "[0]", and "[]" are flexible array members. */
9244 if (is_zero_length_array)
9245 return true;
9246 /* FALLTHROUGH. */
9247 case 3:
9248 /* Level 3: Only "[]" are flexible array members. */
9249 if (is_flexible_array)
9250 return true;
9251 break;
9252 default:
9253 gcc_unreachable ();
9254 }
9255 return false;
9256}
9257
9258
9259/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9260 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9261 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9262 ATTRIBUTES are attributes to be applied to the structure.
9263
9264 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9265 the struct was started. */
9266
9267tree
9268finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9269 class c_struct_parse_info *enclosing_struct_parse_info)
9270{
9271 tree x;
9272 bool toplevel = file_scope == current_scope;
9273
9274 /* If this type was previously laid out as a forward reference,
9275 make sure we lay it out again. */
9276
9277 TYPE_SIZE (t) = NULL_TREE;
9278
9279 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9280
9281 if (pedantic)
9282 {
9283 for (x = fieldlist; x; x = DECL_CHAIN (x))
9284 {
9285 if (DECL_NAME (x) != NULL_TREE)
9286 break;
9287 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9288 break;
9289 }
9290
9291 if (x == NULL_TREE)
9292 {
9293 if (TREE_CODE (t) == UNION_TYPE)
9294 {
9295 if (fieldlist)
9296 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9297 else
9298 pedwarn (loc, OPT_Wpedantic, "union has no members");
9299 }
9300 else
9301 {
9302 if (fieldlist)
9303 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9304 else
9305 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9306 }
9307 }
9308 }
9309
9310 /* Install struct as DECL_CONTEXT of each field decl.
9311 Also process specified field sizes, found in the DECL_INITIAL,
9312 storing 0 there after the type has been changed to precision equal
9313 to its width, rather than the precision of the specified standard
9314 type. (Correct layout requires the original type to have been preserved
9315 until now.) */
9316
9317 bool saw_named_field = false;
9318 for (x = fieldlist; x; x = DECL_CHAIN (x))
9319 {
9320 /* Whether this field is the last field of the structure or union.
9321 for UNION, any field is the last field of it. */
9322 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9323 || (TREE_CODE (t) == UNION_TYPE);
9324
9325 if (TREE_TYPE (x) == error_mark_node)
9326 continue;
9327
9328 DECL_CONTEXT (x) = t;
9329
9330 tree t1 = strip_array_types (TREE_TYPE (x));
9331 /* If any field is const, the structure type is pseudo-const. */
9332 if (TREE_READONLY (x))
9333 C_TYPE_FIELDS_READONLY (t) = 1;
9334 else
9335 {
9336 /* A field that is pseudo-const makes the structure likewise. */
9337 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9338 C_TYPE_FIELDS_READONLY (t) = 1;
9339 }
9340
9341 /* Any field that is volatile means variables of this type must be
9342 treated in some ways as volatile. */
9343 if (TREE_THIS_VOLATILE (x))
9344 {
9345 C_TYPE_FIELDS_VOLATILE (t) = 1;
9346 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9347 }
9348
9349 /* Any field that is volatile, restrict-qualified or atomic
9350 means the type cannot be used for a constexpr object. */
9351 if (TYPE_QUALS (t1)
9352 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9353 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9354 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9355 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9356
9357 /* Any field of nominal variable size implies structure is too. */
9358 if (C_DECL_VARIABLE_SIZE (x))
9359 C_TYPE_VARIABLE_SIZE (t) = 1;
9360
9361 /* If any field is variably modified, record this fact. */
9362 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9363 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9364
9365 if (DECL_C_BIT_FIELD (x))
9366 {
9367 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9368 DECL_SIZE (x) = bitsize_int (width);
9369 DECL_BIT_FIELD (x) = 1;
9370 }
9371
9372 if (TYPE_PACKED (t)
9373 && (DECL_BIT_FIELD (x)
9374 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9375 DECL_PACKED (x) = 1;
9376
9377 /* Detect flexible array member in an invalid context. */
9378 if (flexible_array_member_type_p (TREE_TYPE (x)))
9379 {
9380 if (TREE_CODE (t) == UNION_TYPE)
9381 {
9382 error_at (DECL_SOURCE_LOCATION (x),
9383 "flexible array member in union");
9384 TREE_TYPE (x) = error_mark_node;
9385 }
9386 else if (!is_last_field)
9387 {
9388 error_at (DECL_SOURCE_LOCATION (x),
9389 "flexible array member not at end of struct");
9390 TREE_TYPE (x) = error_mark_node;
9391 }
9392 else if (!saw_named_field)
9393 {
9394 error_at (DECL_SOURCE_LOCATION (x),
9395 "flexible array member in a struct with no named "
9396 "members");
9397 TREE_TYPE (x) = error_mark_node;
9398 }
9399 }
9400
9401 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9402 && flexible_array_type_p (TREE_TYPE (x)))
9403 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9404 "invalid use of structure with flexible array member");
9405
9406 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9407 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9408
9409 /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9410 when x is an array and is the last field. */
9411 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9412 TYPE_INCLUDES_FLEXARRAY (t)
9413 = is_last_field && flexible_array_member_type_p (TREE_TYPE (x));
9414 /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9415 when x is an union or record and is the last field. */
9416 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9417 TYPE_INCLUDES_FLEXARRAY (t)
9418 = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9419
9420 if (warn_flex_array_member_not_at_end
9421 && !is_last_field
9422 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9423 && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9424 warning_at (DECL_SOURCE_LOCATION (x),
9425 OPT_Wflex_array_member_not_at_end,
9426 "structure containing a flexible array member"
9427 " is not at the end of another structure");
9428
9429 if (DECL_NAME (x)
9430 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9431 saw_named_field = true;
9432 }
9433
9434 detect_field_duplicates (fieldlist);
9435
9436 /* Now we have the nearly final fieldlist. Record it,
9437 then lay out the structure or union (including the fields). */
9438
9439 TYPE_FIELDS (t) = fieldlist;
9440
9441 maybe_apply_pragma_scalar_storage_order (t);
9442
9443 layout_type (t);
9444
9445 if (TYPE_SIZE_UNIT (t)
9446 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9447 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9448 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9449 error ("type %qT is too large", t);
9450
9451 /* Give bit-fields their proper types and rewrite the type of array fields
9452 with scalar component if the enclosing type has reverse storage order. */
9453 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9454 {
9455 if (TREE_CODE (field) == FIELD_DECL
9456 && DECL_INITIAL (field)
9457 && TREE_TYPE (field) != error_mark_node)
9458 {
9459 unsigned HOST_WIDE_INT width
9460 = tree_to_uhwi (DECL_INITIAL (field));
9461 tree type = TREE_TYPE (field);
9462 if (width != TYPE_PRECISION (type))
9463 {
9464 if (TREE_CODE (type) == BITINT_TYPE
9465 && (width > 1 || TYPE_UNSIGNED (type)))
9466 TREE_TYPE (field)
9467 = build_bitint_type (width, TYPE_UNSIGNED (type));
9468 else
9469 TREE_TYPE (field)
9470 = c_build_bitfield_integer_type (width,
9471 TYPE_UNSIGNED (type));
9472 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9473 }
9474 DECL_INITIAL (field) = NULL_TREE;
9475 }
9476 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9477 && TREE_CODE (field) == FIELD_DECL
9478 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9479 {
9480 tree ftype = TREE_TYPE (field);
9481 tree ctype = strip_array_types (type: ftype);
9482 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9483 {
9484 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9485 tree *typep = &fmain_type;
9486 do {
9487 *typep = build_distinct_type_copy (*typep);
9488 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9489 typep = &TREE_TYPE (*typep);
9490 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9491 TREE_TYPE (field)
9492 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9493 }
9494 }
9495
9496 /* Warn on problematic type punning for storage order purposes. */
9497 if (TREE_CODE (t) == UNION_TYPE
9498 && TREE_CODE (field) == FIELD_DECL
9499 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9500 {
9501 tree ftype = TREE_TYPE (field);
9502 if (TREE_CODE (ftype) == ARRAY_TYPE)
9503 ftype = strip_array_types (type: ftype);
9504 if (RECORD_OR_UNION_TYPE_P (ftype)
9505 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9506 != TYPE_REVERSE_STORAGE_ORDER (t))
9507 warning_at (DECL_SOURCE_LOCATION (field),
9508 OPT_Wscalar_storage_order,
9509 "type punning toggles scalar storage order");
9510 }
9511 }
9512
9513 /* Now we have the truly final field list.
9514 Store it in this type and in the variants. */
9515
9516 TYPE_FIELDS (t) = fieldlist;
9517
9518 /* If there are lots of fields, sort so we can look through them fast.
9519 We arbitrarily consider 16 or more elts to be "a lot". */
9520
9521 {
9522 int len = 0;
9523
9524 for (x = fieldlist; x; x = DECL_CHAIN (x))
9525 {
9526 if (len > 15 || DECL_NAME (x) == NULL)
9527 break;
9528 len += 1;
9529 }
9530
9531 if (len > 15)
9532 {
9533 tree *field_array;
9534 struct lang_type *space;
9535 struct sorted_fields_type *space2;
9536
9537 len += list_length (x);
9538
9539 /* Use the same allocation policy here that make_node uses, to
9540 ensure that this lives as long as the rest of the struct decl.
9541 All decls in an inline function need to be saved. */
9542
9543 space = ggc_cleared_alloc<struct lang_type> ();
9544 space2 = (sorted_fields_type *) ggc_internal_alloc
9545 (s: sizeof (struct sorted_fields_type) + len * sizeof (tree));
9546
9547 len = 0;
9548 space->s = space2;
9549 field_array = &space2->elts[0];
9550 for (x = fieldlist; x; x = DECL_CHAIN (x))
9551 {
9552 field_array[len++] = x;
9553
9554 /* If there is anonymous struct or union, break out of the loop. */
9555 if (DECL_NAME (x) == NULL)
9556 break;
9557 }
9558 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9559 if (x == NULL)
9560 {
9561 TYPE_LANG_SPECIFIC (t) = space;
9562 TYPE_LANG_SPECIFIC (t)->s->len = len;
9563 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9564 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9565 }
9566 }
9567 }
9568
9569 /* If this was supposed to be a transparent union, but we can't
9570 make it one, warn and turn off the flag. */
9571 if (TREE_CODE (t) == UNION_TYPE
9572 && TYPE_TRANSPARENT_AGGR (t)
9573 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9574 {
9575 TYPE_TRANSPARENT_AGGR (t) = 0;
9576 warning_at (loc, 0, "union cannot be made transparent");
9577 }
9578
9579 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9580 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9581 {
9582 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9583 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9584 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9585 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9586 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9587 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9588 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9589 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9590 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9591 }
9592
9593 /* Update type location to the one of the definition, instead of e.g.
9594 a forward declaration. */
9595 if (TYPE_STUB_DECL (t))
9596 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9597
9598 /* Finish debugging output for this type. */
9599 rest_of_type_compilation (t, toplevel);
9600
9601 finish_incomplete_vars (incomplete_vars, toplevel);
9602
9603 if (warn_cxx_compat)
9604 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), record_loc: loc);
9605
9606 delete struct_parse_info;
9607
9608 struct_parse_info = enclosing_struct_parse_info;
9609
9610 /* If this struct is defined inside a struct, add it to
9611 struct_types. */
9612 if (warn_cxx_compat
9613 && struct_parse_info != NULL
9614 && !in_sizeof && !in_typeof && !in_alignof)
9615 struct_parse_info->struct_types.safe_push (obj: t);
9616
9617 return t;
9618}
9619
9620static struct {
9621 gt_pointer_operator new_value;
9622 void *cookie;
9623} resort_data;
9624
9625/* This routine compares two fields like field_decl_cmp but using the
9626pointer operator in resort_data. */
9627
9628static int
9629resort_field_decl_cmp (const void *x_p, const void *y_p)
9630{
9631 const tree *const x = (const tree *) x_p;
9632 const tree *const y = (const tree *) y_p;
9633
9634 if (DECL_NAME (*x) == DECL_NAME (*y))
9635 /* A nontype is "greater" than a type. */
9636 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9637 if (DECL_NAME (*x) == NULL_TREE)
9638 return -1;
9639 if (DECL_NAME (*y) == NULL_TREE)
9640 return 1;
9641 {
9642 tree d1 = DECL_NAME (*x);
9643 tree d2 = DECL_NAME (*y);
9644 resort_data.new_value (&d1, &d1, resort_data.cookie);
9645 resort_data.new_value (&d2, &d2, resort_data.cookie);
9646 if (d1 < d2)
9647 return -1;
9648 }
9649 return 1;
9650}
9651
9652/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9653
9654void
9655resort_sorted_fields (void *obj,
9656 void * ARG_UNUSED (orig_obj),
9657 gt_pointer_operator new_value,
9658 void *cookie)
9659{
9660 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9661 resort_data.new_value = new_value;
9662 resort_data.cookie = cookie;
9663 qsort (&sf->elts[0], sf->len, sizeof (tree),
9664 resort_field_decl_cmp);
9665}
9666
9667/* Lay out the type T, and its element type, and so on. */
9668
9669static void
9670layout_array_type (tree t)
9671{
9672 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9673 layout_array_type (TREE_TYPE (t));
9674 layout_type (t);
9675}
9676
9677/* Begin compiling the definition of an enumeration type.
9678 NAME is its name (or null if anonymous).
9679 LOC is the enum's location.
9680 FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
9681 definition.
9682 Returns the type object, as yet incomplete.
9683 Also records info about it so that build_enumerator
9684 may be used to declare the individual values as they are read. */
9685
9686tree
9687start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9688 tree fixed_underlying_type)
9689{
9690 tree enumtype = NULL_TREE;
9691 location_t enumloc = UNKNOWN_LOCATION;
9692
9693 /* If this is the real definition for a previous forward reference,
9694 fill in the contents in the same object that used to be the
9695 forward reference. */
9696
9697 if (name != NULL_TREE)
9698 enumtype = lookup_tag (code: ENUMERAL_TYPE, name, thislevel_only: true, ploc: &enumloc);
9699
9700 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9701 {
9702 enumtype = make_node (ENUMERAL_TYPE);
9703 pushtag (loc, name, type: enumtype);
9704 if (fixed_underlying_type != NULL_TREE)
9705 {
9706 /* For an enum definition with a fixed underlying type, the
9707 type is complete during the definition and the
9708 enumeration constants have that type. If there was a
9709 tag, the type was completed in c_parser_enum_specifier.
9710 If not, it must be completed here. */
9711 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9712 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9713 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9714 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9715 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9716 TYPE_SIZE (enumtype) = NULL_TREE;
9717 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9718 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9719 layout_type (enumtype);
9720 }
9721 }
9722 /* Update type location to the one of the definition, instead of e.g.
9723 a forward declaration. */
9724 else if (TYPE_STUB_DECL (enumtype))
9725 {
9726 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9727 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9728 }
9729
9730 if (C_TYPE_BEING_DEFINED (enumtype))
9731 error_at (loc, "nested redefinition of %<enum %E%>", name);
9732
9733 C_TYPE_BEING_DEFINED (enumtype) = 1;
9734
9735 if (TYPE_VALUES (enumtype) != NULL_TREE)
9736 {
9737 /* This enum is a named one that has been declared already. */
9738 auto_diagnostic_group d;
9739 error_at (loc, "redeclaration of %<enum %E%>", name);
9740 if (enumloc != UNKNOWN_LOCATION)
9741 inform (enumloc, "originally defined here");
9742
9743 /* Completely replace its old definition.
9744 The old enumerators remain defined, however. */
9745 TYPE_VALUES (enumtype) = NULL_TREE;
9746 }
9747
9748 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9749 && fixed_underlying_type == NULL_TREE)
9750 error_at (loc, "%<enum%> declared with but defined without "
9751 "fixed underlying type");
9752
9753 the_enum->enum_next_value = integer_zero_node;
9754 the_enum->enum_type = enumtype;
9755 the_enum->enum_overflow = 0;
9756
9757 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9758 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9759 TYPE_PACKED (v) = 1;
9760
9761 /* FIXME: This will issue a warning for a use of a type defined
9762 within sizeof in a statement expr. This is not terribly serious
9763 as C++ doesn't permit statement exprs within sizeof anyhow. */
9764 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9765 warning_at (loc, OPT_Wc___compat,
9766 "defining type in %qs expression is invalid in C++",
9767 (in_sizeof
9768 ? "sizeof"
9769 : (in_typeof ? "typeof" : "alignof")));
9770
9771 if (in_underspecified_init)
9772 error_at (loc, "%qT defined in underspecified object initializer",
9773 enumtype);
9774
9775 return enumtype;
9776}
9777
9778/* After processing and defining all the values of an enumeration type,
9779 install their decls in the enumeration type and finish it off.
9780 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9781 and ATTRIBUTES are the specified attributes.
9782 Returns ENUMTYPE. */
9783
9784tree
9785finish_enum (tree enumtype, tree values, tree attributes)
9786{
9787 tree pair, tem;
9788 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9789 int precision;
9790 signop sign;
9791 bool toplevel = (file_scope == current_scope);
9792 struct lang_type *lt;
9793
9794 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9795
9796 /* Calculate the maximum value of any enumerator in this type. */
9797
9798 if (values == error_mark_node)
9799 minnode = maxnode = integer_zero_node;
9800 else
9801 {
9802 minnode = maxnode = TREE_VALUE (values);
9803 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9804 {
9805 tree value = TREE_VALUE (pair);
9806 if (tree_int_cst_lt (t1: maxnode, t2: value))
9807 maxnode = value;
9808 if (tree_int_cst_lt (t1: value, t2: minnode))
9809 minnode = value;
9810 }
9811 }
9812
9813 /* Construct the final type of this enumeration. It is the same
9814 as one of the integral types - the narrowest one that fits, except
9815 that normally we only go as narrow as int - and signed iff any of
9816 the values are negative. */
9817 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9818 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9819 tree_int_cst_min_precision (maxnode, sign));
9820
9821 bool wider_than_int =
9822 (tree_int_cst_lt (t1: minnode, TYPE_MIN_VALUE (integer_type_node))
9823 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), t2: maxnode));
9824
9825
9826 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9827 {
9828 /* If the precision of the type was specified with an attribute and it
9829 was too small, give an error. Otherwise, use it. */
9830 if (TYPE_PRECISION (enumtype) && lookup_attribute (attr_name: "mode", list: attributes))
9831 {
9832 if (precision > TYPE_PRECISION (enumtype))
9833 {
9834 TYPE_PRECISION (enumtype) = 0;
9835 error ("specified mode too small for enumerated values");
9836 }
9837 else
9838 precision = TYPE_PRECISION (enumtype);
9839 }
9840 else
9841 TYPE_PRECISION (enumtype) = 0;
9842
9843 if (TYPE_PACKED (enumtype)
9844 || precision > TYPE_PRECISION (integer_type_node)
9845 || TYPE_PRECISION (enumtype))
9846 {
9847 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9848 if (tem == NULL)
9849 {
9850 /* This should only occur when both signed and unsigned
9851 values of maximum precision occur among the
9852 enumerators. */
9853 pedwarn (input_location, 0,
9854 "enumeration values exceed range of largest integer");
9855 tem = widest_integer_literal_type_node;
9856 }
9857 else if (precision > TYPE_PRECISION (intmax_type_node)
9858 && !tree_int_cst_lt (t1: minnode,
9859 TYPE_MIN_VALUE (intmax_type_node))
9860 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
9861 t2: maxnode))
9862 pedwarn (input_location, OPT_Wpedantic,
9863 "enumeration values exceed range of %qs",
9864 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
9865 }
9866 else
9867 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9868
9869 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9870 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9871 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9872 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9873 TYPE_SIZE (enumtype) = NULL_TREE;
9874 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9875 ENUM_UNDERLYING_TYPE (enumtype) =
9876 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
9877
9878 layout_type (enumtype);
9879 }
9880
9881 if (values != error_mark_node)
9882 {
9883 /* Change the type of the enumerators to be the enum type. We
9884 need to do this irrespective of the size of the enum, for
9885 proper type checking. Replace the DECL_INITIALs of the
9886 enumerators, and the value slots of the list, with copies
9887 that have the enum type; they cannot be modified in place
9888 because they may be shared (e.g. integer_zero_node) Finally,
9889 change the purpose slots to point to the names of the decls. */
9890 for (pair = values; pair; pair = TREE_CHAIN (pair))
9891 {
9892 tree enu = TREE_PURPOSE (pair);
9893 tree ini = DECL_INITIAL (enu);
9894
9895 TREE_TYPE (enu) = enumtype;
9896
9897 /* Before C23, the ISO C Standard mandates enumerators to
9898 have type int, even though the underlying type of an enum
9899 type is unspecified. However, C23 allows enumerators of
9900 any integer type, and if an enumeration has any
9901 enumerators wider than int, all enumerators have the
9902 enumerated type after it is parsed. Any enumerators that
9903 fit in int are given type int in build_enumerator (which
9904 is the correct type while the enumeration is being
9905 parsed), so no conversions are needed here if all
9906 enumerators fit in int. If the enum has a fixed
9907 underlying type, the correct type was also given in
9908 build_enumerator. */
9909 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
9910 ini = convert (enumtype, ini);
9911
9912 DECL_INITIAL (enu) = ini;
9913 TREE_PURPOSE (pair) = DECL_NAME (enu);
9914 /* To match the C++ FE, store the CONST_DECL rather than just its
9915 value. */
9916 TREE_VALUE (pair) = enu;
9917 }
9918
9919 TYPE_VALUES (enumtype) = values;
9920 }
9921
9922 /* Record the min/max values so that we can warn about bit-field
9923 enumerations that are too small for the values. */
9924 lt = ggc_cleared_alloc<struct lang_type> ();
9925 lt->enum_min = minnode;
9926 lt->enum_max = maxnode;
9927 TYPE_LANG_SPECIFIC (enumtype) = lt;
9928
9929 /* Fix up all variant types of this enum type. */
9930 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9931 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9932 {
9933 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9934 if (tem == enumtype)
9935 continue;
9936 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9937 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9938 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9939 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9940 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9941 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9942 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9943 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9944 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9945 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9946 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9947 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
9948 }
9949
9950 /* Finish debugging output for this type. */
9951 rest_of_type_compilation (enumtype, toplevel);
9952
9953 finish_incomplete_vars (incomplete_vars, toplevel);
9954
9955 /* If this enum is defined inside a struct, add it to
9956 struct_types. */
9957 if (warn_cxx_compat
9958 && struct_parse_info != NULL
9959 && !in_sizeof && !in_typeof && !in_alignof)
9960 struct_parse_info->struct_types.safe_push (obj: enumtype);
9961
9962 C_TYPE_BEING_DEFINED (enumtype) = 0;
9963
9964 return enumtype;
9965}
9966
9967/* Build and install a CONST_DECL for one value of the
9968 current enumeration type (one that was begun with start_enum).
9969 DECL_LOC is the location of the enumerator.
9970 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9971 Return a tree-list containing the CONST_DECL and its value.
9972 Assignment of sequential values by default is handled here. */
9973
9974tree
9975build_enumerator (location_t decl_loc, location_t loc,
9976 struct c_enum_contents *the_enum, tree name, tree value)
9977{
9978 tree decl;
9979
9980 /* Validate and default VALUE. */
9981
9982 if (value != NULL_TREE)
9983 {
9984 /* Don't issue more errors for error_mark_node (i.e. an
9985 undeclared identifier) - just ignore the value expression. */
9986 if (value == error_mark_node)
9987 value = NULL_TREE;
9988 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9989 {
9990 error_at (loc, "enumerator value for %qE is not an integer constant",
9991 name);
9992 value = NULL_TREE;
9993 }
9994 else
9995 {
9996 if (TREE_CODE (value) != INTEGER_CST)
9997 {
9998 value = c_fully_fold (value, false, NULL);
9999 if (TREE_CODE (value) == INTEGER_CST)
10000 pedwarn (loc, OPT_Wpedantic,
10001 "enumerator value for %qE is not an integer "
10002 "constant expression", name);
10003 }
10004 if (TREE_CODE (value) != INTEGER_CST)
10005 {
10006 error ("enumerator value for %qE is not an integer constant",
10007 name);
10008 value = NULL_TREE;
10009 }
10010 else
10011 {
10012 value = default_conversion (value);
10013 constant_expression_warning (value);
10014 }
10015 }
10016 }
10017
10018 /* Default based on previous value. */
10019 /* It should no longer be possible to have NON_LVALUE_EXPR
10020 in the default. */
10021 if (value == NULL_TREE)
10022 {
10023 value = the_enum->enum_next_value;
10024 if (the_enum->enum_overflow)
10025 error_at (loc, "overflow in enumeration values");
10026 }
10027 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10028 {
10029 /* Enumeration constants must fit in the fixed underlying type. */
10030 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10031 error_at (loc,
10032 "enumerator value outside the range of underlying type");
10033 /* Enumeration constants for an enum with fixed underlying type
10034 have the enum type, both inside and outside the
10035 definition. */
10036 value = convert (the_enum->enum_type, value);
10037 }
10038 else
10039 {
10040 /* Even though the underlying type of an enum is unspecified, the
10041 type of enumeration constants is explicitly defined as int
10042 (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10043 GCC allows such types for older standards as an extension. */
10044 bool warned_range = false;
10045 if (!int_fits_type_p (value,
10046 (TYPE_UNSIGNED (TREE_TYPE (value))
10047 ? uintmax_type_node
10048 : intmax_type_node)))
10049 /* GCC does not consider its types larger than intmax_t to be
10050 extended integer types (although C23 would permit such types to
10051 be considered extended integer types if all the features
10052 required by <stdint.h> and <inttypes.h> macros, such as support
10053 for integer constants and I/O, were present), so diagnose if
10054 such a wider type is used. (If the wider type arose from a
10055 constant of such a type, that will also have been diagnosed,
10056 but this is the only diagnostic in the case where it arises
10057 from choosing a wider type automatically when adding 1
10058 overflows.) */
10059 warned_range = pedwarn (loc, OPT_Wpedantic,
10060 "enumerator value outside the range of %qs",
10061 (TYPE_UNSIGNED (TREE_TYPE (value))
10062 ? "uintmax_t"
10063 : "intmax_t"));
10064 if (!warned_range && !int_fits_type_p (value, integer_type_node))
10065 pedwarn_c11 (loc, opt: OPT_Wpedantic,
10066 "ISO C restricts enumerator values to range of %<int%> "
10067 "before C23");
10068
10069 /* The ISO C Standard mandates enumerators to have type int before
10070 C23, even though the underlying type of an enum type is
10071 unspecified. C23 allows enumerators of any integer type. During
10072 the parsing of the enumeration, C23 specifies that constants
10073 representable in int have type int, constants not representable
10074 in int have the type of the given expression if any, and
10075 constants not representable in int and derived by adding 1 to the
10076 previous constant have the type of that constant unless the
10077 addition would overflow or wraparound, in which case a wider type
10078 of the same signedness is chosen automatically; after the
10079 enumeration is parsed, all the constants have the type of the
10080 enumeration if any do not fit in int. */
10081 if (int_fits_type_p (value, integer_type_node))
10082 value = convert (integer_type_node, value);
10083 }
10084
10085 /* Set basis for default for next value. */
10086 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10087 {
10088 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10089 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10090 /* A value of 2 following a value of 1 overflows bool, but we
10091 cannot carry out addition directly on bool without
10092 promotion, and converting the result of arithmetic in a
10093 wider type back to bool would not produce the right result
10094 for this overflow check. */
10095 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10096 else
10097 the_enum->enum_next_value
10098 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10099 PLUS_EXPR, convert (underlying_type, value),
10100 convert (underlying_type, integer_one_node),
10101 false);
10102 }
10103 else
10104 the_enum->enum_next_value
10105 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10106 PLUS_EXPR, value, integer_one_node, false);
10107 the_enum->enum_overflow = tree_int_cst_lt (t1: the_enum->enum_next_value, t2: value);
10108 if (the_enum->enum_overflow
10109 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10110 {
10111 /* Choose a wider type with the same signedness if
10112 available. */
10113 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10114 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10115 tree new_type = (unsignedp
10116 ? long_unsigned_type_node
10117 : long_integer_type_node);
10118 if (prec > TYPE_PRECISION (new_type))
10119 new_type = (unsignedp
10120 ? long_long_unsigned_type_node
10121 : long_long_integer_type_node);
10122 if (prec > TYPE_PRECISION (new_type))
10123 new_type = (unsignedp
10124 ? widest_unsigned_literal_type_node
10125 : widest_integer_literal_type_node);
10126 if (prec <= TYPE_PRECISION (new_type))
10127 {
10128 the_enum->enum_overflow = false;
10129 the_enum->enum_next_value
10130 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10131 PLUS_EXPR, convert (new_type, value),
10132 integer_one_node, false);
10133 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10134 }
10135 }
10136
10137 /* Now create a declaration for the enum value name. */
10138
10139 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10140 DECL_INITIAL (decl) = value;
10141 pushdecl (x: decl);
10142
10143 return tree_cons (decl, value, NULL_TREE);
10144}
10145
10146/* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10147
10148tree
10149c_simulate_enum_decl (location_t loc, const char *name,
10150 vec<string_int_pair> *values_ptr)
10151{
10152 location_t saved_loc = input_location;
10153 input_location = loc;
10154
10155 struct c_enum_contents the_enum;
10156 tree enumtype = start_enum (loc, the_enum: &the_enum, get_identifier (name),
10157 NULL_TREE);
10158
10159 tree value_chain = NULL_TREE;
10160 string_int_pair *value;
10161 vec<string_int_pair> values = *values_ptr;
10162 unsigned int i;
10163 FOR_EACH_VEC_ELT (values, i, value)
10164 {
10165 tree decl = build_enumerator (decl_loc: loc, loc, the_enum: &the_enum,
10166 get_identifier (value->first),
10167 value: build_int_cst (integer_type_node,
10168 value->second));
10169 TREE_CHAIN (decl) = value_chain;
10170 value_chain = decl;
10171 }
10172
10173 finish_enum (enumtype, values: nreverse (value_chain), NULL_TREE);
10174
10175 input_location = saved_loc;
10176 return enumtype;
10177}
10178
10179/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10180
10181tree
10182c_simulate_record_decl (location_t loc, const char *name,
10183 array_slice<const tree> fields)
10184{
10185 location_t saved_loc = input_location;
10186 input_location = loc;
10187
10188 class c_struct_parse_info *struct_info;
10189 tree ident = get_identifier (name);
10190 tree type = start_struct (loc, code: RECORD_TYPE, name: ident, enclosing_struct_parse_info: &struct_info);
10191
10192 for (unsigned int i = 0; i < fields.size (); ++i)
10193 {
10194 DECL_FIELD_CONTEXT (fields[i]) = type;
10195 if (i > 0)
10196 DECL_CHAIN (fields[i - 1]) = fields[i];
10197 }
10198
10199 finish_struct (loc, t: type, fieldlist: fields[0], NULL_TREE, enclosing_struct_parse_info: struct_info);
10200
10201 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10202 set_underlying_type (decl);
10203 lang_hooks.decls.pushdecl (decl);
10204
10205 input_location = saved_loc;
10206 return type;
10207}
10208
10209/* Create the FUNCTION_DECL for a function definition.
10210 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10211 the declaration; they describe the function's name and the type it returns,
10212 but twisted together in a fashion that parallels the syntax of C.
10213
10214 This function creates a binding context for the function body
10215 as well as setting up the FUNCTION_DECL in current_function_decl.
10216
10217 Returns true on success. If the DECLARATOR is not suitable for a function
10218 (it defines a datum instead), we return false to report a parse error. */
10219
10220bool
10221start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10222 tree attributes)
10223{
10224 tree decl1, old_decl;
10225 tree restype, resdecl;
10226 location_t loc;
10227 location_t result_loc;
10228 tree expr = NULL;
10229
10230 current_function_returns_value = 0; /* Assume, until we see it does. */
10231 current_function_returns_null = 0;
10232 current_function_returns_abnormally = 0;
10233 warn_about_return_type = 0;
10234 c_switch_stack = NULL;
10235
10236 /* Indicate no valid break/continue context. */
10237 in_statement = 0;
10238
10239 decl1 = grokdeclarator (declarator, declspecs, decl_context: FUNCDEF, initialized: true, NULL,
10240 decl_attrs: &attributes, expr: &expr, NULL, deprecated_state: DEPRECATED_NORMAL);
10241 invoke_plugin_callbacks (event: PLUGIN_START_PARSE_FUNCTION, gcc_data: decl1);
10242
10243 /* If the declarator is not suitable for a function definition,
10244 cause a syntax error. */
10245 if (decl1 == NULL_TREE
10246 || TREE_CODE (decl1) != FUNCTION_DECL)
10247 return false;
10248
10249 /* Nested functions may have variably modified (return) type.
10250 Make sure the size expression is evaluated at this point. */
10251 if (expr && !current_scope->parm_flag)
10252 add_stmt (fold_convert (void_type_node, expr));
10253
10254 loc = DECL_SOURCE_LOCATION (decl1);
10255
10256 /* A nested function is not global. */
10257 if (current_function_decl != NULL_TREE)
10258 TREE_PUBLIC (decl1) = 0;
10259
10260 c_decl_attributes (node: &decl1, attributes, flags: 0);
10261
10262 if (DECL_DECLARED_INLINE_P (decl1)
10263 && DECL_UNINLINABLE (decl1)
10264 && lookup_attribute (attr_name: "noinline", DECL_ATTRIBUTES (decl1)))
10265 warning_at (loc, OPT_Wattributes,
10266 "inline function %qD given attribute %qs",
10267 decl1, "noinline");
10268
10269 /* Handle gnu_inline attribute. */
10270 if (declspecs->inline_p
10271 && !flag_gnu89_inline
10272 && TREE_CODE (decl1) == FUNCTION_DECL
10273 && (lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (decl1))
10274 || current_function_decl))
10275 {
10276 if (declspecs->storage_class != csc_static)
10277 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10278 }
10279
10280 announce_function (decl1);
10281
10282 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10283 {
10284 error_at (loc, "return type is an incomplete type");
10285 /* Make it return void instead. */
10286 TREE_TYPE (decl1)
10287 = build_function_type (void_type_node,
10288 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10289 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10290 }
10291
10292 if (warn_about_return_type)
10293 warn_defaults_to (location: loc, opt: flag_isoc99 ? OPT_Wimplicit_int
10294 : (warn_return_type > 0 ? OPT_Wreturn_type
10295 : OPT_Wimplicit_int),
10296 gmsgid: "return type defaults to %<int%>");
10297
10298 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10299 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10300 DECL_INITIAL (decl1) = error_mark_node;
10301
10302 /* If this definition isn't a prototype and we had a prototype declaration
10303 before, copy the arg type info from that prototype. */
10304 old_decl = lookup_name_in_scope (DECL_NAME (decl1), scope: current_scope);
10305 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10306 old_decl = NULL_TREE;
10307
10308 current_function_prototype_locus = UNKNOWN_LOCATION;
10309 current_function_prototype_built_in = false;
10310 current_function_prototype_arg_types = NULL_TREE;
10311 tree newtype = TREE_TYPE (decl1);
10312 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10313 if (!prototype_p (newtype))
10314 {
10315 tree oldrt = TREE_TYPE (oldtype);
10316 tree newrt = TREE_TYPE (newtype);
10317 if (old_decl != NULL_TREE
10318 && TREE_CODE (oldtype) == FUNCTION_TYPE
10319 && comptypes (oldrt, newrt))
10320 {
10321 if (stdarg_p (oldtype))
10322 {
10323 auto_diagnostic_group d;
10324 warning_at (loc, 0, "%q+D defined as variadic function "
10325 "without prototype", decl1);
10326 locate_old_decl (decl: old_decl);
10327 }
10328 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10329 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10330 current_function_prototype_built_in
10331 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10332 current_function_prototype_arg_types
10333 = TYPE_ARG_TYPES (newtype);
10334 }
10335 if (TREE_PUBLIC (decl1))
10336 {
10337 /* If there is an external prototype declaration of this
10338 function, record its location but do not copy information
10339 to this decl. This may be an invisible declaration
10340 (built-in or in a scope which has finished) or simply
10341 have more refined argument types than any declaration
10342 found above. */
10343 struct c_binding *b;
10344 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10345 if (B_IN_SCOPE (b, external_scope))
10346 break;
10347 if (b)
10348 {
10349 tree ext_decl, ext_type;
10350 ext_decl = b->decl;
10351 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10352 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10353 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10354 TREE_TYPE (ext_type)))
10355 {
10356 current_function_prototype_locus
10357 = DECL_SOURCE_LOCATION (ext_decl);
10358 current_function_prototype_built_in
10359 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10360 current_function_prototype_arg_types
10361 = TYPE_ARG_TYPES (ext_type);
10362 }
10363 }
10364 }
10365 }
10366
10367 /* Optionally warn of old-fashioned def with no previous prototype. */
10368 if (warn_strict_prototypes
10369 && old_decl != error_mark_node
10370 && !prototype_p (TREE_TYPE (decl1))
10371 && C_DECL_ISNT_PROTOTYPE (old_decl))
10372 warning_at (loc, OPT_Wstrict_prototypes,
10373 "function declaration isn%'t a prototype");
10374 /* Optionally warn of any global def with no previous prototype. */
10375 else if (warn_missing_prototypes
10376 && old_decl != error_mark_node
10377 && TREE_PUBLIC (decl1)
10378 && !MAIN_NAME_P (DECL_NAME (decl1))
10379 && C_DECL_ISNT_PROTOTYPE (old_decl)
10380 && !DECL_DECLARED_INLINE_P (decl1))
10381 warning_at (loc, OPT_Wmissing_prototypes,
10382 "no previous prototype for %qD", decl1);
10383 /* Optionally warn of any def with no previous prototype
10384 if the function has already been used. */
10385 else if (warn_missing_prototypes
10386 && old_decl != NULL_TREE
10387 && old_decl != error_mark_node
10388 && TREE_USED (old_decl)
10389 && !prototype_p (TREE_TYPE (old_decl)))
10390 warning_at (loc, OPT_Wmissing_prototypes,
10391 "%qD was used with no prototype before its definition", decl1);
10392 /* Optionally warn of any global def with no previous declaration. */
10393 else if (warn_missing_declarations
10394 && TREE_PUBLIC (decl1)
10395 && old_decl == NULL_TREE
10396 && !MAIN_NAME_P (DECL_NAME (decl1))
10397 && !DECL_DECLARED_INLINE_P (decl1))
10398 warning_at (loc, OPT_Wmissing_declarations,
10399 "no previous declaration for %qD",
10400 decl1);
10401 /* Optionally warn of any def with no previous declaration
10402 if the function has already been used. */
10403 else if (warn_missing_declarations
10404 && old_decl != NULL_TREE
10405 && old_decl != error_mark_node
10406 && TREE_USED (old_decl)
10407 && C_DECL_IMPLICIT (old_decl))
10408 warning_at (loc, OPT_Wmissing_declarations,
10409 "%qD was used with no declaration before its definition", decl1);
10410
10411 /* This function exists in static storage.
10412 (This does not mean `static' in the C sense!) */
10413 TREE_STATIC (decl1) = 1;
10414
10415 /* This is the earliest point at which we might know the assembler
10416 name of the function. Thus, if it's set before this, die horribly. */
10417 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10418
10419 /* If #pragma weak was used, mark the decl weak now. */
10420 if (current_scope == file_scope)
10421 maybe_apply_pragma_weak (decl1);
10422
10423 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10424 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10425 {
10426 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10427 != integer_type_node)
10428 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10429 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10430 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10431 decl1);
10432
10433 check_main_parameter_types (decl: decl1);
10434
10435 if (!TREE_PUBLIC (decl1))
10436 pedwarn (loc, OPT_Wmain,
10437 "%qD is normally a non-static function", decl1);
10438 }
10439
10440 tree parms = current_function_arg_info->parms;
10441 if (old_decl)
10442 {
10443 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10444 warn_parm_array_mismatch (origloc, old_decl, parms);
10445 }
10446
10447 /* Record the decl so that the function name is defined.
10448 If we already have a decl for this name, and it is a FUNCTION_DECL,
10449 use the old decl. */
10450
10451 current_function_decl = pushdecl (x: decl1);
10452
10453 if (tree access = build_attr_access_from_parms (parms, false))
10454 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10455 old_decl);
10456
10457 push_scope ();
10458 declare_parm_level ();
10459
10460 /* Set the result decl source location to the location of the typespec. */
10461 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10462 ? loc : declspecs->locations[cdw_typespec]);
10463 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10464 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10465 DECL_ARTIFICIAL (resdecl) = 1;
10466 DECL_IGNORED_P (resdecl) = 1;
10467 DECL_RESULT (current_function_decl) = resdecl;
10468
10469 start_fname_decls ();
10470
10471 return true;
10472}
10473
10474/* Subroutine of store_parm_decls which handles new-style function
10475 definitions (prototype format). The parms already have decls, so we
10476 need only record them as in effect and complain if any redundant
10477 old-style parm decls were written. */
10478static void
10479store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10480{
10481 tree decl;
10482 c_arg_tag *tag;
10483 unsigned ix;
10484
10485 if (current_scope->bindings)
10486 {
10487 error_at (DECL_SOURCE_LOCATION (fndecl),
10488 "old-style parameter declarations in prototyped "
10489 "function definition");
10490
10491 /* Get rid of the old-style declarations. */
10492 pop_scope ();
10493 push_scope ();
10494 }
10495 /* Don't issue this warning for nested functions, and don't issue this
10496 warning if we got here because ARG_INFO_TYPES was error_mark_node
10497 (this happens when a function definition has just an ellipsis in
10498 its parameter list). */
10499 else if (!in_system_header_at (loc: input_location)
10500 && !current_function_scope
10501 && arg_info->types != error_mark_node)
10502 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10503 "traditional C rejects ISO C style function definitions");
10504
10505 /* Now make all the parameter declarations visible in the function body.
10506 We can bypass most of the grunt work of pushdecl. */
10507 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10508 {
10509 DECL_CONTEXT (decl) = current_function_decl;
10510 if (DECL_NAME (decl))
10511 {
10512 bind (DECL_NAME (decl), decl, scope: current_scope,
10513 /*invisible=*/false, /*nested=*/false,
10514 UNKNOWN_LOCATION);
10515 if (!TREE_USED (decl))
10516 warn_if_shadowing (new_decl: decl);
10517 }
10518 else
10519 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), opt: OPT_Wpedantic,
10520 "ISO C does not support omitting parameter names in "
10521 "function definitions before C23");
10522 }
10523
10524 /* Record the parameter list in the function declaration. */
10525 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10526
10527 /* Now make all the ancillary declarations visible, likewise. */
10528 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10529 {
10530 DECL_CONTEXT (decl) = current_function_decl;
10531 if (DECL_NAME (decl))
10532 bind (DECL_NAME (decl), decl, scope: current_scope,
10533 /*invisible=*/false,
10534 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10535 UNKNOWN_LOCATION);
10536 }
10537
10538 /* And all the tag declarations. */
10539 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10540 if (tag->id)
10541 bind (name: tag->id, decl: tag->type, scope: current_scope,
10542 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10543}
10544
10545/* Subroutine of store_parm_decls which handles old-style function
10546 definitions (separate parameter list and declarations). */
10547
10548static void
10549store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10550{
10551 struct c_binding *b;
10552 tree parm, decl, last;
10553 tree parmids = arg_info->parms;
10554 hash_set<tree> seen_args;
10555
10556 if (!in_system_header_at (loc: input_location))
10557 {
10558 if (flag_isoc23)
10559 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10560 OPT_Wold_style_definition, "old-style function definition");
10561 else
10562 warning_at (DECL_SOURCE_LOCATION (fndecl),
10563 OPT_Wold_style_definition,
10564 "old-style function definition");
10565 }
10566
10567 if (current_scope->had_vla_unspec)
10568 error ("%<[*]%> not allowed in other than function prototype scope");
10569
10570 /* Match each formal parameter name with its declaration. Save each
10571 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10572 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10573 {
10574 if (TREE_VALUE (parm) == NULL_TREE)
10575 {
10576 error_at (DECL_SOURCE_LOCATION (fndecl),
10577 "parameter name missing from parameter list");
10578 TREE_PURPOSE (parm) = NULL_TREE;
10579 continue;
10580 }
10581
10582 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10583 if (b && B_IN_CURRENT_SCOPE (b))
10584 {
10585 decl = b->decl;
10586 /* Skip erroneous parameters. */
10587 if (decl == error_mark_node)
10588 continue;
10589 /* If we got something other than a PARM_DECL it is an error. */
10590 if (TREE_CODE (decl) != PARM_DECL)
10591 {
10592 error_at (DECL_SOURCE_LOCATION (decl),
10593 "%qD declared as a non-parameter", decl);
10594 continue;
10595 }
10596 /* If the declaration is already marked, we have a duplicate
10597 name. Complain and ignore the duplicate. */
10598 else if (seen_args.contains (k: decl))
10599 {
10600 error_at (DECL_SOURCE_LOCATION (decl),
10601 "multiple parameters named %qD", decl);
10602 TREE_PURPOSE (parm) = NULL_TREE;
10603 continue;
10604 }
10605 /* If the declaration says "void", complain and turn it into
10606 an int. */
10607 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10608 {
10609 error_at (DECL_SOURCE_LOCATION (decl),
10610 "parameter %qD declared with void type", decl);
10611 TREE_TYPE (decl) = integer_type_node;
10612 DECL_ARG_TYPE (decl) = integer_type_node;
10613 layout_decl (decl, 0);
10614 }
10615 warn_if_shadowing (new_decl: decl);
10616 }
10617 /* If no declaration found, default to int. */
10618 else
10619 {
10620 /* FIXME diagnostics: This should be the location of the argument,
10621 not the FNDECL. E.g., for an old-style declaration
10622
10623 int f10(v) { blah; }
10624
10625 We should use the location of the V, not the F10.
10626 Unfortunately, the V is an IDENTIFIER_NODE which has no
10627 location. In the future we need locations for c_arg_info
10628 entries.
10629
10630 See gcc.dg/Wshadow-3.c for an example of this problem. */
10631 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10632 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10633 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10634 pushdecl (x: decl);
10635 warn_if_shadowing (new_decl: decl);
10636
10637 if (flag_isoc99)
10638 pedwarn (DECL_SOURCE_LOCATION (decl),
10639 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10640 decl);
10641 else
10642 warning_at (DECL_SOURCE_LOCATION (decl),
10643 OPT_Wmissing_parameter_type,
10644 "type of %qD defaults to %<int%>", decl);
10645 }
10646
10647 TREE_PURPOSE (parm) = decl;
10648 seen_args.add (k: decl);
10649 }
10650
10651 /* Now examine the parms chain for incomplete declarations
10652 and declarations with no corresponding names. */
10653
10654 for (b = current_scope->bindings; b; b = b->prev)
10655 {
10656 parm = b->decl;
10657 if (TREE_CODE (parm) != PARM_DECL)
10658 continue;
10659
10660 if (TREE_TYPE (parm) != error_mark_node
10661 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10662 {
10663 error_at (DECL_SOURCE_LOCATION (parm),
10664 "parameter %qD has incomplete type", parm);
10665 TREE_TYPE (parm) = error_mark_node;
10666 }
10667
10668 if (!seen_args.contains (k: parm))
10669 {
10670 error_at (DECL_SOURCE_LOCATION (parm),
10671 "declaration for parameter %qD but no such parameter",
10672 parm);
10673
10674 /* Pretend the parameter was not missing.
10675 This gets us to a standard state and minimizes
10676 further error messages. */
10677 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10678 }
10679 }
10680
10681 /* Chain the declarations together in the order of the list of
10682 names. Store that chain in the function decl, replacing the
10683 list of names. Update the current scope to match. */
10684 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10685
10686 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10687 if (TREE_PURPOSE (parm))
10688 break;
10689 if (parm && TREE_PURPOSE (parm))
10690 {
10691 last = TREE_PURPOSE (parm);
10692 DECL_ARGUMENTS (fndecl) = last;
10693
10694 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10695 if (TREE_PURPOSE (parm))
10696 {
10697 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10698 last = TREE_PURPOSE (parm);
10699 }
10700 DECL_CHAIN (last) = NULL_TREE;
10701 }
10702
10703 /* If there was a previous prototype,
10704 set the DECL_ARG_TYPE of each argument according to
10705 the type previously specified, and report any mismatches. */
10706
10707 if (current_function_prototype_arg_types)
10708 {
10709 tree type;
10710 for (parm = DECL_ARGUMENTS (fndecl),
10711 type = current_function_prototype_arg_types;
10712 parm || (type != NULL_TREE
10713 && TREE_VALUE (type) != error_mark_node
10714 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10715 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10716 {
10717 if (parm == NULL_TREE
10718 || type == NULL_TREE
10719 || (TREE_VALUE (type) != error_mark_node
10720 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10721 {
10722 if (current_function_prototype_built_in)
10723 warning_at (DECL_SOURCE_LOCATION (fndecl),
10724 0, "number of arguments doesn%'t match "
10725 "built-in prototype");
10726 else
10727 {
10728 /* FIXME diagnostics: This should be the location of
10729 FNDECL, but there is bug when a prototype is
10730 declared inside function context, but defined
10731 outside of it (e.g., gcc.dg/pr15698-2.c). In
10732 which case FNDECL gets the location of the
10733 prototype, not the definition. */
10734 error_at (input_location,
10735 "number of arguments doesn%'t match prototype");
10736
10737 error_at (current_function_prototype_locus,
10738 "prototype declaration");
10739 }
10740 break;
10741 }
10742 /* Type for passing arg must be consistent with that
10743 declared for the arg. ISO C says we take the unqualified
10744 type for parameters declared with qualified type. */
10745 if (TREE_TYPE (parm) != error_mark_node
10746 && TREE_VALUE (type) != error_mark_node
10747 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10748 != TYPE_ATOMIC (TREE_VALUE (type)))
10749 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10750 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10751 {
10752 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10753 == TYPE_ATOMIC (TREE_VALUE (type)))
10754 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10755 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10756 {
10757 /* Adjust argument to match prototype. E.g. a previous
10758 `int foo(float);' prototype causes
10759 `int foo(x) float x; {...}' to be treated like
10760 `int foo(float x) {...}'. This is particularly
10761 useful for argument types like uid_t. */
10762 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10763
10764 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10765 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10766 && (TYPE_PRECISION (TREE_TYPE (parm))
10767 < TYPE_PRECISION (integer_type_node)))
10768 DECL_ARG_TYPE (parm)
10769 = c_type_promotes_to (TREE_TYPE (parm));
10770
10771 /* ??? Is it possible to get here with a
10772 built-in prototype or will it always have
10773 been diagnosed as conflicting with an
10774 old-style definition and discarded? */
10775 if (current_function_prototype_built_in)
10776 warning_at (DECL_SOURCE_LOCATION (parm),
10777 OPT_Wpedantic, "promoted argument %qD "
10778 "doesn%'t match built-in prototype", parm);
10779 else
10780 {
10781 pedwarn (DECL_SOURCE_LOCATION (parm),
10782 OPT_Wpedantic, "promoted argument %qD "
10783 "doesn%'t match prototype", parm);
10784 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10785 "prototype declaration");
10786 }
10787 }
10788 else
10789 {
10790 if (current_function_prototype_built_in)
10791 warning_at (DECL_SOURCE_LOCATION (parm),
10792 0, "argument %qD doesn%'t match "
10793 "built-in prototype", parm);
10794 else
10795 {
10796 error_at (DECL_SOURCE_LOCATION (parm),
10797 "argument %qD doesn%'t match prototype", parm);
10798 error_at (current_function_prototype_locus,
10799 "prototype declaration");
10800 }
10801 }
10802 }
10803 }
10804 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10805 }
10806
10807 /* Otherwise, create a prototype that would match. */
10808
10809 else
10810 {
10811 tree actual = NULL_TREE, last = NULL_TREE, type;
10812
10813 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10814 {
10815 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10816 if (last)
10817 TREE_CHAIN (last) = type;
10818 else
10819 actual = type;
10820 last = type;
10821 }
10822 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10823 if (last)
10824 TREE_CHAIN (last) = type;
10825 else
10826 actual = type;
10827
10828 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10829 of the type of this function, but we need to avoid having this
10830 affect the types of other similarly-typed functions, so we must
10831 first force the generation of an identical (but separate) type
10832 node for the relevant function type. The new node we create
10833 will be a variant of the main variant of the original function
10834 type. */
10835
10836 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10837
10838 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10839 }
10840}
10841
10842/* Store parameter declarations passed in ARG_INFO into the current
10843 function declaration. */
10844
10845void
10846store_parm_decls_from (struct c_arg_info *arg_info)
10847{
10848 current_function_arg_info = arg_info;
10849 store_parm_decls ();
10850}
10851
10852/* Called by walk_tree to look for and update context-less labels
10853 or labels with context in the parent function. */
10854
10855static tree
10856set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10857{
10858 tree ctx = static_cast<tree>(data);
10859 if (TREE_CODE (*tp) == LABEL_EXPR
10860 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10861 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10862 {
10863 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10864 *walk_subtrees = 0;
10865 }
10866
10867 return NULL_TREE;
10868}
10869
10870/* Store the parameter declarations into the current function declaration.
10871 This is called after parsing the parameter declarations, before
10872 digesting the body of the function.
10873
10874 For an old-style definition, construct a prototype out of the old-style
10875 parameter declarations and inject it into the function's type. */
10876
10877void
10878store_parm_decls (void)
10879{
10880 tree fndecl = current_function_decl;
10881 bool proto;
10882
10883 /* The argument information block for FNDECL. */
10884 struct c_arg_info *arg_info = current_function_arg_info;
10885 current_function_arg_info = 0;
10886
10887 /* True if this definition is written with a prototype. In C23, an
10888 empty argument list was converted to (void) in grokparms; in
10889 older C standard versions, it does not give the function a type
10890 with a prototype for future calls. */
10891 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
10892
10893 if (proto)
10894 store_parm_decls_newstyle (fndecl, arg_info);
10895 else
10896 store_parm_decls_oldstyle (fndecl, arg_info);
10897
10898 /* The next call to push_scope will be a function body. */
10899
10900 next_is_function_body = true;
10901
10902 /* Write a record describing this function definition to the prototypes
10903 file (if requested). */
10904
10905 gen_aux_info_record (fndecl, 1, 0, proto);
10906
10907 /* Initialize the RTL code for the function. */
10908 allocate_struct_function (fndecl, false);
10909
10910 if (warn_unused_local_typedefs)
10911 cfun->language = ggc_cleared_alloc<language_function> ();
10912
10913 /* Begin the statement tree for this function. */
10914 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10915
10916 /* ??? Insert the contents of the pending sizes list into the function
10917 to be evaluated. The only reason left to have this is
10918 void foo(int n, int array[n++])
10919 because we throw away the array type in favor of a pointer type, and
10920 thus won't naturally see the SAVE_EXPR containing the increment. All
10921 other pending sizes would be handled by gimplify_parameters. */
10922 if (arg_info->pending_sizes)
10923 {
10924 /* In very special circumstances, e.g. for code like
10925 _Atomic int i = 5;
10926 void f (int a[i += 2]) {}
10927 we need to execute the atomic assignment on function entry.
10928 But in this case, it is not just a straight store, it has the
10929 op= form, which means that build_atomic_assign has generated
10930 gotos, labels, etc. Because at that time the function decl
10931 for F has not been created yet, those labels do not have any
10932 function context. But we have the fndecl now, so update the
10933 labels accordingly. gimplify_expr would crash otherwise.
10934 Or with nested functions the labels could be created with parent
10935 function's context, while when the statement is emitted at the
10936 start of the nested function, it needs the nested function's
10937 context. */
10938 walk_tree_without_duplicates (&arg_info->pending_sizes,
10939 set_labels_context_r, fndecl);
10940 add_stmt (t: arg_info->pending_sizes);
10941 }
10942}
10943
10944/* Store PARM_DECLs in PARMS into scope temporarily. Used for
10945 c_finish_omp_declare_simd for function prototypes. No diagnostics
10946 should be done. */
10947
10948void
10949temp_store_parm_decls (tree fndecl, tree parms)
10950{
10951 push_scope ();
10952 for (tree p = parms; p; p = DECL_CHAIN (p))
10953 {
10954 DECL_CONTEXT (p) = fndecl;
10955 if (DECL_NAME (p))
10956 bind (DECL_NAME (p), decl: p, scope: current_scope,
10957 /*invisible=*/false, /*nested=*/false,
10958 UNKNOWN_LOCATION);
10959 }
10960}
10961
10962/* Undo what temp_store_parm_decls did. */
10963
10964void
10965temp_pop_parm_decls (void)
10966{
10967 /* Clear all bindings in this temporary scope, so that
10968 pop_scope doesn't create a BLOCK. */
10969 struct c_binding *b = current_scope->bindings;
10970 current_scope->bindings = NULL;
10971 for (; b; b = free_binding_and_advance (b))
10972 {
10973 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10974 || b->decl == error_mark_node);
10975 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10976 I_SYMBOL_BINDING (b->id) = b->shadowed;
10977 if (b->shadowed && b->shadowed->u.type)
10978 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10979 }
10980 pop_scope ();
10981}
10982
10983
10984/* Finish up a function declaration and compile that function
10985 all the way to assembler language output. Then free the storage
10986 for the function definition.
10987
10988 This is called after parsing the body of the function definition. */
10989
10990void
10991finish_function (location_t end_loc)
10992{
10993 tree fndecl = current_function_decl;
10994
10995 if (c_dialect_objc ())
10996 objc_finish_function ();
10997
10998 if (TREE_CODE (fndecl) == FUNCTION_DECL
10999 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
11000 {
11001 tree args = DECL_ARGUMENTS (fndecl);
11002 for (; args; args = DECL_CHAIN (args))
11003 {
11004 tree type = TREE_TYPE (args);
11005 if (INTEGRAL_TYPE_P (type)
11006 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11007 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
11008 }
11009 }
11010
11011 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11012 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11013
11014 /* Must mark the RESULT_DECL as being in this function. */
11015
11016 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11017 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11018
11019 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11020 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11021 == integer_type_node && flag_isoc99)
11022 {
11023 /* Hack. We don't want the middle-end to warn that this return
11024 is unreachable, so we mark its location as special. Using
11025 UNKNOWN_LOCATION has the problem that it gets clobbered in
11026 annotate_one_with_locus. A cleaner solution might be to
11027 ensure ! should_carry_locus_p (stmt), but that needs a flag.
11028 */
11029 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11030 }
11031
11032 /* Tie off the statement tree for this function. */
11033 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11034
11035 finish_fname_decls ();
11036
11037 /* Complain if there's no return statement only if option specified on
11038 command line. */
11039 if (warn_return_type > 0
11040 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11041 && !current_function_returns_value && !current_function_returns_null
11042 /* Don't complain if we are no-return. */
11043 && !current_function_returns_abnormally
11044 /* Don't complain if we are declared noreturn. */
11045 && !TREE_THIS_VOLATILE (fndecl)
11046 /* Don't warn for main(). */
11047 && !MAIN_NAME_P (DECL_NAME (fndecl))
11048 /* Or if they didn't actually specify a return type. */
11049 && !C_FUNCTION_IMPLICIT_INT (fndecl)
11050 /* Normally, with -Wreturn-type, flow will complain, but we might
11051 optimize out static functions. */
11052 && !TREE_PUBLIC (fndecl)
11053 && targetm.warn_func_return (fndecl)
11054 && warning (OPT_Wreturn_type,
11055 "no return statement in function returning non-void"))
11056 suppress_warning (fndecl, OPT_Wreturn_type);
11057
11058 /* Complain about parameters that are only set, but never otherwise used. */
11059 if (warn_unused_but_set_parameter)
11060 {
11061 tree decl;
11062
11063 for (decl = DECL_ARGUMENTS (fndecl);
11064 decl;
11065 decl = DECL_CHAIN (decl))
11066 if (TREE_USED (decl)
11067 && TREE_CODE (decl) == PARM_DECL
11068 && !DECL_READ_P (decl)
11069 && DECL_NAME (decl)
11070 && !DECL_ARTIFICIAL (decl)
11071 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
11072 warning_at (DECL_SOURCE_LOCATION (decl),
11073 OPT_Wunused_but_set_parameter,
11074 "parameter %qD set but not used", decl);
11075 }
11076
11077 /* Complain about locally defined typedefs that are not used in this
11078 function. */
11079 maybe_warn_unused_local_typedefs ();
11080
11081 /* Possibly warn about unused parameters. */
11082 if (warn_unused_parameter)
11083 do_warn_unused_parameter (fndecl);
11084
11085 /* Store the end of the function, so that we get good line number
11086 info for the epilogue. */
11087 cfun->function_end_locus = end_loc;
11088
11089 /* Finalize the ELF visibility for the function. */
11090 c_determine_visibility (fndecl);
11091
11092 /* For GNU C extern inline functions disregard inline limits. */
11093 if (DECL_EXTERNAL (fndecl)
11094 && DECL_DECLARED_INLINE_P (fndecl)
11095 && (flag_gnu89_inline
11096 || lookup_attribute (attr_name: "gnu_inline", DECL_ATTRIBUTES (fndecl))))
11097 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11098
11099 /* Genericize before inlining. Delay genericizing nested functions
11100 until their parent function is genericized. Since finalizing
11101 requires GENERIC, delay that as well. */
11102
11103 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11104 && !undef_nested_function)
11105 {
11106 if (!decl_function_context (fndecl))
11107 {
11108 invoke_plugin_callbacks (event: PLUGIN_PRE_GENERICIZE, gcc_data: fndecl);
11109 c_genericize (fndecl);
11110
11111 /* ??? Objc emits functions after finalizing the compilation unit.
11112 This should be cleaned up later and this conditional removed. */
11113 if (symtab->global_info_ready)
11114 {
11115 cgraph_node::add_new_function (fndecl, lowered: false);
11116 return;
11117 }
11118 cgraph_node::finalize_function (fndecl, false);
11119 }
11120 else
11121 {
11122 /* Register this function with cgraph just far enough to get it
11123 added to our parent's nested function list. Handy, since the
11124 C front end doesn't have such a list. */
11125 (void) cgraph_node::get_create (fndecl);
11126 }
11127 }
11128
11129 if (!decl_function_context (fndecl))
11130 undef_nested_function = false;
11131
11132 if (cfun->language != NULL)
11133 {
11134 ggc_free (cfun->language);
11135 cfun->language = NULL;
11136 }
11137
11138 /* We're leaving the context of this function, so zap cfun.
11139 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11140 tree_rest_of_compilation. */
11141 set_cfun (NULL);
11142 invoke_plugin_callbacks (event: PLUGIN_FINISH_PARSE_FUNCTION, gcc_data: current_function_decl);
11143 current_function_decl = NULL;
11144}
11145
11146/* Check the declarations given in a for-loop for satisfying the C99
11147 constraints. If exactly one such decl is found, return it. LOC is
11148 the location of the opening parenthesis of the for loop. The last
11149 parameter allows you to control the "for loop initial declarations
11150 are only allowed in C99 mode". Normally, you should pass
11151 flag_isoc99 as that parameter. But in some cases (Objective-C
11152 foreach loop, for example) we want to run the checks in this
11153 function even if not in C99 mode, so we allow the caller to turn
11154 off the error about not being in C99 mode.
11155*/
11156
11157tree
11158check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11159{
11160 struct c_binding *b;
11161 tree one_decl = NULL_TREE;
11162 int n_decls = 0;
11163
11164 if (!turn_off_iso_c99_error)
11165 {
11166 static bool hint = true;
11167 /* If we get here, declarations have been used in a for loop without
11168 the C99 for loop scope. This doesn't make much sense, so don't
11169 allow it. */
11170 auto_diagnostic_group d;
11171 error_at (loc, "%<for%> loop initial declarations "
11172 "are only allowed in C99 or C11 mode");
11173 if (hint)
11174 {
11175 inform (loc,
11176 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11177 "%<-std=gnu11%> to compile your code");
11178 hint = false;
11179 }
11180 return NULL_TREE;
11181 }
11182 else
11183 pedwarn_c90 (loc, opt: OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11184 "initial declarations");
11185
11186 /* C99 subclause 6.8.5 paragraph 3:
11187
11188 [#3] The declaration part of a for statement shall only
11189 declare identifiers for objects having storage class auto or
11190 register.
11191
11192 It isn't clear whether, in this sentence, "identifiers" binds to
11193 "shall only declare" or to "objects" - that is, whether all identifiers
11194 declared must be identifiers for objects, or whether the restriction
11195 only applies to those that are. (A question on this in comp.std.c
11196 in November 2000 received no answer.) We implement the strictest
11197 interpretation, to avoid creating an extension which later causes
11198 problems.
11199
11200 This constraint was removed in C23. */
11201
11202 for (b = current_scope->bindings; b; b = b->prev)
11203 {
11204 tree id = b->id;
11205 tree decl = b->decl;
11206
11207 if (!id)
11208 continue;
11209
11210 switch (TREE_CODE (decl))
11211 {
11212 case VAR_DECL:
11213 {
11214 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11215 if (TREE_STATIC (decl))
11216 pedwarn_c11 (decl_loc, opt: OPT_Wpedantic,
11217 "declaration of static variable %qD in %<for%> "
11218 "loop initial declaration", decl);
11219 else if (DECL_EXTERNAL (decl))
11220 pedwarn_c11 (decl_loc, opt: OPT_Wpedantic,
11221 "declaration of %<extern%> variable %qD in %<for%> "
11222 "loop initial declaration", decl);
11223 }
11224 break;
11225
11226 case RECORD_TYPE:
11227 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11228 "%<struct %E%> declared in %<for%> loop initial "
11229 "declaration", id);
11230 break;
11231 case UNION_TYPE:
11232 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11233 "%<union %E%> declared in %<for%> loop initial "
11234 "declaration",
11235 id);
11236 break;
11237 case ENUMERAL_TYPE:
11238 pedwarn_c11 (loc, opt: OPT_Wpedantic,
11239 "%<enum %E%> declared in %<for%> loop "
11240 "initial declaration", id);
11241 break;
11242 default:
11243 pedwarn_c11 (loc, opt: OPT_Wpedantic, "declaration of non-variable "
11244 "%qD in %<for%> loop initial declaration", decl);
11245 }
11246
11247 n_decls++;
11248 one_decl = decl;
11249 }
11250
11251 return n_decls == 1 ? one_decl : NULL_TREE;
11252}
11253
11254/* Save and reinitialize the variables
11255 used during compilation of a C function. */
11256
11257void
11258c_push_function_context (void)
11259{
11260 struct language_function *p = cfun->language;
11261 /* cfun->language might have been already allocated by the use of
11262 -Wunused-local-typedefs. In that case, just re-use it. */
11263 if (p == NULL)
11264 cfun->language = p = ggc_cleared_alloc<language_function> ();
11265
11266 p->base.x_stmt_tree = c_stmt_tree;
11267 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (src: c_stmt_tree.x_cur_stmt_list);
11268 p->x_in_statement = in_statement;
11269 p->x_switch_stack = c_switch_stack;
11270 p->arg_info = current_function_arg_info;
11271 p->returns_value = current_function_returns_value;
11272 p->returns_null = current_function_returns_null;
11273 p->returns_abnormally = current_function_returns_abnormally;
11274 p->warn_about_return_type = warn_about_return_type;
11275
11276 push_function_context ();
11277}
11278
11279/* Restore the variables used during compilation of a C function. */
11280
11281void
11282c_pop_function_context (void)
11283{
11284 struct language_function *p;
11285
11286 pop_function_context ();
11287 p = cfun->language;
11288
11289 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11290 used to store data throughout the life time of the current cfun,
11291 So don't deallocate it. */
11292 if (!warn_unused_local_typedefs)
11293 cfun->language = NULL;
11294
11295 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11296 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11297 {
11298 /* Stop pointing to the local nodes about to be freed. */
11299 /* But DECL_INITIAL must remain nonzero so we know this
11300 was an actual function definition. */
11301 DECL_INITIAL (current_function_decl) = error_mark_node;
11302 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11303 }
11304
11305 c_stmt_tree = p->base.x_stmt_tree;
11306 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11307 in_statement = p->x_in_statement;
11308 c_switch_stack = p->x_switch_stack;
11309 current_function_arg_info = p->arg_info;
11310 current_function_returns_value = p->returns_value;
11311 current_function_returns_null = p->returns_null;
11312 current_function_returns_abnormally = p->returns_abnormally;
11313 warn_about_return_type = p->warn_about_return_type;
11314}
11315
11316/* The functions below are required for functionality of doing
11317 function at once processing in the C front end. Currently these
11318 functions are not called from anywhere in the C front end, but as
11319 these changes continue, that will change. */
11320
11321/* Returns the stmt_tree (if any) to which statements are currently
11322 being added. If there is no active statement-tree, NULL is
11323 returned. */
11324
11325stmt_tree
11326current_stmt_tree (void)
11327{
11328 return &c_stmt_tree;
11329}
11330
11331/* Return the global value of T as a symbol. */
11332
11333tree
11334identifier_global_value (tree t)
11335{
11336 struct c_binding *b;
11337
11338 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11339 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11340 return b->decl;
11341
11342 return NULL_TREE;
11343}
11344
11345/* Return the global value of tag T as a symbol. */
11346
11347tree
11348identifier_global_tag (tree t)
11349{
11350 struct c_binding *b;
11351
11352 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11353 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11354 return b->decl;
11355
11356 return NULL_TREE;
11357}
11358
11359/* Returns true if NAME refers to a built-in function or function-like
11360 operator. */
11361
11362bool
11363names_builtin_p (const char *name)
11364{
11365 tree id = get_identifier (name);
11366 if (tree decl = identifier_global_value (t: id))
11367 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11368
11369 /* Also detect common reserved C words that aren't strictly built-in
11370 functions. */
11371 switch (C_RID_CODE (id))
11372 {
11373 case RID_BUILTIN_CONVERTVECTOR:
11374 case RID_BUILTIN_HAS_ATTRIBUTE:
11375 case RID_BUILTIN_SHUFFLE:
11376 case RID_BUILTIN_SHUFFLEVECTOR:
11377 case RID_BUILTIN_ASSOC_BARRIER:
11378 case RID_CHOOSE_EXPR:
11379 case RID_OFFSETOF:
11380 case RID_TYPES_COMPATIBLE_P:
11381 return true;
11382 default:
11383 break;
11384 }
11385
11386 return false;
11387}
11388
11389/* In C, the only C-linkage public declaration is at file scope. */
11390
11391tree
11392c_linkage_bindings (tree name)
11393{
11394 return identifier_global_value (t: name);
11395}
11396
11397/* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11398 otherwise the name is found in ridpointers from RID_INDEX. */
11399
11400void
11401record_builtin_type (enum rid rid_index, const char *name, tree type)
11402{
11403 tree id, decl;
11404 if (name == 0)
11405 id = ridpointers[(int) rid_index];
11406 else
11407 id = get_identifier (name);
11408 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11409 pushdecl (x: decl);
11410 if (debug_hooks->type_decl)
11411 debug_hooks->type_decl (decl, false);
11412}
11413
11414/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11415
11416struct c_parm *
11417build_c_parm (struct c_declspecs *specs, tree attrs,
11418 struct c_declarator *declarator,
11419 location_t loc)
11420{
11421 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11422 ret->specs = specs;
11423 ret->attrs = attrs;
11424 ret->declarator = declarator;
11425 ret->loc = loc;
11426 return ret;
11427}
11428
11429/* Return a declarator with nested attributes. TARGET is the inner
11430 declarator to which these attributes apply. ATTRS are the
11431 attributes. */
11432
11433struct c_declarator *
11434build_attrs_declarator (tree attrs, struct c_declarator *target)
11435{
11436 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11437 ret->kind = cdk_attrs;
11438 ret->declarator = target;
11439 ret->u.attrs = attrs;
11440 return ret;
11441}
11442
11443/* Return a declarator for a function with arguments specified by ARGS
11444 and return type specified by TARGET. */
11445
11446struct c_declarator *
11447build_function_declarator (struct c_arg_info *args,
11448 struct c_declarator *target)
11449{
11450 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11451 ret->kind = cdk_function;
11452 ret->declarator = target;
11453 ret->u.arg_info = args;
11454 return ret;
11455}
11456
11457/* Return a declarator for the identifier IDENT (which may be
11458 NULL_TREE for an abstract declarator). */
11459
11460struct c_declarator *
11461build_id_declarator (tree ident)
11462{
11463 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11464 ret->kind = cdk_id;
11465 ret->declarator = 0;
11466 ret->u.id.id = ident;
11467 ret->u.id.attrs = NULL_TREE;
11468 /* Default value - may get reset to a more precise location. */
11469 ret->id_loc = input_location;
11470 return ret;
11471}
11472
11473/* Return something to represent absolute declarators containing a *.
11474 TARGET is the absolute declarator that the * contains.
11475 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11476 to apply to the pointer type. */
11477
11478struct c_declarator *
11479make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11480 struct c_declarator *target)
11481{
11482 tree attrs;
11483 int quals = 0;
11484 struct c_declarator *itarget = target;
11485 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11486 if (type_quals_attrs)
11487 {
11488 attrs = type_quals_attrs->attrs;
11489 quals = quals_from_declspecs (specs: type_quals_attrs);
11490 if (attrs != NULL_TREE)
11491 itarget = build_attrs_declarator (attrs, target);
11492 }
11493 ret->kind = cdk_pointer;
11494 ret->declarator = itarget;
11495 ret->u.pointer_quals = quals;
11496 return ret;
11497}
11498
11499/* Return a pointer to a structure for an empty list of declaration
11500 specifiers. */
11501
11502struct c_declspecs *
11503build_null_declspecs (void)
11504{
11505 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11506 memset (s: ret, c: 0, n: sizeof *ret);
11507 ret->align_log = -1;
11508 ret->typespec_word = cts_none;
11509 ret->storage_class = csc_none;
11510 ret->expr_const_operands = true;
11511 ret->typespec_kind = ctsk_none;
11512 ret->address_space = ADDR_SPACE_GENERIC;
11513 return ret;
11514}
11515
11516/* Add the address space ADDRSPACE to the declaration specifiers
11517 SPECS, returning SPECS. */
11518
11519struct c_declspecs *
11520declspecs_add_addrspace (location_t location,
11521 struct c_declspecs *specs, addr_space_t as)
11522{
11523 specs->non_sc_seen_p = true;
11524 specs->declspecs_seen_p = true;
11525 specs->non_std_attrs_seen_p = true;
11526
11527 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11528 && specs->address_space != as)
11529 error ("incompatible address space qualifiers %qs and %qs",
11530 c_addr_space_name (as),
11531 c_addr_space_name (as: specs->address_space));
11532 else
11533 {
11534 specs->address_space = as;
11535 specs->locations[cdw_address_space] = location;
11536 }
11537 return specs;
11538}
11539
11540/* Add the type qualifier QUAL to the declaration specifiers SPECS,
11541 returning SPECS. */
11542
11543struct c_declspecs *
11544declspecs_add_qual (location_t loc,
11545 struct c_declspecs *specs, tree qual)
11546{
11547 enum rid i;
11548 bool dupe = false;
11549 specs->non_sc_seen_p = true;
11550 specs->declspecs_seen_p = true;
11551 specs->non_std_attrs_seen_p = true;
11552 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11553 && C_IS_RESERVED_WORD (qual));
11554 i = C_RID_CODE (qual);
11555 location_t prev_loc = UNKNOWN_LOCATION;
11556 switch (i)
11557 {
11558 case RID_CONST:
11559 dupe = specs->const_p;
11560 specs->const_p = true;
11561 prev_loc = specs->locations[cdw_const];
11562 specs->locations[cdw_const] = loc;
11563 break;
11564 case RID_VOLATILE:
11565 dupe = specs->volatile_p;
11566 specs->volatile_p = true;
11567 prev_loc = specs->locations[cdw_volatile];
11568 specs->locations[cdw_volatile] = loc;
11569 break;
11570 case RID_RESTRICT:
11571 dupe = specs->restrict_p;
11572 specs->restrict_p = true;
11573 prev_loc = specs->locations[cdw_restrict];
11574 specs->locations[cdw_restrict] = loc;
11575 break;
11576 case RID_ATOMIC:
11577 dupe = specs->atomic_p;
11578 specs->atomic_p = true;
11579 prev_loc = specs->locations[cdw_atomic];
11580 specs->locations[cdw_atomic] = loc;
11581 break;
11582 default:
11583 gcc_unreachable ();
11584 }
11585 if (dupe)
11586 {
11587 bool warned = pedwarn_c90 (loc, opt: OPT_Wpedantic,
11588 "duplicate %qE declaration specifier", qual);
11589 if (!warned
11590 && warn_duplicate_decl_specifier
11591 && prev_loc >= RESERVED_LOCATION_COUNT
11592 && !from_macro_expansion_at (loc: prev_loc)
11593 && !from_macro_expansion_at (loc))
11594 warning_at (loc, OPT_Wduplicate_decl_specifier,
11595 "duplicate %qE declaration specifier", qual);
11596 }
11597 return specs;
11598}
11599
11600/* Add the type specifier TYPE to the declaration specifiers SPECS,
11601 returning SPECS. */
11602
11603struct c_declspecs *
11604declspecs_add_type (location_t loc, struct c_declspecs *specs,
11605 struct c_typespec spec)
11606{
11607 tree type = spec.spec;
11608 specs->non_sc_seen_p = true;
11609 specs->declspecs_seen_p = true;
11610 specs->non_std_attrs_seen_p = true;
11611 specs->typespec_kind = spec.kind;
11612 if (TREE_DEPRECATED (type))
11613 specs->deprecated_p = true;
11614 if (TREE_UNAVAILABLE (type))
11615 specs->unavailable_p = true;
11616
11617 /* As a type specifier is present, "auto" must be used as a storage
11618 class specifier, not for type deduction. */
11619 if (specs->c23_auto_p)
11620 {
11621 specs->c23_auto_p = false;
11622 if (specs->storage_class != csc_none)
11623 error ("multiple storage classes in declaration specifiers");
11624 else if (specs->thread_p)
11625 error ("%qs used with %<auto%>",
11626 specs->thread_gnu_p ? "__thread" : "_Thread_local");
11627 else if (specs->constexpr_p)
11628 /* auto may only be used with another storage class specifier,
11629 such as constexpr, if the type is inferred. */
11630 error ("%<auto%> used with %<constexpr%>");
11631 else
11632 specs->storage_class = csc_auto;
11633 }
11634
11635 /* Handle type specifier keywords. */
11636 if (TREE_CODE (type) == IDENTIFIER_NODE
11637 && C_IS_RESERVED_WORD (type)
11638 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11639 {
11640 enum rid i = C_RID_CODE (type);
11641 if (specs->type)
11642 {
11643 error_at (loc, "two or more data types in declaration specifiers");
11644 return specs;
11645 }
11646 if ((int) i <= (int) RID_LAST_MODIFIER)
11647 {
11648 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11649 bool dupe = false;
11650 switch (i)
11651 {
11652 case RID_LONG:
11653 if (specs->long_long_p)
11654 {
11655 error_at (loc, "%<long long long%> is too long for GCC");
11656 break;
11657 }
11658 if (specs->long_p)
11659 {
11660 if (specs->typespec_word == cts_double)
11661 {
11662 error_at (loc,
11663 ("both %<long long%> and %<double%> in "
11664 "declaration specifiers"));
11665 break;
11666 }
11667 pedwarn_c90 (loc, opt: OPT_Wlong_long,
11668 "ISO C90 does not support %<long long%>");
11669 specs->long_long_p = 1;
11670 specs->locations[cdw_long_long] = loc;
11671 break;
11672 }
11673 if (specs->short_p)
11674 error_at (loc,
11675 ("both %<long%> and %<short%> in "
11676 "declaration specifiers"));
11677 else if (specs->typespec_word == cts_auto_type)
11678 error_at (loc,
11679 ("both %<long%> and %<__auto_type%> in "
11680 "declaration specifiers"));
11681 else if (specs->typespec_word == cts_void)
11682 error_at (loc,
11683 ("both %<long%> and %<void%> in "
11684 "declaration specifiers"));
11685 else if (specs->typespec_word == cts_int_n)
11686 error_at (loc,
11687 ("both %<long%> and %<__int%d%> in "
11688 "declaration specifiers"),
11689 int_n_data[specs->u.int_n_idx].bitsize);
11690 else if (specs->typespec_word == cts_bool)
11691 error_at (loc,
11692 ("both %<long%> and %<_Bool%> in "
11693 "declaration specifiers"));
11694 else if (specs->typespec_word == cts_bitint)
11695 error_at (loc,
11696 ("both %<long%> and %<_BitInt%> in "
11697 "declaration specifiers"));
11698 else if (specs->typespec_word == cts_char)
11699 error_at (loc,
11700 ("both %<long%> and %<char%> in "
11701 "declaration specifiers"));
11702 else if (specs->typespec_word == cts_float)
11703 error_at (loc,
11704 ("both %<long%> and %<float%> in "
11705 "declaration specifiers"));
11706 else if (specs->typespec_word == cts_floatn_nx)
11707 error_at (loc,
11708 ("both %<long%> and %<_Float%d%s%> in "
11709 "declaration specifiers"),
11710 floatn_nx_types[specs->u.floatn_nx_idx].n,
11711 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11712 ? "x"
11713 : ""));
11714 else if (specs->typespec_word == cts_dfloat32)
11715 error_at (loc,
11716 ("both %<long%> and %<_Decimal32%> in "
11717 "declaration specifiers"));
11718 else if (specs->typespec_word == cts_dfloat64)
11719 error_at (loc,
11720 ("both %<long%> and %<_Decimal64%> in "
11721 "declaration specifiers"));
11722 else if (specs->typespec_word == cts_dfloat128)
11723 error_at (loc,
11724 ("both %<long%> and %<_Decimal128%> in "
11725 "declaration specifiers"));
11726 else
11727 {
11728 specs->long_p = true;
11729 specs->locations[cdw_long] = loc;
11730 }
11731 break;
11732 case RID_SHORT:
11733 dupe = specs->short_p;
11734 if (specs->long_p)
11735 error_at (loc,
11736 ("both %<long%> and %<short%> in "
11737 "declaration specifiers"));
11738 else if (specs->typespec_word == cts_auto_type)
11739 error_at (loc,
11740 ("both %<short%> and %<__auto_type%> in "
11741 "declaration specifiers"));
11742 else if (specs->typespec_word == cts_void)
11743 error_at (loc,
11744 ("both %<short%> and %<void%> in "
11745 "declaration specifiers"));
11746 else if (specs->typespec_word == cts_int_n)
11747 error_at (loc,
11748 ("both %<short%> and %<__int%d%> in "
11749 "declaration specifiers"),
11750 int_n_data[specs->u.int_n_idx].bitsize);
11751 else if (specs->typespec_word == cts_bool)
11752 error_at (loc,
11753 ("both %<short%> and %<_Bool%> in "
11754 "declaration specifiers"));
11755 else if (specs->typespec_word == cts_bitint)
11756 error_at (loc,
11757 ("both %<short%> and %<_BitInt%> in "
11758 "declaration specifiers"));
11759 else if (specs->typespec_word == cts_char)
11760 error_at (loc,
11761 ("both %<short%> and %<char%> in "
11762 "declaration specifiers"));
11763 else if (specs->typespec_word == cts_float)
11764 error_at (loc,
11765 ("both %<short%> and %<float%> in "
11766 "declaration specifiers"));
11767 else if (specs->typespec_word == cts_double)
11768 error_at (loc,
11769 ("both %<short%> and %<double%> in "
11770 "declaration specifiers"));
11771 else if (specs->typespec_word == cts_floatn_nx)
11772 error_at (loc,
11773 ("both %<short%> and %<_Float%d%s%> in "
11774 "declaration specifiers"),
11775 floatn_nx_types[specs->u.floatn_nx_idx].n,
11776 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11777 ? "x"
11778 : ""));
11779 else if (specs->typespec_word == cts_dfloat32)
11780 error_at (loc,
11781 ("both %<short%> and %<_Decimal32%> in "
11782 "declaration specifiers"));
11783 else if (specs->typespec_word == cts_dfloat64)
11784 error_at (loc,
11785 ("both %<short%> and %<_Decimal64%> in "
11786 "declaration specifiers"));
11787 else if (specs->typespec_word == cts_dfloat128)
11788 error_at (loc,
11789 ("both %<short%> and %<_Decimal128%> in "
11790 "declaration specifiers"));
11791 else
11792 {
11793 specs->short_p = true;
11794 specs->locations[cdw_short] = loc;
11795 }
11796 break;
11797 case RID_SIGNED:
11798 dupe = specs->signed_p;
11799 if (specs->unsigned_p)
11800 error_at (loc,
11801 ("both %<signed%> and %<unsigned%> in "
11802 "declaration specifiers"));
11803 else if (specs->typespec_word == cts_auto_type)
11804 error_at (loc,
11805 ("both %<signed%> and %<__auto_type%> in "
11806 "declaration specifiers"));
11807 else if (specs->typespec_word == cts_void)
11808 error_at (loc,
11809 ("both %<signed%> and %<void%> in "
11810 "declaration specifiers"));
11811 else if (specs->typespec_word == cts_bool)
11812 error_at (loc,
11813 ("both %<signed%> and %<_Bool%> in "
11814 "declaration specifiers"));
11815 else if (specs->typespec_word == cts_float)
11816 error_at (loc,
11817 ("both %<signed%> and %<float%> in "
11818 "declaration specifiers"));
11819 else if (specs->typespec_word == cts_double)
11820 error_at (loc,
11821 ("both %<signed%> and %<double%> in "
11822 "declaration specifiers"));
11823 else if (specs->typespec_word == cts_floatn_nx)
11824 error_at (loc,
11825 ("both %<signed%> and %<_Float%d%s%> in "
11826 "declaration specifiers"),
11827 floatn_nx_types[specs->u.floatn_nx_idx].n,
11828 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11829 ? "x"
11830 : ""));
11831 else if (specs->typespec_word == cts_dfloat32)
11832 error_at (loc,
11833 ("both %<signed%> and %<_Decimal32%> in "
11834 "declaration specifiers"));
11835 else if (specs->typespec_word == cts_dfloat64)
11836 error_at (loc,
11837 ("both %<signed%> and %<_Decimal64%> in "
11838 "declaration specifiers"));
11839 else if (specs->typespec_word == cts_dfloat128)
11840 error_at (loc,
11841 ("both %<signed%> and %<_Decimal128%> in "
11842 "declaration specifiers"));
11843 else
11844 {
11845 specs->signed_p = true;
11846 specs->locations[cdw_signed] = loc;
11847 }
11848 break;
11849 case RID_UNSIGNED:
11850 dupe = specs->unsigned_p;
11851 if (specs->signed_p)
11852 error_at (loc,
11853 ("both %<signed%> and %<unsigned%> in "
11854 "declaration specifiers"));
11855 else if (specs->typespec_word == cts_auto_type)
11856 error_at (loc,
11857 ("both %<unsigned%> and %<__auto_type%> in "
11858 "declaration specifiers"));
11859 else if (specs->typespec_word == cts_void)
11860 error_at (loc,
11861 ("both %<unsigned%> and %<void%> in "
11862 "declaration specifiers"));
11863 else if (specs->typespec_word == cts_bool)
11864 error_at (loc,
11865 ("both %<unsigned%> and %<_Bool%> in "
11866 "declaration specifiers"));
11867 else if (specs->typespec_word == cts_float)
11868 error_at (loc,
11869 ("both %<unsigned%> and %<float%> in "
11870 "declaration specifiers"));
11871 else if (specs->typespec_word == cts_double)
11872 error_at (loc,
11873 ("both %<unsigned%> and %<double%> in "
11874 "declaration specifiers"));
11875 else if (specs->typespec_word == cts_floatn_nx)
11876 error_at (loc,
11877 ("both %<unsigned%> and %<_Float%d%s%> in "
11878 "declaration specifiers"),
11879 floatn_nx_types[specs->u.floatn_nx_idx].n,
11880 (floatn_nx_types[specs->u.floatn_nx_idx].extended
11881 ? "x"
11882 : ""));
11883 else if (specs->typespec_word == cts_dfloat32)
11884 error_at (loc,
11885 ("both %<unsigned%> and %<_Decimal32%> in "
11886 "declaration specifiers"));
11887 else if (specs->typespec_word == cts_dfloat64)
11888 error_at (loc,
11889 ("both %<unsigned%> and %<_Decimal64%> in "
11890 "declaration specifiers"));
11891 else if (specs->typespec_word == cts_dfloat128)
11892 error_at (loc,
11893 ("both %<unsigned%> and %<_Decimal128%> in "
11894 "declaration specifiers"));
11895 else
11896 {
11897 specs->unsigned_p = true;
11898 specs->locations[cdw_unsigned] = loc;
11899 }
11900 break;
11901 case RID_COMPLEX:
11902 dupe = specs->complex_p;
11903 if (!in_system_header_at (loc))
11904 pedwarn_c90 (loc, opt: OPT_Wpedantic,
11905 "ISO C90 does not support complex types");
11906 if (specs->typespec_word == cts_auto_type)
11907 error_at (loc,
11908 ("both %<complex%> and %<__auto_type%> in "
11909 "declaration specifiers"));
11910 else if (specs->typespec_word == cts_void)
11911 error_at (loc,
11912 ("both %<complex%> and %<void%> in "
11913 "declaration specifiers"));
11914 else if (specs->typespec_word == cts_bool)
11915 error_at (loc,
11916 ("both %<complex%> and %<_Bool%> in "
11917 "declaration specifiers"));
11918 else if (specs->typespec_word == cts_bitint)
11919 error_at (loc,
11920 ("both %<complex%> and %<_BitInt%> in "
11921 "declaration specifiers"));
11922 else if (specs->typespec_word == cts_dfloat32)
11923 error_at (loc,
11924 ("both %<complex%> and %<_Decimal32%> in "
11925 "declaration specifiers"));
11926 else if (specs->typespec_word == cts_dfloat64)
11927 error_at (loc,
11928 ("both %<complex%> and %<_Decimal64%> in "
11929 "declaration specifiers"));
11930 else if (specs->typespec_word == cts_dfloat128)
11931 error_at (loc,
11932 ("both %<complex%> and %<_Decimal128%> in "
11933 "declaration specifiers"));
11934 else if (specs->typespec_word == cts_fract)
11935 error_at (loc,
11936 ("both %<complex%> and %<_Fract%> in "
11937 "declaration specifiers"));
11938 else if (specs->typespec_word == cts_accum)
11939 error_at (loc,
11940 ("both %<complex%> and %<_Accum%> in "
11941 "declaration specifiers"));
11942 else if (specs->saturating_p)
11943 error_at (loc,
11944 ("both %<complex%> and %<_Sat%> in "
11945 "declaration specifiers"));
11946 else
11947 {
11948 specs->complex_p = true;
11949 specs->locations[cdw_complex] = loc;
11950 }
11951 break;
11952 case RID_SAT:
11953 dupe = specs->saturating_p;
11954 pedwarn (loc, OPT_Wpedantic,
11955 "ISO C does not support saturating types");
11956 if (specs->typespec_word == cts_int_n)
11957 {
11958 error_at (loc,
11959 ("both %<_Sat%> and %<__int%d%> in "
11960 "declaration specifiers"),
11961 int_n_data[specs->u.int_n_idx].bitsize);
11962 }
11963 else if (specs->typespec_word == cts_auto_type)
11964 error_at (loc,
11965 ("both %<_Sat%> and %<__auto_type%> in "
11966 "declaration specifiers"));
11967 else if (specs->typespec_word == cts_void)
11968 error_at (loc,
11969 ("both %<_Sat%> and %<void%> in "
11970 "declaration specifiers"));
11971 else if (specs->typespec_word == cts_bool)
11972 error_at (loc,
11973 ("both %<_Sat%> and %<_Bool%> in "
11974 "declaration specifiers"));
11975 else if (specs->typespec_word == cts_bitint)
11976 error_at (loc,
11977 ("both %<_Sat%> and %<_BitInt%> in "
11978 "declaration specifiers"));
11979 else if (specs->typespec_word == cts_char)
11980 error_at (loc,
11981 ("both %<_Sat%> and %<char%> in "
11982 "declaration specifiers"));
11983 else if (specs->typespec_word == cts_int)
11984 error_at (loc,
11985 ("both %<_Sat%> and %<int%> in "
11986 "declaration specifiers"));
11987 else if (specs->typespec_word == cts_float)
11988 error_at (loc,
11989 ("both %<_Sat%> and %<float%> in "
11990 "declaration specifiers"));
11991 else if (specs->typespec_word == cts_double)
11992 error_at (loc,
11993 ("both %<_Sat%> and %<double%> in "
11994 "declaration specifiers"));
11995 else if (specs->typespec_word == cts_floatn_nx)
11996 error_at (loc,
11997 ("both %<_Sat%> 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 %<_Sat%> and %<_Decimal32%> in "
12006 "declaration specifiers"));
12007 else if (specs->typespec_word == cts_dfloat64)
12008 error_at (loc,
12009 ("both %<_Sat%> and %<_Decimal64%> in "
12010 "declaration specifiers"));
12011 else if (specs->typespec_word == cts_dfloat128)
12012 error_at (loc,
12013 ("both %<_Sat%> and %<_Decimal128%> in "
12014 "declaration specifiers"));
12015 else if (specs->complex_p)
12016 error_at (loc,
12017 ("both %<_Sat%> and %<complex%> in "
12018 "declaration specifiers"));
12019 else
12020 {
12021 specs->saturating_p = true;
12022 specs->locations[cdw_saturating] = loc;
12023 }
12024 break;
12025 default:
12026 gcc_unreachable ();
12027 }
12028
12029 if (dupe)
12030 error_at (loc, "duplicate %qE", type);
12031
12032 return specs;
12033 }
12034 else
12035 {
12036 /* "void", "_Bool", "char", "int", "float", "double",
12037 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12038 "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12039 "__auto_type". */
12040 if (specs->typespec_word != cts_none)
12041 {
12042 error_at (loc,
12043 "two or more data types in declaration specifiers");
12044 return specs;
12045 }
12046 switch (i)
12047 {
12048 case RID_AUTO_TYPE:
12049 if (specs->long_p)
12050 error_at (loc,
12051 ("both %<long%> and %<__auto_type%> in "
12052 "declaration specifiers"));
12053 else if (specs->short_p)
12054 error_at (loc,
12055 ("both %<short%> and %<__auto_type%> in "
12056 "declaration specifiers"));
12057 else if (specs->signed_p)
12058 error_at (loc,
12059 ("both %<signed%> and %<__auto_type%> in "
12060 "declaration specifiers"));
12061 else if (specs->unsigned_p)
12062 error_at (loc,
12063 ("both %<unsigned%> and %<__auto_type%> in "
12064 "declaration specifiers"));
12065 else if (specs->complex_p)
12066 error_at (loc,
12067 ("both %<complex%> and %<__auto_type%> in "
12068 "declaration specifiers"));
12069 else if (specs->saturating_p)
12070 error_at (loc,
12071 ("both %<_Sat%> and %<__auto_type%> in "
12072 "declaration specifiers"));
12073 else
12074 {
12075 specs->typespec_word = cts_auto_type;
12076 specs->locations[cdw_typespec] = loc;
12077 }
12078 return specs;
12079 case RID_INT_N_0:
12080 case RID_INT_N_1:
12081 case RID_INT_N_2:
12082 case RID_INT_N_3:
12083 specs->u.int_n_idx = i - RID_INT_N_0;
12084 if (!in_system_header_at (loc: input_location)
12085 /* If the INT_N type ends in "__", and so is of the format
12086 "__intN__", don't pedwarn. */
12087 && (strncmp (IDENTIFIER_POINTER (type)
12088 + (IDENTIFIER_LENGTH (type) - 2), s2: "__", n: 2) != 0))
12089 pedwarn (loc, OPT_Wpedantic,
12090 "ISO C does not support %<__int%d%> types",
12091 int_n_data[specs->u.int_n_idx].bitsize);
12092
12093 if (specs->long_p)
12094 error_at (loc,
12095 ("both %<__int%d%> and %<long%> in "
12096 "declaration specifiers"),
12097 int_n_data[specs->u.int_n_idx].bitsize);
12098 else if (specs->saturating_p)
12099 error_at (loc,
12100 ("both %<_Sat%> and %<__int%d%> in "
12101 "declaration specifiers"),
12102 int_n_data[specs->u.int_n_idx].bitsize);
12103 else if (specs->short_p)
12104 error_at (loc,
12105 ("both %<__int%d%> and %<short%> in "
12106 "declaration specifiers"),
12107 int_n_data[specs->u.int_n_idx].bitsize);
12108 else if (! int_n_enabled_p[specs->u.int_n_idx])
12109 {
12110 specs->typespec_word = cts_int_n;
12111 error_at (loc,
12112 "%<__int%d%> is not supported on this target",
12113 int_n_data[specs->u.int_n_idx].bitsize);
12114 }
12115 else
12116 {
12117 specs->typespec_word = cts_int_n;
12118 specs->locations[cdw_typespec] = loc;
12119 }
12120 return specs;
12121 case RID_VOID:
12122 if (specs->long_p)
12123 error_at (loc,
12124 ("both %<long%> and %<void%> in "
12125 "declaration specifiers"));
12126 else if (specs->short_p)
12127 error_at (loc,
12128 ("both %<short%> and %<void%> in "
12129 "declaration specifiers"));
12130 else if (specs->signed_p)
12131 error_at (loc,
12132 ("both %<signed%> and %<void%> in "
12133 "declaration specifiers"));
12134 else if (specs->unsigned_p)
12135 error_at (loc,
12136 ("both %<unsigned%> and %<void%> in "
12137 "declaration specifiers"));
12138 else if (specs->complex_p)
12139 error_at (loc,
12140 ("both %<complex%> and %<void%> in "
12141 "declaration specifiers"));
12142 else if (specs->saturating_p)
12143 error_at (loc,
12144 ("both %<_Sat%> and %<void%> in "
12145 "declaration specifiers"));
12146 else
12147 {
12148 specs->typespec_word = cts_void;
12149 specs->locations[cdw_typespec] = loc;
12150 }
12151 return specs;
12152 case RID_BOOL:
12153 if (!in_system_header_at (loc))
12154 pedwarn_c90 (loc, opt: OPT_Wpedantic,
12155 "ISO C90 does not support boolean types");
12156 if (specs->long_p)
12157 error_at (loc,
12158 ("both %<long%> and %<_Bool%> in "
12159 "declaration specifiers"));
12160 else if (specs->short_p)
12161 error_at (loc,
12162 ("both %<short%> and %<_Bool%> in "
12163 "declaration specifiers"));
12164 else if (specs->signed_p)
12165 error_at (loc,
12166 ("both %<signed%> and %<_Bool%> in "
12167 "declaration specifiers"));
12168 else if (specs->unsigned_p)
12169 error_at (loc,
12170 ("both %<unsigned%> and %<_Bool%> in "
12171 "declaration specifiers"));
12172 else if (specs->complex_p)
12173 error_at (loc,
12174 ("both %<complex%> and %<_Bool%> in "
12175 "declaration specifiers"));
12176 else if (specs->saturating_p)
12177 error_at (loc,
12178 ("both %<_Sat%> and %<_Bool%> in "
12179 "declaration specifiers"));
12180 else
12181 {
12182 specs->typespec_word = cts_bool;
12183 specs->locations[cdw_typespec] = loc;
12184 }
12185 return specs;
12186 case RID_CHAR:
12187 if (specs->long_p)
12188 error_at (loc,
12189 ("both %<long%> and %<char%> in "
12190 "declaration specifiers"));
12191 else if (specs->short_p)
12192 error_at (loc,
12193 ("both %<short%> and %<char%> in "
12194 "declaration specifiers"));
12195 else if (specs->saturating_p)
12196 error_at (loc,
12197 ("both %<_Sat%> and %<char%> in "
12198 "declaration specifiers"));
12199 else
12200 {
12201 specs->typespec_word = cts_char;
12202 specs->locations[cdw_typespec] = loc;
12203 }
12204 return specs;
12205 case RID_INT:
12206 if (specs->saturating_p)
12207 error_at (loc,
12208 ("both %<_Sat%> and %<int%> in "
12209 "declaration specifiers"));
12210 else
12211 {
12212 specs->typespec_word = cts_int;
12213 specs->locations[cdw_typespec] = loc;
12214 }
12215 return specs;
12216 case RID_FLOAT:
12217 if (specs->long_p)
12218 error_at (loc,
12219 ("both %<long%> and %<float%> in "
12220 "declaration specifiers"));
12221 else if (specs->short_p)
12222 error_at (loc,
12223 ("both %<short%> and %<float%> in "
12224 "declaration specifiers"));
12225 else if (specs->signed_p)
12226 error_at (loc,
12227 ("both %<signed%> and %<float%> in "
12228 "declaration specifiers"));
12229 else if (specs->unsigned_p)
12230 error_at (loc,
12231 ("both %<unsigned%> and %<float%> in "
12232 "declaration specifiers"));
12233 else if (specs->saturating_p)
12234 error_at (loc,
12235 ("both %<_Sat%> and %<float%> in "
12236 "declaration specifiers"));
12237 else
12238 {
12239 specs->typespec_word = cts_float;
12240 specs->locations[cdw_typespec] = loc;
12241 }
12242 return specs;
12243 case RID_DOUBLE:
12244 if (specs->long_long_p)
12245 error_at (loc,
12246 ("both %<long long%> and %<double%> in "
12247 "declaration specifiers"));
12248 else if (specs->short_p)
12249 error_at (loc,
12250 ("both %<short%> and %<double%> in "
12251 "declaration specifiers"));
12252 else if (specs->signed_p)
12253 error_at (loc,
12254 ("both %<signed%> and %<double%> in "
12255 "declaration specifiers"));
12256 else if (specs->unsigned_p)
12257 error_at (loc,
12258 ("both %<unsigned%> and %<double%> in "
12259 "declaration specifiers"));
12260 else if (specs->saturating_p)
12261 error_at (loc,
12262 ("both %<_Sat%> and %<double%> in "
12263 "declaration specifiers"));
12264 else
12265 {
12266 specs->typespec_word = cts_double;
12267 specs->locations[cdw_typespec] = loc;
12268 }
12269 return specs;
12270 CASE_RID_FLOATN_NX:
12271 specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12272 if (!in_system_header_at (loc: input_location))
12273 pedwarn_c11 (loc, opt: OPT_Wpedantic,
12274 "ISO C does not support the %<_Float%d%s%> type"
12275 " before C23",
12276 floatn_nx_types[specs->u.floatn_nx_idx].n,
12277 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12278 ? "x"
12279 : ""));
12280
12281 if (specs->long_p)
12282 error_at (loc,
12283 ("both %<long%> and %<_Float%d%s%> in "
12284 "declaration specifiers"),
12285 floatn_nx_types[specs->u.floatn_nx_idx].n,
12286 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12287 ? "x"
12288 : ""));
12289 else if (specs->short_p)
12290 error_at (loc,
12291 ("both %<short%> and %<_Float%d%s%> in "
12292 "declaration specifiers"),
12293 floatn_nx_types[specs->u.floatn_nx_idx].n,
12294 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12295 ? "x"
12296 : ""));
12297 else if (specs->signed_p)
12298 error_at (loc,
12299 ("both %<signed%> and %<_Float%d%s%> in "
12300 "declaration specifiers"),
12301 floatn_nx_types[specs->u.floatn_nx_idx].n,
12302 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12303 ? "x"
12304 : ""));
12305 else if (specs->unsigned_p)
12306 error_at (loc,
12307 ("both %<unsigned%> and %<_Float%d%s%> in "
12308 "declaration specifiers"),
12309 floatn_nx_types[specs->u.floatn_nx_idx].n,
12310 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12311 ? "x"
12312 : ""));
12313 else if (specs->saturating_p)
12314 error_at (loc,
12315 ("both %<_Sat%> and %<_Float%d%s%> in "
12316 "declaration specifiers"),
12317 floatn_nx_types[specs->u.floatn_nx_idx].n,
12318 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12319 ? "x"
12320 : ""));
12321 else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12322 {
12323 specs->typespec_word = cts_floatn_nx;
12324 error_at (loc,
12325 "%<_Float%d%s%> is not supported on this target",
12326 floatn_nx_types[specs->u.floatn_nx_idx].n,
12327 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12328 ? "x"
12329 : ""));
12330 }
12331 else
12332 {
12333 specs->typespec_word = cts_floatn_nx;
12334 specs->locations[cdw_typespec] = loc;
12335 }
12336 return specs;
12337 case RID_DFLOAT32:
12338 case RID_DFLOAT64:
12339 case RID_DFLOAT128:
12340 {
12341 const char *str;
12342 if (i == RID_DFLOAT32)
12343 str = "_Decimal32";
12344 else if (i == RID_DFLOAT64)
12345 str = "_Decimal64";
12346 else
12347 str = "_Decimal128";
12348 if (specs->long_long_p)
12349 error_at (loc,
12350 ("both %<long long%> and %qs in "
12351 "declaration specifiers"),
12352 str);
12353 if (specs->long_p)
12354 error_at (loc,
12355 ("both %<long%> and %qs in "
12356 "declaration specifiers"),
12357 str);
12358 else if (specs->short_p)
12359 error_at (loc,
12360 ("both %<short%> and %qs in "
12361 "declaration specifiers"),
12362 str);
12363 else if (specs->signed_p)
12364 error_at (loc,
12365 ("both %<signed%> and %qs in "
12366 "declaration specifiers"),
12367 str);
12368 else if (specs->unsigned_p)
12369 error_at (loc,
12370 ("both %<unsigned%> and %qs in "
12371 "declaration specifiers"),
12372 str);
12373 else if (specs->complex_p)
12374 error_at (loc,
12375 ("both %<complex%> and %qs in "
12376 "declaration specifiers"),
12377 str);
12378 else if (specs->saturating_p)
12379 error_at (loc,
12380 ("both %<_Sat%> and %qs in "
12381 "declaration specifiers"),
12382 str);
12383 else if (i == RID_DFLOAT32)
12384 specs->typespec_word = cts_dfloat32;
12385 else if (i == RID_DFLOAT64)
12386 specs->typespec_word = cts_dfloat64;
12387 else
12388 specs->typespec_word = cts_dfloat128;
12389 specs->locations[cdw_typespec] = loc;
12390 }
12391 if (!targetm.decimal_float_supported_p ())
12392 error_at (loc,
12393 ("decimal floating-point not supported "
12394 "for this target"));
12395 pedwarn_c11 (loc, opt: OPT_Wpedantic,
12396 "ISO C does not support decimal floating-point "
12397 "before C23");
12398 return specs;
12399 case RID_FRACT:
12400 case RID_ACCUM:
12401 {
12402 const char *str;
12403 if (i == RID_FRACT)
12404 str = "_Fract";
12405 else
12406 str = "_Accum";
12407 if (specs->complex_p)
12408 error_at (loc,
12409 ("both %<complex%> and %qs in "
12410 "declaration specifiers"),
12411 str);
12412 else if (i == RID_FRACT)
12413 specs->typespec_word = cts_fract;
12414 else
12415 specs->typespec_word = cts_accum;
12416 specs->locations[cdw_typespec] = loc;
12417 }
12418 if (!targetm.fixed_point_supported_p ())
12419 error_at (loc,
12420 "fixed-point types not supported for this target");
12421 pedwarn (loc, OPT_Wpedantic,
12422 "ISO C does not support fixed-point types");
12423 return specs;
12424 case RID_BITINT:
12425 if (specs->long_p)
12426 error_at (loc,
12427 ("both %<long%> and %<_BitInt%> in "
12428 "declaration specifiers"));
12429 else if (specs->short_p)
12430 error_at (loc,
12431 ("both %<short%> and %<_BitInt%> in "
12432 "declaration specifiers"));
12433 else if (specs->complex_p)
12434 error_at (loc,
12435 ("both %<complex%> and %<_BitInt%> in "
12436 "declaration specifiers"));
12437 else if (specs->saturating_p)
12438 error_at (loc,
12439 ("both %<_Sat%> and %<_BitInt%> in "
12440 "declaration specifiers"));
12441 else
12442 {
12443 specs->typespec_word = cts_bitint;
12444 specs->locations[cdw_typespec] = loc;
12445 specs->u.bitint_prec = -1;
12446 if (error_operand_p (t: spec.expr))
12447 return specs;
12448 if (TREE_CODE (spec.expr) != INTEGER_CST
12449 || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12450 {
12451 error_at (loc, "%<_BitInt%> argument is not an integer "
12452 "constant expression");
12453 return specs;
12454 }
12455 if (tree_int_cst_sgn (spec.expr) <= 0)
12456 {
12457 error_at (loc, "%<_BitInt%> argument %qE is not a "
12458 "positive integer constant expression",
12459 spec.expr);
12460 return specs;
12461 }
12462 if (wi::to_widest (t: spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12463 {
12464 error_at (loc, "%<_BitInt%> argument %qE is larger than "
12465 "%<BITINT_MAXWIDTH%> %qd",
12466 spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12467 return specs;
12468 }
12469 specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12470 struct bitint_info info;
12471 if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12472 &info))
12473 {
12474 sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12475 "this target", specs->u.bitint_prec);
12476 specs->u.bitint_prec = -1;
12477 return specs;
12478 }
12479 }
12480 return specs;
12481 default:
12482 /* ObjC reserved word "id", handled below. */
12483 break;
12484 }
12485 }
12486 }
12487
12488 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12489 form of ObjC type, cases such as "int" and "long" being handled
12490 above), a TYPE (struct, union, enum and typeof specifiers) or an
12491 ERROR_MARK. In none of these cases may there have previously
12492 been any type specifiers. */
12493 if (specs->type || specs->typespec_word != cts_none
12494 || specs->long_p || specs->short_p || specs->signed_p
12495 || specs->unsigned_p || specs->complex_p)
12496 error_at (loc, "two or more data types in declaration specifiers");
12497 else if (TREE_CODE (type) == TYPE_DECL)
12498 {
12499 specs->type = TREE_TYPE (type);
12500 if (TREE_TYPE (type) != error_mark_node)
12501 {
12502 specs->decl_attr = DECL_ATTRIBUTES (type);
12503 specs->typedef_p = true;
12504 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12505 specs->locations[cdw_typedef] = loc;
12506
12507 /* If this typedef name is defined in a struct, then a C++
12508 lookup would return a different value. */
12509 if (warn_cxx_compat
12510 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12511 warning_at (loc, OPT_Wc___compat,
12512 "C++ lookup of %qD would return a field, not a type",
12513 type);
12514
12515 /* If we are parsing a struct, record that a struct field
12516 used a typedef. */
12517 if (warn_cxx_compat && struct_parse_info != NULL)
12518 struct_parse_info->typedefs_seen.safe_push (obj: type);
12519 }
12520 }
12521 else if (TREE_CODE (type) == IDENTIFIER_NODE)
12522 {
12523 tree t = lookup_name (name: type);
12524 if (!t || TREE_CODE (t) != TYPE_DECL)
12525 error_at (loc, "%qE fails to be a typedef or built in type", type);
12526 else if (TREE_TYPE (t) == error_mark_node)
12527 ;
12528 else
12529 {
12530 specs->type = TREE_TYPE (t);
12531 specs->locations[cdw_typespec] = loc;
12532 }
12533 }
12534 else
12535 {
12536 if (TREE_CODE (type) != ERROR_MARK)
12537 {
12538 if (spec.kind == ctsk_typeof)
12539 {
12540 specs->typedef_p = true;
12541 specs->locations[cdw_typedef] = loc;
12542 }
12543 if (spec.expr)
12544 {
12545 if (specs->expr)
12546 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12547 specs->expr, spec.expr);
12548 else
12549 specs->expr = spec.expr;
12550 specs->expr_const_operands &= spec.expr_const_operands;
12551 }
12552 }
12553 specs->type = type;
12554 if (spec.has_enum_type_specifier
12555 && spec.kind != ctsk_tagdef)
12556 specs->enum_type_specifier_ref_p = true;
12557 }
12558
12559 return specs;
12560}
12561
12562/* Add the storage class specifier or function specifier SCSPEC to the
12563 declaration specifiers SPECS, returning SPECS. */
12564
12565struct c_declspecs *
12566declspecs_add_scspec (location_t loc,
12567 struct c_declspecs *specs,
12568 tree scspec)
12569{
12570 enum rid i;
12571 enum c_storage_class n = csc_none;
12572 bool dupe = false;
12573 specs->declspecs_seen_p = true;
12574 specs->non_std_attrs_seen_p = true;
12575 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12576 && C_IS_RESERVED_WORD (scspec));
12577 i = C_RID_CODE (scspec);
12578 if (specs->non_sc_seen_p)
12579 warning (OPT_Wold_style_declaration,
12580 "%qE is not at beginning of declaration", scspec);
12581 switch (i)
12582 {
12583 case RID_INLINE:
12584 /* C99 permits duplicate inline. Although of doubtful utility,
12585 it seems simplest to permit it in gnu89 mode as well, as
12586 there is also little utility in maintaining this as a
12587 difference between gnu89 and C99 inline. */
12588 dupe = false;
12589 specs->inline_p = true;
12590 specs->locations[cdw_inline] = loc;
12591 break;
12592 case RID_NORETURN:
12593 /* Duplicate _Noreturn is permitted. */
12594 dupe = false;
12595 specs->noreturn_p = true;
12596 specs->locations[cdw_noreturn] = loc;
12597 break;
12598 case RID_THREAD:
12599 dupe = specs->thread_p;
12600 if (specs->storage_class == csc_auto)
12601 error ("%qE used with %<auto%>", scspec);
12602 else if (specs->storage_class == csc_register)
12603 error ("%qE used with %<register%>", scspec);
12604 else if (specs->storage_class == csc_typedef)
12605 error ("%qE used with %<typedef%>", scspec);
12606 else if (specs->constexpr_p)
12607 error ("%qE used with %<constexpr%>", scspec);
12608 else
12609 {
12610 specs->thread_p = true;
12611 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12612 s2: "__thread") == 0);
12613 /* A diagnostic is not required for the use of this
12614 identifier in the implementation namespace; only diagnose
12615 it for the C11 spelling because of existing code using
12616 the other spelling. */
12617 if (!specs->thread_gnu_p)
12618 {
12619 if (flag_isoc99)
12620 pedwarn_c99 (loc, opt: OPT_Wpedantic,
12621 "ISO C99 does not support %qE", scspec);
12622 else
12623 pedwarn_c99 (loc, opt: OPT_Wpedantic,
12624 "ISO C90 does not support %qE", scspec);
12625 }
12626 specs->locations[cdw_thread] = loc;
12627 }
12628 break;
12629 case RID_AUTO:
12630 if (flag_isoc23
12631 && specs->typespec_kind == ctsk_none
12632 && specs->storage_class != csc_typedef)
12633 {
12634 /* "auto" potentially used for type deduction. */
12635 if (specs->c23_auto_p)
12636 error ("duplicate %qE", scspec);
12637 specs->c23_auto_p = true;
12638 return specs;
12639 }
12640 n = csc_auto;
12641 /* auto may only be used with another storage class specifier,
12642 such as constexpr, if the type is inferred. */
12643 if (specs->constexpr_p)
12644 error ("%qE used with %<constexpr%>", scspec);
12645 break;
12646 case RID_EXTERN:
12647 n = csc_extern;
12648 /* Diagnose "__thread extern". */
12649 if (specs->thread_p && specs->thread_gnu_p)
12650 error ("%<__thread%> before %<extern%>");
12651 break;
12652 case RID_REGISTER:
12653 n = csc_register;
12654 break;
12655 case RID_STATIC:
12656 n = csc_static;
12657 /* Diagnose "__thread static". */
12658 if (specs->thread_p && specs->thread_gnu_p)
12659 error ("%<__thread%> before %<static%>");
12660 break;
12661 case RID_TYPEDEF:
12662 n = csc_typedef;
12663 if (specs->c23_auto_p)
12664 {
12665 error ("%<typedef%> used with %<auto%>");
12666 specs->c23_auto_p = false;
12667 }
12668 break;
12669 case RID_CONSTEXPR:
12670 dupe = specs->constexpr_p;
12671 if (specs->storage_class == csc_extern)
12672 error ("%qE used with %<extern%>", scspec);
12673 else if (specs->storage_class == csc_typedef)
12674 error ("%qE used with %<typedef%>", scspec);
12675 else if (specs->storage_class == csc_auto)
12676 /* auto may only be used with another storage class specifier,
12677 such as constexpr, if the type is inferred. */
12678 error ("%qE used with %<auto%>", scspec);
12679 else if (specs->thread_p)
12680 error ("%qE used with %qs", scspec,
12681 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12682 else
12683 specs->constexpr_p = true;
12684 break;
12685 default:
12686 gcc_unreachable ();
12687 }
12688 if (n != csc_none && n == specs->storage_class)
12689 dupe = true;
12690 if (dupe)
12691 {
12692 if (i == RID_THREAD)
12693 error ("duplicate %<_Thread_local%> or %<__thread%>");
12694 else
12695 error ("duplicate %qE", scspec);
12696 }
12697 if (n != csc_none)
12698 {
12699 if (specs->storage_class != csc_none && n != specs->storage_class)
12700 {
12701 error ("multiple storage classes in declaration specifiers");
12702 }
12703 else
12704 {
12705 specs->storage_class = n;
12706 specs->locations[cdw_storage_class] = loc;
12707 if (n != csc_extern && n != csc_static && specs->thread_p)
12708 {
12709 error ("%qs used with %qE",
12710 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12711 scspec);
12712 specs->thread_p = false;
12713 }
12714 if (n != csc_auto && n != csc_register && n != csc_static
12715 && specs->constexpr_p)
12716 {
12717 error ("%<constexpr%> used with %qE", scspec);
12718 specs->constexpr_p = false;
12719 }
12720 }
12721 }
12722 return specs;
12723}
12724
12725/* Add the attributes ATTRS to the declaration specifiers SPECS,
12726 returning SPECS. */
12727
12728struct c_declspecs *
12729declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12730{
12731 specs->attrs = chainon (attrs, specs->attrs);
12732 specs->locations[cdw_attributes] = loc;
12733 specs->declspecs_seen_p = true;
12734 /* In the case of standard attributes at the start of the
12735 declaration, the caller will reset this. */
12736 specs->non_std_attrs_seen_p = true;
12737 return specs;
12738}
12739
12740/* Add an _Alignas specifier (expression ALIGN, or type whose
12741 alignment is ALIGN) to the declaration specifiers SPECS, returning
12742 SPECS. */
12743struct c_declspecs *
12744declspecs_add_alignas (location_t loc,
12745 struct c_declspecs *specs, tree align)
12746{
12747 specs->alignas_p = true;
12748 specs->locations[cdw_alignas] = loc;
12749 if (align == error_mark_node)
12750 return specs;
12751
12752 /* Only accept the alignment if it's valid and greater than
12753 the current one. Zero is invalid but by C11 required to
12754 be silently ignored. */
12755 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12756 if (align_log > specs->align_log)
12757 specs->align_log = align_log;
12758 return specs;
12759}
12760
12761/* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12762 specifiers with any other type specifier to determine the resulting
12763 type. This is where ISO C checks on complex types are made, since
12764 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12765 double". Also apply postfix standard attributes to modify the type. */
12766
12767struct c_declspecs *
12768finish_declspecs (struct c_declspecs *specs)
12769{
12770 /* If a type was specified as a whole, we have no modifiers and are
12771 done. */
12772 if (specs->type != NULL_TREE)
12773 {
12774 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12775 && !specs->signed_p && !specs->unsigned_p
12776 && !specs->complex_p && !specs->c23_auto_p);
12777
12778 /* Set a dummy type. */
12779 if (TREE_CODE (specs->type) == ERROR_MARK)
12780 specs->type = integer_type_node;
12781 goto handle_postfix_attrs;
12782 }
12783
12784 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12785 has been specified, treat it as "int" unless "_Complex" is
12786 present and there are no other specifiers. If we just have
12787 "_Complex", it is equivalent to "_Complex double", but e.g.
12788 "_Complex short" is equivalent to "_Complex short int". */
12789 if (specs->typespec_word == cts_none)
12790 {
12791 if (specs->saturating_p)
12792 {
12793 error_at (specs->locations[cdw_saturating],
12794 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12795 if (!targetm.fixed_point_supported_p ())
12796 error_at (specs->locations[cdw_saturating],
12797 "fixed-point types not supported for this target");
12798 specs->typespec_word = cts_fract;
12799 }
12800 else if (specs->long_p || specs->short_p
12801 || specs->signed_p || specs->unsigned_p)
12802 {
12803 specs->typespec_word = cts_int;
12804 }
12805 else if (specs->complex_p)
12806 {
12807 specs->typespec_word = cts_double;
12808 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12809 "ISO C does not support plain %<complex%> meaning "
12810 "%<double complex%>");
12811 }
12812 else if (specs->c23_auto_p)
12813 {
12814 /* Type to be filled in later, including applying postfix
12815 attributes. This warning only actually appears for
12816 -Wc11-c23-compat in C23 mode; in older modes, there may
12817 be a warning or pedwarn for implicit "int" instead, or
12818 other errors for use of auto at file scope. */
12819 pedwarn_c11 (input_location, opt: OPT_Wpedantic,
12820 "ISO C does not support %<auto%> type deduction "
12821 "before C23");
12822 return specs;
12823 }
12824 else
12825 {
12826 specs->typespec_word = cts_int;
12827 specs->default_int_p = true;
12828 /* We don't diagnose this here because grokdeclarator will
12829 give more specific diagnostics according to whether it is
12830 a function definition. */
12831 }
12832 }
12833
12834 /* If "signed" was specified, record this to distinguish "int" and
12835 "signed int" in the case of a bit-field with
12836 -funsigned-bitfields. */
12837 specs->explicit_signed_p = specs->signed_p;
12838
12839 /* Now compute the actual type. */
12840 gcc_assert (!specs->c23_auto_p);
12841 switch (specs->typespec_word)
12842 {
12843 case cts_auto_type:
12844 gcc_assert (!specs->long_p && !specs->short_p
12845 && !specs->signed_p && !specs->unsigned_p
12846 && !specs->complex_p);
12847 /* Type to be filled in later. */
12848 if (specs->postfix_attrs)
12849 error ("%<__auto_type%> followed by %<[[]]%> attributes");
12850 break;
12851 case cts_void:
12852 gcc_assert (!specs->long_p && !specs->short_p
12853 && !specs->signed_p && !specs->unsigned_p
12854 && !specs->complex_p);
12855 specs->type = void_type_node;
12856 break;
12857 case cts_bool:
12858 gcc_assert (!specs->long_p && !specs->short_p
12859 && !specs->signed_p && !specs->unsigned_p
12860 && !specs->complex_p);
12861 specs->type = boolean_type_node;
12862 break;
12863 case cts_char:
12864 gcc_assert (!specs->long_p && !specs->short_p);
12865 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12866 if (specs->signed_p)
12867 specs->type = signed_char_type_node;
12868 else if (specs->unsigned_p)
12869 specs->type = unsigned_char_type_node;
12870 else
12871 specs->type = char_type_node;
12872 if (specs->complex_p)
12873 {
12874 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12875 "ISO C does not support complex integer types");
12876 specs->type = build_complex_type (specs->type);
12877 }
12878 break;
12879 case cts_int_n:
12880 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12881 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12882 if (! int_n_enabled_p[specs->u.int_n_idx])
12883 specs->type = integer_type_node;
12884 else
12885 specs->type = (specs->unsigned_p
12886 ? int_n_trees[specs->u.int_n_idx].unsigned_type
12887 : int_n_trees[specs->u.int_n_idx].signed_type);
12888 if (specs->complex_p)
12889 {
12890 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12891 "ISO C does not support complex integer types");
12892 specs->type = build_complex_type (specs->type);
12893 }
12894 break;
12895 case cts_int:
12896 gcc_assert (!(specs->long_p && specs->short_p));
12897 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12898 if (specs->long_long_p)
12899 specs->type = (specs->unsigned_p
12900 ? long_long_unsigned_type_node
12901 : long_long_integer_type_node);
12902 else if (specs->long_p)
12903 specs->type = (specs->unsigned_p
12904 ? long_unsigned_type_node
12905 : long_integer_type_node);
12906 else if (specs->short_p)
12907 specs->type = (specs->unsigned_p
12908 ? short_unsigned_type_node
12909 : short_integer_type_node);
12910 else
12911 specs->type = (specs->unsigned_p
12912 ? unsigned_type_node
12913 : integer_type_node);
12914 if (specs->complex_p)
12915 {
12916 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12917 "ISO C does not support complex integer types");
12918 specs->type = build_complex_type (specs->type);
12919 }
12920 break;
12921 case cts_float:
12922 gcc_assert (!specs->long_p && !specs->short_p
12923 && !specs->signed_p && !specs->unsigned_p);
12924 specs->type = (specs->complex_p
12925 ? complex_float_type_node
12926 : float_type_node);
12927 break;
12928 case cts_double:
12929 gcc_assert (!specs->long_long_p && !specs->short_p
12930 && !specs->signed_p && !specs->unsigned_p);
12931 if (specs->long_p)
12932 {
12933 specs->type = (specs->complex_p
12934 ? complex_long_double_type_node
12935 : long_double_type_node);
12936 }
12937 else
12938 {
12939 specs->type = (specs->complex_p
12940 ? complex_double_type_node
12941 : double_type_node);
12942 }
12943 break;
12944 case cts_floatn_nx:
12945 gcc_assert (!specs->long_p && !specs->short_p
12946 && !specs->signed_p && !specs->unsigned_p);
12947 if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12948 specs->type = integer_type_node;
12949 else if (specs->complex_p)
12950 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
12951 else
12952 specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
12953 break;
12954 case cts_dfloat32:
12955 case cts_dfloat64:
12956 case cts_dfloat128:
12957 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12958 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12959 if (!targetm.decimal_float_supported_p ())
12960 specs->type = integer_type_node;
12961 else if (specs->typespec_word == cts_dfloat32)
12962 specs->type = dfloat32_type_node;
12963 else if (specs->typespec_word == cts_dfloat64)
12964 specs->type = dfloat64_type_node;
12965 else
12966 specs->type = dfloat128_type_node;
12967 break;
12968 case cts_fract:
12969 gcc_assert (!specs->complex_p);
12970 if (!targetm.fixed_point_supported_p ())
12971 specs->type = integer_type_node;
12972 else if (specs->saturating_p)
12973 {
12974 if (specs->long_long_p)
12975 specs->type = specs->unsigned_p
12976 ? sat_unsigned_long_long_fract_type_node
12977 : sat_long_long_fract_type_node;
12978 else if (specs->long_p)
12979 specs->type = specs->unsigned_p
12980 ? sat_unsigned_long_fract_type_node
12981 : sat_long_fract_type_node;
12982 else if (specs->short_p)
12983 specs->type = specs->unsigned_p
12984 ? sat_unsigned_short_fract_type_node
12985 : sat_short_fract_type_node;
12986 else
12987 specs->type = specs->unsigned_p
12988 ? sat_unsigned_fract_type_node
12989 : sat_fract_type_node;
12990 }
12991 else
12992 {
12993 if (specs->long_long_p)
12994 specs->type = specs->unsigned_p
12995 ? unsigned_long_long_fract_type_node
12996 : long_long_fract_type_node;
12997 else if (specs->long_p)
12998 specs->type = specs->unsigned_p
12999 ? unsigned_long_fract_type_node
13000 : long_fract_type_node;
13001 else if (specs->short_p)
13002 specs->type = specs->unsigned_p
13003 ? unsigned_short_fract_type_node
13004 : short_fract_type_node;
13005 else
13006 specs->type = specs->unsigned_p
13007 ? unsigned_fract_type_node
13008 : fract_type_node;
13009 }
13010 break;
13011 case cts_accum:
13012 gcc_assert (!specs->complex_p);
13013 if (!targetm.fixed_point_supported_p ())
13014 specs->type = integer_type_node;
13015 else if (specs->saturating_p)
13016 {
13017 if (specs->long_long_p)
13018 specs->type = specs->unsigned_p
13019 ? sat_unsigned_long_long_accum_type_node
13020 : sat_long_long_accum_type_node;
13021 else if (specs->long_p)
13022 specs->type = specs->unsigned_p
13023 ? sat_unsigned_long_accum_type_node
13024 : sat_long_accum_type_node;
13025 else if (specs->short_p)
13026 specs->type = specs->unsigned_p
13027 ? sat_unsigned_short_accum_type_node
13028 : sat_short_accum_type_node;
13029 else
13030 specs->type = specs->unsigned_p
13031 ? sat_unsigned_accum_type_node
13032 : sat_accum_type_node;
13033 }
13034 else
13035 {
13036 if (specs->long_long_p)
13037 specs->type = specs->unsigned_p
13038 ? unsigned_long_long_accum_type_node
13039 : long_long_accum_type_node;
13040 else if (specs->long_p)
13041 specs->type = specs->unsigned_p
13042 ? unsigned_long_accum_type_node
13043 : long_accum_type_node;
13044 else if (specs->short_p)
13045 specs->type = specs->unsigned_p
13046 ? unsigned_short_accum_type_node
13047 : short_accum_type_node;
13048 else
13049 specs->type = specs->unsigned_p
13050 ? unsigned_accum_type_node
13051 : accum_type_node;
13052 }
13053 break;
13054 case cts_bitint:
13055 gcc_assert (!specs->long_p && !specs->short_p
13056 && !specs->complex_p);
13057 if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13058 {
13059 error_at (specs->locations[cdw_typespec],
13060 "%<signed _BitInt%> argument must be at least 2");
13061 specs->type = integer_type_node;
13062 break;
13063 }
13064 if (specs->u.bitint_prec == -1)
13065 specs->type = integer_type_node;
13066 else
13067 {
13068 pedwarn_c11 (specs->locations[cdw_typespec], opt: OPT_Wpedantic,
13069 "ISO C does not support %<%s_BitInt(%d)%> before C23",
13070 specs->unsigned_p ? "unsigned "
13071 : specs->signed_p ? "signed " : "",
13072 specs->u.bitint_prec);
13073 specs->type = build_bitint_type (specs->u.bitint_prec,
13074 specs->unsigned_p);
13075 }
13076 break;
13077 default:
13078 gcc_unreachable ();
13079 }
13080 handle_postfix_attrs:
13081 if (specs->type != NULL)
13082 {
13083 specs->postfix_attrs = c_warn_type_attributes (attrs: specs->postfix_attrs);
13084 decl_attributes (&specs->type, specs->postfix_attrs, 0);
13085 specs->postfix_attrs = NULL_TREE;
13086 }
13087
13088 return specs;
13089}
13090
13091/* Perform final processing on one file scope's declarations (or the
13092 external scope's declarations), GLOBALS. */
13093
13094static void
13095c_write_global_declarations_1 (tree globals)
13096{
13097 tree decl;
13098 bool reconsider;
13099
13100 /* Process the decls in the order they were written. */
13101 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13102 {
13103 /* Check for used but undefined static functions using the C
13104 standard's definition of "used", and set TREE_NO_WARNING so
13105 that check_global_declaration doesn't repeat the check. */
13106 if (TREE_CODE (decl) == FUNCTION_DECL
13107 && DECL_INITIAL (decl) == NULL_TREE
13108 && DECL_EXTERNAL (decl)
13109 && !TREE_PUBLIC (decl))
13110 {
13111 if (C_DECL_USED (decl))
13112 {
13113 /* TODO: Add OPT_Wundefined-inline. */
13114 if (pedwarn (input_location, 0, "%q+F used but never defined",
13115 decl))
13116 suppress_warning (decl /* OPT_Wundefined-inline. */);
13117 }
13118 /* For -Wunused-function warn about unused static prototypes. */
13119 else if (warn_unused_function
13120 && ! DECL_ARTIFICIAL (decl)
13121 && ! warning_suppressed_p (decl, OPT_Wunused_function))
13122 {
13123 if (warning (OPT_Wunused_function,
13124 "%q+F declared %<static%> but never defined",
13125 decl))
13126 suppress_warning (decl, OPT_Wunused_function);
13127 }
13128 }
13129
13130 wrapup_global_declaration_1 (decl);
13131 }
13132
13133 do
13134 {
13135 reconsider = false;
13136 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13137 reconsider |= wrapup_global_declaration_2 (decl);
13138 }
13139 while (reconsider);
13140}
13141
13142/* Preserve the external declarations scope across a garbage collect. */
13143static GTY(()) tree ext_block;
13144
13145/* Collect all references relevant to SOURCE_FILE. */
13146
13147static void
13148collect_all_refs (const char *source_file)
13149{
13150 tree t;
13151 unsigned i;
13152
13153 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13154 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13155
13156 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13157}
13158
13159/* Collect source file references at global level. */
13160
13161static void
13162collect_source_refs (void)
13163{
13164 tree t;
13165 tree decls;
13166 tree decl;
13167 unsigned i;
13168
13169 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13170 {
13171 decls = DECL_INITIAL (t);
13172 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13173 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13174 collect_source_ref (DECL_SOURCE_FILE (decl));
13175 }
13176
13177 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13178 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13179 collect_source_ref (DECL_SOURCE_FILE (decl));
13180}
13181
13182/* Free attribute access data that are not needed by the middle end. */
13183
13184static void
13185free_attr_access_data ()
13186{
13187 struct cgraph_node *n;
13188
13189 /* Iterate over all functions declared in the translation unit. */
13190 FOR_EACH_FUNCTION (n)
13191 {
13192 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13193 if (tree attrs = DECL_ATTRIBUTES (parm))
13194 attr_access::free_lang_data (attrs);
13195
13196 tree fntype = TREE_TYPE (n->decl);
13197 if (!fntype || fntype == error_mark_node)
13198 continue;
13199 tree attrs = TYPE_ATTRIBUTES (fntype);
13200 if (!attrs)
13201 continue;
13202
13203 attr_access::free_lang_data (attrs);
13204 }
13205}
13206
13207/* Perform any final parser cleanups and generate initial debugging
13208 information. */
13209
13210void
13211c_parse_final_cleanups (void)
13212{
13213 tree t;
13214 unsigned i;
13215
13216 /* We don't want to do this if generating a PCH. */
13217 if (pch_file)
13218 return;
13219
13220 timevar_stop (TV_PHASE_PARSING);
13221 timevar_start (TV_PHASE_DEFERRED);
13222
13223 /* Do the Objective-C stuff. This is where all the Objective-C
13224 module stuff gets generated (symtab, class/protocol/selector
13225 lists etc). */
13226 if (c_dialect_objc ())
13227 objc_write_global_declarations ();
13228
13229 /* Close the external scope. */
13230 ext_block = pop_scope ();
13231 external_scope = 0;
13232 gcc_assert (!current_scope);
13233
13234 /* Handle -fdump-ada-spec[-slim]. */
13235 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13236 {
13237 /* Build a table of files to generate specs for */
13238 collect_source_ref (main_input_filename);
13239 if (!flag_dump_ada_spec_slim)
13240 collect_source_refs ();
13241
13242 dump_ada_specs (collect_all_refs, NULL);
13243 }
13244
13245 /* Process all file scopes in this compilation, and the external_scope,
13246 through wrapup_global_declarations. */
13247 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13248 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13249 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13250
13251 if (!in_lto_p)
13252 free_attr_access_data ();
13253
13254 timevar_stop (TV_PHASE_DEFERRED);
13255 timevar_start (TV_PHASE_PARSING);
13256
13257 ext_block = NULL;
13258}
13259
13260/* Register reserved keyword WORD as qualifier for address space AS. */
13261
13262void
13263c_register_addr_space (const char *word, addr_space_t as)
13264{
13265 int rid = RID_FIRST_ADDR_SPACE + as;
13266 tree id;
13267
13268 /* Address space qualifiers are only supported
13269 in C with GNU extensions enabled. */
13270 if (c_dialect_objc () || flag_no_asm)
13271 return;
13272
13273 id = get_identifier (word);
13274 C_SET_RID_CODE (id, rid);
13275 C_IS_RESERVED_WORD (id) = 1;
13276 ridpointers [rid] = id;
13277}
13278
13279/* Return identifier to look up for omp declare reduction. */
13280
13281tree
13282c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13283{
13284 const char *p = NULL;
13285 switch (reduction_code)
13286 {
13287 case PLUS_EXPR: p = "+"; break;
13288 case MULT_EXPR: p = "*"; break;
13289 case MINUS_EXPR: p = "-"; break;
13290 case BIT_AND_EXPR: p = "&"; break;
13291 case BIT_XOR_EXPR: p = "^"; break;
13292 case BIT_IOR_EXPR: p = "|"; break;
13293 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13294 case TRUTH_ORIF_EXPR: p = "||"; break;
13295 case MIN_EXPR: p = "min"; break;
13296 case MAX_EXPR: p = "max"; break;
13297 default:
13298 break;
13299 }
13300
13301 if (p == NULL)
13302 {
13303 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13304 return error_mark_node;
13305 p = IDENTIFIER_POINTER (reduction_id);
13306 }
13307
13308 const char prefix[] = "omp declare reduction ";
13309 size_t lenp = sizeof (prefix);
13310 size_t len = strlen (s: p);
13311 char *name = XALLOCAVEC (char, lenp + len);
13312 memcpy (dest: name, src: prefix, n: lenp - 1);
13313 memcpy (dest: name + lenp - 1, src: p, n: len + 1);
13314 return get_identifier (name);
13315}
13316
13317/* Lookup REDUCTION_ID in the current scope, or create an artificial
13318 VAR_DECL, bind it into the current scope and return it. */
13319
13320tree
13321c_omp_reduction_decl (tree reduction_id)
13322{
13323 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13324 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13325 return b->decl;
13326
13327 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13328 reduction_id, integer_type_node);
13329 DECL_ARTIFICIAL (decl) = 1;
13330 DECL_EXTERNAL (decl) = 1;
13331 TREE_STATIC (decl) = 1;
13332 TREE_PUBLIC (decl) = 0;
13333 bind (name: reduction_id, decl, scope: current_scope, invisible: true, nested: false, BUILTINS_LOCATION);
13334 return decl;
13335}
13336
13337/* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13338
13339tree
13340c_omp_reduction_lookup (tree reduction_id, tree type)
13341{
13342 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13343 while (b)
13344 {
13345 tree t;
13346 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13347 if (comptypes (TREE_PURPOSE (t), type))
13348 return TREE_VALUE (t);
13349 b = b->shadowed;
13350 }
13351 return error_mark_node;
13352}
13353
13354/* Helper function called via walk_tree, to diagnose invalid
13355 #pragma omp declare reduction combiners or initializers. */
13356
13357tree
13358c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13359{
13360 tree *vars = (tree *) data;
13361 if (SSA_VAR_P (*tp)
13362 && !DECL_ARTIFICIAL (*tp)
13363 && *tp != vars[0]
13364 && *tp != vars[1])
13365 {
13366 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13367 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), s2: "omp_out") == 0)
13368 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13369 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13370 *tp);
13371 else
13372 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13373 "to variable %qD which is not %<omp_priv%> nor "
13374 "%<omp_orig%>",
13375 *tp);
13376 return *tp;
13377 }
13378 return NULL_TREE;
13379}
13380
13381
13382bool
13383c_check_in_current_scope (tree decl)
13384{
13385 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13386 return b != NULL && B_IN_CURRENT_SCOPE (b);
13387}
13388
13389#include "gt-c-c-decl.h"
13390

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