Debian Codegraph @

gcc/c/c-decl.cc

raw
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
23
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
26
27 #include "config.h"
28 #define INCLUDE_STRING
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
60 #include "context.h" /* For 'g'. */
61 #include "omp-general.h"
62 #include "omp-offload.h" /* For offload_vars. */
63 #include "c-parser.h"
64 #include "gcc-urlifier.h"
65
66 #include "tree-pretty-print.h"
67
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 enum decl_context
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
75
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)) or __attribute__((unavailable)).
78 An object declared as __attribute__((unavailable)) should suppress
79 any reports of being declared with unavailable or deprecated items.
80 An object declared as __attribute__((deprecated)) should suppress
81 warnings of uses of other deprecated items. */
82
83 enum deprecated_states {
84 DEPRECATED_NORMAL,
85 DEPRECATED_SUPPRESS,
86 UNAVAILABLE_DEPRECATED_SUPPRESS
87 };
88
89
90 /* Nonzero if we have seen an invalid cross reference
91 to a struct, union, or enum, but not yet printed the message. */
92 tree pending_invalid_xref;
93
94 /* File and line to appear in the eventual error message. */
95 location_t pending_invalid_xref_location;
96
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
100
101 static location_t current_function_prototype_locus;
102
103 /* Whether this prototype was built-in. */
104
105 static bool current_function_prototype_built_in;
106
107 /* The argument type information of this prototype. */
108
109 static tree current_function_prototype_arg_types;
110
111 /* The argument information structure for the function currently being
112 defined. */
113
114 static struct c_arg_info *current_function_arg_info;
115
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
118 allocated. */
119 struct obstack parser_obstack;
120
121 /* The current statement tree. */
122
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
124
125 /* Zero if we are not in an iteration or switch statement, otherwise
126 a bitmask. See bitmask definitions in c-tree.h. */
127 unsigned char in_statement;
128
129 /* A list of decls to be made automatically visible in each file scope. */
130 static GTY(()) tree visible_builtins;
131
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a return statement that specifies a return value is seen. */
134
135 int current_function_returns_value;
136
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement with no argument is seen. */
139
140 int current_function_returns_null;
141
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a call to a noreturn function is seen. */
144
145 int current_function_returns_abnormally;
146
147 /* Set to nonzero by `grokdeclarator' for a function
148 whose return type is defaulted, if warnings for this are desired. */
149
150 static int warn_about_return_type;
151
152 /* Nonzero when the current toplevel function contains a declaration
153 of a nested function which is never defined. */
154
155 static bool undef_nested_function;
156
157 /* Vector of implicit "omp declare target" attributes to be added into
158 the attribute lists. */
159 vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160
161 /* Vector of
162 #pragma omp begin assumes ... #pragma omp end assumes regions
163 we are in. */
164 vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
165
166 /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
167 LABEL_DECL as the last and canonical for each loop or switch. */
168 static vec<tree> loop_names;
169
170 /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
171 vector becomes too long. */
172 static decl_tree_map *loop_names_hash;
173
174 /* Each c_binding structure describes one binding of an identifier to
175 a decl. All the decls in a scope - irrespective of namespace - are
176 chained together by the ->prev field, which (as the name implies)
177 runs in reverse order. All the decls in a given namespace bound to
178 a given identifier are chained by the ->shadowed field, which runs
179 from inner to outer scopes.
180
181 The ->decl field usually points to a DECL node, but there are two
182 exceptions. In the namespace of type tags, the bound entity is a
183 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
184 identifier is encountered, it is bound to error_mark_node to
185 suppress further errors about that identifier in the current
186 function.
187
188 The ->u.type field stores the type of the declaration in this scope;
189 if NULL, the type is the type of the ->decl field. This is only of
190 relevance for objects with external or internal linkage which may
191 be redeclared in inner scopes, forming composite types that only
192 persist for the duration of those scopes. In the external scope,
193 this stores the composite of all the types declared for this
194 object, visible or not. The ->inner_comp field (used only at file
195 scope) stores whether an incomplete array type at file scope was
196 completed at an inner scope to an array size other than 1.
197
198 The ->u.label field is used for labels. It points to a structure
199 which stores additional information used for warnings.
200
201 The depth field is copied from the scope structure that holds this
202 decl. It is used to preserve the proper ordering of the ->shadowed
203 field (see bind()) and also for a handful of special-case checks.
204 Finally, the invisible bit is true for a decl which should be
205 ignored for purposes of normal name lookup, and the nested bit is
206 true for a decl that's been bound a second time in an inner scope;
207 in all such cases, the binding in the outer scope will have its
208 invisible bit true. */
209
210 struct GTY((chain_next ("%h.prev"))) c_binding {
211 union GTY(()) { /* first so GTY desc can use decl */
212 tree GTY((tag ("0"))) type; /* the type in this scope */
213 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
214 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
215 tree decl; /* the decl bound */
216 tree id; /* the identifier it's bound to */
217 struct c_binding *prev; /* the previous decl in this scope */
218 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
219 unsigned int depth : 28; /* depth of this scope */
220 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
221 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
222 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
223 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
224 location_t locus; /* location for nested bindings */
225 };
226 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
227 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
228 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
229 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
230
231 /* Each C symbol points to three linked lists of c_binding structures.
232 These describe the values of the identifier in the three different
233 namespaces defined by the language. */
234
235 struct GTY(()) lang_identifier {
236 struct c_common_identifier common_id;
237 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
238 struct c_binding *tag_binding; /* struct/union/enum tags */
239 struct c_binding *label_binding; /* labels */
240 };
241
242 /* Validate c-lang.cc's assumptions. */
243 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
244 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
245
246 /* The binding oracle; see c-tree.h. */
247 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
248
249 /* This flag is set on an identifier if we have previously asked the
250 binding oracle for this identifier's symbol binding. */
251 #define I_SYMBOL_CHECKED(node) \
252 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
253
254 static inline struct c_binding* *
255 i_symbol_binding (tree node)
256 {
257 struct lang_identifier *lid
258 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
259
260 if (lid->symbol_binding == NULL
261 && c_binding_oracle != NULL
262 && !I_SYMBOL_CHECKED (node))
263 {
264 /* Set the "checked" flag first, to avoid infinite recursion
265 when the binding oracle calls back into gcc. */
266 I_SYMBOL_CHECKED (node) = 1;
267 c_binding_oracle (C_ORACLE_SYMBOL, node);
268 }
269
270 return &lid->symbol_binding;
271 }
272
273 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
274
275 #define I_SYMBOL_DECL(node) \
276 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
277
278 /* This flag is set on an identifier if we have previously asked the
279 binding oracle for this identifier's tag binding. */
280 #define I_TAG_CHECKED(node) \
281 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
282
283 static inline struct c_binding **
284 i_tag_binding (tree node)
285 {
286 struct lang_identifier *lid
287 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
288
289 if (lid->tag_binding == NULL
290 && c_binding_oracle != NULL
291 && !I_TAG_CHECKED (node))
292 {
293 /* Set the "checked" flag first, to avoid infinite recursion
294 when the binding oracle calls back into gcc. */
295 I_TAG_CHECKED (node) = 1;
296 c_binding_oracle (C_ORACLE_TAG, node);
297 }
298
299 return &lid->tag_binding;
300 }
301
302 #define I_TAG_BINDING(node) (*i_tag_binding (node))
303
304 #define I_TAG_DECL(node) \
305 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
306
307 /* This flag is set on an identifier if we have previously asked the
308 binding oracle for this identifier's label binding. */
309 #define I_LABEL_CHECKED(node) \
310 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
311
312 static inline struct c_binding **
313 i_label_binding (tree node)
314 {
315 struct lang_identifier *lid
316 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
317
318 if (lid->label_binding == NULL
319 && c_binding_oracle != NULL
320 && !I_LABEL_CHECKED (node))
321 {
322 /* Set the "checked" flag first, to avoid infinite recursion
323 when the binding oracle calls back into gcc. */
324 I_LABEL_CHECKED (node) = 1;
325 c_binding_oracle (C_ORACLE_LABEL, node);
326 }
327
328 return &lid->label_binding;
329 }
330
331 #define I_LABEL_BINDING(node) (*i_label_binding (node))
332
333 #define I_LABEL_DECL(node) \
334 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
335
336 /* Used by C_TOKEN_VEC tree. */
337 struct GTY (()) c_tree_token_vec {
338 struct tree_base base;
339 vec<c_token, va_gc> *tokens;
340 };
341
342 STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
343 STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
344 == offsetof (c_tree_token_vec_struct, tokens));
345
346 /* The resulting tree type. */
347
348 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
349 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
350 {
351 union tree_node GTY ((tag ("0"),
352 desc ("tree_node_structure (&%h)")))
353 generic;
354 struct lang_identifier GTY ((tag ("1"))) identifier;
355 struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
356 };
357
358 /* Langhook for tree_size. */
359 size_t
360 c_tree_size (enum tree_code code)
361 {
362 gcc_checking_assert (code >= NUM_TREE_CODES);
363 switch (code)
364 {
365 case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
366 default:
367 switch (TREE_CODE_CLASS (code))
368 {
369 case tcc_declaration: return sizeof (tree_decl_non_common);
370 case tcc_type: return sizeof (tree_type_non_common);
371 default: gcc_unreachable ();
372 }
373 }
374 }
375
376 /* Track bindings and other things that matter for goto warnings. For
377 efficiency, we do not gather all the decls at the point of
378 definition. Instead, we point into the bindings structure. As
379 scopes are popped, we update these structures and gather the decls
380 that matter at that time. */
381
382 struct GTY(()) c_spot_bindings {
383 /* The currently open scope which holds bindings defined when the
384 label was defined or the goto statement was found. */
385 struct c_scope *scope;
386 /* The bindings in the scope field which were defined at the point
387 of the label or goto. This lets us look at older or newer
388 bindings in the scope, as appropriate. */
389 struct c_binding *bindings_in_scope;
390 /* The number of statement expressions that have started since this
391 label or goto statement was defined. This is zero if we are at
392 the same statement expression level. It is positive if we are in
393 a statement expression started since this spot. It is negative
394 if this spot was in a statement expression and we have left
395 it. */
396 int stmt_exprs;
397 /* Whether we started in a statement expression but are no longer in
398 it. This is set to true if stmt_exprs ever goes negative. */
399 bool left_stmt_expr;
400 };
401
402 /* This structure is used to keep track of bindings seen when a goto
403 statement is defined. This is only used if we see the goto
404 statement before we see the label. */
405
406 struct GTY(()) c_goto_bindings {
407 /* The location of the goto statement. */
408 location_t loc;
409 /* The bindings of the goto statement. */
410 struct c_spot_bindings goto_bindings;
411 };
412
413 typedef struct c_goto_bindings *c_goto_bindings_p;
414
415 /* The additional information we keep track of for a label binding.
416 These fields are updated as scopes are popped. */
417
418 struct GTY(()) c_label_vars {
419 /* The shadowed c_label_vars, when one label shadows another (which
420 can only happen using a __label__ declaration). */
421 struct c_label_vars *shadowed;
422 /* The bindings when the label was defined. */
423 struct c_spot_bindings label_bindings;
424 /* A list of decls that we care about: decls about which we should
425 warn if a goto branches to this label from later in the function.
426 Decls are added to this list as scopes are popped. We only add
427 the decls that matter. */
428 vec<tree, va_gc> *decls_in_scope;
429 /* A list of goto statements to this label. This is only used for
430 goto statements seen before the label was defined, so that we can
431 issue appropriate warnings for them. */
432 vec<c_goto_bindings_p, va_gc> *gotos;
433 };
434
435 /* Each c_scope structure describes the complete contents of one
436 scope. Four scopes are distinguished specially: the innermost or
437 current scope, the innermost function scope, the file scope (always
438 the second to outermost) and the outermost or external scope.
439
440 Most declarations are recorded in the current scope.
441
442 All normal label declarations are recorded in the innermost
443 function scope, as are bindings of undeclared identifiers to
444 error_mark_node. (GCC permits nested functions as an extension,
445 hence the 'innermost' qualifier.) Explicitly declared labels
446 (using the __label__ extension) appear in the current scope.
447
448 Being in the file scope (current_scope == file_scope) causes
449 special behavior in several places below. Also, under some
450 conditions the Objective-C front end records declarations in the
451 file scope even though that isn't the current scope.
452
453 All declarations with external linkage are recorded in the external
454 scope, even if they aren't visible there; this models the fact that
455 such declarations are visible to the entire program, and (with a
456 bit of cleverness, see pushdecl) allows diagnosis of some violations
457 of C99 6.2.2p7 and 6.2.7p2:
458
459 If, within the same translation unit, the same identifier appears
460 with both internal and external linkage, the behavior is
461 undefined.
462
463 All declarations that refer to the same object or function shall
464 have compatible type; otherwise, the behavior is undefined.
465
466 Initially only the built-in declarations, which describe compiler
467 intrinsic functions plus a subset of the standard library, are in
468 this scope.
469
470 The order of the blocks list matters, and it is frequently appended
471 to. To avoid having to walk all the way to the end of the list on
472 each insertion, or reverse the list later, we maintain a pointer to
473 the last list entry. (FIXME: It should be feasible to use a reversed
474 list here.)
475
476 The bindings list is strictly in reverse order of declarations;
477 pop_scope relies on this. */
478
479
480 struct GTY((chain_next ("%h.outer"))) c_scope {
481 /* The scope containing this one. */
482 struct c_scope *outer;
483
484 /* The next outermost function scope. */
485 struct c_scope *outer_function;
486
487 /* All bindings in this scope. */
488 struct c_binding *bindings;
489
490 /* For each scope (except the global one), a chain of BLOCK nodes
491 for all the scopes that were entered and exited one level down. */
492 tree blocks;
493 tree blocks_last;
494
495 /* The depth of this scope. Used to keep the ->shadowed chain of
496 bindings sorted innermost to outermost. */
497 unsigned int depth : 28;
498
499 /* True if we are currently filling this scope with parameter
500 declarations. */
501 BOOL_BITFIELD parm_flag : 1;
502
503 /* True if we saw [*] in this scope. Used to give an error messages
504 if these appears in a function definition. */
505 BOOL_BITFIELD had_vla_unspec : 1;
506
507 /* True if we already complained about forward parameter decls
508 in this scope. This prevents double warnings on
509 foo (int a; int b; ...) */
510 BOOL_BITFIELD warned_forward_parm_decls : 1;
511
512 /* True if this is the outermost block scope of a function body.
513 This scope contains the parameters, the local variables declared
514 in the outermost block, and all the labels (except those in
515 nested functions, or declared at block scope with __label__). */
516 BOOL_BITFIELD function_body : 1;
517
518 /* True means make a BLOCK for this scope no matter what. */
519 BOOL_BITFIELD keep : 1;
520
521 /* True means that an unsuffixed float constant is _Decimal64. */
522 BOOL_BITFIELD float_const_decimal64 : 1;
523
524 /* True if this scope has any label bindings. This is used to speed
525 up searching for labels when popping scopes, particularly since
526 labels are normally only found at function scope. */
527 BOOL_BITFIELD has_label_bindings : 1;
528
529 /* True if we should issue a warning if a goto statement crosses any
530 of the bindings. We still need to check the list of bindings to
531 find the specific ones we need to warn about. This is true if
532 decl_jump_unsafe would return true for any of the bindings. This
533 is used to avoid looping over all the bindings unnecessarily. */
534 BOOL_BITFIELD has_jump_unsafe_decl : 1;
535 };
536
537 /* The scope currently in effect. */
538
539 static GTY(()) struct c_scope *current_scope;
540
541 /* The innermost function scope. Ordinary (not explicitly declared)
542 labels, bindings to error_mark_node, and the lazily-created
543 bindings of __func__ and its friends get this scope. */
544
545 static GTY(()) struct c_scope *current_function_scope;
546
547 /* The C file scope. This is reset for each input translation unit. */
548
549 static GTY(()) struct c_scope *file_scope;
550
551 /* The outermost scope. This is used for all declarations with
552 external linkage, and only these, hence the name. */
553
554 static GTY(()) struct c_scope *external_scope;
555
556 /* A chain of c_scope structures awaiting reuse. */
557
558 static GTY((deletable)) struct c_scope *scope_freelist;
559
560 /* A chain of c_binding structures awaiting reuse. */
561
562 static GTY((deletable)) struct c_binding *binding_freelist;
563
564 /* Append VAR to LIST in scope SCOPE. */
565 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
566 struct c_scope *s_ = (scope); \
567 tree d_ = (decl); \
568 if (s_->list##_last) \
569 BLOCK_CHAIN (s_->list##_last) = d_; \
570 else \
571 s_->list = d_; \
572 s_->list##_last = d_; \
573 } while (0)
574
575 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
576 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
577 struct c_scope *t_ = (tscope); \
578 struct c_scope *f_ = (fscope); \
579 if (t_->to##_last) \
580 BLOCK_CHAIN (t_->to##_last) = f_->from; \
581 else \
582 t_->to = f_->from; \
583 t_->to##_last = f_->from##_last; \
584 } while (0)
585
586 /* A c_inline_static structure stores details of a static identifier
587 referenced in a definition of a function that may be an inline
588 definition if no subsequent declaration of that function uses
589 "extern" or does not use "inline". */
590
591 struct GTY((chain_next ("%h.next"))) c_inline_static {
592 /* The location for a diagnostic. */
593 location_t location;
594
595 /* The function that may be an inline definition. */
596 tree function;
597
598 /* The object or function referenced. */
599 tree static_decl;
600
601 /* What sort of reference this is. */
602 enum c_inline_static_type type;
603
604 /* The next such structure or NULL. */
605 struct c_inline_static *next;
606 };
607
608 /* List of static identifiers used or referenced in functions that may
609 be inline definitions. */
610 static GTY(()) struct c_inline_static *c_inline_statics;
611
612 /* True means unconditionally make a BLOCK for the next scope pushed. */
613
614 static bool keep_next_level_flag;
615
616 /* True means the next call to push_scope will be the outermost scope
617 of a function body, so do not push a new scope, merely cease
618 expecting parameter decls. */
619
620 static bool next_is_function_body;
621
622 /* A vector of pointers to c_binding structures. */
623
624 typedef struct c_binding *c_binding_ptr;
625
626 /* Information that we keep for a struct or union while it is being
627 parsed. */
628
629 class c_struct_parse_info
630 {
631 public:
632 /* If warn_cxx_compat, a list of types defined within this
633 struct. */
634 auto_vec<tree> struct_types;
635 /* If warn_cxx_compat, a list of field names which have bindings,
636 and which are defined in this struct, but which are not defined
637 in any enclosing struct. This is used to clear the in_struct
638 field of the c_bindings structure. */
639 auto_vec<c_binding_ptr> fields;
640 /* If warn_cxx_compat, a list of typedef names used when defining
641 fields in this struct. */
642 auto_vec<tree> typedefs_seen;
643 /* The location of a previous definition of this struct. */
644 location_t refloc;
645 };
646
647
648 /* Hash table for structs and unions. */
649 struct c_struct_hasher : ggc_ptr_hash<tree_node>
650 {
651 static hashval_t hash (tree t);
652 static bool equal (tree, tree);
653 };
654
655 /* Hash an RECORD OR UNION. */
656 hashval_t
657 c_struct_hasher::hash (tree type)
658 {
659 inchash::hash hstate;
660
661 hstate.add_int (TREE_CODE (type));
662 tree tag = c_type_tag (type);
663 hstate.add_object (tag);
664
665 return hstate.end ();
666 }
667
668 /* Compare two RECORD or UNION types. */
669 bool
670 c_struct_hasher::equal (tree t1, tree t2)
671 {
672 return comptypes_equiv_p (t1, t2);
673 }
674
675 /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
676 static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
677
678 /* Information for the struct or union currently being parsed, or
679 NULL if not parsing a struct or union. */
680 static class c_struct_parse_info *struct_parse_info;
681
682 /* Forward declarations. */
683 static tree lookup_name_in_scope (tree, struct c_scope *);
684 static tree c_make_fname_decl (location_t, tree, int);
685 static tree grokdeclarator (const struct c_declarator *,
686 struct c_declspecs *,
687 enum decl_context, bool, tree *, tree *, tree *,
688 bool *, enum deprecated_states);
689 static tree grokparms (struct c_arg_info *, bool);
690 static void layout_array_type (tree);
691 static const char *header_for_builtin_fn (tree);
692
693 /* T is a statement. Add it to the statement-tree. This is the
694 C/ObjC version--C++ has a slightly different version of this
695 function. */
696
697 tree
698 add_stmt (tree t)
699 {
700 enum tree_code code = TREE_CODE (t);
701
702 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
703 {
704 if (!EXPR_HAS_LOCATION (t))
705 SET_EXPR_LOCATION (t, input_location);
706 }
707
708 /* Add T to the statement-tree. Non-side-effect statements need to be
709 recorded during statement expressions. */
710 if (!building_stmt_list_p ())
711 push_stmt_list ();
712
713 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
714 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
715
716 append_to_statement_list_force (t, &cur_stmt_list);
717
718 return t;
719 }
720
721
722
723
724
725 /* Return true if we will want to say something if a goto statement
726 crosses DECL. */
727
728 static bool
729 decl_jump_unsafe (tree decl)
730 {
731 if (error_operand_p (decl))
732 return false;
733
734 /* Don't warn for compound literals. If a goto statement crosses
735 their initialization, it should cross also all the places where
736 the complit is used or where the complit address might be saved
737 into some variable, so code after the label to which goto jumps
738 should not be able to refer to the compound literal. */
739 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
740 return false;
741
742 if (flag_openmp
743 && VAR_P (decl)
744 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
745 return true;
746
747 /* Always warn about crossing variably modified types. */
748 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
749 && c_type_variably_modified_p (TREE_TYPE (decl)))
750 return true;
751
752 /* Otherwise, only warn if -Wgoto-misses-init and this is an
753 initialized automatic decl. */
754 if (warn_jump_misses_init
755 && VAR_P (decl)
756 && !TREE_STATIC (decl)
757 && DECL_INITIAL (decl) != NULL_TREE)
758 return true;
759
760 return false;
761 }
762
763
764 void
765 c_print_identifier (FILE *file, tree node, int indent)
766 {
767 void (*save) (enum c_oracle_request, tree identifier);
768
769 /* Temporarily hide any binding oracle. Without this, calls to
770 debug_tree from the debugger will end up calling into the oracle,
771 making for a confusing debug session. As the oracle isn't needed
772 here for normal operation, it's simplest to suppress it. */
773 save = c_binding_oracle;
774 c_binding_oracle = NULL;
775
776 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
777 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
778 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
779 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
780 {
781 tree rid = ridpointers[C_RID_CODE (node)];
782 indent_to (file, indent + 4);
783 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
784 (void *) rid, IDENTIFIER_POINTER (rid));
785 }
786
787 c_binding_oracle = save;
788 }
789
790 /* Establish that the scope contains declarations that are sensitive to
791 jumps that cross a binding. Together with decl_jump_unsafe, this is
792 used to diagnose such jumps. */
793 void
794 c_mark_decl_jump_unsafe_in_current_scope ()
795 {
796 current_scope->has_jump_unsafe_decl = 1;
797 }
798
799 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
800 which may be any of several kinds of DECL or TYPE or error_mark_node,
801 in the scope SCOPE. */
802 static void
803 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
804 bool nested, location_t locus)
805 {
806 struct c_binding *b, **here;
807
808 if (binding_freelist)
809 {
810 b = binding_freelist;
811 binding_freelist = b->prev;
812 }
813 else
814 b = ggc_alloc<c_binding> ();
815
816 b->shadowed = 0;
817 b->decl = decl;
818 b->id = name;
819 b->depth = scope->depth;
820 b->invisible = invisible;
821 b->nested = nested;
822 b->inner_comp = 0;
823 b->in_struct = 0;
824 b->locus = locus;
825
826 b->u.type = NULL;
827
828 b->prev = scope->bindings;
829 scope->bindings = b;
830
831 if (decl_jump_unsafe (decl))
832 scope->has_jump_unsafe_decl = 1;
833
834 if (!name)
835 return;
836
837 switch (TREE_CODE (decl))
838 {
839 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
840 case ENUMERAL_TYPE:
841 case UNION_TYPE:
842 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
843 case VAR_DECL:
844 case FUNCTION_DECL:
845 case TYPE_DECL:
846 case CONST_DECL:
847 case PARM_DECL:
848 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
849
850 default:
851 gcc_unreachable ();
852 }
853
854 /* Locate the appropriate place in the chain of shadowed decls
855 to insert this binding. Normally, scope == current_scope and
856 this does nothing. */
857 while (*here && (*here)->depth > scope->depth)
858 here = &(*here)->shadowed;
859
860 b->shadowed = *here;
861 *here = b;
862 }
863
864 /* Clear the binding structure B, stick it on the binding_freelist,
865 and return the former value of b->prev. This is used by pop_scope
866 and get_parm_info to iterate destructively over all the bindings
867 from a given scope. */
868 static struct c_binding *
869 free_binding_and_advance (struct c_binding *b)
870 {
871 struct c_binding *prev = b->prev;
872
873 memset (b, 0, sizeof (struct c_binding));
874 b->prev = binding_freelist;
875 binding_freelist = b;
876
877 return prev;
878 }
879
880 /* Bind a label. Like bind, but skip fields which aren't used for
881 labels, and add the LABEL_VARS value. */
882 static void
883 bind_label (tree name, tree label, struct c_scope *scope,
884 struct c_label_vars *label_vars)
885 {
886 struct c_binding *b;
887
888 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
889 UNKNOWN_LOCATION);
890
891 scope->has_label_bindings = true;
892
893 b = scope->bindings;
894 gcc_assert (b->decl == label);
895 label_vars->shadowed = b->u.label;
896 b->u.label = label_vars;
897 }
898
899 /* Hook called at end of compilation to assume 1 elt
900 for a file-scope tentative array defn that wasn't complete before. */
901
902 void
903 c_finish_incomplete_decl (tree decl)
904 {
905 if (VAR_P (decl))
906 {
907 tree type = TREE_TYPE (decl);
908 if (type != error_mark_node
909 && TREE_CODE (type) == ARRAY_TYPE
910 && !DECL_EXTERNAL (decl)
911 && TYPE_DOMAIN (type) == NULL_TREE)
912 {
913 warning_at (DECL_SOURCE_LOCATION (decl),
914 0, "array %q+D assumed to have one element", decl);
915
916 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
917
918 relayout_decl (decl);
919 }
920 }
921 }
922
923 /* Record that inline function FUNC contains a reference (location
924 LOC) to static DECL (file-scope or function-local according to
925 TYPE). */
926
927 void
928 record_inline_static (location_t loc, tree func, tree decl,
929 enum c_inline_static_type type)
930 {
931 c_inline_static *csi = ggc_alloc<c_inline_static> ();
932 csi->location = loc;
933 csi->function = func;
934 csi->static_decl = decl;
935 csi->type = type;
936 csi->next = c_inline_statics;
937 c_inline_statics = csi;
938 }
939
940 /* Check for references to static declarations in inline functions at
941 the end of the translation unit and diagnose them if the functions
942 are still inline definitions. */
943
944 static void
945 check_inline_statics (void)
946 {
947 struct c_inline_static *csi;
948 for (csi = c_inline_statics; csi; csi = csi->next)
949 {
950 if (DECL_EXTERNAL (csi->function))
951 switch (csi->type)
952 {
953 case csi_internal:
954 pedwarn (csi->location, 0,
955 "%qD is static but used in inline function %qD "
956 "which is not static", csi->static_decl, csi->function);
957 break;
958 case csi_modifiable:
959 pedwarn (csi->location, 0,
960 "%q+D is static but declared in inline function %qD "
961 "which is not static", csi->static_decl, csi->function);
962 break;
963 default:
964 gcc_unreachable ();
965 }
966 }
967 c_inline_statics = NULL;
968 }
969
970 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
971 for the current state, otherwise set it to uninitialized. */
972
973 static void
974 set_spot_bindings (struct c_spot_bindings *p, bool defining)
975 {
976 if (defining)
977 {
978 p->scope = current_scope;
979 p->bindings_in_scope = current_scope->bindings;
980 }
981 else
982 {
983 p->scope = NULL;
984 p->bindings_in_scope = NULL;
985 }
986 p->stmt_exprs = 0;
987 p->left_stmt_expr = false;
988 }
989
990 /* Update spot bindings P as we pop out of SCOPE. Return true if we
991 should push decls for a label. */
992
993 static bool
994 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
995 {
996 if (p->scope != scope)
997 {
998 /* This label or goto is defined in some other scope, or it is a
999 label which is not yet defined. There is nothing to
1000 update. */
1001 return false;
1002 }
1003
1004 /* Adjust the spot bindings to refer to the bindings already defined
1005 in the enclosing scope. */
1006 p->scope = scope->outer;
1007 p->bindings_in_scope = p->scope->bindings;
1008
1009 return true;
1010 }
1011
1012 /* The Objective-C front-end often needs to determine the current scope. */
1013
1014 void *
1015 objc_get_current_scope (void)
1016 {
1017 return current_scope;
1018 }
1019
1020 /* The following function is used only by Objective-C. It needs to live here
1021 because it accesses the innards of c_scope. */
1022
1023 void
1024 objc_mark_locals_volatile (void *enclosing_blk)
1025 {
1026 struct c_scope *scope;
1027 struct c_binding *b;
1028
1029 for (scope = current_scope;
1030 scope && scope != enclosing_blk;
1031 scope = scope->outer)
1032 {
1033 for (b = scope->bindings; b; b = b->prev)
1034 objc_volatilize_decl (b->decl);
1035
1036 /* Do not climb up past the current function. */
1037 if (scope->function_body)
1038 break;
1039 }
1040 }
1041
1042 /* Return true if we are in the global binding level. */
1043
1044 bool
1045 global_bindings_p (void)
1046 {
1047 return current_scope == file_scope;
1048 }
1049
1050 /* Return true if we're declaring parameters in an old-style function
1051 declaration. */
1052
1053 bool
1054 old_style_parameter_scope (void)
1055 {
1056 /* If processing parameters and there is no function statement list, we
1057 * have an old-style function declaration. */
1058 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1059 }
1060
1061 void
1062 keep_next_level (void)
1063 {
1064 keep_next_level_flag = true;
1065 }
1066
1067 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1068
1069 void
1070 set_float_const_decimal64 (void)
1071 {
1072 current_scope->float_const_decimal64 = true;
1073 }
1074
1075 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1076
1077 void
1078 clear_float_const_decimal64 (void)
1079 {
1080 current_scope->float_const_decimal64 = false;
1081 }
1082
1083 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1084
1085 bool
1086 float_const_decimal64_p (void)
1087 {
1088 return current_scope->float_const_decimal64;
1089 }
1090
1091 /* Identify this scope as currently being filled with parameters. */
1092
1093 void
1094 declare_parm_level (void)
1095 {
1096 current_scope->parm_flag = true;
1097 }
1098
1099 void
1100 push_scope (void)
1101 {
1102 if (next_is_function_body)
1103 {
1104 /* This is the transition from the parameters to the top level
1105 of the function body. These are the same scope
1106 (C99 6.2.1p4,6) so we do not push another scope structure.
1107 next_is_function_body is set only by store_parm_decls, which
1108 in turn is called when and only when we are about to
1109 encounter the opening curly brace for the function body.
1110
1111 The outermost block of a function always gets a BLOCK node,
1112 because the debugging output routines expect that each
1113 function has at least one BLOCK. */
1114 current_scope->parm_flag = false;
1115 current_scope->function_body = true;
1116 current_scope->keep = true;
1117 current_scope->outer_function = current_function_scope;
1118 current_function_scope = current_scope;
1119
1120 keep_next_level_flag = false;
1121 next_is_function_body = false;
1122
1123 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1124 if (current_scope->outer)
1125 current_scope->float_const_decimal64
1126 = current_scope->outer->float_const_decimal64;
1127 else
1128 current_scope->float_const_decimal64 = false;
1129 }
1130 else
1131 {
1132 struct c_scope *scope;
1133 if (scope_freelist)
1134 {
1135 scope = scope_freelist;
1136 scope_freelist = scope->outer;
1137 }
1138 else
1139 scope = ggc_cleared_alloc<c_scope> ();
1140
1141 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1142 if (current_scope)
1143 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1144 else
1145 scope->float_const_decimal64 = false;
1146
1147 scope->keep = keep_next_level_flag;
1148 scope->outer = current_scope;
1149 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1150
1151 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1152 possible. */
1153 if (current_scope && scope->depth == 0)
1154 {
1155 scope->depth--;
1156 sorry ("GCC supports only %u nested scopes", scope->depth);
1157 }
1158
1159 current_scope = scope;
1160 keep_next_level_flag = false;
1161 }
1162 }
1163
1164 /* This is called when we are leaving SCOPE. For each label defined
1165 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1166 These are the decls whose initialization will be skipped by a goto
1167 later in the function. */
1168
1169 static void
1170 update_label_decls (struct c_scope *scope)
1171 {
1172 struct c_scope *s;
1173
1174 s = scope;
1175 while (s != NULL)
1176 {
1177 if (s->has_label_bindings)
1178 {
1179 struct c_binding *b;
1180
1181 for (b = s->bindings; b != NULL; b = b->prev)
1182 {
1183 struct c_label_vars *label_vars;
1184 struct c_binding *b1;
1185 bool hjud;
1186 unsigned int ix;
1187 struct c_goto_bindings *g;
1188
1189 if (TREE_CODE (b->decl) != LABEL_DECL)
1190 continue;
1191 label_vars = b->u.label;
1192
1193 b1 = label_vars->label_bindings.bindings_in_scope;
1194 if (label_vars->label_bindings.scope == NULL)
1195 hjud = false;
1196 else
1197 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1198 if (update_spot_bindings (scope, &label_vars->label_bindings))
1199 {
1200 /* This label is defined in this scope. */
1201 if (hjud)
1202 {
1203 for (; b1 != NULL; b1 = b1->prev)
1204 {
1205 /* A goto from later in the function to this
1206 label will never see the initialization
1207 of B1, if any. Save it to issue a
1208 warning if needed. */
1209 if (decl_jump_unsafe (b1->decl))
1210 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1211 }
1212 }
1213 }
1214
1215 /* Update the bindings of any goto statements associated
1216 with this label. */
1217 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1218 update_spot_bindings (scope, &g->goto_bindings);
1219 }
1220 }
1221
1222 /* Don't search beyond the current function. */
1223 if (s == current_function_scope)
1224 break;
1225
1226 s = s->outer;
1227 }
1228 }
1229
1230 /* Exit a scope. Restore the state of the identifier-decl mappings
1231 that were in effect when this scope was entered. Return a BLOCK
1232 node containing all the DECLs in this scope that are of interest
1233 to debug info generation. */
1234
1235 tree
1236 pop_scope (void)
1237 {
1238 struct c_scope *scope = current_scope;
1239 tree block, context, p;
1240 struct c_binding *b;
1241
1242 bool functionbody = scope->function_body;
1243 bool keep = functionbody || scope->keep || scope->bindings;
1244
1245 update_label_decls (scope);
1246
1247 /* If appropriate, create a BLOCK to record the decls for the life
1248 of this function. */
1249 block = NULL_TREE;
1250 if (keep)
1251 {
1252 block = make_node (BLOCK);
1253 BLOCK_SUBBLOCKS (block) = scope->blocks;
1254 TREE_USED (block) = 1;
1255
1256 /* In each subblock, record that this is its superior. */
1257 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1258 BLOCK_SUPERCONTEXT (p) = block;
1259
1260 BLOCK_VARS (block) = NULL_TREE;
1261 }
1262
1263 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1264 scope must be set so that they point to the appropriate
1265 construct, i.e. either to the current FUNCTION_DECL node, or
1266 else to the BLOCK node we just constructed.
1267
1268 Note that for tagged types whose scope is just the formal
1269 parameter list for some function type specification, we can't
1270 properly set their TYPE_CONTEXTs here, because we don't have a
1271 pointer to the appropriate FUNCTION_TYPE node readily available
1272 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1273 type nodes get set in `grokdeclarator' as soon as we have created
1274 the FUNCTION_TYPE node which will represent the "scope" for these
1275 "parameter list local" tagged types. */
1276 if (scope->function_body)
1277 context = current_function_decl;
1278 else if (scope == file_scope)
1279 {
1280 tree file_decl
1281 = build_translation_unit_decl (get_identifier (main_input_filename));
1282 context = file_decl;
1283 debug_hooks->register_main_translation_unit (file_decl);
1284 }
1285 else
1286 context = block;
1287
1288 /* Clear all bindings in this scope. */
1289 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1290 {
1291 p = b->decl;
1292 switch (TREE_CODE (p))
1293 {
1294 case LABEL_DECL:
1295 /* Warnings for unused labels, errors for undefined labels. */
1296 if (TREE_USED (p) && !DECL_INITIAL (p))
1297 {
1298 error ("label %q+D used but not defined", p);
1299 DECL_INITIAL (p) = error_mark_node;
1300 }
1301 else
1302 warn_for_unused_label (p);
1303
1304 /* Labels go in BLOCK_VARS. */
1305 DECL_CHAIN (p) = BLOCK_VARS (block);
1306 BLOCK_VARS (block) = p;
1307 gcc_assert (I_LABEL_BINDING (b->id) == b);
1308 I_LABEL_BINDING (b->id) = b->shadowed;
1309
1310 /* Also pop back to the shadowed label_vars. */
1311 release_tree_vector (b->u.label->decls_in_scope);
1312 b->u.label = b->u.label->shadowed;
1313 break;
1314
1315 case ENUMERAL_TYPE:
1316 case UNION_TYPE:
1317 case RECORD_TYPE:
1318
1319 /* Types may not have tag-names, in which case the type
1320 appears in the bindings list with b->id NULL. */
1321 if (b->id)
1322 {
1323 gcc_assert (I_TAG_BINDING (b->id) == b);
1324 I_TAG_BINDING (b->id) = b->shadowed;
1325 }
1326 break;
1327
1328 case FUNCTION_DECL:
1329 /* Propagate TREE_ADDRESSABLE from nested functions to their
1330 containing functions. */
1331 if (!TREE_ASM_WRITTEN (p)
1332 && DECL_INITIAL (p) != NULL_TREE
1333 && TREE_ADDRESSABLE (p)
1334 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1335 && DECL_ABSTRACT_ORIGIN (p) != p)
1336 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1337 if (!TREE_PUBLIC (p)
1338 && !DECL_INITIAL (p)
1339 && !b->nested
1340 && scope != file_scope
1341 && scope != external_scope)
1342 {
1343 error ("nested function %q+D declared but never defined", p);
1344 undef_nested_function = true;
1345 }
1346 else if (DECL_DECLARED_INLINE_P (p)
1347 && TREE_PUBLIC (p)
1348 && !DECL_INITIAL (p))
1349 {
1350 /* C99 6.7.4p6: "a function with external linkage... declared
1351 with an inline function specifier ... shall also be defined
1352 in the same translation unit." */
1353 if (!flag_gnu89_inline
1354 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1355 && scope == external_scope)
1356 pedwarn (input_location, 0,
1357 "inline function %q+D declared but never defined", p);
1358 DECL_EXTERNAL (p) = 1;
1359 }
1360
1361 goto common_symbol;
1362
1363 case VAR_DECL:
1364 /* Warnings for unused variables. */
1365 if ((!TREE_USED (p) || !DECL_READ_P (p))
1366 && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1367 && !DECL_IN_SYSTEM_HEADER (p)
1368 && DECL_NAME (p)
1369 && !DECL_ARTIFICIAL (p)
1370 && scope != file_scope
1371 && scope != external_scope)
1372 {
1373 if (!TREE_USED (p))
1374 {
1375 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1376 suppress_warning (p, OPT_Wunused_variable);
1377 }
1378 else if (DECL_CONTEXT (p) == current_function_decl)
1379 warning_at (DECL_SOURCE_LOCATION (p),
1380 OPT_Wunused_but_set_variable,
1381 "variable %qD set but not used", p);
1382 }
1383
1384 if (b->inner_comp)
1385 {
1386 error ("type of array %q+D completed incompatibly with"
1387 " implicit initialization", p);
1388 }
1389
1390 /* Fall through. */
1391 case TYPE_DECL:
1392 case CONST_DECL:
1393 common_symbol:
1394 /* All of these go in BLOCK_VARS, but only if this is the
1395 binding in the home scope. */
1396 if (!b->nested)
1397 {
1398 DECL_CHAIN (p) = BLOCK_VARS (block);
1399 BLOCK_VARS (block) = p;
1400 }
1401 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1402 {
1403 /* For block local externs add a special
1404 DECL_EXTERNAL decl for debug info generation. */
1405 tree extp = copy_node (p);
1406
1407 DECL_EXTERNAL (extp) = 1;
1408 TREE_STATIC (extp) = 0;
1409 TREE_PUBLIC (extp) = 1;
1410 DECL_INITIAL (extp) = NULL_TREE;
1411 DECL_LANG_SPECIFIC (extp) = NULL;
1412 DECL_CONTEXT (extp) = current_function_decl;
1413 if (TREE_CODE (p) == FUNCTION_DECL)
1414 {
1415 DECL_RESULT (extp) = NULL_TREE;
1416 DECL_SAVED_TREE (extp) = NULL_TREE;
1417 DECL_STRUCT_FUNCTION (extp) = NULL;
1418 }
1419 if (b->locus != UNKNOWN_LOCATION)
1420 DECL_SOURCE_LOCATION (extp) = b->locus;
1421 DECL_CHAIN (extp) = BLOCK_VARS (block);
1422 BLOCK_VARS (block) = extp;
1423 }
1424 /* If this is the file scope set DECL_CONTEXT of each decl to
1425 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1426 work. */
1427 if (scope == file_scope)
1428 DECL_CONTEXT (p) = context;
1429
1430 gcc_fallthrough ();
1431 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1432 already been put there by store_parm_decls. Unused-
1433 parameter warnings are handled by function.cc.
1434 error_mark_node obviously does not go in BLOCK_VARS and
1435 does not get unused-variable warnings. */
1436 case PARM_DECL:
1437 case ERROR_MARK:
1438 /* It is possible for a decl not to have a name. We get
1439 here with b->id NULL in this case. */
1440 if (b->id)
1441 {
1442 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1443 I_SYMBOL_BINDING (b->id) = b->shadowed;
1444 if (b->shadowed && b->shadowed->u.type)
1445 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1446 }
1447 break;
1448
1449 default:
1450 gcc_unreachable ();
1451 }
1452 }
1453
1454
1455 /* Dispose of the block that we just made inside some higher level. */
1456 if ((scope->function_body || scope == file_scope) && context)
1457 {
1458 DECL_INITIAL (context) = block;
1459 BLOCK_SUPERCONTEXT (block) = context;
1460 }
1461 else if (scope->outer)
1462 {
1463 if (block)
1464 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1465 /* If we did not make a block for the scope just exited, any
1466 blocks made for inner scopes must be carried forward so they
1467 will later become subblocks of something else. */
1468 else if (scope->blocks)
1469 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1470 }
1471
1472 /* Pop the current scope, and free the structure for reuse. */
1473 current_scope = scope->outer;
1474 if (scope->function_body)
1475 current_function_scope = scope->outer_function;
1476
1477 memset (scope, 0, sizeof (struct c_scope));
1478 scope->outer = scope_freelist;
1479 scope_freelist = scope;
1480
1481 return block;
1482 }
1483
1484 void
1485 push_file_scope (void)
1486 {
1487 tree decl;
1488
1489 if (file_scope)
1490 return;
1491
1492 push_scope ();
1493 file_scope = current_scope;
1494
1495 start_fname_decls ();
1496
1497 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1498 bind (DECL_NAME (decl), decl, file_scope,
1499 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1500 }
1501
1502 void
1503 pop_file_scope (void)
1504 {
1505 /* In case there were missing closebraces, get us back to the global
1506 binding level. */
1507 while (current_scope != file_scope)
1508 pop_scope ();
1509
1510 /* __FUNCTION__ is defined at file scope (""). This
1511 call may not be necessary as my tests indicate it
1512 still works without it. */
1513 finish_fname_decls ();
1514
1515 check_inline_statics ();
1516
1517 /* This is the point to write out a PCH if we're doing that.
1518 In that case we do not want to do anything else. */
1519 if (pch_file)
1520 {
1521 c_common_write_pch ();
1522 /* Ensure even the callers don't try to finalize the CU. */
1523 flag_syntax_only = 1;
1524 return;
1525 }
1526
1527 /* Pop off the file scope and close this translation unit. */
1528 pop_scope ();
1529 file_scope = 0;
1530
1531 maybe_apply_pending_pragma_weaks ();
1532 }
1533
1534 /* Whether we are curently inside the initializer for an
1535 underspecified object definition (C23 auto or constexpr). */
1536 static bool in_underspecified_init;
1537
1538 /* Start an underspecified object definition for NAME at LOC. This
1539 means that NAME is shadowed inside its initializer, so neither the
1540 definition being initialized, nor any definition from an outer
1541 scope, may be referenced during that initializer. Return state to
1542 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1543 underspecified object is a (constexpr) compound literal; there is
1544 no shadowing in that case, but all the other restrictions on
1545 underspecified object definitions still apply. */
1546 unsigned int
1547 start_underspecified_init (location_t loc, tree name)
1548 {
1549 bool prev = in_underspecified_init;
1550 bool ok;
1551 if (name == NULL_TREE)
1552 ok = true;
1553 else
1554 {
1555 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1556 C_DECL_UNDERSPECIFIED (decl) = 1;
1557 struct c_scope *scope = current_scope;
1558 struct c_binding *b = I_SYMBOL_BINDING (name);
1559 if (b && B_IN_SCOPE (b, scope))
1560 {
1561 error_at (loc, "underspecified declaration of %qE, which is already "
1562 "declared in this scope", name);
1563 ok = false;
1564 }
1565 else
1566 {
1567 bind (name, decl, scope, false, false, loc);
1568 ok = true;
1569 }
1570 }
1571 in_underspecified_init = true;
1572 return ok | (prev << 1);
1573 }
1574
1575 /* Finish an underspecified object definition for NAME, before that
1576 name is bound to the real declaration instead of a placeholder.
1577 PREV_STATE is the value returned by the call to
1578 start_underspecified_init. If NAME is NULL_TREE, this means a
1579 compound literal, as for start_underspecified_init. */
1580 void
1581 finish_underspecified_init (tree name, unsigned int prev_state)
1582 {
1583 if (name != NULL_TREE && (prev_state & 1))
1584 {
1585 /* A VAR_DECL was bound to the name to shadow any previous
1586 declarations for the name; remove that binding now. */
1587 struct c_scope *scope = current_scope;
1588 struct c_binding *b = I_SYMBOL_BINDING (name);
1589 gcc_assert (b);
1590 gcc_assert (B_IN_SCOPE (b, scope));
1591 gcc_assert (VAR_P (b->decl));
1592 gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1593 I_SYMBOL_BINDING (name) = b->shadowed;
1594 /* In erroneous cases there may be other bindings added to this
1595 scope during the initializer. */
1596 struct c_binding **p = &scope->bindings;
1597 while (*p != b)
1598 p = &((*p)->prev);
1599 *p = free_binding_and_advance (*p);
1600 }
1601 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1602 }
1603
1604 /* Adjust the bindings for the start of a statement expression. */
1605
1606 void
1607 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1608 {
1609 struct c_scope *scope;
1610
1611 for (scope = current_scope; scope != NULL; scope = scope->outer)
1612 {
1613 struct c_binding *b;
1614
1615 if (!scope->has_label_bindings)
1616 continue;
1617
1618 for (b = scope->bindings; b != NULL; b = b->prev)
1619 {
1620 struct c_label_vars *label_vars;
1621 unsigned int ix;
1622 struct c_goto_bindings *g;
1623
1624 if (TREE_CODE (b->decl) != LABEL_DECL)
1625 continue;
1626 label_vars = b->u.label;
1627 ++label_vars->label_bindings.stmt_exprs;
1628 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1629 ++g->goto_bindings.stmt_exprs;
1630 }
1631 }
1632
1633 if (switch_bindings != NULL)
1634 ++switch_bindings->stmt_exprs;
1635 }
1636
1637 /* Adjust the bindings for the end of a statement expression. */
1638
1639 void
1640 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1641 {
1642 struct c_scope *scope;
1643
1644 for (scope = current_scope; scope != NULL; scope = scope->outer)
1645 {
1646 struct c_binding *b;
1647
1648 if (!scope->has_label_bindings)
1649 continue;
1650
1651 for (b = scope->bindings; b != NULL; b = b->prev)
1652 {
1653 struct c_label_vars *label_vars;
1654 unsigned int ix;
1655 struct c_goto_bindings *g;
1656
1657 if (TREE_CODE (b->decl) != LABEL_DECL)
1658 continue;
1659 label_vars = b->u.label;
1660 --label_vars->label_bindings.stmt_exprs;
1661 if (label_vars->label_bindings.stmt_exprs < 0)
1662 {
1663 label_vars->label_bindings.left_stmt_expr = true;
1664 label_vars->label_bindings.stmt_exprs = 0;
1665 }
1666 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1667 {
1668 --g->goto_bindings.stmt_exprs;
1669 if (g->goto_bindings.stmt_exprs < 0)
1670 {
1671 g->goto_bindings.left_stmt_expr = true;
1672 g->goto_bindings.stmt_exprs = 0;
1673 }
1674 }
1675 }
1676 }
1677
1678 if (switch_bindings != NULL)
1679 {
1680 --switch_bindings->stmt_exprs;
1681 gcc_assert (switch_bindings->stmt_exprs >= 0);
1682 }
1683 }
1684
1685 /* Push a definition or a declaration of struct, union or enum tag "name".
1686 "type" should be the type node.
1687 We assume that the tag "name" is not already defined, and has a location
1688 of LOC.
1689
1690 Note that the definition may really be just a forward reference.
1691 In that case, the TYPE_SIZE will be zero. */
1692
1693 static void
1694 pushtag (location_t loc, tree name, tree type)
1695 {
1696 /* Record the identifier as the type's name if it has none. */
1697 if (name && !TYPE_NAME (type))
1698 TYPE_NAME (type) = name;
1699 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1700
1701 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1702 tagged type we just added to the current scope. This fake
1703 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1704 to output a representation of a tagged type, and it also gives
1705 us a convenient place to record the "scope start" address for the
1706 tagged type. */
1707
1708 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1709 TYPE_DECL, NULL_TREE, type));
1710
1711 /* An approximation for now, so we can tell this is a function-scope tag.
1712 This will be updated in pop_scope. */
1713 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1714
1715 if (warn_cxx_compat && name != NULL_TREE)
1716 {
1717 struct c_binding *b = I_SYMBOL_BINDING (name);
1718
1719 if (b != NULL
1720 && b->decl != NULL_TREE
1721 && TREE_CODE (b->decl) == TYPE_DECL
1722 && (B_IN_CURRENT_SCOPE (b)
1723 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1724 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1725 != TYPE_MAIN_VARIANT (type)))
1726 {
1727 auto_diagnostic_group d;
1728 if (warning_at (loc, OPT_Wc___compat,
1729 ("using %qD as both a typedef and a tag is "
1730 "invalid in C++"), b->decl)
1731 && b->locus != UNKNOWN_LOCATION)
1732 inform (b->locus, "originally defined here");
1733 }
1734 }
1735 }
1736
1737 /* An exported interface to pushtag. This is used by the gdb plugin's
1738 binding oracle to introduce a new tag binding. */
1739
1740 void
1741 c_pushtag (location_t loc, tree name, tree type)
1742 {
1743 pushtag (loc, name, type);
1744 }
1745
1746 /* An exported interface to bind a declaration. LOC is the location
1747 to use. DECL is the declaration to bind. The decl's name is used
1748 to determine how it is bound. If DECL is a VAR_DECL, then
1749 IS_GLOBAL determines whether the decl is put into the global (file
1750 and external) scope or the current function's scope; if DECL is not
1751 a VAR_DECL then it is always put into the file scope. */
1752
1753 void
1754 c_bind (location_t loc, tree decl, bool is_global)
1755 {
1756 struct c_scope *scope;
1757 bool nested = false;
1758
1759 if (!VAR_P (decl) || current_function_scope == NULL)
1760 {
1761 /* Types and functions are always considered to be global. */
1762 scope = file_scope;
1763 DECL_EXTERNAL (decl) = 1;
1764 TREE_PUBLIC (decl) = 1;
1765 }
1766 else if (is_global)
1767 {
1768 /* Also bind it into the external scope. */
1769 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1770 nested = true;
1771 scope = file_scope;
1772 DECL_EXTERNAL (decl) = 1;
1773 TREE_PUBLIC (decl) = 1;
1774 }
1775 else
1776 {
1777 DECL_CONTEXT (decl) = current_function_decl;
1778 TREE_PUBLIC (decl) = 0;
1779 scope = current_function_scope;
1780 }
1781
1782 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1783 }
1784
1785
1786 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1787 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1788 to the builtin_structptr_types array. Subsequent declarations of such
1789 built-ins are expected to refer to it rather than to fileptr_type_node,
1790 etc. which is just void* (or to any other type).
1791 Used only by match_builtin_function_types. */
1792
1793 static const unsigned builtin_structptr_type_count
1794 = ARRAY_SIZE (builtin_structptr_types);
1795
1796 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1797
1798 /* Returns true if types T1 and T2 representing return types or types
1799 of function arguments are close enough to be considered interchangeable
1800 in redeclarations of built-in functions. */
1801
1802 static bool
1803 types_close_enough_to_match (tree t1, tree t2)
1804 {
1805 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1806 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1807 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1808 }
1809
1810 /* Subroutine of compare_decls. Allow harmless mismatches in return
1811 and argument types provided that the type modes match. Set *STRICT
1812 and *ARGNO to the expected argument type and number in case of
1813 an argument type mismatch or null and zero otherwise. Return
1814 a unified type given a suitable match, and 0 otherwise. */
1815
1816 static tree
1817 match_builtin_function_types (tree newtype, tree oldtype,
1818 tree *strict, unsigned *argno)
1819 {
1820 *argno = 0;
1821 *strict = NULL_TREE;
1822
1823 /* Accept the return type of the new declaration if it has the same
1824 mode and if they're both pointers or if neither is. */
1825 tree oldrettype = TREE_TYPE (oldtype);
1826 tree newrettype = TREE_TYPE (newtype);
1827
1828 if (!types_close_enough_to_match (oldrettype, newrettype))
1829 return NULL_TREE;
1830
1831 /* Check that the return types are compatible but don't fail if they
1832 are not (e.g., int vs long in ILP32) and just let the caller know. */
1833 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1834 TYPE_MAIN_VARIANT (newrettype)))
1835 *strict = oldrettype;
1836
1837 tree oldargs = TYPE_ARG_TYPES (oldtype);
1838 tree newargs = TYPE_ARG_TYPES (newtype);
1839 tree tryargs = newargs;
1840
1841 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1842 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1843
1844 gcc_checking_assert (nlst == nbst);
1845
1846 for (unsigned i = 1; oldargs || newargs; ++i)
1847 {
1848 if (!oldargs
1849 || !newargs
1850 || !TREE_VALUE (oldargs)
1851 || !TREE_VALUE (newargs))
1852 return NULL_TREE;
1853
1854 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1855 tree newtype = TREE_VALUE (newargs);
1856 if (newtype == error_mark_node)
1857 return NULL_TREE;
1858 newtype = TYPE_MAIN_VARIANT (newtype);
1859
1860 if (!types_close_enough_to_match (oldtype, newtype))
1861 return NULL_TREE;
1862
1863 unsigned j = nbst;
1864 if (POINTER_TYPE_P (oldtype))
1865 /* Iterate over well-known struct types like FILE (whose types
1866 aren't known to us) and compare the pointer to each to
1867 the pointer argument. */
1868 for (j = 0; j < nbst; ++j)
1869 {
1870 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1871 continue;
1872 /* Store the first FILE* etc. argument type (whatever it is), and
1873 expect any subsequent declarations of file I/O etc. built-ins
1874 to refer to it rather than to fileptr_type_node etc. which is
1875 just void* (or const void*). */
1876 if (last_structptr_types[j])
1877 {
1878 if (!comptypes (last_structptr_types[j], newtype))
1879 {
1880 *argno = i;
1881 *strict = last_structptr_types[j];
1882 }
1883 }
1884 else
1885 last_structptr_types[j] = newtype;
1886 break;
1887 }
1888
1889 if (j == nbst && !comptypes (oldtype, newtype))
1890 {
1891 if (POINTER_TYPE_P (oldtype))
1892 {
1893 /* For incompatible pointers, only reject differences in
1894 the unqualified variants of the referenced types but
1895 consider differences in qualifiers as benign (report
1896 those to caller via *STRICT below). */
1897 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1898 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1899 if (!comptypes (oldref, newref))
1900 return NULL_TREE;
1901 }
1902
1903 if (!*strict)
1904 {
1905 *argno = i;
1906 *strict = oldtype;
1907 }
1908 }
1909
1910 oldargs = TREE_CHAIN (oldargs);
1911 newargs = TREE_CHAIN (newargs);
1912 }
1913
1914 tree trytype = c_build_function_type (newrettype, tryargs);
1915
1916 /* Allow declaration to change transaction_safe attribute. */
1917 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1918 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1919 tree newattrs = TYPE_ATTRIBUTES (newtype);
1920 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1921 if (oldtsafe && !newtsafe)
1922 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1923 else if (newtsafe && !oldtsafe)
1924 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1925 NULL_TREE, oldattrs);
1926
1927 return c_build_type_attribute_variant (trytype, oldattrs);
1928 }
1929
1930 /* Subroutine of diagnose_mismatched_decls. Check for function type
1931 mismatch involving an empty arglist vs a nonempty one and give clearer
1932 diagnostics. */
1933 static void
1934 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1935 tree newtype, tree oldtype)
1936 {
1937 tree t;
1938
1939 if (TREE_CODE (olddecl) != FUNCTION_DECL
1940 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1941 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1942 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1943 return;
1944
1945 t = TYPE_ARG_TYPES (oldtype);
1946 if (t == NULL_TREE)
1947 t = TYPE_ARG_TYPES (newtype);
1948 for (; t; t = TREE_CHAIN (t))
1949 {
1950 tree type = TREE_VALUE (t);
1951
1952 if (TREE_CHAIN (t) == NULL_TREE
1953 && TYPE_MAIN_VARIANT (type) != void_type_node)
1954 {
1955 inform (input_location, "a parameter list with an ellipsis "
1956 "cannot match an empty parameter name list declaration");
1957 break;
1958 }
1959
1960 if (!error_operand_p (type)
1961 && c_type_promotes_to (type) != type)
1962 {
1963 inform (input_location, "an argument type that has a default "
1964 "promotion cannot match an empty parameter name list "
1965 "declaration");
1966 break;
1967 }
1968 }
1969 }
1970
1971 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1972 old-style function definition, NEWDECL is a prototype declaration.
1973 Diagnose inconsistencies in the argument list. Returns TRUE if
1974 the prototype is compatible, FALSE if not. */
1975 static bool
1976 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1977 {
1978 tree newargs, oldargs;
1979 int i;
1980
1981 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1982
1983 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1984 newargs = TYPE_ARG_TYPES (newtype);
1985 i = 1;
1986
1987 for (;;)
1988 {
1989 tree oldargtype = TREE_VALUE (oldargs);
1990 tree newargtype = TREE_VALUE (newargs);
1991
1992 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1993 return false;
1994
1995 oldargtype = (TYPE_ATOMIC (oldargtype)
1996 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1997 TYPE_QUAL_ATOMIC)
1998 : TYPE_MAIN_VARIANT (oldargtype));
1999 newargtype = (TYPE_ATOMIC (newargtype)
2000 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2001 TYPE_QUAL_ATOMIC)
2002 : TYPE_MAIN_VARIANT (newargtype));
2003
2004 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2005 break;
2006
2007 /* Reaching the end of just one list means the two decls don't
2008 agree on the number of arguments. */
2009 if (END_OF_ARGLIST (oldargtype))
2010 {
2011 error ("prototype for %q+D declares more arguments "
2012 "than previous old-style definition", newdecl);
2013 return false;
2014 }
2015 else if (END_OF_ARGLIST (newargtype))
2016 {
2017 error ("prototype for %q+D declares fewer arguments "
2018 "than previous old-style definition", newdecl);
2019 return false;
2020 }
2021
2022 /* Type for passing arg must be consistent with that declared
2023 for the arg. */
2024 else if (!comptypes (oldargtype, newargtype))
2025 {
2026 error ("prototype for %q+D declares argument %d"
2027 " with incompatible type",
2028 newdecl, i);
2029 return false;
2030 }
2031
2032 oldargs = TREE_CHAIN (oldargs);
2033 newargs = TREE_CHAIN (newargs);
2034 i++;
2035 }
2036
2037 /* If we get here, no errors were found, but do issue a warning
2038 for this poor-style construct. */
2039 warning (0, "prototype for %q+D follows non-prototype definition",
2040 newdecl);
2041 return true;
2042 #undef END_OF_ARGLIST
2043 }
2044
2045 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2046 first in a pair of mismatched declarations, using the diagnostic
2047 function DIAG. */
2048 static void
2049 locate_old_decl (tree decl)
2050 {
2051 if (TREE_CODE (decl) == FUNCTION_DECL
2052 && fndecl_built_in_p (decl)
2053 && !C_DECL_DECLARED_BUILTIN (decl))
2054 ;
2055 else if (DECL_INITIAL (decl))
2056 inform (input_location,
2057 "previous definition of %q+D with type %qT",
2058 decl, TREE_TYPE (decl));
2059 else if (C_DECL_IMPLICIT (decl))
2060 inform (input_location,
2061 "previous implicit declaration of %q+D with type %qT",
2062 decl, TREE_TYPE (decl));
2063 else
2064 inform (input_location,
2065 "previous declaration of %q+D with type %qT",
2066 decl, TREE_TYPE (decl));
2067 }
2068
2069
2070 /* Helper function. For a tagged type, it finds the declaration
2071 for a visible tag declared in the same scope if such a
2072 declaration exists. */
2073 static tree
2074 previous_tag (tree type)
2075 {
2076 struct c_binding *b = NULL;
2077 tree name = c_type_tag (type);
2078
2079 if (name)
2080 b = I_TAG_BINDING (name);
2081
2082 if (b)
2083 b = b->shadowed;
2084
2085 if (b && B_IN_CURRENT_SCOPE (b))
2086 return b->decl;
2087
2088 return NULL_TREE;
2089 }
2090
2091 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2092 Returns true if the caller should proceed to merge the two, false
2093 if OLDDECL should simply be discarded. As a side effect, issues
2094 all necessary diagnostics for invalid or poor-style combinations.
2095 If it returns true, writes the types of NEWDECL and OLDDECL to
2096 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2097 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2098
2099 static bool
2100 diagnose_mismatched_decls (tree newdecl, tree olddecl,
2101 tree *newtypep, tree *oldtypep)
2102 {
2103 tree newtype, oldtype;
2104 bool retval = true;
2105
2106 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2107 && DECL_EXTERNAL (DECL))
2108
2109 /* If we have error_mark_node for either decl or type, just discard
2110 the previous decl - we're in an error cascade already. */
2111 if (olddecl == error_mark_node || newdecl == error_mark_node)
2112 return false;
2113 *oldtypep = oldtype = TREE_TYPE (olddecl);
2114 *newtypep = newtype = TREE_TYPE (newdecl);
2115 if (oldtype == error_mark_node || newtype == error_mark_node)
2116 return false;
2117
2118 /* Two different categories of symbol altogether. This is an error
2119 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2120 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2121 {
2122 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2123 && fndecl_built_in_p (olddecl)
2124 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2125 {
2126 auto_diagnostic_group d;
2127 error ("%q+D redeclared as different kind of symbol", newdecl);
2128 locate_old_decl (olddecl);
2129 }
2130 else if (TREE_PUBLIC (newdecl))
2131 warning (OPT_Wbuiltin_declaration_mismatch,
2132 "built-in function %q+D declared as non-function",
2133 newdecl);
2134 else
2135 warning (OPT_Wshadow, "declaration of %q+D shadows "
2136 "a built-in function", newdecl);
2137 return false;
2138 }
2139
2140 /* Enumerators have no linkage, so may only be declared once in a
2141 given scope. */
2142 if (TREE_CODE (olddecl) == CONST_DECL)
2143 {
2144 if (flag_isoc23
2145 && TYPE_NAME (DECL_CONTEXT (newdecl))
2146 && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2147 && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2148 {
2149 if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2150 {
2151 auto_diagnostic_group d;
2152 error ("conflicting redeclaration of enumerator %q+D", newdecl);
2153 locate_old_decl (olddecl);
2154 }
2155 }
2156 else
2157 {
2158 auto_diagnostic_group d;
2159 error ("redeclaration of enumerator %q+D", newdecl);
2160 locate_old_decl (olddecl);
2161 }
2162 return false;
2163 }
2164
2165 bool pedwarned = false;
2166 bool warned = false;
2167 bool enum_and_int_p = false;
2168 auto_diagnostic_group d;
2169
2170 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2171 &enum_and_int_p);
2172 if (!comptypes_result)
2173 {
2174 if (TREE_CODE (olddecl) == FUNCTION_DECL
2175 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2176 && !C_DECL_DECLARED_BUILTIN (olddecl))
2177 {
2178 /* Accept "harmless" mismatches in function types such
2179 as missing qualifiers or int vs long when they're the same
2180 size. However, diagnose return and argument types that are
2181 incompatible according to language rules. */
2182 tree mismatch_expect;
2183 unsigned mismatch_argno;
2184
2185 tree trytype = match_builtin_function_types (newtype, oldtype,
2186 &mismatch_expect,
2187 &mismatch_argno);
2188
2189 if (trytype && comptypes (newtype, trytype))
2190 *oldtypep = oldtype = trytype;
2191 else
2192 {
2193 /* If types don't match for a built-in, throw away the
2194 built-in. No point in calling locate_old_decl here, it
2195 won't print anything. */
2196 const char *header = header_for_builtin_fn (olddecl);
2197 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2198 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2199 "conflicting types for built-in function %q+D; "
2200 "expected %qT",
2201 newdecl, oldtype)
2202 && header)
2203 {
2204 /* Suggest the right header to include as the preferred
2205 solution rather than the spelling of the declaration. */
2206 rich_location richloc (line_table, loc);
2207 maybe_add_include_fixit (&richloc, header, true);
2208 inform (&richloc,
2209 "%qD is declared in header %qs", olddecl, header);
2210 }
2211 return false;
2212 }
2213
2214 if (mismatch_expect && extra_warnings)
2215 {
2216 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2217 bool warned = false;
2218 if (mismatch_argno)
2219 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2220 "mismatch in argument %u type of built-in "
2221 "function %qD; expected %qT",
2222 mismatch_argno, newdecl, mismatch_expect);
2223 else
2224 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2225 "mismatch in return type of built-in "
2226 "function %qD; expected %qT",
2227 newdecl, mismatch_expect);
2228 const char *header = header_for_builtin_fn (olddecl);
2229 if (warned && header)
2230 {
2231 rich_location richloc (line_table, newloc);
2232 maybe_add_include_fixit (&richloc, header, true);
2233 inform (&richloc,
2234 "%qD is declared in header %qs", olddecl, header);
2235 }
2236 }
2237 }
2238 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2239 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2240 {
2241 /* A conflicting function declaration for a predeclared
2242 function that isn't actually built in. Objective C uses
2243 these. The new declaration silently overrides everything
2244 but the volatility (i.e. noreturn) indication. See also
2245 below. FIXME: Make Objective C use normal builtins. */
2246 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2247 return false;
2248 }
2249 /* Permit void foo (...) to match int foo (...) if the latter is
2250 the definition and implicit int was used. See
2251 c-torture/compile/920625-2.c. */
2252 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2253 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2254 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2255 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2256 {
2257 pedwarned = pedwarn (input_location, 0,
2258 "conflicting types for %q+D", newdecl);
2259 /* Make sure we keep void as the return type. */
2260 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2261 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2262 }
2263 /* Permit void foo (...) to match an earlier call to foo (...) with
2264 no declared type (thus, implicitly int). */
2265 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2266 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2267 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2268 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2269 {
2270 pedwarned = pedwarn (input_location, 0,
2271 "conflicting types for %q+D; have %qT",
2272 newdecl, newtype);
2273 /* Make sure we keep void as the return type. */
2274 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2275 }
2276 else
2277 {
2278 int new_quals = TYPE_QUALS (newtype);
2279 int old_quals = TYPE_QUALS (oldtype);
2280
2281 if (new_quals != old_quals)
2282 {
2283 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2284 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2285 if (new_addr != old_addr)
2286 {
2287 if (ADDR_SPACE_GENERIC_P (new_addr))
2288 error ("conflicting named address spaces (generic vs %s) "
2289 "for %q+D",
2290 c_addr_space_name (old_addr), newdecl);
2291 else if (ADDR_SPACE_GENERIC_P (old_addr))
2292 error ("conflicting named address spaces (%s vs generic) "
2293 "for %q+D",
2294 c_addr_space_name (new_addr), newdecl);
2295 else
2296 error ("conflicting named address spaces (%s vs %s) "
2297 "for %q+D",
2298 c_addr_space_name (new_addr),
2299 c_addr_space_name (old_addr),
2300 newdecl);
2301 }
2302
2303 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2304 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2305 error ("conflicting type qualifiers for %q+D", newdecl);
2306 }
2307 else
2308 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2309 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2310 locate_old_decl (olddecl);
2311 return false;
2312 }
2313 }
2314 /* Warn about enum/integer type mismatches. They are compatible types
2315 (C23 6.7.2.2/5), but may pose portability problems. */
2316 else if (enum_and_int_p
2317 && TREE_CODE (newdecl) != TYPE_DECL
2318 /* Don't warn about acc_on_device built-in redeclaration,
2319 the built-in is declared with int rather than enum because
2320 the enum isn't intrinsic. */
2321 && !(TREE_CODE (olddecl) == FUNCTION_DECL
2322 && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2323 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2324 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2325 OPT_Wenum_int_mismatch,
2326 "conflicting types for %q+D due to enum/integer "
2327 "mismatch; have %qT", newdecl, newtype);
2328
2329 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2330 but silently ignore the redeclaration if either is in a system
2331 header. (Conflicting redeclarations were handled above.) This
2332 is allowed for C11 if the types are the same, not just
2333 compatible. */
2334 if (TREE_CODE (newdecl) == TYPE_DECL)
2335 {
2336 bool types_different = false;
2337
2338 comptypes_result
2339 = comptypes_check_different_types (oldtype, newtype, &types_different);
2340
2341 if (comptypes_result != 1 || types_different)
2342 {
2343 error ("redefinition of typedef %q+D with different type", newdecl);
2344 locate_old_decl (olddecl);
2345 return false;
2346 }
2347
2348 if (DECL_IN_SYSTEM_HEADER (newdecl)
2349 || DECL_IN_SYSTEM_HEADER (olddecl)
2350 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2351 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2352 return true; /* Allow OLDDECL to continue in use. */
2353
2354 if (c_type_variably_modified_p (newtype))
2355 {
2356 error ("redefinition of typedef %q+D with variably modified type",
2357 newdecl);
2358 locate_old_decl (olddecl);
2359 }
2360 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2361 "redefinition of typedef %q+D", newdecl))
2362 locate_old_decl (olddecl);
2363
2364 return true;
2365 }
2366
2367 /* Function declarations can either be 'static' or 'extern' (no
2368 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2369 can never conflict with each other on account of linkage
2370 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2371 gnu89 mode permits two definitions if one is 'extern inline' and
2372 one is not. The non- extern-inline definition supersedes the
2373 extern-inline definition. */
2374
2375 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2376 {
2377 /* If you declare a built-in function name as static, or
2378 define the built-in with an old-style definition (so we
2379 can't validate the argument list) the built-in definition is
2380 overridden, but optionally warn this was a bad choice of name. */
2381 if (fndecl_built_in_p (olddecl)
2382 && !C_DECL_DECLARED_BUILTIN (olddecl))
2383 {
2384 if (!TREE_PUBLIC (newdecl)
2385 || (DECL_INITIAL (newdecl)
2386 && !prototype_p (TREE_TYPE (newdecl))))
2387 {
2388 warning_at (DECL_SOURCE_LOCATION (newdecl),
2389 OPT_Wshadow, "declaration of %qD shadows "
2390 "a built-in function", newdecl);
2391 /* Discard the old built-in function. */
2392 return false;
2393 }
2394
2395 if (!prototype_p (TREE_TYPE (newdecl)))
2396 {
2397 /* Set for built-ins that take no arguments. */
2398 bool func_void_args = false;
2399 if (tree at = TYPE_ARG_TYPES (oldtype))
2400 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2401
2402 if (extra_warnings && !func_void_args)
2403 warning_at (DECL_SOURCE_LOCATION (newdecl),
2404 OPT_Wbuiltin_declaration_mismatch,
2405 "declaration of built-in function %qD without "
2406 "a prototype; expected %qT",
2407 newdecl, TREE_TYPE (olddecl));
2408 }
2409 }
2410
2411 if (DECL_INITIAL (newdecl))
2412 {
2413 if (DECL_INITIAL (olddecl))
2414 {
2415 /* If the new declaration isn't overriding an extern inline
2416 reject the new decl. In c99, no overriding is allowed
2417 in the same translation unit. */
2418 if (!DECL_EXTERN_INLINE (olddecl)
2419 || DECL_EXTERN_INLINE (newdecl)
2420 || (!flag_gnu89_inline
2421 && (!DECL_DECLARED_INLINE_P (olddecl)
2422 || !lookup_attribute ("gnu_inline",
2423 DECL_ATTRIBUTES (olddecl)))
2424 && (!DECL_DECLARED_INLINE_P (newdecl)
2425 || !lookup_attribute ("gnu_inline",
2426 DECL_ATTRIBUTES (newdecl)))))
2427 {
2428 auto_diagnostic_group d;
2429 error ("redefinition of %q+D", newdecl);
2430 locate_old_decl (olddecl);
2431 return false;
2432 }
2433 }
2434 }
2435 /* If we have a prototype after an old-style function definition,
2436 the argument types must be checked specially. */
2437 else if (DECL_INITIAL (olddecl)
2438 && !prototype_p (oldtype) && prototype_p (newtype)
2439 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2440 {
2441 auto_diagnostic_group d;
2442 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2443 {
2444 locate_old_decl (olddecl);
2445 return false;
2446 }
2447 }
2448 /* A non-static declaration (even an "extern") followed by a
2449 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2450 The same is true for a static forward declaration at block
2451 scope followed by a non-static declaration/definition at file
2452 scope. Static followed by non-static at the same scope is
2453 not undefined behavior, and is the most convenient way to get
2454 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2455 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2456 we do diagnose it if -Wtraditional. */
2457 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2458 {
2459 /* Two exceptions to the rule. If olddecl is an extern
2460 inline, or a predeclared function that isn't actually
2461 built in, newdecl silently overrides olddecl. The latter
2462 occur only in Objective C; see also above. (FIXME: Make
2463 Objective C use normal builtins.) */
2464 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2465 && !DECL_EXTERN_INLINE (olddecl))
2466 {
2467 auto_diagnostic_group d;
2468 error ("static declaration of %q+D follows "
2469 "non-static declaration", newdecl);
2470 locate_old_decl (olddecl);
2471 }
2472 return false;
2473 }
2474 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2475 {
2476 if (DECL_CONTEXT (olddecl))
2477 {
2478 auto_diagnostic_group d;
2479 error ("non-static declaration of %q+D follows "
2480 "static declaration", newdecl);
2481 locate_old_decl (olddecl);
2482 return false;
2483 }
2484 else if (warn_traditional)
2485 {
2486 warned |= warning (OPT_Wtraditional,
2487 "non-static declaration of %q+D "
2488 "follows static declaration", newdecl);
2489 }
2490 }
2491
2492 /* Make sure gnu_inline attribute is either not present, or
2493 present on all inline decls. */
2494 if (DECL_DECLARED_INLINE_P (olddecl)
2495 && DECL_DECLARED_INLINE_P (newdecl))
2496 {
2497 bool newa = lookup_attribute ("gnu_inline",
2498 DECL_ATTRIBUTES (newdecl)) != NULL;
2499 bool olda = lookup_attribute ("gnu_inline",
2500 DECL_ATTRIBUTES (olddecl)) != NULL;
2501 if (newa != olda)
2502 {
2503 auto_diagnostic_group d;
2504 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2505 newa ? newdecl : olddecl);
2506 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2507 "but not here");
2508 }
2509 }
2510 }
2511 else if (VAR_P (newdecl))
2512 {
2513 /* Only variables can be thread-local, and all declarations must
2514 agree on this property. */
2515 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2516 {
2517 /* Nothing to check. Since OLDDECL is marked threadprivate
2518 and NEWDECL does not have a thread-local attribute, we
2519 will merge the threadprivate attribute into NEWDECL. */
2520 ;
2521 }
2522 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2523 {
2524 auto_diagnostic_group d;
2525 if (DECL_THREAD_LOCAL_P (newdecl))
2526 error ("thread-local declaration of %q+D follows "
2527 "non-thread-local declaration", newdecl);
2528 else
2529 error ("non-thread-local declaration of %q+D follows "
2530 "thread-local declaration", newdecl);
2531
2532 locate_old_decl (olddecl);
2533 return false;
2534 }
2535
2536 /* Multiple initialized definitions are not allowed (6.9p3,5).
2537 For this purpose, C23 makes it clear that thread-local
2538 declarations without extern are definitions, not tentative
2539 definitions, whether or not they have initializers. The
2540 wording before C23 was unclear; literally it would have made
2541 uninitialized thread-local declarations into tentative
2542 definitions only if they also used static, but without saying
2543 explicitly whether or not other cases count as
2544 definitions at all. */
2545 if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2546 || (flag_isoc23
2547 && DECL_THREAD_LOCAL_P (newdecl)
2548 && !DECL_EXTERNAL (newdecl)
2549 && !DECL_EXTERNAL (olddecl)))
2550 {
2551 auto_diagnostic_group d;
2552 error ("redefinition of %q+D", newdecl);
2553 locate_old_decl (olddecl);
2554 return false;
2555 }
2556
2557 /* Objects declared at file scope: if the first declaration had
2558 external linkage (even if it was an external reference) the
2559 second must have external linkage as well, or the behavior is
2560 undefined. If the first declaration had internal linkage, then
2561 the second must too, or else be an external reference (in which
2562 case the composite declaration still has internal linkage).
2563 As for function declarations, we warn about the static-then-
2564 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2565 if (DECL_FILE_SCOPE_P (newdecl)
2566 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2567 {
2568 if (DECL_EXTERNAL (newdecl))
2569 {
2570 if (!DECL_FILE_SCOPE_P (olddecl))
2571 {
2572 auto_diagnostic_group d;
2573 error ("extern declaration of %q+D follows "
2574 "declaration with no linkage", newdecl);
2575 locate_old_decl (olddecl);
2576 return false;
2577 }
2578 else if (warn_traditional)
2579 {
2580 warned |= warning (OPT_Wtraditional,
2581 "non-static declaration of %q+D "
2582 "follows static declaration", newdecl);
2583 }
2584 }
2585 else
2586 {
2587 auto_diagnostic_group d;
2588 if (TREE_PUBLIC (newdecl))
2589 error ("non-static declaration of %q+D follows "
2590 "static declaration", newdecl);
2591 else
2592 error ("static declaration of %q+D follows "
2593 "non-static declaration", newdecl);
2594
2595 locate_old_decl (olddecl);
2596 return false;
2597 }
2598 }
2599 /* Two objects with the same name declared at the same block
2600 scope must both be external references (6.7p3). */
2601 else if (!DECL_FILE_SCOPE_P (newdecl))
2602 {
2603 if (DECL_EXTERNAL (newdecl))
2604 {
2605 /* Extern with initializer at block scope, which will
2606 already have received an error. */
2607 }
2608 else if (DECL_EXTERNAL (olddecl))
2609 {
2610 auto_diagnostic_group d;
2611 error ("declaration of %q+D with no linkage follows "
2612 "extern declaration", newdecl);
2613 locate_old_decl (olddecl);
2614 }
2615 else
2616 {
2617 auto_diagnostic_group d;
2618 error ("redeclaration of %q+D with no linkage", newdecl);
2619 locate_old_decl (olddecl);
2620 }
2621
2622 return false;
2623 }
2624
2625 /* C++ does not permit a decl to appear multiple times at file
2626 scope. */
2627 if (warn_cxx_compat
2628 && DECL_FILE_SCOPE_P (newdecl)
2629 && !DECL_EXTERNAL (newdecl)
2630 && !DECL_EXTERNAL (olddecl))
2631 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2632 OPT_Wc___compat,
2633 ("duplicate declaration of %qD is "
2634 "invalid in C++"),
2635 newdecl);
2636 }
2637
2638 /* warnings */
2639 /* All decls must agree on a visibility. */
2640 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2641 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2642 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2643 {
2644 warned |= warning (0, "redeclaration of %q+D with different visibility "
2645 "(old visibility preserved)", newdecl);
2646 }
2647
2648 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2649 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2650 else /* PARM_DECL, VAR_DECL */
2651 {
2652 /* Redeclaration of a parameter is a constraint violation (this is
2653 not explicitly stated, but follows from C99 6.7p3 [no more than
2654 one declaration of the same identifier with no linkage in the
2655 same scope, except type tags] and 6.2.2p6 [parameters have no
2656 linkage]). We must check for a forward parameter declaration,
2657 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2658 an extension, the mandatory diagnostic for which is handled by
2659 mark_forward_parm_decls. */
2660
2661 if (TREE_CODE (newdecl) == PARM_DECL
2662 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2663 {
2664 auto_diagnostic_group d;
2665 error ("redefinition of parameter %q+D", newdecl);
2666 locate_old_decl (olddecl);
2667 return false;
2668 }
2669 }
2670
2671 /* Optional warning for completely redundant decls. */
2672 if (!warned && !pedwarned
2673 && warn_redundant_decls
2674 /* Don't warn about a function declaration followed by a
2675 definition. */
2676 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2677 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2678 /* Don't warn about redundant redeclarations of builtins. */
2679 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2680 && !fndecl_built_in_p (newdecl)
2681 && fndecl_built_in_p (olddecl)
2682 && !C_DECL_DECLARED_BUILTIN (olddecl))
2683 /* Don't warn about an extern followed by a definition. */
2684 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2685 /* Don't warn about forward parameter decls. */
2686 && !(TREE_CODE (newdecl) == PARM_DECL
2687 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2688 /* Don't warn about a variable definition following a declaration. */
2689 && !(VAR_P (newdecl)
2690 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2691 {
2692 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2693 newdecl);
2694 }
2695
2696 /* Report location of previous decl/defn. */
2697 if (warned || pedwarned)
2698 locate_old_decl (olddecl);
2699
2700 #undef DECL_EXTERN_INLINE
2701
2702 return retval;
2703 }
2704
2705 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2706 consistent with OLDDECL, but carries new information. Merge the
2707 new information into OLDDECL. This function issues no
2708 diagnostics. */
2709
2710 static void
2711 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2712 {
2713 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2714 && DECL_INITIAL (newdecl) != NULL_TREE);
2715 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2716 && prototype_p (TREE_TYPE (newdecl)));
2717 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2718 && prototype_p (TREE_TYPE (olddecl)));
2719
2720 /* For real parm decl following a forward decl, rechain the old decl
2721 in its new location and clear TREE_ASM_WRITTEN (it's not a
2722 forward decl anymore). */
2723 if (TREE_CODE (newdecl) == PARM_DECL
2724 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2725 {
2726 struct c_binding *b, **here;
2727
2728 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2729 if ((*here)->decl == olddecl)
2730 goto found;
2731 gcc_unreachable ();
2732
2733 found:
2734 b = *here;
2735 *here = b->prev;
2736 b->prev = current_scope->bindings;
2737 current_scope->bindings = b;
2738
2739 TREE_ASM_WRITTEN (olddecl) = 0;
2740 }
2741
2742 DECL_ATTRIBUTES (newdecl)
2743 = targetm.merge_decl_attributes (olddecl, newdecl);
2744
2745 /* For typedefs use the old type, as the new type's DECL_NAME points
2746 at newdecl, which will be ggc_freed. */
2747 if (TREE_CODE (newdecl) == TYPE_DECL)
2748 {
2749 /* But NEWTYPE might have an attribute, honor that. */
2750 tree tem = newtype;
2751 newtype = oldtype;
2752
2753 if (TYPE_USER_ALIGN (tem))
2754 {
2755 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2756 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2757 TYPE_USER_ALIGN (newtype) = true;
2758 }
2759
2760 /* And remove the new type from the variants list. */
2761 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2762 {
2763 tree remove = TREE_TYPE (newdecl);
2764 if (TYPE_MAIN_VARIANT (remove) == remove)
2765 {
2766 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2767 /* If remove is the main variant, no need to remove that
2768 from the list. One of the DECL_ORIGINAL_TYPE
2769 variants, e.g. created for aligned attribute, might still
2770 refer to the newdecl TYPE_DECL though, so remove that one
2771 in that case. */
2772 if (DECL_ORIGINAL_TYPE (newdecl)
2773 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2774 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2775 t; t = TYPE_MAIN_VARIANT (t))
2776 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2777 {
2778 TYPE_NEXT_VARIANT (t)
2779 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2780 break;
2781 }
2782 }
2783 else
2784 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2785 t = TYPE_NEXT_VARIANT (t))
2786 if (TYPE_NEXT_VARIANT (t) == remove)
2787 {
2788 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2789 break;
2790 }
2791 }
2792
2793 /* Make sure we refer to the same type as the olddecl. */
2794 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2795 }
2796
2797 /* Merge the data types specified in the two decls. */
2798 TREE_TYPE (newdecl)
2799 = TREE_TYPE (olddecl)
2800 = composite_type (newtype, oldtype);
2801
2802 /* Lay the type out, unless already done. */
2803 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2804 {
2805 if (TREE_TYPE (newdecl) != error_mark_node)
2806 layout_type (TREE_TYPE (newdecl));
2807 if (TREE_CODE (newdecl) != FUNCTION_DECL
2808 && TREE_CODE (newdecl) != TYPE_DECL
2809 && TREE_CODE (newdecl) != CONST_DECL)
2810 layout_decl (newdecl, 0);
2811 }
2812 else
2813 {
2814 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2815 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2816 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2817 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2818 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2819 {
2820 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2821 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2822 }
2823 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2824 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2825 DECL_USER_ALIGN (newdecl) = 1;
2826 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2827 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2828 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2829 DECL_WARN_IF_NOT_ALIGN (olddecl));
2830 }
2831
2832 /* Keep the old rtl since we can safely use it. */
2833 if (HAS_RTL_P (olddecl))
2834 COPY_DECL_RTL (olddecl, newdecl);
2835
2836 /* Merge the type qualifiers. */
2837 if (TREE_READONLY (newdecl))
2838 TREE_READONLY (olddecl) = 1;
2839
2840 if (TREE_THIS_VOLATILE (newdecl))
2841 TREE_THIS_VOLATILE (olddecl) = 1;
2842
2843 /* Merge deprecatedness. */
2844 if (TREE_DEPRECATED (newdecl))
2845 TREE_DEPRECATED (olddecl) = 1;
2846
2847 /* Merge unavailability. */
2848 if (TREE_UNAVAILABLE (newdecl))
2849 TREE_UNAVAILABLE (olddecl) = 1;
2850
2851 /* If a decl is in a system header and the other isn't, keep the one on the
2852 system header. Otherwise, keep source location of definition rather than
2853 declaration and of prototype rather than non-prototype unless that
2854 prototype is built-in. */
2855 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2856 && DECL_IN_SYSTEM_HEADER (olddecl)
2857 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2858 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2859 else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2860 && DECL_IN_SYSTEM_HEADER (newdecl)
2861 && !DECL_IN_SYSTEM_HEADER (olddecl))
2862 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2863 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2864 && DECL_INITIAL (olddecl) != NULL_TREE)
2865 || (old_is_prototype && !new_is_prototype
2866 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2867 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2868
2869 /* Merge the initialization information. */
2870 if (DECL_INITIAL (newdecl) == NULL_TREE)
2871 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2872
2873 /* Merge 'constexpr' information. */
2874 if (VAR_P (olddecl) && VAR_P (newdecl))
2875 {
2876 if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2877 C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2878 else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2879 C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2880 }
2881
2882 /* Merge the threadprivate attribute. */
2883 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2884 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2885
2886 if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2887 {
2888 /* Copy the assembler name.
2889 Currently, it can only be defined in the prototype. */
2890 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2891
2892 /* Use visibility of whichever declaration had it specified */
2893 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2894 {
2895 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2896 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2897 }
2898
2899 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2900 {
2901 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2902 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2903 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2904 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2905 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2906 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2907 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2908 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2909 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2910 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2911 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2912 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2913 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2914 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2915 }
2916
2917 /* Merge the storage class information. */
2918 merge_weak (newdecl, olddecl);
2919
2920 /* For functions, static overrides non-static. */
2921 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2922 {
2923 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2924 /* This is since we don't automatically
2925 copy the attributes of NEWDECL into OLDDECL. */
2926 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2927 /* If this clears `static', clear it in the identifier too. */
2928 if (!TREE_PUBLIC (olddecl))
2929 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2930 }
2931 }
2932
2933 /* In c99, 'extern' declaration before (or after) 'inline' means this
2934 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2935 is present. */
2936 if (TREE_CODE (newdecl) == FUNCTION_DECL
2937 && !flag_gnu89_inline
2938 && (DECL_DECLARED_INLINE_P (newdecl)
2939 || DECL_DECLARED_INLINE_P (olddecl))
2940 && (!DECL_DECLARED_INLINE_P (newdecl)
2941 || !DECL_DECLARED_INLINE_P (olddecl)
2942 || !DECL_EXTERNAL (olddecl))
2943 && DECL_EXTERNAL (newdecl)
2944 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2945 && !current_function_decl)
2946 DECL_EXTERNAL (newdecl) = 0;
2947
2948 /* An inline definition following a static declaration is not
2949 DECL_EXTERNAL. */
2950 if (new_is_definition
2951 && (DECL_DECLARED_INLINE_P (newdecl)
2952 || DECL_DECLARED_INLINE_P (olddecl))
2953 && !TREE_PUBLIC (olddecl))
2954 DECL_EXTERNAL (newdecl) = 0;
2955
2956 if (DECL_EXTERNAL (newdecl))
2957 {
2958 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2959 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2960
2961 /* An extern decl does not override previous storage class. */
2962 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2963 if (!DECL_EXTERNAL (newdecl))
2964 {
2965 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2966 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2967 }
2968 }
2969 else
2970 {
2971 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2972 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2973 }
2974
2975 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2976 {
2977 /* If we're redefining a function previously defined as extern
2978 inline, make sure we emit debug info for the inline before we
2979 throw it away, in case it was inlined into a function that
2980 hasn't been written out yet. */
2981 if (new_is_definition && DECL_INITIAL (olddecl))
2982 /* The new defn must not be inline. */
2983 DECL_UNINLINABLE (newdecl) = 1;
2984 else
2985 {
2986 /* If either decl says `inline', this fn is inline, unless
2987 its definition was passed already. */
2988 if (DECL_DECLARED_INLINE_P (newdecl)
2989 || DECL_DECLARED_INLINE_P (olddecl))
2990 DECL_DECLARED_INLINE_P (newdecl) = 1;
2991
2992 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2993 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2994
2995 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2996 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2997 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2998 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2999 }
3000
3001 if (fndecl_built_in_p (olddecl))
3002 {
3003 /* If redeclaring a builtin function, it stays built in.
3004 But it gets tagged as having been declared. */
3005 copy_decl_built_in_function (newdecl, olddecl);
3006 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3007 if (new_is_prototype)
3008 {
3009 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3010 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3011 {
3012 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3013 switch (fncode)
3014 {
3015 /* If a compatible prototype of these builtin functions
3016 is seen, assume the runtime implements it with the
3017 expected semantics. */
3018 case BUILT_IN_STPCPY:
3019 if (builtin_decl_explicit_p (fncode))
3020 set_builtin_decl_implicit_p (fncode, true);
3021 break;
3022 default:
3023 if (builtin_decl_explicit_p (fncode))
3024 set_builtin_decl_declared_p (fncode, true);
3025 break;
3026 }
3027
3028 copy_attributes_to_builtin (newdecl);
3029 }
3030 }
3031 else
3032 C_DECL_BUILTIN_PROTOTYPE (newdecl)
3033 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3034 }
3035
3036 /* Preserve function specific target and optimization options */
3037 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3038 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3039 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3040 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3041
3042 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3043 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3044 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3045 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3046
3047 /* Also preserve various other info from the definition. */
3048 if (!new_is_definition)
3049 {
3050 tree t;
3051 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3052 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3053 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3054 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3055 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3056 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3057 DECL_CONTEXT (t) = newdecl;
3058
3059 /* See if we've got a function to instantiate from. */
3060 if (DECL_SAVED_TREE (olddecl))
3061 DECL_ABSTRACT_ORIGIN (newdecl)
3062 = DECL_ABSTRACT_ORIGIN (olddecl);
3063 }
3064 }
3065
3066 /* Merge the USED information. */
3067 if (TREE_USED (olddecl))
3068 TREE_USED (newdecl) = 1;
3069 else if (TREE_USED (newdecl))
3070 TREE_USED (olddecl) = 1;
3071 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3072 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3073 if (DECL_PRESERVE_P (olddecl))
3074 DECL_PRESERVE_P (newdecl) = 1;
3075 else if (DECL_PRESERVE_P (newdecl))
3076 DECL_PRESERVE_P (olddecl) = 1;
3077
3078 /* Merge DECL_COMMON */
3079 if (VAR_P (olddecl) && VAR_P (newdecl)
3080 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3081 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3082 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3083
3084 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3085 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3086 DECL_ARGUMENTS (if appropriate). */
3087 {
3088 unsigned olddecl_uid = DECL_UID (olddecl);
3089 tree olddecl_context = DECL_CONTEXT (olddecl);
3090 tree olddecl_arguments = NULL;
3091 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3092 olddecl_arguments = DECL_ARGUMENTS (olddecl);
3093
3094 memcpy ((char *) olddecl + sizeof (struct tree_common),
3095 (char *) newdecl + sizeof (struct tree_common),
3096 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3097 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3098 switch (TREE_CODE (olddecl))
3099 {
3100 case FUNCTION_DECL:
3101 case VAR_DECL:
3102 {
3103 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3104
3105 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3106 (char *) newdecl + sizeof (struct tree_decl_common),
3107 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3108 olddecl->decl_with_vis.symtab_node = snode;
3109
3110 if ((DECL_EXTERNAL (olddecl)
3111 || TREE_PUBLIC (olddecl)
3112 || TREE_STATIC (olddecl))
3113 && DECL_SECTION_NAME (newdecl) != NULL)
3114 set_decl_section_name (olddecl, newdecl);
3115
3116 /* This isn't quite correct for something like
3117 int __thread x attribute ((tls_model ("local-exec")));
3118 extern int __thread x;
3119 as we'll lose the "local-exec" model. */
3120 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3121 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3122 break;
3123 }
3124
3125 case FIELD_DECL:
3126 case PARM_DECL:
3127 case LABEL_DECL:
3128 case RESULT_DECL:
3129 case CONST_DECL:
3130 case TYPE_DECL:
3131 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3132 (char *) newdecl + sizeof (struct tree_decl_common),
3133 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3134 break;
3135
3136 default:
3137
3138 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3139 (char *) newdecl + sizeof (struct tree_decl_common),
3140 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3141 }
3142 DECL_UID (olddecl) = olddecl_uid;
3143 DECL_CONTEXT (olddecl) = olddecl_context;
3144 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3145 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3146 }
3147
3148 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3149 so that encode_section_info has a chance to look at the new decl
3150 flags and attributes. */
3151 if (DECL_RTL_SET_P (olddecl)
3152 && (TREE_CODE (olddecl) == FUNCTION_DECL
3153 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3154 make_decl_rtl (olddecl);
3155 }
3156
3157 /* Handle when a new declaration NEWDECL has the same name as an old
3158 one OLDDECL in the same binding contour. Prints an error message
3159 if appropriate.
3160
3161 If safely possible, alter OLDDECL to look like NEWDECL, and return
3162 true. Otherwise, return false. */
3163
3164 static bool
3165 duplicate_decls (tree newdecl, tree olddecl)
3166 {
3167 tree newtype = NULL, oldtype = NULL;
3168
3169 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3170 {
3171 /* Avoid `unused variable' and other warnings for OLDDECL. */
3172 suppress_warning (olddecl, OPT_Wunused);
3173 /* If the types are completely different, poison them both with
3174 error_mark_node. */
3175 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3176 && olddecl != error_mark_node
3177 && seen_error ())
3178 {
3179 if (TREE_CODE (olddecl) != FUNCTION_DECL)
3180 TREE_TYPE (olddecl) = error_mark_node;
3181 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3182 TREE_TYPE (newdecl) = error_mark_node;
3183 }
3184 return false;
3185 }
3186
3187 merge_decls (newdecl, olddecl, newtype, oldtype);
3188
3189 /* The NEWDECL will no longer be needed.
3190
3191 Before releasing the node, be sure to remove function from symbol
3192 table that might have been inserted there to record comdat group.
3193 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3194 structure is shared in between NEWDECL and OLDECL. */
3195 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3196 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3197 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3198 {
3199 struct symtab_node *snode = symtab_node::get (newdecl);
3200 if (snode)
3201 snode->remove ();
3202 }
3203 ggc_free (newdecl);
3204 return true;
3205 }
3206
3207
3208 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3209 static void
3210 warn_if_shadowing (tree new_decl)
3211 {
3212 struct c_binding *b;
3213
3214 /* Shadow warnings wanted? */
3215 if (!(warn_shadow
3216 || warn_shadow_local
3217 || warn_shadow_compatible_local)
3218 /* No shadow warnings for internally generated vars. */
3219 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3220 return;
3221
3222 /* Is anything being shadowed? Invisible decls do not count. */
3223 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3224 if (b->decl && b->decl != new_decl && !b->invisible
3225 && (b->decl == error_mark_node
3226 || diagnostic_report_warnings_p (global_dc,
3227 DECL_SOURCE_LOCATION (b->decl))))
3228 {
3229 tree old_decl = b->decl;
3230
3231 if (old_decl == error_mark_node)
3232 {
3233 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3234 "non-variable", new_decl);
3235 break;
3236 }
3237
3238 bool warned = false;
3239 auto_diagnostic_group d;
3240 if (TREE_CODE (old_decl) == PARM_DECL)
3241 {
3242 enum opt_code warning_code;
3243
3244 /* If '-Wshadow=compatible-local' is specified without other
3245 -Wshadow= flags, we will warn only when the types of the
3246 shadowing variable (i.e. new_decl) and the shadowed variable
3247 (old_decl) are compatible. */
3248 if (warn_shadow)
3249 warning_code = OPT_Wshadow;
3250 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3251 warning_code = OPT_Wshadow_compatible_local;
3252 else
3253 warning_code = OPT_Wshadow_local;
3254 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3255 "declaration of %qD shadows a parameter",
3256 new_decl);
3257 }
3258 else if (DECL_FILE_SCOPE_P (old_decl))
3259 {
3260 /* Do not warn if a variable shadows a function, unless
3261 the variable is a function or a pointer-to-function. */
3262 if (TREE_CODE (old_decl) == FUNCTION_DECL
3263 && TREE_CODE (new_decl) != FUNCTION_DECL
3264 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3265 continue;
3266
3267 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3268 "declaration of %qD shadows a global "
3269 "declaration",
3270 new_decl);
3271 }
3272 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3273 && fndecl_built_in_p (old_decl))
3274 {
3275 warning (OPT_Wshadow, "declaration of %q+D shadows "
3276 "a built-in function", new_decl);
3277 break;
3278 }
3279 else
3280 {
3281 enum opt_code warning_code;
3282
3283 /* If '-Wshadow=compatible-local' is specified without other
3284 -Wshadow= flags, we will warn only when the types of the
3285 shadowing variable (i.e. new_decl) and the shadowed variable
3286 (old_decl) are compatible. */
3287 if (warn_shadow)
3288 warning_code = OPT_Wshadow;
3289 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3290 warning_code = OPT_Wshadow_compatible_local;
3291 else
3292 warning_code = OPT_Wshadow_local;
3293 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3294 "declaration of %qD shadows a previous local",
3295 new_decl);
3296 }
3297
3298 if (warned)
3299 inform (DECL_SOURCE_LOCATION (old_decl),
3300 "shadowed declaration is here");
3301
3302 break;
3303 }
3304 }
3305
3306 /* Record a decl-node X as belonging to the current lexical scope.
3307 Check for errors (such as an incompatible declaration for the same
3308 name already seen in the same scope).
3309
3310 Returns either X or an old decl for the same name.
3311 If an old decl is returned, it may have been smashed
3312 to agree with what X says. */
3313
3314 tree
3315 pushdecl (tree x)
3316 {
3317 tree name = DECL_NAME (x);
3318 struct c_scope *scope = current_scope;
3319 struct c_binding *b;
3320 bool nested = false;
3321 location_t locus = DECL_SOURCE_LOCATION (x);
3322
3323 /* Must set DECL_CONTEXT for everything not at file scope or
3324 DECL_FILE_SCOPE_P won't work. Local externs don't count
3325 unless they have initializers (which generate code). We
3326 also exclude CONST_DECLs because enumerators will get the
3327 type of the enum as context. */
3328 if (current_function_decl
3329 && TREE_CODE (x) != CONST_DECL
3330 && (!VAR_OR_FUNCTION_DECL_P (x)
3331 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3332 DECL_CONTEXT (x) = current_function_decl;
3333
3334 /* Anonymous decls are just inserted in the scope. */
3335 if (!name)
3336 {
3337 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3338 locus);
3339 return x;
3340 }
3341
3342 /* First, see if there is another declaration with the same name in
3343 the current scope. If there is, duplicate_decls may do all the
3344 work for us. If duplicate_decls returns false, that indicates
3345 two incompatible decls in the same scope; we are to silently
3346 replace the old one (duplicate_decls has issued all appropriate
3347 diagnostics). In particular, we should not consider possible
3348 duplicates in the external scope, or shadowing. */
3349 b = I_SYMBOL_BINDING (name);
3350 if (b && B_IN_SCOPE (b, scope))
3351 {
3352 struct c_binding *b_ext, *b_use;
3353 tree type = TREE_TYPE (x);
3354 tree visdecl = b->decl;
3355 tree vistype = TREE_TYPE (visdecl);
3356 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3357 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3358 b->inner_comp = false;
3359 b_use = b;
3360 b_ext = b;
3361 /* If this is an external linkage declaration, we should check
3362 for compatibility with the type in the external scope before
3363 setting the type at this scope based on the visible
3364 information only. */
3365 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3366 {
3367 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3368 b_ext = b_ext->shadowed;
3369 if (b_ext)
3370 {
3371 b_use = b_ext;
3372 if (b_use->u.type)
3373 TREE_TYPE (b_use->decl) = b_use->u.type;
3374 }
3375 }
3376 if (duplicate_decls (x, b_use->decl))
3377 {
3378 if (b_use != b)
3379 {
3380 /* Save the updated type in the external scope and
3381 restore the proper type for this scope. */
3382 tree thistype;
3383 if (comptypes (vistype, type))
3384 thistype = composite_type (vistype, type);
3385 else
3386 thistype = TREE_TYPE (b_use->decl);
3387 b_use->u.type = TREE_TYPE (b_use->decl);
3388 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3389 && fndecl_built_in_p (b_use->decl))
3390 thistype
3391 = c_build_type_attribute_variant (thistype,
3392 TYPE_ATTRIBUTES
3393 (b_use->u.type));
3394 TREE_TYPE (b_use->decl) = thistype;
3395 }
3396 return b_use->decl;
3397 }
3398 else
3399 goto skip_external_and_shadow_checks;
3400 }
3401
3402 /* All declarations with external linkage, and all external
3403 references, go in the external scope, no matter what scope is
3404 current. However, the binding in that scope is ignored for
3405 purposes of normal name lookup. A separate binding structure is
3406 created in the requested scope; this governs the normal
3407 visibility of the symbol.
3408
3409 The binding in the externals scope is used exclusively for
3410 detecting duplicate declarations of the same object, no matter
3411 what scope they are in; this is what we do here. (C99 6.2.7p2:
3412 All declarations that refer to the same object or function shall
3413 have compatible type; otherwise, the behavior is undefined.)
3414 However, in Objective-C, we also want to detect declarations
3415 conflicting with those of the basic types. */
3416 if ((DECL_EXTERNAL (x) || scope == file_scope)
3417 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3418 {
3419 tree type = TREE_TYPE (x);
3420 tree vistype = NULL_TREE;
3421 tree visdecl = NULL_TREE;
3422 bool type_saved = false;
3423 if (b && !B_IN_EXTERNAL_SCOPE (b)
3424 && VAR_OR_FUNCTION_DECL_P (b->decl)
3425 && DECL_FILE_SCOPE_P (b->decl))
3426 {
3427 visdecl = b->decl;
3428 vistype = TREE_TYPE (visdecl);
3429 }
3430 if (scope != file_scope
3431 && !DECL_IN_SYSTEM_HEADER (x))
3432 warning_at (locus, OPT_Wnested_externs,
3433 "nested extern declaration of %qD", x);
3434
3435 while (b && !B_IN_EXTERNAL_SCOPE (b))
3436 {
3437 /* If this decl might be modified, save its type. This is
3438 done here rather than when the decl is first bound
3439 because the type may change after first binding, through
3440 being completed or through attributes being added. If we
3441 encounter multiple such decls, only the first should have
3442 its type saved; the others will already have had their
3443 proper types saved and the types will not have changed as
3444 their scopes will not have been re-entered. */
3445 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3446 {
3447 b->u.type = TREE_TYPE (b->decl);
3448 type_saved = true;
3449 }
3450 if (B_IN_FILE_SCOPE (b)
3451 && VAR_P (b->decl)
3452 && TREE_STATIC (b->decl)
3453 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3454 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3455 && TREE_CODE (type) == ARRAY_TYPE
3456 && TYPE_DOMAIN (type)
3457 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3458 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3459 {
3460 /* Array type completed in inner scope, which should be
3461 diagnosed if the completion does not have size 1 and
3462 it does not get completed in the file scope. */
3463 b->inner_comp = true;
3464 }
3465 b = b->shadowed;
3466 }
3467
3468 /* If a matching external declaration has been found, set its
3469 type to the composite of all the types of that declaration.
3470 After the consistency checks, it will be reset to the
3471 composite of the visible types only. */
3472 if (b && b->u.type)
3473 TREE_TYPE (b->decl) = b->u.type;
3474
3475 /* the static does not go in the externals scope. */
3476 if (b && duplicate_decls (x, b->decl))
3477 {
3478 tree thistype;
3479 if (vistype)
3480 {
3481 if (comptypes (vistype, type))
3482 thistype = composite_type (vistype, type);
3483 else
3484 thistype = TREE_TYPE (b->decl);
3485 }
3486 else
3487 thistype = type;
3488 b->u.type = TREE_TYPE (b->decl);
3489 /* Propagate the type attributes to the decl. */
3490 thistype
3491 = c_build_type_attribute_variant (thistype,
3492 TYPE_ATTRIBUTES (b->u.type));
3493 TREE_TYPE (b->decl) = thistype;
3494 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3495 locus);
3496 return b->decl;
3497 }
3498 else if (TREE_PUBLIC (x))
3499 {
3500 if (visdecl && !b && duplicate_decls (x, visdecl))
3501 {
3502 /* An external declaration at block scope referring to a
3503 visible entity with internal linkage. The composite
3504 type will already be correct for this scope, so we
3505 just need to fall through to make the declaration in
3506 this scope. */
3507 nested = true;
3508 x = visdecl;
3509 }
3510 else
3511 {
3512 bind (name, x, external_scope, /*invisible=*/true,
3513 /*nested=*/false, locus);
3514 nested = true;
3515 }
3516 }
3517 }
3518
3519 if (TREE_CODE (x) != PARM_DECL)
3520 warn_if_shadowing (x);
3521
3522 skip_external_and_shadow_checks:
3523 if (TREE_CODE (x) == TYPE_DECL)
3524 {
3525 /* So this is a typedef, set its underlying type. */
3526 set_underlying_type (x);
3527
3528 /* If X is a typedef defined in the current function, record it
3529 for the purpose of implementing the -Wunused-local-typedefs
3530 warning. */
3531 record_locally_defined_typedef (x);
3532 }
3533
3534 bind (name, x, scope, /*invisible=*/false, nested, locus);
3535
3536 /* If x's type is incomplete because it's based on a
3537 structure or union which has not yet been fully declared,
3538 attach it to that structure or union type, so we can go
3539 back and complete the variable declaration later, if the
3540 structure or union gets fully declared.
3541
3542 If the input is erroneous, we can have error_mark in the type
3543 slot (e.g. "f(void a, ...)") - that doesn't count as an
3544 incomplete type. */
3545 if (TREE_TYPE (x) != error_mark_node
3546 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3547 {
3548 tree element = TREE_TYPE (x);
3549
3550 while (TREE_CODE (element) == ARRAY_TYPE)
3551 element = TREE_TYPE (element);
3552 element = TYPE_MAIN_VARIANT (element);
3553
3554 if ((RECORD_OR_UNION_TYPE_P (element)
3555 || TREE_CODE (element) == ENUMERAL_TYPE)
3556 && (TREE_CODE (x) != TYPE_DECL
3557 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3558 && !COMPLETE_TYPE_P (element))
3559 C_TYPE_INCOMPLETE_VARS (element)
3560 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3561 }
3562 return x;
3563 }
3564
3565
3566 /* Issue a permerror about implicit function declaration. ID is the function
3567 identifier, OLDDECL is a declaration of the function in a different scope,
3568 or NULL_TREE. */
3569
3570 static void
3571 implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3572 {
3573 if (!warn_implicit_function_declaration)
3574 return;
3575
3576 bool warned;
3577 auto_diagnostic_group d;
3578 name_hint hint;
3579 if (!olddecl)
3580 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3581
3582 if (flag_isoc99)
3583 {
3584 if (const char *suggestion = hint.suggestion ())
3585 {
3586 gcc_rich_location richloc (loc);
3587 richloc.add_fixit_replace (suggestion);
3588 warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3589 "implicit declaration of function %qE;"
3590 " did you mean %qs?",
3591 id, suggestion);
3592 }
3593 else
3594 warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3595 "implicit declaration of function %qE", id);
3596 }
3597 else if (const char *suggestion = hint.suggestion ())
3598 {
3599 gcc_rich_location richloc (loc);
3600 richloc.add_fixit_replace (suggestion);
3601 warned = warning_at
3602 (&richloc, OPT_Wimplicit_function_declaration,
3603 G_("implicit declaration of function %qE; did you mean %qs?"),
3604 id, suggestion);
3605 }
3606 else
3607 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3608 G_("implicit declaration of function %qE"), id);
3609
3610 if (warned)
3611 {
3612 /* Whether the olddecl is an undeclared builtin function.
3613 locate_old_decl will not generate a diagnostic for those,
3614 so in that case we want to look elsewhere. */
3615 bool undeclared_builtin = (olddecl
3616 && TREE_CODE (olddecl) == FUNCTION_DECL
3617 && fndecl_built_in_p (olddecl)
3618 && !C_DECL_DECLARED_BUILTIN (olddecl));
3619 if (undeclared_builtin)
3620 {
3621 const char *header = header_for_builtin_fn (olddecl);
3622 if (header)
3623 {
3624 rich_location richloc (line_table, loc);
3625 maybe_add_include_fixit (&richloc, header, true);
3626 inform (&richloc,
3627 "include %qs or provide a declaration of %qE",
3628 header, id);
3629 }
3630 }
3631 else if (olddecl)
3632 locate_old_decl (olddecl);
3633 }
3634
3635 if (!warned)
3636 hint.suppress ();
3637 }
3638
3639 /* Return the name of the header file that declares built-in function
3640 FNDECL, or null if either we don't know or don't expect to see an
3641 explicit declaration. */
3642
3643 static const char *
3644 header_for_builtin_fn (tree fndecl)
3645 {
3646 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3647 return NULL;
3648
3649 switch (DECL_FUNCTION_CODE (fndecl))
3650 {
3651 CASE_FLT_FN (BUILT_IN_ACOS):
3652 CASE_FLT_FN (BUILT_IN_ACOSH):
3653 CASE_FLT_FN (BUILT_IN_ASIN):
3654 CASE_FLT_FN (BUILT_IN_ASINH):
3655 CASE_FLT_FN (BUILT_IN_ATAN):
3656 CASE_FLT_FN (BUILT_IN_ATANH):
3657 CASE_FLT_FN (BUILT_IN_ATAN2):
3658 CASE_FLT_FN (BUILT_IN_CBRT):
3659 CASE_FLT_FN (BUILT_IN_CEIL):
3660 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3661 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3662 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3663 CASE_FLT_FN (BUILT_IN_COS):
3664 CASE_FLT_FN (BUILT_IN_COSH):
3665 CASE_FLT_FN (BUILT_IN_ERF):
3666 CASE_FLT_FN (BUILT_IN_ERFC):
3667 CASE_FLT_FN (BUILT_IN_EXP):
3668 CASE_FLT_FN (BUILT_IN_EXP2):
3669 CASE_FLT_FN (BUILT_IN_EXPM1):
3670 CASE_FLT_FN (BUILT_IN_FABS):
3671 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3672 CASE_FLT_FN (BUILT_IN_FDIM):
3673 CASE_FLT_FN (BUILT_IN_FLOOR):
3674 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3675 CASE_FLT_FN (BUILT_IN_FMA):
3676 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3677 CASE_FLT_FN (BUILT_IN_FMAX):
3678 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3679 CASE_FLT_FN (BUILT_IN_FMIN):
3680 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3681 CASE_FLT_FN (BUILT_IN_FMOD):
3682 CASE_FLT_FN (BUILT_IN_FREXP):
3683 CASE_FLT_FN (BUILT_IN_HYPOT):
3684 CASE_FLT_FN (BUILT_IN_ILOGB):
3685 CASE_FLT_FN (BUILT_IN_LDEXP):
3686 CASE_FLT_FN (BUILT_IN_LGAMMA):
3687 CASE_FLT_FN (BUILT_IN_LLRINT):
3688 CASE_FLT_FN (BUILT_IN_LLROUND):
3689 CASE_FLT_FN (BUILT_IN_LOG):
3690 CASE_FLT_FN (BUILT_IN_LOG10):
3691 CASE_FLT_FN (BUILT_IN_LOG1P):
3692 CASE_FLT_FN (BUILT_IN_LOG2):
3693 CASE_FLT_FN (BUILT_IN_LOGB):
3694 CASE_FLT_FN (BUILT_IN_LRINT):
3695 CASE_FLT_FN (BUILT_IN_LROUND):
3696 CASE_FLT_FN (BUILT_IN_MODF):
3697 CASE_FLT_FN (BUILT_IN_NAN):
3698 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3699 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3700 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3701 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3702 CASE_FLT_FN (BUILT_IN_POW):
3703 CASE_FLT_FN (BUILT_IN_REMAINDER):
3704 CASE_FLT_FN (BUILT_IN_REMQUO):
3705 CASE_FLT_FN (BUILT_IN_RINT):
3706 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3707 CASE_FLT_FN (BUILT_IN_ROUND):
3708 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3709 CASE_FLT_FN (BUILT_IN_SCALBLN):
3710 CASE_FLT_FN (BUILT_IN_SCALBN):
3711 CASE_FLT_FN (BUILT_IN_SIN):
3712 CASE_FLT_FN (BUILT_IN_SINH):
3713 CASE_FLT_FN (BUILT_IN_SINCOS):
3714 CASE_FLT_FN (BUILT_IN_SQRT):
3715 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3716 CASE_FLT_FN (BUILT_IN_TAN):
3717 CASE_FLT_FN (BUILT_IN_TANH):
3718 CASE_FLT_FN (BUILT_IN_TGAMMA):
3719 CASE_FLT_FN (BUILT_IN_TRUNC):
3720 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3721 case BUILT_IN_ISINF:
3722 case BUILT_IN_ISNAN:
3723 return "<math.h>";
3724 CASE_FLT_FN (BUILT_IN_CABS):
3725 CASE_FLT_FN (BUILT_IN_CACOS):
3726 CASE_FLT_FN (BUILT_IN_CACOSH):
3727 CASE_FLT_FN (BUILT_IN_CARG):
3728 CASE_FLT_FN (BUILT_IN_CASIN):
3729 CASE_FLT_FN (BUILT_IN_CASINH):
3730 CASE_FLT_FN (BUILT_IN_CATAN):
3731 CASE_FLT_FN (BUILT_IN_CATANH):
3732 CASE_FLT_FN (BUILT_IN_CCOS):
3733 CASE_FLT_FN (BUILT_IN_CCOSH):
3734 CASE_FLT_FN (BUILT_IN_CEXP):
3735 CASE_FLT_FN (BUILT_IN_CIMAG):
3736 CASE_FLT_FN (BUILT_IN_CLOG):
3737 CASE_FLT_FN (BUILT_IN_CONJ):
3738 CASE_FLT_FN (BUILT_IN_CPOW):
3739 CASE_FLT_FN (BUILT_IN_CPROJ):
3740 CASE_FLT_FN (BUILT_IN_CREAL):
3741 CASE_FLT_FN (BUILT_IN_CSIN):
3742 CASE_FLT_FN (BUILT_IN_CSINH):
3743 CASE_FLT_FN (BUILT_IN_CSQRT):
3744 CASE_FLT_FN (BUILT_IN_CTAN):
3745 CASE_FLT_FN (BUILT_IN_CTANH):
3746 return "<complex.h>";
3747 case BUILT_IN_MEMCHR:
3748 case BUILT_IN_MEMCMP:
3749 case BUILT_IN_MEMCPY:
3750 case BUILT_IN_MEMMOVE:
3751 case BUILT_IN_MEMSET:
3752 case BUILT_IN_STRCAT:
3753 case BUILT_IN_STRCHR:
3754 case BUILT_IN_STRCMP:
3755 case BUILT_IN_STRCPY:
3756 case BUILT_IN_STRCSPN:
3757 case BUILT_IN_STRLEN:
3758 case BUILT_IN_STRNCAT:
3759 case BUILT_IN_STRNCMP:
3760 case BUILT_IN_STRNCPY:
3761 case BUILT_IN_STRPBRK:
3762 case BUILT_IN_STRRCHR:
3763 case BUILT_IN_STRSPN:
3764 case BUILT_IN_STRSTR:
3765 return "<string.h>";
3766 case BUILT_IN_FPRINTF:
3767 case BUILT_IN_PUTC:
3768 case BUILT_IN_FPUTC:
3769 case BUILT_IN_FPUTS:
3770 case BUILT_IN_FSCANF:
3771 case BUILT_IN_FWRITE:
3772 case BUILT_IN_PRINTF:
3773 case BUILT_IN_PUTCHAR:
3774 case BUILT_IN_PUTS:
3775 case BUILT_IN_SCANF:
3776 case BUILT_IN_SNPRINTF:
3777 case BUILT_IN_SPRINTF:
3778 case BUILT_IN_SSCANF:
3779 case BUILT_IN_VFPRINTF:
3780 case BUILT_IN_VFSCANF:
3781 case BUILT_IN_VPRINTF:
3782 case BUILT_IN_VSCANF:
3783 case BUILT_IN_VSNPRINTF:
3784 case BUILT_IN_VSPRINTF:
3785 case BUILT_IN_VSSCANF:
3786 return "<stdio.h>";
3787 case BUILT_IN_ISALNUM:
3788 case BUILT_IN_ISALPHA:
3789 case BUILT_IN_ISBLANK:
3790 case BUILT_IN_ISCNTRL:
3791 case BUILT_IN_ISDIGIT:
3792 case BUILT_IN_ISGRAPH:
3793 case BUILT_IN_ISLOWER:
3794 case BUILT_IN_ISPRINT:
3795 case BUILT_IN_ISPUNCT:
3796 case BUILT_IN_ISSPACE:
3797 case BUILT_IN_ISUPPER:
3798 case BUILT_IN_ISXDIGIT:
3799 case BUILT_IN_TOLOWER:
3800 case BUILT_IN_TOUPPER:
3801 return "<ctype.h>";
3802 case BUILT_IN_ISWALNUM:
3803 case BUILT_IN_ISWALPHA:
3804 case BUILT_IN_ISWBLANK:
3805 case BUILT_IN_ISWCNTRL:
3806 case BUILT_IN_ISWDIGIT:
3807 case BUILT_IN_ISWGRAPH:
3808 case BUILT_IN_ISWLOWER:
3809 case BUILT_IN_ISWPRINT:
3810 case BUILT_IN_ISWPUNCT:
3811 case BUILT_IN_ISWSPACE:
3812 case BUILT_IN_ISWUPPER:
3813 case BUILT_IN_ISWXDIGIT:
3814 case BUILT_IN_TOWLOWER:
3815 case BUILT_IN_TOWUPPER:
3816 return "<wctype.h>";
3817 case BUILT_IN_ABORT:
3818 case BUILT_IN_ABS:
3819 case BUILT_IN_CALLOC:
3820 case BUILT_IN_EXIT:
3821 case BUILT_IN_FREE:
3822 case BUILT_IN_LABS:
3823 case BUILT_IN_LLABS:
3824 case BUILT_IN_MALLOC:
3825 case BUILT_IN_REALLOC:
3826 case BUILT_IN__EXIT2:
3827 case BUILT_IN_ALIGNED_ALLOC:
3828 return "<stdlib.h>";
3829 case BUILT_IN_IMAXABS:
3830 return "<inttypes.h>";
3831 case BUILT_IN_STRFTIME:
3832 return "<time.h>";
3833 default:
3834 return NULL;
3835 }
3836 }
3837
3838 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3839 function of type int (). */
3840
3841 tree
3842 implicitly_declare (location_t loc, tree functionid)
3843 {
3844 struct c_binding *b;
3845 tree decl = NULL_TREE;
3846 tree asmspec_tree;
3847
3848 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3849 {
3850 if (B_IN_SCOPE (b, external_scope))
3851 {
3852 decl = b->decl;
3853 break;
3854 }
3855 }
3856
3857 if (decl)
3858 {
3859 if (TREE_CODE (decl) != FUNCTION_DECL)
3860 return decl;
3861
3862 /* FIXME: Objective-C has weird not-really-builtin functions
3863 which are supposed to be visible automatically. They wind up
3864 in the external scope because they're pushed before the file
3865 scope gets created. Catch this here and rebind them into the
3866 file scope. */
3867 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3868 {
3869 bind (functionid, decl, file_scope,
3870 /*invisible=*/false, /*nested=*/true,
3871 DECL_SOURCE_LOCATION (decl));
3872 return decl;
3873 }
3874 else
3875 {
3876 tree newtype = default_function_type;
3877 if (b->u.type)
3878 TREE_TYPE (decl) = b->u.type;
3879 /* Implicit declaration of a function already declared
3880 (somehow) in a different scope, or as a built-in.
3881 If this is the first time this has happened, warn;
3882 then recycle the old declaration but with the new type. */
3883 if (!C_DECL_IMPLICIT (decl))
3884 {
3885 implicit_decl_permerror (loc, functionid, decl);
3886 C_DECL_IMPLICIT (decl) = 1;
3887 }
3888 if (fndecl_built_in_p (decl))
3889 {
3890 newtype = c_build_type_attribute_variant (newtype,
3891 TYPE_ATTRIBUTES
3892 (TREE_TYPE (decl)));
3893 if (!comptypes (newtype, TREE_TYPE (decl)))
3894 {
3895 auto_diagnostic_group d;
3896 bool warned = warning_at (loc,
3897 OPT_Wbuiltin_declaration_mismatch,
3898 "incompatible implicit "
3899 "declaration of built-in "
3900 "function %qD", decl);
3901 /* See if we can hint which header to include. */
3902 const char *header = header_for_builtin_fn (decl);
3903 if (header != NULL && warned)
3904 {
3905 rich_location richloc (line_table, loc);
3906 maybe_add_include_fixit (&richloc, header, true);
3907 inform (&richloc,
3908 "include %qs or provide a declaration of %qD",
3909 header, decl);
3910 }
3911 newtype = TREE_TYPE (decl);
3912 }
3913 }
3914 else
3915 {
3916 if (!comptypes (newtype, TREE_TYPE (decl)))
3917 {
3918 auto_diagnostic_group d;
3919 error_at (loc, "incompatible implicit declaration of "
3920 "function %qD", decl);
3921 locate_old_decl (decl);
3922 }
3923 }
3924 b->u.type = TREE_TYPE (decl);
3925 TREE_TYPE (decl) = newtype;
3926 bind (functionid, decl, current_scope,
3927 /*invisible=*/false, /*nested=*/true,
3928 DECL_SOURCE_LOCATION (decl));
3929 return decl;
3930 }
3931 }
3932
3933 /* Not seen before. */
3934 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3935 DECL_EXTERNAL (decl) = 1;
3936 TREE_PUBLIC (decl) = 1;
3937 C_DECL_IMPLICIT (decl) = 1;
3938 implicit_decl_permerror (loc, functionid, 0);
3939 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3940 if (asmspec_tree)
3941 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3942
3943 /* C89 says implicit declarations are in the innermost block.
3944 So we record the decl in the standard fashion. */
3945 decl = pushdecl (decl);
3946
3947 /* No need to call objc_check_decl here - it's a function type. */
3948 rest_of_decl_compilation (decl, 0, 0);
3949
3950 /* Write a record describing this implicit function declaration
3951 to the prototypes file (if requested). */
3952 gen_aux_info_record (decl, 0, 1, 0);
3953
3954 /* Possibly apply some default attributes to this implicit declaration. */
3955 decl_attributes (&decl, NULL_TREE, 0);
3956
3957 return decl;
3958 }
3959
3960 /* Issue an error message for a reference to an undeclared variable
3961 ID, including a reference to a builtin outside of function-call
3962 context. Establish a binding of the identifier to error_mark_node
3963 in an appropriate scope, which will suppress further errors for the
3964 same identifier. The error message should be given location LOC. */
3965 void
3966 undeclared_variable (location_t loc, tree id)
3967 {
3968 static bool already = false;
3969 struct c_scope *scope;
3970
3971 auto_diagnostic_group d;
3972 if (current_function_decl == NULL_TREE)
3973 {
3974 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3975 if (const char *suggestion = guessed_id.suggestion ())
3976 {
3977 gcc_rich_location richloc (loc);
3978 richloc.add_fixit_replace (suggestion);
3979 error_at (&richloc,
3980 "%qE undeclared here (not in a function);"
3981 " did you mean %qs?",
3982 id, suggestion);
3983 }
3984 else
3985 error_at (loc, "%qE undeclared here (not in a function)", id);
3986 scope = current_scope;
3987 }
3988 else
3989 {
3990 if (!objc_diagnose_private_ivar (id))
3991 {
3992 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3993 if (const char *suggestion = guessed_id.suggestion ())
3994 {
3995 gcc_rich_location richloc (loc);
3996 richloc.add_fixit_replace (suggestion);
3997 error_at (&richloc,
3998 "%qE undeclared (first use in this function);"
3999 " did you mean %qs?",
4000 id, suggestion);
4001 }
4002 else
4003 error_at (loc, "%qE undeclared (first use in this function)", id);
4004 }
4005 if (!already)
4006 {
4007 inform (loc, "each undeclared identifier is reported only"
4008 " once for each function it appears in");
4009 already = true;
4010 }
4011
4012 /* If we are parsing old-style parameter decls, current_function_decl
4013 will be nonnull but current_function_scope will be null. */
4014 scope = current_function_scope ? current_function_scope : current_scope;
4015 }
4016 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4017 UNKNOWN_LOCATION);
4018 }
4019
4020 /* Subroutine of lookup_label, declare_label, define_label: construct a
4021 LABEL_DECL with all the proper frills. Also create a struct
4022 c_label_vars initialized for the current scope. */
4023
4024 static tree
4025 make_label (location_t location, tree name, bool defining,
4026 struct c_label_vars **p_label_vars)
4027 {
4028 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4029 DECL_CONTEXT (label) = current_function_decl;
4030 SET_DECL_MODE (label, VOIDmode);
4031
4032 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4033 label_vars->shadowed = NULL;
4034 set_spot_bindings (&label_vars->label_bindings, defining);
4035 label_vars->decls_in_scope = make_tree_vector ();
4036 label_vars->gotos = NULL;
4037 *p_label_vars = label_vars;
4038
4039 return label;
4040 }
4041
4042 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4043 Create one if none exists so far for the current function.
4044 This is called when a label is used in a goto expression or
4045 has its address taken. */
4046
4047 tree
4048 lookup_label (tree name)
4049 {
4050 tree label;
4051 struct c_label_vars *label_vars;
4052
4053 if (current_function_scope == 0)
4054 {
4055 error ("label %qE referenced outside of any function", name);
4056 return NULL_TREE;
4057 }
4058
4059 /* Use a label already defined or ref'd with this name, but not if
4060 it is inherited from a containing function and wasn't declared
4061 using __label__. */
4062 label = I_LABEL_DECL (name);
4063 if (label && (DECL_CONTEXT (label) == current_function_decl
4064 || C_DECLARED_LABEL_FLAG (label)))
4065 {
4066 /* If the label has only been declared, update its apparent
4067 location to point here, for better diagnostics if it
4068 turns out not to have been defined. */
4069 if (DECL_INITIAL (label) == NULL_TREE)
4070 DECL_SOURCE_LOCATION (label) = input_location;
4071 return label;
4072 }
4073
4074 /* No label binding for that identifier; make one. */
4075 label = make_label (input_location, name, false, &label_vars);
4076
4077 /* Ordinary labels go in the current function scope. */
4078 bind_label (name, label, current_function_scope, label_vars);
4079
4080 return label;
4081 }
4082
4083 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4084 to LABEL. */
4085
4086 static void
4087 warn_about_goto (location_t goto_loc, tree label, tree decl)
4088 {
4089 auto_diagnostic_group d;
4090 if (c_type_variably_modified_p (TREE_TYPE (decl)))
4091 error_at (goto_loc,
4092 "jump into scope of identifier with variably modified type");
4093 else if (flag_openmp
4094 && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4095 error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4096 else
4097 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4098 "jump skips variable initialization"))
4099 return;
4100 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4101 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4102 }
4103
4104 /* Look up a label because of a goto statement. This is like
4105 lookup_label, but also issues any appropriate warnings. */
4106
4107 tree
4108 lookup_label_for_goto (location_t loc, tree name)
4109 {
4110 tree label;
4111 struct c_label_vars *label_vars;
4112 unsigned int ix;
4113 tree decl;
4114
4115 label = lookup_label (name);
4116 if (label == NULL_TREE)
4117 return NULL_TREE;
4118
4119 /* If we are jumping to a different function, we can't issue any
4120 useful warnings. */
4121 if (DECL_CONTEXT (label) != current_function_decl)
4122 {
4123 gcc_assert (C_DECLARED_LABEL_FLAG (label));
4124 return label;
4125 }
4126
4127 label_vars = I_LABEL_BINDING (name)->u.label;
4128
4129 /* If the label has not yet been defined, then push this goto on a
4130 list for possible later warnings. */
4131 if (label_vars->label_bindings.scope == NULL)
4132 {
4133 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4134
4135 g->loc = loc;
4136 set_spot_bindings (&g->goto_bindings, true);
4137 vec_safe_push (label_vars->gotos, g);
4138 return label;
4139 }
4140
4141 /* If there are any decls in label_vars->decls_in_scope, then this
4142 goto has missed the declaration of the decl. This happens for a
4143 case like
4144 int i = 1;
4145 lab:
4146 ...
4147 goto lab;
4148 Issue a warning or error. */
4149 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4150 warn_about_goto (loc, label, decl);
4151
4152 if (label_vars->label_bindings.left_stmt_expr)
4153 {
4154 auto_diagnostic_group d;
4155 error_at (loc, "jump into statement expression");
4156 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4157 }
4158
4159 return label;
4160 }
4161
4162 /* Make a label named NAME in the current function, shadowing silently
4163 any that may be inherited from containing functions or containing
4164 scopes. This is called for __label__ declarations. */
4165
4166 tree
4167 declare_label (tree name)
4168 {
4169 struct c_binding *b = I_LABEL_BINDING (name);
4170 tree label;
4171 struct c_label_vars *label_vars;
4172
4173 /* Check to make sure that the label hasn't already been declared
4174 at this scope */
4175 if (b && B_IN_CURRENT_SCOPE (b))
4176 {
4177 auto_diagnostic_group d;
4178 error ("duplicate label declaration %qE", name);
4179 locate_old_decl (b->decl);
4180
4181 /* Just use the previous declaration. */
4182 return b->decl;
4183 }
4184
4185 label = make_label (input_location, name, false, &label_vars);
4186 C_DECLARED_LABEL_FLAG (label) = 1;
4187
4188 /* Declared labels go in the current scope. */
4189 bind_label (name, label, current_scope, label_vars);
4190
4191 return label;
4192 }
4193
4194 /* When we define a label, issue any appropriate warnings if there are
4195 any gotos earlier in the function which jump to this label. */
4196
4197 static void
4198 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4199 {
4200 unsigned int ix;
4201 struct c_goto_bindings *g;
4202
4203 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4204 {
4205 struct c_binding *b;
4206 struct c_scope *scope;
4207
4208 /* We have a goto to this label. The goto is going forward. In
4209 g->scope, the goto is going to skip any binding which was
4210 defined after g->bindings_in_scope. */
4211 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4212 {
4213 for (b = g->goto_bindings.scope->bindings;
4214 b != g->goto_bindings.bindings_in_scope;
4215 b = b->prev)
4216 {
4217 if (decl_jump_unsafe (b->decl))
4218 warn_about_goto (g->loc, label, b->decl);
4219 }
4220 }
4221
4222 /* We also need to warn about decls defined in any scopes
4223 between the scope of the label and the scope of the goto. */
4224 for (scope = label_vars->label_bindings.scope;
4225 scope != g->goto_bindings.scope;
4226 scope = scope->outer)
4227 {
4228 gcc_assert (scope != NULL);
4229 if (scope->has_jump_unsafe_decl)
4230 {
4231 if (scope == label_vars->label_bindings.scope)
4232 b = label_vars->label_bindings.bindings_in_scope;
4233 else
4234 b = scope->bindings;
4235 for (; b != NULL; b = b->prev)
4236 {
4237 if (decl_jump_unsafe (b->decl))
4238 warn_about_goto (g->loc, label, b->decl);
4239 }
4240 }
4241 }
4242
4243 if (g->goto_bindings.stmt_exprs > 0)
4244 {
4245 auto_diagnostic_group d;
4246 error_at (g->loc, "jump into statement expression");
4247 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4248 label);
4249 }
4250 }
4251
4252 /* Now that the label is defined, we will issue warnings about
4253 subsequent gotos to this label when we see them. */
4254 vec_safe_truncate (label_vars->gotos, 0);
4255 label_vars->gotos = NULL;
4256 }
4257
4258 /* Define a label, specifying the location in the source file.
4259 Return the LABEL_DECL node for the label, if the definition is valid.
4260 Otherwise return NULL_TREE. */
4261
4262 tree
4263 define_label (location_t location, tree name)
4264 {
4265 /* Find any preexisting label with this name. It is an error
4266 if that label has already been defined in this function, or
4267 if there is a containing function with a declared label with
4268 the same name. */
4269 tree label = I_LABEL_DECL (name);
4270
4271 if (label
4272 && ((DECL_CONTEXT (label) == current_function_decl
4273 && DECL_INITIAL (label) != NULL_TREE)
4274 || (DECL_CONTEXT (label) != current_function_decl
4275 && C_DECLARED_LABEL_FLAG (label))))
4276 {
4277 auto_diagnostic_group d;
4278 error_at (location, "duplicate label %qD", label);
4279 locate_old_decl (label);
4280 return NULL_TREE;
4281 }
4282 else if (label && DECL_CONTEXT (label) == current_function_decl)
4283 {
4284 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4285
4286 /* The label has been used or declared already in this function,
4287 but not defined. Update its location to point to this
4288 definition. */
4289 DECL_SOURCE_LOCATION (label) = location;
4290 set_spot_bindings (&label_vars->label_bindings, true);
4291
4292 /* Issue warnings as required about any goto statements from
4293 earlier in the function. */
4294 check_earlier_gotos (label, label_vars);
4295 }
4296 else
4297 {
4298 struct c_label_vars *label_vars;
4299
4300 /* No label binding for that identifier; make one. */
4301 label = make_label (location, name, true, &label_vars);
4302
4303 /* Ordinary labels go in the current function scope. */
4304 bind_label (name, label, current_function_scope, label_vars);
4305 }
4306
4307 if (!in_system_header_at (input_location) && lookup_name (name))
4308 warning_at (location, OPT_Wtraditional,
4309 "traditional C lacks a separate namespace "
4310 "for labels, identifier %qE conflicts", name);
4311
4312 /* Mark label as having been defined. */
4313 DECL_INITIAL (label) = error_mark_node;
4314 return label;
4315 }
4316
4317 /* Get the bindings for a new switch statement. This is used to issue
4318 warnings as appropriate for jumps from the switch to case or
4319 default labels. */
4320
4321 struct c_spot_bindings *
4322 c_get_switch_bindings (void)
4323 {
4324 struct c_spot_bindings *switch_bindings;
4325
4326 switch_bindings = XNEW (struct c_spot_bindings);
4327 set_spot_bindings (switch_bindings, true);
4328 return switch_bindings;
4329 }
4330
4331 void
4332 c_release_switch_bindings (struct c_spot_bindings *bindings)
4333 {
4334 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4335 XDELETE (bindings);
4336 }
4337
4338 /* This is called at the point of a case or default label to issue
4339 warnings about decls as needed. It returns true if it found an
4340 error, not just a warning. */
4341
4342 bool
4343 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4344 location_t switch_loc, location_t case_loc)
4345 {
4346 bool saw_error;
4347 struct c_scope *scope;
4348
4349 saw_error = false;
4350 for (scope = current_scope;
4351 scope != switch_bindings->scope;
4352 scope = scope->outer)
4353 {
4354 struct c_binding *b;
4355
4356 gcc_assert (scope != NULL);
4357
4358 if (!scope->has_jump_unsafe_decl)
4359 continue;
4360
4361 for (b = scope->bindings; b != NULL; b = b->prev)
4362 {
4363 if (decl_jump_unsafe (b->decl))
4364 {
4365 auto_diagnostic_group d;
4366 bool emitted;
4367 if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4368 {
4369 saw_error = true;
4370 error_at (case_loc,
4371 ("switch jumps into scope of identifier with "
4372 "variably modified type"));
4373 emitted = true;
4374 }
4375 else if (flag_openmp
4376 && lookup_attribute ("omp allocate",
4377 DECL_ATTRIBUTES (b->decl)))
4378 {
4379 saw_error = true;
4380 error_at (case_loc,
4381 "switch jumps over OpenMP %<allocate%> allocation");
4382 emitted = true;
4383 }
4384 else
4385 emitted
4386 = warning_at (case_loc, OPT_Wjump_misses_init,
4387 "switch jumps over variable initialization");
4388 if (emitted)
4389 {
4390 inform (switch_loc, "switch starts here");
4391 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4392 b->decl);
4393 }
4394 }
4395 }
4396 }
4397
4398 if (switch_bindings->stmt_exprs > 0)
4399 {
4400 saw_error = true;
4401 auto_diagnostic_group d;
4402 error_at (case_loc, "switch jumps into statement expression");
4403 inform (switch_loc, "switch starts here");
4404 }
4405
4406 return saw_error;
4407 }
4408
4409 /* Given NAME, an IDENTIFIER_NODE,
4410 return the structure (or union or enum) definition for that name.
4411 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4412 CODE says which kind of type the caller wants;
4413 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4414 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4415 location where the tag was defined.
4416 If the wrong kind of type is found, an error is reported. */
4417
4418 static tree
4419 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4420 location_t *ploc)
4421 {
4422 struct c_binding *b = I_TAG_BINDING (name);
4423 bool thislevel = false;
4424
4425 if (!b || !b->decl)
4426 return NULL_TREE;
4427
4428 /* We only care about whether it's in this level if
4429 thislevel_only was set or it might be a type clash. */
4430 if (thislevel_only || TREE_CODE (b->decl) != code)
4431 {
4432 /* For our purposes, a tag in the external scope is the same as
4433 a tag in the file scope. (Primarily relevant to Objective-C
4434 and its builtin structure tags, which get pushed before the
4435 file scope is created.) */
4436 if (B_IN_CURRENT_SCOPE (b)
4437 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4438 thislevel = true;
4439 }
4440
4441 if (thislevel_only && !thislevel)
4442 return NULL_TREE;
4443
4444 if (TREE_CODE (b->decl) != code)
4445 {
4446 /* Definition isn't the kind we were looking for. */
4447 pending_invalid_xref = name;
4448 pending_invalid_xref_location = input_location;
4449
4450 /* If in the same binding level as a declaration as a tag
4451 of a different type, this must not be allowed to
4452 shadow that tag, so give the error immediately.
4453 (For example, "struct foo; union foo;" is invalid.) */
4454 if (thislevel)
4455 pending_xref_error ();
4456 }
4457
4458 if (ploc != NULL)
4459 *ploc = b->locus;
4460
4461 return b->decl;
4462 }
4463
4464 /* Return true if a definition exists for NAME with code CODE. */
4465
4466 bool
4467 tag_exists_p (enum tree_code code, tree name)
4468 {
4469 struct c_binding *b = I_TAG_BINDING (name);
4470
4471 if (b == NULL || b->decl == NULL_TREE)
4472 return false;
4473 return TREE_CODE (b->decl) == code;
4474 }
4475
4476 /* Print an error message now
4477 for a recent invalid struct, union or enum cross reference.
4478 We don't print them immediately because they are not invalid
4479 when used in the `struct foo;' construct for shadowing. */
4480
4481 void
4482 pending_xref_error (void)
4483 {
4484 if (pending_invalid_xref != NULL_TREE)
4485 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4486 pending_invalid_xref);
4487 pending_invalid_xref = NULL_TREE;
4488 }
4489
4490
4491 /* Look up NAME in the current scope and its superiors
4492 in the namespace of variables, functions and typedefs.
4493 Return a ..._DECL node of some kind representing its definition,
4494 or return NULL_TREE if it is undefined. */
4495
4496 tree
4497 lookup_name (tree name)
4498 {
4499 struct c_binding *b = I_SYMBOL_BINDING (name);
4500 if (b && !b->invisible)
4501 {
4502 maybe_record_typedef_use (b->decl);
4503 return b->decl;
4504 }
4505 return NULL_TREE;
4506 }
4507
4508 /* Similar to `lookup_name' but look only at the indicated scope. */
4509
4510 static tree
4511 lookup_name_in_scope (tree name, struct c_scope *scope)
4512 {
4513 struct c_binding *b;
4514
4515 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4516 if (B_IN_SCOPE (b, scope))
4517 return b->decl;
4518 return NULL_TREE;
4519 }
4520
4521 /* Look for the closest match for NAME within the currently valid
4522 scopes.
4523
4524 This finds the identifier with the lowest Levenshtein distance to
4525 NAME. If there are multiple candidates with equal minimal distance,
4526 the first one found is returned. Scopes are searched from innermost
4527 outwards, and within a scope in reverse order of declaration, thus
4528 benefiting candidates "near" to the current scope.
4529
4530 The function also looks for similar macro names to NAME, since a
4531 misspelled macro name will not be expanded, and hence looks like an
4532 identifier to the C frontend.
4533
4534 It also looks for start_typename keywords, to detect "singed" vs "signed"
4535 typos.
4536
4537 Use LOC for any deferred diagnostics. */
4538
4539 name_hint
4540 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4541 {
4542 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4543
4544 /* First, try some well-known names in the C standard library, in case
4545 the user forgot a #include. */
4546 const char *header_hint
4547 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4548
4549 if (header_hint)
4550 return name_hint (NULL,
4551 new suggest_missing_header (loc,
4552 IDENTIFIER_POINTER (name),
4553 header_hint));
4554
4555 /* Next, look for exact matches for builtin defines that would have been
4556 defined if the user had passed a command-line option (e.g. -fopenmp
4557 for "_OPENMP"). */
4558 diagnostic_option_id option_id
4559 = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4560 if (option_id.m_idx > 0)
4561 return name_hint (nullptr,
4562 new suggest_missing_option (loc,
4563 IDENTIFIER_POINTER (name),
4564 option_id));
4565
4566 /* Only suggest names reserved for the implementation if NAME begins
4567 with an underscore. */
4568 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4569
4570 best_match<tree, tree> bm (name);
4571
4572 /* Look within currently valid scopes. */
4573 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4574 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4575 {
4576 if (!binding->id || binding->invisible)
4577 continue;
4578 if (binding->decl == error_mark_node)
4579 continue;
4580 /* Don't use bindings from implicitly declared functions,
4581 as they were likely misspellings themselves. */
4582 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4583 if (C_DECL_IMPLICIT (binding->decl))
4584 continue;
4585 /* Don't suggest names that are reserved for use by the
4586 implementation, unless NAME began with an underscore. */
4587 if (!consider_implementation_names)
4588 {
4589 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4590 if (name_reserved_for_implementation_p (suggestion_str))
4591 continue;
4592 }
4593 switch (kind)
4594 {
4595 case FUZZY_LOOKUP_TYPENAME:
4596 if (TREE_CODE (binding->decl) != TYPE_DECL)
4597 continue;
4598 break;
4599
4600 case FUZZY_LOOKUP_FUNCTION_NAME:
4601 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4602 {
4603 /* Allow function pointers. */
4604 if ((VAR_P (binding->decl)
4605 || TREE_CODE (binding->decl) == PARM_DECL)
4606 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4607 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4608 == FUNCTION_TYPE))
4609 break;
4610 continue;
4611 }
4612 break;
4613
4614 default:
4615 break;
4616 }
4617 bm.consider (binding->id);
4618 }
4619
4620 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4621 as:
4622 x = SOME_OTHER_MACRO (y);
4623 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4624 as a misspelled identifier.
4625
4626 Use the best distance so far so that a candidate is only set if
4627 a macro is better than anything so far. This allows early rejection
4628 (without calculating the edit distance) of macro names that must have
4629 distance >= bm.get_best_distance (), and means that we only get a
4630 non-NULL result for best_macro_match if it's better than any of
4631 the identifiers already checked, which avoids needless creation
4632 of identifiers for macro hashnodes. */
4633 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4634 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4635 /* If a macro is the closest so far to NAME, use it, creating an
4636 identifier tree node for it. */
4637 if (best_macro)
4638 {
4639 const char *id = (const char *)best_macro->ident.str;
4640 tree macro_as_identifier
4641 = get_identifier_with_length (id, best_macro->ident.len);
4642 bm.set_best_so_far (macro_as_identifier,
4643 bmm.get_best_distance (),
4644 bmm.get_best_candidate_length ());
4645 }
4646
4647 /* Try the "start_typename" keywords to detect
4648 "singed" vs "signed" typos. */
4649 if (kind == FUZZY_LOOKUP_TYPENAME)
4650 {
4651 for (unsigned i = 0; i < num_c_common_reswords; i++)
4652 {
4653 const c_common_resword *resword = &c_common_reswords[i];
4654 if (!c_keyword_starts_typename (resword->rid))
4655 continue;
4656 tree resword_identifier = ridpointers [resword->rid];
4657 if (!resword_identifier)
4658 continue;
4659 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4660 bm.consider (resword_identifier);
4661 }
4662 }
4663
4664 tree best = bm.get_best_meaningful_candidate ();
4665 if (best)
4666 return name_hint (IDENTIFIER_POINTER (best), NULL);
4667 else
4668 return name_hint (NULL, NULL);
4669 }
4670
4671
4672 /* Handle the standard [[nodiscard]] attribute. */
4673
4674 static tree
4675 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4676 int /*flags*/, bool *no_add_attrs)
4677 {
4678 if (TREE_CODE (*node) == FUNCTION_DECL)
4679 {
4680 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4681 warning_at (DECL_SOURCE_LOCATION (*node),
4682 OPT_Wattributes, "%qE attribute applied to %qD with void "
4683 "return type", name, *node);
4684 }
4685 else if (RECORD_OR_UNION_TYPE_P (*node)
4686 || TREE_CODE (*node) == ENUMERAL_TYPE)
4687 /* OK */;
4688 else
4689 {
4690 pedwarn (input_location,
4691 OPT_Wattributes, "%qE attribute can only be applied to "
4692 "functions or to structure, union or enumeration types", name);
4693 *no_add_attrs = true;
4694 }
4695 return NULL_TREE;
4696 }
4697
4698 /* Handle the standard [[noreturn]] attribute. */
4699
4700 static tree
4701 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4702 int flags, bool *no_add_attrs)
4703 {
4704 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4705 only applies to functions, not function pointers. */
4706 if (TREE_CODE (*node) == FUNCTION_DECL)
4707 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4708 else
4709 {
4710 pedwarn (input_location, OPT_Wattributes,
4711 "standard %qE attribute can only be applied to functions",
4712 name);
4713 *no_add_attrs = true;
4714 return NULL_TREE;
4715 }
4716 }
4717
4718 /* Handle the standard [[unsequenced]] attribute. */
4719
4720 static tree
4721 handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4722 int flags, bool *no_add_attrs)
4723 {
4724 /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4725 should be only applied to function declarators or type specifiers which
4726 have function type. */
4727 if (node[2])
4728 {
4729 auto_diagnostic_group d;
4730 if (pedwarn (input_location, OPT_Wattributes,
4731 "standard %qE attribute can only be applied to function "
4732 "declarators or type specifiers with function type", name))
4733 inform (input_location, "did you mean to specify it after %<)%> "
4734 "following function parameters?");
4735 *no_add_attrs = true;
4736 return NULL_TREE;
4737 }
4738 return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4739 }
4740
4741 /* Handle the standard [[reproducible]] attribute. */
4742
4743 static tree
4744 handle_std_reproducible_attribute (tree *node, tree name, tree args,
4745 int flags, bool *no_add_attrs)
4746 {
4747 return handle_std_unsequenced_attribute (node, name, args, flags,
4748 no_add_attrs);
4749 }
4750
4751 /* Table of supported standard (C23) attributes. */
4752 static const attribute_spec std_attributes[] =
4753 {
4754 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4755 affects_type_identity, handler, exclude } */
4756 { "_Noreturn", 0, 0, false, false, false, false,
4757 handle_std_noreturn_attribute, NULL },
4758 { "deprecated", 0, 1, false, false, false, false,
4759 handle_deprecated_attribute, NULL },
4760 { "fallthrough", 0, 0, false, false, false, false,
4761 handle_fallthrough_attribute, NULL },
4762 { "maybe_unused", 0, 0, false, false, false, false,
4763 handle_unused_attribute, NULL },
4764 { "nodiscard", 0, 1, false, false, false, false,
4765 handle_nodiscard_attribute, NULL },
4766 { "noreturn", 0, 0, false, false, false, false,
4767 handle_std_noreturn_attribute, NULL },
4768 { "reproducible", 0, 0, false, true, true, false,
4769 handle_std_reproducible_attribute, NULL },
4770 { "unsequenced", 0, 0, false, true, true, false,
4771 handle_std_unsequenced_attribute, NULL }
4772 };
4773
4774 const scoped_attribute_specs std_attribute_table =
4775 {
4776 nullptr, { std_attributes }
4777 };
4778
4779 /* Create the predefined scalar types of C,
4780 and some nodes representing standard constants (0, 1, (void *) 0).
4781 Initialize the global scope.
4782 Make definitions for built-in primitive functions. */
4783
4784 void
4785 c_init_decl_processing (void)
4786 {
4787 location_t save_loc = input_location;
4788
4789 /* Initialize reserved words for parser. */
4790 c_parse_init ();
4791
4792 current_function_decl = NULL_TREE;
4793
4794 gcc_obstack_init (&parser_obstack);
4795
4796 /* Make the externals scope. */
4797 push_scope ();
4798 external_scope = current_scope;
4799
4800 /* Declarations from c_common_nodes_and_builtins must not be associated
4801 with this input file, lest we get differences between using and not
4802 using preprocessed headers. */
4803 input_location = BUILTINS_LOCATION;
4804
4805 c_common_nodes_and_builtins ();
4806
4807 /* In C, comparisons and TRUTH_* expressions have type int. */
4808 truthvalue_type_node = integer_type_node;
4809 truthvalue_true_node = integer_one_node;
4810 truthvalue_false_node = integer_zero_node;
4811
4812 /* Even in C99, which has a real boolean type. */
4813 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4814 boolean_type_node));
4815
4816 /* C-specific nullptr initialization. */
4817 record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4818 /* The size and alignment of nullptr_t is the same as for a pointer to
4819 character type. */
4820 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4821
4822 input_location = save_loc;
4823
4824 make_fname_decl = c_make_fname_decl;
4825 start_fname_decls ();
4826 }
4827
4828 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4829 give the decl, NAME is the initialization string and TYPE_DEP
4830 indicates whether NAME depended on the type of the function. As we
4831 don't yet implement delayed emission of static data, we mark the
4832 decl as emitted so it is not placed in the output. Anything using
4833 it must therefore pull out the STRING_CST initializer directly.
4834 FIXME. */
4835
4836 static tree
4837 c_make_fname_decl (location_t loc, tree id, int type_dep)
4838 {
4839 const char *name = fname_as_string (type_dep);
4840 tree decl, type, init;
4841 size_t length = strlen (name);
4842
4843 type = c_build_array_type (char_type_node,
4844 build_index_type (size_int (length)));
4845 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4846
4847 decl = build_decl (loc, VAR_DECL, id, type);
4848
4849 TREE_STATIC (decl) = 1;
4850 TREE_READONLY (decl) = 1;
4851 DECL_ARTIFICIAL (decl) = 1;
4852
4853 init = build_string (length + 1, name);
4854 free (CONST_CAST (char *, name));
4855 TREE_TYPE (init) = type;
4856 DECL_INITIAL (decl) = init;
4857
4858 TREE_USED (decl) = 1;
4859
4860 if (current_function_decl
4861 /* For invalid programs like this:
4862
4863 void foo()
4864 const char* p = __FUNCTION__;
4865
4866 the __FUNCTION__ is believed to appear in K&R style function
4867 parameter declarator. In that case we still don't have
4868 function_scope. */
4869 && current_function_scope)
4870 {
4871 DECL_CONTEXT (decl) = current_function_decl;
4872 bind (id, decl, current_function_scope,
4873 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4874 }
4875
4876 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4877
4878 return decl;
4879 }
4880
4881 tree
4882 c_builtin_function (tree decl)
4883 {
4884 tree type = TREE_TYPE (decl);
4885 tree id = DECL_NAME (decl);
4886
4887 const char *name = IDENTIFIER_POINTER (id);
4888 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4889
4890 /* Should never be called on a symbol with a preexisting meaning. */
4891 gcc_assert (!I_SYMBOL_BINDING (id));
4892
4893 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4894 UNKNOWN_LOCATION);
4895
4896 /* Builtins in the implementation namespace are made visible without
4897 needing to be explicitly declared. See push_file_scope. */
4898 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4899 {
4900 DECL_CHAIN (decl) = visible_builtins;
4901 visible_builtins = decl;
4902 }
4903
4904 return decl;
4905 }
4906
4907 tree
4908 c_builtin_function_ext_scope (tree decl)
4909 {
4910 tree type = TREE_TYPE (decl);
4911 tree id = DECL_NAME (decl);
4912
4913 const char *name = IDENTIFIER_POINTER (id);
4914 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4915
4916 if (external_scope)
4917 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4918 UNKNOWN_LOCATION);
4919
4920 /* Builtins in the implementation namespace are made visible without
4921 needing to be explicitly declared. See push_file_scope. */
4922 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4923 {
4924 DECL_CHAIN (decl) = visible_builtins;
4925 visible_builtins = decl;
4926 }
4927
4928 return decl;
4929 }
4930
4931 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4932
4933 tree
4934 c_simulate_builtin_function_decl (tree decl)
4935 {
4936 tree type = TREE_TYPE (decl);
4937 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4938 return pushdecl (decl);
4939 }
4940
4941 /* Warn about attributes in a context where they are unused
4942 (attribute-declarations, except for the "fallthrough" case, and
4943 attributes on statements). */
4944
4945 void
4946 c_warn_unused_attributes (tree attrs)
4947 {
4948 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4949 if (get_attribute_namespace (t) == NULL_TREE)
4950 /* The specifications of standard attributes mean this is a
4951 constraint violation. */
4952 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4953 get_attribute_name (t));
4954 else if (!attribute_ignored_p (t))
4955 warning (OPT_Wattributes, "%qE attribute ignored",
4956 get_attribute_name (t));
4957 }
4958
4959 /* Warn for standard attributes being applied to a type that is not
4960 being defined, where that is a constraint violation, and return a
4961 list of attributes with them removed. */
4962
4963 tree
4964 c_warn_type_attributes (tree type, tree attrs)
4965 {
4966 tree *attr_ptr = &attrs;
4967 while (*attr_ptr)
4968 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4969 {
4970 if (TREE_CODE (type) == FUNCTION_TYPE)
4971 {
4972 tree name = get_attribute_name (*attr_ptr);
4973 /* [[unsequenced]] and [[reproducible]] is fine on function
4974 types that aren't being defined. */
4975 if (is_attribute_p ("unsequenced", name)
4976 || is_attribute_p ("reproducible", name))
4977 {
4978 attr_ptr = &TREE_CHAIN (*attr_ptr);
4979 continue;
4980 }
4981 }
4982 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4983 get_attribute_name (*attr_ptr));
4984 *attr_ptr = TREE_CHAIN (*attr_ptr);
4985 }
4986 else
4987 attr_ptr = &TREE_CHAIN (*attr_ptr);
4988 return attrs;
4989 }
4990
4991 /* Called when a declaration is seen that contains no names to declare.
4992 If its type is a reference to a structure, union or enum inherited
4993 from a containing scope, shadow that tag name for the current scope
4994 with a forward reference.
4995 If its type defines a new named structure or union
4996 or defines an enum, it is valid but we need not do anything here.
4997 Otherwise, it is an error. */
4998
4999 void
5000 shadow_tag (const struct c_declspecs *declspecs)
5001 {
5002 shadow_tag_warned (declspecs, 0);
5003 }
5004
5005 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5006 but no pedwarn. */
5007 void
5008 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5009 {
5010 bool found_tag = false;
5011
5012 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5013 {
5014 tree value = declspecs->type;
5015 enum tree_code code = TREE_CODE (value);
5016
5017 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5018 /* Used to test also that TYPE_SIZE (value) != 0.
5019 That caused warning for `struct foo;' at top level in the file. */
5020 {
5021 tree name = TYPE_NAME (value);
5022 tree t;
5023
5024 found_tag = true;
5025
5026 if (declspecs->restrict_p)
5027 {
5028 error ("invalid use of %<restrict%>");
5029 warned = 1;
5030 }
5031
5032 if (in_underspecified_init)
5033 {
5034 /* This can only occur with extensions such as statement
5035 expressions, but is still appropriate as an error to
5036 avoid types declared in such a context escaping to
5037 the type of an auto variable. */
5038 error ("%qT declared in underspecified object initializer",
5039 value);
5040 warned = 1;
5041 }
5042
5043 if (name == NULL_TREE)
5044 {
5045 if (warned != 1 && code != ENUMERAL_TYPE)
5046 /* Empty unnamed enum OK */
5047 {
5048 pedwarn (input_location, 0,
5049 "unnamed struct/union that defines no instances");
5050 warned = 1;
5051 }
5052 }
5053 else if (declspecs->typespec_kind != ctsk_tagdef
5054 && declspecs->typespec_kind != ctsk_tagfirstref
5055 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5056 && declspecs->storage_class != csc_none)
5057 {
5058 if (warned != 1)
5059 pedwarn (input_location, 0,
5060 "empty declaration with storage class specifier "
5061 "does not redeclare tag");
5062 warned = 1;
5063 pending_xref_error ();
5064 }
5065 else if (declspecs->typespec_kind != ctsk_tagdef
5066 && declspecs->typespec_kind != ctsk_tagfirstref
5067 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5068 && (declspecs->const_p
5069 || declspecs->volatile_p
5070 || declspecs->atomic_p
5071 || declspecs->restrict_p
5072 || declspecs->address_space))
5073 {
5074 if (warned != 1)
5075 pedwarn (input_location, 0,
5076 "empty declaration with type qualifier "
5077 "does not redeclare tag");
5078 warned = 1;
5079 pending_xref_error ();
5080 }
5081 else if (declspecs->typespec_kind != ctsk_tagdef
5082 && declspecs->typespec_kind != ctsk_tagfirstref
5083 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5084 && declspecs->alignas_p)
5085 {
5086 if (warned != 1)
5087 pedwarn (input_location, 0,
5088 "empty declaration with %<_Alignas%> "
5089 "does not redeclare tag");
5090 warned = 1;
5091 pending_xref_error ();
5092 }
5093 else if (declspecs->typespec_kind != ctsk_tagdef
5094 && declspecs->typespec_kind != ctsk_tagfirstref
5095 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5096 && code == ENUMERAL_TYPE
5097 && !declspecs->enum_type_specifier_ref_p)
5098 {
5099 bool warned_enum = false;
5100 if (warned != 1)
5101 warned_enum = pedwarn (input_location, OPT_Wpedantic,
5102 "empty declaration of %<enum%> type "
5103 "does not redeclare tag");
5104 if (warned_enum)
5105 warned = 1;
5106 pending_xref_error ();
5107 }
5108 else
5109 {
5110 pending_invalid_xref = NULL_TREE;
5111 t = lookup_tag (code, name, true, NULL);
5112
5113 if (t == NULL_TREE)
5114 {
5115 t = make_node (code);
5116 if (flag_isoc23 || code == ENUMERAL_TYPE)
5117 SET_TYPE_STRUCTURAL_EQUALITY (t);
5118 pushtag (input_location, name, t);
5119 }
5120 }
5121 }
5122 else
5123 {
5124 if (warned != 1 && !in_system_header_at (input_location))
5125 {
5126 pedwarn (input_location, 0,
5127 "useless type name in empty declaration");
5128 warned = 1;
5129 }
5130 }
5131 }
5132 else if (warned != 1 && !in_system_header_at (input_location)
5133 && declspecs->typedef_p)
5134 {
5135 pedwarn (input_location, 0, "useless type name in empty declaration");
5136 warned = 1;
5137 }
5138
5139 pending_invalid_xref = NULL_TREE;
5140
5141 if (declspecs->inline_p)
5142 {
5143 error ("%<inline%> in empty declaration");
5144 warned = 1;
5145 }
5146
5147 if (declspecs->noreturn_p)
5148 {
5149 error ("%<_Noreturn%> in empty declaration");
5150 warned = 1;
5151 }
5152
5153 if (declspecs->constexpr_p)
5154 {
5155 error ("%<constexpr%> in empty declaration");
5156 warned = 1;
5157 }
5158
5159 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5160 {
5161 error ("%<auto%> in file-scope empty declaration");
5162 warned = 1;
5163 }
5164
5165 if (current_scope == file_scope && declspecs->storage_class == csc_register)
5166 {
5167 error ("%<register%> in file-scope empty declaration");
5168 warned = 1;
5169 }
5170
5171 if (declspecs->enum_type_specifier_ref_p && !warned)
5172 {
5173 if (declspecs->storage_class != csc_none)
5174 {
5175 error ("storage class specifier in empty declaration with %<enum%> "
5176 "underlying type");
5177 warned = 1;
5178 }
5179 else if (declspecs->thread_p)
5180 {
5181 error ("%qs in empty declaration with %<enum%> underlying type",
5182 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5183 warned = 1;
5184 }
5185 else if (declspecs->const_p
5186 || declspecs->volatile_p
5187 || declspecs->atomic_p
5188 || declspecs->restrict_p
5189 || declspecs->address_space)
5190 {
5191 error ("type qualifier in empty declaration with %<enum%> "
5192 "underlying type");
5193 warned = 1;
5194 }
5195 else if (declspecs->alignas_p)
5196 {
5197 error ("%<alignas%> in empty declaration with %<enum%> "
5198 "underlying type");
5199 warned = 1;
5200 }
5201 }
5202
5203 if (!warned && !in_system_header_at (input_location)
5204 && declspecs->storage_class != csc_none)
5205 {
5206 warning (0, "useless storage class specifier in empty declaration");
5207 warned = 2;
5208 }
5209
5210 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5211 {
5212 warning (0, "useless %qs in empty declaration",
5213 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5214 warned = 2;
5215 }
5216
5217 if (!warned
5218 && !in_system_header_at (input_location)
5219 && (declspecs->const_p
5220 || declspecs->volatile_p
5221 || declspecs->atomic_p
5222 || declspecs->restrict_p
5223 || declspecs->address_space))
5224 {
5225 warning (0, "useless type qualifier in empty declaration");
5226 warned = 2;
5227 }
5228
5229 if (!warned && !in_system_header_at (input_location)
5230 && declspecs->alignas_p)
5231 {
5232 warning (0, "useless %<_Alignas%> in empty declaration");
5233 warned = 2;
5234 }
5235
5236 if (found_tag
5237 && warned == 2
5238 && (declspecs->typespec_kind == ctsk_tagref_attrs
5239 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5240 {
5241 /* Standard attributes after the "struct" or "union" keyword are
5242 only permitted when the contents of the type are defined, or
5243 in the form "struct-or-union attribute-specifier-sequence
5244 identifier;". If the ';' was not present, attributes were
5245 diagnosed in the parser. Here, ensure that any other useless
5246 elements of the declaration result in a pedwarn, not just a
5247 warning. Forward declarations of enum types are not part of
5248 standard C, but handle them the same. */
5249 pedwarn (input_location, 0,
5250 "invalid use of attributes in empty declaration");
5251 warned = 1;
5252 }
5253
5254 if (warned != 1)
5255 {
5256 if (declspecs->declspecs_seen_p
5257 && !declspecs->non_std_attrs_seen_p)
5258 /* An attribute declaration (but not a fallthrough attribute
5259 declaration, which was handled separately); warn if there
5260 are any attributes being ignored (but not if the attributes
5261 were empty). */
5262 c_warn_unused_attributes (declspecs->attrs);
5263 else if (!found_tag)
5264 pedwarn (input_location, 0, "empty declaration");
5265 }
5266 }
5267
5268
5269 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5270 bits. SPECS represents declaration specifiers that the grammar
5271 only permits to contain type qualifiers and attributes. */
5272
5273 int
5274 quals_from_declspecs (const struct c_declspecs *specs)
5275 {
5276 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5277 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5278 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5279 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5280 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5281 gcc_assert (!specs->type
5282 && !specs->decl_attr
5283 && specs->typespec_word == cts_none
5284 && specs->storage_class == csc_none
5285 && !specs->typedef_p
5286 && !specs->explicit_signed_p
5287 && !specs->deprecated_p
5288 && !specs->unavailable_p
5289 && !specs->long_p
5290 && !specs->long_long_p
5291 && !specs->short_p
5292 && !specs->signed_p
5293 && !specs->unsigned_p
5294 && !specs->complex_p
5295 && !specs->inline_p
5296 && !specs->noreturn_p
5297 && !specs->thread_p);
5298 return quals;
5299 }
5300
5301 /* Construct an array declarator. LOC is the location of the
5302 beginning of the array (usually the opening brace). EXPR is the
5303 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5304 inside the [] (to be applied to the pointer to which a parameter
5305 array is converted). STATIC_P is true if "static" is inside the
5306 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5307 VLA of unspecified length which is nevertheless a complete type,
5308 false otherwise. The field for the contained declarator is left to
5309 be filled in by set_array_declarator_inner. */
5310
5311 struct c_declarator *
5312 build_array_declarator (location_t loc,
5313 tree expr, struct c_declspecs *quals, bool static_p,
5314 bool vla_unspec_p)
5315 {
5316 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5317 struct c_declarator);
5318 declarator->id_loc = loc;
5319 declarator->kind = cdk_array;
5320 declarator->declarator = 0;
5321 declarator->u.array.dimen = expr;
5322 if (quals)
5323 {
5324 declarator->u.array.attrs = quals->attrs;
5325 declarator->u.array.quals = quals_from_declspecs (quals);
5326 }
5327 else
5328 {
5329 declarator->u.array.attrs = NULL_TREE;
5330 declarator->u.array.quals = 0;
5331 }
5332 declarator->u.array.static_p = static_p;
5333 declarator->u.array.vla_unspec_p = vla_unspec_p;
5334 if (static_p || quals != NULL)
5335 pedwarn_c90 (loc, OPT_Wpedantic,
5336 "ISO C90 does not support %<static%> or type "
5337 "qualifiers in parameter array declarators");
5338 if (vla_unspec_p)
5339 pedwarn_c90 (loc, OPT_Wpedantic,
5340 "ISO C90 does not support %<[*]%> array declarators");
5341 if (vla_unspec_p)
5342 {
5343 if (!current_scope->parm_flag)
5344 {
5345 /* C99 6.7.5.2p4 */
5346 error_at (loc, "%<[*]%> not allowed in other than "
5347 "function prototype scope");
5348 declarator->u.array.vla_unspec_p = false;
5349 return NULL;
5350 }
5351 current_scope->had_vla_unspec = true;
5352 }
5353 return declarator;
5354 }
5355
5356 /* Set the contained declarator of an array declarator. DECL is the
5357 declarator, as constructed by build_array_declarator; INNER is what
5358 appears on the left of the []. */
5359
5360 struct c_declarator *
5361 set_array_declarator_inner (struct c_declarator *decl,
5362 struct c_declarator *inner)
5363 {
5364 decl->declarator = inner;
5365 return decl;
5366 }
5367
5368 /* Determine whether TYPE is a one-element array type "[1]". */
5369 static bool
5370 one_element_array_type_p (const_tree type)
5371 {
5372 if (TREE_CODE (type) != ARRAY_TYPE)
5373 return false;
5374 return integer_zerop (array_type_nelts_minus_one (type));
5375 }
5376
5377 /* Determine whether TYPE is a zero-length array type "[0]". */
5378 static bool
5379 zero_length_array_type_p (const_tree type)
5380 {
5381 if (TREE_CODE (type) == ARRAY_TYPE)
5382 if (tree type_size = TYPE_SIZE_UNIT (type))
5383 if ((integer_zerop (type_size))
5384 && TYPE_DOMAIN (type) != NULL_TREE
5385 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5386 return true;
5387 return false;
5388 }
5389
5390 /* INIT is a constructor that forms DECL's initializer. If the final
5391 element initializes a flexible array field, adjust the size of the
5392 DECL with the initializer based on whether the DECL is a union or
5393 a structure. */
5394
5395 static void
5396 add_flexible_array_elts_to_size (tree decl, tree init)
5397 {
5398 tree elt, type;
5399
5400 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5401 return;
5402
5403 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5404 type = TREE_TYPE (elt);
5405 if (c_flexible_array_member_type_p (type))
5406 {
5407 complete_array_type (&type, elt, false);
5408 /* For a structure, add the size of the initializer to the DECL's
5409 size. */
5410 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5411 {
5412 DECL_SIZE (decl)
5413 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5414 DECL_SIZE_UNIT (decl)
5415 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5416 TYPE_SIZE_UNIT (type));
5417 }
5418 /* For a union, the DECL's size is the maximum of the current size
5419 and the size of the initializer. */
5420 else
5421 {
5422 DECL_SIZE (decl)
5423 = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5424 DECL_SIZE_UNIT (decl)
5425 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5426 TYPE_SIZE_UNIT (type));
5427 }
5428 }
5429 }
5430
5431 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5432 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5433 before the type name, and set *EXPR_CONST_OPERANDS, if
5434 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5435 appear in a constant expression. */
5436
5437 tree
5438 groktypename (struct c_type_name *type_name, tree *expr,
5439 bool *expr_const_operands)
5440 {
5441 tree type;
5442 tree attrs = type_name->specs->attrs;
5443
5444 type_name->specs->attrs = NULL_TREE;
5445
5446 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5447 false, NULL, &attrs, expr, expr_const_operands,
5448 DEPRECATED_NORMAL);
5449
5450 /* Apply attributes. */
5451 attrs = c_warn_type_attributes (type, attrs);
5452 decl_attributes (&type, attrs, 0);
5453
5454 return type;
5455 }
5456
5457 /* Looks up the most recent pushed declaration corresponding to DECL. */
5458
5459 static tree
5460 lookup_last_decl (tree decl)
5461 {
5462 tree last_decl = lookup_name (DECL_NAME (decl));
5463 if (!last_decl)
5464 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5465 return last_decl;
5466 }
5467
5468 /* Wrapper for decl_attributes that adds some implicit attributes
5469 to VAR_DECLs or FUNCTION_DECLs. */
5470
5471 static tree
5472 c_decl_attributes (tree *node, tree attributes, int flags)
5473 {
5474 /* Add implicit "omp declare target" attribute if requested. */
5475 if (vec_safe_length (current_omp_declare_target_attribute)
5476 && ((VAR_P (*node) && is_global_var (*node))
5477 || TREE_CODE (*node) == FUNCTION_DECL))
5478 {
5479 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5480 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5481 NULL_TREE, attributes);
5482 else
5483 attributes = tree_cons (get_identifier ("omp declare target"),
5484 NULL_TREE, attributes);
5485 if (TREE_CODE (*node) == FUNCTION_DECL)
5486 {
5487 int device_type
5488 = current_omp_declare_target_attribute->last ().device_type;
5489 device_type = MAX (device_type, 0);
5490 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5491 && !lookup_attribute ("omp declare target host", attributes))
5492 attributes
5493 = tree_cons (get_identifier ("omp declare target host"),
5494 NULL_TREE, attributes);
5495 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5496 && !lookup_attribute ("omp declare target nohost", attributes))
5497 attributes
5498 = tree_cons (get_identifier ("omp declare target nohost"),
5499 NULL_TREE, attributes);
5500
5501 int indirect
5502 = current_omp_declare_target_attribute->last ().indirect;
5503 if (indirect && !lookup_attribute ("omp declare target indirect",
5504 attributes))
5505 attributes
5506 = tree_cons (get_identifier ("omp declare target indirect"),
5507 NULL_TREE, attributes);
5508 }
5509 }
5510
5511 if (flag_openmp || flag_openmp_simd)
5512 {
5513 bool diagnosed = false;
5514 for (tree *pa = &attributes; *pa; )
5515 {
5516 if (is_attribute_namespace_p ("omp", *pa))
5517 {
5518 tree name = get_attribute_name (*pa);
5519 if (is_attribute_p ("directive", name)
5520 || is_attribute_p ("sequence", name)
5521 || is_attribute_p ("decl", name))
5522 {
5523 const char *p = NULL;
5524 if (TREE_VALUE (*pa) == NULL_TREE)
5525 p = IDENTIFIER_POINTER (name);
5526 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5527 {
5528 tree d = TREE_VALUE (a);
5529 gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5530 if (TREE_PUBLIC (d)
5531 && (VAR_P (*node)
5532 || TREE_CODE (*node) == FUNCTION_DECL)
5533 && c_maybe_parse_omp_decl (*node, d))
5534 continue;
5535 p = TREE_PUBLIC (d) ? "decl" : "directive";
5536 }
5537 if (p && !diagnosed)
5538 {
5539 error ("%<omp::%s%> not allowed to be specified in "
5540 "this context", p);
5541 diagnosed = true;
5542 }
5543 if (p)
5544 {
5545 *pa = TREE_CHAIN (*pa);
5546 continue;
5547 }
5548 }
5549 }
5550 pa = &TREE_CHAIN (*pa);
5551 }
5552 }
5553
5554 /* Look up the current declaration with all the attributes merged
5555 so far so that attributes on the current declaration that's
5556 about to be pushed that conflict with the former can be detected,
5557 diagnosed, and rejected as appropriate. To match the C++ FE, do
5558 not pass an error_mark_node when we found an undeclared variable. */
5559 tree last_decl = lookup_last_decl (*node);
5560 if (last_decl == error_mark_node)
5561 last_decl = NULL_TREE;
5562 return decl_attributes (node, attributes, flags, last_decl);
5563 }
5564
5565
5566 /* Decode a declarator in an ordinary declaration or data definition.
5567 This is called as soon as the type information and variable name
5568 have been parsed, before parsing the initializer if any.
5569 Here we create the ..._DECL node, fill in its type,
5570 and (if DO_PUSH) put it on the list of decls for the current context.
5571 When nonnull, set *LASTLOC to the location of the prior declaration
5572 of the same entity if one exists.
5573 The ..._DECL node is returned as the value.
5574
5575 Exception: for arrays where the length is not specified,
5576 the type is left null, to be filled in by `finish_decl'.
5577
5578 Function definitions do not come here; they go to start_function
5579 instead. However, external and forward declarations of functions
5580 do go through here. Structure field declarations are done by
5581 grokfield and not through here. */
5582
5583 tree
5584 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5585 bool initialized, tree attributes, bool do_push /* = true */,
5586 location_t *lastloc /* = NULL */)
5587 {
5588 tree decl;
5589 tree old_decl;
5590 tree tem;
5591 tree expr = NULL_TREE;
5592 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5593
5594 /* An object declared as __attribute__((unavailable)) suppresses
5595 warnings and errors from __attribute__((deprecated/unavailable))
5596 components.
5597 An object declared as __attribute__((deprecated)) suppresses
5598 warnings of uses of other deprecated items. */
5599 if (lookup_attribute ("unavailable", attributes))
5600 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5601 else if (lookup_attribute ("deprecated", attributes))
5602 deprecated_state = DEPRECATED_SUPPRESS;
5603
5604 decl = grokdeclarator (declarator, declspecs,
5605 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5606 deprecated_state);
5607 if (!decl || decl == error_mark_node)
5608 return NULL_TREE;
5609
5610 old_decl = lookup_last_decl (decl);
5611
5612 if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5613 if (lastdecl != error_mark_node)
5614 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5615
5616 /* Make sure the size expression is evaluated at this point. */
5617 if (expr && !current_scope->parm_flag)
5618 add_stmt (fold_convert (void_type_node, expr));
5619
5620 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5621 && TREE_PUBLIC (decl))
5622 warning (OPT_Wmain, "%q+D is usually a function", decl);
5623
5624 if (warn_missing_variable_declarations && VAR_P (decl)
5625 && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5626 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5627 "no previous declaration for %qD", decl);
5628
5629 if (initialized)
5630 /* Is it valid for this decl to have an initializer at all?
5631 If not, set INITIALIZED to zero, which will indirectly
5632 tell 'finish_decl' to ignore the initializer once it is parsed. */
5633 switch (TREE_CODE (decl))
5634 {
5635 case TYPE_DECL:
5636 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5637 initialized = false;
5638 break;
5639
5640 case FUNCTION_DECL:
5641 error ("function %qD is initialized like a variable", decl);
5642 initialized = false;
5643 break;
5644
5645 case PARM_DECL:
5646 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5647 error ("parameter %qD is initialized", decl);
5648 initialized = false;
5649 break;
5650
5651 default:
5652 /* Don't allow initializations for incomplete types except for
5653 arrays which might be completed by the initialization. */
5654
5655 /* This can happen if the array size is an undefined macro.
5656 We already gave a warning, so we don't need another one. */
5657 if (TREE_TYPE (decl) == error_mark_node)
5658 initialized = false;
5659 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5660 {
5661 /* A complete type is ok if size is fixed. If the size is
5662 variable, an empty initializer is OK and nonempty
5663 initializers will be diagnosed in the parser. */
5664 }
5665 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5666 {
5667 error ("variable %qD has initializer but incomplete type", decl);
5668 initialized = false;
5669 }
5670 }
5671
5672 if (initialized)
5673 {
5674 if (current_scope == file_scope)
5675 TREE_STATIC (decl) = 1;
5676
5677 /* Tell 'pushdecl' this is an initialized decl
5678 even though we don't yet have the initializer expression.
5679 Also tell 'finish_decl' it may store the real initializer. */
5680 DECL_INITIAL (decl) = error_mark_node;
5681 }
5682
5683 /* If this is a function declaration, write a record describing it to the
5684 prototypes file (if requested). */
5685
5686 if (TREE_CODE (decl) == FUNCTION_DECL)
5687 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5688
5689 /* ANSI specifies that a tentative definition which is not merged with
5690 a non-tentative definition behaves exactly like a definition with an
5691 initializer equal to zero. (Section 3.7.2)
5692
5693 -fno-common gives strict ANSI behavior, though this tends to break
5694 a large body of code that grew up without this rule.
5695
5696 Thread-local variables are never common, since there's no entrenched
5697 body of code to break, and it allows more efficient variable references
5698 in the presence of dynamic linking. */
5699
5700 if (VAR_P (decl)
5701 && !initialized
5702 && TREE_PUBLIC (decl)
5703 && !DECL_THREAD_LOCAL_P (decl)
5704 && !flag_no_common)
5705 DECL_COMMON (decl) = 1;
5706
5707 /* Set attributes here so if duplicate decl, will have proper attributes. */
5708 c_decl_attributes (&decl, attributes, 0);
5709
5710 /* Handle gnu_inline attribute. */
5711 if (declspecs->inline_p
5712 && !flag_gnu89_inline
5713 && TREE_CODE (decl) == FUNCTION_DECL
5714 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5715 || current_function_decl))
5716 {
5717 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5718 ;
5719 else if (declspecs->storage_class != csc_static)
5720 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5721 }
5722
5723 if (TREE_CODE (decl) == FUNCTION_DECL
5724 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5725 {
5726 struct c_declarator *ce = declarator;
5727
5728 if (ce->kind == cdk_pointer)
5729 ce = declarator->declarator;
5730 if (ce->kind == cdk_function)
5731 {
5732 tree args = ce->u.arg_info->parms;
5733 for (; args; args = DECL_CHAIN (args))
5734 {
5735 tree type = TREE_TYPE (args);
5736 if (type && INTEGRAL_TYPE_P (type)
5737 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5738 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5739 }
5740 }
5741 }
5742
5743 if (TREE_CODE (decl) == FUNCTION_DECL
5744 && DECL_DECLARED_INLINE_P (decl)
5745 && DECL_UNINLINABLE (decl)
5746 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5747 {
5748 auto_urlify_attributes sentinel;
5749 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5750 decl, "noinline");
5751 }
5752
5753 /* C99 6.7.4p3: An inline definition of a function with external
5754 linkage shall not contain a definition of a modifiable object
5755 with static storage duration... */
5756 if (VAR_P (decl)
5757 && current_scope != file_scope
5758 && TREE_STATIC (decl)
5759 && !TREE_READONLY (decl)
5760 && DECL_DECLARED_INLINE_P (current_function_decl)
5761 && DECL_EXTERNAL (current_function_decl))
5762 record_inline_static (input_location, current_function_decl,
5763 decl, csi_modifiable);
5764
5765 if (c_dialect_objc ()
5766 && VAR_OR_FUNCTION_DECL_P (decl))
5767 objc_check_global_decl (decl);
5768
5769 /* Add this decl to the current scope.
5770 TEM may equal DECL or it may be a previous decl of the same name. */
5771 if (do_push)
5772 {
5773 tem = pushdecl (decl);
5774
5775 if (initialized && DECL_EXTERNAL (tem))
5776 {
5777 DECL_EXTERNAL (tem) = 0;
5778 TREE_STATIC (tem) = 1;
5779 }
5780
5781 return tem;
5782 }
5783 else
5784 return decl;
5785 }
5786
5787 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5788 DECL or the non-array element type if DECL is an uninitialized array.
5789 If that type has a const member, diagnose this. */
5790
5791 static void
5792 diagnose_uninitialized_cst_member (tree decl, tree type)
5793 {
5794 tree field;
5795 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5796 {
5797 tree field_type;
5798 if (TREE_CODE (field) != FIELD_DECL)
5799 continue;
5800 field_type = strip_array_types (TREE_TYPE (field));
5801
5802 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5803 {
5804 auto_diagnostic_group d;
5805 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5806 "uninitialized const member in %qT is invalid in C++",
5807 strip_array_types (TREE_TYPE (decl))))
5808 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5809 }
5810
5811 if (RECORD_OR_UNION_TYPE_P (field_type))
5812 diagnose_uninitialized_cst_member (decl, field_type);
5813 }
5814 }
5815
5816 /* Finish processing of a declaration;
5817 install its initial value.
5818 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5819 If the length of an array type is not known before,
5820 it must be determined now, from the initial value, or it is an error.
5821
5822 INIT_LOC is the location of the initial value. */
5823
5824 void
5825 finish_decl (tree decl, location_t init_loc, tree init,
5826 tree origtype, tree asmspec_tree)
5827 {
5828 tree type;
5829 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5830 const char *asmspec = 0;
5831
5832 /* If a name was specified, get the string. */
5833 if (VAR_OR_FUNCTION_DECL_P (decl)
5834 && DECL_FILE_SCOPE_P (decl))
5835 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5836 if (asmspec_tree)
5837 asmspec = TREE_STRING_POINTER (asmspec_tree);
5838
5839 if (VAR_P (decl)
5840 && TREE_STATIC (decl)
5841 && global_bindings_p ())
5842 /* So decl is a global variable. Record the types it uses
5843 so that we can decide later to emit debug info for them. */
5844 record_types_used_by_current_var_decl (decl);
5845
5846 /* If `start_decl' didn't like having an initialization, ignore it now. */
5847 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5848 init = NULL_TREE;
5849
5850 /* Don't crash if parm is initialized. */
5851 if (TREE_CODE (decl) == PARM_DECL)
5852 init = NULL_TREE;
5853
5854 if (init)
5855 store_init_value (init_loc, decl, init, origtype);
5856
5857 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5858 || TREE_CODE (decl) == FIELD_DECL))
5859 objc_check_decl (decl);
5860
5861 type = TREE_TYPE (decl);
5862
5863 /* Deduce size of array from initialization, if not already known.
5864 This is only needed for an initialization in the current scope;
5865 it must not be done for a file-scope initialization of a
5866 declaration with external linkage, redeclared in an inner scope
5867 with the outer declaration shadowed in an intermediate scope. */
5868 if (TREE_CODE (type) == ARRAY_TYPE
5869 && TYPE_DOMAIN (type) == NULL_TREE
5870 && TREE_CODE (decl) != TYPE_DECL
5871 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5872 {
5873 bool do_default
5874 = (TREE_STATIC (decl)
5875 /* Even if pedantic, an external linkage array
5876 may have incomplete type at first. */
5877 ? pedantic && !TREE_PUBLIC (decl)
5878 : !DECL_EXTERNAL (decl));
5879 int failure
5880 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5881 do_default);
5882
5883 /* Get the completed type made by complete_array_type. */
5884 type = TREE_TYPE (decl);
5885
5886 switch (failure)
5887 {
5888 case 1:
5889 error ("initializer fails to determine size of %q+D", decl);
5890 break;
5891
5892 case 2:
5893 if (do_default)
5894 error ("array size missing in %q+D", decl);
5895 break;
5896
5897 case 3:
5898 error ("zero or negative size array %q+D", decl);
5899 break;
5900
5901 case 0:
5902 /* For global variables, update the copy of the type that
5903 exists in the binding. */
5904 if (TREE_PUBLIC (decl))
5905 {
5906 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5907 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5908 b_ext = b_ext->shadowed;
5909 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5910 {
5911 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5912 b_ext->u.type = composite_type (b_ext->u.type, type);
5913 else
5914 b_ext->u.type = type;
5915 }
5916 }
5917 break;
5918
5919 default:
5920 gcc_unreachable ();
5921 }
5922
5923 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5924 TREE_TYPE (DECL_INITIAL (decl)) = type;
5925
5926 relayout_decl (decl);
5927 }
5928
5929 /* Look for braced array initializers for character arrays and
5930 recursively convert them into STRING_CSTs. */
5931 if (tree init = DECL_INITIAL (decl))
5932 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5933
5934 if (VAR_P (decl))
5935 {
5936 if (init && TREE_CODE (init) == CONSTRUCTOR)
5937 add_flexible_array_elts_to_size (decl, init);
5938
5939 complete_flexible_array_elts (DECL_INITIAL (decl));
5940
5941 if (is_global_var (decl))
5942 {
5943 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5944 ? TCTX_THREAD_STORAGE
5945 : TCTX_STATIC_STORAGE);
5946 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5947 TREE_TYPE (decl) = error_mark_node;
5948 }
5949
5950 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5951 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5952 layout_decl (decl, 0);
5953
5954 if (DECL_SIZE (decl) == NULL_TREE
5955 /* Don't give an error if we already gave one earlier. */
5956 && TREE_TYPE (decl) != error_mark_node
5957 && (TREE_STATIC (decl)
5958 /* A static variable with an incomplete type
5959 is an error if it is initialized.
5960 Also if it is not file scope.
5961 Also if it is thread-local (in C23).
5962 Otherwise, let it through, but if it is not `extern'
5963 then it may cause an error message later. */
5964 ? (DECL_INITIAL (decl) != NULL_TREE
5965 || !DECL_FILE_SCOPE_P (decl)
5966 || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
5967 /* An automatic variable with an incomplete type
5968 is an error. */
5969 : !DECL_EXTERNAL (decl)))
5970 {
5971 error ("storage size of %q+D isn%'t known", decl);
5972 TREE_TYPE (decl) = error_mark_node;
5973 }
5974
5975 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5976 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5977 && DECL_SIZE (decl) == NULL_TREE
5978 && TREE_STATIC (decl))
5979 incomplete_record_decls.safe_push (decl);
5980
5981 if (is_global_var (decl)
5982 && DECL_SIZE (decl) != NULL_TREE
5983 && TREE_TYPE (decl) != error_mark_node)
5984 {
5985 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5986 constant_expression_warning (DECL_SIZE (decl));
5987 else
5988 {
5989 error ("storage size of %q+D isn%'t constant", decl);
5990 TREE_TYPE (decl) = error_mark_node;
5991 }
5992 }
5993
5994 if (TREE_USED (type))
5995 {
5996 TREE_USED (decl) = 1;
5997 DECL_READ_P (decl) = 1;
5998 }
5999 }
6000
6001 /* If this is a function and an assembler name is specified, reset DECL_RTL
6002 so we can give it its new name. Also, update builtin_decl if it
6003 was a normal built-in. */
6004 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6005 {
6006 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6007 set_builtin_user_assembler_name (decl, asmspec);
6008 set_user_assembler_name (decl, asmspec);
6009 }
6010
6011 /* If #pragma weak was used, mark the decl weak now. */
6012 maybe_apply_pragma_weak (decl);
6013
6014 /* Output the assembler code and/or RTL code for variables and functions,
6015 unless the type is an undefined structure or union.
6016 If not, it will get done when the type is completed. */
6017
6018 if (VAR_OR_FUNCTION_DECL_P (decl))
6019 {
6020 /* Determine the ELF visibility. */
6021 if (TREE_PUBLIC (decl))
6022 c_determine_visibility (decl);
6023
6024 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6025 if (c_dialect_objc ())
6026 objc_check_decl (decl);
6027
6028 if (asmspec)
6029 {
6030 /* If this is not a static variable, issue a warning.
6031 It doesn't make any sense to give an ASMSPEC for an
6032 ordinary, non-register local variable. Historically,
6033 GCC has accepted -- but ignored -- the ASMSPEC in
6034 this case. */
6035 if (!DECL_FILE_SCOPE_P (decl)
6036 && VAR_P (decl)
6037 && !C_DECL_REGISTER (decl)
6038 && !TREE_STATIC (decl))
6039 warning (0, "ignoring %<asm%> specifier for non-static local "
6040 "variable %q+D", decl);
6041 else
6042 set_user_assembler_name (decl, asmspec);
6043 }
6044
6045 if (DECL_FILE_SCOPE_P (decl))
6046 {
6047 if (DECL_INITIAL (decl) == NULL_TREE
6048 || DECL_INITIAL (decl) == error_mark_node)
6049 /* Don't output anything
6050 when a tentative file-scope definition is seen.
6051 But at end of compilation, do output code for them. */
6052 DECL_DEFER_OUTPUT (decl) = 1;
6053 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6054 DECL_HARD_REGISTER (decl) = 1;
6055 rest_of_decl_compilation (decl, true, 0);
6056
6057 if (TREE_CODE (decl) == FUNCTION_DECL)
6058 {
6059 tree parms = DECL_ARGUMENTS (decl);
6060 const bool builtin = fndecl_built_in_p (decl);
6061 if (tree access = build_attr_access_from_parms (parms, !builtin))
6062 decl_attributes (&decl, access, 0);
6063 }
6064 }
6065 else
6066 {
6067 /* In conjunction with an ASMSPEC, the `register'
6068 keyword indicates that we should place the variable
6069 in a particular register. */
6070 if (asmspec && C_DECL_REGISTER (decl))
6071 {
6072 DECL_HARD_REGISTER (decl) = 1;
6073 /* This cannot be done for a structure with volatile
6074 fields, on which DECL_REGISTER will have been
6075 reset. */
6076 if (!DECL_REGISTER (decl))
6077 error ("cannot put object with volatile field into register");
6078 }
6079
6080 if (TREE_CODE (decl) != FUNCTION_DECL)
6081 {
6082 /* If we're building a variable sized type, and we might be
6083 reachable other than via the top of the current binding
6084 level, then create a new BIND_EXPR so that we deallocate
6085 the object at the right time. */
6086 /* Note that DECL_SIZE can be null due to errors. */
6087 if (DECL_SIZE (decl)
6088 && !TREE_CONSTANT (DECL_SIZE (decl))
6089 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6090 {
6091 tree bind;
6092 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6093 TREE_SIDE_EFFECTS (bind) = 1;
6094 add_stmt (bind);
6095 BIND_EXPR_BODY (bind) = push_stmt_list ();
6096 }
6097 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6098 DECL_EXPR, decl));
6099 }
6100 }
6101
6102
6103 if (!DECL_FILE_SCOPE_P (decl))
6104 {
6105 /* Recompute the RTL of a local array now
6106 if it used to be an incomplete type. */
6107 if (was_incomplete && !is_global_var (decl))
6108 {
6109 /* If we used it already as memory, it must stay in memory. */
6110 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6111 /* If it's still incomplete now, no init will save it. */
6112 if (DECL_SIZE (decl) == NULL_TREE)
6113 DECL_INITIAL (decl) = NULL_TREE;
6114 }
6115 }
6116 }
6117
6118 if (TREE_CODE (decl) == TYPE_DECL)
6119 {
6120 if (!DECL_FILE_SCOPE_P (decl)
6121 && c_type_variably_modified_p (TREE_TYPE (decl)))
6122 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6123
6124 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6125 }
6126
6127 /* Install a cleanup (aka destructor) if one was given. */
6128 if (VAR_P (decl) && !TREE_STATIC (decl))
6129 {
6130 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6131 if (attr)
6132 {
6133 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6134 tree cleanup_decl = lookup_name (cleanup_id);
6135 tree cleanup;
6136 vec<tree, va_gc> *v;
6137
6138 /* Build "cleanup(&decl)" for the destructor. */
6139 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6140 vec_alloc (v, 1);
6141 v->quick_push (cleanup);
6142 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6143 vNULL, cleanup_decl, v, NULL);
6144 vec_free (v);
6145
6146 /* Don't warn about decl unused; the cleanup uses it. */
6147 TREE_USED (decl) = 1;
6148 TREE_USED (cleanup_decl) = 1;
6149 DECL_READ_P (decl) = 1;
6150
6151 push_cleanup (decl, cleanup, false);
6152 }
6153 }
6154
6155 if (warn_cxx_compat
6156 && VAR_P (decl)
6157 && !DECL_EXTERNAL (decl)
6158 && DECL_INITIAL (decl) == NULL_TREE)
6159 {
6160 type = strip_array_types (type);
6161 if (TREE_READONLY (decl))
6162 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6163 "uninitialized %<const %D%> is invalid in C++", decl);
6164 else if (RECORD_OR_UNION_TYPE_P (type)
6165 && C_TYPE_FIELDS_READONLY (type))
6166 diagnose_uninitialized_cst_member (decl, type);
6167 }
6168
6169 if (flag_openmp
6170 && VAR_P (decl)
6171 && lookup_attribute ("omp declare target implicit",
6172 DECL_ATTRIBUTES (decl)))
6173 {
6174 DECL_ATTRIBUTES (decl)
6175 = remove_attribute ("omp declare target implicit",
6176 DECL_ATTRIBUTES (decl));
6177 if (!omp_mappable_type (TREE_TYPE (decl)))
6178 error ("%q+D in declare target directive does not have mappable type",
6179 decl);
6180 else if (!lookup_attribute ("omp declare target",
6181 DECL_ATTRIBUTES (decl))
6182 && !lookup_attribute ("omp declare target link",
6183 DECL_ATTRIBUTES (decl)))
6184 {
6185 DECL_ATTRIBUTES (decl)
6186 = tree_cons (get_identifier ("omp declare target"),
6187 NULL_TREE, DECL_ATTRIBUTES (decl));
6188 symtab_node *node = symtab_node::get (decl);
6189 if (node != NULL)
6190 {
6191 node->offloadable = 1;
6192 if (ENABLE_OFFLOADING)
6193 {
6194 g->have_offload = true;
6195 if (is_a <varpool_node *> (node))
6196 vec_safe_push (offload_vars, decl);
6197 }
6198 }
6199 }
6200 }
6201
6202 /* This is the last point we can lower alignment so give the target the
6203 chance to do so. */
6204 if (VAR_P (decl)
6205 && !is_global_var (decl)
6206 && !DECL_HARD_REGISTER (decl))
6207 targetm.lower_local_decl_alignment (decl);
6208
6209 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6210 }
6211
6212 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6213 EXPR is NULL or a pointer to an expression that needs to be
6214 evaluated for the side effects of array size expressions in the
6215 parameters. */
6216
6217 tree
6218 grokparm (const struct c_parm *parm, tree *expr)
6219 {
6220 tree attrs = parm->attrs;
6221 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6222 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6223
6224 decl_attributes (&decl, attrs, 0);
6225
6226 return decl;
6227 }
6228
6229 /* Return attribute "arg spec" corresponding to an array/VLA parameter
6230 described by PARM, concatenated onto attributes ATTRS.
6231 The spec consists of one dollar symbol for each specified variable
6232 bound, one asterisk for each unspecified variable bound, followed
6233 by at most one specification of the most significant bound of
6234 an ordinary array parameter. For ordinary arrays the specification
6235 is either the constant bound itself, or the space character for
6236 an array with an unspecified bound (the [] form). Finally, a chain
6237 of specified variable bounds is appended to the spec, starting with
6238 the most significant bound. For example, the PARM T a[2][m][3][n]
6239 will produce __attribute__((arg spec ("[$$2]", m, n)).
6240 For T a typedef for an array with variable bounds, the bounds are
6241 included in the specification in the expected order.
6242 No "arg spec" is created for parameters of pointer types, making
6243 a distinction between T(*)[N] (or, equivalently, T[][N]) and
6244 the T[M][N] form, all of which have the same type and are represented
6245 the same, but only the last of which gets an "arg spec" describing
6246 the most significant bound M. */
6247
6248 static tree
6249 get_parm_array_spec (const struct c_parm *parm, tree attrs)
6250 {
6251 /* The attribute specification string, minor bound first. */
6252 std::string spec;
6253
6254 /* A list of VLA variable bounds, major first, or null if unspecified
6255 or not a VLA. */
6256 tree vbchain = NULL_TREE;
6257 /* True for a pointer parameter. */
6258 bool pointer = false;
6259 /* True for an ordinary array with an unpecified bound. */
6260 bool nobound = false;
6261
6262 /* Create a string representation for the bounds of the array/VLA. */
6263 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6264 {
6265 next = pd->declarator;
6266 while (next && next->kind == cdk_attrs)
6267 next = next->declarator;
6268
6269 /* Remember if a pointer has been seen to avoid storing the constant
6270 bound. */
6271 if (pd->kind == cdk_pointer)
6272 pointer = true;
6273
6274 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6275 && (!next || next->kind == cdk_id))
6276 {
6277 /* Do nothing for the common case of a pointer. The fact that
6278 the parameter is one can be deduced from the absence of
6279 an arg spec for it. */
6280 return attrs;
6281 }
6282
6283 if (pd->kind == cdk_id)
6284 {
6285 if (pointer
6286 || !parm->specs->type
6287 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6288 || !TYPE_DOMAIN (parm->specs->type)
6289 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6290 continue;
6291
6292 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6293 if (!vbchain
6294 && TREE_CODE (max) == INTEGER_CST)
6295 {
6296 /* Extract the upper bound from a parameter of an array type
6297 unless the parameter is an ordinary array of unspecified
6298 bound in which case a next iteration of the loop will
6299 exit. */
6300 if (spec.empty () || spec.end ()[-1] != ' ')
6301 {
6302 if (!tree_fits_shwi_p (max))
6303 continue;
6304
6305 /* The upper bound is the value of the largest valid
6306 index. */
6307 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6308 char buf[40];
6309 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6310 spec += buf;
6311 }
6312 continue;
6313 }
6314
6315 /* For a VLA typedef, create a list of its variable bounds and
6316 append it in the expected order to VBCHAIN. */
6317 tree tpbnds = NULL_TREE;
6318 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6319 type = TREE_TYPE (type))
6320 {
6321 tree nelts_minus_one = array_type_nelts_minus_one (type);
6322 if (error_operand_p (nelts_minus_one))
6323 return attrs;
6324 if (TREE_CODE (nelts_minus_one) != INTEGER_CST)
6325 {
6326 /* Each variable VLA bound is represented by the dollar
6327 sign. */
6328 spec += "$";
6329 tpbnds = tree_cons (NULL_TREE, nelts_minus_one, tpbnds);
6330 }
6331 }
6332 tpbnds = nreverse (tpbnds);
6333 vbchain = chainon (vbchain, tpbnds);
6334 continue;
6335 }
6336
6337 if (pd->kind != cdk_array)
6338 continue;
6339
6340 if (pd->u.array.vla_unspec_p)
6341 {
6342 /* Each unspecified bound is represented by a star. There
6343 can be any number of these in a declaration (but none in
6344 a definition). */
6345 spec += '*';
6346 continue;
6347 }
6348
6349 tree nelts = pd->u.array.dimen;
6350 if (!nelts)
6351 {
6352 /* Ordinary array of unspecified size. There can be at most
6353 one for the most significant bound. Exit on the next
6354 iteration which determines whether or not PARM is declared
6355 as a pointer or an array. */
6356 nobound = true;
6357 continue;
6358 }
6359
6360 if (pd->u.array.static_p)
6361 spec += 's';
6362
6363 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6364 /* Avoid invalid NELTS. */
6365 return attrs;
6366
6367 STRIP_NOPS (nelts);
6368 nelts = c_fully_fold (nelts, false, nullptr);
6369 if (TREE_CODE (nelts) == INTEGER_CST)
6370 {
6371 /* Skip all constant bounds except the most significant one.
6372 The interior ones are included in the array type. */
6373 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6374 continue;
6375
6376 if (!tree_fits_uhwi_p (nelts))
6377 /* Bail completely on invalid bounds. */
6378 return attrs;
6379
6380 char buf[40];
6381 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6382 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, n);
6383 spec += buf;
6384 break;
6385 }
6386
6387 /* Each variable VLA bound is represented by a dollar sign. */
6388 spec += "$";
6389 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6390 }
6391
6392 if (spec.empty () && !nobound)
6393 return attrs;
6394
6395 spec.insert (0, "[");
6396 if (nobound)
6397 /* Ordinary array of unspecified bound is represented by a space.
6398 It must be last in the spec. */
6399 spec += ' ';
6400 spec += ']';
6401
6402 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6403 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6404 tree name = get_identifier ("arg spec");
6405 return tree_cons (name, args, attrs);
6406 }
6407
6408 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6409 and push that on the current scope. EXPR is a pointer to an
6410 expression that needs to be evaluated for the side effects of array
6411 size expressions in the parameters. */
6412
6413 void
6414 push_parm_decl (const struct c_parm *parm, tree *expr)
6415 {
6416 tree attrs = parm->attrs;
6417 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6418 &attrs, expr, NULL, DEPRECATED_NORMAL);
6419 if (decl && DECL_P (decl))
6420 DECL_SOURCE_LOCATION (decl) = parm->loc;
6421
6422 attrs = get_parm_array_spec (parm, attrs);
6423 decl_attributes (&decl, attrs, 0);
6424
6425 decl = pushdecl (decl);
6426
6427 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6428 }
6429
6430 /* Mark all the parameter declarations to date as forward decls.
6431 Also diagnose use of this extension. */
6432
6433 void
6434 mark_forward_parm_decls (void)
6435 {
6436 struct c_binding *b;
6437
6438 if (pedantic && !current_scope->warned_forward_parm_decls)
6439 {
6440 pedwarn (input_location, OPT_Wpedantic,
6441 "ISO C forbids forward parameter declarations");
6442 current_scope->warned_forward_parm_decls = true;
6443 }
6444
6445 for (b = current_scope->bindings; b; b = b->prev)
6446 if (TREE_CODE (b->decl) == PARM_DECL)
6447 TREE_ASM_WRITTEN (b->decl) = 1;
6448 }
6449
6450 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6451 literal, which may be an incomplete array type completed by the
6452 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6453 literal. NON_CONST is true if the initializers contain something
6454 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6455 it is the (valid) alignment for this compound literal, as specified
6456 with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6457 compound literal. */
6458
6459 tree
6460 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6461 unsigned int alignas_align,
6462 struct c_declspecs *scspecs)
6463 {
6464 /* We do not use start_decl here because we have a type, not a declarator;
6465 and do not use finish_decl because the decl should be stored inside
6466 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6467 tree decl;
6468 tree complit;
6469 tree stmt;
6470 bool threadp = scspecs ? scspecs->thread_p : false;
6471 enum c_storage_class storage_class = (scspecs
6472 ? scspecs->storage_class
6473 : csc_none);
6474
6475 if (type == error_mark_node
6476 || init == error_mark_node)
6477 return error_mark_node;
6478
6479 if (current_scope == file_scope && storage_class == csc_register)
6480 {
6481 error_at (loc, "file-scope compound literal specifies %<register%>");
6482 storage_class = csc_none;
6483 }
6484
6485 if (current_scope != file_scope && threadp && storage_class == csc_none)
6486 {
6487 error_at (loc, "compound literal implicitly auto and declared %qs",
6488 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6489 threadp = false;
6490 }
6491
6492 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6493 DECL_EXTERNAL (decl) = 0;
6494 TREE_PUBLIC (decl) = 0;
6495 TREE_STATIC (decl) = (current_scope == file_scope
6496 || storage_class == csc_static);
6497 DECL_CONTEXT (decl) = current_function_decl;
6498 TREE_USED (decl) = 1;
6499 DECL_READ_P (decl) = 1;
6500 DECL_ARTIFICIAL (decl) = 1;
6501 DECL_IGNORED_P (decl) = 1;
6502 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6503 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6504 TREE_TYPE (decl) = type;
6505 if (threadp)
6506 set_decl_tls_model (decl, decl_default_tls_model (decl));
6507 if (storage_class == csc_register)
6508 {
6509 C_DECL_REGISTER (decl) = 1;
6510 DECL_REGISTER (decl) = 1;
6511 }
6512 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6513 if (alignas_align)
6514 {
6515 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6516 DECL_USER_ALIGN (decl) = 1;
6517 }
6518 store_init_value (loc, decl, init, NULL_TREE);
6519 if (current_scope != file_scope
6520 && TREE_STATIC (decl)
6521 && !TREE_READONLY (decl)
6522 && DECL_DECLARED_INLINE_P (current_function_decl)
6523 && DECL_EXTERNAL (current_function_decl))
6524 record_inline_static (input_location, current_function_decl,
6525 decl, csi_modifiable);
6526
6527 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6528 {
6529 int failure = complete_array_type (&TREE_TYPE (decl),
6530 DECL_INITIAL (decl), true);
6531 /* If complete_array_type returns 3, it means that the initial value of
6532 the compound literal is empty. Allow it with a pedwarn; in pre-C23
6533 modes, the empty initializer itself has been diagnosed if pedantic so
6534 does not need to be diagnosed again here. */
6535 gcc_assert (failure == 0 || failure == 3);
6536 if (failure == 3 && flag_isoc23)
6537 pedwarn (loc, OPT_Wpedantic,
6538 "array of unknown size with empty initializer");
6539
6540 type = TREE_TYPE (decl);
6541 TREE_TYPE (DECL_INITIAL (decl)) = type;
6542 relayout_decl (decl);
6543 }
6544
6545 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6546 {
6547 c_incomplete_type_error (loc, NULL_TREE, type);
6548 return error_mark_node;
6549 }
6550
6551 if (TREE_STATIC (decl)
6552 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6553 return error_mark_node;
6554
6555 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6556 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6557 TREE_SIDE_EFFECTS (complit) = 1;
6558
6559 layout_decl (decl, 0);
6560
6561 if (TREE_STATIC (decl))
6562 {
6563 /* This decl needs a name for the assembler output. */
6564 set_compound_literal_name (decl);
6565 DECL_DEFER_OUTPUT (decl) = 1;
6566 DECL_COMDAT (decl) = 1;
6567 pushdecl (decl);
6568 rest_of_decl_compilation (decl, 1, 0);
6569 }
6570 else if (current_function_decl && !current_scope->parm_flag)
6571 pushdecl (decl);
6572
6573 if (non_const)
6574 {
6575 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6576 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6577 }
6578
6579 return complit;
6580 }
6581
6582 /* Check the type of a compound literal. Here we just check that it
6583 is valid for C++. */
6584
6585 void
6586 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6587 {
6588 if (warn_cxx_compat
6589 && (type_name->specs->typespec_kind == ctsk_tagdef
6590 || type_name->specs->typespec_kind == ctsk_tagfirstref
6591 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6592 warning_at (loc, OPT_Wc___compat,
6593 "defining a type in a compound literal is invalid in C++");
6594 }
6595
6596 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6597 replacing with appropriate values if they are invalid. */
6598
6599 static void
6600 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6601 tree orig_name)
6602 {
6603 tree type_mv;
6604 unsigned int max_width;
6605 unsigned HOST_WIDE_INT w;
6606 const char *name = (orig_name
6607 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6608 : _("<anonymous>"));
6609
6610 /* Detect and ignore out of range field width and process valid
6611 field widths. */
6612 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6613 {
6614 error_at (loc, "bit-field %qs width not an integer constant", name);
6615 *width = integer_one_node;
6616 }
6617 else
6618 {
6619 if (TREE_CODE (*width) != INTEGER_CST)
6620 {
6621 *width = c_fully_fold (*width, false, NULL);
6622 if (TREE_CODE (*width) == INTEGER_CST)
6623 pedwarn (loc, OPT_Wpedantic,
6624 "bit-field %qs width not an integer constant expression",
6625 name);
6626 }
6627 if (TREE_CODE (*width) != INTEGER_CST)
6628 {
6629 error_at (loc, "bit-field %qs width not an integer constant", name);
6630 *width = integer_one_node;
6631 }
6632 constant_expression_warning (*width);
6633 if (tree_int_cst_sgn (*width) < 0)
6634 {
6635 error_at (loc, "negative width in bit-field %qs", name);
6636 *width = integer_one_node;
6637 }
6638 else if (integer_zerop (*width) && orig_name)
6639 {
6640 error_at (loc, "zero width for bit-field %qs", name);
6641 *width = integer_one_node;
6642 }
6643 }
6644
6645 /* Detect invalid bit-field type. */
6646 if (TREE_CODE (*type) != INTEGER_TYPE
6647 && TREE_CODE (*type) != BOOLEAN_TYPE
6648 && TREE_CODE (*type) != ENUMERAL_TYPE
6649 && TREE_CODE (*type) != BITINT_TYPE)
6650 {
6651 error_at (loc, "bit-field %qs has invalid type", name);
6652 *type = unsigned_type_node;
6653 }
6654
6655 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6656 {
6657 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6658 name);
6659 *type = unsigned_type_node;
6660 }
6661
6662 type_mv = TYPE_MAIN_VARIANT (*type);
6663 if (!in_system_header_at (input_location)
6664 && type_mv != integer_type_node
6665 && type_mv != unsigned_type_node
6666 && type_mv != boolean_type_node)
6667 pedwarn_c90 (loc, OPT_Wpedantic,
6668 "type of bit-field %qs is a GCC extension", name);
6669
6670 max_width = TYPE_PRECISION (*type);
6671
6672 if (compare_tree_int (*width, max_width) > 0)
6673 {
6674 error_at (loc, "width of %qs exceeds its type", name);
6675 w = max_width;
6676 *width = build_int_cst (integer_type_node, w);
6677 }
6678 else
6679 w = tree_to_uhwi (*width);
6680
6681 /* Truncation of hardbool false and true representation values is always safe:
6682 either the values remain different, or we'll report a problem when creating
6683 the narrower type. */
6684 if (c_hardbool_type_attr (*type))
6685 return;
6686
6687 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6688 {
6689 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6690 if (!lt
6691 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6692 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6693 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6694 }
6695 }
6696
6697
6698
6699 /* Print warning about variable length array if necessary. */
6700
6701 static void
6702 warn_variable_length_array (tree name, tree size)
6703 {
6704 if (TREE_CONSTANT (size))
6705 {
6706 if (name)
6707 pedwarn_c90 (input_location, OPT_Wvla,
6708 "ISO C90 forbids array %qE whose size "
6709 "cannot be evaluated", name);
6710 else
6711 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6712 "whose size cannot be evaluated");
6713 }
6714 else
6715 {
6716 if (name)
6717 pedwarn_c90 (input_location, OPT_Wvla,
6718 "ISO C90 forbids variable length array %qE", name);
6719 else
6720 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6721 "length array");
6722 }
6723 }
6724
6725 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6726 considering only those c_declspec_words found in LIST, which
6727 must be terminated by cdw_number_of_elements. */
6728
6729 static location_t
6730 smallest_type_quals_location (const location_t *locations,
6731 const c_declspec_word *list)
6732 {
6733 location_t loc = UNKNOWN_LOCATION;
6734 while (*list != cdw_number_of_elements)
6735 {
6736 location_t newloc = locations[*list];
6737 if (loc == UNKNOWN_LOCATION
6738 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6739 loc = newloc;
6740 list++;
6741 }
6742
6743 return loc;
6744 }
6745
6746
6747 /* We attach an artificial TYPE_DECL to pointed-to type
6748 and arrange for it to be included in a DECL_EXPR. This
6749 forces the sizes evaluation at a safe point and ensures it
6750 is not deferred until e.g. within a deeper conditional context.
6751
6752 PARM contexts have no enclosing statement list that
6753 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6754 instead, and add it to the list of expressions that
6755 need to be evaluated.
6756
6757 TYPENAME contexts do have an enclosing statement list,
6758 but it would be incorrect to use it, as the size should
6759 only be evaluated if the containing expression is
6760 evaluated. We might also be in the middle of an
6761 expression with side effects on the pointed-to type size
6762 "arguments" prior to the pointer declaration point and
6763 the fake TYPE_DECL in the enclosing context would force
6764 the size evaluation prior to the side effects. We therefore
6765 use BIND_EXPRs in TYPENAME contexts too. */
6766 static void
6767 add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6768 {
6769 tree bind = NULL_TREE;
6770 if (expr)
6771 {
6772 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6773 NULL_TREE);
6774 TREE_SIDE_EFFECTS (bind) = 1;
6775 BIND_EXPR_BODY (bind) = push_stmt_list ();
6776 push_scope ();
6777 }
6778
6779 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6780 pushdecl (decl);
6781 DECL_ARTIFICIAL (decl) = 1;
6782 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6783 if (set_name_p)
6784 TYPE_NAME (type) = decl;
6785
6786 if (bind)
6787 {
6788 pop_scope ();
6789 BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6790 if (*expr)
6791 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6792 else
6793 *expr = bind;
6794 }
6795 }
6796
6797 /* Given declspecs and a declarator,
6798 determine the name and type of the object declared
6799 and construct a ..._DECL node for it.
6800 (In one case we can return a ..._TYPE node instead.
6801 For invalid input we sometimes return NULL_TREE.)
6802
6803 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6804
6805 DECL_CONTEXT says which syntactic context this declaration is in:
6806 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6807 FUNCDEF for a function definition. Like NORMAL but a few different
6808 error messages in each case. Return value may be zero meaning
6809 this definition is too screwy to try to parse.
6810 PARM for a parameter declaration (either within a function prototype
6811 or before a function body). Make a PARM_DECL, or return void_type_node.
6812 TYPENAME if for a typename (in a cast or sizeof).
6813 Don't make a DECL node; just return the ..._TYPE node.
6814 FIELD for a struct or union field; make a FIELD_DECL.
6815 INITIALIZED is true if the decl has an initializer.
6816 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6817 representing the width of the bit-field.
6818 DECL_ATTRS points to the list of attributes that should be added to this
6819 decl. Any nested attributes that belong on the decl itself will be
6820 added to this list.
6821 If EXPR is not NULL, any expressions that need to be evaluated as
6822 part of evaluating variably modified types will be stored in *EXPR.
6823 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6824 set to indicate whether operands in *EXPR can be used in constant
6825 expressions.
6826 DEPRECATED_STATE is a deprecated_states value indicating whether
6827 deprecation/unavailability warnings should be suppressed.
6828
6829 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6830 It may also be so in the PARM case, for a prototype where the
6831 argument type is specified but not the name.
6832
6833 This function is where the complicated C meanings of `static'
6834 and `extern' are interpreted. */
6835
6836 static tree
6837 grokdeclarator (const struct c_declarator *declarator,
6838 struct c_declspecs *declspecs,
6839 enum decl_context decl_context, bool initialized, tree *width,
6840 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6841 enum deprecated_states deprecated_state)
6842 {
6843 tree type = declspecs->type;
6844 bool threadp = declspecs->thread_p;
6845 bool constexprp = declspecs->constexpr_p;
6846 enum c_storage_class storage_class = declspecs->storage_class;
6847 int constp;
6848 int restrictp;
6849 int volatilep;
6850 int atomicp;
6851 int type_quals = TYPE_UNQUALIFIED;
6852 tree name = NULL_TREE;
6853 bool funcdef_flag = false;
6854 bool funcdef_syntax = false;
6855 bool size_varies = false;
6856 tree decl_attr = declspecs->decl_attr;
6857 int array_ptr_quals = TYPE_UNQUALIFIED;
6858 tree array_ptr_attrs = NULL_TREE;
6859 bool array_parm_static = false;
6860 bool array_parm_vla_unspec_p = false;
6861 tree returned_attrs = NULL_TREE;
6862 tree decl_id_attrs = NULL_TREE;
6863 bool bitfield = width != NULL;
6864 tree element_type;
6865 tree orig_qual_type = NULL;
6866 size_t orig_qual_indirect = 0;
6867 struct c_arg_info *arg_info = 0;
6868 addr_space_t as1, as2, address_space;
6869 location_t loc = UNKNOWN_LOCATION;
6870 tree expr_dummy;
6871 bool expr_const_operands_dummy;
6872 enum c_declarator_kind first_non_attr_kind;
6873 unsigned int alignas_align = 0;
6874
6875 if (type == NULL_TREE)
6876 {
6877 /* This can occur for auto on a parameter in C23 mode. Set a
6878 dummy type here so subsequent code can give diagnostics for
6879 this case. */
6880 gcc_assert (declspecs->c23_auto_p);
6881 gcc_assert (decl_context == PARM);
6882 type = declspecs->type = integer_type_node;
6883 }
6884 if (TREE_CODE (type) == ERROR_MARK)
6885 return error_mark_node;
6886 if (expr == NULL)
6887 {
6888 expr = &expr_dummy;
6889 expr_dummy = NULL_TREE;
6890 }
6891 if (expr_const_operands == NULL)
6892 expr_const_operands = &expr_const_operands_dummy;
6893
6894 if (declspecs->expr)
6895 {
6896 if (*expr)
6897 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6898 declspecs->expr);
6899 else
6900 *expr = declspecs->expr;
6901 }
6902 *expr_const_operands = declspecs->expr_const_operands;
6903
6904 if (decl_context == FUNCDEF)
6905 funcdef_flag = true, decl_context = NORMAL;
6906
6907 /* Look inside a declarator for the name being declared
6908 and get it as an IDENTIFIER_NODE, for an error message. */
6909 {
6910 const struct c_declarator *decl = declarator;
6911
6912 first_non_attr_kind = cdk_attrs;
6913 while (decl)
6914 switch (decl->kind)
6915 {
6916 case cdk_array:
6917 loc = decl->id_loc;
6918 /* FALL THRU. */
6919
6920 case cdk_function:
6921 case cdk_pointer:
6922 funcdef_syntax = (decl->kind == cdk_function);
6923 if (first_non_attr_kind == cdk_attrs)
6924 first_non_attr_kind = decl->kind;
6925 decl = decl->declarator;
6926 break;
6927
6928 case cdk_attrs:
6929 decl = decl->declarator;
6930 break;
6931
6932 case cdk_id:
6933 loc = decl->id_loc;
6934 if (decl->u.id.id)
6935 name = decl->u.id.id;
6936 decl_id_attrs = decl->u.id.attrs;
6937 if (first_non_attr_kind == cdk_attrs)
6938 first_non_attr_kind = decl->kind;
6939 decl = 0;
6940 break;
6941
6942 default:
6943 gcc_unreachable ();
6944 }
6945 if (name == NULL_TREE)
6946 {
6947 gcc_assert (decl_context == PARM
6948 || decl_context == TYPENAME
6949 || (decl_context == FIELD
6950 && declarator->kind == cdk_id));
6951 gcc_assert (!initialized);
6952 }
6953 }
6954
6955 /* An enum type specifier (": specifier-qualifier-list") may only be
6956 specified when the enum is being defined or in an empty
6957 declaration of the form "enum identifier enum-type-specifier;".
6958 Except for the case of an empty declaration that has additional
6959 declaration specifiers, all invalid contexts (declarations that
6960 aren't empty, type names, parameter declarations, member
6961 declarations) pass through grokdeclarator. */
6962 if (declspecs->enum_type_specifier_ref_p)
6963 error_at (loc, "%<enum%> underlying type may not be specified here");
6964
6965 /* A function definition's declarator must have the form of
6966 a function declarator. */
6967
6968 if (funcdef_flag && !funcdef_syntax)
6969 return NULL_TREE;
6970
6971 /* If this looks like a function definition, make it one,
6972 even if it occurs where parms are expected.
6973 Then store_parm_decls will reject it and not use it as a parm. */
6974 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6975 decl_context = PARM;
6976
6977 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6978 {
6979 if (declspecs->unavailable_p)
6980 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6981 else if (declspecs->deprecated_p
6982 && deprecated_state != DEPRECATED_SUPPRESS)
6983 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6984 }
6985
6986 if ((decl_context == NORMAL || decl_context == FIELD)
6987 && current_scope == file_scope
6988 && c_type_variably_modified_p (type))
6989 {
6990 if (name)
6991 error_at (loc, "variably modified %qE at file scope", name);
6992 else
6993 error_at (loc, "variably modified field at file scope");
6994 type = integer_type_node;
6995 }
6996
6997 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6998
6999 /* Diagnose defaulting to "int". */
7000
7001 if (declspecs->default_int_p)
7002 {
7003 /* Issue a warning if this is an ISO C 99 program or if
7004 -Wreturn-type and this is a function, or if -Wimplicit;
7005 prefer the former warning since it is more explicit. */
7006 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
7007 && funcdef_flag)
7008 warn_about_return_type = 1;
7009 else
7010 {
7011 if (name)
7012 permerror_opt (loc, OPT_Wimplicit_int,
7013 "type defaults to %<int%> in declaration "
7014 "of %qE", name);
7015 else
7016 permerror_opt (loc, OPT_Wimplicit_int,
7017 "type defaults to %<int%> in type name");
7018 }
7019 }
7020
7021 /* Adjust the type if a bit-field is being declared,
7022 -funsigned-bitfields applied and the type is not explicitly
7023 "signed". */
7024 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
7025 && TREE_CODE (type) == INTEGER_TYPE)
7026 type = c_common_unsigned_type (type);
7027
7028 /* Figure out the type qualifiers for the declaration. There are
7029 two ways a declaration can become qualified. One is something
7030 like `const int i' where the `const' is explicit. Another is
7031 something like `typedef const int CI; CI i' where the type of the
7032 declaration contains the `const'. A third possibility is that
7033 there is a type qualifier on the element type of a typedefed
7034 array type, in which case we should extract that qualifier so
7035 that c_apply_type_quals_to_decl receives the full list of
7036 qualifiers to work with (C90 is not entirely clear about whether
7037 duplicate qualifiers should be diagnosed in this case, but it
7038 seems most appropriate to do so). */
7039 element_type = strip_array_types (type);
7040 constp = declspecs->const_p + TYPE_READONLY (element_type);
7041 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7042 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7043 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7044 as1 = declspecs->address_space;
7045 as2 = TYPE_ADDR_SPACE (element_type);
7046 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7047
7048 if (constp > 1)
7049 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7050 if (restrictp > 1)
7051 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7052 if (volatilep > 1)
7053 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7054 if (atomicp > 1)
7055 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7056
7057 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7058 error_at (loc, "conflicting named address spaces (%s vs %s)",
7059 c_addr_space_name (as1), c_addr_space_name (as2));
7060
7061 if ((TREE_CODE (type) == ARRAY_TYPE
7062 || first_non_attr_kind == cdk_array)
7063 && TYPE_QUALS (element_type))
7064 {
7065 orig_qual_type = type;
7066 type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7067 }
7068 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7069 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7070 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7071 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7072 | ENCODE_QUAL_ADDR_SPACE (address_space));
7073 if (type_quals != TYPE_QUALS (element_type))
7074 orig_qual_type = NULL_TREE;
7075
7076 /* Applying the _Atomic qualifier to an array type (through the use
7077 of typedefs or typeof) must be detected here. If the qualifier
7078 is introduced later, any appearance of applying it to an array is
7079 actually applying it to an element of that array. */
7080 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7081 error_at (loc, "%<_Atomic%>-qualified array type");
7082
7083 /* Warn about storage classes that are invalid for certain
7084 kinds of declarations (parameters, typenames, etc.). */
7085
7086 if (funcdef_flag
7087 && (threadp
7088 || constexprp
7089 || storage_class == csc_auto
7090 || storage_class == csc_register
7091 || storage_class == csc_typedef))
7092 {
7093 if (storage_class == csc_auto)
7094 pedwarn (loc,
7095 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7096 "function definition declared %<auto%>");
7097 if (storage_class == csc_register)
7098 error_at (loc, "function definition declared %<register%>");
7099 if (storage_class == csc_typedef)
7100 error_at (loc, "function definition declared %<typedef%>");
7101 if (threadp)
7102 error_at (loc, "function definition declared %qs",
7103 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7104 threadp = false;
7105 /* The parser ensures a constexpr function definition never
7106 reaches here. */
7107 gcc_assert (!constexprp);
7108 if (storage_class == csc_auto
7109 || storage_class == csc_register
7110 || storage_class == csc_typedef)
7111 storage_class = csc_none;
7112 }
7113 else if (decl_context != NORMAL && (storage_class != csc_none
7114 || threadp
7115 || constexprp
7116 || declspecs->c23_auto_p))
7117 {
7118 if (decl_context == PARM
7119 && storage_class == csc_register
7120 && !constexprp
7121 && !declspecs->c23_auto_p)
7122 ;
7123 else
7124 {
7125 switch (decl_context)
7126 {
7127 case FIELD:
7128 if (name)
7129 error_at (loc, "storage class specified for structure "
7130 "field %qE", name);
7131 else
7132 error_at (loc, "storage class specified for structure field");
7133 break;
7134 case PARM:
7135 if (name)
7136 error_at (loc, "storage class specified for parameter %qE",
7137 name);
7138 else
7139 error_at (loc, "storage class specified for unnamed parameter");
7140 break;
7141 default:
7142 error_at (loc, "storage class specified for typename");
7143 break;
7144 }
7145 storage_class = csc_none;
7146 threadp = false;
7147 constexprp = false;
7148 }
7149 }
7150 else if (storage_class == csc_extern
7151 && initialized
7152 && !funcdef_flag)
7153 {
7154 /* 'extern' with initialization is invalid if not at file scope. */
7155 if (current_scope == file_scope)
7156 {
7157 /* It is fine to have 'extern const' when compiling at C
7158 and C++ intersection. */
7159 if (!(warn_cxx_compat && constp))
7160 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7161 name);
7162 }
7163 else
7164 error_at (loc, "%qE has both %<extern%> and initializer", name);
7165 }
7166 else if (current_scope == file_scope)
7167 {
7168 if (storage_class == csc_auto)
7169 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7170 name);
7171 if (pedantic && storage_class == csc_register)
7172 pedwarn (input_location, OPT_Wpedantic,
7173 "file-scope declaration of %qE specifies %<register%>", name);
7174 }
7175 else
7176 {
7177 if (storage_class == csc_extern && funcdef_flag)
7178 error_at (loc, "nested function %qE declared %<extern%>", name);
7179 else if (threadp && storage_class == csc_none)
7180 {
7181 error_at (loc, "function-scope %qE implicitly auto and declared "
7182 "%qs", name,
7183 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7184 threadp = false;
7185 }
7186 }
7187
7188 /* Now figure out the structure of the declarator proper.
7189 Descend through it, creating more complex types, until we reach
7190 the declared identifier (or NULL_TREE, in an absolute declarator).
7191 At each stage we maintain an unqualified version of the type
7192 together with any qualifiers that should be applied to it with
7193 c_build_qualified_type; this way, array types including
7194 multidimensional array types are first built up in unqualified
7195 form and then the qualified form is created with
7196 TYPE_MAIN_VARIANT pointing to the unqualified form. */
7197
7198 while (declarator && declarator->kind != cdk_id)
7199 {
7200 if (type == error_mark_node)
7201 {
7202 declarator = declarator->declarator;
7203 continue;
7204 }
7205
7206 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7207 a cdk_pointer (for *...),
7208 a cdk_function (for ...(...)),
7209 a cdk_attrs (for nested attributes),
7210 or a cdk_id (for the name being declared
7211 or the place in an absolute declarator
7212 where the name was omitted).
7213 For the last case, we have just exited the loop.
7214
7215 At this point, TYPE is the type of elements of an array,
7216 or for a function to return, or for a pointer to point to.
7217 After this sequence of ifs, TYPE is the type of the
7218 array or function or pointer, and DECLARATOR has had its
7219 outermost layer removed. */
7220
7221 if (array_ptr_quals != TYPE_UNQUALIFIED
7222 || array_ptr_attrs != NULL_TREE
7223 || array_parm_static)
7224 {
7225 /* Only the innermost declarator (making a parameter be of
7226 array type which is converted to pointer type)
7227 may have static or type qualifiers. */
7228 error_at (loc, "static or type qualifiers in non-parameter array declarator");
7229 array_ptr_quals = TYPE_UNQUALIFIED;
7230 array_ptr_attrs = NULL_TREE;
7231 array_parm_static = false;
7232 }
7233
7234 switch (declarator->kind)
7235 {
7236 case cdk_attrs:
7237 {
7238 /* A declarator with embedded attributes. */
7239 tree attrs = declarator->u.attrs;
7240 const struct c_declarator *inner_decl;
7241 int attr_flags = 0;
7242 declarator = declarator->declarator;
7243 /* Standard attribute syntax precisely defines what entity
7244 an attribute in each position appertains to, so only
7245 apply laxity about positioning to GNU attribute syntax.
7246 Standard attributes applied to a function or array
7247 declarator apply exactly to that type; standard
7248 attributes applied to the identifier apply to the
7249 declaration rather than to the type, and are specified
7250 using a cdk_id declarator rather than using
7251 cdk_attrs. */
7252 inner_decl = declarator;
7253 while (inner_decl->kind == cdk_attrs)
7254 inner_decl = inner_decl->declarator;
7255 if (!cxx11_attribute_p (attrs))
7256 {
7257 if (inner_decl->kind == cdk_id)
7258 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7259 else if (inner_decl->kind == cdk_function)
7260 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7261 else if (inner_decl->kind == cdk_array)
7262 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7263 }
7264 attrs = c_warn_type_attributes (type, attrs);
7265 returned_attrs = decl_attributes (&type,
7266 chainon (returned_attrs, attrs),
7267 attr_flags);
7268 break;
7269 }
7270 case cdk_array:
7271 {
7272 tree itype = NULL_TREE;
7273 tree size = declarator->u.array.dimen;
7274 /* The index is a signed object `sizetype' bits wide. */
7275 tree index_type = c_common_signed_type (sizetype);
7276
7277 array_ptr_quals = declarator->u.array.quals;
7278 array_ptr_attrs = declarator->u.array.attrs;
7279 array_parm_static = declarator->u.array.static_p;
7280 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7281
7282 declarator = declarator->declarator;
7283
7284 /* Check for some types that there cannot be arrays of. */
7285
7286 if (VOID_TYPE_P (type))
7287 {
7288 if (name)
7289 error_at (loc, "declaration of %qE as array of voids", name);
7290 else
7291 error_at (loc, "declaration of type name as array of voids");
7292 type = error_mark_node;
7293 }
7294
7295 if (TREE_CODE (type) == FUNCTION_TYPE)
7296 {
7297 if (name)
7298 error_at (loc, "declaration of %qE as array of functions",
7299 name);
7300 else
7301 error_at (loc, "declaration of type name as array of "
7302 "functions");
7303 type = error_mark_node;
7304 }
7305
7306 if (pedantic && !in_system_header_at (input_location)
7307 && flexible_array_type_p (type))
7308 pedwarn (loc, OPT_Wpedantic,
7309 "invalid use of structure with flexible array member");
7310
7311 if (size == error_mark_node)
7312 type = error_mark_node;
7313
7314 if (type == error_mark_node)
7315 continue;
7316
7317 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7318 {
7319 type = error_mark_node;
7320 continue;
7321 }
7322
7323 /* If size was specified, set ITYPE to a range-type for
7324 that size. Otherwise, ITYPE remains null. finish_decl
7325 may figure it out from an initial value. */
7326
7327 if (size)
7328 {
7329 bool size_maybe_const = true;
7330 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7331 && !TREE_OVERFLOW (size));
7332 bool this_size_varies = false;
7333
7334 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7335 lvalue. */
7336 STRIP_TYPE_NOPS (size);
7337
7338 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7339 {
7340 if (name)
7341 error_at (loc, "size of array %qE has non-integer type",
7342 name);
7343 else
7344 error_at (loc,
7345 "size of unnamed array has non-integer type");
7346 size = integer_one_node;
7347 size_int_const = true;
7348 }
7349 /* This can happen with enum forward declaration. */
7350 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7351 {
7352 if (name)
7353 error_at (loc, "size of array %qE has incomplete type",
7354 name);
7355 else
7356 error_at (loc, "size of unnamed array has incomplete "
7357 "type");
7358 size = integer_one_node;
7359 size_int_const = true;
7360 }
7361
7362 size = c_fully_fold (size, false, &size_maybe_const);
7363
7364 if (pedantic && size_maybe_const && integer_zerop (size))
7365 {
7366 if (name)
7367 pedwarn (loc, OPT_Wpedantic,
7368 "ISO C forbids zero-size array %qE", name);
7369 else
7370 pedwarn (loc, OPT_Wpedantic,
7371 "ISO C forbids zero-size array");
7372 }
7373
7374 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7375 {
7376 constant_expression_warning (size);
7377 if (tree_int_cst_sgn (size) < 0)
7378 {
7379 if (name)
7380 error_at (loc, "size of array %qE is negative", name);
7381 else
7382 error_at (loc, "size of unnamed array is negative");
7383 size = integer_one_node;
7384 size_int_const = true;
7385 }
7386 /* Handle a size folded to an integer constant but
7387 not an integer constant expression. */
7388 if (!size_int_const)
7389 {
7390 /* If this is a file scope declaration of an
7391 ordinary identifier, this is invalid code;
7392 diagnosing it here and not subsequently
7393 treating the type as variable-length avoids
7394 more confusing diagnostics later. */
7395 if ((decl_context == NORMAL || decl_context == FIELD)
7396 && current_scope == file_scope)
7397 pedwarn (input_location, 0,
7398 "variably modified %qE at file scope",
7399 name);
7400 else
7401 this_size_varies = size_varies = true;
7402 warn_variable_length_array (name, size);
7403 }
7404 }
7405 else if ((decl_context == NORMAL || decl_context == FIELD)
7406 && current_scope == file_scope)
7407 {
7408 error_at (loc, "variably modified %qE at file scope", name);
7409 size = integer_one_node;
7410 }
7411 else
7412 {
7413 /* Make sure the array size remains visibly
7414 nonconstant even if it is (eg) a const variable
7415 with known value. */
7416 this_size_varies = size_varies = true;
7417 warn_variable_length_array (name, size);
7418 if (sanitize_flags_p (SANITIZE_VLA)
7419 && current_function_decl != NULL_TREE
7420 && decl_context == NORMAL)
7421 {
7422 /* Evaluate the array size only once. */
7423 size = save_expr (size);
7424 size = c_fully_fold (size, false, NULL);
7425 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7426 ubsan_instrument_vla (loc, size),
7427 size);
7428 }
7429 }
7430
7431 if (integer_zerop (size) && !this_size_varies)
7432 {
7433 /* A zero-length array cannot be represented with
7434 an unsigned index type, which is what we'll
7435 get with build_index_type. Create an
7436 open-ended range instead. */
7437 itype = build_range_type (sizetype, size, NULL_TREE);
7438 }
7439 else
7440 {
7441 /* Arrange for the SAVE_EXPR on the inside of the
7442 MINUS_EXPR, which allows the -1 to get folded
7443 with the +1 that happens when building TYPE_SIZE. */
7444 if (size_varies)
7445 size = save_expr (size);
7446 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7447 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7448 integer_zero_node, size);
7449
7450 /* Compute the maximum valid index, that is, size
7451 - 1. Do the calculation in index_type, so that
7452 if it is a variable the computations will be
7453 done in the proper mode. */
7454 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7455 convert (index_type, size),
7456 convert (index_type,
7457 size_one_node));
7458
7459 /* The above overflows when size does not fit
7460 in index_type.
7461 ??? While a size of INT_MAX+1 technically shouldn't
7462 cause an overflow (because we subtract 1), handling
7463 this case seems like an unnecessary complication. */
7464 if (TREE_CODE (size) == INTEGER_CST
7465 && !int_fits_type_p (size, index_type))
7466 {
7467 if (name)
7468 error_at (loc, "size of array %qE is too large",
7469 name);
7470 else
7471 error_at (loc, "size of unnamed array is too large");
7472 type = error_mark_node;
7473 continue;
7474 }
7475
7476 itype = build_index_type (itype);
7477 }
7478 if (this_size_varies)
7479 {
7480 if (TREE_SIDE_EFFECTS (size))
7481 {
7482 if (*expr)
7483 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7484 *expr, size);
7485 else
7486 *expr = size;
7487 }
7488 *expr_const_operands &= size_maybe_const;
7489 }
7490 }
7491 else if (decl_context == FIELD)
7492 {
7493 bool flexible_array_member = false;
7494 if (array_parm_vla_unspec_p)
7495 /* Field names can in fact have function prototype
7496 scope so [*] is disallowed here through making
7497 the field variably modified, not through being
7498 something other than a declaration with function
7499 prototype scope. */
7500 size_varies = true;
7501 else
7502 {
7503 const struct c_declarator *t = declarator;
7504 while (t->kind == cdk_attrs)
7505 t = t->declarator;
7506 flexible_array_member = (t->kind == cdk_id);
7507 }
7508 if (flexible_array_member
7509 && !in_system_header_at (input_location))
7510 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7511 "support flexible array members");
7512
7513 /* ISO C99 Flexible array members are effectively
7514 identical to GCC's zero-length array extension. */
7515 if (flexible_array_member)
7516 itype = build_index_type (NULL_TREE);
7517 }
7518
7519 if (array_parm_vla_unspec_p)
7520 {
7521 /* C99 6.7.5.2p4 */
7522 if (decl_context == TYPENAME)
7523 warning (0, "%<[*]%> not in a declaration");
7524 size_varies = true;
7525 }
7526
7527 /* Complain about arrays of incomplete types. */
7528 if (!COMPLETE_TYPE_P (type))
7529 {
7530 auto_diagnostic_group d;
7531 error_at (loc, "array type has incomplete element type %qT",
7532 type);
7533 /* See if we can be more helpful. */
7534 if (TREE_CODE (type) == ARRAY_TYPE)
7535 {
7536 if (name)
7537 inform (loc, "declaration of %qE as multidimensional "
7538 "array must have bounds for all dimensions "
7539 "except the first", name);
7540 else
7541 inform (loc, "declaration of multidimensional array "
7542 "must have bounds for all dimensions except "
7543 "the first");
7544 }
7545 type = error_mark_node;
7546 }
7547 else
7548 /* When itype is NULL, a shared incomplete array type is
7549 returned for all array of a given type. Elsewhere we
7550 make sure we don't complete that type before copying
7551 it, but here we want to make sure we don't ever
7552 modify the shared type, so we gcc_assert (itype)
7553 below. */
7554 {
7555 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7556 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7557 type = c_build_qualified_type (type,
7558 ENCODE_QUAL_ADDR_SPACE (as));
7559 if (array_parm_vla_unspec_p)
7560 type = c_build_array_type_unspecified (type);
7561 else
7562 type = c_build_array_type (type, itype);
7563 }
7564
7565 if (type != error_mark_node)
7566 {
7567 /* The GCC extension for zero-length arrays differs from
7568 ISO flexible array members in that sizeof yields
7569 zero. */
7570 if (size && integer_zerop (size))
7571 {
7572 gcc_assert (itype);
7573 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7574 TYPE_SIZE (type) = bitsize_zero_node;
7575 TYPE_SIZE_UNIT (type) = size_zero_node;
7576 SET_TYPE_STRUCTURAL_EQUALITY (type);
7577 }
7578
7579 if (!valid_array_size_p (loc, type, name))
7580 type = error_mark_node;
7581 }
7582
7583 if (decl_context != PARM
7584 && (array_ptr_quals != TYPE_UNQUALIFIED
7585 || array_ptr_attrs != NULL_TREE
7586 || array_parm_static))
7587 {
7588 error_at (loc, "static or type qualifiers in non-parameter "
7589 "array declarator");
7590 array_ptr_quals = TYPE_UNQUALIFIED;
7591 array_ptr_attrs = NULL_TREE;
7592 array_parm_static = false;
7593 }
7594 orig_qual_indirect++;
7595 break;
7596 }
7597 case cdk_function:
7598 {
7599 /* Say it's a definition only for the declarator closest
7600 to the identifier, apart possibly from some
7601 attributes. */
7602 bool really_funcdef = false;
7603 tree arg_types;
7604 orig_qual_type = NULL_TREE;
7605 if (funcdef_flag)
7606 {
7607 const struct c_declarator *t = declarator->declarator;
7608 while (t->kind == cdk_attrs)
7609 t = t->declarator;
7610 really_funcdef = (t->kind == cdk_id);
7611 }
7612
7613 /* Declaring a function type. Make sure we have a valid
7614 type for the function to return. */
7615 if (type == error_mark_node)
7616 continue;
7617
7618 size_varies = false;
7619
7620 /* Warn about some types functions can't return. */
7621 if (TREE_CODE (type) == FUNCTION_TYPE)
7622 {
7623 if (name)
7624 error_at (loc, "%qE declared as function returning a "
7625 "function", name);
7626 else
7627 error_at (loc, "type name declared as function "
7628 "returning a function");
7629 type = integer_type_node;
7630 }
7631 if (TREE_CODE (type) == ARRAY_TYPE)
7632 {
7633 if (name)
7634 error_at (loc, "%qE declared as function returning an array",
7635 name);
7636 else
7637 error_at (loc, "type name declared as function returning "
7638 "an array");
7639 type = integer_type_node;
7640 }
7641
7642 /* Construct the function type and go to the next
7643 inner layer of declarator. */
7644 arg_info = declarator->u.arg_info;
7645 arg_types = grokparms (arg_info, really_funcdef);
7646
7647 /* Type qualifiers before the return type of the function
7648 qualify the return type, not the function type. */
7649 if (type_quals)
7650 {
7651 const enum c_declspec_word ignored_quals_list[] =
7652 {
7653 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7654 cdw_atomic, cdw_number_of_elements
7655 };
7656 location_t specs_loc
7657 = smallest_type_quals_location (declspecs->locations,
7658 ignored_quals_list);
7659 if (specs_loc == UNKNOWN_LOCATION)
7660 specs_loc = declspecs->locations[cdw_typedef];
7661 if (specs_loc == UNKNOWN_LOCATION)
7662 specs_loc = loc;
7663
7664 /* Type qualifiers on a function return type are
7665 normally permitted by the standard but have no
7666 effect, so give a warning at -Wreturn-type.
7667 Qualifiers on a void return type are banned on
7668 function definitions in ISO C; GCC used to used
7669 them for noreturn functions. The resolution of C11
7670 DR#423 means qualifiers (other than _Atomic) are
7671 actually removed from the return type when
7672 determining the function type. For C23, _Atomic is
7673 removed as well. */
7674 int quals_used = type_quals;
7675 if (flag_isoc23)
7676 quals_used = 0;
7677 else if (flag_isoc11)
7678 quals_used &= TYPE_QUAL_ATOMIC;
7679 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7680 pedwarn (specs_loc, 0,
7681 "function definition has qualified void "
7682 "return type");
7683 else
7684 warning_at (specs_loc, OPT_Wignored_qualifiers,
7685 "type qualifiers ignored on function "
7686 "return type");
7687
7688 /* Ensure an error for restrict on invalid types; the
7689 DR#423 resolution is not entirely clear about
7690 this. */
7691 if (flag_isoc11
7692 && (type_quals & TYPE_QUAL_RESTRICT)
7693 && (!POINTER_TYPE_P (type)
7694 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7695 error_at (loc, "invalid use of %<restrict%>");
7696 type = c_build_qualified_type (type, quals_used);
7697 }
7698 type_quals = TYPE_UNQUALIFIED;
7699
7700 type = c_build_function_type (type, arg_types,
7701 arg_info->no_named_args_stdarg_p);
7702 declarator = declarator->declarator;
7703
7704 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7705 the formal parameter list of this FUNCTION_TYPE to point to
7706 the FUNCTION_TYPE node itself. */
7707 {
7708 c_arg_tag *tag;
7709 unsigned ix;
7710
7711 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7712 TYPE_CONTEXT (tag->type) = type;
7713 }
7714 break;
7715 }
7716 case cdk_pointer:
7717 {
7718 /* Merge any constancy or volatility into the target type
7719 for the pointer. */
7720 if ((type_quals & TYPE_QUAL_ATOMIC)
7721 && TREE_CODE (type) == FUNCTION_TYPE)
7722 {
7723 error_at (loc,
7724 "%<_Atomic%>-qualified function type");
7725 type_quals &= ~TYPE_QUAL_ATOMIC;
7726 }
7727 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7728 && type_quals)
7729 pedwarn (loc, OPT_Wpedantic,
7730 "ISO C forbids qualified function types");
7731 if (type_quals)
7732 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7733 orig_qual_indirect);
7734 orig_qual_type = NULL_TREE;
7735 size_varies = false;
7736
7737 /* When the pointed-to type involves components of variable size,
7738 care must be taken to ensure that the size evaluation code is
7739 emitted early enough to dominate all the possible later uses
7740 and late enough for the variables on which it depends to have
7741 been assigned.
7742
7743 This is expected to happen automatically when the pointed-to
7744 type has a name/declaration of it's own, but special attention
7745 is required if the type is anonymous. */
7746 if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7747 {
7748 bool bind_p = decl_context == TYPENAME
7749 || decl_context == FIELD
7750 || decl_context == PARM;
7751 add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7752 }
7753
7754 type = c_build_pointer_type (type);
7755
7756 /* Process type qualifiers (such as const or volatile)
7757 that were given inside the `*'. */
7758 type_quals = declarator->u.pointer_quals;
7759
7760 declarator = declarator->declarator;
7761 break;
7762 }
7763 default:
7764 gcc_unreachable ();
7765 }
7766 }
7767 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7768 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7769
7770 /* Now TYPE has the actual type, apart from any qualifiers in
7771 TYPE_QUALS. */
7772
7773 /* Warn about address space used for things other than static memory or
7774 pointers. */
7775 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7776 if (!ADDR_SPACE_GENERIC_P (address_space))
7777 {
7778 if (decl_context == NORMAL)
7779 {
7780 switch (storage_class)
7781 {
7782 case csc_auto:
7783 error ("%qs combined with %<auto%> qualifier for %qE",
7784 c_addr_space_name (address_space), name);
7785 break;
7786 case csc_register:
7787 error ("%qs combined with %<register%> qualifier for %qE",
7788 c_addr_space_name (address_space), name);
7789 break;
7790 case csc_none:
7791 if (current_function_scope)
7792 {
7793 error ("%qs specified for auto variable %qE",
7794 c_addr_space_name (address_space), name);
7795 break;
7796 }
7797 break;
7798 case csc_static:
7799 case csc_extern:
7800 case csc_typedef:
7801 break;
7802 default:
7803 gcc_unreachable ();
7804 }
7805 }
7806 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7807 {
7808 if (name)
7809 error ("%qs specified for parameter %qE",
7810 c_addr_space_name (address_space), name);
7811 else
7812 error ("%qs specified for unnamed parameter",
7813 c_addr_space_name (address_space));
7814 }
7815 else if (decl_context == FIELD)
7816 {
7817 if (name)
7818 error ("%qs specified for structure field %qE",
7819 c_addr_space_name (address_space), name);
7820 else
7821 error ("%qs specified for structure field",
7822 c_addr_space_name (address_space));
7823 }
7824 }
7825
7826 /* Check the type and width of a bit-field. */
7827 if (bitfield)
7828 {
7829 check_bitfield_type_and_width (loc, &type, width, name);
7830 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7831 atomic types are permitted for bit-fields; we have no code to
7832 make bit-field accesses atomic, so disallow them. */
7833 if (type_quals & TYPE_QUAL_ATOMIC)
7834 {
7835 if (name)
7836 error_at (loc, "bit-field %qE has atomic type", name);
7837 else
7838 error_at (loc, "bit-field has atomic type");
7839 type_quals &= ~TYPE_QUAL_ATOMIC;
7840 }
7841 }
7842
7843 /* Reject invalid uses of _Alignas. */
7844 if (declspecs->alignas_p)
7845 {
7846 if (storage_class == csc_typedef)
7847 error_at (loc, "alignment specified for typedef %qE", name);
7848 else if (storage_class == csc_register)
7849 error_at (loc, "alignment specified for %<register%> object %qE",
7850 name);
7851 else if (decl_context == PARM)
7852 {
7853 if (name)
7854 error_at (loc, "alignment specified for parameter %qE", name);
7855 else
7856 error_at (loc, "alignment specified for unnamed parameter");
7857 }
7858 else if (bitfield)
7859 {
7860 if (name)
7861 error_at (loc, "alignment specified for bit-field %qE", name);
7862 else
7863 error_at (loc, "alignment specified for unnamed bit-field");
7864 }
7865 else if (TREE_CODE (type) == FUNCTION_TYPE)
7866 error_at (loc, "alignment specified for function %qE", name);
7867 else if (declspecs->align_log != -1 && TYPE_P (type))
7868 {
7869 alignas_align = 1U << declspecs->align_log;
7870 if (alignas_align < min_align_of_type (type))
7871 {
7872 if (name)
7873 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7874 "alignment of %qE", name);
7875 else
7876 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7877 "alignment of unnamed field");
7878 alignas_align = 0;
7879 }
7880 }
7881 }
7882
7883 /* If this is declaring a typedef name, return a TYPE_DECL. */
7884
7885 if (storage_class == csc_typedef)
7886 {
7887 tree decl;
7888 if ((type_quals & TYPE_QUAL_ATOMIC)
7889 && TREE_CODE (type) == FUNCTION_TYPE)
7890 {
7891 error_at (loc,
7892 "%<_Atomic%>-qualified function type");
7893 type_quals &= ~TYPE_QUAL_ATOMIC;
7894 }
7895 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7896 && type_quals)
7897 pedwarn (loc, OPT_Wpedantic,
7898 "ISO C forbids qualified function types");
7899 if (type_quals)
7900 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7901 orig_qual_indirect);
7902 decl = build_decl (declarator->id_loc,
7903 TYPE_DECL, declarator->u.id.id, type);
7904 if (declspecs->explicit_signed_p)
7905 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7906 if (declspecs->inline_p)
7907 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7908 if (declspecs->noreturn_p)
7909 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7910
7911 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7912 {
7913 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7914
7915 if (b != NULL
7916 && b->decl != NULL_TREE
7917 && (B_IN_CURRENT_SCOPE (b)
7918 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7919 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7920 {
7921 auto_diagnostic_group d;
7922 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7923 ("using %qD as both a typedef and a tag is "
7924 "invalid in C++"), decl)
7925 && b->locus != UNKNOWN_LOCATION)
7926 inform (b->locus, "originally defined here");
7927 }
7928 }
7929
7930 return decl;
7931 }
7932
7933 /* If this is a type name (such as, in a cast or sizeof),
7934 compute the type and return it now. */
7935
7936 if (decl_context == TYPENAME)
7937 {
7938 /* Note that the grammar rejects storage classes in typenames
7939 and fields. */
7940 gcc_assert (storage_class == csc_none && !threadp
7941 && !declspecs->inline_p && !declspecs->noreturn_p);
7942 if ((type_quals & TYPE_QUAL_ATOMIC)
7943 && TREE_CODE (type) == FUNCTION_TYPE)
7944 {
7945 error_at (loc,
7946 "%<_Atomic%>-qualified function type");
7947 type_quals &= ~TYPE_QUAL_ATOMIC;
7948 }
7949 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7950 && type_quals)
7951 pedwarn (loc, OPT_Wpedantic,
7952 "ISO C forbids const or volatile function types");
7953 if (type_quals)
7954 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7955 orig_qual_indirect);
7956 return type;
7957 }
7958
7959 if (pedantic && decl_context == FIELD
7960 && c_type_variably_modified_p (type))
7961 {
7962 /* C99 6.7.2.1p8 */
7963 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7964 "have a variably modified type");
7965 }
7966
7967 /* Aside from typedefs and type names (handle above),
7968 `void' at top level (not within pointer)
7969 is allowed only in public variables.
7970 We don't complain about parms either, but that is because
7971 a better error message can be made later. */
7972
7973 if (VOID_TYPE_P (type) && decl_context != PARM
7974 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7975 && (storage_class == csc_extern
7976 || (current_scope == file_scope
7977 && !(storage_class == csc_static
7978 || storage_class == csc_register)))))
7979 {
7980 error_at (loc, "variable or field %qE declared void", name);
7981 type = integer_type_node;
7982 }
7983
7984 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7985 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7986
7987 {
7988 tree decl;
7989
7990 if (decl_context == PARM)
7991 {
7992 tree promoted_type;
7993 bool array_parameter_p = false;
7994
7995 /* A parameter declared as an array of T is really a pointer to T.
7996 One declared as a function is really a pointer to a function. */
7997
7998 if (TREE_CODE (type) == ARRAY_TYPE)
7999 {
8000 /* Transfer const-ness of array into that of type pointed to. */
8001 type = TREE_TYPE (type);
8002 if (orig_qual_type != NULL_TREE)
8003 {
8004 if (orig_qual_indirect == 0)
8005 orig_qual_type = TREE_TYPE (orig_qual_type);
8006 else
8007 orig_qual_indirect--;
8008 }
8009 if (type_quals)
8010 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8011 orig_qual_indirect);
8012
8013 /* The pointed-to type may need a decl expr (see above). */
8014 if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
8015 {
8016 bool bind_p = decl_context == TYPENAME
8017 || decl_context == FIELD
8018 || decl_context == PARM;
8019 add_decl_expr (loc, type, bind_p ? expr : NULL, true);
8020 }
8021
8022 type = c_build_pointer_type (type);
8023 type_quals = array_ptr_quals;
8024 if (type_quals)
8025 type = c_build_qualified_type (type, type_quals);
8026
8027 /* We don't yet implement attributes in this context. */
8028 if (array_ptr_attrs != NULL_TREE)
8029 warning_at (loc, OPT_Wattributes,
8030 "attributes in parameter array declarator ignored");
8031
8032 size_varies = false;
8033 array_parameter_p = true;
8034 }
8035 else if (TREE_CODE (type) == FUNCTION_TYPE)
8036 {
8037 if (type_quals & TYPE_QUAL_ATOMIC)
8038 {
8039 error_at (loc,
8040 "%<_Atomic%>-qualified function type");
8041 type_quals &= ~TYPE_QUAL_ATOMIC;
8042 }
8043 else if (type_quals)
8044 pedwarn (loc, OPT_Wpedantic,
8045 "ISO C forbids qualified function types");
8046 if (type_quals)
8047 type = c_build_qualified_type (type, type_quals);
8048 type = c_build_pointer_type (type);
8049 type_quals = TYPE_UNQUALIFIED;
8050 }
8051 else if (type_quals)
8052 type = c_build_qualified_type (type, type_quals);
8053
8054 decl = build_decl (declarator->id_loc,
8055 PARM_DECL, declarator->u.id.id, type);
8056 if (size_varies)
8057 C_DECL_VARIABLE_SIZE (decl) = 1;
8058 C_ARRAY_PARAMETER (decl) = array_parameter_p;
8059
8060 /* Compute the type actually passed in the parmlist,
8061 for the case where there is no prototype.
8062 (For example, shorts and chars are passed as ints.)
8063 When there is a prototype, this is overridden later. */
8064
8065 if (type == error_mark_node)
8066 promoted_type = type;
8067 else
8068 promoted_type = c_type_promotes_to (type);
8069
8070 DECL_ARG_TYPE (decl) = promoted_type;
8071 if (declspecs->inline_p)
8072 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8073 if (declspecs->noreturn_p)
8074 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8075 }
8076 else if (decl_context == FIELD)
8077 {
8078 /* Note that the grammar rejects storage classes in typenames
8079 and fields. */
8080 gcc_assert (storage_class == csc_none && !threadp
8081 && !declspecs->inline_p && !declspecs->noreturn_p);
8082
8083 /* Structure field. It may not be a function. */
8084
8085 if (TREE_CODE (type) == FUNCTION_TYPE)
8086 {
8087 error_at (loc, "field %qE declared as a function", name);
8088 type = c_build_pointer_type (type);
8089 }
8090 else if (TREE_CODE (type) != ERROR_MARK
8091 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8092 {
8093 if (name)
8094 error_at (loc, "field %qE has incomplete type", name);
8095 else
8096 error_at (loc, "unnamed field has incomplete type");
8097 type = error_mark_node;
8098 }
8099 else if (TREE_CODE (type) == ARRAY_TYPE
8100 && TYPE_DOMAIN (type) == NULL_TREE)
8101 {
8102 /* We have a flexible array member through a typedef.
8103 Set suitable range. Whether this is a correct position
8104 for a flexible array member will be determined elsewhere. */
8105 if (!in_system_header_at (input_location))
8106 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8107 "support flexible array members");
8108 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8109 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8110 NULL_TREE);
8111 if (orig_qual_indirect == 0)
8112 orig_qual_type = NULL_TREE;
8113 }
8114 if (type != error_mark_node
8115 && !verify_type_context (loc, TCTX_FIELD, type))
8116 type = error_mark_node;
8117
8118 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8119 orig_qual_indirect);
8120 decl = build_decl (declarator->id_loc,
8121 FIELD_DECL, declarator->u.id.id, type);
8122 DECL_NONADDRESSABLE_P (decl) = bitfield;
8123 if (bitfield && !declarator->u.id.id)
8124 DECL_PADDING_P (decl) = 1;
8125
8126 if (size_varies)
8127 C_DECL_VARIABLE_SIZE (decl) = 1;
8128 }
8129 else if (TREE_CODE (type) == FUNCTION_TYPE)
8130 {
8131 if (storage_class == csc_register || threadp || constexprp)
8132 {
8133 error_at (loc, "invalid storage class for function %qE", name);
8134 }
8135 else if (current_scope != file_scope)
8136 {
8137 /* Function declaration not at file scope. Storage
8138 classes other than `extern' are not allowed, C99
8139 6.7.1p5, and `extern' makes no difference. However,
8140 GCC allows 'auto', perhaps with 'inline', to support
8141 nested functions. */
8142 if (storage_class == csc_auto)
8143 pedwarn (loc, OPT_Wpedantic,
8144 "invalid storage class for function %qE", name);
8145 else if (storage_class == csc_static)
8146 {
8147 error_at (loc, "invalid storage class for function %qE", name);
8148 if (funcdef_flag)
8149 storage_class = declspecs->storage_class = csc_none;
8150 else
8151 return NULL_TREE;
8152 }
8153 }
8154
8155 decl = build_decl (declarator->id_loc,
8156 FUNCTION_DECL, declarator->u.id.id, type);
8157 decl = build_decl_attribute_variant (decl, decl_attr);
8158
8159 if (type_quals & TYPE_QUAL_ATOMIC)
8160 {
8161 error_at (loc,
8162 "%<_Atomic%>-qualified function type");
8163 type_quals &= ~TYPE_QUAL_ATOMIC;
8164 }
8165 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8166 pedwarn (loc, OPT_Wpedantic,
8167 "ISO C forbids qualified function types");
8168
8169 /* Every function declaration is an external reference
8170 (DECL_EXTERNAL) except for those which are not at file
8171 scope and are explicitly declared "auto". This is
8172 forbidden by standard C (C99 6.7.1p5) and is interpreted by
8173 GCC to signify a forward declaration of a nested function. */
8174 if (storage_class == csc_auto && current_scope != file_scope)
8175 DECL_EXTERNAL (decl) = 0;
8176 /* In C99, a function which is declared 'inline' with 'extern'
8177 is not an external reference (which is confusing). It
8178 means that the later definition of the function must be output
8179 in this file, C99 6.7.4p6. In GNU C89, a function declared
8180 'extern inline' is an external reference. */
8181 else if (declspecs->inline_p && storage_class != csc_static)
8182 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8183 == flag_gnu89_inline);
8184 else
8185 DECL_EXTERNAL (decl) = !initialized;
8186
8187 /* Record absence of global scope for `static' or `auto'. */
8188 TREE_PUBLIC (decl)
8189 = !(storage_class == csc_static || storage_class == csc_auto);
8190
8191 /* For a function definition, record the argument information
8192 block where store_parm_decls will look for it. */
8193 if (funcdef_flag)
8194 current_function_arg_info = arg_info;
8195
8196 if (declspecs->default_int_p)
8197 C_FUNCTION_IMPLICIT_INT (decl) = 1;
8198
8199 /* Record presence of `inline' and `_Noreturn', if it is
8200 reasonable. */
8201 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8202 {
8203 if (declspecs->inline_p)
8204 pedwarn (loc, 0, "cannot inline function %<main%>");
8205 if (declspecs->noreturn_p)
8206 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8207 }
8208 else
8209 {
8210 if (declspecs->inline_p)
8211 /* Record that the function is declared `inline'. */
8212 DECL_DECLARED_INLINE_P (decl) = 1;
8213 if (declspecs->noreturn_p)
8214 {
8215 if (flag_isoc99)
8216 pedwarn_c99 (loc, OPT_Wpedantic,
8217 "ISO C99 does not support %<_Noreturn%>");
8218 else
8219 pedwarn_c99 (loc, OPT_Wpedantic,
8220 "ISO C90 does not support %<_Noreturn%>");
8221 TREE_THIS_VOLATILE (decl) = 1;
8222 }
8223 }
8224
8225 /* C99 6.2.2p7: It is invalid (compile-time undefined
8226 behavior) to create an 'extern' declaration for a
8227 function if there is a global declaration that is
8228 'static' and the global declaration is not visible.
8229 (If the static declaration _is_ currently visible,
8230 the 'extern' declaration is taken to refer to that decl.) */
8231 if (!initialized
8232 && TREE_PUBLIC (decl)
8233 && current_scope != file_scope)
8234 {
8235 tree global_decl = identifier_global_value (declarator->u.id.id);
8236 tree visible_decl = lookup_name (declarator->u.id.id);
8237
8238 if (global_decl
8239 && global_decl != visible_decl
8240 && VAR_OR_FUNCTION_DECL_P (global_decl)
8241 && !TREE_PUBLIC (global_decl))
8242 error_at (loc, "function previously declared %<static%> "
8243 "redeclared %<extern%>");
8244 }
8245 }
8246 else
8247 {
8248 /* It's a variable. */
8249 /* An uninitialized decl with `extern' is a reference. */
8250 int extern_ref = !initialized && storage_class == csc_extern;
8251
8252 if (constexprp)
8253 {
8254 /* The type of a constexpr variable must not be variably
8255 modified, volatile, atomic or restrict qualified or
8256 have a member with such a qualifier. const
8257 qualification is implicitly added, and, at file scope,
8258 has internal linkage. */
8259 if (c_type_variably_modified_p (type))
8260 error_at (loc, "%<constexpr%> object has variably modified "
8261 "type");
8262 if (type_quals
8263 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8264 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8265 else
8266 {
8267 tree type_no_array = strip_array_types (type);
8268 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8269 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8270 error_at (loc, "invalid qualifiers for field of "
8271 "%<constexpr%> object");
8272 }
8273 type_quals |= TYPE_QUAL_CONST;
8274 if (current_scope == file_scope)
8275 storage_class = csc_static;
8276 }
8277
8278 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8279 orig_qual_indirect);
8280
8281 /* C99 6.2.2p7: It is invalid (compile-time undefined
8282 behavior) to create an 'extern' declaration for a
8283 variable if there is a global declaration that is
8284 'static' and the global declaration is not visible.
8285 (If the static declaration _is_ currently visible,
8286 the 'extern' declaration is taken to refer to that decl.) */
8287 if (extern_ref && current_scope != file_scope)
8288 {
8289 tree global_decl = identifier_global_value (declarator->u.id.id);
8290 tree visible_decl = lookup_name (declarator->u.id.id);
8291
8292 if (global_decl
8293 && global_decl != visible_decl
8294 && VAR_P (global_decl)
8295 && !TREE_PUBLIC (global_decl))
8296 error_at (loc, "variable previously declared %<static%> "
8297 "redeclared %<extern%>");
8298 }
8299
8300 decl = build_decl (declarator->id_loc,
8301 VAR_DECL, declarator->u.id.id, type);
8302 if (size_varies)
8303 C_DECL_VARIABLE_SIZE (decl) = 1;
8304 if (constexprp)
8305 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8306
8307 if (declspecs->inline_p)
8308 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8309 if (declspecs->noreturn_p)
8310 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8311
8312 /* At file scope, an initialized extern declaration may follow
8313 a static declaration. In that case, DECL_EXTERNAL will be
8314 reset later in start_decl. */
8315 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8316
8317 /* At file scope, the presence of a `static' or `register' storage
8318 class specifier, or the absence of all storage class specifiers
8319 makes this declaration a definition (perhaps tentative). Also,
8320 the absence of `static' makes it public. */
8321 if (current_scope == file_scope)
8322 {
8323 TREE_PUBLIC (decl) = storage_class != csc_static;
8324 TREE_STATIC (decl) = !extern_ref;
8325 }
8326 /* Not at file scope, only `static' makes a static definition. */
8327 else
8328 {
8329 TREE_STATIC (decl) = (storage_class == csc_static);
8330 TREE_PUBLIC (decl) = extern_ref;
8331 }
8332
8333 if (threadp)
8334 set_decl_tls_model (decl, decl_default_tls_model (decl));
8335 }
8336
8337 if ((storage_class == csc_extern
8338 || (storage_class == csc_none
8339 && TREE_CODE (type) == FUNCTION_TYPE
8340 && !funcdef_flag))
8341 && c_type_variably_modified_p (type))
8342 {
8343 /* C99 6.7.5.2p2 */
8344 if (TREE_CODE (type) == FUNCTION_TYPE)
8345 error_at (loc, "non-nested function with variably modified type");
8346 else
8347 error_at (loc, "object with variably modified type must have "
8348 "no linkage");
8349 }
8350
8351 /* For nested functions disqualify ones taking VLAs by value
8352 from inlining since the middle-end cannot deal with this.
8353 ??? We should arrange for those to be passed by reference
8354 with emitting the copy on the caller side in the frontend. */
8355 if (storage_class == csc_none
8356 && TREE_CODE (type) == FUNCTION_TYPE)
8357 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8358 {
8359 tree arg = TREE_VALUE (al);
8360 if (arg != error_mark_node
8361 && C_TYPE_VARIABLE_SIZE (arg))
8362 {
8363 DECL_UNINLINABLE (decl) = 1;
8364 break;
8365 }
8366 }
8367
8368 /* Record `register' declaration for warnings on &
8369 and in case doing stupid register allocation. */
8370
8371 if (storage_class == csc_register)
8372 {
8373 C_DECL_REGISTER (decl) = 1;
8374 DECL_REGISTER (decl) = 1;
8375 }
8376
8377 /* Record constancy and volatility. */
8378 c_apply_type_quals_to_decl (type_quals, decl);
8379
8380 /* Apply _Alignas specifiers. */
8381 if (alignas_align)
8382 {
8383 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8384 DECL_USER_ALIGN (decl) = 1;
8385 }
8386
8387 /* If a type has volatile components, it should be stored in memory.
8388 Otherwise, the fact that those components are volatile
8389 will be ignored, and would even crash the compiler.
8390 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8391 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8392 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8393 || TREE_CODE (decl) == RESULT_DECL))
8394 {
8395 /* It is not an error for a structure with volatile fields to
8396 be declared register, but reset DECL_REGISTER since it
8397 cannot actually go in a register. */
8398 int was_reg = C_DECL_REGISTER (decl);
8399 C_DECL_REGISTER (decl) = 0;
8400 DECL_REGISTER (decl) = 0;
8401 c_mark_addressable (decl);
8402 C_DECL_REGISTER (decl) = was_reg;
8403 }
8404
8405 /* This is the earliest point at which we might know the assembler
8406 name of a variable. Thus, if it's known before this, die horribly. */
8407 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8408 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8409
8410 if (warn_cxx_compat
8411 && VAR_P (decl)
8412 && TREE_PUBLIC (decl)
8413 && TREE_STATIC (decl)
8414 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8415 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8416 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8417 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8418 ("non-local variable %qD with anonymous type is "
8419 "questionable in C++"),
8420 decl);
8421
8422 return decl;
8423 }
8424 }
8425
8426 /* Decode the parameter-list info for a function type or function definition.
8427 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8428 if there is an identifier list instead of a parameter decl list).
8429 These two functions are separate because when a function returns
8430 or receives functions then each is called multiple times but the order
8431 of calls is different. The last call to `grokparms' is always the one
8432 that contains the formal parameter names of a function definition.
8433
8434 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8435
8436 FUNCDEF_FLAG is true for a function definition, false for
8437 a mere declaration. A nonempty identifier-list gets an error message
8438 when FUNCDEF_FLAG is false. */
8439
8440 static tree
8441 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8442 {
8443 tree arg_types = arg_info->types;
8444
8445 if (funcdef_flag && arg_info->had_vla_unspec)
8446 {
8447 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8448 /* C99 6.7.5.2p4 */
8449 error ("%<[*]%> not allowed in other than function prototype scope");
8450 }
8451
8452 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8453 && !in_system_header_at (input_location))
8454 warning (OPT_Wstrict_prototypes,
8455 "function declaration isn%'t a prototype");
8456
8457 if (arg_types == error_mark_node)
8458 /* Don't set TYPE_ARG_TYPES in this case. */
8459 return NULL_TREE;
8460
8461 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8462 {
8463 if (!funcdef_flag)
8464 {
8465 permerror_opt (input_location,
8466 OPT_Wdeclaration_missing_parameter_type,
8467 "parameter names (without types) in "
8468 "function declaration");
8469 arg_info->parms = NULL_TREE;
8470 }
8471 else
8472 arg_info->parms = arg_info->types;
8473
8474 arg_info->types = NULL_TREE;
8475 return NULL_TREE;
8476 }
8477 else
8478 {
8479 tree parm, type, typelt;
8480 unsigned int parmno;
8481
8482 /* In C23, convert () to (void). */
8483 if (flag_isoc23
8484 && !arg_types
8485 && !arg_info->parms
8486 && !arg_info->no_named_args_stdarg_p)
8487 {
8488 arg_types = arg_info->types = void_list_node;
8489 arg_info->c23_empty_parens = 1;
8490 }
8491
8492 /* If there is a parameter of incomplete type in a definition,
8493 this is an error. In a declaration this is valid, and a
8494 struct or union type may be completed later, before any calls
8495 or definition of the function. In the case where the tag was
8496 first declared within the parameter list, a warning has
8497 already been given. If a parameter has void type, then
8498 this has already received an error (constraint violation in C2Y,
8499 previously implicitly undefined behavior). */
8500
8501 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8502 parm;
8503 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8504 {
8505 type = TREE_VALUE (typelt);
8506 if (type == error_mark_node)
8507 continue;
8508
8509 if (!COMPLETE_TYPE_P (type))
8510 {
8511 if (funcdef_flag)
8512 {
8513 if (DECL_NAME (parm))
8514 error_at (input_location,
8515 "parameter %u (%q+D) has incomplete type",
8516 parmno, parm);
8517 else
8518 error_at (DECL_SOURCE_LOCATION (parm),
8519 "parameter %u has incomplete type",
8520 parmno);
8521
8522 TREE_VALUE (typelt) = error_mark_node;
8523 TREE_TYPE (parm) = error_mark_node;
8524 arg_types = NULL_TREE;
8525 }
8526 }
8527
8528 if (DECL_NAME (parm) && TREE_USED (parm))
8529 warn_if_shadowing (parm);
8530 }
8531 return arg_types;
8532 }
8533 }
8534
8535 /* Allocate and initialize a c_arg_info structure from the parser's
8536 obstack. */
8537
8538 struct c_arg_info *
8539 build_arg_info (void)
8540 {
8541 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8542 ret->parms = NULL_TREE;
8543 ret->tags = NULL;
8544 ret->types = NULL_TREE;
8545 ret->others = NULL_TREE;
8546 ret->pending_sizes = NULL;
8547 ret->had_vla_unspec = 0;
8548 ret->no_named_args_stdarg_p = 0;
8549 ret->c23_empty_parens = 0;
8550 return ret;
8551 }
8552
8553 /* Take apart the current scope and return a c_arg_info structure with
8554 info on a parameter list just parsed.
8555
8556 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8557
8558 ELLIPSIS being true means the argument list ended in '...' so don't
8559 append a sentinel (void_list_node) to the end of the type-list.
8560
8561 EXPR is NULL or an expression that needs to be evaluated for the
8562 side effects of array size expressions in the parameters. */
8563
8564 struct c_arg_info *
8565 get_parm_info (bool ellipsis, tree expr)
8566 {
8567 struct c_binding *b = current_scope->bindings;
8568 struct c_arg_info *arg_info = build_arg_info ();
8569
8570 tree parms = NULL_TREE;
8571 vec<c_arg_tag, va_gc> *tags = NULL;
8572 tree types = NULL_TREE;
8573 tree others = NULL_TREE;
8574
8575 bool gave_void_only_once_err = false;
8576
8577 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8578
8579 /* The bindings in this scope must not get put into a block.
8580 We will take care of deleting the binding nodes. */
8581 current_scope->bindings = 0;
8582
8583 /* This function is only called if there was *something* on the
8584 parameter list. */
8585 gcc_assert (b);
8586
8587 /* A parameter list consisting solely of 'void' indicates that the
8588 function takes no arguments. But if the 'void' is qualified
8589 (by 'const' or 'volatile'), or has a storage class specifier
8590 ('register'), then the behavior is undefined; issue an error.
8591 Typedefs for 'void' are OK (see DR#157). */
8592 if (b->prev == 0 /* one binding */
8593 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8594 && !DECL_NAME (b->decl) /* anonymous */
8595 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8596 {
8597 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8598 || C_DECL_REGISTER (b->decl))
8599 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8600
8601 /* There cannot be an ellipsis. */
8602 if (ellipsis)
8603 error_at (b->locus, "%<void%> must be the only parameter");
8604
8605 arg_info->types = void_list_node;
8606 return arg_info;
8607 }
8608
8609 if (!ellipsis)
8610 types = void_list_node;
8611
8612 /* Break up the bindings list into parms, tags, types, and others;
8613 apply sanity checks; purge the name-to-decl bindings. */
8614 while (b)
8615 {
8616 tree decl = b->decl;
8617 tree type = TREE_TYPE (decl);
8618 c_arg_tag tag;
8619 const char *keyword;
8620
8621 switch (TREE_CODE (decl))
8622 {
8623 case PARM_DECL:
8624 if (b->id)
8625 {
8626 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8627 I_SYMBOL_BINDING (b->id) = b->shadowed;
8628 }
8629
8630 /* Check for forward decls that never got their actual decl. */
8631 if (TREE_ASM_WRITTEN (decl))
8632 error_at (b->locus,
8633 "parameter %q+D has just a forward declaration", decl);
8634 /* Check for (..., void, ...) and named void parameters and issue an
8635 error. */
8636 else if (VOID_TYPE_P (type))
8637 {
8638 if (!gave_void_only_once_err)
8639 {
8640 error_at (b->locus,
8641 "%<void%> must be the only parameter and unnamed");
8642 gave_void_only_once_err = true;
8643 }
8644 }
8645 else
8646 {
8647 /* Valid parameter, add it to the list. */
8648 DECL_CHAIN (decl) = parms;
8649 parms = decl;
8650
8651 /* Since there is a prototype, args are passed in their
8652 declared types. The back end may override this later. */
8653 DECL_ARG_TYPE (decl) = type;
8654 types = tree_cons (0, type, types);
8655 }
8656 break;
8657
8658 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8659 case UNION_TYPE: keyword = "union"; goto tag;
8660 case RECORD_TYPE: keyword = "struct"; goto tag;
8661 tag:
8662 /* Types may not have tag-names, in which case the type
8663 appears in the bindings list with b->id NULL. */
8664 if (b->id)
8665 {
8666 gcc_assert (I_TAG_BINDING (b->id) == b);
8667 I_TAG_BINDING (b->id) = b->shadowed;
8668 }
8669
8670 /* Warn about any struct, union or enum tags defined in a
8671 parameter list. The scope of such types is limited to
8672 the parameter list, which is rarely if ever desirable
8673 (it's impossible to call such a function with type-
8674 correct arguments). An anonymous union parm type is
8675 meaningful as a GNU extension, so don't warn for that. */
8676 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8677 {
8678 if (b->id)
8679 {
8680 /* The %s will be one of 'struct', 'union', or 'enum'. */
8681 if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8682 warning_at (b->locus, 0,
8683 "%<%s %E%> declared inside parameter list"
8684 " will not be visible outside of this definition or"
8685 " declaration", keyword, b->id);
8686 }
8687 else
8688 /* The %s will be one of 'struct', 'union', or 'enum'. */
8689 warning_at (b->locus, 0,
8690 "anonymous %s declared inside parameter list"
8691 " will not be visible outside of this definition or"
8692 " declaration", keyword);
8693 }
8694
8695 tag.id = b->id;
8696 tag.type = decl;
8697 vec_safe_push (tags, tag);
8698 break;
8699
8700 case FUNCTION_DECL:
8701 /* FUNCTION_DECLs appear when there is an implicit function
8702 declaration in the parameter list. */
8703 gcc_assert (b->nested || seen_error ());
8704 goto set_shadowed;
8705
8706 case CONST_DECL:
8707 case TYPE_DECL:
8708 /* CONST_DECLs appear here when we have an embedded enum,
8709 and TYPE_DECLs appear here when we have an embedded struct
8710 or union. No warnings for this - we already warned about the
8711 type itself. */
8712
8713 /* When we reinsert this decl in the function body, we need
8714 to reconstruct whether it was marked as nested. */
8715 gcc_assert (!b->nested);
8716 DECL_CHAIN (decl) = others;
8717 others = decl;
8718 /* fall through */
8719
8720 case ERROR_MARK:
8721 set_shadowed:
8722 /* error_mark_node appears here when we have an undeclared
8723 variable. Just throw it away. */
8724 if (b->id)
8725 {
8726 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8727 I_SYMBOL_BINDING (b->id) = b->shadowed;
8728 }
8729 break;
8730
8731 /* Other things that might be encountered. */
8732 case LABEL_DECL:
8733 case VAR_DECL:
8734 default:
8735 gcc_unreachable ();
8736 }
8737
8738 b = free_binding_and_advance (b);
8739 }
8740
8741 arg_info->parms = parms;
8742 arg_info->tags = tags;
8743 arg_info->types = types;
8744 arg_info->others = others;
8745 arg_info->pending_sizes = expr;
8746 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8747 return arg_info;
8748 }
8749
8750 /* Get the struct, enum or union (CODE says which) with tag NAME.
8751 Define the tag as a forward-reference with location LOC if it is
8752 not defined. HAVE_STD_ATTRS says whether any standard attributes
8753 were present after the struct, union or enum keyword; ATTRS are the
8754 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8755 whether an enum type specifier (": specifier-qualifier-list") is
8756 present; if so, this is called before that specifier is parsed, so
8757 that the tag is in scope for that specifier. Return a c_typespec
8758 structure for the type specifier. */
8759
8760 struct c_typespec
8761 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8762 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8763 {
8764 struct c_typespec ret;
8765 tree ref;
8766 location_t refloc;
8767
8768 ret.expr = NULL_TREE;
8769 ret.expr_const_operands = true;
8770 ret.has_enum_type_specifier = has_enum_type_specifier;
8771
8772 /* If a cross reference is requested, look up the type already
8773 defined for this tag and return it. If an enum type specifier is
8774 present, only a definition in the current scope is relevant. */
8775
8776 ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8777
8778 /* If the visble type is still being defined, see if there is
8779 an earlier definition (which may be complete). We do not
8780 have to loop because nested redefinitions are not allowed. */
8781 if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8782 {
8783 tree vis = previous_tag (ref);
8784 if (vis)
8785 ref = vis;
8786 }
8787
8788 /* If this is the right type of tag, return what we found.
8789 (This reference will be shadowed by shadow_tag later if appropriate.)
8790 If this is the wrong type of tag, do not return it. If it was the
8791 wrong type in the same scope, we will have had an error
8792 message already; if in a different scope and declaring
8793 a name, pending_xref_error will give an error message; but if in a
8794 different scope and not declaring a name, this tag should
8795 shadow the previous declaration of a different type of tag, and
8796 this would not work properly if we return the reference found.
8797 (For example, with "struct foo" in an outer scope, "union foo;"
8798 must shadow that tag with a new one of union type.) */
8799 ret.kind = (ref
8800 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8801 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8802 if (ref && TREE_CODE (ref) == code)
8803 {
8804 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8805 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8806 && loc != UNKNOWN_LOCATION
8807 && warn_cxx_compat)
8808 {
8809 auto_diagnostic_group d;
8810 switch (code)
8811 {
8812 case ENUMERAL_TYPE:
8813 if (warning_at (loc, OPT_Wc___compat,
8814 ("enum type defined in struct or union "
8815 "is not visible in C++")))
8816 inform (refloc, "enum type defined here");
8817 break;
8818 case RECORD_TYPE:
8819 if (warning_at (loc, OPT_Wc___compat,
8820 ("struct defined in struct or union "
8821 "is not visible in C++")))
8822 inform (refloc, "struct defined here");
8823 break;
8824 case UNION_TYPE:
8825 if (warning_at (loc, OPT_Wc___compat,
8826 ("union defined in struct or union "
8827 "is not visible in C++")))
8828 inform (refloc, "union defined here");
8829 break;
8830 default:
8831 gcc_unreachable();
8832 }
8833 }
8834
8835 ret.spec = ref;
8836 return ret;
8837 }
8838
8839 /* If no such tag is yet defined, create a forward-reference node
8840 and record it as the "definition".
8841 When a real declaration of this type is found,
8842 the forward-reference will be altered into a real type. */
8843
8844 ref = make_node (code);
8845 if (flag_isoc23 || code == ENUMERAL_TYPE)
8846 SET_TYPE_STRUCTURAL_EQUALITY (ref);
8847 if (code == ENUMERAL_TYPE)
8848 {
8849 /* Give the type a default layout like unsigned int
8850 to avoid crashing if it does not get defined. */
8851 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8852 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8853 TYPE_USER_ALIGN (ref) = 0;
8854 TYPE_UNSIGNED (ref) = 1;
8855 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8856 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8857 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8858 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8859 }
8860
8861 pushtag (loc, name, ref);
8862 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8863 if (in_underspecified_init)
8864 error_at (loc, "%qT declared in underspecified object initializer",
8865 ref);
8866
8867 ret.spec = ref;
8868 return ret;
8869 }
8870
8871 /* Get the struct, enum or union (CODE says which) with tag NAME.
8872 Define the tag as a forward-reference if it is not defined.
8873 Return a tree for the type. */
8874
8875 tree
8876 xref_tag (enum tree_code code, tree name)
8877 {
8878 return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8879 false).spec;
8880 }
8881
8882 /* Make sure that the tag NAME is defined *in the current scope*
8883 at least as a forward reference.
8884 LOC is the location of the struct's definition.
8885 CODE says which kind of tag NAME ought to be.
8886
8887 This stores the current value of the file static STRUCT_PARSE_INFO
8888 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8889 new c_struct_parse_info structure. The old value of
8890 STRUCT_PARSE_INFO is restored in finish_struct. */
8891
8892 tree
8893 start_struct (location_t loc, enum tree_code code, tree name,
8894 class c_struct_parse_info **enclosing_struct_parse_info)
8895 {
8896 /* If there is already a tag defined at this scope
8897 (as a forward reference), just return it. */
8898
8899 tree ref = NULL_TREE;
8900 location_t refloc = UNKNOWN_LOCATION;
8901
8902 if (name != NULL_TREE)
8903 ref = lookup_tag (code, name, true, &refloc);
8904
8905 /* For C23, even if we already have a completed definition,
8906 we do not use it. We will check for consistency later.
8907 If we are in a nested redefinition the type is not
8908 complete. We will then detect this below. */
8909 if (flag_isoc23 && ref && TYPE_SIZE (ref))
8910 ref = NULL_TREE;
8911
8912 if (ref && TREE_CODE (ref) == code)
8913 {
8914 if (TYPE_STUB_DECL (ref))
8915 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8916
8917 if (TYPE_SIZE (ref))
8918 {
8919 auto_diagnostic_group d;
8920 if (code == UNION_TYPE)
8921 error_at (loc, "redefinition of %<union %E%>", name);
8922 else
8923 error_at (loc, "redefinition of %<struct %E%>", name);
8924 if (refloc != UNKNOWN_LOCATION)
8925 inform (refloc, "originally defined here");
8926 /* Don't create structures using a name already in use. */
8927 ref = NULL_TREE;
8928 }
8929 else if (C_TYPE_BEING_DEFINED (ref))
8930 {
8931 if (code == UNION_TYPE)
8932 error_at (loc, "nested redefinition of %<union %E%>", name);
8933 else
8934 error_at (loc, "nested redefinition of %<struct %E%>", name);
8935 /* Don't bother to report "originally defined here" for a
8936 nested redefinition; the original definition should be
8937 obvious. */
8938 /* Don't create structures that contain themselves. */
8939 ref = NULL_TREE;
8940 }
8941 }
8942
8943 /* Otherwise create a forward-reference just so the tag is in scope. */
8944
8945 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8946 {
8947 ref = make_node (code);
8948 if (flag_isoc23)
8949 SET_TYPE_STRUCTURAL_EQUALITY (ref);
8950 pushtag (loc, name, ref);
8951 }
8952
8953 C_TYPE_BEING_DEFINED (ref) = 1;
8954 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8955 TYPE_PACKED (v) = flag_pack_struct;
8956
8957 *enclosing_struct_parse_info = struct_parse_info;
8958 struct_parse_info = new c_struct_parse_info ();
8959 struct_parse_info->refloc = refloc;
8960
8961 /* FIXME: This will issue a warning for a use of a type defined
8962 within a statement expr used within sizeof, et. al. This is not
8963 terribly serious as C++ doesn't permit statement exprs within
8964 sizeof anyhow. */
8965 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8966 warning_at (loc, OPT_Wc___compat,
8967 "defining type in %qs expression is invalid in C++",
8968 (in_sizeof
8969 ? "sizeof"
8970 : (in_typeof ? "typeof" : "alignof")));
8971
8972 if (in_underspecified_init)
8973 error_at (loc, "%qT defined in underspecified object initializer", ref);
8974
8975 return ref;
8976 }
8977
8978 /* Process the specs, declarator and width (NULL if omitted)
8979 of a structure component, returning a FIELD_DECL node.
8980 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8981 DECL_ATTRS is as for grokdeclarator.
8982
8983 LOC is the location of the structure component.
8984
8985 This is done during the parsing of the struct declaration.
8986 The FIELD_DECL nodes are chained together and the lot of them
8987 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8988
8989 tree
8990 grokfield (location_t loc,
8991 struct c_declarator *declarator, struct c_declspecs *declspecs,
8992 tree width, tree *decl_attrs, tree *expr)
8993 {
8994 tree value;
8995
8996 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8997 && width == NULL_TREE)
8998 {
8999 /* This is an unnamed decl.
9000
9001 If we have something of the form "union { list } ;" then this
9002 is the anonymous union extension. Similarly for struct.
9003
9004 If this is something of the form "struct foo;", then
9005 If MS or Plan 9 extensions are enabled, this is handled as
9006 an anonymous struct.
9007 Otherwise this is a forward declaration of a structure tag.
9008
9009 If this is something of the form "foo;" and foo is a TYPE_DECL, then
9010 If foo names a structure or union without a tag, then this
9011 is an anonymous struct (this is permitted by C11).
9012 If MS or Plan 9 extensions are enabled and foo names a
9013 structure, then again this is an anonymous struct.
9014 Otherwise this is an error.
9015
9016 Oh what a horrid tangled web we weave. I wonder if MS consciously
9017 took this from Plan 9 or if it was an accident of implementation
9018 that took root before someone noticed the bug... */
9019
9020 tree type = declspecs->type;
9021 bool ok = false;
9022
9023 if (RECORD_OR_UNION_TYPE_P (type)
9024 && (flag_ms_extensions
9025 || flag_plan9_extensions
9026 || !declspecs->typedef_p))
9027 {
9028 if (flag_ms_extensions || flag_plan9_extensions)
9029 ok = true;
9030 else if (TYPE_NAME (type) == NULL)
9031 ok = true;
9032 else
9033 ok = false;
9034 }
9035 if (!ok)
9036 {
9037 pedwarn (loc, 0, "declaration does not declare anything");
9038 return NULL_TREE;
9039 }
9040 if (flag_isoc99)
9041 pedwarn_c99 (loc, OPT_Wpedantic,
9042 "ISO C99 doesn%'t support unnamed structs/unions");
9043 else
9044 pedwarn_c99 (loc, OPT_Wpedantic,
9045 "ISO C90 doesn%'t support unnamed structs/unions");
9046 }
9047
9048 value = grokdeclarator (declarator, declspecs, FIELD, false,
9049 width ? &width : NULL, decl_attrs, expr, NULL,
9050 DEPRECATED_NORMAL);
9051
9052 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9053 DECL_INITIAL (value) = width;
9054 if (width)
9055 SET_DECL_C_BIT_FIELD (value);
9056
9057 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9058 {
9059 /* If we currently have a binding for this field, set the
9060 in_struct field in the binding, so that we warn about lookups
9061 which find it. */
9062 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9063 if (b != NULL)
9064 {
9065 /* If the in_struct field is not yet set, push it on a list
9066 to be cleared when this struct is finished. */
9067 if (!b->in_struct)
9068 {
9069 struct_parse_info->fields.safe_push (b);
9070 b->in_struct = 1;
9071 }
9072 }
9073 }
9074
9075 return value;
9076 }
9077
9078 /* Subroutine of detect_field_duplicates: return whether X and Y,
9079 which are both fields in the same struct, have duplicate field
9080 names. */
9081
9082 static bool
9083 is_duplicate_field (tree x, tree y)
9084 {
9085 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9086 return true;
9087
9088 /* When using -fplan9-extensions, an anonymous field whose name is a
9089 typedef can duplicate a field name. */
9090 if (flag_plan9_extensions
9091 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9092 {
9093 tree xt, xn, yt, yn;
9094
9095 xt = TREE_TYPE (x);
9096 if (DECL_NAME (x) != NULL_TREE)
9097 xn = DECL_NAME (x);
9098 else if (RECORD_OR_UNION_TYPE_P (xt)
9099 && TYPE_NAME (xt) != NULL_TREE
9100 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9101 xn = DECL_NAME (TYPE_NAME (xt));
9102 else
9103 xn = NULL_TREE;
9104
9105 yt = TREE_TYPE (y);
9106 if (DECL_NAME (y) != NULL_TREE)
9107 yn = DECL_NAME (y);
9108 else if (RECORD_OR_UNION_TYPE_P (yt)
9109 && TYPE_NAME (yt) != NULL_TREE
9110 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9111 yn = DECL_NAME (TYPE_NAME (yt));
9112 else
9113 yn = NULL_TREE;
9114
9115 if (xn != NULL_TREE && xn == yn)
9116 return true;
9117 }
9118
9119 return false;
9120 }
9121
9122 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9123 to HTAB, giving errors for any duplicates. */
9124
9125 static void
9126 detect_field_duplicates_hash (tree fieldlist,
9127 hash_table<nofree_ptr_hash <tree_node> > *htab)
9128 {
9129 tree x, y;
9130 tree_node **slot;
9131
9132 for (x = fieldlist; x ; x = DECL_CHAIN (x))
9133 if ((y = DECL_NAME (x)) != NULL_TREE)
9134 {
9135 slot = htab->find_slot (y, INSERT);
9136 if (*slot)
9137 {
9138 error ("duplicate member %q+D", x);
9139 DECL_NAME (x) = NULL_TREE;
9140 }
9141 *slot = y;
9142 }
9143 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9144 {
9145 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9146
9147 /* When using -fplan9-extensions, an anonymous field whose
9148 name is a typedef can duplicate a field name. */
9149 if (flag_plan9_extensions
9150 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9151 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9152 {
9153 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9154 slot = htab->find_slot (xn, INSERT);
9155 if (*slot)
9156 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9157 *slot = xn;
9158 }
9159 }
9160 }
9161
9162 /* Generate an error for any duplicate field names in FIELDLIST. Munge
9163 the list such that this does not present a problem later. */
9164
9165 static void
9166 detect_field_duplicates (tree fieldlist)
9167 {
9168 tree x, y;
9169 int timeout = 10;
9170
9171 /* If the struct is the list of instance variables of an Objective-C
9172 class, then we need to check all the instance variables of
9173 superclasses when checking for duplicates (since you can't have
9174 an instance variable in a subclass with the same name as an
9175 instance variable in a superclass). We pass on this job to the
9176 Objective-C compiler. objc_detect_field_duplicates() will return
9177 false if we are not checking the list of instance variables and
9178 the C frontend should proceed with the standard field duplicate
9179 checks. If we are checking the list of instance variables, the
9180 ObjC frontend will do the check, emit the errors if needed, and
9181 then return true. */
9182 if (c_dialect_objc ())
9183 if (objc_detect_field_duplicates (false))
9184 return;
9185
9186 /* First, see if there are more than "a few" fields.
9187 This is trivially true if there are zero or one fields. */
9188 if (!fieldlist || !DECL_CHAIN (fieldlist))
9189 return;
9190 x = fieldlist;
9191 do {
9192 timeout--;
9193 if (DECL_NAME (x) == NULL_TREE
9194 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9195 timeout = 0;
9196 x = DECL_CHAIN (x);
9197 } while (timeout > 0 && x);
9198
9199 /* If there were "few" fields and no anonymous structures or unions,
9200 avoid the overhead of allocating a hash table. Instead just do
9201 the nested traversal thing. */
9202 if (timeout > 0)
9203 {
9204 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9205 /* When using -fplan9-extensions, we can have duplicates
9206 between typedef names and fields. */
9207 if (DECL_NAME (x)
9208 || (flag_plan9_extensions
9209 && DECL_NAME (x) == NULL_TREE
9210 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9211 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9212 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9213 {
9214 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9215 if (is_duplicate_field (y, x))
9216 {
9217 error ("duplicate member %q+D", x);
9218 DECL_NAME (x) = NULL_TREE;
9219 }
9220 }
9221 }
9222 else
9223 {
9224 hash_table<nofree_ptr_hash <tree_node> > htab (37);
9225 detect_field_duplicates_hash (fieldlist, &htab);
9226 }
9227 }
9228
9229 /* Finish up struct info used by -Wc++-compat. */
9230
9231 static void
9232 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9233 location_t record_loc)
9234 {
9235 unsigned int ix;
9236 tree x;
9237 struct c_binding *b;
9238
9239 if (fieldlist == NULL_TREE)
9240 {
9241 if (code == RECORD_TYPE)
9242 warning_at (record_loc, OPT_Wc___compat,
9243 "empty struct has size 0 in C, size 1 in C++");
9244 else
9245 warning_at (record_loc, OPT_Wc___compat,
9246 "empty union has size 0 in C, size 1 in C++");
9247 }
9248
9249 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9250 the current struct. We do this now at the end of the struct
9251 because the flag is used to issue visibility warnings, and we
9252 only want to issue those warnings if the type is referenced
9253 outside of the struct declaration. */
9254 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9255 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9256
9257 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9258 typedefs used when declaring fields in this struct. If the name
9259 of any of the fields is also a typedef name then the struct would
9260 not parse in C++, because the C++ lookup rules say that the
9261 typedef name would be looked up in the context of the struct, and
9262 would thus be the field rather than the typedef. */
9263 if (!struct_parse_info->typedefs_seen.is_empty ()
9264 && fieldlist != NULL_TREE)
9265 {
9266 /* Use a hash_set<tree> using the name of the typedef. We can use
9267 a hash_set<tree> because identifiers are interned. */
9268 hash_set<tree> tset;
9269
9270 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9271 tset.add (DECL_NAME (x));
9272
9273 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9274 {
9275 if (DECL_NAME (x) != NULL_TREE
9276 && tset.contains (DECL_NAME (x)))
9277 {
9278 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9279 ("using %qD as both field and typedef name is "
9280 "invalid in C++"),
9281 x);
9282 /* FIXME: It would be nice to report the location where
9283 the typedef name is used. */
9284 }
9285 }
9286 }
9287
9288 /* For each field which has a binding and which was not defined in
9289 an enclosing struct, clear the in_struct field. */
9290 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9291 b->in_struct = 0;
9292 }
9293
9294 /* Function to help qsort sort FIELD_DECLs by name order. */
9295
9296 static int
9297 field_decl_cmp (const void *x_p, const void *y_p)
9298 {
9299 const tree *const x = (const tree *) x_p;
9300 const tree *const y = (const tree *) y_p;
9301
9302 if (DECL_NAME (*x) == DECL_NAME (*y))
9303 /* A nontype is "greater" than a type. */
9304 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9305 if (DECL_NAME (*x) == NULL_TREE)
9306 return -1;
9307 if (DECL_NAME (*y) == NULL_TREE)
9308 return 1;
9309 if (DECL_NAME (*x) < DECL_NAME (*y))
9310 return -1;
9311 return 1;
9312 }
9313
9314 /* If this structure or union completes the type of any previous
9315 variable declaration, lay it out and output its rtl. */
9316 static void
9317 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9318 {
9319 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9320 {
9321 tree decl = TREE_VALUE (x);
9322 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9323 layout_array_type (TREE_TYPE (decl));
9324 if (TREE_CODE (decl) != TYPE_DECL)
9325 {
9326 relayout_decl (decl);
9327 if (c_dialect_objc ())
9328 objc_check_decl (decl);
9329 rest_of_decl_compilation (decl, toplevel, 0);
9330 }
9331 }
9332 }
9333
9334 /* Determine whether the FIELD_DECL X is a flexible array member according to
9335 the following info:
9336 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9337 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9338 or "[1]";
9339 C. flag_strict_flex_arrays;
9340 D. the attribute strict_flex_array that is attached to the field
9341 if presenting.
9342 Return TRUE when it's a flexible array member, FALSE otherwise. */
9343
9344 static bool
9345 is_flexible_array_member_p (bool is_last_field,
9346 tree x)
9347 {
9348 /* If not the last field, return false. */
9349 if (!is_last_field)
9350 return false;
9351
9352 /* If not an array field, return false. */
9353 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9354 return false;
9355
9356 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9357 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9358 bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9359
9360 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9361
9362 switch (strict_flex_array_level)
9363 {
9364 case 0:
9365 /* Default, all trailing arrays are flexible array members. */
9366 return true;
9367 case 1:
9368 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9369 if (is_one_element_array)
9370 return true;
9371 /* FALLTHROUGH. */
9372 case 2:
9373 /* Level 2: all "[0]", and "[]" are flexible array members. */
9374 if (is_zero_length_array)
9375 return true;
9376 /* FALLTHROUGH. */
9377 case 3:
9378 /* Level 3: Only "[]" are flexible array members. */
9379 if (is_flexible_array)
9380 return true;
9381 break;
9382 default:
9383 gcc_unreachable ();
9384 }
9385 return false;
9386 }
9387
9388 /* Recompute TYPE_CANONICAL for variants of the type including qualified
9389 versions of the type and related pointer types after an aggregate type
9390 has been finalized.
9391 Will not update array types, pointers to array types, function
9392 types and other derived types created while the type was still
9393 incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9394
9395 static void
9396 c_update_type_canonical (tree t)
9397 {
9398 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9399 for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9400 {
9401 if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9402 {
9403 if (!TYPE_QUALS (x))
9404 TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9405 else
9406 {
9407 tree
9408 c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9409 if (TYPE_STRUCTURAL_EQUALITY_P (c))
9410 {
9411 gcc_checking_assert (TYPE_CANONICAL (t) == t);
9412 if (c == x)
9413 TYPE_CANONICAL (x) = x;
9414 else
9415 {
9416 /* build_qualified_type for this function unhelpfully
9417 moved c from some later spot in TYPE_MAIN_VARIANT (t)
9418 chain to right after t (or created it there). Move
9419 it right before x and process c and then x. */
9420 gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9421 if (l != t)
9422 {
9423 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9424 TYPE_NEXT_VARIANT (l) = c;
9425 TYPE_NEXT_VARIANT (c) = x;
9426 }
9427 TYPE_CANONICAL (c) = c;
9428 x = c;
9429 }
9430 }
9431 else
9432 TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9433 }
9434 }
9435 else if (x != t)
9436 continue;
9437 for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9438 {
9439 if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9440 continue;
9441 if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9442 TYPE_CANONICAL (p)
9443 = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9444 false);
9445 else
9446 TYPE_CANONICAL (p) = p;
9447 c_update_type_canonical (p);
9448 }
9449 }
9450 }
9451
9452 /* Verify the argument of the counted_by attribute of the flexible array
9453 member FIELD_DECL is a valid field of the containing structure,
9454 STRUCT_TYPE, Report error and remove this attribute when it's not. */
9455
9456 static void
9457 verify_counted_by_attribute (tree struct_type, tree field_decl)
9458 {
9459 tree attr_counted_by = lookup_attribute ("counted_by",
9460 DECL_ATTRIBUTES (field_decl));
9461
9462 if (!attr_counted_by)
9463 return;
9464
9465 /* If there is an counted_by attribute attached to the field,
9466 verify it. */
9467
9468 tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9469
9470 /* Verify the argument of the attrbute is a valid field of the
9471 containing structure. */
9472
9473 tree counted_by_field = lookup_field (struct_type, fieldname);
9474
9475 /* Error when the field is not found in the containing structure and
9476 remove the corresponding counted_by attribute from the field_decl. */
9477 if (!counted_by_field)
9478 {
9479 error_at (DECL_SOURCE_LOCATION (field_decl),
9480 "argument %qE to the %<counted_by%> attribute"
9481 " is not a field declaration in the same structure"
9482 " as %qD", fieldname, field_decl);
9483 DECL_ATTRIBUTES (field_decl)
9484 = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9485 }
9486 else
9487 /* Error when the field is not with an integer type. */
9488 {
9489 while (TREE_CHAIN (counted_by_field))
9490 counted_by_field = TREE_CHAIN (counted_by_field);
9491 tree real_field = TREE_VALUE (counted_by_field);
9492
9493 if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9494 {
9495 error_at (DECL_SOURCE_LOCATION (field_decl),
9496 "argument %qE to the %<counted_by%> attribute"
9497 " is not a field declaration with an integer type",
9498 fieldname);
9499 DECL_ATTRIBUTES (field_decl)
9500 = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9501 }
9502 }
9503 }
9504
9505 /* TYPE is a struct or union that we're applying may_alias to after the body is
9506 parsed. Fixup any POINTER_TO types. */
9507
9508 static void
9509 c_fixup_may_alias (tree type)
9510 {
9511 for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9512 for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9513 TYPE_REF_CAN_ALIAS_ALL (v) = true;
9514 }
9515
9516 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9517 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9518 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9519 ATTRIBUTES are attributes to be applied to the structure.
9520
9521 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9522 the struct was started. */
9523
9524 tree
9525 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9526 class c_struct_parse_info *enclosing_struct_parse_info,
9527 tree *expr)
9528 {
9529 tree x;
9530 bool toplevel = file_scope == current_scope;
9531
9532 /* If this type was previously laid out as a forward reference,
9533 make sure we lay it out again. */
9534
9535 TYPE_SIZE (t) = NULL_TREE;
9536
9537 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9538
9539 if (pedantic)
9540 {
9541 for (x = fieldlist; x; x = DECL_CHAIN (x))
9542 {
9543 if (DECL_NAME (x) != NULL_TREE)
9544 break;
9545 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9546 break;
9547 }
9548
9549 if (x == NULL_TREE)
9550 {
9551 if (TREE_CODE (t) == UNION_TYPE)
9552 {
9553 if (fieldlist)
9554 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9555 else
9556 pedwarn (loc, OPT_Wpedantic, "union has no members");
9557 }
9558 else
9559 {
9560 if (fieldlist)
9561 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9562 else
9563 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9564 }
9565 }
9566 }
9567
9568 /* Install struct as DECL_CONTEXT of each field decl.
9569 Also process specified field sizes, found in the DECL_INITIAL,
9570 storing 0 there after the type has been changed to precision equal
9571 to its width, rather than the precision of the specified standard
9572 type. (Correct layout requires the original type to have been preserved
9573 until now.) */
9574
9575 bool saw_named_field = false;
9576 tree counted_by_fam_field = NULL_TREE;
9577 for (x = fieldlist; x; x = DECL_CHAIN (x))
9578 {
9579 /* Whether this field is the last field of the structure or union.
9580 for UNION, any field is the last field of it. */
9581 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9582 || (TREE_CODE (t) == UNION_TYPE);
9583
9584 if (TREE_TYPE (x) == error_mark_node)
9585 continue;
9586
9587 DECL_CONTEXT (x) = t;
9588
9589 tree t1 = strip_array_types (TREE_TYPE (x));
9590 /* If any field is const, the structure type is pseudo-const. */
9591 if (TREE_READONLY (x))
9592 C_TYPE_FIELDS_READONLY (t) = 1;
9593 else
9594 {
9595 /* A field that is pseudo-const makes the structure likewise. */
9596 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9597 C_TYPE_FIELDS_READONLY (t) = 1;
9598 }
9599
9600 /* Any field that is volatile means variables of this type must be
9601 treated in some ways as volatile. */
9602 if (TREE_THIS_VOLATILE (x))
9603 {
9604 C_TYPE_FIELDS_VOLATILE (t) = 1;
9605 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9606 }
9607
9608 /* Any field that is volatile, restrict-qualified or atomic
9609 means the type cannot be used for a constexpr object. */
9610 if (TYPE_QUALS (t1)
9611 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9612 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9613 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9614 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9615
9616 /* Any field of nominal variable size implies structure is too. */
9617 if (C_DECL_VARIABLE_SIZE (x))
9618 C_TYPE_VARIABLE_SIZE (t) = 1;
9619
9620 /* If any field is variably modified, record this fact. */
9621 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9622 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9623
9624 if (DECL_C_BIT_FIELD (x))
9625 {
9626 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9627 DECL_SIZE (x) = bitsize_int (width);
9628 DECL_BIT_FIELD (x) = 1;
9629 }
9630
9631 if (TYPE_PACKED (t)
9632 && (DECL_BIT_FIELD (x)
9633 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9634 DECL_PACKED (x) = 1;
9635
9636 /* Detect flexible array member in an invalid context. */
9637 if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9638 {
9639 if (TREE_CODE (t) == UNION_TYPE)
9640 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9641 "flexible array member in union is a GCC extension");
9642 else if (!is_last_field)
9643 {
9644 error_at (DECL_SOURCE_LOCATION (x),
9645 "flexible array member not at end of struct");
9646 TREE_TYPE (x) = error_mark_node;
9647 }
9648 else if (!saw_named_field)
9649 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9650 "flexible array member in a struct with no named "
9651 "members is a GCC extension");
9652
9653 /* If there is a counted_by attribute attached to this field,
9654 record it here and do more verification later after the
9655 whole structure is complete. */
9656 if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9657 counted_by_fam_field = x;
9658 }
9659
9660 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9661 && flexible_array_type_p (TREE_TYPE (x)))
9662 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9663 "invalid use of structure with flexible array member");
9664
9665 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9666 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9667
9668 /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9669 when x is an array and is the last field.
9670 There is only one last_field for a structure type, but there might
9671 be multiple last_fields for a union type, therefore we should ORed
9672 the result for multiple last_fields. */
9673 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9674 TYPE_INCLUDES_FLEXARRAY (t)
9675 |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9676 /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9677 when x is an union or record and is the last field. */
9678 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9679 TYPE_INCLUDES_FLEXARRAY (t)
9680 |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9681
9682 if (warn_flex_array_member_not_at_end
9683 && !is_last_field
9684 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9685 && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9686 warning_at (DECL_SOURCE_LOCATION (x),
9687 OPT_Wflex_array_member_not_at_end,
9688 "structure containing a flexible array member"
9689 " is not at the end of another structure");
9690
9691 if (DECL_NAME (x)
9692 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9693 saw_named_field = true;
9694
9695 if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9696 && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9697 TYPE_TYPELESS_STORAGE (t) = true;
9698 }
9699
9700 detect_field_duplicates (fieldlist);
9701
9702 /* Now we have the nearly final fieldlist. Record it,
9703 then lay out the structure or union (including the fields). */
9704
9705 TYPE_FIELDS (t) = fieldlist;
9706
9707 maybe_apply_pragma_scalar_storage_order (t);
9708
9709 layout_type (t);
9710
9711 if (TYPE_SIZE_UNIT (t)
9712 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9713 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9714 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9715 error ("type %qT is too large", t);
9716
9717 /* Give bit-fields their proper types and rewrite the type of array fields
9718 with scalar component if the enclosing type has reverse storage order. */
9719 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9720 {
9721 if (TREE_CODE (field) == FIELD_DECL
9722 && DECL_INITIAL (field)
9723 && TREE_TYPE (field) != error_mark_node)
9724 {
9725 unsigned HOST_WIDE_INT width
9726 = tree_to_uhwi (DECL_INITIAL (field));
9727 tree type = TREE_TYPE (field);
9728 if (VECTOR_TYPE_P (type))
9729 {
9730 error_at (DECL_SOURCE_LOCATION (field),
9731 "bit-field %qD has invalid type", field);
9732 type = TREE_TYPE (type);
9733 TREE_TYPE (field) = type;
9734 }
9735 if (width != TYPE_PRECISION (type))
9736 {
9737 if (TREE_CODE (type) == BITINT_TYPE
9738 && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9739 TREE_TYPE (field)
9740 = build_bitint_type (width, TYPE_UNSIGNED (type));
9741 else
9742 TREE_TYPE (field)
9743 = c_build_bitfield_integer_type (width,
9744 TYPE_UNSIGNED (type));
9745 if (tree attr = c_hardbool_type_attr (type))
9746 decl_attributes (&TREE_TYPE (field),
9747 copy_list (attr),
9748 0, NULL_TREE);
9749 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9750 }
9751 DECL_INITIAL (field) = NULL_TREE;
9752 }
9753 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9754 && TREE_CODE (field) == FIELD_DECL
9755 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9756 {
9757 tree ftype = TREE_TYPE (field);
9758 tree ctype = strip_array_types (ftype);
9759 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9760 {
9761 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9762 tree *typep = &fmain_type;
9763 do {
9764 *typep = build_distinct_type_copy (*typep);
9765 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9766 typep = &TREE_TYPE (*typep);
9767 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9768 TREE_TYPE (field)
9769 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9770 }
9771 }
9772
9773 /* Warn on problematic type punning for storage order purposes. */
9774 if (TREE_CODE (t) == UNION_TYPE
9775 && TREE_CODE (field) == FIELD_DECL
9776 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9777 {
9778 tree ftype = TREE_TYPE (field);
9779 if (TREE_CODE (ftype) == ARRAY_TYPE)
9780 ftype = strip_array_types (ftype);
9781 if (RECORD_OR_UNION_TYPE_P (ftype)
9782 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9783 != TYPE_REVERSE_STORAGE_ORDER (t))
9784 warning_at (DECL_SOURCE_LOCATION (field),
9785 OPT_Wscalar_storage_order,
9786 "type punning toggles scalar storage order");
9787 }
9788 }
9789
9790 /* Now we have the truly final field list.
9791 Store it in this type and in the variants. */
9792
9793 TYPE_FIELDS (t) = fieldlist;
9794
9795 /* If there are lots of fields, sort so we can look through them fast.
9796 We arbitrarily consider 16 or more elts to be "a lot". */
9797
9798 {
9799 int len = 0;
9800
9801 for (x = fieldlist; x; x = DECL_CHAIN (x))
9802 {
9803 if (len > 15 || DECL_NAME (x) == NULL)
9804 break;
9805 len += 1;
9806 }
9807
9808 if (len > 15)
9809 {
9810 tree *field_array;
9811 struct lang_type *space;
9812 struct sorted_fields_type *space2;
9813
9814 len += list_length (x);
9815
9816 /* Use the same allocation policy here that make_node uses, to
9817 ensure that this lives as long as the rest of the struct decl.
9818 All decls in an inline function need to be saved. */
9819
9820 space = ggc_cleared_alloc<struct lang_type> ();
9821 space2 = (sorted_fields_type *) ggc_internal_alloc
9822 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9823
9824 len = 0;
9825 space->s = space2;
9826 field_array = &space2->elts[0];
9827 for (x = fieldlist; x; x = DECL_CHAIN (x))
9828 {
9829 field_array[len++] = x;
9830
9831 /* If there is anonymous struct or union, break out of the loop. */
9832 if (DECL_NAME (x) == NULL)
9833 break;
9834 }
9835 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9836 if (x == NULL)
9837 {
9838 TYPE_LANG_SPECIFIC (t) = space;
9839 TYPE_LANG_SPECIFIC (t)->s->len = len;
9840 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9841 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9842 }
9843 }
9844 }
9845
9846 /* If this was supposed to be a transparent union, but we can't
9847 make it one, warn and turn off the flag. */
9848 if (TREE_CODE (t) == UNION_TYPE
9849 && TYPE_TRANSPARENT_AGGR (t)
9850 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9851 {
9852 TYPE_TRANSPARENT_AGGR (t) = 0;
9853 warning_at (loc, 0, "union cannot be made transparent");
9854 }
9855
9856 /* Check for consistency with previous definition. */
9857 if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9858 {
9859 tree vistype = previous_tag (t);
9860 if (vistype
9861 && TREE_CODE (vistype) == TREE_CODE (t)
9862 && !C_TYPE_BEING_DEFINED (vistype))
9863 {
9864 TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9865 if (c_type_variably_modified_p (t))
9866 {
9867 error ("redefinition of struct or union %qT with variably "
9868 "modified type", t);
9869 if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9870 inform (struct_parse_info->refloc, "originally defined here");
9871 }
9872 else if (!comptypes_same_p (t, vistype))
9873 {
9874 error ("redefinition of struct or union %qT", t);
9875 if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9876 inform (struct_parse_info->refloc, "originally defined here");
9877 }
9878 }
9879 }
9880
9881 C_TYPE_BEING_DEFINED (t) = 0;
9882
9883 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9884 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9885 c_fixup_may_alias (x);
9886
9887 /* Set type canonical based on equivalence class. */
9888 if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9889 {
9890 if (c_struct_htab == NULL)
9891 c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9892
9893 hashval_t hash = c_struct_hasher::hash (t);
9894
9895 gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9896 gcc_checking_assert (!TYPE_NAME (t)
9897 || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9898
9899 tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9900 if (*e)
9901 TYPE_CANONICAL (t) = TYPE_CANONICAL (*e);
9902 else
9903 {
9904 TYPE_CANONICAL (t) = c_type_canonical (t);
9905 *e = t;
9906 }
9907 c_update_type_canonical (t);
9908 }
9909
9910 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9911 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9912 {
9913 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9914 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9915 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9916 TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9917 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9918 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9919 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9920 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9921 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9922 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9923 TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9924 }
9925
9926 /* Update type location to the one of the definition, instead of e.g.
9927 a forward declaration. */
9928 if (TYPE_STUB_DECL (t))
9929 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9930
9931 /* Finish debugging output for this type. */
9932 rest_of_type_compilation (t, toplevel);
9933
9934 finish_incomplete_vars (incomplete_vars, toplevel);
9935
9936 /* Make sure a DECL_EXPR is created for structs with VLA members.
9937 Because we do not know the context, we always pass expr
9938 to force creation of a BIND_EXPR which is required in some
9939 contexts. */
9940 if (c_type_variably_modified_p (t))
9941 add_decl_expr (loc, t, expr, false);
9942
9943 if (warn_cxx_compat)
9944 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9945
9946 if (NULL != enclosing_struct_parse_info)
9947 {
9948 delete struct_parse_info;
9949
9950 struct_parse_info = enclosing_struct_parse_info;
9951
9952 /* If this struct is defined inside a struct, add it to
9953 struct_types. */
9954 if (warn_cxx_compat
9955 && struct_parse_info != NULL
9956 && !in_sizeof && !in_typeof && !in_alignof)
9957 struct_parse_info->struct_types.safe_push (t);
9958 }
9959
9960 if (counted_by_fam_field)
9961 verify_counted_by_attribute (t, counted_by_fam_field);
9962
9963 return t;
9964 }
9965
9966 static struct {
9967 gt_pointer_operator new_value;
9968 void *cookie;
9969 } resort_data;
9970
9971 /* This routine compares two fields like field_decl_cmp but using the
9972 pointer operator in resort_data. */
9973
9974 static int
9975 resort_field_decl_cmp (const void *x_p, const void *y_p)
9976 {
9977 const tree *const x = (const tree *) x_p;
9978 const tree *const y = (const tree *) y_p;
9979
9980 if (DECL_NAME (*x) == DECL_NAME (*y))
9981 /* A nontype is "greater" than a type. */
9982 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9983 if (DECL_NAME (*x) == NULL_TREE)
9984 return -1;
9985 if (DECL_NAME (*y) == NULL_TREE)
9986 return 1;
9987 {
9988 tree d1 = DECL_NAME (*x);
9989 tree d2 = DECL_NAME (*y);
9990 resort_data.new_value (&d1, &d1, resort_data.cookie);
9991 resort_data.new_value (&d2, &d2, resort_data.cookie);
9992 if (d1 < d2)
9993 return -1;
9994 }
9995 return 1;
9996 }
9997
9998 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9999
10000 void
10001 resort_sorted_fields (void *obj,
10002 void * ARG_UNUSED (orig_obj),
10003 gt_pointer_operator new_value,
10004 void *cookie)
10005 {
10006 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10007 resort_data.new_value = new_value;
10008 resort_data.cookie = cookie;
10009 qsort (&sf->elts[0], sf->len, sizeof (tree),
10010 resort_field_decl_cmp);
10011 }
10012
10013 /* Lay out the type T, and its element type, and so on. */
10014
10015 static void
10016 layout_array_type (tree t)
10017 {
10018 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10019 layout_array_type (TREE_TYPE (t));
10020 layout_type (t);
10021 }
10022
10023 /* Begin compiling the definition of an enumeration type.
10024 NAME is its name (or null if anonymous).
10025 LOC is the enum's location.
10026 FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10027 definition.
10028 Returns the type object, as yet incomplete.
10029 Also records info about it so that build_enumerator
10030 may be used to declare the individual values as they are read. */
10031
10032 tree
10033 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10034 tree fixed_underlying_type, bool potential_nesting_p)
10035 {
10036 tree enumtype = NULL_TREE;
10037 location_t enumloc = UNKNOWN_LOCATION;
10038
10039 /* If this is the real definition for a previous forward reference,
10040 fill in the contents in the same object that used to be the
10041 forward reference. */
10042
10043 if (name != NULL_TREE)
10044 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10045
10046 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10047 {
10048 /* If the type is currently being defined or if we have seen an
10049 incomplete version which is now complete, this is a nested
10050 redefinition. The later happens if the redefinition occurs
10051 inside the enum specifier itself. */
10052 if (C_TYPE_BEING_DEFINED (enumtype)
10053 || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10054 error_at (loc, "nested redefinition of %<enum %E%>", name);
10055
10056 /* For C23 we allow redefinitions. We set to zero and check for
10057 consistency later. */
10058 if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10059 enumtype = NULL_TREE;
10060 }
10061
10062 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10063 {
10064 enumtype = make_node (ENUMERAL_TYPE);
10065 TYPE_SIZE (enumtype) = NULL_TREE;
10066 SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10067 pushtag (loc, name, enumtype);
10068 if (fixed_underlying_type != NULL_TREE)
10069 {
10070 /* For an enum definition with a fixed underlying type, the
10071 type is complete during the definition and the
10072 enumeration constants have that type. If there was a
10073 tag, the type was completed in c_parser_enum_specifier.
10074 If not, it must be completed here. */
10075 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10076 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10077 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10078 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10079 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10080 TYPE_SIZE (enumtype) = NULL_TREE;
10081 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10082 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10083 TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10084 c_update_type_canonical (enumtype);
10085 layout_type (enumtype);
10086 }
10087 }
10088 /* Update type location to the one of the definition, instead of e.g.
10089 a forward declaration. */
10090 else if (TYPE_STUB_DECL (enumtype))
10091 {
10092 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10093 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10094 }
10095
10096 C_TYPE_BEING_DEFINED (enumtype) = 1;
10097
10098 if (TYPE_VALUES (enumtype) != NULL_TREE)
10099 {
10100 /* This enum is a named one that has been declared already. */
10101 auto_diagnostic_group d;
10102 error_at (loc, "redeclaration of %<enum %E%>", name);
10103 if (enumloc != UNKNOWN_LOCATION)
10104 inform (enumloc, "originally defined here");
10105
10106 /* Completely replace its old definition.
10107 The old enumerators remain defined, however. */
10108 TYPE_VALUES (enumtype) = NULL_TREE;
10109 }
10110
10111 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10112 && fixed_underlying_type == NULL_TREE)
10113 {
10114 error_at (loc, "%<enum%> declared with but defined without "
10115 "fixed underlying type");
10116 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10117 }
10118
10119 the_enum->enum_next_value = integer_zero_node;
10120 the_enum->enum_type = enumtype;
10121 the_enum->enum_overflow = 0;
10122
10123 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10124 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10125 TYPE_PACKED (v) = 1;
10126
10127 /* FIXME: This will issue a warning for a use of a type defined
10128 within sizeof in a statement expr. This is not terribly serious
10129 as C++ doesn't permit statement exprs within sizeof anyhow. */
10130 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
10131 warning_at (loc, OPT_Wc___compat,
10132 "defining type in %qs expression is invalid in C++",
10133 (in_sizeof
10134 ? "sizeof"
10135 : (in_typeof ? "typeof" : "alignof")));
10136
10137 if (in_underspecified_init)
10138 error_at (loc, "%qT defined in underspecified object initializer",
10139 enumtype);
10140
10141 return enumtype;
10142 }
10143
10144 /* After processing and defining all the values of an enumeration type,
10145 install their decls in the enumeration type and finish it off.
10146 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10147 and ATTRIBUTES are the specified attributes.
10148 Returns ENUMTYPE. */
10149
10150 tree
10151 finish_enum (tree enumtype, tree values, tree attributes)
10152 {
10153 tree pair, tem;
10154 tree minnode = NULL_TREE, maxnode = NULL_TREE;
10155 int precision;
10156 signop sign;
10157 bool toplevel = (file_scope == current_scope);
10158 struct lang_type *lt;
10159
10160 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10161
10162 /* Calculate the maximum value of any enumerator in this type. */
10163
10164 if (values == error_mark_node)
10165 minnode = maxnode = integer_zero_node;
10166 else
10167 {
10168 minnode = maxnode = TREE_VALUE (values);
10169 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10170 {
10171 tree value = TREE_VALUE (pair);
10172 if (tree_int_cst_lt (maxnode, value))
10173 maxnode = value;
10174 if (tree_int_cst_lt (value, minnode))
10175 minnode = value;
10176 }
10177 }
10178
10179 /* Construct the final type of this enumeration. It is the same
10180 as one of the integral types - the narrowest one that fits, except
10181 that normally we only go as narrow as int - and signed iff any of
10182 the values are negative. */
10183 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10184 precision = MAX (tree_int_cst_min_precision (minnode, sign),
10185 tree_int_cst_min_precision (maxnode, sign));
10186
10187 bool wider_than_int =
10188 (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10189 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10190
10191
10192 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10193 {
10194 /* If the precision of the type was specified with an attribute and it
10195 was too small, give an error. Otherwise, use it. */
10196 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10197 {
10198 if (precision > TYPE_PRECISION (enumtype))
10199 {
10200 TYPE_PRECISION (enumtype) = 0;
10201 error ("specified mode too small for enumerated values");
10202 }
10203 else
10204 precision = TYPE_PRECISION (enumtype);
10205 }
10206 else
10207 TYPE_PRECISION (enumtype) = 0;
10208
10209 if (TYPE_PACKED (enumtype)
10210 || precision > TYPE_PRECISION (integer_type_node)
10211 || TYPE_PRECISION (enumtype))
10212 {
10213 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10214 if (tem == NULL)
10215 {
10216 /* This should only occur when both signed and unsigned
10217 values of maximum precision occur among the
10218 enumerators. */
10219 pedwarn (input_location, 0,
10220 "enumeration values exceed range of largest integer");
10221 tem = widest_integer_literal_type_node;
10222 }
10223 else if (precision > TYPE_PRECISION (intmax_type_node)
10224 && !tree_int_cst_lt (minnode,
10225 TYPE_MIN_VALUE (intmax_type_node))
10226 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10227 maxnode))
10228 pedwarn (input_location, OPT_Wpedantic,
10229 "enumeration values exceed range of %qs",
10230 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10231 }
10232 else
10233 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10234
10235 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10236 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10237 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10238 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10239 TYPE_SIZE (enumtype) = NULL_TREE;
10240 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10241 ENUM_UNDERLYING_TYPE (enumtype) =
10242 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10243
10244 TYPE_CANONICAL (enumtype) =
10245 TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10246 c_update_type_canonical (enumtype);
10247
10248 layout_type (enumtype);
10249 }
10250
10251 if (values != error_mark_node)
10252 {
10253 /* Change the type of the enumerators to be the enum type. We
10254 need to do this irrespective of the size of the enum, for
10255 proper type checking. Replace the DECL_INITIALs of the
10256 enumerators, and the value slots of the list, with copies
10257 that have the enum type; they cannot be modified in place
10258 because they may be shared (e.g. integer_zero_node) Finally,
10259 change the purpose slots to point to the names of the decls. */
10260 for (pair = values; pair; pair = TREE_CHAIN (pair))
10261 {
10262 tree enu = TREE_PURPOSE (pair);
10263 tree ini = DECL_INITIAL (enu);
10264
10265 TREE_TYPE (enu) = enumtype;
10266
10267 /* Before C23, the ISO C Standard mandates enumerators to
10268 have type int, even though the underlying type of an enum
10269 type is unspecified. However, C23 allows enumerators of
10270 any integer type, and if an enumeration has any
10271 enumerators wider than int, all enumerators have the
10272 enumerated type after it is parsed. Any enumerators that
10273 fit in int are given type int in build_enumerator (which
10274 is the correct type while the enumeration is being
10275 parsed), so no conversions are needed here if all
10276 enumerators fit in int. If the enum has a fixed
10277 underlying type, the correct type was also given in
10278 build_enumerator. */
10279 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10280 ini = convert (enumtype, ini);
10281
10282 DECL_INITIAL (enu) = ini;
10283 TREE_PURPOSE (pair) = DECL_NAME (enu);
10284 /* To match the C++ FE, store the CONST_DECL rather than just its
10285 value. */
10286 TREE_VALUE (pair) = enu;
10287 }
10288
10289 TYPE_VALUES (enumtype) = values;
10290 }
10291
10292 /* Record the min/max values so that we can warn about bit-field
10293 enumerations that are too small for the values. */
10294 lt = ggc_cleared_alloc<struct lang_type> ();
10295 lt->enum_min = minnode;
10296 lt->enum_max = maxnode;
10297 TYPE_LANG_SPECIFIC (enumtype) = lt;
10298
10299 /* Fix up all variant types of this enum type. */
10300 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10301 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10302 {
10303 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10304 if (tem == enumtype)
10305 continue;
10306 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10307 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10308 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10309 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10310 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10311 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10312 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10313 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10314 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10315 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10316 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10317 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10318 }
10319
10320 /* Finish debugging output for this type. */
10321 rest_of_type_compilation (enumtype, toplevel);
10322
10323 finish_incomplete_vars (incomplete_vars, toplevel);
10324
10325 /* If this enum is defined inside a struct, add it to
10326 struct_types. */
10327 if (warn_cxx_compat
10328 && struct_parse_info != NULL
10329 && !in_sizeof && !in_typeof && !in_alignof)
10330 struct_parse_info->struct_types.safe_push (enumtype);
10331
10332 /* Check for consistency with previous definition */
10333 if (flag_isoc23)
10334 {
10335 tree vistype = previous_tag (enumtype);
10336 if (vistype
10337 && TREE_CODE (vistype) == TREE_CODE (enumtype)
10338 && !C_TYPE_BEING_DEFINED (vistype))
10339 {
10340 TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10341 if (!comptypes_same_p (enumtype, vistype))
10342 error("conflicting redefinition of enum %qT", enumtype);
10343 }
10344 }
10345
10346 C_TYPE_BEING_DEFINED (enumtype) = 0;
10347
10348 return enumtype;
10349 }
10350
10351 /* Build and install a CONST_DECL for one value of the
10352 current enumeration type (one that was begun with start_enum).
10353 DECL_LOC is the location of the enumerator.
10354 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10355 Return a tree-list containing the CONST_DECL and its value.
10356 Assignment of sequential values by default is handled here. */
10357
10358 tree
10359 build_enumerator (location_t decl_loc, location_t loc,
10360 struct c_enum_contents *the_enum, tree name, tree value)
10361 {
10362 tree decl;
10363 tree old_decl;
10364
10365 /* Validate and default VALUE. */
10366
10367 if (value != NULL_TREE)
10368 {
10369 /* Don't issue more errors for error_mark_node (i.e. an
10370 undeclared identifier) - just ignore the value expression. */
10371 if (value == error_mark_node)
10372 value = NULL_TREE;
10373 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10374 {
10375 error_at (loc, "enumerator value for %qE is not an integer constant",
10376 name);
10377 value = NULL_TREE;
10378 }
10379 else
10380 {
10381 if (TREE_CODE (value) != INTEGER_CST)
10382 {
10383 value = c_fully_fold (value, false, NULL);
10384 if (TREE_CODE (value) == INTEGER_CST)
10385 pedwarn (loc, OPT_Wpedantic,
10386 "enumerator value for %qE is not an integer "
10387 "constant expression", name);
10388 }
10389 if (TREE_CODE (value) != INTEGER_CST)
10390 {
10391 error ("enumerator value for %qE is not an integer constant",
10392 name);
10393 value = NULL_TREE;
10394 }
10395 else
10396 {
10397 value = default_conversion (value);
10398 constant_expression_warning (value);
10399 }
10400 }
10401 }
10402
10403 /* Default based on previous value. */
10404 /* It should no longer be possible to have NON_LVALUE_EXPR
10405 in the default. */
10406 if (value == NULL_TREE)
10407 {
10408 value = the_enum->enum_next_value;
10409 if (the_enum->enum_overflow)
10410 error_at (loc, "overflow in enumeration values");
10411 }
10412 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10413 {
10414 /* Enumeration constants must fit in the fixed underlying type. */
10415 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10416 error_at (loc,
10417 "enumerator value outside the range of underlying type");
10418 /* Enumeration constants for an enum with fixed underlying type
10419 have the enum type, both inside and outside the
10420 definition. */
10421 value = convert (the_enum->enum_type, value);
10422 }
10423 else if (flag_isoc23
10424 && (old_decl = lookup_name_in_scope (name, current_scope))
10425 && old_decl != error_mark_node
10426 && TREE_TYPE (old_decl)
10427 && TREE_TYPE (TREE_TYPE (old_decl))
10428 && TREE_CODE (old_decl) == CONST_DECL)
10429 {
10430 /* Enumeration constants in a redeclaration have the previous type. */
10431 tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10432 if (!int_fits_type_p (value, previous_type))
10433 {
10434 error_at (loc, "value of redeclared enumerator outside the range "
10435 "of %qT", previous_type);
10436 locate_old_decl (old_decl);
10437 }
10438 value = convert (previous_type, value);
10439 }
10440 else
10441 {
10442 /* Even though the underlying type of an enum is unspecified, the
10443 type of enumeration constants is explicitly defined as int
10444 (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10445 GCC allows such types for older standards as an extension. */
10446 bool warned_range = false;
10447 if (!int_fits_type_p (value,
10448 (TYPE_UNSIGNED (TREE_TYPE (value))
10449 ? uintmax_type_node
10450 : intmax_type_node)))
10451 /* GCC does not consider its types larger than intmax_t to be
10452 extended integer types (although C23 would permit such types to
10453 be considered extended integer types if all the features
10454 required by <stdint.h> and <inttypes.h> macros, such as support
10455 for integer constants and I/O, were present), so diagnose if
10456 such a wider type is used. (If the wider type arose from a
10457 constant of such a type, that will also have been diagnosed,
10458 but this is the only diagnostic in the case where it arises
10459 from choosing a wider type automatically when adding 1
10460 overflows.) */
10461 warned_range = pedwarn (loc, OPT_Wpedantic,
10462 "enumerator value outside the range of %qs",
10463 (TYPE_UNSIGNED (TREE_TYPE (value))
10464 ? "uintmax_t"
10465 : "intmax_t"));
10466 if (!warned_range && !int_fits_type_p (value, integer_type_node))
10467 pedwarn_c11 (loc, OPT_Wpedantic,
10468 "ISO C restricts enumerator values to range of %<int%> "
10469 "before C23");
10470
10471 /* The ISO C Standard mandates enumerators to have type int before
10472 C23, even though the underlying type of an enum type is
10473 unspecified. C23 allows enumerators of any integer type. During
10474 the parsing of the enumeration, C23 specifies that constants
10475 representable in int have type int, constants not representable
10476 in int have the type of the given expression if any, and
10477 constants not representable in int and derived by adding 1 to the
10478 previous constant have the type of that constant unless the
10479 addition would overflow or wraparound, in which case a wider type
10480 of the same signedness is chosen automatically; after the
10481 enumeration is parsed, all the constants have the type of the
10482 enumeration if any do not fit in int. */
10483 if (int_fits_type_p (value, integer_type_node))
10484 value = convert (integer_type_node, value);
10485 }
10486
10487 /* Set basis for default for next value. */
10488 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10489 {
10490 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10491 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10492 /* A value of 2 following a value of 1 overflows bool, but we
10493 cannot carry out addition directly on bool without
10494 promotion, and converting the result of arithmetic in a
10495 wider type back to bool would not produce the right result
10496 for this overflow check. */
10497 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10498 else
10499 the_enum->enum_next_value
10500 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10501 PLUS_EXPR, convert (underlying_type, value),
10502 convert (underlying_type, integer_one_node),
10503 false);
10504 }
10505 else
10506 {
10507 /* In a redeclaration the type can already be the enumeral type. */
10508 if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10509 value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10510 the_enum->enum_next_value
10511 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10512 PLUS_EXPR, value, integer_one_node, false);
10513 }
10514 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10515 if (the_enum->enum_overflow
10516 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10517 {
10518 /* Choose a wider type with the same signedness if
10519 available. */
10520 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10521 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10522 tree new_type = (unsignedp
10523 ? long_unsigned_type_node
10524 : long_integer_type_node);
10525 if (prec > TYPE_PRECISION (new_type))
10526 new_type = (unsignedp
10527 ? long_long_unsigned_type_node
10528 : long_long_integer_type_node);
10529 if (prec > TYPE_PRECISION (new_type))
10530 new_type = (unsignedp
10531 ? widest_unsigned_literal_type_node
10532 : widest_integer_literal_type_node);
10533 if (prec <= TYPE_PRECISION (new_type))
10534 {
10535 the_enum->enum_overflow = false;
10536 the_enum->enum_next_value
10537 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10538 PLUS_EXPR, convert (new_type, value),
10539 integer_one_node, false);
10540 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10541 }
10542 }
10543
10544 /* Now create a declaration for the enum value name. */
10545
10546 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10547 DECL_INITIAL (decl) = value;
10548 DECL_CONTEXT (decl) = the_enum->enum_type;
10549 pushdecl (decl);
10550
10551 return tree_cons (decl, value, NULL_TREE);
10552 }
10553
10554 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10555
10556 tree
10557 c_simulate_enum_decl (location_t loc, const char *name,
10558 vec<string_int_pair> *values_ptr)
10559 {
10560 location_t saved_loc = input_location;
10561 input_location = loc;
10562
10563 struct c_enum_contents the_enum;
10564 tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10565 NULL_TREE, false);
10566
10567 tree value_chain = NULL_TREE;
10568 string_int_pair *value;
10569 vec<string_int_pair> values = *values_ptr;
10570 unsigned int i;
10571 FOR_EACH_VEC_ELT (values, i, value)
10572 {
10573 tree decl = build_enumerator (loc, loc, &the_enum,
10574 get_identifier (value->first),
10575 build_int_cst (integer_type_node,
10576 value->second));
10577 TREE_CHAIN (decl) = value_chain;
10578 value_chain = decl;
10579 }
10580
10581 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10582
10583 input_location = saved_loc;
10584 return enumtype;
10585 }
10586
10587 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10588
10589 tree
10590 c_simulate_record_decl (location_t loc, const char *name,
10591 array_slice<const tree> fields)
10592 {
10593 location_t saved_loc = input_location;
10594 input_location = loc;
10595
10596 class c_struct_parse_info *struct_info;
10597 tree ident = get_identifier (name);
10598 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10599
10600 for (unsigned int i = 0; i < fields.size (); ++i)
10601 {
10602 DECL_FIELD_CONTEXT (fields[i]) = type;
10603 if (i > 0)
10604 DECL_CHAIN (fields[i - 1]) = fields[i];
10605 }
10606
10607 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10608
10609 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10610 set_underlying_type (decl);
10611 lang_hooks.decls.pushdecl (decl);
10612
10613 input_location = saved_loc;
10614 return type;
10615 }
10616
10617 /* Create the FUNCTION_DECL for a function definition.
10618 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10619 the declaration; they describe the function's name and the type it returns,
10620 but twisted together in a fashion that parallels the syntax of C.
10621
10622 This function creates a binding context for the function body
10623 as well as setting up the FUNCTION_DECL in current_function_decl.
10624
10625 Returns true on success. If the DECLARATOR is not suitable for a function
10626 (it defines a datum instead), we return false to report a parse error. */
10627
10628 bool
10629 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10630 tree attributes)
10631 {
10632 tree decl1, old_decl;
10633 tree restype, resdecl;
10634 location_t loc;
10635 location_t result_loc;
10636 tree expr = NULL;
10637
10638 current_function_returns_value = 0; /* Assume, until we see it does. */
10639 current_function_returns_null = 0;
10640 current_function_returns_abnormally = 0;
10641 warn_about_return_type = 0;
10642 c_switch_stack = NULL;
10643
10644 /* Indicate no valid break/continue context. */
10645 in_statement = 0;
10646
10647 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10648 &attributes, &expr, NULL, DEPRECATED_NORMAL);
10649 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10650
10651 /* If the declarator is not suitable for a function definition,
10652 cause a syntax error. */
10653 if (decl1 == NULL_TREE
10654 || TREE_CODE (decl1) != FUNCTION_DECL)
10655 return false;
10656
10657 /* Nested functions may have variably modified (return) type.
10658 Make sure the size expression is evaluated at this point. */
10659 if (expr && !current_scope->parm_flag)
10660 add_stmt (fold_convert (void_type_node, expr));
10661
10662 loc = DECL_SOURCE_LOCATION (decl1);
10663
10664 /* A nested function is not global. */
10665 if (current_function_decl != NULL_TREE)
10666 TREE_PUBLIC (decl1) = 0;
10667
10668 c_decl_attributes (&decl1, attributes, 0);
10669
10670 if (DECL_DECLARED_INLINE_P (decl1)
10671 && DECL_UNINLINABLE (decl1)
10672 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10673 {
10674 auto_urlify_attributes sentinel;
10675 warning_at (loc, OPT_Wattributes,
10676 "inline function %qD given attribute %qs",
10677 decl1, "noinline");
10678 }
10679
10680 /* Handle gnu_inline attribute. */
10681 if (declspecs->inline_p
10682 && !flag_gnu89_inline
10683 && TREE_CODE (decl1) == FUNCTION_DECL
10684 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10685 || current_function_decl))
10686 {
10687 if (declspecs->storage_class != csc_static)
10688 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10689 }
10690
10691 announce_function (decl1);
10692
10693 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10694 {
10695 error_at (loc, "return type is an incomplete type");
10696 /* Make it return void instead. */
10697 TREE_TYPE (decl1)
10698 = c_build_function_type (void_type_node,
10699 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10700 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10701 }
10702
10703 if (warn_about_return_type)
10704 permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10705 : (warn_return_type > 0 ? OPT_Wreturn_type
10706 : OPT_Wimplicit_int),
10707 "return type defaults to %<int%>");
10708
10709 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10710 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10711 DECL_INITIAL (decl1) = error_mark_node;
10712
10713 /* If this definition isn't a prototype and we had a prototype declaration
10714 before, copy the arg type info from that prototype. */
10715 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10716 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10717 old_decl = NULL_TREE;
10718
10719 current_function_prototype_locus = UNKNOWN_LOCATION;
10720 current_function_prototype_built_in = false;
10721 current_function_prototype_arg_types = NULL_TREE;
10722 tree newtype = TREE_TYPE (decl1);
10723 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10724 if (!prototype_p (newtype))
10725 {
10726 tree oldrt = TREE_TYPE (oldtype);
10727 tree newrt = TREE_TYPE (newtype);
10728 if (old_decl != NULL_TREE
10729 && TREE_CODE (oldtype) == FUNCTION_TYPE
10730 && comptypes (oldrt, newrt))
10731 {
10732 if (stdarg_p (oldtype))
10733 {
10734 auto_diagnostic_group d;
10735 warning_at (loc, 0, "%q+D defined as variadic function "
10736 "without prototype", decl1);
10737 locate_old_decl (old_decl);
10738 }
10739 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10740 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10741 current_function_prototype_built_in
10742 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10743 current_function_prototype_arg_types
10744 = TYPE_ARG_TYPES (newtype);
10745 }
10746 if (TREE_PUBLIC (decl1))
10747 {
10748 /* If there is an external prototype declaration of this
10749 function, record its location but do not copy information
10750 to this decl. This may be an invisible declaration
10751 (built-in or in a scope which has finished) or simply
10752 have more refined argument types than any declaration
10753 found above. */
10754 struct c_binding *b;
10755 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10756 if (B_IN_SCOPE (b, external_scope))
10757 break;
10758 if (b)
10759 {
10760 tree ext_decl, ext_type;
10761 ext_decl = b->decl;
10762 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10763 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10764 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10765 TREE_TYPE (ext_type)))
10766 {
10767 current_function_prototype_locus
10768 = DECL_SOURCE_LOCATION (ext_decl);
10769 current_function_prototype_built_in
10770 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10771 current_function_prototype_arg_types
10772 = TYPE_ARG_TYPES (ext_type);
10773 }
10774 }
10775 }
10776 }
10777
10778 /* Optionally warn about C23 compatibility. */
10779 if (warn_deprecated_non_prototype
10780 && old_decl != NULL_TREE
10781 && TREE_CODE (oldtype) == FUNCTION_TYPE
10782 && !TYPE_ARG_TYPES (oldtype)
10783 && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10784 && (TYPE_ARG_TYPES (newtype)
10785 && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10786 {
10787 bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10788 "ISO C23 does not allow defining"
10789 " parameters for function %qE declared"
10790 " without parameters",
10791 decl1);
10792 if (warned)
10793 inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10794 }
10795
10796 /* Optionally warn of old-fashioned def with no previous prototype. */
10797 if (warn_strict_prototypes
10798 && old_decl != error_mark_node
10799 && !prototype_p (TREE_TYPE (decl1))
10800 && C_DECL_ISNT_PROTOTYPE (old_decl))
10801 warning_at (loc, OPT_Wstrict_prototypes,
10802 "function declaration isn%'t a prototype");
10803 /* Optionally warn of any global def with no previous prototype. */
10804 else if (warn_missing_prototypes
10805 && old_decl != error_mark_node
10806 && TREE_PUBLIC (decl1)
10807 && !MAIN_NAME_P (DECL_NAME (decl1))
10808 && C_DECL_ISNT_PROTOTYPE (old_decl)
10809 && !DECL_DECLARED_INLINE_P (decl1))
10810 warning_at (loc, OPT_Wmissing_prototypes,
10811 "no previous prototype for %qD", decl1);
10812 /* Optionally warn of any def with no previous prototype
10813 if the function has already been used. */
10814 else if (warn_missing_prototypes
10815 && old_decl != NULL_TREE
10816 && old_decl != error_mark_node
10817 && TREE_USED (old_decl)
10818 && !prototype_p (TREE_TYPE (old_decl)))
10819 warning_at (loc, OPT_Wmissing_prototypes,
10820 "%qD was used with no prototype before its definition", decl1);
10821 /* Optionally warn of any global def with no previous declaration. */
10822 else if (warn_missing_declarations
10823 && TREE_PUBLIC (decl1)
10824 && old_decl == NULL_TREE
10825 && !MAIN_NAME_P (DECL_NAME (decl1))
10826 && !DECL_DECLARED_INLINE_P (decl1))
10827 warning_at (loc, OPT_Wmissing_declarations,
10828 "no previous declaration for %qD",
10829 decl1);
10830 /* Optionally warn of any def with no previous declaration
10831 if the function has already been used. */
10832 else if (warn_missing_declarations
10833 && old_decl != NULL_TREE
10834 && old_decl != error_mark_node
10835 && TREE_USED (old_decl)
10836 && C_DECL_IMPLICIT (old_decl))
10837 warning_at (loc, OPT_Wmissing_declarations,
10838 "%qD was used with no declaration before its definition", decl1);
10839
10840 /* This function exists in static storage.
10841 (This does not mean `static' in the C sense!) */
10842 TREE_STATIC (decl1) = 1;
10843
10844 /* This is the earliest point at which we might know the assembler
10845 name of the function. Thus, if it's set before this, die horribly. */
10846 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10847
10848 /* If #pragma weak was used, mark the decl weak now. */
10849 if (current_scope == file_scope)
10850 maybe_apply_pragma_weak (decl1);
10851
10852 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10853 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10854 {
10855 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10856 != integer_type_node)
10857 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10858 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10859 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10860 decl1);
10861
10862 check_main_parameter_types (decl1);
10863
10864 if (!TREE_PUBLIC (decl1))
10865 pedwarn (loc, OPT_Wmain,
10866 "%qD is normally a non-static function", decl1);
10867 }
10868
10869 tree parms = current_function_arg_info->parms;
10870 if (old_decl)
10871 {
10872 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10873 warn_parm_array_mismatch (origloc, old_decl, parms);
10874 }
10875
10876 /* Record the decl so that the function name is defined.
10877 If we already have a decl for this name, and it is a FUNCTION_DECL,
10878 use the old decl. */
10879
10880 current_function_decl = pushdecl (decl1);
10881
10882 if (tree access = build_attr_access_from_parms (parms, false))
10883 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10884 old_decl);
10885
10886 push_scope ();
10887 declare_parm_level ();
10888
10889 /* Set the result decl source location to the location of the typespec. */
10890 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10891 ? loc : declspecs->locations[cdw_typespec]);
10892 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10893 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10894 DECL_ARTIFICIAL (resdecl) = 1;
10895 DECL_IGNORED_P (resdecl) = 1;
10896 DECL_RESULT (current_function_decl) = resdecl;
10897
10898 start_fname_decls ();
10899
10900 return true;
10901 }
10902
10903 /* Subroutine of store_parm_decls which handles new-style function
10904 definitions (prototype format). The parms already have decls, so we
10905 need only record them as in effect and complain if any redundant
10906 old-style parm decls were written. */
10907 static void
10908 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10909 {
10910 tree decl;
10911 c_arg_tag *tag;
10912 unsigned ix;
10913
10914 if (current_scope->bindings)
10915 {
10916 error_at (DECL_SOURCE_LOCATION (fndecl),
10917 "old-style parameter declarations in prototyped "
10918 "function definition");
10919
10920 /* Get rid of the old-style declarations. */
10921 pop_scope ();
10922 push_scope ();
10923 }
10924 /* Don't issue this warning for nested functions, and don't issue this
10925 warning if we got here because ARG_INFO_TYPES was error_mark_node
10926 (this happens when a function definition has just an ellipsis in
10927 its parameter list). */
10928 else if (!in_system_header_at (input_location)
10929 && !current_function_scope
10930 && arg_info->types != error_mark_node
10931 && !arg_info->c23_empty_parens)
10932 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10933 "traditional C rejects ISO C style function definitions");
10934
10935 /* Now make all the parameter declarations visible in the function body.
10936 We can bypass most of the grunt work of pushdecl. */
10937 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10938 {
10939 DECL_CONTEXT (decl) = current_function_decl;
10940 if (DECL_NAME (decl))
10941 {
10942 bind (DECL_NAME (decl), decl, current_scope,
10943 /*invisible=*/false, /*nested=*/false,
10944 UNKNOWN_LOCATION);
10945 if (!TREE_USED (decl))
10946 warn_if_shadowing (decl);
10947 }
10948 else
10949 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
10950 "ISO C does not support omitting parameter names in "
10951 "function definitions before C23");
10952 }
10953
10954 /* Record the parameter list in the function declaration. */
10955 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10956
10957 /* Now make all the ancillary declarations visible, likewise. */
10958 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10959 {
10960 DECL_CONTEXT (decl) = current_function_decl;
10961 if (DECL_NAME (decl))
10962 bind (DECL_NAME (decl), decl, current_scope,
10963 /*invisible=*/false,
10964 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10965 UNKNOWN_LOCATION);
10966 }
10967
10968 /* And all the tag declarations. */
10969 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10970 if (tag->id)
10971 bind (tag->id, tag->type, current_scope,
10972 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10973 }
10974
10975 /* Subroutine of store_parm_decls which handles old-style function
10976 definitions (separate parameter list and declarations). */
10977
10978 static void
10979 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10980 {
10981 struct c_binding *b;
10982 tree parm, decl, last;
10983 tree parmids = arg_info->parms;
10984 hash_set<tree> seen_args;
10985
10986 if (!in_system_header_at (input_location))
10987 {
10988 if (flag_isoc23)
10989 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10990 OPT_Wold_style_definition, "old-style function definition");
10991 else
10992 warning_at (DECL_SOURCE_LOCATION (fndecl),
10993 OPT_Wold_style_definition,
10994 "old-style function definition");
10995 }
10996
10997 if (current_scope->had_vla_unspec)
10998 error ("%<[*]%> not allowed in other than function prototype scope");
10999
11000 /* Match each formal parameter name with its declaration. Save each
11001 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
11002 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11003 {
11004 if (TREE_VALUE (parm) == NULL_TREE)
11005 {
11006 error_at (DECL_SOURCE_LOCATION (fndecl),
11007 "parameter name missing from parameter list");
11008 TREE_PURPOSE (parm) = NULL_TREE;
11009 continue;
11010 }
11011
11012 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11013 if (b && B_IN_CURRENT_SCOPE (b))
11014 {
11015 decl = b->decl;
11016 /* Skip erroneous parameters. */
11017 if (decl == error_mark_node)
11018 continue;
11019 /* If we got something other than a PARM_DECL it is an error. */
11020 if (TREE_CODE (decl) != PARM_DECL)
11021 {
11022 error_at (DECL_SOURCE_LOCATION (decl),
11023 "%qD declared as a non-parameter", decl);
11024 continue;
11025 }
11026 /* If the declaration is already marked, we have a duplicate
11027 name. Complain and ignore the duplicate. */
11028 else if (seen_args.contains (decl))
11029 {
11030 error_at (DECL_SOURCE_LOCATION (decl),
11031 "multiple parameters named %qD", decl);
11032 TREE_PURPOSE (parm) = NULL_TREE;
11033 continue;
11034 }
11035 /* If the declaration says "void", complain and turn it into
11036 an int. */
11037 else if (VOID_TYPE_P (TREE_TYPE (decl)))
11038 {
11039 error_at (DECL_SOURCE_LOCATION (decl),
11040 "parameter %qD declared with void type", decl);
11041 TREE_TYPE (decl) = integer_type_node;
11042 DECL_ARG_TYPE (decl) = integer_type_node;
11043 layout_decl (decl, 0);
11044 }
11045 warn_if_shadowing (decl);
11046 }
11047 /* If no declaration found, default to int. */
11048 else
11049 {
11050 /* FIXME diagnostics: This should be the location of the argument,
11051 not the FNDECL. E.g., for an old-style declaration
11052
11053 int f10(v) { blah; }
11054
11055 We should use the location of the V, not the F10.
11056 Unfortunately, the V is an IDENTIFIER_NODE which has no
11057 location. In the future we need locations for c_arg_info
11058 entries.
11059
11060 See gcc.dg/Wshadow-3.c for an example of this problem. */
11061 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11062 PARM_DECL, TREE_VALUE (parm), integer_type_node);
11063 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11064 pushdecl (decl);
11065 warn_if_shadowing (decl);
11066
11067 if (flag_isoc99)
11068 permerror_opt (DECL_SOURCE_LOCATION (decl),
11069 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11070 decl);
11071 else
11072 warning_at (DECL_SOURCE_LOCATION (decl),
11073 OPT_Wmissing_parameter_type,
11074 "type of %qD defaults to %<int%>", decl);
11075 }
11076
11077 TREE_PURPOSE (parm) = decl;
11078 seen_args.add (decl);
11079 }
11080
11081 /* Now examine the parms chain for incomplete declarations
11082 and declarations with no corresponding names. */
11083
11084 for (b = current_scope->bindings; b; b = b->prev)
11085 {
11086 parm = b->decl;
11087 if (TREE_CODE (parm) != PARM_DECL)
11088 continue;
11089
11090 if (TREE_TYPE (parm) != error_mark_node
11091 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11092 {
11093 error_at (DECL_SOURCE_LOCATION (parm),
11094 "parameter %qD has incomplete type", parm);
11095 TREE_TYPE (parm) = error_mark_node;
11096 }
11097
11098 if (!seen_args.contains (parm))
11099 {
11100 error_at (DECL_SOURCE_LOCATION (parm),
11101 "declaration for parameter %qD but no such parameter",
11102 parm);
11103
11104 /* Pretend the parameter was not missing.
11105 This gets us to a standard state and minimizes
11106 further error messages. */
11107 parmids = chainon (parmids, tree_cons (parm, 0, 0));
11108 }
11109 }
11110
11111 /* Chain the declarations together in the order of the list of
11112 names. Store that chain in the function decl, replacing the
11113 list of names. Update the current scope to match. */
11114 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11115
11116 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11117 if (TREE_PURPOSE (parm))
11118 break;
11119 if (parm && TREE_PURPOSE (parm))
11120 {
11121 last = TREE_PURPOSE (parm);
11122 DECL_ARGUMENTS (fndecl) = last;
11123
11124 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11125 if (TREE_PURPOSE (parm))
11126 {
11127 DECL_CHAIN (last) = TREE_PURPOSE (parm);
11128 last = TREE_PURPOSE (parm);
11129 }
11130 DECL_CHAIN (last) = NULL_TREE;
11131 }
11132
11133 /* If there was a previous prototype,
11134 set the DECL_ARG_TYPE of each argument according to
11135 the type previously specified, and report any mismatches. */
11136
11137 if (current_function_prototype_arg_types)
11138 {
11139 tree type;
11140 for (parm = DECL_ARGUMENTS (fndecl),
11141 type = current_function_prototype_arg_types;
11142 parm || (type != NULL_TREE
11143 && TREE_VALUE (type) != error_mark_node
11144 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11145 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11146 {
11147 if (parm == NULL_TREE
11148 || type == NULL_TREE
11149 || (TREE_VALUE (type) != error_mark_node
11150 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11151 {
11152 if (current_function_prototype_built_in)
11153 warning_at (DECL_SOURCE_LOCATION (fndecl),
11154 0, "number of arguments doesn%'t match "
11155 "built-in prototype");
11156 else
11157 {
11158 /* FIXME diagnostics: This should be the location of
11159 FNDECL, but there is bug when a prototype is
11160 declared inside function context, but defined
11161 outside of it (e.g., gcc.dg/pr15698-2.c). In
11162 which case FNDECL gets the location of the
11163 prototype, not the definition. */
11164 error_at (input_location,
11165 "number of arguments doesn%'t match prototype");
11166
11167 error_at (current_function_prototype_locus,
11168 "prototype declaration");
11169 }
11170 break;
11171 }
11172 /* Type for passing arg must be consistent with that
11173 declared for the arg. ISO C says we take the unqualified
11174 type for parameters declared with qualified type. */
11175 if (TREE_TYPE (parm) != error_mark_node
11176 && TREE_VALUE (type) != error_mark_node
11177 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11178 != TYPE_ATOMIC (TREE_VALUE (type)))
11179 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11180 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11181 {
11182 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11183 == TYPE_ATOMIC (TREE_VALUE (type)))
11184 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11185 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11186 {
11187 /* Adjust argument to match prototype. E.g. a previous
11188 `int foo(float);' prototype causes
11189 `int foo(x) float x; {...}' to be treated like
11190 `int foo(float x) {...}'. This is particularly
11191 useful for argument types like uid_t. */
11192 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11193
11194 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
11195 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
11196 && (TYPE_PRECISION (TREE_TYPE (parm))
11197 < TYPE_PRECISION (integer_type_node)))
11198 DECL_ARG_TYPE (parm)
11199 = c_type_promotes_to (TREE_TYPE (parm));
11200
11201 /* ??? Is it possible to get here with a
11202 built-in prototype or will it always have
11203 been diagnosed as conflicting with an
11204 old-style definition and discarded? */
11205 if (current_function_prototype_built_in)
11206 warning_at (DECL_SOURCE_LOCATION (parm),
11207 OPT_Wpedantic, "promoted argument %qD "
11208 "doesn%'t match built-in prototype", parm);
11209 else
11210 {
11211 pedwarn (DECL_SOURCE_LOCATION (parm),
11212 OPT_Wpedantic, "promoted argument %qD "
11213 "doesn%'t match prototype", parm);
11214 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11215 "prototype declaration");
11216 }
11217 }
11218 else
11219 {
11220 if (current_function_prototype_built_in)
11221 warning_at (DECL_SOURCE_LOCATION (parm),
11222 0, "argument %qD doesn%'t match "
11223 "built-in prototype", parm);
11224 else
11225 {
11226 error_at (DECL_SOURCE_LOCATION (parm),
11227 "argument %qD doesn%'t match prototype", parm);
11228 error_at (current_function_prototype_locus,
11229 "prototype declaration");
11230 }
11231 }
11232 }
11233 }
11234 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11235 }
11236
11237 /* Otherwise, create a prototype that would match. */
11238
11239 else
11240 {
11241 tree actual = NULL_TREE, last = NULL_TREE, type;
11242
11243 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11244 {
11245 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11246 if (last)
11247 TREE_CHAIN (last) = type;
11248 else
11249 actual = type;
11250 last = type;
11251 }
11252 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11253 if (last)
11254 TREE_CHAIN (last) = type;
11255 else
11256 actual = type;
11257
11258 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11259 of the type of this function, but we need to avoid having this
11260 affect the types of other similarly-typed functions, so we must
11261 first force the generation of an identical (but separate) type
11262 node for the relevant function type. The new node we create
11263 will be a variant of the main variant of the original function
11264 type. */
11265
11266 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11267
11268 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11269 }
11270 }
11271
11272 /* Store parameter declarations passed in ARG_INFO into the current
11273 function declaration. */
11274
11275 void
11276 store_parm_decls_from (struct c_arg_info *arg_info)
11277 {
11278 current_function_arg_info = arg_info;
11279 store_parm_decls ();
11280 }
11281
11282 /* Called by walk_tree to look for and update context-less labels
11283 or labels with context in the parent function. */
11284
11285 static tree
11286 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11287 {
11288 tree ctx = static_cast<tree>(data);
11289 if (TREE_CODE (*tp) == LABEL_EXPR
11290 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11291 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11292 {
11293 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11294 *walk_subtrees = 0;
11295 }
11296
11297 return NULL_TREE;
11298 }
11299
11300 /* Store the parameter declarations into the current function declaration.
11301 This is called after parsing the parameter declarations, before
11302 digesting the body of the function.
11303
11304 For an old-style definition, construct a prototype out of the old-style
11305 parameter declarations and inject it into the function's type. */
11306
11307 void
11308 store_parm_decls (void)
11309 {
11310 tree fndecl = current_function_decl;
11311 bool proto;
11312
11313 /* The argument information block for FNDECL. */
11314 struct c_arg_info *arg_info = current_function_arg_info;
11315 current_function_arg_info = 0;
11316
11317 /* True if this definition is written with a prototype. In C23, an
11318 empty argument list was converted to (void) in grokparms; in
11319 older C standard versions, it does not give the function a type
11320 with a prototype for future calls. */
11321 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11322
11323 if (proto)
11324 store_parm_decls_newstyle (fndecl, arg_info);
11325 else
11326 store_parm_decls_oldstyle (fndecl, arg_info);
11327
11328 /* The next call to push_scope will be a function body. */
11329
11330 next_is_function_body = true;
11331
11332 /* Write a record describing this function definition to the prototypes
11333 file (if requested). */
11334
11335 gen_aux_info_record (fndecl, 1, 0, proto);
11336
11337 /* Initialize the RTL code for the function. */
11338 allocate_struct_function (fndecl, false);
11339
11340 if (warn_unused_local_typedefs)
11341 cfun->language = ggc_cleared_alloc<language_function> ();
11342
11343 /* Begin the statement tree for this function. */
11344 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11345
11346 /* ??? Insert the contents of the pending sizes list into the function
11347 to be evaluated. The only reason left to have this is
11348 void foo(int n, int array[n++])
11349 because we throw away the array type in favor of a pointer type, and
11350 thus won't naturally see the SAVE_EXPR containing the increment. All
11351 other pending sizes would be handled by gimplify_parameters. */
11352 if (arg_info->pending_sizes)
11353 {
11354 /* In very special circumstances, e.g. for code like
11355 _Atomic int i = 5;
11356 void f (int a[i += 2]) {}
11357 we need to execute the atomic assignment on function entry.
11358 But in this case, it is not just a straight store, it has the
11359 op= form, which means that build_atomic_assign has generated
11360 gotos, labels, etc. Because at that time the function decl
11361 for F has not been created yet, those labels do not have any
11362 function context. But we have the fndecl now, so update the
11363 labels accordingly. gimplify_expr would crash otherwise.
11364 Or with nested functions the labels could be created with parent
11365 function's context, while when the statement is emitted at the
11366 start of the nested function, it needs the nested function's
11367 context. */
11368 walk_tree_without_duplicates (&arg_info->pending_sizes,
11369 set_labels_context_r, fndecl);
11370 add_stmt (arg_info->pending_sizes);
11371 }
11372 }
11373
11374 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11375 c_finish_omp_declare_simd for function prototypes. No diagnostics
11376 should be done. */
11377
11378 void
11379 temp_store_parm_decls (tree fndecl, tree parms)
11380 {
11381 push_scope ();
11382 for (tree p = parms; p; p = DECL_CHAIN (p))
11383 {
11384 DECL_CONTEXT (p) = fndecl;
11385 if (DECL_NAME (p))
11386 bind (DECL_NAME (p), p, current_scope,
11387 /*invisible=*/false, /*nested=*/false,
11388 UNKNOWN_LOCATION);
11389 }
11390 }
11391
11392 /* Undo what temp_store_parm_decls did. */
11393
11394 void
11395 temp_pop_parm_decls (void)
11396 {
11397 /* Clear all bindings in this temporary scope, so that
11398 pop_scope doesn't create a BLOCK. */
11399 struct c_binding *b = current_scope->bindings;
11400 current_scope->bindings = NULL;
11401 for (; b; b = free_binding_and_advance (b))
11402 {
11403 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11404 || b->decl == error_mark_node);
11405 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11406 I_SYMBOL_BINDING (b->id) = b->shadowed;
11407 if (b->shadowed && b->shadowed->u.type)
11408 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11409 }
11410 pop_scope ();
11411 }
11412
11413
11414 /* Finish up a function declaration and compile that function
11415 all the way to assembler language output. Then free the storage
11416 for the function definition.
11417
11418 This is called after parsing the body of the function definition. */
11419
11420 void
11421 finish_function (location_t end_loc)
11422 {
11423 tree fndecl = current_function_decl;
11424
11425 if (c_dialect_objc ())
11426 objc_finish_function ();
11427
11428 if (TREE_CODE (fndecl) == FUNCTION_DECL
11429 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
11430 {
11431 tree args = DECL_ARGUMENTS (fndecl);
11432 for (; args; args = DECL_CHAIN (args))
11433 {
11434 tree type = TREE_TYPE (args);
11435 if (INTEGRAL_TYPE_P (type)
11436 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
11437 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
11438 }
11439 }
11440
11441 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11442 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11443
11444 /* Must mark the RESULT_DECL as being in this function. */
11445
11446 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11447 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11448
11449 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11450 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11451 == integer_type_node && flag_isoc99)
11452 {
11453 /* Hack. We don't want the middle-end to warn that this return
11454 is unreachable, so we mark its location as special. Using
11455 UNKNOWN_LOCATION has the problem that it gets clobbered in
11456 annotate_one_with_locus. A cleaner solution might be to
11457 ensure ! should_carry_locus_p (stmt), but that needs a flag.
11458 */
11459 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11460 }
11461
11462 /* Tie off the statement tree for this function. */
11463 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11464
11465 finish_fname_decls ();
11466
11467 /* Complain if there's no return statement only if option specified on
11468 command line. */
11469 if (warn_return_type > 0
11470 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11471 && !current_function_returns_value && !current_function_returns_null
11472 /* Don't complain if we are no-return. */
11473 && !current_function_returns_abnormally
11474 /* Don't complain if we are declared noreturn. */
11475 && !TREE_THIS_VOLATILE (fndecl)
11476 /* Don't warn for main(). */
11477 && !MAIN_NAME_P (DECL_NAME (fndecl))
11478 /* Or if they didn't actually specify a return type. */
11479 && !C_FUNCTION_IMPLICIT_INT (fndecl)
11480 /* Normally, with -Wreturn-type, flow will complain, but we might
11481 optimize out static functions. */
11482 && !TREE_PUBLIC (fndecl)
11483 && targetm.warn_func_return (fndecl)
11484 && warning (OPT_Wreturn_type,
11485 "no return statement in function returning non-void"))
11486 suppress_warning (fndecl, OPT_Wreturn_type);
11487
11488 /* Complain about parameters that are only set, but never otherwise used. */
11489 if (warn_unused_but_set_parameter)
11490 {
11491 tree decl;
11492
11493 for (decl = DECL_ARGUMENTS (fndecl);
11494 decl;
11495 decl = DECL_CHAIN (decl))
11496 if (TREE_USED (decl)
11497 && TREE_CODE (decl) == PARM_DECL
11498 && !DECL_READ_P (decl)
11499 && DECL_NAME (decl)
11500 && !DECL_ARTIFICIAL (decl)
11501 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
11502 warning_at (DECL_SOURCE_LOCATION (decl),
11503 OPT_Wunused_but_set_parameter,
11504 "parameter %qD set but not used", decl);
11505 }
11506
11507 /* Complain about locally defined typedefs that are not used in this
11508 function. */
11509 maybe_warn_unused_local_typedefs ();
11510
11511 /* Possibly warn about unused parameters. */
11512 if (warn_unused_parameter)
11513 do_warn_unused_parameter (fndecl);
11514
11515 /* Store the end of the function, so that we get good line number
11516 info for the epilogue. */
11517 cfun->function_end_locus = end_loc;
11518
11519 /* Finalize the ELF visibility for the function. */
11520 c_determine_visibility (fndecl);
11521
11522 /* For GNU C extern inline functions disregard inline limits. */
11523 if (DECL_EXTERNAL (fndecl)
11524 && DECL_DECLARED_INLINE_P (fndecl)
11525 && (flag_gnu89_inline
11526 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11527 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11528
11529 /* Genericize before inlining. Delay genericizing nested functions
11530 until their parent function is genericized. Since finalizing
11531 requires GENERIC, delay that as well. */
11532
11533 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11534 && !undef_nested_function)
11535 {
11536 if (!decl_function_context (fndecl))
11537 {
11538 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11539 c_genericize (fndecl);
11540
11541 /* ??? Objc emits functions after finalizing the compilation unit.
11542 This should be cleaned up later and this conditional removed. */
11543 if (symtab->global_info_ready)
11544 {
11545 cgraph_node::add_new_function (fndecl, false);
11546 return;
11547 }
11548 cgraph_node::finalize_function (fndecl, false);
11549 }
11550 else
11551 {
11552 /* Register this function with cgraph just far enough to get it
11553 added to our parent's nested function list. Handy, since the
11554 C front end doesn't have such a list. */
11555 (void) cgraph_node::get_create (fndecl);
11556 }
11557 }
11558
11559 if (!decl_function_context (fndecl))
11560 undef_nested_function = false;
11561
11562 if (cfun->language != NULL)
11563 {
11564 ggc_free (cfun->language);
11565 cfun->language = NULL;
11566 }
11567
11568 /* We're leaving the context of this function, so zap cfun.
11569 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11570 tree_rest_of_compilation. */
11571 set_cfun (NULL);
11572 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11573 current_function_decl = NULL;
11574 }
11575
11576 /* Check the declarations given in a for-loop for satisfying the C99
11577 constraints. If exactly one such decl is found, return it. LOC is
11578 the location of the opening parenthesis of the for loop. The last
11579 parameter allows you to control the "for loop initial declarations
11580 are only allowed in C99 mode". Normally, you should pass
11581 flag_isoc99 as that parameter. But in some cases (Objective-C
11582 foreach loop, for example) we want to run the checks in this
11583 function even if not in C99 mode, so we allow the caller to turn
11584 off the error about not being in C99 mode.
11585 */
11586
11587 tree
11588 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11589 {
11590 struct c_binding *b;
11591 tree one_decl = NULL_TREE;
11592 int n_decls = 0;
11593
11594 if (!turn_off_iso_c99_error)
11595 {
11596 static bool hint = true;
11597 /* If we get here, declarations have been used in a for loop without
11598 the C99 for loop scope. This doesn't make much sense, so don't
11599 allow it. */
11600 auto_diagnostic_group d;
11601 error_at (loc, "%<for%> loop initial declarations "
11602 "are only allowed in C99 or C11 mode");
11603 if (hint)
11604 {
11605 inform (loc,
11606 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11607 "%<-std=gnu11%> to compile your code");
11608 hint = false;
11609 }
11610 return NULL_TREE;
11611 }
11612 else
11613 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11614 "initial declarations");
11615
11616 /* C99 subclause 6.8.5 paragraph 3:
11617
11618 [#3] The declaration part of a for statement shall only
11619 declare identifiers for objects having storage class auto or
11620 register.
11621
11622 It isn't clear whether, in this sentence, "identifiers" binds to
11623 "shall only declare" or to "objects" - that is, whether all identifiers
11624 declared must be identifiers for objects, or whether the restriction
11625 only applies to those that are. (A question on this in comp.std.c
11626 in November 2000 received no answer.) We implement the strictest
11627 interpretation, to avoid creating an extension which later causes
11628 problems.
11629
11630 This constraint was removed in C23. */
11631
11632 for (b = current_scope->bindings; b; b = b->prev)
11633 {
11634 tree id = b->id;
11635 tree decl = b->decl;
11636
11637 if (!id)
11638 continue;
11639
11640 switch (TREE_CODE (decl))
11641 {
11642 case VAR_DECL:
11643 {
11644 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11645 if (TREE_STATIC (decl))
11646 pedwarn_c11 (decl_loc, OPT_Wpedantic,
11647 "declaration of static variable %qD in %<for%> "
11648 "loop initial declaration", decl);
11649 else if (DECL_EXTERNAL (decl))
11650 pedwarn_c11 (decl_loc, OPT_Wpedantic,
11651 "declaration of %<extern%> variable %qD in %<for%> "
11652 "loop initial declaration", decl);
11653 }
11654 break;
11655
11656 case RECORD_TYPE:
11657 pedwarn_c11 (loc, OPT_Wpedantic,
11658 "%<struct %E%> declared in %<for%> loop initial "
11659 "declaration", id);
11660 break;
11661 case UNION_TYPE:
11662 pedwarn_c11 (loc, OPT_Wpedantic,
11663 "%<union %E%> declared in %<for%> loop initial "
11664 "declaration",
11665 id);
11666 break;
11667 case ENUMERAL_TYPE:
11668 pedwarn_c11 (loc, OPT_Wpedantic,
11669 "%<enum %E%> declared in %<for%> loop "
11670 "initial declaration", id);
11671 break;
11672 default:
11673 pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11674 "%qD in %<for%> loop initial declaration", decl);
11675 }
11676
11677 n_decls++;
11678 one_decl = decl;
11679 }
11680
11681 return n_decls == 1 ? one_decl : NULL_TREE;
11682 }
11683
11684 /* Save and reinitialize the variables
11685 used during compilation of a C function. */
11686
11687 void
11688 c_push_function_context (void)
11689 {
11690 struct language_function *p = cfun->language;
11691 /* cfun->language might have been already allocated by the use of
11692 -Wunused-local-typedefs. In that case, just re-use it. */
11693 if (p == NULL)
11694 cfun->language = p = ggc_cleared_alloc<language_function> ();
11695
11696 p->base.x_stmt_tree = c_stmt_tree;
11697 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11698 p->x_in_statement = in_statement;
11699 p->x_switch_stack = c_switch_stack;
11700 p->loop_names = loop_names;
11701 loop_names = vNULL;
11702 p->loop_names_hash = loop_names_hash;
11703 loop_names_hash = NULL;
11704 p->arg_info = current_function_arg_info;
11705 p->returns_value = current_function_returns_value;
11706 p->returns_null = current_function_returns_null;
11707 p->returns_abnormally = current_function_returns_abnormally;
11708 p->warn_about_return_type = warn_about_return_type;
11709
11710 push_function_context ();
11711 }
11712
11713 /* Restore the variables used during compilation of a C function. */
11714
11715 void
11716 c_pop_function_context (void)
11717 {
11718 struct language_function *p;
11719
11720 pop_function_context ();
11721 p = cfun->language;
11722
11723 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11724 used to store data throughout the life time of the current cfun,
11725 So don't deallocate it. */
11726 if (!warn_unused_local_typedefs)
11727 cfun->language = NULL;
11728
11729 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11730 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11731 {
11732 /* Stop pointing to the local nodes about to be freed. */
11733 /* But DECL_INITIAL must remain nonzero so we know this
11734 was an actual function definition. */
11735 DECL_INITIAL (current_function_decl) = error_mark_node;
11736 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11737 }
11738
11739 c_stmt_tree = p->base.x_stmt_tree;
11740 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11741 in_statement = p->x_in_statement;
11742 c_switch_stack = p->x_switch_stack;
11743 loop_names.release ();
11744 loop_names = p->loop_names;
11745 p->loop_names = vNULL;
11746 delete loop_names_hash;
11747 loop_names_hash = p->loop_names_hash;
11748 p->loop_names_hash = NULL;
11749 current_function_arg_info = p->arg_info;
11750 current_function_returns_value = p->returns_value;
11751 current_function_returns_null = p->returns_null;
11752 current_function_returns_abnormally = p->returns_abnormally;
11753 warn_about_return_type = p->warn_about_return_type;
11754 }
11755
11756 /* The functions below are required for functionality of doing
11757 function at once processing in the C front end. Currently these
11758 functions are not called from anywhere in the C front end, but as
11759 these changes continue, that will change. */
11760
11761 /* Returns the stmt_tree (if any) to which statements are currently
11762 being added. If there is no active statement-tree, NULL is
11763 returned. */
11764
11765 stmt_tree
11766 current_stmt_tree (void)
11767 {
11768 return &c_stmt_tree;
11769 }
11770
11771 /* Return the global value of T as a symbol. */
11772
11773 tree
11774 identifier_global_value (tree t)
11775 {
11776 struct c_binding *b;
11777
11778 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11779 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11780 return b->decl;
11781
11782 return NULL_TREE;
11783 }
11784
11785 /* Return the global value of tag T as a symbol. */
11786
11787 tree
11788 identifier_global_tag (tree t)
11789 {
11790 struct c_binding *b;
11791
11792 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11793 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11794 return b->decl;
11795
11796 return NULL_TREE;
11797 }
11798
11799 /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11800 function or function-like operator. */
11801
11802 int
11803 names_builtin_p (const char *name)
11804 {
11805 tree id = get_identifier (name);
11806 if (tree decl = identifier_global_value (id))
11807 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11808
11809 /* Also detect common reserved C words that aren't strictly built-in
11810 functions. */
11811 switch (C_RID_CODE (id))
11812 {
11813 case RID_BUILTIN_ASSOC_BARRIER:
11814 case RID_BUILTIN_CONVERTVECTOR:
11815 case RID_BUILTIN_HAS_ATTRIBUTE:
11816 case RID_BUILTIN_SHUFFLE:
11817 case RID_BUILTIN_SHUFFLEVECTOR:
11818 case RID_BUILTIN_STDC:
11819 case RID_BUILTIN_COUNTED_BY_REF:
11820 case RID_CHOOSE_EXPR:
11821 case RID_OFFSETOF:
11822 case RID_TYPES_COMPATIBLE_P:
11823 case RID_C23_VA_START:
11824 case RID_VA_ARG:
11825 return 1;
11826 default:
11827 break;
11828 }
11829
11830 return 0;
11831 }
11832
11833 /* In C, the only C-linkage public declaration is at file scope. */
11834
11835 tree
11836 c_linkage_bindings (tree name)
11837 {
11838 return identifier_global_value (name);
11839 }
11840
11841 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11842 otherwise the name is found in ridpointers from RID_INDEX. */
11843
11844 void
11845 record_builtin_type (enum rid rid_index, const char *name, tree type)
11846 {
11847 tree id, decl;
11848 if (name == 0)
11849 id = ridpointers[(int) rid_index];
11850 else
11851 id = get_identifier (name);
11852 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11853 pushdecl (decl);
11854 if (debug_hooks->type_decl)
11855 debug_hooks->type_decl (decl, false);
11856 }
11857
11858 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11859
11860 struct c_parm *
11861 build_c_parm (struct c_declspecs *specs, tree attrs,
11862 struct c_declarator *declarator,
11863 location_t loc)
11864 {
11865 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11866 ret->specs = specs;
11867 ret->attrs = attrs;
11868 ret->declarator = declarator;
11869 ret->loc = loc;
11870 return ret;
11871 }
11872
11873 /* Return a declarator with nested attributes. TARGET is the inner
11874 declarator to which these attributes apply. ATTRS are the
11875 attributes. */
11876
11877 struct c_declarator *
11878 build_attrs_declarator (tree attrs, struct c_declarator *target)
11879 {
11880 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11881 ret->kind = cdk_attrs;
11882 ret->declarator = target;
11883 ret->u.attrs = attrs;
11884 return ret;
11885 }
11886
11887 /* Return a declarator for a function with arguments specified by ARGS
11888 and return type specified by TARGET. */
11889
11890 struct c_declarator *
11891 build_function_declarator (struct c_arg_info *args,
11892 struct c_declarator *target)
11893 {
11894 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11895 ret->kind = cdk_function;
11896 ret->declarator = target;
11897 ret->u.arg_info = args;
11898 return ret;
11899 }
11900
11901 /* Return a declarator for the identifier IDENT (which may be
11902 NULL_TREE for an abstract declarator). */
11903
11904 struct c_declarator *
11905 build_id_declarator (tree ident)
11906 {
11907 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11908 ret->kind = cdk_id;
11909 ret->declarator = 0;
11910 ret->u.id.id = ident;
11911 ret->u.id.attrs = NULL_TREE;
11912 /* Default value - may get reset to a more precise location. */
11913 ret->id_loc = input_location;
11914 return ret;
11915 }
11916
11917 /* Return something to represent absolute declarators containing a *.
11918 TARGET is the absolute declarator that the * contains.
11919 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11920 to apply to the pointer type. */
11921
11922 struct c_declarator *
11923 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11924 struct c_declarator *target)
11925 {
11926 tree attrs;
11927 int quals = 0;
11928 struct c_declarator *itarget = target;
11929 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11930 if (type_quals_attrs)
11931 {
11932 attrs = type_quals_attrs->attrs;
11933 quals = quals_from_declspecs (type_quals_attrs);
11934 if (attrs != NULL_TREE)
11935 itarget = build_attrs_declarator (attrs, target);
11936 }
11937 ret->kind = cdk_pointer;
11938 ret->declarator = itarget;
11939 ret->u.pointer_quals = quals;
11940 return ret;
11941 }
11942
11943 /* Return a pointer to a structure for an empty list of declaration
11944 specifiers. */
11945
11946 struct c_declspecs *
11947 build_null_declspecs (void)
11948 {
11949 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11950 memset (ret, 0, sizeof *ret);
11951 ret->align_log = -1;
11952 ret->typespec_word = cts_none;
11953 ret->storage_class = csc_none;
11954 ret->expr_const_operands = true;
11955 ret->typespec_kind = ctsk_none;
11956 ret->address_space = ADDR_SPACE_GENERIC;
11957 return ret;
11958 }
11959
11960 /* Add the address space ADDRSPACE to the declaration specifiers
11961 SPECS, returning SPECS. */
11962
11963 struct c_declspecs *
11964 declspecs_add_addrspace (location_t location,
11965 struct c_declspecs *specs, addr_space_t as)
11966 {
11967 specs->non_sc_seen_p = true;
11968 specs->declspecs_seen_p = true;
11969 specs->non_std_attrs_seen_p = true;
11970
11971 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11972 && specs->address_space != as)
11973 error ("incompatible address space qualifiers %qs and %qs",
11974 c_addr_space_name (as),
11975 c_addr_space_name (specs->address_space));
11976 else
11977 {
11978 specs->address_space = as;
11979 specs->locations[cdw_address_space] = location;
11980 }
11981 return specs;
11982 }
11983
11984 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11985 returning SPECS. */
11986
11987 struct c_declspecs *
11988 declspecs_add_qual (location_t loc,
11989 struct c_declspecs *specs, tree qual)
11990 {
11991 enum rid i;
11992 bool dupe = false;
11993 specs->non_sc_seen_p = true;
11994 specs->declspecs_seen_p = true;
11995 specs->non_std_attrs_seen_p = true;
11996 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11997 && C_IS_RESERVED_WORD (qual));
11998 i = C_RID_CODE (qual);
11999 location_t prev_loc = UNKNOWN_LOCATION;
12000 switch (i)
12001 {
12002 case RID_CONST:
12003 dupe = specs->const_p;
12004 specs->const_p = true;
12005 prev_loc = specs->locations[cdw_const];
12006 specs->locations[cdw_const] = loc;
12007 break;
12008 case RID_VOLATILE:
12009 dupe = specs->volatile_p;
12010 specs->volatile_p = true;
12011 prev_loc = specs->locations[cdw_volatile];
12012 specs->locations[cdw_volatile] = loc;
12013 break;
12014 case RID_RESTRICT:
12015 dupe = specs->restrict_p;
12016 specs->restrict_p = true;
12017 prev_loc = specs->locations[cdw_restrict];
12018 specs->locations[cdw_restrict] = loc;
12019 break;
12020 case RID_ATOMIC:
12021 dupe = specs->atomic_p;
12022 specs->atomic_p = true;
12023 prev_loc = specs->locations[cdw_atomic];
12024 specs->locations[cdw_atomic] = loc;
12025 break;
12026 default:
12027 gcc_unreachable ();
12028 }
12029 if (dupe)
12030 {
12031 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
12032 "duplicate %qE declaration specifier", qual);
12033 if (!warned
12034 && warn_duplicate_decl_specifier
12035 && prev_loc >= RESERVED_LOCATION_COUNT
12036 && !from_macro_expansion_at (prev_loc)
12037 && !from_macro_expansion_at (loc))
12038 warning_at (loc, OPT_Wduplicate_decl_specifier,
12039 "duplicate %qE declaration specifier", qual);
12040 }
12041 return specs;
12042 }
12043
12044 /* Add the type specifier TYPE to the declaration specifiers SPECS,
12045 returning SPECS. */
12046
12047 struct c_declspecs *
12048 declspecs_add_type (location_t loc, struct c_declspecs *specs,
12049 struct c_typespec spec)
12050 {
12051 tree type = spec.spec;
12052 specs->non_sc_seen_p = true;
12053 specs->declspecs_seen_p = true;
12054 specs->non_std_attrs_seen_p = true;
12055 specs->typespec_kind = spec.kind;
12056 if (TREE_DEPRECATED (type))
12057 specs->deprecated_p = true;
12058 if (TREE_UNAVAILABLE (type))
12059 specs->unavailable_p = true;
12060
12061 /* As a type specifier is present, "auto" must be used as a storage
12062 class specifier, not for type deduction. */
12063 if (specs->c23_auto_p)
12064 {
12065 specs->c23_auto_p = false;
12066 if (specs->storage_class != csc_none)
12067 error ("multiple storage classes in declaration specifiers");
12068 else if (specs->thread_p)
12069 error ("%qs used with %<auto%>",
12070 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12071 else if (specs->constexpr_p)
12072 /* auto may only be used with another storage class specifier,
12073 such as constexpr, if the type is inferred. */
12074 error ("%<auto%> used with %<constexpr%>");
12075 else
12076 specs->storage_class = csc_auto;
12077 }
12078
12079 /* Handle type specifier keywords. */
12080 if (TREE_CODE (type) == IDENTIFIER_NODE
12081 && C_IS_RESERVED_WORD (type)
12082 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12083 {
12084 enum rid i = C_RID_CODE (type);
12085 if (specs->type)
12086 {
12087 error_at (loc, "two or more data types in declaration specifiers");
12088 return specs;
12089 }
12090 if ((int) i <= (int) RID_LAST_MODIFIER)
12091 {
12092 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12093 bool dupe = false;
12094 switch (i)
12095 {
12096 case RID_LONG:
12097 if (specs->long_long_p)
12098 {
12099 error_at (loc, "%<long long long%> is too long for GCC");
12100 break;
12101 }
12102 if (specs->long_p)
12103 {
12104 if (specs->typespec_word == cts_double)
12105 {
12106 error_at (loc,
12107 ("both %<long long%> and %<double%> in "
12108 "declaration specifiers"));
12109 break;
12110 }
12111 pedwarn_c90 (loc, OPT_Wlong_long,
12112 "ISO C90 does not support %<long long%>");
12113 specs->long_long_p = 1;
12114 specs->locations[cdw_long_long] = loc;
12115 break;
12116 }
12117 if (specs->short_p)
12118 error_at (loc,
12119 ("both %<long%> and %<short%> in "
12120 "declaration specifiers"));
12121 else if (specs->typespec_word == cts_auto_type)
12122 error_at (loc,
12123 ("both %<long%> and %<__auto_type%> in "
12124 "declaration specifiers"));
12125 else if (specs->typespec_word == cts_void)
12126 error_at (loc,
12127 ("both %<long%> and %<void%> in "
12128 "declaration specifiers"));
12129 else if (specs->typespec_word == cts_int_n)
12130 error_at (loc,
12131 ("both %<long%> and %<__int%d%> in "
12132 "declaration specifiers"),
12133 int_n_data[specs->u.int_n_idx].bitsize);
12134 else if (specs->typespec_word == cts_bool)
12135 error_at (loc,
12136 ("both %<long%> and %<_Bool%> in "
12137 "declaration specifiers"));
12138 else if (specs->typespec_word == cts_bitint)
12139 error_at (loc,
12140 ("both %<long%> and %<_BitInt%> in "
12141 "declaration specifiers"));
12142 else if (specs->typespec_word == cts_char)
12143 error_at (loc,
12144 ("both %<long%> and %<char%> in "
12145 "declaration specifiers"));
12146 else if (specs->typespec_word == cts_float)
12147 error_at (loc,
12148 ("both %<long%> and %<float%> in "
12149 "declaration specifiers"));
12150 else if (specs->typespec_word == cts_floatn_nx)
12151 error_at (loc,
12152 ("both %<long%> and %<_Float%d%s%> in "
12153 "declaration specifiers"),
12154 floatn_nx_types[specs->u.floatn_nx_idx].n,
12155 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12156 ? "x"
12157 : ""));
12158 else if (specs->typespec_word == cts_dfloat32)
12159 error_at (loc,
12160 ("both %<long%> and %<_Decimal32%> in "
12161 "declaration specifiers"));
12162 else if (specs->typespec_word == cts_dfloat64)
12163 error_at (loc,
12164 ("both %<long%> and %<_Decimal64%> in "
12165 "declaration specifiers"));
12166 else if (specs->typespec_word == cts_dfloat128)
12167 error_at (loc,
12168 ("both %<long%> and %<_Decimal128%> in "
12169 "declaration specifiers"));
12170 else if (specs->typespec_word == cts_dfloat64x)
12171 error_at (loc,
12172 ("both %<long%> and %<_Decimal64x%> in "
12173 "declaration specifiers"));
12174 else
12175 {
12176 specs->long_p = true;
12177 specs->locations[cdw_long] = loc;
12178 }
12179 break;
12180 case RID_SHORT:
12181 dupe = specs->short_p;
12182 if (specs->long_p)
12183 error_at (loc,
12184 ("both %<long%> and %<short%> in "
12185 "declaration specifiers"));
12186 else if (specs->typespec_word == cts_auto_type)
12187 error_at (loc,
12188 ("both %<short%> and %<__auto_type%> in "
12189 "declaration specifiers"));
12190 else if (specs->typespec_word == cts_void)
12191 error_at (loc,
12192 ("both %<short%> and %<void%> in "
12193 "declaration specifiers"));
12194 else if (specs->typespec_word == cts_int_n)
12195 error_at (loc,
12196 ("both %<short%> and %<__int%d%> in "
12197 "declaration specifiers"),
12198 int_n_data[specs->u.int_n_idx].bitsize);
12199 else if (specs->typespec_word == cts_bool)
12200 error_at (loc,
12201 ("both %<short%> and %<_Bool%> in "
12202 "declaration specifiers"));
12203 else if (specs->typespec_word == cts_bitint)
12204 error_at (loc,
12205 ("both %<short%> and %<_BitInt%> in "
12206 "declaration specifiers"));
12207 else if (specs->typespec_word == cts_char)
12208 error_at (loc,
12209 ("both %<short%> and %<char%> in "
12210 "declaration specifiers"));
12211 else if (specs->typespec_word == cts_float)
12212 error_at (loc,
12213 ("both %<short%> and %<float%> in "
12214 "declaration specifiers"));
12215 else if (specs->typespec_word == cts_double)
12216 error_at (loc,
12217 ("both %<short%> and %<double%> in "
12218 "declaration specifiers"));
12219 else if (specs->typespec_word == cts_floatn_nx)
12220 error_at (loc,
12221 ("both %<short%> and %<_Float%d%s%> in "
12222 "declaration specifiers"),
12223 floatn_nx_types[specs->u.floatn_nx_idx].n,
12224 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12225 ? "x"
12226 : ""));
12227 else if (specs->typespec_word == cts_dfloat32)
12228 error_at (loc,
12229 ("both %<short%> and %<_Decimal32%> in "
12230 "declaration specifiers"));
12231 else if (specs->typespec_word == cts_dfloat64)
12232 error_at (loc,
12233 ("both %<short%> and %<_Decimal64%> in "
12234 "declaration specifiers"));
12235 else if (specs->typespec_word == cts_dfloat128)
12236 error_at (loc,
12237 ("both %<short%> and %<_Decimal128%> in "
12238 "declaration specifiers"));
12239 else if (specs->typespec_word == cts_dfloat64x)
12240 error_at (loc,
12241 ("both %<short%> and %<_Decimal64x%> in "
12242 "declaration specifiers"));
12243 else
12244 {
12245 specs->short_p = true;
12246 specs->locations[cdw_short] = loc;
12247 }
12248 break;
12249 case RID_SIGNED:
12250 dupe = specs->signed_p;
12251 if (specs->unsigned_p)
12252 error_at (loc,
12253 ("both %<signed%> and %<unsigned%> in "
12254 "declaration specifiers"));
12255 else if (specs->typespec_word == cts_auto_type)
12256 error_at (loc,
12257 ("both %<signed%> and %<__auto_type%> in "
12258 "declaration specifiers"));
12259 else if (specs->typespec_word == cts_void)
12260 error_at (loc,
12261 ("both %<signed%> and %<void%> in "
12262 "declaration specifiers"));
12263 else if (specs->typespec_word == cts_bool)
12264 error_at (loc,
12265 ("both %<signed%> and %<_Bool%> in "
12266 "declaration specifiers"));
12267 else if (specs->typespec_word == cts_float)
12268 error_at (loc,
12269 ("both %<signed%> and %<float%> in "
12270 "declaration specifiers"));
12271 else if (specs->typespec_word == cts_double)
12272 error_at (loc,
12273 ("both %<signed%> and %<double%> in "
12274 "declaration specifiers"));
12275 else if (specs->typespec_word == cts_floatn_nx)
12276 error_at (loc,
12277 ("both %<signed%> and %<_Float%d%s%> in "
12278 "declaration specifiers"),
12279 floatn_nx_types[specs->u.floatn_nx_idx].n,
12280 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12281 ? "x"
12282 : ""));
12283 else if (specs->typespec_word == cts_dfloat32)
12284 error_at (loc,
12285 ("both %<signed%> and %<_Decimal32%> in "
12286 "declaration specifiers"));
12287 else if (specs->typespec_word == cts_dfloat64)
12288 error_at (loc,
12289 ("both %<signed%> and %<_Decimal64%> in "
12290 "declaration specifiers"));
12291 else if (specs->typespec_word == cts_dfloat128)
12292 error_at (loc,
12293 ("both %<signed%> and %<_Decimal128%> in "
12294 "declaration specifiers"));
12295 else if (specs->typespec_word == cts_dfloat64x)
12296 error_at (loc,
12297 ("both %<signed%> and %<_Decimal64x%> in "
12298 "declaration specifiers"));
12299 else
12300 {
12301 specs->signed_p = true;
12302 specs->locations[cdw_signed] = loc;
12303 }
12304 break;
12305 case RID_UNSIGNED:
12306 dupe = specs->unsigned_p;
12307 if (specs->signed_p)
12308 error_at (loc,
12309 ("both %<signed%> and %<unsigned%> in "
12310 "declaration specifiers"));
12311 else if (specs->typespec_word == cts_auto_type)
12312 error_at (loc,
12313 ("both %<unsigned%> and %<__auto_type%> in "
12314 "declaration specifiers"));
12315 else if (specs->typespec_word == cts_void)
12316 error_at (loc,
12317 ("both %<unsigned%> and %<void%> in "
12318 "declaration specifiers"));
12319 else if (specs->typespec_word == cts_bool)
12320 error_at (loc,
12321 ("both %<unsigned%> and %<_Bool%> in "
12322 "declaration specifiers"));
12323 else if (specs->typespec_word == cts_float)
12324 error_at (loc,
12325 ("both %<unsigned%> and %<float%> in "
12326 "declaration specifiers"));
12327 else if (specs->typespec_word == cts_double)
12328 error_at (loc,
12329 ("both %<unsigned%> and %<double%> in "
12330 "declaration specifiers"));
12331 else if (specs->typespec_word == cts_floatn_nx)
12332 error_at (loc,
12333 ("both %<unsigned%> and %<_Float%d%s%> in "
12334 "declaration specifiers"),
12335 floatn_nx_types[specs->u.floatn_nx_idx].n,
12336 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12337 ? "x"
12338 : ""));
12339 else if (specs->typespec_word == cts_dfloat32)
12340 error_at (loc,
12341 ("both %<unsigned%> and %<_Decimal32%> in "
12342 "declaration specifiers"));
12343 else if (specs->typespec_word == cts_dfloat64)
12344 error_at (loc,
12345 ("both %<unsigned%> and %<_Decimal64%> in "
12346 "declaration specifiers"));
12347 else if (specs->typespec_word == cts_dfloat128)
12348 error_at (loc,
12349 ("both %<unsigned%> and %<_Decimal128%> in "
12350 "declaration specifiers"));
12351 else if (specs->typespec_word == cts_dfloat64x)
12352 error_at (loc,
12353 ("both %<unsigned%> and %<_Decimal64x%> in "
12354 "declaration specifiers"));
12355 else
12356 {
12357 specs->unsigned_p = true;
12358 specs->locations[cdw_unsigned] = loc;
12359 }
12360 break;
12361 case RID_COMPLEX:
12362 dupe = specs->complex_p;
12363 if (!in_system_header_at (loc))
12364 pedwarn_c90 (loc, OPT_Wpedantic,
12365 "ISO C90 does not support complex types");
12366 if (specs->typespec_word == cts_auto_type)
12367 error_at (loc,
12368 ("both %<complex%> and %<__auto_type%> in "
12369 "declaration specifiers"));
12370 else if (specs->typespec_word == cts_void)
12371 error_at (loc,
12372 ("both %<complex%> and %<void%> in "
12373 "declaration specifiers"));
12374 else if (specs->typespec_word == cts_bool)
12375 error_at (loc,
12376 ("both %<complex%> and %<_Bool%> in "
12377 "declaration specifiers"));
12378 else if (specs->typespec_word == cts_bitint)
12379 error_at (loc,
12380 ("both %<complex%> and %<_BitInt%> in "
12381 "declaration specifiers"));
12382 else if (specs->typespec_word == cts_dfloat32)
12383 error_at (loc,
12384 ("both %<complex%> and %<_Decimal32%> in "
12385 "declaration specifiers"));
12386 else if (specs->typespec_word == cts_dfloat64)
12387 error_at (loc,
12388 ("both %<complex%> and %<_Decimal64%> in "
12389 "declaration specifiers"));
12390 else if (specs->typespec_word == cts_dfloat128)
12391 error_at (loc,
12392 ("both %<complex%> and %<_Decimal128%> in "
12393 "declaration specifiers"));
12394 else if (specs->typespec_word == cts_dfloat64x)
12395 error_at (loc,
12396 ("both %<complex%> and %<_Decimal64x%> in "
12397 "declaration specifiers"));
12398 else if (specs->typespec_word == cts_fract)
12399 error_at (loc,
12400 ("both %<complex%> and %<_Fract%> in "
12401 "declaration specifiers"));
12402 else if (specs->typespec_word == cts_accum)
12403 error_at (loc,
12404 ("both %<complex%> and %<_Accum%> in "
12405 "declaration specifiers"));
12406 else if (specs->saturating_p)
12407 error_at (loc,
12408 ("both %<complex%> and %<_Sat%> in "
12409 "declaration specifiers"));
12410 else
12411 {
12412 specs->complex_p = true;
12413 specs->locations[cdw_complex] = loc;
12414 }
12415 break;
12416 case RID_SAT:
12417 dupe = specs->saturating_p;
12418 pedwarn (loc, OPT_Wpedantic,
12419 "ISO C does not support saturating types");
12420 if (specs->typespec_word == cts_int_n)
12421 {
12422 error_at (loc,
12423 ("both %<_Sat%> and %<__int%d%> in "
12424 "declaration specifiers"),
12425 int_n_data[specs->u.int_n_idx].bitsize);
12426 }
12427 else if (specs->typespec_word == cts_auto_type)
12428 error_at (loc,
12429 ("both %<_Sat%> and %<__auto_type%> in "
12430 "declaration specifiers"));
12431 else if (specs->typespec_word == cts_void)
12432 error_at (loc,
12433 ("both %<_Sat%> and %<void%> in "
12434 "declaration specifiers"));
12435 else if (specs->typespec_word == cts_bool)
12436 error_at (loc,
12437 ("both %<_Sat%> and %<_Bool%> in "
12438 "declaration specifiers"));
12439 else if (specs->typespec_word == cts_bitint)
12440 error_at (loc,
12441 ("both %<_Sat%> and %<_BitInt%> in "
12442 "declaration specifiers"));
12443 else if (specs->typespec_word == cts_char)
12444 error_at (loc,
12445 ("both %<_Sat%> and %<char%> in "
12446 "declaration specifiers"));
12447 else if (specs->typespec_word == cts_int)
12448 error_at (loc,
12449 ("both %<_Sat%> and %<int%> in "
12450 "declaration specifiers"));
12451 else if (specs->typespec_word == cts_float)
12452 error_at (loc,
12453 ("both %<_Sat%> and %<float%> in "
12454 "declaration specifiers"));
12455 else if (specs->typespec_word == cts_double)
12456 error_at (loc,
12457 ("both %<_Sat%> and %<double%> in "
12458 "declaration specifiers"));
12459 else if (specs->typespec_word == cts_floatn_nx)
12460 error_at (loc,
12461 ("both %<_Sat%> and %<_Float%d%s%> in "
12462 "declaration specifiers"),
12463 floatn_nx_types[specs->u.floatn_nx_idx].n,
12464 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12465 ? "x"
12466 : ""));
12467 else if (specs->typespec_word == cts_dfloat32)
12468 error_at (loc,
12469 ("both %<_Sat%> and %<_Decimal32%> in "
12470 "declaration specifiers"));
12471 else if (specs->typespec_word == cts_dfloat64)
12472 error_at (loc,
12473 ("both %<_Sat%> and %<_Decimal64%> in "
12474 "declaration specifiers"));
12475 else if (specs->typespec_word == cts_dfloat128)
12476 error_at (loc,
12477 ("both %<_Sat%> and %<_Decimal128%> in "
12478 "declaration specifiers"));
12479 else if (specs->typespec_word == cts_dfloat64x)
12480 error_at (loc,
12481 ("both %<_Sat%> and %<_Decimal64x%> in "
12482 "declaration specifiers"));
12483 else if (specs->complex_p)
12484 error_at (loc,
12485 ("both %<_Sat%> and %<complex%> in "
12486 "declaration specifiers"));
12487 else
12488 {
12489 specs->saturating_p = true;
12490 specs->locations[cdw_saturating] = loc;
12491 }
12492 break;
12493 default:
12494 gcc_unreachable ();
12495 }
12496
12497 if (dupe)
12498 error_at (loc, "duplicate %qE", type);
12499
12500 return specs;
12501 }
12502 else
12503 {
12504 /* "void", "_Bool", "char", "int", "float", "double",
12505 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12506 "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12507 "__auto_type". */
12508 if (specs->typespec_word != cts_none)
12509 {
12510 if (i == RID_BOOL)
12511 {
12512 auto_diagnostic_group d;
12513 if (specs->storage_class == csc_typedef)
12514 error_at (loc,
12515 "%qs cannot be defined via %<typedef%>",
12516 IDENTIFIER_POINTER (type));
12517 else
12518 error_at (loc,
12519 "%qs cannot be used here",
12520 IDENTIFIER_POINTER (type));
12521 add_note_about_new_keyword (loc, type);
12522 }
12523 else
12524 error_at (loc,
12525 "two or more data types in declaration specifiers");
12526 return specs;
12527 }
12528 switch (i)
12529 {
12530 case RID_AUTO_TYPE:
12531 if (specs->long_p)
12532 error_at (loc,
12533 ("both %<long%> and %<__auto_type%> in "
12534 "declaration specifiers"));
12535 else if (specs->short_p)
12536 error_at (loc,
12537 ("both %<short%> and %<__auto_type%> in "
12538 "declaration specifiers"));
12539 else if (specs->signed_p)
12540 error_at (loc,
12541 ("both %<signed%> and %<__auto_type%> in "
12542 "declaration specifiers"));
12543 else if (specs->unsigned_p)
12544 error_at (loc,
12545 ("both %<unsigned%> and %<__auto_type%> in "
12546 "declaration specifiers"));
12547 else if (specs->complex_p)
12548 error_at (loc,
12549 ("both %<complex%> and %<__auto_type%> in "
12550 "declaration specifiers"));
12551 else if (specs->saturating_p)
12552 error_at (loc,
12553 ("both %<_Sat%> and %<__auto_type%> in "
12554 "declaration specifiers"));
12555 else
12556 {
12557 specs->typespec_word = cts_auto_type;
12558 specs->locations[cdw_typespec] = loc;
12559 }
12560 return specs;
12561 case RID_INT_N_0:
12562 case RID_INT_N_1:
12563 case RID_INT_N_2:
12564 case RID_INT_N_3:
12565 specs->u.int_n_idx = i - RID_INT_N_0;
12566 if (!in_system_header_at (input_location)
12567 /* If the INT_N type ends in "__", and so is of the format
12568 "__intN__", don't pedwarn. */
12569 && (strncmp (IDENTIFIER_POINTER (type)
12570 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12571 pedwarn (loc, OPT_Wpedantic,
12572 "ISO C does not support %<__int%d%> types",
12573 int_n_data[specs->u.int_n_idx].bitsize);
12574
12575 if (specs->long_p)
12576 error_at (loc,
12577 ("both %<__int%d%> and %<long%> in "
12578 "declaration specifiers"),
12579 int_n_data[specs->u.int_n_idx].bitsize);
12580 else if (specs->saturating_p)
12581 error_at (loc,
12582 ("both %<_Sat%> and %<__int%d%> in "
12583 "declaration specifiers"),
12584 int_n_data[specs->u.int_n_idx].bitsize);
12585 else if (specs->short_p)
12586 error_at (loc,
12587 ("both %<__int%d%> and %<short%> in "
12588 "declaration specifiers"),
12589 int_n_data[specs->u.int_n_idx].bitsize);
12590 else if (! int_n_enabled_p[specs->u.int_n_idx])
12591 {
12592 specs->typespec_word = cts_int_n;
12593 error_at (loc,
12594 "%<__int%d%> is not supported on this target",
12595 int_n_data[specs->u.int_n_idx].bitsize);
12596 }
12597 else
12598 {
12599 specs->typespec_word = cts_int_n;
12600 specs->locations[cdw_typespec] = loc;
12601 }
12602 return specs;
12603 case RID_VOID:
12604 if (specs->long_p)
12605 error_at (loc,
12606 ("both %<long%> and %<void%> in "
12607 "declaration specifiers"));
12608 else if (specs->short_p)
12609 error_at (loc,
12610 ("both %<short%> and %<void%> in "
12611 "declaration specifiers"));
12612 else if (specs->signed_p)
12613 error_at (loc,
12614 ("both %<signed%> and %<void%> in "
12615 "declaration specifiers"));
12616 else if (specs->unsigned_p)
12617 error_at (loc,
12618 ("both %<unsigned%> and %<void%> in "
12619 "declaration specifiers"));
12620 else if (specs->complex_p)
12621 error_at (loc,
12622 ("both %<complex%> and %<void%> in "
12623 "declaration specifiers"));
12624 else if (specs->saturating_p)
12625 error_at (loc,
12626 ("both %<_Sat%> and %<void%> in "
12627 "declaration specifiers"));
12628 else
12629 {
12630 specs->typespec_word = cts_void;
12631 specs->locations[cdw_typespec] = loc;
12632 }
12633 return specs;
12634 case RID_BOOL:
12635 if (!in_system_header_at (loc))
12636 pedwarn_c90 (loc, OPT_Wpedantic,
12637 "ISO C90 does not support boolean types");
12638 if (specs->long_p)
12639 error_at (loc,
12640 ("both %<long%> and %<_Bool%> in "
12641 "declaration specifiers"));
12642 else if (specs->short_p)
12643 error_at (loc,
12644 ("both %<short%> and %<_Bool%> in "
12645 "declaration specifiers"));
12646 else if (specs->signed_p)
12647 error_at (loc,
12648 ("both %<signed%> and %<_Bool%> in "
12649 "declaration specifiers"));
12650 else if (specs->unsigned_p)
12651 error_at (loc,
12652 ("both %<unsigned%> and %<_Bool%> in "
12653 "declaration specifiers"));
12654 else if (specs->complex_p)
12655 error_at (loc,
12656 ("both %<complex%> and %<_Bool%> in "
12657 "declaration specifiers"));
12658 else if (specs->saturating_p)
12659 error_at (loc,
12660 ("both %<_Sat%> and %<_Bool%> in "
12661 "declaration specifiers"));
12662 else
12663 {
12664 specs->typespec_word = cts_bool;
12665 specs->locations[cdw_typespec] = loc;
12666 }
12667 return specs;
12668 case RID_CHAR:
12669 if (specs->long_p)
12670 error_at (loc,
12671 ("both %<long%> and %<char%> in "
12672 "declaration specifiers"));
12673 else if (specs->short_p)
12674 error_at (loc,
12675 ("both %<short%> and %<char%> in "
12676 "declaration specifiers"));
12677 else if (specs->saturating_p)
12678 error_at (loc,
12679 ("both %<_Sat%> and %<char%> in "
12680 "declaration specifiers"));
12681 else
12682 {
12683 specs->typespec_word = cts_char;
12684 specs->locations[cdw_typespec] = loc;
12685 }
12686 return specs;
12687 case RID_INT:
12688 if (specs->saturating_p)
12689 error_at (loc,
12690 ("both %<_Sat%> and %<int%> in "
12691 "declaration specifiers"));
12692 else
12693 {
12694 specs->typespec_word = cts_int;
12695 specs->locations[cdw_typespec] = loc;
12696 }
12697 return specs;
12698 case RID_FLOAT:
12699 if (specs->long_p)
12700 error_at (loc,
12701 ("both %<long%> and %<float%> in "
12702 "declaration specifiers"));
12703 else if (specs->short_p)
12704 error_at (loc,
12705 ("both %<short%> and %<float%> in "
12706 "declaration specifiers"));
12707 else if (specs->signed_p)
12708 error_at (loc,
12709 ("both %<signed%> and %<float%> in "
12710 "declaration specifiers"));
12711 else if (specs->unsigned_p)
12712 error_at (loc,
12713 ("both %<unsigned%> and %<float%> in "
12714 "declaration specifiers"));
12715 else if (specs->saturating_p)
12716 error_at (loc,
12717 ("both %<_Sat%> and %<float%> in "
12718 "declaration specifiers"));
12719 else
12720 {
12721 specs->typespec_word = cts_float;
12722 specs->locations[cdw_typespec] = loc;
12723 }
12724 return specs;
12725 case RID_DOUBLE:
12726 if (specs->long_long_p)
12727 error_at (loc,
12728 ("both %<long long%> and %<double%> in "
12729 "declaration specifiers"));
12730 else if (specs->short_p)
12731 error_at (loc,
12732 ("both %<short%> and %<double%> in "
12733 "declaration specifiers"));
12734 else if (specs->signed_p)
12735 error_at (loc,
12736 ("both %<signed%> and %<double%> in "
12737 "declaration specifiers"));
12738 else if (specs->unsigned_p)
12739 error_at (loc,
12740 ("both %<unsigned%> and %<double%> in "
12741 "declaration specifiers"));
12742 else if (specs->saturating_p)
12743 error_at (loc,
12744 ("both %<_Sat%> and %<double%> in "
12745 "declaration specifiers"));
12746 else
12747 {
12748 specs->typespec_word = cts_double;
12749 specs->locations[cdw_typespec] = loc;
12750 }
12751 return specs;
12752 CASE_RID_FLOATN_NX:
12753 specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12754 if (!in_system_header_at (input_location))
12755 pedwarn_c11 (loc, OPT_Wpedantic,
12756 "ISO C does not support the %<_Float%d%s%> type"
12757 " before C23",
12758 floatn_nx_types[specs->u.floatn_nx_idx].n,
12759 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12760 ? "x"
12761 : ""));
12762
12763 if (specs->long_p)
12764 error_at (loc,
12765 ("both %<long%> and %<_Float%d%s%> in "
12766 "declaration specifiers"),
12767 floatn_nx_types[specs->u.floatn_nx_idx].n,
12768 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12769 ? "x"
12770 : ""));
12771 else if (specs->short_p)
12772 error_at (loc,
12773 ("both %<short%> and %<_Float%d%s%> in "
12774 "declaration specifiers"),
12775 floatn_nx_types[specs->u.floatn_nx_idx].n,
12776 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12777 ? "x"
12778 : ""));
12779 else if (specs->signed_p)
12780 error_at (loc,
12781 ("both %<signed%> and %<_Float%d%s%> in "
12782 "declaration specifiers"),
12783 floatn_nx_types[specs->u.floatn_nx_idx].n,
12784 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12785 ? "x"
12786 : ""));
12787 else if (specs->unsigned_p)
12788 error_at (loc,
12789 ("both %<unsigned%> and %<_Float%d%s%> in "
12790 "declaration specifiers"),
12791 floatn_nx_types[specs->u.floatn_nx_idx].n,
12792 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12793 ? "x"
12794 : ""));
12795 else if (specs->saturating_p)
12796 error_at (loc,
12797 ("both %<_Sat%> and %<_Float%d%s%> in "
12798 "declaration specifiers"),
12799 floatn_nx_types[specs->u.floatn_nx_idx].n,
12800 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12801 ? "x"
12802 : ""));
12803 else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12804 {
12805 specs->typespec_word = cts_floatn_nx;
12806 error_at (loc,
12807 "%<_Float%d%s%> is not supported on this target",
12808 floatn_nx_types[specs->u.floatn_nx_idx].n,
12809 (floatn_nx_types[specs->u.floatn_nx_idx].extended
12810 ? "x"
12811 : ""));
12812 }
12813 else
12814 {
12815 specs->typespec_word = cts_floatn_nx;
12816 specs->locations[cdw_typespec] = loc;
12817 }
12818 return specs;
12819 case RID_DFLOAT32:
12820 case RID_DFLOAT64:
12821 case RID_DFLOAT128:
12822 case RID_DFLOAT64X:
12823 {
12824 const char *str;
12825 if (i == RID_DFLOAT32)
12826 str = "_Decimal32";
12827 else if (i == RID_DFLOAT64)
12828 str = "_Decimal64";
12829 else if (i == RID_DFLOAT128)
12830 str = "_Decimal128";
12831 else
12832 str = "_Decimal64x";
12833 if (specs->long_long_p)
12834 error_at (loc,
12835 ("both %<long long%> and %qs in "
12836 "declaration specifiers"),
12837 str);
12838 if (specs->long_p)
12839 error_at (loc,
12840 ("both %<long%> and %qs in "
12841 "declaration specifiers"),
12842 str);
12843 else if (specs->short_p)
12844 error_at (loc,
12845 ("both %<short%> and %qs in "
12846 "declaration specifiers"),
12847 str);
12848 else if (specs->signed_p)
12849 error_at (loc,
12850 ("both %<signed%> and %qs in "
12851 "declaration specifiers"),
12852 str);
12853 else if (specs->unsigned_p)
12854 error_at (loc,
12855 ("both %<unsigned%> and %qs in "
12856 "declaration specifiers"),
12857 str);
12858 else if (specs->complex_p)
12859 error_at (loc,
12860 ("both %<complex%> and %qs in "
12861 "declaration specifiers"),
12862 str);
12863 else if (specs->saturating_p)
12864 error_at (loc,
12865 ("both %<_Sat%> and %qs in "
12866 "declaration specifiers"),
12867 str);
12868 else if (i == RID_DFLOAT32)
12869 specs->typespec_word = cts_dfloat32;
12870 else if (i == RID_DFLOAT64)
12871 specs->typespec_word = cts_dfloat64;
12872 else if (i == RID_DFLOAT128)
12873 specs->typespec_word = cts_dfloat128;
12874 else
12875 specs->typespec_word = cts_dfloat64x;
12876 specs->locations[cdw_typespec] = loc;
12877 }
12878 if (!targetm.decimal_float_supported_p ())
12879 error_at (loc,
12880 ("decimal floating-point not supported "
12881 "for this target"));
12882 pedwarn_c11 (loc, OPT_Wpedantic,
12883 "ISO C does not support decimal floating-point "
12884 "before C23");
12885 return specs;
12886 case RID_FRACT:
12887 case RID_ACCUM:
12888 {
12889 const char *str;
12890 if (i == RID_FRACT)
12891 str = "_Fract";
12892 else
12893 str = "_Accum";
12894 if (specs->complex_p)
12895 error_at (loc,
12896 ("both %<complex%> and %qs in "
12897 "declaration specifiers"),
12898 str);
12899 else if (i == RID_FRACT)
12900 specs->typespec_word = cts_fract;
12901 else
12902 specs->typespec_word = cts_accum;
12903 specs->locations[cdw_typespec] = loc;
12904 }
12905 if (!targetm.fixed_point_supported_p ())
12906 error_at (loc,
12907 "fixed-point types not supported for this target");
12908 pedwarn (loc, OPT_Wpedantic,
12909 "ISO C does not support fixed-point types");
12910 return specs;
12911 case RID_BITINT:
12912 if (specs->long_p)
12913 error_at (loc,
12914 ("both %<long%> and %<_BitInt%> in "
12915 "declaration specifiers"));
12916 else if (specs->short_p)
12917 error_at (loc,
12918 ("both %<short%> and %<_BitInt%> in "
12919 "declaration specifiers"));
12920 else if (specs->complex_p)
12921 error_at (loc,
12922 ("both %<complex%> and %<_BitInt%> in "
12923 "declaration specifiers"));
12924 else if (specs->saturating_p)
12925 error_at (loc,
12926 ("both %<_Sat%> and %<_BitInt%> in "
12927 "declaration specifiers"));
12928 else
12929 {
12930 specs->typespec_word = cts_bitint;
12931 specs->locations[cdw_typespec] = loc;
12932 specs->u.bitint_prec = -1;
12933 if (error_operand_p (spec.expr))
12934 return specs;
12935 if (TREE_CODE (spec.expr) != INTEGER_CST
12936 || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12937 {
12938 error_at (loc, "%<_BitInt%> argument is not an integer "
12939 "constant expression");
12940 return specs;
12941 }
12942 if (tree_int_cst_sgn (spec.expr) <= 0)
12943 {
12944 error_at (loc, "%<_BitInt%> argument %qE is not a "
12945 "positive integer constant expression",
12946 spec.expr);
12947 return specs;
12948 }
12949 if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12950 {
12951 error_at (loc, "%<_BitInt%> argument %qE is larger than "
12952 "%<BITINT_MAXWIDTH%> %qd",
12953 spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12954 return specs;
12955 }
12956 specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12957 struct bitint_info info;
12958 if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12959 &info))
12960 {
12961 sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12962 "this target", specs->u.bitint_prec);
12963 specs->u.bitint_prec = -1;
12964 return specs;
12965 }
12966 }
12967 return specs;
12968 default:
12969 /* ObjC reserved word "id", handled below. */
12970 break;
12971 }
12972 }
12973 }
12974
12975 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12976 form of ObjC type, cases such as "int" and "long" being handled
12977 above), a TYPE (struct, union, enum and typeof specifiers) or an
12978 ERROR_MARK. In none of these cases may there have previously
12979 been any type specifiers. */
12980 if (specs->type || specs->typespec_word != cts_none
12981 || specs->long_p || specs->short_p || specs->signed_p
12982 || specs->unsigned_p || specs->complex_p)
12983 error_at (loc, "two or more data types in declaration specifiers");
12984 else if (TREE_CODE (type) == TYPE_DECL)
12985 {
12986 specs->type = TREE_TYPE (type);
12987 if (TREE_TYPE (type) != error_mark_node)
12988 {
12989 specs->decl_attr = DECL_ATTRIBUTES (type);
12990 specs->typedef_p = true;
12991 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12992 specs->locations[cdw_typedef] = loc;
12993
12994 /* If this typedef name is defined in a struct, then a C++
12995 lookup would return a different value. */
12996 if (warn_cxx_compat
12997 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12998 warning_at (loc, OPT_Wc___compat,
12999 "C++ lookup of %qD would return a field, not a type",
13000 type);
13001
13002 /* If we are parsing a struct, record that a struct field
13003 used a typedef. */
13004 if (warn_cxx_compat && struct_parse_info != NULL)
13005 struct_parse_info->typedefs_seen.safe_push (type);
13006 }
13007 }
13008 else if (TREE_CODE (type) == IDENTIFIER_NODE)
13009 {
13010 tree t = lookup_name (type);
13011 if (!t || TREE_CODE (t) != TYPE_DECL)
13012 error_at (loc, "%qE fails to be a typedef or built in type", type);
13013 else if (TREE_TYPE (t) == error_mark_node)
13014 ;
13015 else
13016 {
13017 specs->type = TREE_TYPE (t);
13018 specs->locations[cdw_typespec] = loc;
13019 }
13020 }
13021 else
13022 {
13023 if (TREE_CODE (type) != ERROR_MARK)
13024 {
13025 if (spec.kind == ctsk_typeof)
13026 {
13027 specs->typedef_p = true;
13028 specs->locations[cdw_typedef] = loc;
13029 }
13030 if (spec.expr)
13031 {
13032 if (specs->expr)
13033 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
13034 specs->expr, spec.expr);
13035 else
13036 specs->expr = spec.expr;
13037 specs->expr_const_operands &= spec.expr_const_operands;
13038 }
13039 }
13040 specs->type = type;
13041 if (spec.has_enum_type_specifier
13042 && spec.kind != ctsk_tagdef)
13043 specs->enum_type_specifier_ref_p = true;
13044 }
13045
13046 return specs;
13047 }
13048
13049 /* Add the storage class specifier or function specifier SCSPEC to the
13050 declaration specifiers SPECS, returning SPECS. */
13051
13052 struct c_declspecs *
13053 declspecs_add_scspec (location_t loc,
13054 struct c_declspecs *specs,
13055 tree scspec)
13056 {
13057 enum rid i;
13058 enum c_storage_class n = csc_none;
13059 bool dupe = false;
13060 specs->declspecs_seen_p = true;
13061 specs->non_std_attrs_seen_p = true;
13062 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13063 && C_IS_RESERVED_WORD (scspec));
13064 i = C_RID_CODE (scspec);
13065 if (specs->non_sc_seen_p)
13066 warning (OPT_Wold_style_declaration,
13067 "%qE is not at beginning of declaration", scspec);
13068 switch (i)
13069 {
13070 case RID_INLINE:
13071 /* C99 permits duplicate inline. Although of doubtful utility,
13072 it seems simplest to permit it in gnu89 mode as well, as
13073 there is also little utility in maintaining this as a
13074 difference between gnu89 and C99 inline. */
13075 dupe = false;
13076 specs->inline_p = true;
13077 specs->locations[cdw_inline] = loc;
13078 break;
13079 case RID_NORETURN:
13080 /* Duplicate _Noreturn is permitted. */
13081 dupe = false;
13082 specs->noreturn_p = true;
13083 specs->locations[cdw_noreturn] = loc;
13084 break;
13085 case RID_THREAD:
13086 dupe = specs->thread_p;
13087 if (specs->storage_class == csc_auto)
13088 error ("%qE used with %<auto%>", scspec);
13089 else if (specs->storage_class == csc_register)
13090 error ("%qE used with %<register%>", scspec);
13091 else if (specs->storage_class == csc_typedef)
13092 error ("%qE used with %<typedef%>", scspec);
13093 else if (specs->constexpr_p)
13094 error ("%qE used with %<constexpr%>", scspec);
13095 else
13096 {
13097 specs->thread_p = true;
13098 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13099 "__thread") == 0);
13100 /* A diagnostic is not required for the use of this
13101 identifier in the implementation namespace; only diagnose
13102 it for the C11 spelling because of existing code using
13103 the other spelling. */
13104 if (!specs->thread_gnu_p)
13105 {
13106 if (flag_isoc99)
13107 pedwarn_c99 (loc, OPT_Wpedantic,
13108 "ISO C99 does not support %qE", scspec);
13109 else
13110 pedwarn_c99 (loc, OPT_Wpedantic,
13111 "ISO C90 does not support %qE", scspec);
13112 }
13113 specs->locations[cdw_thread] = loc;
13114 }
13115 break;
13116 case RID_AUTO:
13117 if (flag_isoc23
13118 && specs->typespec_kind == ctsk_none
13119 && specs->storage_class != csc_typedef)
13120 {
13121 /* "auto" potentially used for type deduction. */
13122 if (specs->c23_auto_p)
13123 error ("duplicate %qE", scspec);
13124 specs->c23_auto_p = true;
13125 return specs;
13126 }
13127 n = csc_auto;
13128 /* auto may only be used with another storage class specifier,
13129 such as constexpr, if the type is inferred. */
13130 if (specs->constexpr_p)
13131 error ("%qE used with %<constexpr%>", scspec);
13132 break;
13133 case RID_EXTERN:
13134 n = csc_extern;
13135 /* Diagnose "__thread extern". */
13136 if (specs->thread_p && specs->thread_gnu_p)
13137 error ("%<__thread%> before %<extern%>");
13138 break;
13139 case RID_REGISTER:
13140 n = csc_register;
13141 break;
13142 case RID_STATIC:
13143 n = csc_static;
13144 /* Diagnose "__thread static". */
13145 if (specs->thread_p && specs->thread_gnu_p)
13146 error ("%<__thread%> before %<static%>");
13147 break;
13148 case RID_TYPEDEF:
13149 n = csc_typedef;
13150 if (specs->c23_auto_p)
13151 {
13152 error ("%<typedef%> used with %<auto%>");
13153 specs->c23_auto_p = false;
13154 }
13155 break;
13156 case RID_CONSTEXPR:
13157 dupe = specs->constexpr_p;
13158 if (specs->storage_class == csc_extern)
13159 error ("%qE used with %<extern%>", scspec);
13160 else if (specs->storage_class == csc_typedef)
13161 error ("%qE used with %<typedef%>", scspec);
13162 else if (specs->storage_class == csc_auto)
13163 /* auto may only be used with another storage class specifier,
13164 such as constexpr, if the type is inferred. */
13165 error ("%qE used with %<auto%>", scspec);
13166 else if (specs->thread_p)
13167 error ("%qE used with %qs", scspec,
13168 specs->thread_gnu_p ? "__thread" : "_Thread_local");
13169 else
13170 specs->constexpr_p = true;
13171 break;
13172 default:
13173 gcc_unreachable ();
13174 }
13175 if (n != csc_none && n == specs->storage_class)
13176 dupe = true;
13177 if (dupe)
13178 {
13179 if (i == RID_THREAD)
13180 error ("duplicate %<_Thread_local%> or %<__thread%>");
13181 else
13182 error ("duplicate %qE", scspec);
13183 }
13184 if (n != csc_none)
13185 {
13186 if (specs->storage_class != csc_none && n != specs->storage_class)
13187 {
13188 error ("multiple storage classes in declaration specifiers");
13189 }
13190 else
13191 {
13192 specs->storage_class = n;
13193 specs->locations[cdw_storage_class] = loc;
13194 if (n != csc_extern && n != csc_static && specs->thread_p)
13195 {
13196 error ("%qs used with %qE",
13197 specs->thread_gnu_p ? "__thread" : "_Thread_local",
13198 scspec);
13199 specs->thread_p = false;
13200 }
13201 if (n != csc_auto && n != csc_register && n != csc_static
13202 && specs->constexpr_p)
13203 {
13204 error ("%<constexpr%> used with %qE", scspec);
13205 specs->constexpr_p = false;
13206 }
13207 }
13208 }
13209 return specs;
13210 }
13211
13212 /* Add the attributes ATTRS to the declaration specifiers SPECS,
13213 returning SPECS. */
13214
13215 struct c_declspecs *
13216 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13217 {
13218 specs->attrs = chainon (attrs, specs->attrs);
13219 specs->locations[cdw_attributes] = loc;
13220 specs->declspecs_seen_p = true;
13221 /* In the case of standard attributes at the start of the
13222 declaration, the caller will reset this. */
13223 specs->non_std_attrs_seen_p = true;
13224 return specs;
13225 }
13226
13227 /* Add an _Alignas specifier (expression ALIGN, or type whose
13228 alignment is ALIGN) to the declaration specifiers SPECS, returning
13229 SPECS. */
13230 struct c_declspecs *
13231 declspecs_add_alignas (location_t loc,
13232 struct c_declspecs *specs, tree align)
13233 {
13234 specs->alignas_p = true;
13235 specs->locations[cdw_alignas] = loc;
13236 if (align == error_mark_node)
13237 return specs;
13238
13239 /* Only accept the alignment if it's valid and greater than
13240 the current one. Zero is invalid but by C11 required to
13241 be silently ignored. */
13242 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13243 if (align_log > specs->align_log)
13244 specs->align_log = align_log;
13245 return specs;
13246 }
13247
13248 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13249 specifiers with any other type specifier to determine the resulting
13250 type. This is where ISO C checks on complex types are made, since
13251 "_Complex long" is a prefix of the valid ISO C type "_Complex long
13252 double". Also apply postfix standard attributes to modify the type. */
13253
13254 struct c_declspecs *
13255 finish_declspecs (struct c_declspecs *specs)
13256 {
13257 /* If a type was specified as a whole, we have no modifiers and are
13258 done. */
13259 if (specs->type != NULL_TREE)
13260 {
13261 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13262 && !specs->signed_p && !specs->unsigned_p
13263 && !specs->complex_p && !specs->c23_auto_p);
13264
13265 /* Set a dummy type. */
13266 if (TREE_CODE (specs->type) == ERROR_MARK)
13267 specs->type = integer_type_node;
13268 goto handle_postfix_attrs;
13269 }
13270
13271 /* If none of "void", "_Bool", "char", "int", "float" or "double"
13272 has been specified, treat it as "int" unless "_Complex" is
13273 present and there are no other specifiers. If we just have
13274 "_Complex", it is equivalent to "_Complex double", but e.g.
13275 "_Complex short" is equivalent to "_Complex short int". */
13276 if (specs->typespec_word == cts_none)
13277 {
13278 if (specs->saturating_p)
13279 {
13280 error_at (specs->locations[cdw_saturating],
13281 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13282 if (!targetm.fixed_point_supported_p ())
13283 error_at (specs->locations[cdw_saturating],
13284 "fixed-point types not supported for this target");
13285 specs->typespec_word = cts_fract;
13286 }
13287 else if (specs->long_p || specs->short_p
13288 || specs->signed_p || specs->unsigned_p)
13289 {
13290 specs->typespec_word = cts_int;
13291 }
13292 else if (specs->complex_p)
13293 {
13294 specs->typespec_word = cts_double;
13295 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13296 "ISO C does not support plain %<complex%> meaning "
13297 "%<double complex%>");
13298 }
13299 else if (specs->c23_auto_p)
13300 {
13301 /* Type to be filled in later, including applying postfix
13302 attributes. This warning only actually appears for
13303 -Wc11-c23-compat in C23 mode; in older modes, there may
13304 be a warning or pedwarn for implicit "int" instead, or
13305 other errors for use of auto at file scope. */
13306 pedwarn_c11 (input_location, OPT_Wpedantic,
13307 "ISO C does not support %<auto%> type deduction "
13308 "before C23");
13309 return specs;
13310 }
13311 else
13312 {
13313 specs->typespec_word = cts_int;
13314 specs->default_int_p = true;
13315 /* We don't diagnose this here because grokdeclarator will
13316 give more specific diagnostics according to whether it is
13317 a function definition. */
13318 }
13319 }
13320
13321 /* If "signed" was specified, record this to distinguish "int" and
13322 "signed int" in the case of a bit-field with
13323 -funsigned-bitfields. */
13324 specs->explicit_signed_p = specs->signed_p;
13325
13326 /* Now compute the actual type. */
13327 gcc_assert (!specs->c23_auto_p);
13328 switch (specs->typespec_word)
13329 {
13330 case cts_auto_type:
13331 gcc_assert (!specs->long_p && !specs->short_p
13332 && !specs->signed_p && !specs->unsigned_p
13333 && !specs->complex_p);
13334 /* Type to be filled in later. */
13335 if (specs->postfix_attrs)
13336 error ("%<__auto_type%> followed by %<[[]]%> attributes");
13337 break;
13338 case cts_void:
13339 gcc_assert (!specs->long_p && !specs->short_p
13340 && !specs->signed_p && !specs->unsigned_p
13341 && !specs->complex_p);
13342 specs->type = void_type_node;
13343 break;
13344 case cts_bool:
13345 gcc_assert (!specs->long_p && !specs->short_p
13346 && !specs->signed_p && !specs->unsigned_p
13347 && !specs->complex_p);
13348 specs->type = boolean_type_node;
13349 break;
13350 case cts_char:
13351 gcc_assert (!specs->long_p && !specs->short_p);
13352 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13353 if (specs->signed_p)
13354 specs->type = signed_char_type_node;
13355 else if (specs->unsigned_p)
13356 specs->type = unsigned_char_type_node;
13357 else
13358 specs->type = char_type_node;
13359 if (specs->complex_p)
13360 {
13361 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13362 "ISO C does not support complex integer types");
13363 specs->type = build_complex_type (specs->type);
13364 }
13365 break;
13366 case cts_int_n:
13367 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13368 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13369 if (! int_n_enabled_p[specs->u.int_n_idx])
13370 specs->type = integer_type_node;
13371 else
13372 specs->type = (specs->unsigned_p
13373 ? int_n_trees[specs->u.int_n_idx].unsigned_type
13374 : int_n_trees[specs->u.int_n_idx].signed_type);
13375 if (specs->complex_p)
13376 {
13377 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13378 "ISO C does not support complex integer types");
13379 specs->type = build_complex_type (specs->type);
13380 }
13381 break;
13382 case cts_int:
13383 gcc_assert (!(specs->long_p && specs->short_p));
13384 gcc_assert (!(specs->signed_p && specs->unsigned_p));
13385 if (specs->long_long_p)
13386 specs->type = (specs->unsigned_p
13387 ? long_long_unsigned_type_node
13388 : long_long_integer_type_node);
13389 else if (specs->long_p)
13390 specs->type = (specs->unsigned_p
13391 ? long_unsigned_type_node
13392 : long_integer_type_node);
13393 else if (specs->short_p)
13394 specs->type = (specs->unsigned_p
13395 ? short_unsigned_type_node
13396 : short_integer_type_node);
13397 else
13398 specs->type = (specs->unsigned_p
13399 ? unsigned_type_node
13400 : integer_type_node);
13401 if (specs->complex_p)
13402 {
13403 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13404 "ISO C does not support complex integer types");
13405 specs->type = build_complex_type (specs->type);
13406 }
13407 break;
13408 case cts_float:
13409 gcc_assert (!specs->long_p && !specs->short_p
13410 && !specs->signed_p && !specs->unsigned_p);
13411 specs->type = (specs->complex_p
13412 ? complex_float_type_node
13413 : float_type_node);
13414 break;
13415 case cts_double:
13416 gcc_assert (!specs->long_long_p && !specs->short_p
13417 && !specs->signed_p && !specs->unsigned_p);
13418 if (specs->long_p)
13419 {
13420 specs->type = (specs->complex_p
13421 ? complex_long_double_type_node
13422 : long_double_type_node);
13423 }
13424 else
13425 {
13426 specs->type = (specs->complex_p
13427 ? complex_double_type_node
13428 : double_type_node);
13429 }
13430 break;
13431 case cts_floatn_nx:
13432 gcc_assert (!specs->long_p && !specs->short_p
13433 && !specs->signed_p && !specs->unsigned_p);
13434 if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13435 specs->type = integer_type_node;
13436 else if (specs->complex_p)
13437 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13438 else
13439 specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13440 break;
13441 case cts_dfloat32:
13442 case cts_dfloat64:
13443 case cts_dfloat128:
13444 case cts_dfloat64x:
13445 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13446 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13447 if (!targetm.decimal_float_supported_p ())
13448 specs->type = integer_type_node;
13449 else if (specs->typespec_word == cts_dfloat32)
13450 specs->type = dfloat32_type_node;
13451 else if (specs->typespec_word == cts_dfloat64)
13452 specs->type = dfloat64_type_node;
13453 else if (specs->typespec_word == cts_dfloat128)
13454 specs->type = dfloat128_type_node;
13455 else
13456 specs->type = dfloat64x_type_node;
13457 break;
13458 case cts_fract:
13459 gcc_assert (!specs->complex_p);
13460 if (!targetm.fixed_point_supported_p ())
13461 specs->type = integer_type_node;
13462 else if (specs->saturating_p)
13463 {
13464 if (specs->long_long_p)
13465 specs->type = specs->unsigned_p
13466 ? sat_unsigned_long_long_fract_type_node
13467 : sat_long_long_fract_type_node;
13468 else if (specs->long_p)
13469 specs->type = specs->unsigned_p
13470 ? sat_unsigned_long_fract_type_node
13471 : sat_long_fract_type_node;
13472 else if (specs->short_p)
13473 specs->type = specs->unsigned_p
13474 ? sat_unsigned_short_fract_type_node
13475 : sat_short_fract_type_node;
13476 else
13477 specs->type = specs->unsigned_p
13478 ? sat_unsigned_fract_type_node
13479 : sat_fract_type_node;
13480 }
13481 else
13482 {
13483 if (specs->long_long_p)
13484 specs->type = specs->unsigned_p
13485 ? unsigned_long_long_fract_type_node
13486 : long_long_fract_type_node;
13487 else if (specs->long_p)
13488 specs->type = specs->unsigned_p
13489 ? unsigned_long_fract_type_node
13490 : long_fract_type_node;
13491 else if (specs->short_p)
13492 specs->type = specs->unsigned_p
13493 ? unsigned_short_fract_type_node
13494 : short_fract_type_node;
13495 else
13496 specs->type = specs->unsigned_p
13497 ? unsigned_fract_type_node
13498 : fract_type_node;
13499 }
13500 break;
13501 case cts_accum:
13502 gcc_assert (!specs->complex_p);
13503 if (!targetm.fixed_point_supported_p ())
13504 specs->type = integer_type_node;
13505 else if (specs->saturating_p)
13506 {
13507 if (specs->long_long_p)
13508 specs->type = specs->unsigned_p
13509 ? sat_unsigned_long_long_accum_type_node
13510 : sat_long_long_accum_type_node;
13511 else if (specs->long_p)
13512 specs->type = specs->unsigned_p
13513 ? sat_unsigned_long_accum_type_node
13514 : sat_long_accum_type_node;
13515 else if (specs->short_p)
13516 specs->type = specs->unsigned_p
13517 ? sat_unsigned_short_accum_type_node
13518 : sat_short_accum_type_node;
13519 else
13520 specs->type = specs->unsigned_p
13521 ? sat_unsigned_accum_type_node
13522 : sat_accum_type_node;
13523 }
13524 else
13525 {
13526 if (specs->long_long_p)
13527 specs->type = specs->unsigned_p
13528 ? unsigned_long_long_accum_type_node
13529 : long_long_accum_type_node;
13530 else if (specs->long_p)
13531 specs->type = specs->unsigned_p
13532 ? unsigned_long_accum_type_node
13533 : long_accum_type_node;
13534 else if (specs->short_p)
13535 specs->type = specs->unsigned_p
13536 ? unsigned_short_accum_type_node
13537 : short_accum_type_node;
13538 else
13539 specs->type = specs->unsigned_p
13540 ? unsigned_accum_type_node
13541 : accum_type_node;
13542 }
13543 break;
13544 case cts_bitint:
13545 gcc_assert (!specs->long_p && !specs->short_p
13546 && !specs->complex_p);
13547 if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13548 {
13549 error_at (specs->locations[cdw_typespec],
13550 "%<signed _BitInt%> argument must be at least 2");
13551 specs->type = integer_type_node;
13552 break;
13553 }
13554 if (specs->u.bitint_prec == -1)
13555 specs->type = integer_type_node;
13556 else
13557 {
13558 pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13559 "ISO C does not support %<%s_BitInt(%d)%> before C23",
13560 specs->unsigned_p ? "unsigned "
13561 : specs->signed_p ? "signed " : "",
13562 specs->u.bitint_prec);
13563 specs->type = build_bitint_type (specs->u.bitint_prec,
13564 specs->unsigned_p);
13565 }
13566 break;
13567 default:
13568 gcc_unreachable ();
13569 }
13570 handle_postfix_attrs:
13571 if (specs->type != NULL)
13572 {
13573 specs->postfix_attrs
13574 = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13575 decl_attributes (&specs->type, specs->postfix_attrs, 0);
13576 specs->postfix_attrs = NULL_TREE;
13577 }
13578
13579 return specs;
13580 }
13581
13582 /* Perform final processing on one file scope's declarations (or the
13583 external scope's declarations), GLOBALS. */
13584
13585 static void
13586 c_write_global_declarations_1 (tree globals)
13587 {
13588 tree decl;
13589 bool reconsider;
13590
13591 /* Process the decls in the order they were written. */
13592 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13593 {
13594 /* Check for used but undefined static functions using the C
13595 standard's definition of "used", and set TREE_NO_WARNING so
13596 that check_global_declaration doesn't repeat the check. */
13597 if (TREE_CODE (decl) == FUNCTION_DECL
13598 && DECL_INITIAL (decl) == NULL_TREE
13599 && DECL_EXTERNAL (decl)
13600 && !TREE_PUBLIC (decl))
13601 {
13602 if (C_DECL_USED (decl))
13603 {
13604 /* TODO: Add OPT_Wundefined-inline. */
13605 if (pedwarn (input_location, 0, "%q+F used but never defined",
13606 decl))
13607 suppress_warning (decl /* OPT_Wundefined-inline. */);
13608 }
13609 /* For -Wunused-function warn about unused static prototypes. */
13610 else if (warn_unused_function
13611 && ! DECL_ARTIFICIAL (decl)
13612 && ! warning_suppressed_p (decl, OPT_Wunused_function))
13613 {
13614 if (warning (OPT_Wunused_function,
13615 "%q+F declared %<static%> but never defined",
13616 decl))
13617 suppress_warning (decl, OPT_Wunused_function);
13618 }
13619 }
13620
13621 wrapup_global_declaration_1 (decl);
13622 }
13623
13624 do
13625 {
13626 reconsider = false;
13627 for (decl = globals; decl; decl = DECL_CHAIN (decl))
13628 reconsider |= wrapup_global_declaration_2 (decl);
13629 }
13630 while (reconsider);
13631 }
13632
13633 /* Preserve the external declarations scope across a garbage collect. */
13634 static GTY(()) tree ext_block;
13635
13636 /* Collect all references relevant to SOURCE_FILE. */
13637
13638 static void
13639 collect_all_refs (const char *source_file)
13640 {
13641 tree t;
13642 unsigned i;
13643
13644 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13645 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13646
13647 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13648 }
13649
13650 /* Collect source file references at global level. */
13651
13652 static void
13653 collect_source_refs (void)
13654 {
13655 tree t;
13656 tree decls;
13657 tree decl;
13658 unsigned i;
13659
13660 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13661 {
13662 decls = DECL_INITIAL (t);
13663 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13664 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13665 collect_source_ref (DECL_SOURCE_FILE (decl));
13666 }
13667
13668 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13669 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13670 collect_source_ref (DECL_SOURCE_FILE (decl));
13671 }
13672
13673 /* Free attribute access data that are not needed by the middle end. */
13674
13675 static void
13676 free_attr_access_data ()
13677 {
13678 struct cgraph_node *n;
13679
13680 /* Iterate over all functions declared in the translation unit. */
13681 FOR_EACH_FUNCTION (n)
13682 {
13683 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13684 if (tree attrs = DECL_ATTRIBUTES (parm))
13685 attr_access::free_lang_data (attrs);
13686
13687 tree fntype = TREE_TYPE (n->decl);
13688 if (!fntype || fntype == error_mark_node)
13689 continue;
13690 tree attrs = TYPE_ATTRIBUTES (fntype);
13691 if (!attrs)
13692 continue;
13693
13694 attr_access::free_lang_data (attrs);
13695 }
13696 }
13697
13698 /* Perform any final parser cleanups and generate initial debugging
13699 information. */
13700
13701 void
13702 c_parse_final_cleanups (void)
13703 {
13704 tree t;
13705 unsigned i;
13706
13707 /* We don't want to do this if generating a PCH. */
13708 if (pch_file)
13709 return;
13710
13711 timevar_stop (TV_PHASE_PARSING);
13712 timevar_start (TV_PHASE_DEFERRED);
13713
13714 /* Do the Objective-C stuff. This is where all the Objective-C
13715 module stuff gets generated (symtab, class/protocol/selector
13716 lists etc). */
13717 if (c_dialect_objc ())
13718 objc_write_global_declarations ();
13719
13720 /* Close the external scope. */
13721 ext_block = pop_scope ();
13722 external_scope = 0;
13723 gcc_assert (!current_scope);
13724
13725 /* Handle -fdump-ada-spec[-slim]. */
13726 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13727 {
13728 /* Build a table of files to generate specs for */
13729 collect_source_ref (main_input_filename);
13730 if (!flag_dump_ada_spec_slim)
13731 collect_source_refs ();
13732
13733 dump_ada_specs (collect_all_refs, NULL);
13734 }
13735
13736 /* Process all file scopes in this compilation, and the external_scope,
13737 through wrapup_global_declarations. */
13738 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13739 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13740 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13741
13742 if (!in_lto_p)
13743 free_attr_access_data ();
13744
13745 timevar_stop (TV_PHASE_DEFERRED);
13746 timevar_start (TV_PHASE_PARSING);
13747
13748 ext_block = NULL;
13749 }
13750
13751 /* Register reserved keyword WORD as qualifier for address space AS. */
13752
13753 void
13754 c_register_addr_space (const char *word, addr_space_t as)
13755 {
13756 int rid = RID_FIRST_ADDR_SPACE + as;
13757 tree id;
13758
13759 /* Address space qualifiers are only supported
13760 in C with GNU extensions enabled. */
13761 if (c_dialect_objc () || flag_no_asm)
13762 return;
13763
13764 id = get_identifier (word);
13765 C_SET_RID_CODE (id, rid);
13766 C_IS_RESERVED_WORD (id) = 1;
13767 ridpointers [rid] = id;
13768 }
13769
13770 /* Return identifier to look up for omp declare reduction. */
13771
13772 tree
13773 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13774 {
13775 const char *p = NULL;
13776 switch (reduction_code)
13777 {
13778 case PLUS_EXPR: p = "+"; break;
13779 case MULT_EXPR: p = "*"; break;
13780 case MINUS_EXPR: p = "-"; break;
13781 case BIT_AND_EXPR: p = "&"; break;
13782 case BIT_XOR_EXPR: p = "^"; break;
13783 case BIT_IOR_EXPR: p = "|"; break;
13784 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13785 case TRUTH_ORIF_EXPR: p = "||"; break;
13786 case MIN_EXPR: p = "min"; break;
13787 case MAX_EXPR: p = "max"; break;
13788 default:
13789 break;
13790 }
13791
13792 if (p == NULL)
13793 {
13794 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13795 return error_mark_node;
13796 p = IDENTIFIER_POINTER (reduction_id);
13797 }
13798
13799 const char prefix[] = "omp declare reduction ";
13800 size_t lenp = sizeof (prefix);
13801 size_t len = strlen (p);
13802 char *name = XALLOCAVEC (char, lenp + len);
13803 memcpy (name, prefix, lenp - 1);
13804 memcpy (name + lenp - 1, p, len + 1);
13805 return get_identifier (name);
13806 }
13807
13808 /* Lookup REDUCTION_ID in the current scope, or create an artificial
13809 VAR_DECL, bind it into the current scope and return it. */
13810
13811 tree
13812 c_omp_reduction_decl (tree reduction_id)
13813 {
13814 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13815 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13816 return b->decl;
13817
13818 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13819 reduction_id, integer_type_node);
13820 DECL_ARTIFICIAL (decl) = 1;
13821 DECL_EXTERNAL (decl) = 1;
13822 TREE_STATIC (decl) = 1;
13823 TREE_PUBLIC (decl) = 0;
13824 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13825 return decl;
13826 }
13827
13828 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13829
13830 tree
13831 c_omp_reduction_lookup (tree reduction_id, tree type)
13832 {
13833 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13834 while (b)
13835 {
13836 tree t;
13837 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13838 if (comptypes (TREE_PURPOSE (t), type))
13839 return TREE_VALUE (t);
13840 b = b->shadowed;
13841 }
13842 return error_mark_node;
13843 }
13844
13845 /* Helper function called via walk_tree, to diagnose invalid
13846 #pragma omp declare reduction combiners or initializers. */
13847
13848 tree
13849 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13850 {
13851 tree *vars = (tree *) data;
13852 if (SSA_VAR_P (*tp)
13853 && !DECL_ARTIFICIAL (*tp)
13854 && *tp != vars[0]
13855 && *tp != vars[1])
13856 {
13857 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13858 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13859 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13860 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13861 *tp);
13862 else
13863 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13864 "to variable %qD which is not %<omp_priv%> nor "
13865 "%<omp_orig%>",
13866 *tp);
13867 return *tp;
13868 }
13869 return NULL_TREE;
13870 }
13871
13872
13873 bool
13874 c_check_in_current_scope (tree decl)
13875 {
13876 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13877 return b != NULL && B_IN_CURRENT_SCOPE (b);
13878 }
13879
13880 /* Search for loop or switch names. BEFORE_LABELS is last statement before
13881 possible labels and SWITCH_P true for a switch, false for loops.
13882 Searches through last statements in cur_stmt_list, stops when seeing
13883 BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
13884 Returns number of loop/switch names found and if any are found, sets
13885 *LAST_P to the canonical loop/switch name LABEL_DECL. */
13886
13887 int
13888 c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
13889 {
13890 *last_p = NULL_TREE;
13891 if (!building_stmt_list_p ()
13892 || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
13893 || before_labels == void_list_node)
13894 return 0;
13895
13896 int ret = 0;
13897 tree last = NULL_TREE;
13898 for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
13899 !tsi_end_p (tsi); tsi_prev (&tsi))
13900 {
13901 tree stmt = tsi_stmt (tsi);
13902 if (stmt == before_labels)
13903 break;
13904 else if (TREE_CODE (stmt) == LABEL_EXPR)
13905 {
13906 if (last == NULL_TREE)
13907 last = LABEL_EXPR_LABEL (stmt);
13908 else
13909 {
13910 loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
13911 ++ret;
13912 }
13913 }
13914 else if (TREE_CODE (stmt) != CASE_LABEL_EXPR)
13915 break;
13916 }
13917 if (last)
13918 {
13919 if (switch_p)
13920 C_DECL_SWITCH_NAME (last) = 1;
13921 else
13922 C_DECL_LOOP_NAME (last) = 1;
13923 loop_names.safe_push (last);
13924 ++ret;
13925 if (loop_names.length () > 16)
13926 {
13927 unsigned int first = 0, i;
13928 tree l, c = NULL_TREE;
13929 if (loop_names_hash == NULL)
13930 loop_names_hash = new decl_tree_map (ret);
13931 else
13932 first = loop_names.length () - ret;
13933 FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13934 {
13935 if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
13936 c = l;
13937 gcc_checking_assert (c);
13938 loop_names_hash->put (l, c);
13939 if (i == first)
13940 break;
13941 }
13942 }
13943 *last_p = last;
13944 }
13945 return ret;
13946 }
13947
13948 /* Undoes what get_loop_names did when it returned NUM_NAMES. */
13949
13950 void
13951 c_release_loop_names (int num_names)
13952 {
13953 unsigned len = loop_names.length () - num_names;
13954 if (loop_names_hash)
13955 {
13956 if (len <= 16)
13957 {
13958 delete loop_names_hash;
13959 loop_names_hash = NULL;
13960 }
13961 else
13962 {
13963 unsigned int i;
13964 tree l;
13965 FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
13966 {
13967 loop_names_hash->remove (l);
13968 if (i == len)
13969 break;
13970 }
13971 }
13972 }
13973 loop_names.truncate (len);
13974 }
13975
13976 /* Finish processing of break or continue identifier operand.
13977 NAME is the identifier operand of break or continue and
13978 IS_BREAK is true iff it is break stmt. Returns the operand
13979 to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
13980 canonical loop/switch name LABEL_DECL. */
13981
13982 tree
13983 c_finish_bc_name (location_t loc, tree name, bool is_break)
13984 {
13985 tree label = NULL_TREE, lab;
13986 pedwarn_c23 (loc, OPT_Wpedantic,
13987 "ISO C does not support %qs statement with an identifier "
13988 "operand before C2Y", is_break ? "break" : "continue");
13989
13990 /* If I_LABEL_DECL is NULL or not from current function, don't waste time
13991 trying to find it among loop_names, it can't be there. */
13992 if (!loop_names.is_empty ()
13993 && current_function_scope
13994 && (lab = I_LABEL_DECL (name))
13995 && DECL_CONTEXT (lab) == current_function_decl)
13996 {
13997 unsigned int i;
13998 tree l, c = NULL_TREE;
13999 if (loop_names_hash)
14000 {
14001 if (tree *val = loop_names_hash->get (lab))
14002 label = *val;
14003 }
14004 else
14005 FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14006 {
14007 if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14008 c = l;
14009 gcc_checking_assert (c);
14010 if (l == lab)
14011 {
14012 label = c;
14013 break;
14014 }
14015 }
14016 if (label)
14017 TREE_USED (lab) = 1;
14018 }
14019 if (label == NULL_TREE)
14020 {
14021 auto_vec<const char *> candidates;
14022 unsigned int i;
14023 tree l, c = NULL_TREE;
14024 FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14025 {
14026 if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14027 c = l;
14028 gcc_checking_assert (c);
14029 if (is_break || C_DECL_LOOP_NAME (c))
14030 candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14031 }
14032 const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14033 &candidates);
14034 if (hint)
14035 {
14036 gcc_rich_location richloc (loc);
14037 richloc.add_fixit_replace (hint);
14038 if (is_break)
14039 error_at (&richloc, "%<break%> statement operand %qE does not "
14040 "refer to a named loop or %<switch%>; "
14041 "did you mean %qs?", name, hint);
14042 else
14043 error_at (&richloc, "%<continue%> statement operand %qE does not "
14044 "refer to a named loop; did you mean %qs?",
14045 name, hint);
14046 }
14047 else if (is_break)
14048 error_at (loc, "%<break%> statement operand %qE does not refer to a "
14049 "named loop or %<switch%>", name);
14050 else
14051 error_at (loc, "%<continue%> statement operand %qE does not refer to "
14052 "a named loop", name);
14053 }
14054 else if (!C_DECL_LOOP_NAME (label) && !is_break)
14055 {
14056 auto_diagnostic_group d;
14057 error_at (loc, "%<continue%> statement operand %qE refers to a named "
14058 "%<switch%>", name);
14059 inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14060 label = NULL_TREE;
14061 }
14062 else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14063 {
14064 auto_diagnostic_group d;
14065 if (C_DECL_LOOP_NAME (label))
14066 {
14067 error_at (loc, "%qs statement operand %qE refers to a loop outside "
14068 "of its body", is_break ? "break" : "continue", name);
14069 inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14070 }
14071 else
14072 {
14073 error_at (loc, "%<break%> statement operand %qE refers to a "
14074 "%<switch%> outside of its body", name);
14075 inform (DECL_SOURCE_LOCATION (label),
14076 "%<switch%> name defined here");
14077 }
14078 label = NULL_TREE;
14079 }
14080 else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14081 /* If it is just a fancy reference to the innermost construct, handle it
14082 just like break; or continue; though tracking cheaply what is the
14083 innermost loop for continue when nested in switches would require
14084 another global variable and updating it. */
14085 label = NULL_TREE;
14086 else
14087 C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14088 return label;
14089 }
14090
14091 #include "gt-c-c-decl.h"