Line data Source code
1 : /* Process declarations and variables for C compiler.
2 : Copyright (C) 1988-2026 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 : GENERIC_ASSOC }; /* Typename in generic association */
76 :
77 : /* States indicating how grokdeclarator() should handle declspecs marked
78 : with __attribute__((deprecated)) or __attribute__((unavailable)).
79 : An object declared as __attribute__((unavailable)) should suppress
80 : any reports of being declared with unavailable or deprecated items.
81 : An object declared as __attribute__((deprecated)) should suppress
82 : warnings of uses of other deprecated items. */
83 :
84 : enum deprecated_states {
85 : DEPRECATED_NORMAL,
86 : DEPRECATED_SUPPRESS,
87 : UNAVAILABLE_DEPRECATED_SUPPRESS
88 : };
89 :
90 :
91 : /* Nonzero if we have seen an invalid cross reference
92 : to a struct, union, or enum, but not yet printed the message. */
93 : tree pending_invalid_xref;
94 :
95 : /* File and line to appear in the eventual error message. */
96 : location_t pending_invalid_xref_location;
97 :
98 : /* The file and line that the prototype came from if this is an
99 : old-style definition; used for diagnostics in
100 : store_parm_decls_oldstyle. */
101 :
102 : static location_t current_function_prototype_locus;
103 :
104 : /* Whether this prototype was built-in. */
105 :
106 : static bool current_function_prototype_built_in;
107 :
108 : /* The argument type information of this prototype. */
109 :
110 : static tree current_function_prototype_arg_types;
111 :
112 : /* The argument information structure for the function currently being
113 : defined. */
114 :
115 : static struct c_arg_info *current_function_arg_info;
116 :
117 : /* The obstack on which parser and related data structures, which are
118 : not live beyond their top-level declaration or definition, are
119 : allocated. */
120 : struct obstack parser_obstack;
121 :
122 : /* The current statement tree. */
123 :
124 : static GTY(()) struct stmt_tree_s c_stmt_tree;
125 :
126 : /* Zero if we are not in an iteration or switch statement, otherwise
127 : a bitmask. See bitmask definitions in c-tree.h. */
128 : unsigned char in_statement;
129 :
130 : /* A list of decls to be made automatically visible in each file scope. */
131 : static GTY(()) tree visible_builtins;
132 :
133 : /* Set to 0 at beginning of a function definition, set to 1 if
134 : a return statement that specifies a return value is seen. */
135 :
136 : int current_function_returns_value;
137 :
138 : /* Set to 0 at beginning of a function definition, set to 1 if
139 : a return statement with no argument is seen. */
140 :
141 : int current_function_returns_null;
142 :
143 : /* Set to 0 at beginning of a function definition, set to 1 if
144 : a call to a noreturn function is seen. */
145 :
146 : int current_function_returns_abnormally;
147 :
148 : /* Set to nonzero by `grokdeclarator' for a function
149 : whose return type is defaulted, if warnings for this are desired. */
150 :
151 : static int warn_about_return_type;
152 :
153 : /* Nonzero when the current toplevel function contains a declaration
154 : of a nested function which is never defined. */
155 :
156 : static bool undef_nested_function;
157 :
158 : /* Vector of implicit "omp declare target" attributes to be added into
159 : the attribute lists. */
160 : vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
161 :
162 : /* Vector of
163 : #pragma omp begin assumes ... #pragma omp end assumes regions
164 : we are in. */
165 : vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
166 :
167 : /* Vector of "omp begin/end declare variant" blocks we are in. */
168 : vec<c_omp_declare_variant_attr, va_gc> *current_omp_declare_variant_attribute;
169 :
170 : /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
171 : LABEL_DECL as the last and canonical for each loop or switch. */
172 : static vec<tree> loop_names;
173 :
174 : /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
175 : vector becomes too long. */
176 : static decl_tree_map *loop_names_hash;
177 :
178 : /* Each c_binding structure describes one binding of an identifier to
179 : a decl. All the decls in a scope - irrespective of namespace - are
180 : chained together by the ->prev field, which (as the name implies)
181 : runs in reverse order. All the decls in a given namespace bound to
182 : a given identifier are chained by the ->shadowed field, which runs
183 : from inner to outer scopes.
184 :
185 : The ->decl field usually points to a DECL node, but there are two
186 : exceptions. In the namespace of type tags, the bound entity is a
187 : RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
188 : identifier is encountered, it is bound to error_mark_node to
189 : suppress further errors about that identifier in the current
190 : function.
191 :
192 : The ->u.type field stores the type of the declaration in this scope;
193 : if NULL, the type is the type of the ->decl field. This is only of
194 : relevance for objects with external or internal linkage which may
195 : be redeclared in inner scopes, forming composite types that only
196 : persist for the duration of those scopes. In the external scope,
197 : this stores the composite of all the types declared for this
198 : object, visible or not. The ->inner_comp field (used only at file
199 : scope) stores whether an incomplete array type at file scope was
200 : completed at an inner scope to an array size other than 1.
201 :
202 : The ->u.label field is used for labels. It points to a structure
203 : which stores additional information used for warnings.
204 :
205 : The depth field is copied from the scope structure that holds this
206 : decl. It is used to preserve the proper ordering of the ->shadowed
207 : field (see bind()) and also for a handful of special-case checks.
208 : Finally, the invisible bit is true for a decl which should be
209 : ignored for purposes of normal name lookup, and the nested bit is
210 : true for a decl that's been bound a second time in an inner scope;
211 : in all such cases, the binding in the outer scope will have its
212 : invisible bit true. */
213 :
214 : struct GTY((chain_next ("%h.prev"))) c_binding {
215 : union GTY(()) { /* first so GTY desc can use decl */
216 : tree GTY((tag ("0"))) type; /* the type in this scope */
217 : struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
218 : } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
219 : tree decl; /* the decl bound */
220 : tree id; /* the identifier it's bound to */
221 : struct c_binding *prev; /* the previous decl in this scope */
222 : struct c_binding *shadowed; /* the innermost decl shadowed by this one */
223 : unsigned int depth : 28; /* depth of this scope */
224 : BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
225 : BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
226 : BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
227 : BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
228 : location_t locus; /* location for nested bindings */
229 : };
230 : #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
231 : #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
232 : #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
233 : #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
234 :
235 : /* Each C symbol points to three linked lists of c_binding structures.
236 : These describe the values of the identifier in the three different
237 : namespaces defined by the language. */
238 :
239 : struct GTY(()) lang_identifier {
240 : struct c_common_identifier common_id;
241 : struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
242 : struct c_binding *tag_binding; /* struct/union/enum tags */
243 : struct c_binding *label_binding; /* labels */
244 : };
245 :
246 : /* Validate c-lang.cc's assumptions. */
247 : extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
248 : [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
249 :
250 : /* The binding oracle; see c-tree.h. */
251 : void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
252 :
253 : /* This flag is set on an identifier if we have previously asked the
254 : binding oracle for this identifier's symbol binding. */
255 : #define I_SYMBOL_CHECKED(node) \
256 : (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
257 :
258 : static inline struct c_binding* *
259 4529744557 : i_symbol_binding (tree node)
260 : {
261 4529744557 : struct lang_identifier *lid
262 4529744557 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
263 :
264 4529744557 : if (lid->symbol_binding == NULL
265 1667358097 : && c_binding_oracle != NULL
266 4529744557 : && !I_SYMBOL_CHECKED (node))
267 : {
268 : /* Set the "checked" flag first, to avoid infinite recursion
269 : when the binding oracle calls back into gcc. */
270 0 : I_SYMBOL_CHECKED (node) = 1;
271 0 : c_binding_oracle (C_ORACLE_SYMBOL, node);
272 : }
273 :
274 4529744557 : return &lid->symbol_binding;
275 : }
276 :
277 : #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
278 :
279 : #define I_SYMBOL_DECL(node) \
280 : (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
281 :
282 : /* This flag is set on an identifier if we have previously asked the
283 : binding oracle for this identifier's tag binding. */
284 : #define I_TAG_CHECKED(node) \
285 : (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
286 :
287 : static inline struct c_binding **
288 4527810 : i_tag_binding (tree node)
289 : {
290 4527810 : struct lang_identifier *lid
291 4527810 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
292 :
293 4527810 : if (lid->tag_binding == NULL
294 1254084 : && c_binding_oracle != NULL
295 4527810 : && !I_TAG_CHECKED (node))
296 : {
297 : /* Set the "checked" flag first, to avoid infinite recursion
298 : when the binding oracle calls back into gcc. */
299 0 : I_TAG_CHECKED (node) = 1;
300 0 : c_binding_oracle (C_ORACLE_TAG, node);
301 : }
302 :
303 4527810 : return &lid->tag_binding;
304 : }
305 :
306 : #define I_TAG_BINDING(node) (*i_tag_binding (node))
307 :
308 : #define I_TAG_DECL(node) \
309 : (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
310 :
311 : /* This flag is set on an identifier if we have previously asked the
312 : binding oracle for this identifier's label binding. */
313 : #define I_LABEL_CHECKED(node) \
314 : (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
315 :
316 : static inline struct c_binding **
317 359625 : i_label_binding (tree node)
318 : {
319 359625 : struct lang_identifier *lid
320 359625 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
321 :
322 359625 : if (lid->label_binding == NULL
323 47840 : && c_binding_oracle != NULL
324 359625 : && !I_LABEL_CHECKED (node))
325 : {
326 : /* Set the "checked" flag first, to avoid infinite recursion
327 : when the binding oracle calls back into gcc. */
328 0 : I_LABEL_CHECKED (node) = 1;
329 0 : c_binding_oracle (C_ORACLE_LABEL, node);
330 : }
331 :
332 359625 : return &lid->label_binding;
333 : }
334 :
335 : #define I_LABEL_BINDING(node) (*i_label_binding (node))
336 :
337 : #define I_LABEL_DECL(node) \
338 : (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
339 :
340 : /* Used by C_TOKEN_VEC tree. */
341 : struct GTY (()) c_tree_token_vec {
342 : struct tree_base base;
343 : vec<c_token, va_gc> *tokens;
344 : };
345 :
346 : STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
347 : STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
348 : == offsetof (c_tree_token_vec_struct, tokens));
349 :
350 : /* The resulting tree type. */
351 :
352 : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
353 : chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
354 : {
355 : union tree_node GTY ((tag ("0"),
356 : desc ("tree_node_structure (&%h)")))
357 : generic;
358 : struct lang_identifier GTY ((tag ("1"))) identifier;
359 : struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
360 : };
361 :
362 : /* Langhook for tree_size. */
363 : size_t
364 911 : c_tree_size (enum tree_code code)
365 : {
366 911 : gcc_checking_assert (code >= NUM_TREE_CODES);
367 911 : switch (code)
368 : {
369 : case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
370 0 : default:
371 0 : switch (TREE_CODE_CLASS (code))
372 : {
373 : case tcc_declaration: return sizeof (tree_decl_non_common);
374 0 : case tcc_type: return sizeof (tree_type_non_common);
375 0 : default: gcc_unreachable ();
376 : }
377 : }
378 : }
379 :
380 : /* Track bindings and other things that matter for goto warnings. For
381 : efficiency, we do not gather all the decls at the point of
382 : definition. Instead, we point into the bindings structure. As
383 : scopes are popped, we update these structures and gather the decls
384 : that matter at that time. */
385 :
386 : struct GTY(()) c_spot_bindings {
387 : /* The currently open scope which holds bindings defined when the
388 : label was defined or the goto statement was found. */
389 : struct c_scope *scope;
390 : /* The bindings in the scope field which were defined at the point
391 : of the label or goto. This lets us look at older or newer
392 : bindings in the scope, as appropriate. */
393 : struct c_binding *bindings_in_scope;
394 : /* The number of statement expressions that have started since this
395 : label or goto statement was defined. This is zero if we are at
396 : the same statement expression level. It is positive if we are in
397 : a statement expression started since this spot. It is negative
398 : if this spot was in a statement expression and we have left
399 : it. */
400 : int stmt_exprs;
401 : /* Whether we started in a statement expression but are no longer in
402 : it. This is set to true if stmt_exprs ever goes negative. */
403 : bool left_stmt_expr;
404 : };
405 :
406 : /* This structure is used to keep track of bindings seen when a goto
407 : statement is defined. This is only used if we see the goto
408 : statement before we see the label. */
409 :
410 : struct GTY(()) c_goto_bindings {
411 : /* The location of the goto statement. */
412 : location_t loc;
413 : /* The bindings of the goto statement. */
414 : struct c_spot_bindings goto_bindings;
415 : };
416 :
417 : typedef struct c_goto_bindings *c_goto_bindings_p;
418 :
419 : /* The additional information we keep track of for a label binding.
420 : These fields are updated as scopes are popped. */
421 :
422 : struct GTY(()) c_label_vars {
423 : /* The shadowed c_label_vars, when one label shadows another (which
424 : can only happen using a __label__ declaration). */
425 : struct c_label_vars *shadowed;
426 : /* The bindings when the label was defined. */
427 : struct c_spot_bindings label_bindings;
428 : /* A list of decls that we care about: decls about which we should
429 : warn if a goto branches to this label from later in the function.
430 : Decls are added to this list as scopes are popped. We only add
431 : the decls that matter. */
432 : vec<tree, va_gc> *decls_in_scope;
433 : /* A list of goto statements to this label. This is only used for
434 : goto statements seen before the label was defined, so that we can
435 : issue appropriate warnings for them. */
436 : vec<c_goto_bindings_p, va_gc> *gotos;
437 : };
438 :
439 : /* Each c_scope structure describes the complete contents of one
440 : scope. Four scopes are distinguished specially: the innermost or
441 : current scope, the innermost function scope, the file scope (always
442 : the second to outermost) and the outermost or external scope.
443 :
444 : Most declarations are recorded in the current scope.
445 :
446 : All normal label declarations are recorded in the innermost
447 : function scope, as are bindings of undeclared identifiers to
448 : error_mark_node. (GCC permits nested functions as an extension,
449 : hence the 'innermost' qualifier.) Explicitly declared labels
450 : (using the __label__ extension) appear in the current scope.
451 :
452 : Being in the file scope (current_scope == file_scope) causes
453 : special behavior in several places below. Also, under some
454 : conditions the Objective-C front end records declarations in the
455 : file scope even though that isn't the current scope.
456 :
457 : All declarations with external linkage are recorded in the external
458 : scope, even if they aren't visible there; this models the fact that
459 : such declarations are visible to the entire program, and (with a
460 : bit of cleverness, see pushdecl) allows diagnosis of some violations
461 : of C99 6.2.2p7 and 6.2.7p2:
462 :
463 : If, within the same translation unit, the same identifier appears
464 : with both internal and external linkage, the behavior is
465 : undefined.
466 :
467 : All declarations that refer to the same object or function shall
468 : have compatible type; otherwise, the behavior is undefined.
469 :
470 : Initially only the built-in declarations, which describe compiler
471 : intrinsic functions plus a subset of the standard library, are in
472 : this scope.
473 :
474 : The order of the blocks list matters, and it is frequently appended
475 : to. To avoid having to walk all the way to the end of the list on
476 : each insertion, or reverse the list later, we maintain a pointer to
477 : the last list entry. (FIXME: It should be feasible to use a reversed
478 : list here.)
479 :
480 : The bindings list is strictly in reverse order of declarations;
481 : pop_scope relies on this. */
482 :
483 :
484 : struct GTY((chain_next ("%h.outer"))) c_scope {
485 : /* The scope containing this one. */
486 : struct c_scope *outer;
487 :
488 : /* The next outermost function scope. */
489 : struct c_scope *outer_function;
490 :
491 : /* All bindings in this scope. */
492 : struct c_binding *bindings;
493 :
494 : /* For each scope (except the global one), a chain of BLOCK nodes
495 : for all the scopes that were entered and exited one level down. */
496 : tree blocks;
497 : tree blocks_last;
498 :
499 : /* The depth of this scope. Used to keep the ->shadowed chain of
500 : bindings sorted innermost to outermost. */
501 : unsigned int depth : 28;
502 :
503 : /* True if we are currently filling this scope with parameter
504 : declarations. */
505 : BOOL_BITFIELD parm_flag : 1;
506 :
507 : /* True if we saw [*] in this scope. Used to give an error messages
508 : if these appears in a function definition. */
509 : BOOL_BITFIELD had_vla_unspec : 1;
510 :
511 : /* True if we parsed a list of forward parameter decls in this scope. */
512 : BOOL_BITFIELD had_forward_parm_decls : 1;
513 :
514 : /* True if this is the outermost block scope of a function body.
515 : This scope contains the parameters, the local variables declared
516 : in the outermost block, and all the labels (except those in
517 : nested functions, or declared at block scope with __label__). */
518 : BOOL_BITFIELD function_body : 1;
519 :
520 : /* True means make a BLOCK for this scope no matter what. */
521 : BOOL_BITFIELD keep : 1;
522 :
523 : /* True means that an unsuffixed float constant is _Decimal64. */
524 : BOOL_BITFIELD float_const_decimal64 : 1;
525 :
526 : /* True if this scope has any label bindings. This is used to speed
527 : up searching for labels when popping scopes, particularly since
528 : labels are normally only found at function scope. */
529 : BOOL_BITFIELD has_label_bindings : 1;
530 :
531 : /* True if we should issue a warning if a goto statement crosses any
532 : of the bindings. We still need to check the list of bindings to
533 : find the specific ones we need to warn about. This is true if
534 : decl_jump_unsafe would return true for any of the bindings. This
535 : is used to avoid looping over all the bindings unnecessarily. */
536 : BOOL_BITFIELD has_jump_unsafe_decl : 1;
537 : };
538 :
539 : /* The scope currently in effect. */
540 :
541 : static GTY(()) struct c_scope *current_scope;
542 :
543 : /* The innermost function scope. Ordinary (not explicitly declared)
544 : labels, bindings to error_mark_node, and the lazily-created
545 : bindings of __func__ and its friends get this scope. */
546 :
547 : static GTY(()) struct c_scope *current_function_scope;
548 :
549 : /* The C file scope. This is reset for each input translation unit. */
550 :
551 : static GTY(()) struct c_scope *file_scope;
552 :
553 : /* The outermost scope. This is used for all declarations with
554 : external linkage, and only these, hence the name. */
555 :
556 : static GTY(()) struct c_scope *external_scope;
557 :
558 : /* A chain of c_scope structures awaiting reuse. */
559 :
560 : static GTY((deletable)) struct c_scope *scope_freelist;
561 :
562 : /* A chain of c_binding structures awaiting reuse. */
563 :
564 : static GTY((deletable)) struct c_binding *binding_freelist;
565 :
566 : /* Append VAR to LIST in scope SCOPE. */
567 : #define SCOPE_LIST_APPEND(scope, list, decl) do { \
568 : struct c_scope *s_ = (scope); \
569 : tree d_ = (decl); \
570 : if (s_->list##_last) \
571 : BLOCK_CHAIN (s_->list##_last) = d_; \
572 : else \
573 : s_->list = d_; \
574 : s_->list##_last = d_; \
575 : } while (0)
576 :
577 : /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
578 : #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
579 : struct c_scope *t_ = (tscope); \
580 : struct c_scope *f_ = (fscope); \
581 : if (t_->to##_last) \
582 : BLOCK_CHAIN (t_->to##_last) = f_->from; \
583 : else \
584 : t_->to = f_->from; \
585 : t_->to##_last = f_->from##_last; \
586 : } while (0)
587 :
588 : /* A c_inline_static structure stores details of a static identifier
589 : referenced in a definition of a function that may be an inline
590 : definition if no subsequent declaration of that function uses
591 : "extern" or does not use "inline". */
592 :
593 : struct GTY((chain_next ("%h.next"))) c_inline_static {
594 : /* The location for a diagnostic. */
595 : location_t location;
596 :
597 : /* The function that may be an inline definition. */
598 : tree function;
599 :
600 : /* The object or function referenced. */
601 : tree static_decl;
602 :
603 : /* What sort of reference this is. */
604 : enum c_inline_static_type type;
605 :
606 : /* The next such structure or NULL. */
607 : struct c_inline_static *next;
608 : };
609 :
610 : /* List of static identifiers used or referenced in functions that may
611 : be inline definitions. */
612 : static GTY(()) struct c_inline_static *c_inline_statics;
613 :
614 : /* True means unconditionally make a BLOCK for the next scope pushed. */
615 :
616 : static bool keep_next_level_flag;
617 :
618 : /* True means the next call to push_scope will be the outermost scope
619 : of a function body, so do not push a new scope, merely cease
620 : expecting parameter decls. */
621 :
622 : static bool next_is_function_body;
623 :
624 : /* A vector of pointers to c_binding structures. */
625 :
626 : typedef struct c_binding *c_binding_ptr;
627 :
628 : /* Information that we keep for a struct or union while it is being
629 : parsed. */
630 :
631 1179714 : class c_struct_parse_info
632 : {
633 : public:
634 : /* If warn_cxx_compat, a list of types defined within this
635 : struct. */
636 : auto_vec<tree> struct_types;
637 : /* If warn_cxx_compat, a list of field names which have bindings,
638 : and which are defined in this struct, but which are not defined
639 : in any enclosing struct. This is used to clear the in_struct
640 : field of the c_bindings structure. */
641 : auto_vec<c_binding_ptr> fields;
642 : /* If warn_cxx_compat, a list of typedef names used when defining
643 : fields in this struct. */
644 : auto_vec<tree> typedefs_seen;
645 : /* The location of a previous definition of this struct. */
646 : location_t refloc;
647 : };
648 :
649 :
650 : /* Hash table for structs and unions. */
651 : struct c_struct_hasher : ggc_ptr_hash<tree_node>
652 : {
653 : static hashval_t hash (tree t);
654 : static bool equal (tree, tree);
655 : };
656 :
657 : /* Hash an RECORD OR UNION. */
658 : hashval_t
659 4331953 : c_struct_hasher::hash (tree type)
660 : {
661 4331953 : inchash::hash hstate;
662 :
663 4331953 : hstate.add_int (TREE_CODE (type));
664 4331953 : tree tag = c_type_tag (type);
665 4331953 : hstate.add_object (tag);
666 :
667 4331953 : return hstate.end ();
668 : }
669 :
670 : /* Compare two RECORD or UNION types. */
671 : bool
672 14643587 : c_struct_hasher::equal (tree t1, tree t2)
673 : {
674 14643587 : return comptypes_equiv_p (t1, t2);
675 : }
676 :
677 : /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
678 : static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
679 :
680 : /* Information for the struct or union currently being parsed, or
681 : NULL if not parsing a struct or union. */
682 : static class c_struct_parse_info *struct_parse_info;
683 :
684 : /* Forward declarations. */
685 : static tree lookup_name_in_scope (tree, struct c_scope *);
686 : static tree c_make_fname_decl (location_t, tree, int);
687 : static tree grokdeclarator (const struct c_declarator *,
688 : struct c_declspecs *,
689 : enum decl_context, bool, tree *, tree *, tree *,
690 : bool *, enum deprecated_states);
691 : static tree grokparms (struct c_arg_info *, bool);
692 : static void layout_array_type (tree);
693 : static const char *header_for_builtin_fn (tree);
694 :
695 : /* T is a statement. Add it to the statement-tree. This is the
696 : C/ObjC version--C++ has a slightly different version of this
697 : function. */
698 :
699 : tree
700 93698634 : add_stmt (tree t)
701 : {
702 93698634 : enum tree_code code = TREE_CODE (t);
703 :
704 93698634 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
705 : {
706 92984750 : if (!EXPR_HAS_LOCATION (t))
707 155030 : SET_EXPR_LOCATION (t, input_location);
708 : }
709 :
710 : /* Add T to the statement-tree. Non-side-effect statements need to be
711 : recorded during statement expressions. */
712 93698634 : if (!building_stmt_list_p ())
713 1 : push_stmt_list ();
714 :
715 93698634 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
716 1079679 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
717 :
718 93698634 : append_to_statement_list_force (t, &cur_stmt_list);
719 :
720 93698634 : return t;
721 : }
722 :
723 :
724 :
725 :
726 :
727 : /* Return true if we will want to say something if a goto statement
728 : crosses DECL. */
729 :
730 : static bool
731 890418158 : decl_jump_unsafe (tree decl)
732 : {
733 890418158 : if (error_operand_p (decl))
734 : return false;
735 :
736 : /* Don't warn for compound literals. If a goto statement crosses
737 : their initialization, it should cross also all the places where
738 : the complit is used or where the complit address might be saved
739 : into some variable, so code after the label to which goto jumps
740 : should not be able to refer to the compound literal. */
741 890416166 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
742 : return false;
743 :
744 889497048 : if (flag_openmp
745 12453539 : && VAR_P (decl)
746 889528429 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
747 : return true;
748 :
749 : /* Always warn about crossing variably modified types. */
750 879462120 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
751 901477358 : && c_type_variably_modified_p (TREE_TYPE (decl)))
752 : return true;
753 :
754 : /* Otherwise, only warn if -Wgoto-misses-init and this is an
755 : initialized automatic decl. */
756 889456541 : if (warn_jump_misses_init
757 6000501 : && VAR_P (decl)
758 10211 : && !TREE_STATIC (decl)
759 889461986 : && DECL_INITIAL (decl) != NULL_TREE)
760 : return true;
761 :
762 : return false;
763 : }
764 :
765 :
766 : void
767 0 : c_print_identifier (FILE *file, tree node, int indent)
768 : {
769 0 : void (*save) (enum c_oracle_request, tree identifier);
770 :
771 : /* Temporarily hide any binding oracle. Without this, calls to
772 : debug_tree from the debugger will end up calling into the oracle,
773 : making for a confusing debug session. As the oracle isn't needed
774 : here for normal operation, it's simplest to suppress it. */
775 0 : save = c_binding_oracle;
776 0 : c_binding_oracle = NULL;
777 :
778 0 : print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
779 0 : print_node (file, "tag", I_TAG_DECL (node), indent + 4);
780 0 : print_node (file, "label", I_LABEL_DECL (node), indent + 4);
781 0 : if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
782 : {
783 0 : tree rid = ridpointers[C_RID_CODE (node)];
784 0 : indent_to (file, indent + 4);
785 0 : fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
786 0 : (void *) rid, IDENTIFIER_POINTER (rid));
787 : }
788 :
789 0 : c_binding_oracle = save;
790 0 : }
791 :
792 : /* Establish that the scope contains declarations that are sensitive to
793 : jumps that cross a binding. Together with decl_jump_unsafe, this is
794 : used to diagnose such jumps. */
795 : void
796 71 : c_mark_decl_jump_unsafe_in_current_scope ()
797 : {
798 71 : current_scope->has_jump_unsafe_decl = 1;
799 71 : }
800 :
801 : /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
802 : which may be any of several kinds of DECL or TYPE or error_mark_node,
803 : in the scope SCOPE. */
804 : static void
805 890269003 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
806 : bool nested, location_t locus)
807 : {
808 890269003 : struct c_binding *b, **here;
809 :
810 890269003 : if (binding_freelist)
811 : {
812 230391823 : b = binding_freelist;
813 230391823 : binding_freelist = b->prev;
814 : }
815 : else
816 659877180 : b = ggc_alloc<c_binding> ();
817 :
818 890269003 : b->shadowed = 0;
819 890269003 : b->decl = decl;
820 890269003 : b->id = name;
821 890269003 : b->depth = scope->depth;
822 890269003 : b->invisible = invisible;
823 890269003 : b->nested = nested;
824 890269003 : b->inner_comp = 0;
825 890269003 : b->in_struct = 0;
826 890269003 : b->locus = locus;
827 :
828 890269003 : b->u.type = NULL;
829 :
830 890269003 : b->prev = scope->bindings;
831 890269003 : scope->bindings = b;
832 :
833 890269003 : if (decl_jump_unsafe (decl))
834 24191 : scope->has_jump_unsafe_decl = 1;
835 :
836 890269003 : if (!name)
837 : return;
838 :
839 881641328 : switch (TREE_CODE (decl))
840 : {
841 23949 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
842 626358 : case ENUMERAL_TYPE:
843 626358 : case UNION_TYPE:
844 626358 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
845 880991021 : case VAR_DECL:
846 880991021 : case FUNCTION_DECL:
847 880991021 : case TYPE_DECL:
848 880991021 : case CONST_DECL:
849 880991021 : case PARM_DECL:
850 880991021 : case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
851 :
852 0 : default:
853 0 : gcc_unreachable ();
854 : }
855 :
856 : /* Locate the appropriate place in the chain of shadowed decls
857 : to insert this binding. Normally, scope == current_scope and
858 : this does nothing. */
859 881641372 : while (*here && (*here)->depth > scope->depth)
860 44 : here = &(*here)->shadowed;
861 :
862 881641328 : b->shadowed = *here;
863 881641328 : *here = b;
864 : }
865 :
866 : /* Clear the binding structure B, stick it on the binding_freelist,
867 : and return the former value of b->prev. This is used by pop_scope
868 : and get_parm_info to iterate destructively over all the bindings
869 : from a given scope. */
870 : static struct c_binding *
871 873333695 : free_binding_and_advance (struct c_binding *b)
872 : {
873 873333695 : struct c_binding *prev = b->prev;
874 :
875 873333695 : memset (b, 0, sizeof (struct c_binding));
876 873333695 : b->prev = binding_freelist;
877 873333695 : binding_freelist = b;
878 :
879 873333695 : return prev;
880 : }
881 :
882 : /* Bind a label. Like bind, but skip fields which aren't used for
883 : labels, and add the LABEL_VARS value. */
884 : static void
885 23949 : bind_label (tree name, tree label, struct c_scope *scope,
886 : struct c_label_vars *label_vars)
887 : {
888 23949 : struct c_binding *b;
889 :
890 23949 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
891 : UNKNOWN_LOCATION);
892 :
893 23949 : scope->has_label_bindings = true;
894 :
895 23949 : b = scope->bindings;
896 23949 : gcc_assert (b->decl == label);
897 23949 : label_vars->shadowed = b->u.label;
898 23949 : b->u.label = label_vars;
899 23949 : }
900 :
901 : /* Hook called at end of compilation to assume 1 elt
902 : for a file-scope tentative array defn that wasn't complete before. */
903 :
904 : void
905 7498 : c_finish_incomplete_decl (tree decl)
906 : {
907 7498 : if (VAR_P (decl))
908 : {
909 7498 : tree type = TREE_TYPE (decl);
910 7498 : if (type != error_mark_node
911 7421 : && TREE_CODE (type) == ARRAY_TYPE
912 7226 : && !DECL_EXTERNAL (decl)
913 7587 : && TYPE_DOMAIN (type) == NULL_TREE)
914 : {
915 89 : if (flag_isoc2y && !TREE_PUBLIC (decl))
916 2 : error_at (DECL_SOURCE_LOCATION (decl),
917 : "array size missing in %q+D", decl);
918 : else
919 87 : warning_at (DECL_SOURCE_LOCATION (decl),
920 87 : 0, "array %q+D assumed to have one element", decl);
921 :
922 89 : complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
923 :
924 89 : relayout_decl (decl);
925 : }
926 : }
927 7498 : }
928 :
929 : /* Record that inline function FUNC contains a reference (location
930 : LOC) to static DECL (file-scope or function-local according to
931 : TYPE). */
932 :
933 : void
934 36 : record_inline_static (location_t loc, tree func, tree decl,
935 : enum c_inline_static_type type)
936 : {
937 36 : c_inline_static *csi = ggc_alloc<c_inline_static> ();
938 36 : csi->location = loc;
939 36 : csi->function = func;
940 36 : csi->static_decl = decl;
941 36 : csi->type = type;
942 36 : csi->next = c_inline_statics;
943 36 : c_inline_statics = csi;
944 36 : }
945 :
946 : /* Check for references to static declarations in inline functions at
947 : the end of the translation unit and diagnose them if the functions
948 : are still inline definitions. */
949 :
950 : static void
951 104574 : check_inline_statics (void)
952 : {
953 104574 : struct c_inline_static *csi;
954 104610 : for (csi = c_inline_statics; csi; csi = csi->next)
955 : {
956 36 : if (DECL_EXTERNAL (csi->function))
957 30 : switch (csi->type)
958 : {
959 23 : case csi_internal:
960 23 : pedwarn (csi->location, 0,
961 : "%qD is static but used in inline function %qD "
962 : "which is not static", csi->static_decl, csi->function);
963 23 : break;
964 7 : case csi_modifiable:
965 7 : pedwarn (csi->location, 0,
966 : "%q+D is static but declared in inline function %qD "
967 : "which is not static", csi->static_decl, csi->function);
968 7 : break;
969 0 : default:
970 0 : gcc_unreachable ();
971 : }
972 : }
973 104574 : c_inline_statics = NULL;
974 104574 : }
975 :
976 : /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
977 : for the current state, otherwise set it to uninitialized. */
978 :
979 : static void
980 134817 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
981 : {
982 0 : if (defining)
983 : {
984 127672 : p->scope = current_scope;
985 16804 : p->bindings_in_scope = current_scope->bindings;
986 : }
987 : else
988 : {
989 7145 : p->scope = NULL;
990 7145 : p->bindings_in_scope = NULL;
991 : }
992 134817 : p->stmt_exprs = 0;
993 134817 : p->left_stmt_expr = false;
994 0 : }
995 :
996 : /* Update spot bindings P as we pop out of SCOPE. Return true if we
997 : should push decls for a label. */
998 :
999 : static bool
1000 470609260 : update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
1001 : {
1002 0 : if (p->scope != scope)
1003 : {
1004 : /* This label or goto is defined in some other scope, or it is a
1005 : label which is not yet defined. There is nothing to
1006 : update. */
1007 : return false;
1008 : }
1009 :
1010 : /* Adjust the spot bindings to refer to the bindings already defined
1011 : in the enclosing scope. */
1012 205676 : p->scope = scope->outer;
1013 205676 : p->bindings_in_scope = p->scope->bindings;
1014 :
1015 151499 : return true;
1016 : }
1017 :
1018 : /* The Objective-C front-end often needs to determine the current scope. */
1019 :
1020 : void *
1021 0 : objc_get_current_scope (void)
1022 : {
1023 0 : return current_scope;
1024 : }
1025 :
1026 : /* The following function is used only by Objective-C. It needs to live here
1027 : because it accesses the innards of c_scope. */
1028 :
1029 : void
1030 0 : objc_mark_locals_volatile (void *enclosing_blk)
1031 : {
1032 0 : struct c_scope *scope;
1033 0 : struct c_binding *b;
1034 :
1035 0 : for (scope = current_scope;
1036 0 : scope && scope != enclosing_blk;
1037 0 : scope = scope->outer)
1038 : {
1039 0 : for (b = scope->bindings; b; b = b->prev)
1040 0 : objc_volatilize_decl (b->decl);
1041 :
1042 : /* Do not climb up past the current function. */
1043 0 : if (scope->function_body)
1044 : break;
1045 : }
1046 0 : }
1047 :
1048 : /* Return true if we are in the global binding level. */
1049 :
1050 : bool
1051 2089844 : global_bindings_p (void)
1052 : {
1053 2089844 : return current_scope == file_scope;
1054 : }
1055 :
1056 : /* Return true if we're declaring parameters in an old-style function
1057 : declaration. */
1058 :
1059 : bool
1060 34896 : old_style_parameter_scope (void)
1061 : {
1062 : /* If processing parameters and there is no function statement list, we
1063 : * have an old-style function declaration. */
1064 34896 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1065 : }
1066 :
1067 : void
1068 52252 : keep_next_level (void)
1069 : {
1070 52252 : keep_next_level_flag = true;
1071 52252 : }
1072 :
1073 : /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1074 :
1075 : void
1076 20 : set_float_const_decimal64 (void)
1077 : {
1078 20 : current_scope->float_const_decimal64 = true;
1079 20 : }
1080 :
1081 : /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1082 :
1083 : void
1084 28 : clear_float_const_decimal64 (void)
1085 : {
1086 28 : current_scope->float_const_decimal64 = false;
1087 28 : }
1088 :
1089 : /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1090 :
1091 : bool
1092 602499 : float_const_decimal64_p (void)
1093 : {
1094 602499 : return current_scope->float_const_decimal64;
1095 : }
1096 :
1097 : /* Identify this scope as currently being filled with parameters. */
1098 :
1099 : void
1100 86860561 : declare_parm_level (void)
1101 : {
1102 86860561 : current_scope->parm_flag = true;
1103 86860561 : }
1104 :
1105 : void
1106 128126477 : push_scope (void)
1107 : {
1108 128126477 : if (next_is_function_body)
1109 : {
1110 : /* This is the transition from the parameters to the top level
1111 : of the function body. These are the same scope
1112 : (C99 6.2.1p4,6) so we do not push another scope structure.
1113 : next_is_function_body is set only by store_parm_decls, which
1114 : in turn is called when and only when we are about to
1115 : encounter the opening curly brace for the function body.
1116 :
1117 : The outermost block of a function always gets a BLOCK node,
1118 : because the debugging output routines expect that each
1119 : function has at least one BLOCK. */
1120 36301432 : current_scope->parm_flag = false;
1121 36301432 : current_scope->function_body = true;
1122 36301432 : current_scope->keep = true;
1123 36301432 : current_scope->outer_function = current_function_scope;
1124 36301432 : current_function_scope = current_scope;
1125 :
1126 36301432 : keep_next_level_flag = false;
1127 36301432 : next_is_function_body = false;
1128 :
1129 : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1130 36301432 : if (current_scope->outer)
1131 36301432 : current_scope->float_const_decimal64
1132 36301432 : = current_scope->outer->float_const_decimal64;
1133 : else
1134 0 : current_scope->float_const_decimal64 = false;
1135 : }
1136 : else
1137 : {
1138 91825045 : struct c_scope *scope;
1139 91825045 : if (scope_freelist)
1140 : {
1141 90980886 : scope = scope_freelist;
1142 90980886 : scope_freelist = scope->outer;
1143 : }
1144 : else
1145 844159 : scope = ggc_cleared_alloc<c_scope> ();
1146 :
1147 : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1148 91825045 : if (current_scope)
1149 91714917 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1150 : else
1151 110128 : scope->float_const_decimal64 = false;
1152 :
1153 91825045 : scope->keep = keep_next_level_flag;
1154 91825045 : scope->outer = current_scope;
1155 91825045 : scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1156 :
1157 : /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1158 : possible. */
1159 91825045 : if (current_scope && scope->depth == 0)
1160 : {
1161 0 : scope->depth--;
1162 0 : sorry ("GCC supports only %u nested scopes", scope->depth);
1163 : }
1164 :
1165 91825045 : current_scope = scope;
1166 91825045 : keep_next_level_flag = false;
1167 : }
1168 128126477 : }
1169 :
1170 : /* This is called when we are leaving SCOPE. For each label defined
1171 : in SCOPE, add any appropriate decls to its decls_in_scope fields.
1172 : These are the decls whose initialization will be skipped by a goto
1173 : later in the function. */
1174 :
1175 : static void
1176 91818644 : update_label_decls (struct c_scope *scope)
1177 : {
1178 91818644 : struct c_scope *s;
1179 :
1180 91818644 : s = scope;
1181 331304127 : while (s != NULL)
1182 : {
1183 280551927 : if (s->has_label_bindings)
1184 : {
1185 232704 : struct c_binding *b;
1186 :
1187 2149751 : for (b = s->bindings; b != NULL; b = b->prev)
1188 : {
1189 1917047 : struct c_label_vars *label_vars;
1190 1917047 : struct c_binding *b1;
1191 1917047 : bool hjud;
1192 1917047 : unsigned int ix;
1193 1917047 : struct c_goto_bindings *g;
1194 :
1195 1917047 : if (TREE_CODE (b->decl) != LABEL_DECL)
1196 1412041 : continue;
1197 505006 : label_vars = b->u.label;
1198 :
1199 505006 : b1 = label_vars->label_bindings.bindings_in_scope;
1200 505006 : if (label_vars->label_bindings.scope == NULL)
1201 : hjud = false;
1202 : else
1203 195571 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1204 505006 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1205 : {
1206 : /* This label is defined in this scope. */
1207 54177 : if (hjud)
1208 : {
1209 169690 : for (; b1 != NULL; b1 = b1->prev)
1210 : {
1211 : /* A goto from later in the function to this
1212 : label will never see the initialization
1213 : of B1, if any. Save it to issue a
1214 : warning if needed. */
1215 148267 : if (decl_jump_unsafe (b1->decl))
1216 19464 : vec_safe_push(label_vars->decls_in_scope, b1->decl);
1217 : }
1218 : }
1219 : }
1220 :
1221 : /* Update the bindings of any goto statements associated
1222 : with this label. */
1223 472314439 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1224 470255753 : update_spot_bindings (scope, &g->goto_bindings);
1225 : }
1226 : }
1227 :
1228 : /* Don't search beyond the current function. */
1229 280551927 : if (s == current_function_scope)
1230 : break;
1231 :
1232 239485483 : s = s->outer;
1233 : }
1234 91818644 : }
1235 :
1236 : /* Exit a scope. Restore the state of the identifier-decl mappings
1237 : that were in effect when this scope was entered. Return a BLOCK
1238 : node containing all the DECLs in this scope that are of interest
1239 : to debug info generation. */
1240 :
1241 : tree
1242 91818644 : pop_scope (void)
1243 : {
1244 91818644 : struct c_scope *scope = current_scope;
1245 91818644 : tree block, context, p;
1246 91818644 : struct c_binding *b;
1247 :
1248 91818644 : bool functionbody = scope->function_body;
1249 91818644 : bool keep = functionbody || scope->keep || scope->bindings;
1250 :
1251 91818644 : update_label_decls (scope);
1252 :
1253 : /* If appropriate, create a BLOCK to record the decls for the life
1254 : of this function. */
1255 91818644 : block = NULL_TREE;
1256 91818644 : if (keep)
1257 : {
1258 36789264 : block = make_node (BLOCK);
1259 36789264 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1260 36789264 : TREE_USED (block) = 1;
1261 :
1262 : /* In each subblock, record that this is its superior. */
1263 37068637 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1264 279373 : BLOCK_SUPERCONTEXT (p) = block;
1265 :
1266 36789264 : BLOCK_VARS (block) = NULL_TREE;
1267 : }
1268 :
1269 : /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1270 : scope must be set so that they point to the appropriate
1271 : construct, i.e. either to the current FUNCTION_DECL node, or
1272 : else to the BLOCK node we just constructed.
1273 :
1274 : Note that for tagged types whose scope is just the formal
1275 : parameter list for some function type specification, we can't
1276 : properly set their TYPE_CONTEXTs here, because we don't have a
1277 : pointer to the appropriate FUNCTION_TYPE node readily available
1278 : to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1279 : type nodes get set in `grokdeclarator' as soon as we have created
1280 : the FUNCTION_TYPE node which will represent the "scope" for these
1281 : "parameter list local" tagged types. */
1282 91818644 : if (scope->function_body)
1283 36301431 : context = current_function_decl;
1284 55517213 : else if (scope == file_scope)
1285 : {
1286 104231 : tree file_decl
1287 104231 : = build_translation_unit_decl (get_identifier (main_input_filename));
1288 104231 : context = file_decl;
1289 104231 : debug_hooks->register_main_translation_unit (file_decl);
1290 : }
1291 : else
1292 : context = block;
1293 :
1294 : /* Clear all bindings in this scope. */
1295 842079314 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1296 : {
1297 750260670 : p = b->decl;
1298 750260670 : switch (TREE_CODE (p))
1299 : {
1300 23949 : case LABEL_DECL:
1301 : /* Warnings for unused labels, errors for undefined labels. */
1302 23949 : if (TREE_USED (p) && !DECL_INITIAL (p))
1303 : {
1304 63 : error ("label %q+D used but not defined", p);
1305 63 : DECL_INITIAL (p) = error_mark_node;
1306 : }
1307 : else
1308 23886 : warn_for_unused_label (p);
1309 :
1310 : /* Labels go in BLOCK_VARS. */
1311 23949 : DECL_CHAIN (p) = BLOCK_VARS (block);
1312 23949 : BLOCK_VARS (block) = p;
1313 23949 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1314 23949 : I_LABEL_BINDING (b->id) = b->shadowed;
1315 :
1316 : /* Also pop back to the shadowed label_vars. */
1317 23949 : release_tree_vector (b->u.label->decls_in_scope);
1318 23949 : b->u.label = b->u.label->shadowed;
1319 23949 : break;
1320 :
1321 1425721 : case ENUMERAL_TYPE:
1322 1425721 : case UNION_TYPE:
1323 1425721 : case RECORD_TYPE:
1324 :
1325 : /* Types may not have tag-names, in which case the type
1326 : appears in the bindings list with b->id NULL. */
1327 1425721 : if (b->id)
1328 : {
1329 624532 : gcc_assert (I_TAG_BINDING (b->id) == b);
1330 624532 : I_TAG_BINDING (b->id) = b->shadowed;
1331 : }
1332 : break;
1333 :
1334 620541239 : case FUNCTION_DECL:
1335 : /* Propagate TREE_ADDRESSABLE from nested functions to their
1336 : containing functions. */
1337 620541239 : if (!TREE_ASM_WRITTEN (p)
1338 620541239 : && DECL_INITIAL (p) != NULL_TREE
1339 72244270 : && TREE_ADDRESSABLE (p)
1340 3689634 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1341 620541239 : && DECL_ABSTRACT_ORIGIN (p) != p)
1342 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1343 620541239 : if (!TREE_PUBLIC (p)
1344 358009 : && !DECL_INITIAL (p)
1345 175 : && !b->nested
1346 144 : && scope != file_scope
1347 620541247 : && scope != external_scope)
1348 : {
1349 8 : error ("nested function %q+D declared but never defined", p);
1350 8 : undef_nested_function = true;
1351 : }
1352 620541231 : else if (DECL_DECLARED_INLINE_P (p)
1353 71061147 : && TREE_PUBLIC (p)
1354 691443781 : && !DECL_INITIAL (p))
1355 : {
1356 : /* C99 6.7.4p6: "a function with external linkage... declared
1357 : with an inline function specifier ... shall also be defined
1358 : in the same translation unit." */
1359 76 : if (!flag_gnu89_inline
1360 51 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1361 111 : && scope == external_scope)
1362 13 : pedwarn (input_location, 0,
1363 : "inline function %q+D declared but never defined", p);
1364 76 : DECL_EXTERNAL (p) = 1;
1365 : }
1366 :
1367 620541239 : goto common_symbol;
1368 :
1369 10945881 : case VAR_DECL:
1370 : /* Warnings for unused variables. */
1371 8382748 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1372 2592054 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1373 2591546 : && !DECL_IN_SYSTEM_HEADER (p)
1374 2512483 : && DECL_NAME (p)
1375 2512483 : && !DECL_ARTIFICIAL (p)
1376 2512116 : && scope != file_scope
1377 12570364 : && scope != external_scope)
1378 : {
1379 738488 : if (!TREE_USED (p))
1380 : {
1381 732811 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1382 732811 : suppress_warning (p, OPT_Wunused_variable);
1383 : }
1384 5677 : else if (DECL_CONTEXT (p) == current_function_decl)
1385 5656 : warning_at (DECL_SOURCE_LOCATION (p),
1386 5656 : OPT_Wunused_but_set_variable_,
1387 : "variable %qD set but not used", p);
1388 : }
1389 :
1390 10945881 : if (b->inner_comp)
1391 : {
1392 2 : error ("type of array %q+D completed incompatibly with"
1393 : " implicit initialization", p);
1394 : }
1395 :
1396 : /* Fall through. */
1397 648791355 : case TYPE_DECL:
1398 648791355 : case CONST_DECL:
1399 10945879 : common_symbol:
1400 : /* All of these go in BLOCK_VARS, but only if this is the
1401 : binding in the home scope. */
1402 648791355 : if (!b->nested)
1403 : {
1404 390477994 : DECL_CHAIN (p) = BLOCK_VARS (block);
1405 390477994 : BLOCK_VARS (block) = p;
1406 : }
1407 258313361 : else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1408 : {
1409 : /* For block local externs add a special
1410 : DECL_EXTERNAL decl for debug info generation. */
1411 14595 : tree extp = copy_node (p);
1412 :
1413 14595 : DECL_EXTERNAL (extp) = 1;
1414 14595 : TREE_STATIC (extp) = 0;
1415 14595 : TREE_PUBLIC (extp) = 1;
1416 14595 : DECL_INITIAL (extp) = NULL_TREE;
1417 14595 : DECL_LANG_SPECIFIC (extp) = NULL;
1418 14595 : DECL_CONTEXT (extp) = current_function_decl;
1419 14595 : if (TREE_CODE (p) == FUNCTION_DECL)
1420 : {
1421 13056 : DECL_RESULT (extp) = NULL_TREE;
1422 13056 : DECL_SAVED_TREE (extp) = NULL_TREE;
1423 13056 : DECL_STRUCT_FUNCTION (extp) = NULL;
1424 : }
1425 14595 : if (b->locus != UNKNOWN_LOCATION)
1426 14595 : DECL_SOURCE_LOCATION (extp) = b->locus;
1427 14595 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1428 14595 : BLOCK_VARS (block) = extp;
1429 : }
1430 : /* If this is the file scope set DECL_CONTEXT of each decl to
1431 : the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1432 : work. */
1433 648791355 : if (scope == file_scope)
1434 269667516 : DECL_CONTEXT (p) = context;
1435 :
1436 748811000 : gcc_fallthrough ();
1437 : /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1438 : already been put there by store_parm_decls. Unused-
1439 : parameter warnings are handled by function.cc.
1440 : error_mark_node obviously does not go in BLOCK_VARS and
1441 : does not get unused-variable warnings. */
1442 748811000 : case PARM_DECL:
1443 748811000 : case ERROR_MARK:
1444 : /* It is possible for a decl not to have a name. We get
1445 : here with b->id NULL in this case. */
1446 748811000 : if (b->id)
1447 : {
1448 745625460 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1449 745625460 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1450 745625460 : if (b->shadowed && b->shadowed->u.type)
1451 4654684 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1452 : }
1453 : break;
1454 :
1455 0 : default:
1456 0 : gcc_unreachable ();
1457 : }
1458 : }
1459 :
1460 :
1461 : /* Dispose of the block that we just made inside some higher level. */
1462 91818644 : if ((scope->function_body || scope == file_scope) && context)
1463 : {
1464 36405660 : DECL_INITIAL (context) = block;
1465 36405660 : BLOCK_SUPERCONTEXT (block) = context;
1466 : }
1467 55412984 : else if (scope->outer)
1468 : {
1469 55308753 : if (block)
1470 279373 : SCOPE_LIST_APPEND (scope->outer, blocks, block);
1471 : /* If we did not make a block for the scope just exited, any
1472 : blocks made for inner scopes must be carried forward so they
1473 : will later become subblocks of something else. */
1474 55029380 : else if (scope->blocks)
1475 358691 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1476 : }
1477 :
1478 : /* Pop the current scope, and free the structure for reuse. */
1479 91818644 : current_scope = scope->outer;
1480 91818644 : if (scope->function_body)
1481 36301431 : current_function_scope = scope->outer_function;
1482 :
1483 91818644 : memset (scope, 0, sizeof (struct c_scope));
1484 91818644 : scope->outer = scope_freelist;
1485 91818644 : scope_freelist = scope;
1486 :
1487 91818644 : return block;
1488 : }
1489 :
1490 : void
1491 104733 : push_file_scope (void)
1492 : {
1493 104733 : tree decl;
1494 :
1495 104733 : if (file_scope)
1496 : return;
1497 :
1498 104733 : push_scope ();
1499 104733 : file_scope = current_scope;
1500 :
1501 104733 : start_fname_decls ();
1502 :
1503 208994143 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1504 208889410 : bind (DECL_NAME (decl), decl, file_scope,
1505 208889410 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1506 : }
1507 :
1508 : void
1509 104574 : pop_file_scope (void)
1510 : {
1511 : /* In case there were missing closebraces, get us back to the global
1512 : binding level. */
1513 104579 : while (current_scope != file_scope)
1514 5 : pop_scope ();
1515 :
1516 : /* __FUNCTION__ is defined at file scope (""). This
1517 : call may not be necessary as my tests indicate it
1518 : still works without it. */
1519 104574 : finish_fname_decls ();
1520 :
1521 104574 : check_inline_statics ();
1522 :
1523 : /* This is the point to write out a PCH if we're doing that.
1524 : In that case we do not want to do anything else. */
1525 104574 : if (pch_file)
1526 : {
1527 343 : c_common_write_pch ();
1528 : /* Ensure even the callers don't try to finalize the CU. */
1529 343 : flag_syntax_only = 1;
1530 343 : return;
1531 : }
1532 :
1533 : /* Pop off the file scope and close this translation unit. */
1534 104231 : pop_scope ();
1535 104231 : file_scope = 0;
1536 :
1537 104231 : maybe_apply_pending_pragma_weaks ();
1538 : }
1539 :
1540 : /* Whether we are curently inside the initializer for an
1541 : underspecified object definition (C23 auto or constexpr). */
1542 : static bool in_underspecified_init;
1543 :
1544 : /* Start an underspecified object definition for NAME at LOC. This
1545 : means that NAME is shadowed inside its initializer, so neither the
1546 : definition being initialized, nor any definition from an outer
1547 : scope, may be referenced during that initializer. Return state to
1548 : be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1549 : underspecified object is a (constexpr) compound literal; there is
1550 : no shadowing in that case, but all the other restrictions on
1551 : underspecified object definitions still apply. */
1552 : unsigned int
1553 661 : start_underspecified_init (location_t loc, tree name)
1554 : {
1555 661 : bool prev = in_underspecified_init;
1556 661 : bool ok;
1557 661 : if (name == NULL_TREE)
1558 : ok = true;
1559 : else
1560 : {
1561 427 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1562 427 : C_DECL_UNDERSPECIFIED (decl) = 1;
1563 427 : struct c_scope *scope = current_scope;
1564 427 : struct c_binding *b = I_SYMBOL_BINDING (name);
1565 427 : if (b && B_IN_SCOPE (b, scope))
1566 : {
1567 4 : error_at (loc, "underspecified declaration of %qE, which is already "
1568 : "declared in this scope", name);
1569 4 : ok = false;
1570 : }
1571 : else
1572 : {
1573 423 : bind (name, decl, scope, false, false, loc);
1574 423 : ok = true;
1575 : }
1576 : }
1577 661 : in_underspecified_init = true;
1578 661 : return ok | (prev << 1);
1579 : }
1580 :
1581 : /* Finish an underspecified object definition for NAME, before that
1582 : name is bound to the real declaration instead of a placeholder.
1583 : PREV_STATE is the value returned by the call to
1584 : start_underspecified_init. If NAME is NULL_TREE, this means a
1585 : compound literal, as for start_underspecified_init. */
1586 : void
1587 661 : finish_underspecified_init (tree name, unsigned int prev_state)
1588 : {
1589 661 : if (name != NULL_TREE && (prev_state & 1))
1590 : {
1591 : /* A VAR_DECL was bound to the name to shadow any previous
1592 : declarations for the name; remove that binding now. */
1593 423 : struct c_scope *scope = current_scope;
1594 423 : struct c_binding *b = I_SYMBOL_BINDING (name);
1595 423 : gcc_assert (b);
1596 423 : gcc_assert (B_IN_SCOPE (b, scope));
1597 423 : gcc_assert (VAR_P (b->decl));
1598 423 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1599 423 : I_SYMBOL_BINDING (name) = b->shadowed;
1600 : /* In erroneous cases there may be other bindings added to this
1601 : scope during the initializer. */
1602 423 : struct c_binding **p = &scope->bindings;
1603 478 : while (*p != b)
1604 55 : p = &((*p)->prev);
1605 423 : *p = free_binding_and_advance (*p);
1606 : }
1607 661 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1608 661 : }
1609 :
1610 : /* Adjust the bindings for the start of a statement expression. */
1611 :
1612 : void
1613 34875 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1614 : {
1615 34875 : struct c_scope *scope;
1616 :
1617 235546 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1618 : {
1619 200671 : struct c_binding *b;
1620 :
1621 200671 : if (!scope->has_label_bindings)
1622 198264 : continue;
1623 :
1624 12756 : for (b = scope->bindings; b != NULL; b = b->prev)
1625 : {
1626 10349 : struct c_label_vars *label_vars;
1627 10349 : unsigned int ix;
1628 10349 : struct c_goto_bindings *g;
1629 :
1630 10349 : if (TREE_CODE (b->decl) != LABEL_DECL)
1631 1588 : continue;
1632 8761 : label_vars = b->u.label;
1633 8761 : ++label_vars->label_bindings.stmt_exprs;
1634 12885 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1635 1795 : ++g->goto_bindings.stmt_exprs;
1636 : }
1637 : }
1638 :
1639 34875 : if (switch_bindings != NULL)
1640 173 : ++switch_bindings->stmt_exprs;
1641 34875 : }
1642 :
1643 : /* Adjust the bindings for the end of a statement expression. */
1644 :
1645 : void
1646 34875 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1647 : {
1648 34875 : struct c_scope *scope;
1649 :
1650 200671 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1651 : {
1652 165796 : struct c_binding *b;
1653 :
1654 165796 : if (!scope->has_label_bindings)
1655 163154 : continue;
1656 :
1657 16232 : for (b = scope->bindings; b != NULL; b = b->prev)
1658 : {
1659 13590 : struct c_label_vars *label_vars;
1660 13590 : unsigned int ix;
1661 13590 : struct c_goto_bindings *g;
1662 :
1663 13590 : if (TREE_CODE (b->decl) != LABEL_DECL)
1664 1648 : continue;
1665 11942 : label_vars = b->u.label;
1666 11942 : --label_vars->label_bindings.stmt_exprs;
1667 11942 : if (label_vars->label_bindings.stmt_exprs < 0)
1668 : {
1669 3322 : label_vars->label_bindings.left_stmt_expr = true;
1670 3322 : label_vars->label_bindings.stmt_exprs = 0;
1671 : }
1672 16118 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1673 : {
1674 1791 : --g->goto_bindings.stmt_exprs;
1675 1791 : if (g->goto_bindings.stmt_exprs < 0)
1676 : {
1677 128 : g->goto_bindings.left_stmt_expr = true;
1678 128 : g->goto_bindings.stmt_exprs = 0;
1679 : }
1680 : }
1681 : }
1682 : }
1683 :
1684 34875 : if (switch_bindings != NULL)
1685 : {
1686 173 : --switch_bindings->stmt_exprs;
1687 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1688 : }
1689 34875 : }
1690 :
1691 : /* Push a definition or a declaration of struct, union or enum tag "name".
1692 : "type" should be the type node.
1693 : We assume that the tag "name" is not already defined, and has a location
1694 : of LOC.
1695 :
1696 : Note that the definition may really be just a forward reference.
1697 : In that case, the TYPE_SIZE will be zero. */
1698 :
1699 : static void
1700 1429678 : pushtag (location_t loc, tree name, tree type)
1701 : {
1702 : /* Record the identifier as the type's name if it has none. */
1703 1429678 : if (name && !TYPE_NAME (type))
1704 626330 : TYPE_NAME (type) = name;
1705 1429678 : bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1706 :
1707 : /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1708 : tagged type we just added to the current scope. This fake
1709 : NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1710 : to output a representation of a tagged type, and it also gives
1711 : us a convenient place to record the "scope start" address for the
1712 : tagged type. */
1713 :
1714 1429678 : TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1715 : TYPE_DECL, NULL_TREE, type));
1716 :
1717 : /* An approximation for now, so we can tell this is a function-scope tag.
1718 : This will be updated in pop_scope. */
1719 1429678 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1720 :
1721 1429678 : if (warn_cxx_compat && name != NULL_TREE)
1722 : {
1723 629 : struct c_binding *b = I_SYMBOL_BINDING (name);
1724 :
1725 629 : if (b != NULL
1726 12 : && b->decl != NULL_TREE
1727 12 : && TREE_CODE (b->decl) == TYPE_DECL
1728 11 : && (B_IN_CURRENT_SCOPE (b)
1729 6 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1730 634 : && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1731 5 : != TYPE_MAIN_VARIANT (type)))
1732 : {
1733 5 : auto_diagnostic_group d;
1734 6 : if (warning_at (loc, OPT_Wc___compat,
1735 : "using %qD as both a typedef and a tag is "
1736 : "invalid in C++", b->decl)
1737 5 : && b->locus != UNKNOWN_LOCATION)
1738 4 : inform (b->locus, "originally defined here");
1739 5 : }
1740 : }
1741 1429678 : }
1742 :
1743 : /* An exported interface to pushtag. This is used by the gdb plugin's
1744 : binding oracle to introduce a new tag binding. */
1745 :
1746 : void
1747 0 : c_pushtag (location_t loc, tree name, tree type)
1748 : {
1749 0 : pushtag (loc, name, type);
1750 0 : }
1751 :
1752 : /* An exported interface to bind a declaration. LOC is the location
1753 : to use. DECL is the declaration to bind. The decl's name is used
1754 : to determine how it is bound. If DECL is a VAR_DECL, then
1755 : IS_GLOBAL determines whether the decl is put into the global (file
1756 : and external) scope or the current function's scope; if DECL is not
1757 : a VAR_DECL then it is always put into the file scope. */
1758 :
1759 : void
1760 0 : c_bind (location_t loc, tree decl, bool is_global)
1761 : {
1762 0 : struct c_scope *scope;
1763 0 : bool nested = false;
1764 :
1765 0 : if (!VAR_P (decl) || current_function_scope == NULL)
1766 : {
1767 : /* Types and functions are always considered to be global. */
1768 0 : scope = file_scope;
1769 0 : DECL_EXTERNAL (decl) = 1;
1770 0 : TREE_PUBLIC (decl) = 1;
1771 : }
1772 0 : else if (is_global)
1773 : {
1774 : /* Also bind it into the external scope. */
1775 0 : bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1776 0 : nested = true;
1777 0 : scope = file_scope;
1778 0 : DECL_EXTERNAL (decl) = 1;
1779 0 : TREE_PUBLIC (decl) = 1;
1780 : }
1781 : else
1782 : {
1783 0 : DECL_CONTEXT (decl) = current_function_decl;
1784 0 : TREE_PUBLIC (decl) = 0;
1785 0 : scope = current_function_scope;
1786 : }
1787 :
1788 0 : bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1789 0 : }
1790 :
1791 :
1792 : /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1793 : it is) seen in a declaration of a file I/O etc. built-in, corresponding
1794 : to the builtin_structptr_types array. Subsequent declarations of such
1795 : built-ins are expected to refer to it rather than to fileptr_type_node,
1796 : etc. which is just void* (or to any other type).
1797 : Used only by match_builtin_function_types. */
1798 :
1799 : static const unsigned builtin_structptr_type_count
1800 : = ARRAY_SIZE (builtin_structptr_types);
1801 :
1802 : static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1803 :
1804 : /* Returns true if types T1 and T2 representing return types or types
1805 : of function arguments are close enough to be considered interchangeable
1806 : in redeclarations of built-in functions. */
1807 :
1808 : static bool
1809 623547 : types_close_enough_to_match (tree t1, tree t2)
1810 : {
1811 623547 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1812 623092 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1813 2492670 : && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1814 : }
1815 :
1816 : /* Subroutine of compare_decls. Allow harmless mismatches in return
1817 : and argument types provided that the type modes match. Set *STRICT
1818 : and *ARGNO to the expected argument type and number in case of
1819 : an argument type mismatch or null and zero otherwise. Return
1820 : a unified type given a suitable match, and 0 otherwise. */
1821 :
1822 : static tree
1823 144628 : match_builtin_function_types (tree newtype, tree oldtype,
1824 : tree *strict, unsigned *argno)
1825 : {
1826 144628 : *argno = 0;
1827 144628 : *strict = NULL_TREE;
1828 :
1829 : /* Accept the return type of the new declaration if it has the same
1830 : mode and if they're both pointers or if neither is. */
1831 144628 : tree oldrettype = TREE_TYPE (oldtype);
1832 144628 : tree newrettype = TREE_TYPE (newtype);
1833 :
1834 144628 : if (!types_close_enough_to_match (oldrettype, newrettype))
1835 : return NULL_TREE;
1836 :
1837 : /* Check that the return types are compatible but don't fail if they
1838 : are not (e.g., int vs long in ILP32) and just let the caller know. */
1839 144266 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1840 144266 : TYPE_MAIN_VARIANT (newrettype)))
1841 46 : *strict = oldrettype;
1842 :
1843 144266 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1844 144266 : tree newargs = TYPE_ARG_TYPES (newtype);
1845 144266 : tree tryargs = newargs;
1846 :
1847 144266 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1848 144266 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1849 :
1850 144266 : gcc_checking_assert (nlst == nbst);
1851 :
1852 622988 : for (unsigned i = 1; oldargs || newargs; ++i)
1853 : {
1854 478978 : if (!oldargs
1855 478978 : || !newargs
1856 478923 : || !TREE_VALUE (oldargs)
1857 957901 : || !TREE_VALUE (newargs))
1858 : return NULL_TREE;
1859 :
1860 478923 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1861 478923 : tree newtype = TREE_VALUE (newargs);
1862 478923 : if (newtype == error_mark_node)
1863 : return NULL_TREE;
1864 478919 : newtype = TYPE_MAIN_VARIANT (newtype);
1865 :
1866 478919 : if (!types_close_enough_to_match (oldtype, newtype))
1867 : return NULL_TREE;
1868 :
1869 478775 : unsigned j = nbst;
1870 478775 : if (POINTER_TYPE_P (oldtype))
1871 : /* Iterate over well-known struct types like FILE (whose types
1872 : aren't known to us) and compare the pointer to each to
1873 : the pointer argument. */
1874 977972 : for (j = 0; j < nbst; ++j)
1875 : {
1876 861588 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1877 717793 : continue;
1878 : /* Store the first FILE* etc. argument type (whatever it is), and
1879 : expect any subsequent declarations of file I/O etc. built-ins
1880 : to refer to it rather than to fileptr_type_node etc. which is
1881 : just void* (or const void*). */
1882 143795 : if (last_structptr_types[j])
1883 : {
1884 126317 : if (!comptypes (last_structptr_types[j], newtype))
1885 : {
1886 2 : *argno = i;
1887 2 : *strict = last_structptr_types[j];
1888 : }
1889 : }
1890 : else
1891 17478 : last_structptr_types[j] = newtype;
1892 : break;
1893 : }
1894 :
1895 478775 : if (j == nbst && !comptypes (oldtype, newtype))
1896 : {
1897 243 : if (POINTER_TYPE_P (oldtype))
1898 : {
1899 : /* For incompatible pointers, only reject differences in
1900 : the unqualified variants of the referenced types but
1901 : consider differences in qualifiers as benign (report
1902 : those to caller via *STRICT below). */
1903 204 : tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1904 204 : tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1905 204 : if (!comptypes (oldref, newref))
1906 : return NULL_TREE;
1907 : }
1908 :
1909 190 : if (!*strict)
1910 : {
1911 186 : *argno = i;
1912 186 : *strict = oldtype;
1913 : }
1914 : }
1915 :
1916 478722 : oldargs = TREE_CHAIN (oldargs);
1917 478722 : newargs = TREE_CHAIN (newargs);
1918 : }
1919 :
1920 144010 : tree trytype = c_build_function_type (newrettype, tryargs);
1921 :
1922 : /* Allow declaration to change transaction_safe attribute. */
1923 144010 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1924 144010 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1925 144010 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1926 144010 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1927 144010 : if (oldtsafe && !newtsafe)
1928 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1929 144010 : else if (newtsafe && !oldtsafe)
1930 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1931 : NULL_TREE, oldattrs);
1932 :
1933 144010 : return c_build_type_attribute_variant (trytype, oldattrs);
1934 : }
1935 :
1936 : /* Subroutine of diagnose_mismatched_decls. Check for function type
1937 : mismatch involving an empty arglist vs a nonempty one and give clearer
1938 : diagnostics. */
1939 : static void
1940 176 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1941 : tree newtype, tree oldtype)
1942 : {
1943 176 : tree t;
1944 :
1945 176 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1946 98 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1947 303 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1948 68 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1949 159 : return;
1950 :
1951 17 : t = TYPE_ARG_TYPES (oldtype);
1952 17 : if (t == NULL_TREE)
1953 9 : t = TYPE_ARG_TYPES (newtype);
1954 18 : for (; t; t = TREE_CHAIN (t))
1955 : {
1956 18 : tree type = TREE_VALUE (t);
1957 :
1958 18 : if (TREE_CHAIN (t) == NULL_TREE
1959 18 : && TYPE_MAIN_VARIANT (type) != void_type_node)
1960 : {
1961 10 : inform (input_location, "a parameter list with an ellipsis "
1962 : "cannot match an empty parameter name list declaration");
1963 10 : break;
1964 : }
1965 :
1966 8 : if (!error_operand_p (type)
1967 8 : && c_type_promotes_to (type) != type)
1968 : {
1969 7 : inform (input_location, "an argument type that has a default "
1970 : "promotion cannot match an empty parameter name list "
1971 : "declaration");
1972 7 : break;
1973 : }
1974 : }
1975 : }
1976 :
1977 : /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1978 : old-style function definition, NEWDECL is a prototype declaration.
1979 : Diagnose inconsistencies in the argument list. Returns TRUE if
1980 : the prototype is compatible, FALSE if not. */
1981 : static bool
1982 19 : validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1983 : {
1984 19 : tree newargs, oldargs;
1985 19 : int i;
1986 :
1987 : #define END_OF_ARGLIST(t) ((t) == void_type_node)
1988 :
1989 19 : oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1990 19 : newargs = TYPE_ARG_TYPES (newtype);
1991 19 : i = 1;
1992 :
1993 45 : for (;;)
1994 : {
1995 32 : tree oldargtype = TREE_VALUE (oldargs);
1996 32 : tree newargtype = TREE_VALUE (newargs);
1997 :
1998 32 : if (oldargtype == error_mark_node || newargtype == error_mark_node)
1999 : return false;
2000 :
2001 58 : oldargtype = (TYPE_ATOMIC (oldargtype)
2002 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
2003 : TYPE_QUAL_ATOMIC)
2004 27 : : TYPE_MAIN_VARIANT (oldargtype));
2005 60 : newargtype = (TYPE_ATOMIC (newargtype)
2006 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2007 : TYPE_QUAL_ATOMIC)
2008 29 : : TYPE_MAIN_VARIANT (newargtype));
2009 :
2010 31 : if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2011 : break;
2012 :
2013 : /* Reaching the end of just one list means the two decls don't
2014 : agree on the number of arguments. */
2015 22 : if (END_OF_ARGLIST (oldargtype))
2016 : {
2017 2 : error ("prototype for %q+D declares more arguments "
2018 : "than previous old-style definition", newdecl);
2019 2 : return false;
2020 : }
2021 20 : else if (END_OF_ARGLIST (newargtype))
2022 : {
2023 2 : error ("prototype for %q+D declares fewer arguments "
2024 : "than previous old-style definition", newdecl);
2025 2 : return false;
2026 : }
2027 :
2028 : /* Type for passing arg must be consistent with that declared
2029 : for the arg. */
2030 18 : else if (!comptypes (oldargtype, newargtype))
2031 : {
2032 5 : error ("prototype for %q+D declares argument %d"
2033 : " with incompatible type",
2034 : newdecl, i);
2035 5 : return false;
2036 : }
2037 :
2038 13 : oldargs = TREE_CHAIN (oldargs);
2039 13 : newargs = TREE_CHAIN (newargs);
2040 13 : i++;
2041 13 : }
2042 :
2043 : /* If we get here, no errors were found, but do issue a warning
2044 : for this poor-style construct. */
2045 9 : warning (0, "prototype for %q+D follows non-prototype definition",
2046 : newdecl);
2047 9 : return true;
2048 : #undef END_OF_ARGLIST
2049 : }
2050 :
2051 : /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2052 : first in a pair of mismatched declarations, using the diagnostic
2053 : function DIAG. */
2054 : static void
2055 416 : locate_old_decl (tree decl)
2056 : {
2057 416 : if (TREE_CODE (decl) == FUNCTION_DECL
2058 198 : && fndecl_built_in_p (decl)
2059 419 : && !C_DECL_DECLARED_BUILTIN (decl))
2060 : ;
2061 416 : else if (DECL_INITIAL (decl))
2062 119 : inform (input_location,
2063 : "previous definition of %q+D with type %qT",
2064 119 : decl, TREE_TYPE (decl));
2065 297 : else if (C_DECL_IMPLICIT (decl))
2066 16 : inform (input_location,
2067 : "previous implicit declaration of %q+D with type %qT",
2068 16 : decl, TREE_TYPE (decl));
2069 : else
2070 281 : inform (input_location,
2071 : "previous declaration of %q+D with type %qT",
2072 281 : decl, TREE_TYPE (decl));
2073 416 : }
2074 :
2075 :
2076 : /* Helper function. For a tagged type, it finds the declaration
2077 : for a visible tag declared in the same scope if such a
2078 : declaration exists. */
2079 : static tree
2080 1139523 : previous_tag (tree type)
2081 : {
2082 1139523 : struct c_binding *b = NULL;
2083 1139523 : tree name = c_type_tag (type);
2084 :
2085 1139523 : if (name)
2086 566114 : b = I_TAG_BINDING (name);
2087 :
2088 566114 : if (b)
2089 566114 : b = b->shadowed;
2090 :
2091 1139523 : if (b && B_IN_CURRENT_SCOPE (b))
2092 176 : return b->decl;
2093 :
2094 : return NULL_TREE;
2095 : }
2096 :
2097 : /* Subroutine to mark functions as versioned when using the attribute
2098 : 'target_version'. */
2099 :
2100 : static void
2101 0 : maybe_mark_function_versioned (tree decl)
2102 : {
2103 0 : if (!DECL_FUNCTION_VERSIONED (decl))
2104 : {
2105 : /* Check if the name of the function has been overridden. */
2106 0 : if (DECL_ASSEMBLER_NAME_SET_P (decl)
2107 0 : && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))[0] == '*')
2108 0 : error_at (DECL_SOURCE_LOCATION (decl),
2109 : "cannot use function multiversioning on a renamed function");
2110 :
2111 : /* We need to insert function version now to make sure the correct
2112 : pre-mangled assembler name is recorded. */
2113 0 : cgraph_node *node = cgraph_node::get_create (decl);
2114 :
2115 0 : if (!node->function_version ())
2116 0 : node->insert_new_function_version ();
2117 :
2118 0 : DECL_FUNCTION_VERSIONED (decl) = 1;
2119 :
2120 0 : tree mangled_name
2121 0 : = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
2122 0 : SET_DECL_ASSEMBLER_NAME (decl, mangled_name);
2123 : }
2124 0 : }
2125 :
2126 : /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2127 : Returns true if the caller should proceed to merge the two, false
2128 : if OLDDECL should simply be discarded. As a side effect, issues
2129 : all necessary diagnostics for invalid or poor-style combinations.
2130 : If it returns true, writes the types of NEWDECL and OLDDECL to
2131 : *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2132 : TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2133 :
2134 : static bool
2135 4981116 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2136 : tree *newtypep, tree *oldtypep)
2137 : {
2138 4981116 : tree newtype, oldtype;
2139 4981116 : bool retval = true;
2140 :
2141 : #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2142 : && DECL_EXTERNAL (DECL))
2143 :
2144 : /* If we have error_mark_node for either decl or type, just discard
2145 : the previous decl - we're in an error cascade already. */
2146 4981116 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2147 : return false;
2148 4981096 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2149 4981096 : *newtypep = newtype = TREE_TYPE (newdecl);
2150 4981096 : if (oldtype == error_mark_node || newtype == error_mark_node)
2151 : return false;
2152 :
2153 : /* Two different categories of symbol altogether. This is an error
2154 : unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2155 4981088 : if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2156 : {
2157 30 : if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2158 18 : && fndecl_built_in_p (olddecl)
2159 15 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2160 : {
2161 16 : auto_diagnostic_group d;
2162 16 : error ("%q+D redeclared as different kind of symbol", newdecl);
2163 16 : locate_old_decl (olddecl);
2164 16 : }
2165 14 : else if (TREE_PUBLIC (newdecl))
2166 3 : warning (OPT_Wbuiltin_declaration_mismatch,
2167 : "built-in function %q+D declared as non-function",
2168 : newdecl);
2169 : else
2170 11 : warning (OPT_Wshadow, "declaration of %q+D shadows "
2171 : "a built-in function", newdecl);
2172 30 : return false;
2173 : }
2174 :
2175 : /* Enumerators have no linkage, so may only be declared once in a
2176 : given scope. */
2177 4981058 : if (TREE_CODE (olddecl) == CONST_DECL)
2178 : {
2179 46 : if (flag_isoc23
2180 45 : && TYPE_NAME (DECL_CONTEXT (newdecl))
2181 43 : && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2182 87 : && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2183 : {
2184 38 : if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2185 : {
2186 1 : auto_diagnostic_group d;
2187 1 : error ("conflicting redeclaration of enumerator %q+D", newdecl);
2188 1 : locate_old_decl (olddecl);
2189 1 : }
2190 : }
2191 : else
2192 : {
2193 8 : auto_diagnostic_group d;
2194 8 : error ("redeclaration of enumerator %q+D", newdecl);
2195 8 : locate_old_decl (olddecl);
2196 8 : }
2197 46 : return false;
2198 : }
2199 :
2200 4981012 : bool pedwarned = false;
2201 4981012 : bool warned = false;
2202 4981012 : bool enum_and_int_p = false;
2203 4981012 : auto_diagnostic_group d;
2204 :
2205 4981012 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2206 : &enum_and_int_p);
2207 4981012 : if (!comptypes_result)
2208 : {
2209 144835 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2210 144757 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2211 289464 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2212 : {
2213 : /* Accept "harmless" mismatches in function types such
2214 : as missing qualifiers or int vs long when they're the same
2215 : size. However, diagnose return and argument types that are
2216 : incompatible according to language rules. */
2217 144628 : tree mismatch_expect;
2218 144628 : unsigned mismatch_argno;
2219 :
2220 144628 : tree trytype = match_builtin_function_types (newtype, oldtype,
2221 : &mismatch_expect,
2222 : &mismatch_argno);
2223 :
2224 144628 : if (trytype && comptypes (newtype, trytype))
2225 144010 : *oldtypep = oldtype = trytype;
2226 : else
2227 : {
2228 : /* If types don't match for a built-in, throw away the
2229 : built-in. No point in calling locate_old_decl here, it
2230 : won't print anything. */
2231 618 : const char *header = header_for_builtin_fn (olddecl);
2232 618 : location_t loc = DECL_SOURCE_LOCATION (newdecl);
2233 1019 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2234 : "conflicting types for built-in function %q+D; "
2235 : "expected %qT",
2236 : newdecl, oldtype)
2237 618 : && header)
2238 : {
2239 : /* Suggest the right header to include as the preferred
2240 : solution rather than the spelling of the declaration. */
2241 217 : rich_location richloc (line_table, loc);
2242 217 : maybe_add_include_fixit (&richloc, header, true);
2243 217 : inform (&richloc,
2244 : "%qD is declared in header %qs", olddecl, header);
2245 217 : }
2246 618 : return false;
2247 : }
2248 :
2249 144010 : if (mismatch_expect && extra_warnings)
2250 : {
2251 5 : location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2252 5 : bool warned = false;
2253 5 : if (mismatch_argno)
2254 5 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2255 : "mismatch in argument %u type of built-in "
2256 : "function %qD; expected %qT",
2257 : mismatch_argno, newdecl, mismatch_expect);
2258 : else
2259 0 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2260 : "mismatch in return type of built-in "
2261 : "function %qD; expected %qT",
2262 : newdecl, mismatch_expect);
2263 5 : const char *header = header_for_builtin_fn (olddecl);
2264 5 : if (warned && header)
2265 : {
2266 5 : rich_location richloc (line_table, newloc);
2267 5 : maybe_add_include_fixit (&richloc, header, true);
2268 5 : inform (&richloc,
2269 : "%qD is declared in header %qs", olddecl, header);
2270 5 : }
2271 : }
2272 : }
2273 207 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2274 207 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2275 : {
2276 : /* A conflicting function declaration for a predeclared
2277 : function that isn't actually built in. Objective C uses
2278 : these. The new declaration silently overrides everything
2279 : but the volatility (i.e. noreturn) indication. See also
2280 : below. FIXME: Make Objective C use normal builtins. */
2281 0 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2282 0 : return false;
2283 : }
2284 : /* Permit void foo (...) to match int foo (...) if the latter is
2285 : the definition and implicit int was used. See
2286 : c-torture/compile/920625-2.c. */
2287 129 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2288 64 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2289 32 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2290 213 : && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2291 : {
2292 5 : pedwarned = pedwarn (input_location, 0,
2293 : "conflicting types for %q+D", newdecl);
2294 : /* Make sure we keep void as the return type. */
2295 5 : TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2296 5 : C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2297 : }
2298 : /* Permit void foo (...) to match an earlier call to foo (...) with
2299 : no declared type (thus, implicitly int). */
2300 202 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2301 124 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2302 92 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2303 225 : && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2304 : {
2305 19 : pedwarned = pedwarn (input_location, 0,
2306 : "conflicting types for %q+D; have %qT",
2307 : newdecl, newtype);
2308 : /* Make sure we keep void as the return type. */
2309 19 : TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2310 : }
2311 : else
2312 : {
2313 183 : int new_quals = TYPE_QUALS (newtype);
2314 183 : int old_quals = TYPE_QUALS (oldtype);
2315 :
2316 183 : if (new_quals != old_quals)
2317 : {
2318 21 : addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2319 21 : addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2320 21 : if (new_addr != old_addr)
2321 : {
2322 0 : if (ADDR_SPACE_GENERIC_P (new_addr))
2323 0 : error ("conflicting named address spaces (generic vs %s) "
2324 : "for %q+D",
2325 : c_addr_space_name (old_addr), newdecl);
2326 0 : else if (ADDR_SPACE_GENERIC_P (old_addr))
2327 0 : error ("conflicting named address spaces (%s vs generic) "
2328 : "for %q+D",
2329 : c_addr_space_name (new_addr), newdecl);
2330 : else
2331 0 : error ("conflicting named address spaces (%s vs %s) "
2332 : "for %q+D",
2333 : c_addr_space_name (new_addr),
2334 : c_addr_space_name (old_addr),
2335 : newdecl);
2336 : }
2337 :
2338 21 : if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2339 21 : != CLEAR_QUAL_ADDR_SPACE (old_quals))
2340 21 : error ("conflicting type qualifiers for %q+D", newdecl);
2341 : }
2342 : else
2343 : {
2344 162 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
2345 : {
2346 93 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (olddecl));
2347 93 : if (attrs && !TYPE_ATTRIBUTES (TREE_TYPE (newdecl)))
2348 : {
2349 : /* Similar to the C++ front-end, for FUNCTION_DECL,
2350 : if OLDDECL has attributes and NEWDECL doesn't,
2351 : try the type with OLDDECL attributes. */
2352 12 : tree rettype = TREE_TYPE (newtype);
2353 12 : tree tryargs = TYPE_ARG_TYPES (newtype);
2354 12 : tree trytype = c_build_function_type (rettype,
2355 : tryargs);
2356 12 : trytype = c_build_type_attribute_variant (trytype,
2357 : attrs);
2358 12 : if (comptypes (oldtype, trytype))
2359 : {
2360 7 : *newtypep = newtype = trytype;
2361 7 : comptypes_result = 1;
2362 : }
2363 : }
2364 : }
2365 :
2366 162 : if (!comptypes_result)
2367 155 : error ("conflicting types for %q+D; have %qT", newdecl,
2368 : newtype);
2369 : }
2370 7 : if (!comptypes_result)
2371 : {
2372 176 : diagnose_arglist_conflict (newdecl, olddecl, newtype,
2373 : oldtype);
2374 176 : locate_old_decl (olddecl);
2375 176 : return false;
2376 : }
2377 : }
2378 : }
2379 : /* Warn about enum/integer type mismatches. They are compatible types
2380 : (C23 6.7.2.2/5), but may pose portability problems. */
2381 4836177 : else if (enum_and_int_p
2382 190 : && TREE_CODE (newdecl) != TYPE_DECL
2383 : /* Don't warn about acc_on_device built-in redeclaration,
2384 : the built-in is declared with int rather than enum because
2385 : the enum isn't intrinsic. */
2386 4836363 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2387 149 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2388 119 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2389 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2390 67 : OPT_Wenum_int_mismatch,
2391 : "conflicting types for %q+D due to enum/integer "
2392 : "mismatch; have %qT", newdecl, newtype);
2393 :
2394 : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2395 : but silently ignore the redeclaration if either is in a system
2396 : header. (Conflicting redeclarations were handled above.) This
2397 : is allowed for C11 if the types are the same, not just
2398 : compatible. */
2399 4980218 : if (TREE_CODE (newdecl) == TYPE_DECL)
2400 : {
2401 45831 : bool types_different = false;
2402 :
2403 45831 : comptypes_result
2404 45831 : = comptypes_check_different_types (oldtype, newtype, &types_different);
2405 :
2406 45831 : if (comptypes_result != 1 || types_different)
2407 : {
2408 11 : error ("redefinition of typedef %q+D with different type", newdecl);
2409 11 : locate_old_decl (olddecl);
2410 11 : return false;
2411 : }
2412 :
2413 45820 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2414 2484 : || DECL_IN_SYSTEM_HEADER (olddecl)
2415 2262 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2416 48082 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2417 43558 : return true; /* Allow OLDDECL to continue in use. */
2418 :
2419 2262 : if (c_type_variably_modified_p (newtype))
2420 : {
2421 3 : error ("redefinition of typedef %q+D with variably modified type",
2422 : newdecl);
2423 3 : locate_old_decl (olddecl);
2424 : }
2425 2259 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2426 : "redefinition of typedef %q+D", newdecl))
2427 8 : locate_old_decl (olddecl);
2428 :
2429 2262 : return true;
2430 : }
2431 :
2432 : /* Function declarations can either be 'static' or 'extern' (no
2433 : qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2434 : can never conflict with each other on account of linkage
2435 : (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2436 : gnu89 mode permits two definitions if one is 'extern inline' and
2437 : one is not. The non- extern-inline definition supersedes the
2438 : extern-inline definition. */
2439 :
2440 4934387 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2441 : {
2442 : /* If you declare a built-in function name as static, or
2443 : define the built-in with an old-style definition (so we
2444 : can't validate the argument list) the built-in definition is
2445 : overridden, but optionally warn this was a bad choice of name. */
2446 4915700 : if (fndecl_built_in_p (olddecl)
2447 8785538 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2448 : {
2449 3611172 : if (!TREE_PUBLIC (newdecl)
2450 3611172 : || (DECL_INITIAL (newdecl)
2451 4855 : && !prototype_p (TREE_TYPE (newdecl))))
2452 : {
2453 103 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2454 103 : OPT_Wshadow, "declaration of %qD shadows "
2455 : "a built-in function", newdecl);
2456 : /* Discard the old built-in function. */
2457 103 : return false;
2458 : }
2459 :
2460 3611069 : if (!prototype_p (TREE_TYPE (newdecl)))
2461 : {
2462 : /* Set for built-ins that take no arguments. */
2463 342 : bool func_void_args = false;
2464 342 : if (tree at = TYPE_ARG_TYPES (oldtype))
2465 342 : func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2466 :
2467 342 : if (extra_warnings && !func_void_args)
2468 47 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2469 47 : OPT_Wbuiltin_declaration_mismatch,
2470 : "declaration of built-in function %qD without "
2471 : "a prototype; expected %qT",
2472 47 : newdecl, TREE_TYPE (olddecl));
2473 : }
2474 : }
2475 :
2476 4915597 : if (DECL_INITIAL (newdecl))
2477 : {
2478 232810 : if (DECL_INITIAL (olddecl))
2479 : {
2480 : /* If the new declaration isn't overriding an extern inline
2481 : reject the new decl. In c99, no overriding is allowed
2482 : in the same translation unit. */
2483 209 : if (!DECL_EXTERN_INLINE (olddecl)
2484 91 : || DECL_EXTERN_INLINE (newdecl)
2485 204 : || (!flag_gnu89_inline
2486 15 : && (!DECL_DECLARED_INLINE_P (olddecl)
2487 15 : || !lookup_attribute ("gnu_inline",
2488 15 : DECL_ATTRIBUTES (olddecl)))
2489 0 : && (!DECL_DECLARED_INLINE_P (newdecl)
2490 0 : || !lookup_attribute ("gnu_inline",
2491 0 : DECL_ATTRIBUTES (newdecl)))))
2492 : {
2493 26 : auto_diagnostic_group d;
2494 26 : error ("redefinition of %q+D", newdecl);
2495 26 : locate_old_decl (olddecl);
2496 26 : return false;
2497 26 : }
2498 : }
2499 : }
2500 : /* If we have a prototype after an old-style function definition,
2501 : the argument types must be checked specially. */
2502 4682787 : else if (DECL_INITIAL (olddecl)
2503 824 : && !prototype_p (oldtype) && prototype_p (newtype)
2504 4682807 : && TYPE_ACTUAL_ARG_TYPES (oldtype))
2505 : {
2506 19 : auto_diagnostic_group d;
2507 19 : if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2508 : {
2509 10 : locate_old_decl (olddecl);
2510 10 : return false;
2511 : }
2512 19 : }
2513 : /* A non-static declaration (even an "extern") followed by a
2514 : static declaration is undefined behavior per C99 6.2.2p3-5,7.
2515 : The same is true for a static forward declaration at block
2516 : scope followed by a non-static declaration/definition at file
2517 : scope. Static followed by non-static at the same scope is
2518 : not undefined behavior, and is the most convenient way to get
2519 : some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2520 : the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2521 : we do diagnose it if -Wtraditional. */
2522 4915561 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2523 : {
2524 : /* Two exceptions to the rule. If olddecl is an extern
2525 : inline, or a predeclared function that isn't actually
2526 : built in, newdecl silently overrides olddecl. The latter
2527 : occur only in Objective C; see also above. (FIXME: Make
2528 : Objective C use normal builtins.) */
2529 19 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2530 38 : && !DECL_EXTERN_INLINE (olddecl))
2531 : {
2532 5 : auto_diagnostic_group d;
2533 5 : error ("static declaration of %q+D follows "
2534 : "non-static declaration", newdecl);
2535 5 : locate_old_decl (olddecl);
2536 5 : }
2537 19 : return false;
2538 : }
2539 4915542 : else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2540 : {
2541 2329 : if (DECL_CONTEXT (olddecl))
2542 : {
2543 0 : auto_diagnostic_group d;
2544 0 : error ("non-static declaration of %q+D follows "
2545 : "static declaration", newdecl);
2546 0 : locate_old_decl (olddecl);
2547 0 : return false;
2548 0 : }
2549 2329 : else if (warn_traditional)
2550 : {
2551 2 : warned |= warning (OPT_Wtraditional,
2552 : "non-static declaration of %q+D "
2553 : "follows static declaration", newdecl);
2554 : }
2555 : }
2556 :
2557 : /* Make sure gnu_inline attribute is either not present, or
2558 : present on all inline decls. */
2559 4915542 : if (DECL_DECLARED_INLINE_P (olddecl)
2560 4916617 : && DECL_DECLARED_INLINE_P (newdecl))
2561 : {
2562 820 : bool newa = lookup_attribute ("gnu_inline",
2563 820 : DECL_ATTRIBUTES (newdecl)) != NULL;
2564 820 : bool olda = lookup_attribute ("gnu_inline",
2565 820 : DECL_ATTRIBUTES (olddecl)) != NULL;
2566 820 : if (newa != olda)
2567 : {
2568 0 : auto_diagnostic_group d;
2569 0 : error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2570 : newa ? newdecl : olddecl);
2571 0 : error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2572 : "but not here");
2573 0 : }
2574 : }
2575 : /* Check if these are unmergable overlapping FMV declarations. */
2576 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2577 : && diagnose_versioned_decls (olddecl, newdecl))
2578 : return false;
2579 : }
2580 18687 : else if (VAR_P (newdecl))
2581 : {
2582 : /* Only variables can be thread-local, and all declarations must
2583 : agree on this property. */
2584 18650 : if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2585 : {
2586 : /* Nothing to check. Since OLDDECL is marked threadprivate
2587 : and NEWDECL does not have a thread-local attribute, we
2588 : will merge the threadprivate attribute into NEWDECL. */
2589 : ;
2590 : }
2591 55737 : else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2592 : {
2593 6 : auto_diagnostic_group d;
2594 6 : if (DECL_THREAD_LOCAL_P (newdecl))
2595 3 : error ("thread-local declaration of %q+D follows "
2596 : "non-thread-local declaration", newdecl);
2597 : else
2598 3 : error ("non-thread-local declaration of %q+D follows "
2599 : "thread-local declaration", newdecl);
2600 :
2601 6 : locate_old_decl (olddecl);
2602 6 : return false;
2603 6 : }
2604 :
2605 : /* Multiple initialized definitions are not allowed (6.9p3,5).
2606 : For this purpose, C23 makes it clear that thread-local
2607 : declarations without extern are definitions, not tentative
2608 : definitions, whether or not they have initializers. The
2609 : wording before C23 was unclear; literally it would have made
2610 : uninitialized thread-local declarations into tentative
2611 : definitions only if they also used static, but without saying
2612 : explicitly whether or not other cases count as
2613 : definitions at all. */
2614 19213 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2615 19212 : || (flag_isoc23
2616 6476 : && DECL_THREAD_LOCAL_P (newdecl)
2617 25 : && !DECL_EXTERNAL (newdecl)
2618 21 : && !DECL_EXTERNAL (olddecl)))
2619 : {
2620 7 : auto_diagnostic_group d;
2621 7 : error ("redefinition of %q+D", newdecl);
2622 7 : locate_old_decl (olddecl);
2623 7 : return false;
2624 7 : }
2625 :
2626 : /* Objects declared at file scope: if the first declaration had
2627 : external linkage (even if it was an external reference) the
2628 : second must have external linkage as well, or the behavior is
2629 : undefined. If the first declaration had internal linkage, then
2630 : the second must too, or else be an external reference (in which
2631 : case the composite declaration still has internal linkage).
2632 : As for function declarations, we warn about the static-then-
2633 : extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2634 18654 : if (DECL_FILE_SCOPE_P (newdecl)
2635 18637 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2636 : {
2637 147 : if (DECL_EXTERNAL (newdecl))
2638 : {
2639 144 : if (!DECL_FILE_SCOPE_P (olddecl))
2640 : {
2641 2 : auto_diagnostic_group d;
2642 2 : error ("extern declaration of %q+D follows "
2643 : "declaration with no linkage", newdecl);
2644 2 : locate_old_decl (olddecl);
2645 2 : return false;
2646 2 : }
2647 142 : else if (warn_traditional)
2648 : {
2649 0 : warned |= warning (OPT_Wtraditional,
2650 : "non-static declaration of %q+D "
2651 : "follows static declaration", newdecl);
2652 : }
2653 : }
2654 : else
2655 : {
2656 3 : auto_diagnostic_group d;
2657 3 : if (TREE_PUBLIC (newdecl))
2658 2 : error ("non-static declaration of %q+D follows "
2659 : "static declaration", newdecl);
2660 : else
2661 1 : error ("static declaration of %q+D follows "
2662 : "non-static declaration", newdecl);
2663 :
2664 3 : locate_old_decl (olddecl);
2665 3 : return false;
2666 3 : }
2667 : }
2668 : /* Two objects with the same name declared at the same block
2669 : scope must both be external references (6.7p3). */
2670 18490 : else if (!DECL_FILE_SCOPE_P (newdecl))
2671 : {
2672 17 : if (DECL_EXTERNAL (newdecl))
2673 : {
2674 : /* Extern with initializer at block scope, which will
2675 : already have received an error. */
2676 : }
2677 15 : else if (DECL_EXTERNAL (olddecl))
2678 : {
2679 4 : auto_diagnostic_group d;
2680 4 : error ("declaration of %q+D with no linkage follows "
2681 : "extern declaration", newdecl);
2682 4 : locate_old_decl (olddecl);
2683 4 : }
2684 : else
2685 : {
2686 11 : auto_diagnostic_group d;
2687 11 : error ("redeclaration of %q+D with no linkage", newdecl);
2688 11 : locate_old_decl (olddecl);
2689 11 : }
2690 :
2691 17 : return false;
2692 : }
2693 :
2694 : /* C++ does not permit a decl to appear multiple times at file
2695 : scope. */
2696 18615 : if (warn_cxx_compat
2697 68 : && DECL_FILE_SCOPE_P (newdecl)
2698 68 : && !DECL_EXTERNAL (newdecl)
2699 18633 : && !DECL_EXTERNAL (olddecl))
2700 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2701 5 : OPT_Wc___compat,
2702 : "duplicate declaration of %qD is "
2703 : "invalid in C++", newdecl);
2704 : }
2705 :
2706 : /* warnings */
2707 : /* All decls must agree on a visibility. */
2708 4934194 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2709 4934157 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2710 4934937 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2711 : {
2712 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2713 : "(old visibility preserved)", newdecl);
2714 : }
2715 :
2716 4934194 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2717 4915542 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2718 : else /* PARM_DECL, VAR_DECL */
2719 : {
2720 : /* Redeclaration of a parameter is a constraint violation (this is
2721 : not explicitly stated, but follows from C99 6.7p3 [no more than
2722 : one declaration of the same identifier with no linkage in the
2723 : same scope, except type tags] and 6.2.2p6 [parameters have no
2724 : linkage]). We must check for a forward parameter declaration,
2725 : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2726 : an extension, the mandatory diagnostic for which is handled by
2727 : mark_forward_parm_decls. */
2728 :
2729 18652 : if (TREE_CODE (newdecl) == PARM_DECL
2730 37 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2731 : {
2732 2 : auto_diagnostic_group d;
2733 2 : error ("redefinition of parameter %q+D", newdecl);
2734 2 : locate_old_decl (olddecl);
2735 2 : return false;
2736 2 : }
2737 : }
2738 :
2739 : /* Optional warning for completely redundant decls. */
2740 4934192 : if (!warned && !pedwarned
2741 4934118 : && warn_redundant_decls
2742 : /* Don't warn about a function declaration followed by a
2743 : definition. */
2744 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2745 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2746 : /* Don't warn about redundant redeclarations of builtins. */
2747 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2748 2 : && !fndecl_built_in_p (newdecl)
2749 2 : && fndecl_built_in_p (olddecl)
2750 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2751 : /* Don't warn about an extern followed by a definition. */
2752 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2753 : /* Don't warn about forward parameter decls. */
2754 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2755 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2756 : /* Don't warn about a variable definition following a declaration. */
2757 4934203 : && !(VAR_P (newdecl)
2758 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2759 : {
2760 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2761 : newdecl);
2762 : }
2763 :
2764 : /* Report location of previous decl/defn. */
2765 4934192 : if (warned || pedwarned)
2766 80 : locate_old_decl (olddecl);
2767 :
2768 : #undef DECL_EXTERN_INLINE
2769 :
2770 : return retval;
2771 4981012 : }
2772 :
2773 : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2774 : consistent with OLDDECL, but carries new information. Merge the
2775 : new information into OLDDECL. This function issues no
2776 : diagnostics. */
2777 :
2778 : static void
2779 4980012 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2780 : {
2781 4980012 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2782 4980012 : && DECL_INITIAL (newdecl) != NULL_TREE);
2783 4980012 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2784 4980012 : && prototype_p (TREE_TYPE (newdecl)));
2785 4980012 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2786 4980012 : && prototype_p (TREE_TYPE (olddecl)));
2787 :
2788 : /* For real parm decl following a forward decl, rechain the old decl
2789 : in its new location and clear TREE_ASM_WRITTEN (it's not a
2790 : forward decl anymore). */
2791 4980012 : if (TREE_CODE (newdecl) == PARM_DECL
2792 35 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2793 : {
2794 35 : struct c_binding *b, **here;
2795 :
2796 54 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2797 54 : if ((*here)->decl == olddecl)
2798 35 : goto found;
2799 0 : gcc_unreachable ();
2800 :
2801 35 : found:
2802 35 : b = *here;
2803 35 : *here = b->prev;
2804 35 : b->prev = current_scope->bindings;
2805 35 : current_scope->bindings = b;
2806 :
2807 35 : TREE_ASM_WRITTEN (olddecl) = 0;
2808 : }
2809 :
2810 4980012 : DECL_ATTRIBUTES (newdecl)
2811 4980012 : = targetm.merge_decl_attributes (olddecl, newdecl);
2812 :
2813 : /* For typedefs use the old type, as the new type's DECL_NAME points
2814 : at newdecl, which will be ggc_freed. */
2815 4980012 : if (TREE_CODE (newdecl) == TYPE_DECL)
2816 : {
2817 : /* But NEWTYPE might have an attribute, honor that. */
2818 45820 : tree tem = newtype;
2819 45820 : newtype = oldtype;
2820 :
2821 45820 : if (TYPE_USER_ALIGN (tem))
2822 : {
2823 15 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2824 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2825 15 : TYPE_USER_ALIGN (newtype) = true;
2826 : }
2827 :
2828 : /* And remove the new type from the variants list. */
2829 45820 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2830 : {
2831 15 : tree remove = TREE_TYPE (newdecl);
2832 15 : if (TYPE_MAIN_VARIANT (remove) == remove)
2833 : {
2834 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2835 : /* If remove is the main variant, no need to remove that
2836 : from the list. One of the DECL_ORIGINAL_TYPE
2837 : variants, e.g. created for aligned attribute, might still
2838 : refer to the newdecl TYPE_DECL though, so remove that one
2839 : in that case. */
2840 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2841 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2842 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2843 2 : t; t = TYPE_MAIN_VARIANT (t))
2844 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2845 : {
2846 4 : TYPE_NEXT_VARIANT (t)
2847 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2848 2 : break;
2849 : }
2850 : }
2851 : else
2852 13 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2853 0 : t = TYPE_NEXT_VARIANT (t))
2854 13 : if (TYPE_NEXT_VARIANT (t) == remove)
2855 : {
2856 13 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2857 13 : break;
2858 : }
2859 : }
2860 :
2861 : /* Make sure we refer to the same type as the olddecl. */
2862 45820 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2863 : }
2864 :
2865 : /* Merge the data types specified in the two decls. */
2866 9960024 : TREE_TYPE (newdecl)
2867 4980012 : = TREE_TYPE (olddecl)
2868 9960024 : = composite_type (newtype, oldtype);
2869 :
2870 : /* Lay the type out, unless already done. */
2871 4980012 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2872 : {
2873 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2874 0 : layout_type (TREE_TYPE (newdecl));
2875 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2876 : && TREE_CODE (newdecl) != TYPE_DECL
2877 : && TREE_CODE (newdecl) != CONST_DECL)
2878 0 : layout_decl (newdecl, 0);
2879 : }
2880 : else
2881 : {
2882 : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2883 4980012 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2884 4980012 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2885 4980012 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2886 4980012 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2887 : {
2888 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2889 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2890 : }
2891 4979968 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2892 4979968 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2893 3 : DECL_USER_ALIGN (newdecl) = 1;
2894 9960024 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2895 4980012 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2896 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2897 : DECL_WARN_IF_NOT_ALIGN (olddecl));
2898 : }
2899 :
2900 : /* Keep the old rtl since we can safely use it. */
2901 4980012 : if (HAS_RTL_P (olddecl))
2902 4980012 : COPY_DECL_RTL (olddecl, newdecl);
2903 :
2904 : /* Merge the type qualifiers. */
2905 4980012 : if (TREE_READONLY (newdecl))
2906 392688 : TREE_READONLY (olddecl) = 1;
2907 :
2908 4980012 : if (TREE_THIS_VOLATILE (newdecl))
2909 49383 : TREE_THIS_VOLATILE (olddecl) = 1;
2910 :
2911 : /* Merge deprecatedness. */
2912 4980012 : if (TREE_DEPRECATED (newdecl))
2913 383 : TREE_DEPRECATED (olddecl) = 1;
2914 :
2915 : /* Merge unavailability. */
2916 4980012 : if (TREE_UNAVAILABLE (newdecl))
2917 2 : TREE_UNAVAILABLE (olddecl) = 1;
2918 :
2919 : /* If a decl is in a system header and the other isn't, keep the one on the
2920 : system header. Otherwise, keep source location of definition rather than
2921 : declaration and of prototype rather than non-prototype unless that
2922 : prototype is built-in. */
2923 4980012 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2924 4979977 : && DECL_IN_SYSTEM_HEADER (olddecl)
2925 5664110 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2926 1042 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2927 4978970 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2928 4978935 : && DECL_IN_SYSTEM_HEADER (newdecl)
2929 9220321 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2930 3558295 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2931 1420675 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2932 1187391 : && DECL_INITIAL (olddecl) != NULL_TREE)
2933 2607144 : || (old_is_prototype && !new_is_prototype
2934 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2935 950 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2936 :
2937 : /* Merge the initialization information. */
2938 4980012 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2939 4746656 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2940 :
2941 : /* Merge 'constexpr' information. */
2942 4980012 : if (VAR_P (olddecl) && VAR_P (newdecl))
2943 : {
2944 18615 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2945 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2946 18613 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2947 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2948 : }
2949 :
2950 : /* Merge the threadprivate attribute. */
2951 4980012 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2952 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2953 :
2954 4980012 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2955 : {
2956 : /* Copy the assembler name.
2957 : Currently, it can only be defined in the prototype. */
2958 4979977 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2959 :
2960 : /* Use visibility of whichever declaration had it specified */
2961 4979977 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2962 : {
2963 4764 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2964 4764 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2965 : }
2966 :
2967 4979977 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2968 : {
2969 4915542 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2970 4915542 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2971 4915542 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2972 4915542 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2973 4915542 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2974 4915542 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2975 4915542 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2976 4915542 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2977 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2978 4915542 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2979 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2980 4915542 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2981 4915542 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2982 4915542 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2983 : }
2984 :
2985 : /* Merge the storage class information. */
2986 4979977 : merge_weak (newdecl, olddecl);
2987 :
2988 : /* For functions, static overrides non-static. */
2989 4979977 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2990 : {
2991 4915542 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2992 : /* This is since we don't automatically
2993 : copy the attributes of NEWDECL into OLDDECL. */
2994 4915542 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2995 : /* If this clears `static', clear it in the identifier too. */
2996 4915542 : if (!TREE_PUBLIC (olddecl))
2997 7810 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2998 : }
2999 : }
3000 :
3001 : /* In c99, 'extern' declaration before (or after) 'inline' means this
3002 : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
3003 : is present. */
3004 4980012 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3005 4915542 : && !flag_gnu89_inline
3006 4891149 : && (DECL_DECLARED_INLINE_P (newdecl)
3007 4701190 : || DECL_DECLARED_INLINE_P (olddecl))
3008 190153 : && (!DECL_DECLARED_INLINE_P (newdecl)
3009 189959 : || !DECL_DECLARED_INLINE_P (olddecl)
3010 807 : || !DECL_EXTERNAL (olddecl))
3011 189364 : && DECL_EXTERNAL (newdecl)
3012 189156 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
3013 4980110 : && !current_function_decl)
3014 81 : DECL_EXTERNAL (newdecl) = 0;
3015 :
3016 : /* An inline definition following a static declaration is not
3017 : DECL_EXTERNAL. */
3018 4980012 : if (new_is_definition
3019 232767 : && (DECL_DECLARED_INLINE_P (newdecl)
3020 42586 : || DECL_DECLARED_INLINE_P (olddecl))
3021 5170407 : && !TREE_PUBLIC (olddecl))
3022 900 : DECL_EXTERNAL (newdecl) = 0;
3023 :
3024 4980012 : if (DECL_EXTERNAL (newdecl))
3025 : {
3026 4888379 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
3027 4888379 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
3028 :
3029 : /* An extern decl does not override previous storage class. */
3030 4888379 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3031 4888379 : if (!DECL_EXTERNAL (newdecl))
3032 : {
3033 1437 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3034 1437 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3035 : }
3036 : }
3037 : else
3038 : {
3039 91633 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
3040 91633 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
3041 : }
3042 :
3043 4980012 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3044 : {
3045 4915542 : if (DECL_FUNCTION_VERSIONED (olddecl)
3046 4915542 : || DECL_FUNCTION_VERSIONED (newdecl))
3047 : {
3048 0 : maybe_mark_function_versioned (olddecl);
3049 0 : maybe_mark_function_versioned (newdecl);
3050 : }
3051 : /* If we're redefining a function previously defined as extern
3052 : inline, make sure we emit debug info for the inline before we
3053 : throw it away, in case it was inlined into a function that
3054 : hasn't been written out yet. */
3055 4915542 : if (new_is_definition && DECL_INITIAL (olddecl))
3056 : /* The new defn must not be inline. */
3057 75 : DECL_UNINLINABLE (newdecl) = 1;
3058 : else
3059 : {
3060 : /* If either decl says `inline', this fn is inline, unless
3061 : its definition was passed already. */
3062 4915467 : if (DECL_DECLARED_INLINE_P (newdecl)
3063 9640701 : || DECL_DECLARED_INLINE_P (olddecl))
3064 190418 : DECL_DECLARED_INLINE_P (newdecl) = 1;
3065 :
3066 14746401 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3067 9832561 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3068 :
3069 9830934 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3070 4915467 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3071 4915467 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3072 9830796 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3073 : }
3074 :
3075 4915542 : if (fndecl_built_in_p (olddecl))
3076 : {
3077 : /* If redeclaring a builtin function, it stays built in.
3078 : But it gets tagged as having been declared. */
3079 3869735 : copy_decl_built_in_function (newdecl, olddecl);
3080 3869735 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3081 3869735 : if (new_is_prototype)
3082 : {
3083 3869376 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3084 3869376 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3085 : {
3086 3869376 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3087 3869376 : switch (fncode)
3088 : {
3089 : /* If a compatible prototype of these builtin functions
3090 : is seen, assume the runtime implements it with the
3091 : expected semantics. */
3092 6493 : case BUILT_IN_STPCPY:
3093 6493 : if (builtin_decl_explicit_p (fncode))
3094 6493 : set_builtin_decl_implicit_p (fncode, true);
3095 : break;
3096 3862883 : default:
3097 3862883 : if (builtin_decl_explicit_p (fncode))
3098 3862883 : set_builtin_decl_declared_p (fncode, true);
3099 : break;
3100 : }
3101 :
3102 3869376 : copy_attributes_to_builtin (newdecl);
3103 : }
3104 : }
3105 : else
3106 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3107 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3108 : }
3109 :
3110 : /* Preserve function specific target and optimization options */
3111 4915542 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3112 4916048 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3113 470 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3114 470 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3115 :
3116 4915542 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3117 4938964 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3118 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3119 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3120 :
3121 : /* Also preserve various other info from the definition. */
3122 4915542 : if (!new_is_definition)
3123 : {
3124 4682775 : tree t;
3125 4682775 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3126 4682775 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3127 4682775 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3128 4682775 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3129 4682775 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3130 7345425 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3131 2662650 : DECL_CONTEXT (t) = newdecl;
3132 :
3133 : /* See if we've got a function to instantiate from. */
3134 4682775 : if (DECL_SAVED_TREE (olddecl))
3135 1620 : DECL_ABSTRACT_ORIGIN (newdecl)
3136 810 : = DECL_ABSTRACT_ORIGIN (olddecl);
3137 : }
3138 : }
3139 :
3140 : /* Merge the USED information. */
3141 4980012 : if (TREE_USED (olddecl))
3142 717037 : TREE_USED (newdecl) = 1;
3143 4262975 : else if (TREE_USED (newdecl))
3144 4 : TREE_USED (olddecl) = 1;
3145 4980012 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3146 18650 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3147 4980012 : if (DECL_PRESERVE_P (olddecl))
3148 25 : DECL_PRESERVE_P (newdecl) = 1;
3149 4979987 : else if (DECL_PRESERVE_P (newdecl))
3150 4 : DECL_PRESERVE_P (olddecl) = 1;
3151 :
3152 : /* Merge DECL_COMMON */
3153 18615 : if (VAR_P (olddecl) && VAR_P (newdecl)
3154 18615 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3155 4998625 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3156 37222 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3157 :
3158 : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3159 : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3160 : DECL_ARGUMENTS (if appropriate). */
3161 4980012 : {
3162 4980012 : unsigned olddecl_uid = DECL_UID (olddecl);
3163 4980012 : tree olddecl_context = DECL_CONTEXT (olddecl);
3164 4980012 : tree olddecl_arguments = NULL;
3165 4980012 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3166 4915542 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3167 :
3168 4980012 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3169 : (char *) newdecl + sizeof (struct tree_common),
3170 : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3171 4980012 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3172 4980012 : switch (TREE_CODE (olddecl))
3173 : {
3174 4934157 : case FUNCTION_DECL:
3175 4934157 : case VAR_DECL:
3176 4934157 : {
3177 4934157 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3178 :
3179 9868314 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3180 : (char *) newdecl + sizeof (struct tree_decl_common),
3181 4934157 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3182 4934157 : olddecl->decl_with_vis.symtab_node = snode;
3183 :
3184 4934157 : if ((DECL_EXTERNAL (olddecl)
3185 47215 : || TREE_PUBLIC (olddecl)
3186 8081 : || TREE_STATIC (olddecl))
3187 4981365 : && DECL_SECTION_NAME (newdecl) != NULL)
3188 8 : set_decl_section_name (olddecl, newdecl);
3189 :
3190 : /* This isn't quite correct for something like
3191 : int __thread x attribute ((tls_model ("local-exec")));
3192 : extern int __thread x;
3193 : as we'll lose the "local-exec" model. */
3194 4934157 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3195 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3196 : break;
3197 : }
3198 :
3199 45855 : case FIELD_DECL:
3200 45855 : case PARM_DECL:
3201 45855 : case LABEL_DECL:
3202 45855 : case RESULT_DECL:
3203 45855 : case CONST_DECL:
3204 45855 : case TYPE_DECL:
3205 91710 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3206 : (char *) newdecl + sizeof (struct tree_decl_common),
3207 45855 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3208 45855 : break;
3209 :
3210 0 : default:
3211 :
3212 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3213 : (char *) newdecl + sizeof (struct tree_decl_common),
3214 : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3215 : }
3216 4980012 : DECL_UID (olddecl) = olddecl_uid;
3217 4980012 : DECL_CONTEXT (olddecl) = olddecl_context;
3218 4980012 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3219 4915542 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3220 : }
3221 :
3222 : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3223 : so that encode_section_info has a chance to look at the new decl
3224 : flags and attributes. */
3225 4980012 : if (DECL_RTL_SET_P (olddecl)
3226 4980012 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3227 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3228 0 : make_decl_rtl (olddecl);
3229 4980012 : }
3230 :
3231 : /* Handle when a new declaration NEWDECL has the same name as an old
3232 : one OLDDECL in the same binding contour. Prints an error message
3233 : if appropriate.
3234 :
3235 : If safely possible, alter OLDDECL to look like NEWDECL, and return
3236 : true. Otherwise, return false. */
3237 :
3238 : static bool
3239 4981116 : duplicate_decls (tree newdecl, tree olddecl)
3240 : {
3241 4981116 : tree newtype = NULL, oldtype = NULL;
3242 :
3243 4981116 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3244 : {
3245 : /* Avoid `unused variable' and other warnings for OLDDECL. */
3246 1104 : suppress_warning (olddecl, OPT_Wunused);
3247 : /* If the types are completely different, poison them both with
3248 : error_mark_node. */
3249 1104 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3250 118 : && olddecl != error_mark_node
3251 1202 : && seen_error ())
3252 : {
3253 64 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3254 52 : TREE_TYPE (olddecl) = error_mark_node;
3255 64 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3256 60 : TREE_TYPE (newdecl) = error_mark_node;
3257 : }
3258 1104 : return false;
3259 : }
3260 :
3261 4980012 : merge_decls (newdecl, olddecl, newtype, oldtype);
3262 :
3263 : /* The NEWDECL will no longer be needed.
3264 :
3265 : Before releasing the node, be sure to remove function from symbol
3266 : table that might have been inserted there to record comdat group.
3267 : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3268 : structure is shared in between NEWDECL and OLDECL. */
3269 4980012 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3270 4915542 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3271 4980012 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3272 : {
3273 4934157 : struct symtab_node *snode = symtab_node::get (newdecl);
3274 4934157 : if (snode)
3275 104 : snode->remove ();
3276 : }
3277 4980012 : ggc_free (newdecl);
3278 4980012 : return true;
3279 : }
3280 :
3281 :
3282 : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3283 : static void
3284 170359328 : warn_if_shadowing (tree new_decl)
3285 : {
3286 170359328 : struct c_binding *b;
3287 :
3288 : /* Shadow warnings wanted? */
3289 340717740 : if (!(warn_shadow
3290 170358592 : || warn_shadow_local
3291 170358412 : || warn_shadow_compatible_local)
3292 : /* No shadow warnings for internally generated vars. */
3293 170359571 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3294 : return;
3295 :
3296 : /* Is anything being shadowed? Invisible decls do not count. */
3297 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3298 157 : if (b->decl && b->decl != new_decl && !b->invisible
3299 214 : && (b->decl == error_mark_node
3300 43 : || diagnostic_report_warnings_p (global_dc,
3301 : DECL_SOURCE_LOCATION (b->decl))))
3302 : {
3303 57 : tree old_decl = b->decl;
3304 :
3305 57 : if (old_decl == error_mark_node)
3306 : {
3307 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3308 : "non-variable", new_decl);
3309 50 : break;
3310 : }
3311 :
3312 43 : bool warned = false;
3313 43 : auto_diagnostic_group d;
3314 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3315 : {
3316 5 : enum opt_code warning_code;
3317 :
3318 : /* If '-Wshadow=compatible-local' is specified without other
3319 : -Wshadow= flags, we will warn only when the types of the
3320 : shadowing variable (i.e. new_decl) and the shadowed variable
3321 : (old_decl) are compatible. */
3322 5 : if (warn_shadow)
3323 : warning_code = OPT_Wshadow;
3324 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3325 : warning_code = OPT_Wshadow_compatible_local;
3326 : else
3327 2 : warning_code = OPT_Wshadow_local;
3328 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3329 : "declaration of %qD shadows a parameter",
3330 : new_decl);
3331 : }
3332 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3333 : {
3334 : /* Do not warn if a variable shadows a function, unless
3335 : the variable is a function or a pointer-to-function. */
3336 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3337 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3338 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3339 7 : continue;
3340 :
3341 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3342 : "declaration of %qD shadows a global "
3343 : "declaration",
3344 : new_decl);
3345 : }
3346 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3347 11 : && fndecl_built_in_p (old_decl))
3348 : {
3349 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3350 : "a built-in function", new_decl);
3351 0 : break;
3352 : }
3353 : else
3354 : {
3355 11 : enum opt_code warning_code;
3356 :
3357 : /* If '-Wshadow=compatible-local' is specified without other
3358 : -Wshadow= flags, we will warn only when the types of the
3359 : shadowing variable (i.e. new_decl) and the shadowed variable
3360 : (old_decl) are compatible. */
3361 11 : if (warn_shadow)
3362 : warning_code = OPT_Wshadow;
3363 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3364 : warning_code = OPT_Wshadow_compatible_local;
3365 : else
3366 2 : warning_code = OPT_Wshadow_local;
3367 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3368 : "declaration of %qD shadows a previous local",
3369 : new_decl);
3370 : }
3371 :
3372 36 : if (warned)
3373 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3374 : "shadowed declaration is here");
3375 :
3376 : break;
3377 43 : }
3378 : }
3379 :
3380 : /* Record a decl-node X as belonging to the current lexical scope.
3381 : Check for errors (such as an incompatible declaration for the same
3382 : name already seen in the same scope).
3383 :
3384 : Returns either X or an old decl for the same name.
3385 : If an old decl is returned, it may have been smashed
3386 : to agree with what X says. */
3387 :
3388 : tree
3389 202618349 : pushdecl (tree x)
3390 : {
3391 202618349 : tree name = DECL_NAME (x);
3392 202618349 : struct c_scope *scope = current_scope;
3393 202618349 : struct c_binding *b;
3394 202618349 : bool nested = false;
3395 202618349 : location_t locus = DECL_SOURCE_LOCATION (x);
3396 :
3397 : /* Must set DECL_CONTEXT for everything not at file scope or
3398 : DECL_FILE_SCOPE_P won't work. Local externs don't count
3399 : unless they have initializers (which generate code). We
3400 : also exclude CONST_DECLs because enumerators will get the
3401 : type of the enum as context. */
3402 202618349 : if (current_function_decl
3403 9136455 : && TREE_CODE (x) != CONST_DECL
3404 211597767 : && (!VAR_OR_FUNCTION_DECL_P (x)
3405 8701213 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3406 8967391 : DECL_CONTEXT (x) = current_function_decl;
3407 :
3408 : /* Anonymous decls are just inserted in the scope. */
3409 202618349 : if (!name)
3410 : {
3411 7824327 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3412 : locus);
3413 7824327 : return x;
3414 : }
3415 :
3416 : /* First, see if there is another declaration with the same name in
3417 : the current scope. If there is, duplicate_decls may do all the
3418 : work for us. If duplicate_decls returns false, that indicates
3419 : two incompatible decls in the same scope; we are to silently
3420 : replace the old one (duplicate_decls has issued all appropriate
3421 : diagnostics). In particular, we should not consider possible
3422 : duplicates in the external scope, or shadowing. */
3423 194794022 : b = I_SYMBOL_BINDING (name);
3424 194794022 : if (b && B_IN_SCOPE (b, scope))
3425 : {
3426 1389289 : struct c_binding *b_ext, *b_use;
3427 1389289 : tree type = TREE_TYPE (x);
3428 1389289 : tree visdecl = b->decl;
3429 1389289 : tree vistype = TREE_TYPE (visdecl);
3430 1389289 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3431 1389289 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3432 1370 : b->inner_comp = false;
3433 1389289 : b_use = b;
3434 1389289 : b_ext = b;
3435 : /* If this is an external linkage declaration, we should check
3436 : for compatibility with the type in the external scope before
3437 : setting the type at this scope based on the visible
3438 : information only. */
3439 1389289 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3440 : {
3441 2670506 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3442 1335263 : b_ext = b_ext->shadowed;
3443 1335243 : if (b_ext)
3444 : {
3445 1335242 : b_use = b_ext;
3446 1335242 : if (b_use->u.type)
3447 270418 : TREE_TYPE (b_use->decl) = b_use->u.type;
3448 : }
3449 : }
3450 :
3451 : /* Check if x is part of a FMV set with b_use.
3452 : FMV is only supported in c for targets with target_version
3453 : attributes. */
3454 1389289 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
3455 : && b_use && TREE_CODE (b_use->decl) == FUNCTION_DECL
3456 : && TREE_CODE (x) == FUNCTION_DECL && DECL_FILE_SCOPE_P (b_use->decl)
3457 : && DECL_FILE_SCOPE_P (x)
3458 : && disjoint_version_decls (x, b_use->decl)
3459 : && comptypes (vistype, type) != 0)
3460 : {
3461 : maybe_mark_function_versioned (b_use->decl);
3462 : maybe_mark_function_versioned (b->decl);
3463 : maybe_mark_function_versioned (x);
3464 :
3465 : cgraph_node *b_node = cgraph_node::get_create (b_use->decl);
3466 : cgraph_function_version_info *b_v = b_node->function_version ();
3467 : if (!b_v)
3468 : b_v = b_node->insert_new_function_version ();
3469 :
3470 : /* Check if this new node conflicts with any previous functions
3471 : in the set. */
3472 : cgraph_function_version_info *version = b_v;
3473 : for (; version; version = version->next)
3474 : if (!disjoint_version_decls (version->this_node->decl, x))
3475 : {
3476 : /* The decls define overlapping version, so attempt to merge
3477 : or diagnose the conflict. */
3478 : if (duplicate_decls (x, version->this_node->decl))
3479 : return version->this_node->decl;
3480 : else
3481 : return error_mark_node;
3482 : }
3483 :
3484 : /* This is a new version to be added to FMV structure. */
3485 : cgraph_node::add_function_version (b_v, x);
3486 :
3487 : /* Get the first node from the structure. */
3488 : cgraph_function_version_info *default_v = b_v;
3489 : while (default_v->prev)
3490 : default_v = default_v->prev;
3491 : /* Always use the default node for the bindings. */
3492 : b_use->decl = default_v->this_node->decl;
3493 : b->decl = default_v->this_node->decl;
3494 :
3495 : /* Node is not a duplicate, so no need to do the rest of the
3496 : checks. */
3497 : return x;
3498 : }
3499 :
3500 1389289 : if (duplicate_decls (x, b_use->decl))
3501 : {
3502 1388919 : if (b_use != b)
3503 : {
3504 : /* Save the updated type in the external scope and
3505 : restore the proper type for this scope. */
3506 1335036 : tree thistype;
3507 1335036 : if (comptypes (vistype, type))
3508 1334994 : thistype = composite_type (vistype, type);
3509 : else
3510 42 : thistype = TREE_TYPE (b_use->decl);
3511 1335036 : b_use->u.type = TREE_TYPE (b_use->decl);
3512 1335036 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3513 1335036 : && fndecl_built_in_p (b_use->decl))
3514 280379 : thistype
3515 280379 : = c_build_type_attribute_variant (thistype,
3516 280379 : TYPE_ATTRIBUTES
3517 : (b_use->u.type));
3518 1335036 : TREE_TYPE (b_use->decl) = thistype;
3519 : }
3520 1388919 : return b_use->decl;
3521 : }
3522 : else
3523 370 : goto skip_external_and_shadow_checks;
3524 : }
3525 :
3526 : /* All declarations with external linkage, and all external
3527 : references, go in the external scope, no matter what scope is
3528 : current. However, the binding in that scope is ignored for
3529 : purposes of normal name lookup. A separate binding structure is
3530 : created in the requested scope; this governs the normal
3531 : visibility of the symbol.
3532 :
3533 : The binding in the externals scope is used exclusively for
3534 : detecting duplicate declarations of the same object, no matter
3535 : what scope they are in; this is what we do here. (C99 6.2.7p2:
3536 : All declarations that refer to the same object or function shall
3537 : have compatible type; otherwise, the behavior is undefined.)
3538 : However, in Objective-C, we also want to detect declarations
3539 : conflicting with those of the basic types. */
3540 337802317 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3541 204896465 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3542 : {
3543 50795732 : tree type = TREE_TYPE (x);
3544 50795732 : tree vistype = NULL_TREE;
3545 50795732 : tree visdecl = NULL_TREE;
3546 50795732 : bool type_saved = false;
3547 3591854 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3548 1038 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3549 50796761 : && DECL_FILE_SCOPE_P (b->decl))
3550 : {
3551 803 : visdecl = b->decl;
3552 803 : vistype = TREE_TYPE (visdecl);
3553 : }
3554 50795732 : if (scope != file_scope
3555 50795732 : && !DECL_IN_SYSTEM_HEADER (x))
3556 11477 : warning_at (locus, OPT_Wnested_externs,
3557 : "nested extern declaration of %qD", x);
3558 :
3559 50797156 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3560 : {
3561 : /* If this decl might be modified, save its type. This is
3562 : done here rather than when the decl is first bound
3563 : because the type may change after first binding, through
3564 : being completed or through attributes being added. If we
3565 : encounter multiple such decls, only the first should have
3566 : its type saved; the others will already have had their
3567 : proper types saved and the types will not have changed as
3568 : their scopes will not have been re-entered. */
3569 1424 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3570 : {
3571 1022 : b->u.type = TREE_TYPE (b->decl);
3572 1022 : type_saved = true;
3573 : }
3574 1424 : if (B_IN_FILE_SCOPE (b)
3575 1012 : && VAR_P (b->decl)
3576 572 : && TREE_STATIC (b->decl)
3577 281 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3578 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3579 47 : && TREE_CODE (type) == ARRAY_TYPE
3580 47 : && TYPE_DOMAIN (type)
3581 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3582 1452 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3583 : {
3584 : /* Array type completed in inner scope, which should be
3585 : diagnosed if the completion does not have size 1 and
3586 : it does not get completed in the file scope. */
3587 6 : b->inner_comp = true;
3588 : }
3589 1424 : b = b->shadowed;
3590 : }
3591 :
3592 : /* If a matching external declaration has been found, set its
3593 : type to the composite of all the types of that declaration.
3594 : After the consistency checks, it will be reset to the
3595 : composite of the visible types only. */
3596 50795732 : if (b && b->u.type)
3597 768 : TREE_TYPE (b->decl) = b->u.type;
3598 :
3599 : /* the static does not go in the externals scope. */
3600 3591710 : if (b && duplicate_decls (x, b->decl))
3601 : {
3602 3590976 : tree thistype;
3603 3590976 : if (vistype)
3604 : {
3605 675 : if (comptypes (vistype, type))
3606 638 : thistype = composite_type (vistype, type);
3607 : else
3608 37 : thistype = TREE_TYPE (b->decl);
3609 : }
3610 : else
3611 : thistype = type;
3612 3590976 : b->u.type = TREE_TYPE (b->decl);
3613 : /* Propagate the type attributes to the decl. */
3614 3590976 : thistype
3615 3590976 : = c_build_type_attribute_variant (thistype,
3616 3590976 : TYPE_ATTRIBUTES (b->u.type));
3617 3590976 : TREE_TYPE (b->decl) = thistype;
3618 3590976 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3619 : locus);
3620 3590976 : return b->decl;
3621 : }
3622 47204756 : else if (TREE_PUBLIC (x))
3623 : {
3624 46831081 : if (visdecl && !b && duplicate_decls (x, visdecl))
3625 : {
3626 : /* An external declaration at block scope referring to a
3627 : visible entity with internal linkage. The composite
3628 : type will already be correct for this scope, so we
3629 : just need to fall through to make the declaration in
3630 : this scope. */
3631 : nested = true;
3632 : x = visdecl;
3633 : }
3634 : else
3635 : {
3636 46830964 : bind (name, x, external_scope, /*invisible=*/true,
3637 : /*nested=*/false, locus);
3638 46830964 : nested = true;
3639 : }
3640 : }
3641 : }
3642 :
3643 189813757 : if (TREE_CODE (x) != PARM_DECL)
3644 70340742 : warn_if_shadowing (x);
3645 :
3646 119473015 : skip_external_and_shadow_checks:
3647 189814127 : if (TREE_CODE (x) == TYPE_DECL)
3648 : {
3649 : /* So this is a typedef, set its underlying type. */
3650 9649071 : set_underlying_type (x);
3651 :
3652 : /* If X is a typedef defined in the current function, record it
3653 : for the purpose of implementing the -Wunused-local-typedefs
3654 : warning. */
3655 9649071 : record_locally_defined_typedef (x);
3656 : }
3657 :
3658 189814127 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3659 :
3660 : /* If x's type is incomplete because it's based on a
3661 : structure or union which has not yet been fully declared,
3662 : attach it to that structure or union type, so we can go
3663 : back and complete the variable declaration later, if the
3664 : structure or union gets fully declared.
3665 :
3666 : If the input is erroneous, we can have error_mark in the type
3667 : slot (e.g. "f(void a, ...)") - that doesn't count as an
3668 : incomplete type. */
3669 189814127 : if (TREE_TYPE (x) != error_mark_node
3670 189814127 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3671 : {
3672 182797 : tree element = TREE_TYPE (x);
3673 :
3674 202828 : while (TREE_CODE (element) == ARRAY_TYPE)
3675 20031 : element = TREE_TYPE (element);
3676 182797 : element = TYPE_MAIN_VARIANT (element);
3677 :
3678 182797 : if ((RECORD_OR_UNION_TYPE_P (element)
3679 138878 : || TREE_CODE (element) == ENUMERAL_TYPE)
3680 43990 : && (TREE_CODE (x) != TYPE_DECL
3681 40523 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3682 186274 : && !COMPLETE_TYPE_P (element))
3683 321 : C_TYPE_INCOMPLETE_VARS (element)
3684 642 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3685 : }
3686 : return x;
3687 : }
3688 :
3689 :
3690 : /* Issue a permerror about implicit function declaration. ID is the function
3691 : identifier, OLDDECL is a declaration of the function in a different scope,
3692 : or NULL_TREE. */
3693 :
3694 : static void
3695 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3696 : {
3697 3776 : if (!warn_implicit_function_declaration)
3698 2617 : return;
3699 :
3700 1159 : bool warned;
3701 1159 : auto_diagnostic_group d;
3702 1159 : name_hint hint;
3703 1159 : if (!olddecl)
3704 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3705 :
3706 1159 : if (flag_isoc99)
3707 : {
3708 1153 : if (const char *suggestion = hint.suggestion ())
3709 : {
3710 107 : gcc_rich_location richloc (loc);
3711 107 : richloc.add_fixit_replace (suggestion);
3712 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3713 : "implicit declaration of function %qE;"
3714 : " did you mean %qs?",
3715 : id, suggestion);
3716 107 : }
3717 : else
3718 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3719 : "implicit declaration of function %qE", id);
3720 : }
3721 6 : else if (const char *suggestion = hint.suggestion ())
3722 : {
3723 2 : gcc_rich_location richloc (loc);
3724 2 : richloc.add_fixit_replace (suggestion);
3725 2 : warned = warning_at
3726 2 : (&richloc, OPT_Wimplicit_function_declaration,
3727 : G_("implicit declaration of function %qE; did you mean %qs?"),
3728 : id, suggestion);
3729 2 : }
3730 : else
3731 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3732 : G_("implicit declaration of function %qE"), id);
3733 :
3734 1159 : if (warned)
3735 : {
3736 : /* Whether the olddecl is an undeclared builtin function.
3737 : locate_old_decl will not generate a diagnostic for those,
3738 : so in that case we want to look elsewhere. */
3739 90 : bool undeclared_builtin = (olddecl
3740 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3741 28 : && fndecl_built_in_p (olddecl)
3742 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3743 90 : if (undeclared_builtin)
3744 : {
3745 27 : const char *header = header_for_builtin_fn (olddecl);
3746 27 : if (header)
3747 : {
3748 20 : rich_location richloc (line_table, loc);
3749 20 : maybe_add_include_fixit (&richloc, header, true);
3750 20 : inform (&richloc,
3751 : "include %qs or provide a declaration of %qE",
3752 : header, id);
3753 20 : }
3754 : }
3755 63 : else if (olddecl)
3756 1 : locate_old_decl (olddecl);
3757 : }
3758 :
3759 1069 : if (!warned)
3760 1159 : hint.suppress ();
3761 1159 : }
3762 :
3763 : /* Return the name of the header file that declares built-in function
3764 : FNDECL, or null if either we don't know or don't expect to see an
3765 : explicit declaration. */
3766 :
3767 : static const char *
3768 3178 : header_for_builtin_fn (tree fndecl)
3769 : {
3770 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3771 : return NULL;
3772 :
3773 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3774 : {
3775 : CASE_FLT_FN (BUILT_IN_ACOS):
3776 : CASE_FLT_FN (BUILT_IN_ACOSH):
3777 : CASE_FLT_FN (BUILT_IN_ASIN):
3778 : CASE_FLT_FN (BUILT_IN_ASINH):
3779 : CASE_FLT_FN (BUILT_IN_ATAN):
3780 : CASE_FLT_FN (BUILT_IN_ATANH):
3781 : CASE_FLT_FN (BUILT_IN_ATAN2):
3782 : CASE_FLT_FN (BUILT_IN_CBRT):
3783 : CASE_FLT_FN (BUILT_IN_CEIL):
3784 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3785 : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3786 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3787 : CASE_FLT_FN (BUILT_IN_COS):
3788 : CASE_FLT_FN (BUILT_IN_COSH):
3789 : CASE_FLT_FN (BUILT_IN_ERF):
3790 : CASE_FLT_FN (BUILT_IN_ERFC):
3791 : CASE_FLT_FN (BUILT_IN_EXP):
3792 : CASE_FLT_FN (BUILT_IN_EXP2):
3793 : CASE_FLT_FN (BUILT_IN_EXPM1):
3794 : CASE_FLT_FN (BUILT_IN_FABS):
3795 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3796 : CASE_FLT_FN (BUILT_IN_FDIM):
3797 : CASE_FLT_FN (BUILT_IN_FLOOR):
3798 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3799 : CASE_FLT_FN (BUILT_IN_FMA):
3800 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3801 : CASE_FLT_FN (BUILT_IN_FMAX):
3802 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3803 : CASE_FLT_FN (BUILT_IN_FMIN):
3804 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3805 : CASE_FLT_FN (BUILT_IN_FMOD):
3806 : CASE_FLT_FN (BUILT_IN_FREXP):
3807 : CASE_FLT_FN (BUILT_IN_HYPOT):
3808 : CASE_FLT_FN (BUILT_IN_ILOGB):
3809 : CASE_FLT_FN (BUILT_IN_LDEXP):
3810 : CASE_FLT_FN (BUILT_IN_LGAMMA):
3811 : CASE_FLT_FN (BUILT_IN_LLRINT):
3812 : CASE_FLT_FN (BUILT_IN_LLROUND):
3813 : CASE_FLT_FN (BUILT_IN_LOG):
3814 : CASE_FLT_FN (BUILT_IN_LOG10):
3815 : CASE_FLT_FN (BUILT_IN_LOG1P):
3816 : CASE_FLT_FN (BUILT_IN_LOG2):
3817 : CASE_FLT_FN (BUILT_IN_LOGB):
3818 : CASE_FLT_FN (BUILT_IN_LRINT):
3819 : CASE_FLT_FN (BUILT_IN_LROUND):
3820 : CASE_FLT_FN (BUILT_IN_MODF):
3821 : CASE_FLT_FN (BUILT_IN_NAN):
3822 : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3823 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3824 : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3825 : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3826 : CASE_FLT_FN (BUILT_IN_POW):
3827 : CASE_FLT_FN (BUILT_IN_REMAINDER):
3828 : CASE_FLT_FN (BUILT_IN_REMQUO):
3829 : CASE_FLT_FN (BUILT_IN_RINT):
3830 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3831 : CASE_FLT_FN (BUILT_IN_ROUND):
3832 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3833 : CASE_FLT_FN (BUILT_IN_SCALBLN):
3834 : CASE_FLT_FN (BUILT_IN_SCALBN):
3835 : CASE_FLT_FN (BUILT_IN_SIN):
3836 : CASE_FLT_FN (BUILT_IN_SINH):
3837 : CASE_FLT_FN (BUILT_IN_SINCOS):
3838 : CASE_FLT_FN (BUILT_IN_SQRT):
3839 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3840 : CASE_FLT_FN (BUILT_IN_TAN):
3841 : CASE_FLT_FN (BUILT_IN_TANH):
3842 : CASE_FLT_FN (BUILT_IN_TGAMMA):
3843 : CASE_FLT_FN (BUILT_IN_TRUNC):
3844 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3845 : case BUILT_IN_ISINF:
3846 : case BUILT_IN_ISNAN:
3847 : return "<math.h>";
3848 29 : CASE_FLT_FN (BUILT_IN_CABS):
3849 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3850 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3851 29 : CASE_FLT_FN (BUILT_IN_CARG):
3852 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3853 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3854 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3855 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3856 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3857 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3858 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3859 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3860 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3861 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3862 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3863 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3864 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3865 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3866 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3867 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3868 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3869 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3870 29 : return "<complex.h>";
3871 228 : case BUILT_IN_MEMCHR:
3872 228 : case BUILT_IN_MEMCMP:
3873 228 : case BUILT_IN_MEMCPY:
3874 228 : case BUILT_IN_MEMMOVE:
3875 228 : case BUILT_IN_MEMSET:
3876 228 : case BUILT_IN_STRCAT:
3877 228 : case BUILT_IN_STRCHR:
3878 228 : case BUILT_IN_STRCMP:
3879 228 : case BUILT_IN_STRCPY:
3880 228 : case BUILT_IN_STRCSPN:
3881 228 : case BUILT_IN_STRLEN:
3882 228 : case BUILT_IN_STRNCAT:
3883 228 : case BUILT_IN_STRNCMP:
3884 228 : case BUILT_IN_STRNCPY:
3885 228 : case BUILT_IN_STRPBRK:
3886 228 : case BUILT_IN_STRRCHR:
3887 228 : case BUILT_IN_STRSPN:
3888 228 : case BUILT_IN_STRSTR:
3889 228 : return "<string.h>";
3890 544 : case BUILT_IN_FPRINTF:
3891 544 : case BUILT_IN_PUTC:
3892 544 : case BUILT_IN_FPUTC:
3893 544 : case BUILT_IN_FPUTS:
3894 544 : case BUILT_IN_FSCANF:
3895 544 : case BUILT_IN_FWRITE:
3896 544 : case BUILT_IN_PRINTF:
3897 544 : case BUILT_IN_PUTCHAR:
3898 544 : case BUILT_IN_PUTS:
3899 544 : case BUILT_IN_SCANF:
3900 544 : case BUILT_IN_SNPRINTF:
3901 544 : case BUILT_IN_SPRINTF:
3902 544 : case BUILT_IN_SSCANF:
3903 544 : case BUILT_IN_VFPRINTF:
3904 544 : case BUILT_IN_VFSCANF:
3905 544 : case BUILT_IN_VPRINTF:
3906 544 : case BUILT_IN_VSCANF:
3907 544 : case BUILT_IN_VSNPRINTF:
3908 544 : case BUILT_IN_VSPRINTF:
3909 544 : case BUILT_IN_VSSCANF:
3910 544 : return "<stdio.h>";
3911 2 : case BUILT_IN_ISALNUM:
3912 2 : case BUILT_IN_ISALPHA:
3913 2 : case BUILT_IN_ISBLANK:
3914 2 : case BUILT_IN_ISCNTRL:
3915 2 : case BUILT_IN_ISDIGIT:
3916 2 : case BUILT_IN_ISGRAPH:
3917 2 : case BUILT_IN_ISLOWER:
3918 2 : case BUILT_IN_ISPRINT:
3919 2 : case BUILT_IN_ISPUNCT:
3920 2 : case BUILT_IN_ISSPACE:
3921 2 : case BUILT_IN_ISUPPER:
3922 2 : case BUILT_IN_ISXDIGIT:
3923 2 : case BUILT_IN_TOLOWER:
3924 2 : case BUILT_IN_TOUPPER:
3925 2 : return "<ctype.h>";
3926 0 : case BUILT_IN_ISWALNUM:
3927 0 : case BUILT_IN_ISWALPHA:
3928 0 : case BUILT_IN_ISWBLANK:
3929 0 : case BUILT_IN_ISWCNTRL:
3930 0 : case BUILT_IN_ISWDIGIT:
3931 0 : case BUILT_IN_ISWGRAPH:
3932 0 : case BUILT_IN_ISWLOWER:
3933 0 : case BUILT_IN_ISWPRINT:
3934 0 : case BUILT_IN_ISWPUNCT:
3935 0 : case BUILT_IN_ISWSPACE:
3936 0 : case BUILT_IN_ISWUPPER:
3937 0 : case BUILT_IN_ISWXDIGIT:
3938 0 : case BUILT_IN_TOWLOWER:
3939 0 : case BUILT_IN_TOWUPPER:
3940 0 : return "<wctype.h>";
3941 1818 : case BUILT_IN_ABORT:
3942 1818 : case BUILT_IN_ABS:
3943 1818 : case BUILT_IN_CALLOC:
3944 1818 : case BUILT_IN_EXIT:
3945 1818 : case BUILT_IN_FREE:
3946 1818 : case BUILT_IN_LABS:
3947 1818 : case BUILT_IN_LLABS:
3948 1818 : case BUILT_IN_MALLOC:
3949 1818 : case BUILT_IN_REALLOC:
3950 1818 : case BUILT_IN__EXIT2:
3951 1818 : case BUILT_IN_ALIGNED_ALLOC:
3952 1818 : return "<stdlib.h>";
3953 1 : case BUILT_IN_IMAXABS:
3954 1 : return "<inttypes.h>";
3955 3 : case BUILT_IN_STRFTIME:
3956 3 : return "<time.h>";
3957 : default:
3958 : return NULL;
3959 : }
3960 : }
3961 :
3962 : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3963 : function of type int (). */
3964 :
3965 : tree
3966 4729 : implicitly_declare (location_t loc, tree functionid)
3967 : {
3968 4729 : struct c_binding *b;
3969 4729 : tree decl = NULL_TREE;
3970 4729 : tree asmspec_tree;
3971 :
3972 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3973 : {
3974 3158 : if (B_IN_SCOPE (b, external_scope))
3975 : {
3976 3144 : decl = b->decl;
3977 3144 : break;
3978 : }
3979 : }
3980 :
3981 4729 : if (decl)
3982 : {
3983 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3984 : return decl;
3985 :
3986 : /* FIXME: Objective-C has weird not-really-builtin functions
3987 : which are supposed to be visible automatically. They wind up
3988 : in the external scope because they're pushed before the file
3989 : scope gets created. Catch this here and rebind them into the
3990 : file scope. */
3991 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3992 : {
3993 0 : bind (functionid, decl, file_scope,
3994 : /*invisible=*/false, /*nested=*/true,
3995 0 : DECL_SOURCE_LOCATION (decl));
3996 0 : return decl;
3997 : }
3998 : else
3999 : {
4000 3137 : tree newtype = default_function_type;
4001 3137 : if (b->u.type)
4002 753 : TREE_TYPE (decl) = b->u.type;
4003 : /* Implicit declaration of a function already declared
4004 : (somehow) in a different scope, or as a built-in.
4005 : If this is the first time this has happened, warn;
4006 : then recycle the old declaration but with the new type. */
4007 3137 : if (!C_DECL_IMPLICIT (decl))
4008 : {
4009 2191 : implicit_decl_permerror (loc, functionid, decl);
4010 2191 : C_DECL_IMPLICIT (decl) = 1;
4011 : }
4012 3137 : if (fndecl_built_in_p (decl))
4013 : {
4014 2705 : newtype = c_build_type_attribute_variant (newtype,
4015 2705 : TYPE_ATTRIBUTES
4016 : (TREE_TYPE (decl)));
4017 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
4018 : {
4019 2528 : auto_diagnostic_group d;
4020 2528 : bool warned = warning_at (loc,
4021 2528 : OPT_Wbuiltin_declaration_mismatch,
4022 : "incompatible implicit "
4023 : "declaration of built-in "
4024 : "function %qD", decl);
4025 : /* See if we can hint which header to include. */
4026 2528 : const char *header = header_for_builtin_fn (decl);
4027 2528 : if (header != NULL && warned)
4028 : {
4029 133 : rich_location richloc (line_table, loc);
4030 133 : maybe_add_include_fixit (&richloc, header, true);
4031 133 : inform (&richloc,
4032 : "include %qs or provide a declaration of %qD",
4033 : header, decl);
4034 133 : }
4035 2528 : newtype = TREE_TYPE (decl);
4036 2528 : }
4037 : }
4038 : else
4039 : {
4040 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
4041 : {
4042 2 : auto_diagnostic_group d;
4043 2 : error_at (loc, "incompatible implicit declaration of "
4044 : "function %qD", decl);
4045 2 : locate_old_decl (decl);
4046 2 : }
4047 : }
4048 3137 : b->u.type = TREE_TYPE (decl);
4049 3137 : TREE_TYPE (decl) = newtype;
4050 3137 : bind (functionid, decl, current_scope,
4051 : /*invisible=*/false, /*nested=*/true,
4052 3137 : DECL_SOURCE_LOCATION (decl));
4053 3137 : return decl;
4054 : }
4055 : }
4056 :
4057 : /* Not seen before. */
4058 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
4059 1585 : DECL_EXTERNAL (decl) = 1;
4060 1585 : TREE_PUBLIC (decl) = 1;
4061 1585 : C_DECL_IMPLICIT (decl) = 1;
4062 1585 : implicit_decl_permerror (loc, functionid, 0);
4063 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
4064 1585 : if (asmspec_tree)
4065 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
4066 :
4067 : /* C89 says implicit declarations are in the innermost block.
4068 : So we record the decl in the standard fashion. */
4069 1585 : decl = pushdecl (decl);
4070 :
4071 : /* No need to call objc_check_decl here - it's a function type. */
4072 1585 : rest_of_decl_compilation (decl, 0, 0);
4073 :
4074 : /* Write a record describing this implicit function declaration
4075 : to the prototypes file (if requested). */
4076 1585 : gen_aux_info_record (decl, 0, 1, 0);
4077 :
4078 : /* Possibly apply some default attributes to this implicit declaration. */
4079 1585 : decl_attributes (&decl, NULL_TREE, 0);
4080 :
4081 1585 : return decl;
4082 : }
4083 :
4084 : /* Issue an error message for a reference to an undeclared variable
4085 : ID, including a reference to a builtin outside of function-call
4086 : context. Establish a binding of the identifier to error_mark_node
4087 : in an appropriate scope, which will suppress further errors for the
4088 : same identifier. The error message should be given location LOC. */
4089 : void
4090 1214 : undeclared_variable (location_t loc, tree id)
4091 : {
4092 1214 : static bool already = false;
4093 1214 : struct c_scope *scope;
4094 :
4095 1214 : auto_diagnostic_group d;
4096 1214 : if (current_function_decl == NULL_TREE)
4097 : {
4098 390 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4099 390 : if (const char *suggestion = guessed_id.suggestion ())
4100 : {
4101 86 : gcc_rich_location richloc (loc);
4102 86 : richloc.add_fixit_replace (suggestion);
4103 86 : error_at (&richloc,
4104 : "%qE undeclared here (not in a function);"
4105 : " did you mean %qs?",
4106 : id, suggestion);
4107 86 : }
4108 : else
4109 304 : error_at (loc, "%qE undeclared here (not in a function)", id);
4110 390 : scope = current_scope;
4111 390 : }
4112 : else
4113 : {
4114 824 : if (!objc_diagnose_private_ivar (id))
4115 : {
4116 824 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4117 824 : if (const char *suggestion = guessed_id.suggestion ())
4118 : {
4119 39 : gcc_rich_location richloc (loc);
4120 39 : richloc.add_fixit_replace (suggestion);
4121 39 : error_at (&richloc,
4122 : "%qE undeclared (first use in this function);"
4123 : " did you mean %qs?",
4124 : id, suggestion);
4125 39 : }
4126 : else
4127 785 : error_at (loc, "%qE undeclared (first use in this function)", id);
4128 824 : }
4129 824 : if (!already)
4130 : {
4131 201 : inform (loc, "each undeclared identifier is reported only"
4132 : " once for each function it appears in");
4133 201 : already = true;
4134 : }
4135 :
4136 : /* If we are parsing old-style parameter decls, current_function_decl
4137 : will be nonnull but current_function_scope will be null. */
4138 824 : scope = current_function_scope ? current_function_scope : current_scope;
4139 : }
4140 1214 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4141 : UNKNOWN_LOCATION);
4142 1214 : }
4143 :
4144 : /* Subroutine of lookup_label, declare_label, define_label: construct a
4145 : LABEL_DECL with all the proper frills. Also create a struct
4146 : c_label_vars initialized for the current scope. */
4147 :
4148 : static tree
4149 23949 : make_label (location_t location, tree name, bool defining,
4150 : struct c_label_vars **p_label_vars)
4151 : {
4152 23949 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4153 23949 : DECL_CONTEXT (label) = current_function_decl;
4154 23949 : SET_DECL_MODE (label, VOIDmode);
4155 :
4156 23949 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4157 23949 : label_vars->shadowed = NULL;
4158 23949 : set_spot_bindings (&label_vars->label_bindings, defining);
4159 23949 : label_vars->decls_in_scope = make_tree_vector ();
4160 23949 : label_vars->gotos = NULL;
4161 23949 : *p_label_vars = label_vars;
4162 :
4163 23949 : return label;
4164 : }
4165 :
4166 : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4167 : Create one if none exists so far for the current function.
4168 : This is called when a label is used in a goto expression or
4169 : has its address taken. */
4170 :
4171 : tree
4172 85417 : lookup_label (tree name)
4173 : {
4174 85417 : tree label;
4175 85417 : struct c_label_vars *label_vars;
4176 :
4177 85417 : if (current_function_scope == 0)
4178 : {
4179 2 : error ("label %qE referenced outside of any function", name);
4180 2 : return NULL_TREE;
4181 : }
4182 :
4183 : /* Use a label already defined or ref'd with this name, but not if
4184 : it is inherited from a containing function and wasn't declared
4185 : using __label__. */
4186 85415 : label = I_LABEL_DECL (name);
4187 79467 : if (label && (DECL_CONTEXT (label) == current_function_decl
4188 608 : || C_DECLARED_LABEL_FLAG (label)))
4189 : {
4190 : /* If the label has only been declared, update its apparent
4191 : location to point here, for better diagnostics if it
4192 : turns out not to have been defined. */
4193 79460 : if (DECL_INITIAL (label) == NULL_TREE)
4194 62685 : DECL_SOURCE_LOCATION (label) = input_location;
4195 79460 : return label;
4196 : }
4197 :
4198 : /* No label binding for that identifier; make one. */
4199 5955 : label = make_label (input_location, name, false, &label_vars);
4200 :
4201 : /* Ordinary labels go in the current function scope. */
4202 5955 : bind_label (name, label, current_function_scope, label_vars);
4203 :
4204 5955 : return label;
4205 : }
4206 :
4207 : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4208 : to LABEL. */
4209 :
4210 : static void
4211 1490 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4212 : {
4213 1490 : auto_diagnostic_group d;
4214 1490 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4215 1484 : error_at (goto_loc,
4216 : "jump into scope of identifier with variably modified type");
4217 6 : else if (flag_openmp
4218 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4219 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4220 : else
4221 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4222 : "jump skips variable initialization"))
4223 0 : return;
4224 1490 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4225 1490 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4226 1490 : }
4227 :
4228 : /* Look up a label because of a goto statement. This is like
4229 : lookup_label, but also issues any appropriate warnings. */
4230 :
4231 : tree
4232 83576 : lookup_label_for_goto (location_t loc, tree name)
4233 : {
4234 83576 : tree label;
4235 83576 : struct c_label_vars *label_vars;
4236 83576 : unsigned int ix;
4237 83576 : tree decl;
4238 :
4239 83576 : label = lookup_label (name);
4240 83576 : if (label == NULL_TREE)
4241 : return NULL_TREE;
4242 :
4243 : /* If we are jumping to a different function, we can't issue any
4244 : useful warnings. */
4245 83576 : if (DECL_CONTEXT (label) != current_function_decl)
4246 : {
4247 522 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4248 : return label;
4249 : }
4250 :
4251 83054 : label_vars = I_LABEL_BINDING (name)->u.label;
4252 :
4253 : /* If the label has not yet been defined, then push this goto on a
4254 : list for possible later warnings. */
4255 83054 : if (label_vars->label_bindings.scope == NULL)
4256 : {
4257 66504 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4258 :
4259 66504 : g->loc = loc;
4260 66504 : set_spot_bindings (&g->goto_bindings, true);
4261 66504 : vec_safe_push (label_vars->gotos, g);
4262 66504 : return label;
4263 : }
4264 :
4265 : /* If there are any decls in label_vars->decls_in_scope, then this
4266 : goto has missed the declaration of the decl. This happens for a
4267 : case like
4268 : int i = 1;
4269 : lab:
4270 : ...
4271 : goto lab;
4272 : Issue a warning or error. */
4273 17325 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4274 775 : warn_about_goto (loc, label, decl);
4275 :
4276 16550 : if (label_vars->label_bindings.left_stmt_expr)
4277 : {
4278 120 : auto_diagnostic_group d;
4279 120 : error_at (loc, "jump into statement expression");
4280 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4281 120 : }
4282 :
4283 : return label;
4284 : }
4285 :
4286 : /* Make a label named NAME in the current function, shadowing silently
4287 : any that may be inherited from containing functions or containing
4288 : scopes. This is called for __label__ declarations. */
4289 :
4290 : tree
4291 1192 : declare_label (tree name)
4292 : {
4293 1192 : struct c_binding *b = I_LABEL_BINDING (name);
4294 1192 : tree label;
4295 1192 : struct c_label_vars *label_vars;
4296 :
4297 : /* Check to make sure that the label hasn't already been declared
4298 : at this scope */
4299 1192 : if (b && B_IN_CURRENT_SCOPE (b))
4300 : {
4301 2 : auto_diagnostic_group d;
4302 2 : error ("duplicate label declaration %qE", name);
4303 2 : locate_old_decl (b->decl);
4304 :
4305 : /* Just use the previous declaration. */
4306 2 : return b->decl;
4307 2 : }
4308 :
4309 1190 : label = make_label (input_location, name, false, &label_vars);
4310 1190 : C_DECLARED_LABEL_FLAG (label) = 1;
4311 :
4312 : /* Declared labels go in the current scope. */
4313 1190 : bind_label (name, label, current_scope, label_vars);
4314 :
4315 1190 : return label;
4316 : }
4317 :
4318 : /* When we define a label, issue any appropriate warnings if there are
4319 : any gotos earlier in the function which jump to this label. */
4320 :
4321 : static void
4322 7065 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4323 : {
4324 7065 : unsigned int ix;
4325 7065 : struct c_goto_bindings *g;
4326 :
4327 73503 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4328 : {
4329 66438 : struct c_binding *b;
4330 66438 : struct c_scope *scope;
4331 :
4332 : /* We have a goto to this label. The goto is going forward. In
4333 : g->scope, the goto is going to skip any binding which was
4334 : defined after g->bindings_in_scope. */
4335 66438 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4336 : {
4337 255 : for (b = g->goto_bindings.scope->bindings;
4338 593 : b != g->goto_bindings.bindings_in_scope;
4339 338 : b = b->prev)
4340 : {
4341 338 : if (decl_jump_unsafe (b->decl))
4342 176 : warn_about_goto (g->loc, label, b->decl);
4343 : }
4344 : }
4345 :
4346 : /* We also need to warn about decls defined in any scopes
4347 : between the scope of the label and the scope of the goto. */
4348 66438 : for (scope = label_vars->label_bindings.scope;
4349 69132 : scope != g->goto_bindings.scope;
4350 2694 : scope = scope->outer)
4351 : {
4352 2694 : gcc_assert (scope != NULL);
4353 2694 : if (scope->has_jump_unsafe_decl)
4354 : {
4355 325 : if (scope == label_vars->label_bindings.scope)
4356 249 : b = label_vars->label_bindings.bindings_in_scope;
4357 : else
4358 76 : b = scope->bindings;
4359 864 : for (; b != NULL; b = b->prev)
4360 : {
4361 539 : if (decl_jump_unsafe (b->decl))
4362 539 : warn_about_goto (g->loc, label, b->decl);
4363 : }
4364 : }
4365 : }
4366 :
4367 66438 : if (g->goto_bindings.stmt_exprs > 0)
4368 : {
4369 100 : auto_diagnostic_group d;
4370 100 : error_at (g->loc, "jump into statement expression");
4371 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4372 : label);
4373 100 : }
4374 : }
4375 :
4376 : /* Now that the label is defined, we will issue warnings about
4377 : subsequent gotos to this label when we see them. */
4378 7065 : vec_safe_truncate (label_vars->gotos, 0);
4379 7065 : label_vars->gotos = NULL;
4380 7065 : }
4381 :
4382 : /* Define a label, specifying the location in the source file.
4383 : Return the LABEL_DECL node for the label, if the definition is valid.
4384 : Otherwise return NULL_TREE. */
4385 :
4386 : tree
4387 23898 : define_label (location_t location, tree name)
4388 : {
4389 : /* Find any preexisting label with this name. It is an error
4390 : if that label has already been defined in this function, or
4391 : if there is a containing function with a declared label with
4392 : the same name. */
4393 23898 : tree label = I_LABEL_DECL (name);
4394 :
4395 7101 : if (label
4396 7101 : && ((DECL_CONTEXT (label) == current_function_decl
4397 7087 : && DECL_INITIAL (label) != NULL_TREE)
4398 7079 : || (DECL_CONTEXT (label) != current_function_decl
4399 14 : && C_DECLARED_LABEL_FLAG (label))))
4400 : {
4401 29 : auto_diagnostic_group d;
4402 29 : error_at (location, "duplicate label %qD", label);
4403 29 : locate_old_decl (label);
4404 29 : return NULL_TREE;
4405 29 : }
4406 23869 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4407 : {
4408 7065 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4409 :
4410 : /* The label has been used or declared already in this function,
4411 : but not defined. Update its location to point to this
4412 : definition. */
4413 7065 : DECL_SOURCE_LOCATION (label) = location;
4414 7065 : set_spot_bindings (&label_vars->label_bindings, true);
4415 :
4416 : /* Issue warnings as required about any goto statements from
4417 : earlier in the function. */
4418 7065 : check_earlier_gotos (label, label_vars);
4419 : }
4420 : else
4421 : {
4422 16804 : struct c_label_vars *label_vars;
4423 :
4424 : /* No label binding for that identifier; make one. */
4425 16804 : label = make_label (location, name, true, &label_vars);
4426 :
4427 : /* Ordinary labels go in the current function scope. */
4428 16804 : bind_label (name, label, current_function_scope, label_vars);
4429 : }
4430 :
4431 23869 : if (!in_system_header_at (input_location) && lookup_name (name))
4432 139 : warning_at (location, OPT_Wtraditional,
4433 : "traditional C lacks a separate namespace "
4434 : "for labels, identifier %qE conflicts", name);
4435 :
4436 : /* Mark label as having been defined. */
4437 23869 : DECL_INITIAL (label) = error_mark_node;
4438 23869 : return label;
4439 : }
4440 :
4441 : /* Get the bindings for a new switch statement. This is used to issue
4442 : warnings as appropriate for jumps from the switch to case or
4443 : default labels. */
4444 :
4445 : struct c_spot_bindings *
4446 37299 : c_get_switch_bindings (void)
4447 : {
4448 37299 : struct c_spot_bindings *switch_bindings;
4449 :
4450 37299 : switch_bindings = XNEW (struct c_spot_bindings);
4451 37299 : set_spot_bindings (switch_bindings, true);
4452 37299 : return switch_bindings;
4453 : }
4454 :
4455 : void
4456 37299 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4457 : {
4458 37299 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4459 37299 : XDELETE (bindings);
4460 37299 : }
4461 :
4462 : /* This is called at the point of a case or default label to issue
4463 : warnings about decls as needed. It returns true if it found an
4464 : error, not just a warning. */
4465 :
4466 : bool
4467 1030320 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4468 : location_t switch_loc, location_t case_loc)
4469 : {
4470 1030320 : bool saw_error;
4471 1030320 : struct c_scope *scope;
4472 :
4473 1030320 : saw_error = false;
4474 1030320 : for (scope = current_scope;
4475 3090988 : scope != switch_bindings->scope;
4476 2060668 : scope = scope->outer)
4477 : {
4478 2060668 : struct c_binding *b;
4479 :
4480 2060668 : gcc_assert (scope != NULL);
4481 :
4482 2060668 : if (!scope->has_jump_unsafe_decl)
4483 2060657 : continue;
4484 :
4485 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4486 : {
4487 11 : if (decl_jump_unsafe (b->decl))
4488 : {
4489 11 : auto_diagnostic_group d;
4490 11 : bool emitted;
4491 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4492 : {
4493 6 : saw_error = true;
4494 6 : error_at (case_loc,
4495 : "switch jumps into scope of identifier with "
4496 : "variably modified type");
4497 6 : emitted = true;
4498 : }
4499 5 : else if (flag_openmp
4500 7 : && lookup_attribute ("omp allocate",
4501 2 : DECL_ATTRIBUTES (b->decl)))
4502 : {
4503 2 : saw_error = true;
4504 2 : error_at (case_loc,
4505 : "switch jumps over OpenMP %<allocate%> allocation");
4506 2 : emitted = true;
4507 : }
4508 : else
4509 3 : emitted
4510 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4511 : "switch jumps over variable initialization");
4512 11 : if (emitted)
4513 : {
4514 11 : inform (switch_loc, "switch starts here");
4515 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4516 : b->decl);
4517 : }
4518 11 : }
4519 : }
4520 : }
4521 :
4522 1030320 : if (switch_bindings->stmt_exprs > 0)
4523 : {
4524 4 : saw_error = true;
4525 4 : auto_diagnostic_group d;
4526 4 : error_at (case_loc, "switch jumps into statement expression");
4527 4 : inform (switch_loc, "switch starts here");
4528 4 : }
4529 :
4530 1030320 : return saw_error;
4531 : }
4532 :
4533 : /* Given NAME, an IDENTIFIER_NODE,
4534 : return the structure (or union or enum) definition for that name.
4535 : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4536 : CODE says which kind of type the caller wants;
4537 : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4538 : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4539 : location where the tag was defined.
4540 : If the wrong kind of type is found, an error is reported. */
4541 :
4542 : static tree
4543 2083855 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4544 : location_t *ploc)
4545 : {
4546 2083855 : struct c_binding *b = I_TAG_BINDING (name);
4547 2083855 : bool thislevel = false;
4548 :
4549 2083855 : if (!b || !b->decl)
4550 : return NULL_TREE;
4551 :
4552 : /* We only care about whether it's in this level if
4553 : thislevel_only was set or it might be a type clash. */
4554 1457903 : if (thislevel_only || TREE_CODE (b->decl) != code)
4555 : {
4556 : /* For our purposes, a tag in the external scope is the same as
4557 : a tag in the file scope. (Primarily relevant to Objective-C
4558 : and its builtin structure tags, which get pushed before the
4559 : file scope is created.) */
4560 467807 : if (B_IN_CURRENT_SCOPE (b)
4561 196 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4562 1457903 : thislevel = true;
4563 : }
4564 :
4565 1457903 : if (thislevel_only && !thislevel)
4566 : return NULL_TREE;
4567 :
4568 1457728 : if (TREE_CODE (b->decl) != code)
4569 : {
4570 : /* Definition isn't the kind we were looking for. */
4571 39 : pending_invalid_xref = name;
4572 39 : pending_invalid_xref_location = input_location;
4573 :
4574 : /* If in the same binding level as a declaration as a tag
4575 : of a different type, this must not be allowed to
4576 : shadow that tag, so give the error immediately.
4577 : (For example, "struct foo; union foo;" is invalid.) */
4578 39 : if (thislevel)
4579 18 : pending_xref_error ();
4580 : }
4581 :
4582 1457728 : if (ploc != NULL)
4583 1015587 : *ploc = b->locus;
4584 :
4585 1457728 : return b->decl;
4586 : }
4587 :
4588 : /* Return true if a definition exists for NAME with code CODE. */
4589 :
4590 : bool
4591 387 : tag_exists_p (enum tree_code code, tree name)
4592 : {
4593 387 : struct c_binding *b = I_TAG_BINDING (name);
4594 :
4595 387 : if (b == NULL || b->decl == NULL_TREE)
4596 : return false;
4597 18 : return TREE_CODE (b->decl) == code;
4598 : }
4599 :
4600 : /* Print an error message now
4601 : for a recent invalid struct, union or enum cross reference.
4602 : We don't print them immediately because they are not invalid
4603 : when used in the `struct foo;' construct for shadowing. */
4604 :
4605 : void
4606 313599550 : pending_xref_error (void)
4607 : {
4608 313599550 : if (pending_invalid_xref != NULL_TREE)
4609 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4610 : pending_invalid_xref);
4611 313599550 : pending_invalid_xref = NULL_TREE;
4612 313599550 : }
4613 :
4614 :
4615 : /* Look up NAME in the current scope and its superiors
4616 : in the namespace of variables, functions and typedefs.
4617 : Return a ..._DECL node of some kind representing its definition,
4618 : or return NULL_TREE if it is undefined. */
4619 :
4620 : tree
4621 1267093452 : lookup_name (tree name)
4622 : {
4623 1267093452 : struct c_binding *b = I_SYMBOL_BINDING (name);
4624 : /* Do not resolve non-default function versions. */
4625 1267093452 : if (b
4626 856593087 : && TREE_CODE (b->decl) == FUNCTION_DECL
4627 117155519 : && DECL_FUNCTION_VERSIONED (b->decl)
4628 1267093452 : && !is_function_default_version (b->decl))
4629 : return NULL_TREE;
4630 1267093452 : if (b && !b->invisible)
4631 : {
4632 845797325 : maybe_record_typedef_use (b->decl);
4633 845797325 : return b->decl;
4634 : }
4635 : return NULL_TREE;
4636 : }
4637 :
4638 : /* Similar to `lookup_name' but look only at the indicated scope. */
4639 :
4640 : static tree
4641 131222123 : lookup_name_in_scope (tree name, struct c_scope *scope)
4642 : {
4643 131222123 : struct c_binding *b;
4644 :
4645 131225350 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4646 7413401 : if (B_IN_SCOPE (b, scope))
4647 7410174 : return b->decl;
4648 : return NULL_TREE;
4649 : }
4650 :
4651 : /* Look for the closest match for NAME within the currently valid
4652 : scopes.
4653 :
4654 : This finds the identifier with the lowest Levenshtein distance to
4655 : NAME. If there are multiple candidates with equal minimal distance,
4656 : the first one found is returned. Scopes are searched from innermost
4657 : outwards, and within a scope in reverse order of declaration, thus
4658 : benefiting candidates "near" to the current scope.
4659 :
4660 : The function also looks for similar macro names to NAME, since a
4661 : misspelled macro name will not be expanded, and hence looks like an
4662 : identifier to the C frontend.
4663 :
4664 : It also looks for start_typename keywords, to detect "singed" vs "signed"
4665 : typos.
4666 :
4667 : Use LOC for any deferred diagnostics. */
4668 :
4669 : name_hint
4670 2022 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4671 : {
4672 2022 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4673 :
4674 : /* Look up function-like macros first; maybe misusing them. */
4675 4044 : auto cpp_node = cpp_lookup (parse_in,
4676 2022 : (const unsigned char*)IDENTIFIER_POINTER (name),
4677 2022 : IDENTIFIER_LENGTH (name));
4678 2022 : if (cpp_node && cpp_fun_like_macro_p (cpp_node))
4679 5 : return name_hint
4680 : (nullptr,
4681 5 : std::make_unique<macro_like_function_used> (loc,
4682 10 : IDENTIFIER_POINTER (name)));
4683 :
4684 : /* Next, try some well-known names in the C standard library, in case
4685 : the user forgot a #include. */
4686 2017 : const char *header_hint
4687 2017 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4688 :
4689 2017 : if (header_hint)
4690 56 : return name_hint
4691 : (nullptr,
4692 56 : std::make_unique<suggest_missing_header> (loc,
4693 112 : IDENTIFIER_POINTER (name),
4694 56 : header_hint));
4695 :
4696 : /* Next, look for exact matches for builtin defines that would have been
4697 : defined if the user had passed a command-line option (e.g. -fopenmp
4698 : for "_OPENMP"). */
4699 1961 : diagnostics::option_id option_id
4700 1961 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4701 1961 : if (option_id.m_idx > 0)
4702 2 : return name_hint
4703 : (nullptr,
4704 2 : std::make_unique<suggest_missing_option> (loc,
4705 4 : IDENTIFIER_POINTER (name),
4706 2 : option_id));
4707 :
4708 : /* Only suggest names reserved for the implementation if NAME begins
4709 : with an underscore. */
4710 1959 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4711 :
4712 1959 : best_match<tree, tree> bm (name);
4713 :
4714 : /* Look within currently valid scopes. */
4715 8979 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4716 11153663 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4717 : {
4718 11146643 : if (!binding->id || binding->invisible)
4719 6295396 : continue;
4720 4851247 : if (binding->decl == error_mark_node)
4721 374 : continue;
4722 : /* Don't use bindings from implicitly declared functions,
4723 : as they were likely misspellings themselves. */
4724 4850873 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4725 4449034 : if (C_DECL_IMPLICIT (binding->decl))
4726 229 : continue;
4727 : /* Don't suggest names that are reserved for use by the
4728 : implementation, unless NAME began with an underscore. */
4729 4850644 : if (!consider_implementation_names)
4730 : {
4731 2338547 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4732 2338547 : if (name_reserved_for_implementation_p (suggestion_str))
4733 2280147 : continue;
4734 : }
4735 2570497 : switch (kind)
4736 : {
4737 30980 : case FUZZY_LOOKUP_TYPENAME:
4738 30980 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4739 23525 : continue;
4740 : break;
4741 :
4742 128923 : case FUZZY_LOOKUP_FUNCTION_NAME:
4743 128923 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4744 : {
4745 : /* Allow function pointers. */
4746 26502 : if ((VAR_P (binding->decl)
4747 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4748 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4749 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4750 : == FUNCTION_TYPE))
4751 : break;
4752 26483 : continue;
4753 : }
4754 : break;
4755 :
4756 : default:
4757 : break;
4758 : }
4759 2520489 : bm.consider (binding->id);
4760 : }
4761 :
4762 : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4763 : as:
4764 : x = SOME_OTHER_MACRO (y);
4765 : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4766 : as a misspelled identifier.
4767 :
4768 : Use the best distance so far so that a candidate is only set if
4769 : a macro is better than anything so far. This allows early rejection
4770 : (without calculating the edit distance) of macro names that must have
4771 : distance >= bm.get_best_distance (), and means that we only get a
4772 : non-NULL result for best_macro_match if it's better than any of
4773 : the identifiers already checked, which avoids needless creation
4774 : of identifiers for macro hashnodes. */
4775 1959 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4776 1959 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4777 : /* If a macro is the closest so far to NAME, use it, creating an
4778 : identifier tree node for it. */
4779 1959 : if (best_macro)
4780 : {
4781 11 : const char *id = (const char *)best_macro->ident.str;
4782 11 : tree macro_as_identifier
4783 11 : = get_identifier_with_length (id, best_macro->ident.len);
4784 11 : bm.set_best_so_far (macro_as_identifier,
4785 : bmm.get_best_distance (),
4786 : bmm.get_best_candidate_length ());
4787 : }
4788 :
4789 : /* Try the "start_typename" keywords to detect
4790 : "singed" vs "signed" typos. */
4791 1959 : if (kind == FUZZY_LOOKUP_TYPENAME)
4792 : {
4793 56386 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4794 : {
4795 56144 : const c_common_resword *resword = &c_common_reswords[i];
4796 56144 : if (!c_keyword_starts_typename (resword->rid))
4797 42834 : continue;
4798 13310 : tree resword_identifier = ridpointers [resword->rid];
4799 13310 : if (!resword_identifier)
4800 30 : continue;
4801 13280 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4802 13280 : bm.consider (resword_identifier);
4803 : }
4804 : }
4805 :
4806 1959 : tree best = bm.get_best_meaningful_candidate ();
4807 1959 : if (best)
4808 278 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4809 : else
4810 1681 : return name_hint (NULL, NULL);
4811 : }
4812 :
4813 :
4814 : /* Handle the standard [[nodiscard]] attribute. */
4815 :
4816 : static tree
4817 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4818 : int /*flags*/, bool *no_add_attrs)
4819 : {
4820 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4821 : {
4822 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4823 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4824 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4825 : "return type", name, *node);
4826 : }
4827 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4828 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4829 : /* OK */;
4830 : else
4831 : {
4832 10 : pedwarn (input_location,
4833 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4834 : "functions or to structure, union or enumeration types", name);
4835 10 : *no_add_attrs = true;
4836 : }
4837 34 : return NULL_TREE;
4838 : }
4839 :
4840 : /* Handle the standard [[noreturn]] attribute. */
4841 :
4842 : static tree
4843 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4844 : int flags, bool *no_add_attrs)
4845 : {
4846 : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4847 : only applies to functions, not function pointers. */
4848 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4849 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4850 : else
4851 : {
4852 22 : pedwarn (input_location, OPT_Wattributes,
4853 : "standard %qE attribute can only be applied to functions",
4854 : name);
4855 22 : *no_add_attrs = true;
4856 22 : return NULL_TREE;
4857 : }
4858 : }
4859 :
4860 : /* Handle the standard [[unsequenced]] attribute. */
4861 :
4862 : static tree
4863 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4864 : int flags, bool *no_add_attrs)
4865 : {
4866 : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4867 : should be only applied to function declarators or type specifiers which
4868 : have function type. */
4869 68 : if (node[2])
4870 : {
4871 8 : auto_diagnostic_group d;
4872 8 : if (pedwarn (input_location, OPT_Wattributes,
4873 : "standard %qE attribute can only be applied to function "
4874 : "declarators or type specifiers with function type", name))
4875 8 : inform (input_location, "did you mean to specify it after %<)%> "
4876 : "following function parameters?");
4877 8 : *no_add_attrs = true;
4878 8 : return NULL_TREE;
4879 8 : }
4880 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4881 : }
4882 :
4883 : /* Handle the standard [[reproducible]] attribute. */
4884 :
4885 : static tree
4886 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4887 : int flags, bool *no_add_attrs)
4888 : {
4889 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4890 34 : no_add_attrs);
4891 : }
4892 :
4893 : /* Table of supported standard (C23) attributes. */
4894 : static const attribute_spec std_attributes[] =
4895 : {
4896 : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4897 : affects_type_identity, handler, exclude } */
4898 : { "_Noreturn", 0, 0, false, false, false, false,
4899 : handle_std_noreturn_attribute, NULL },
4900 : { "deprecated", 0, 1, false, false, false, false,
4901 : handle_deprecated_attribute, NULL },
4902 : { "fallthrough", 0, 0, false, false, false, false,
4903 : handle_fallthrough_attribute, NULL },
4904 : { "maybe_unused", 0, 0, false, false, false, false,
4905 : handle_unused_attribute, NULL },
4906 : { "nodiscard", 0, 1, false, false, false, false,
4907 : handle_nodiscard_attribute, NULL },
4908 : { "noreturn", 0, 0, false, false, false, false,
4909 : handle_std_noreturn_attribute, NULL },
4910 : { "reproducible", 0, 0, false, true, true, false,
4911 : handle_std_reproducible_attribute, NULL },
4912 : { "unsequenced", 0, 0, false, true, true, false,
4913 : handle_std_unsequenced_attribute, NULL }
4914 : };
4915 :
4916 : const scoped_attribute_specs std_attribute_table =
4917 : {
4918 : nullptr, { std_attributes }
4919 : };
4920 :
4921 : /* Create the predefined scalar types of C,
4922 : and some nodes representing standard constants (0, 1, (void *) 0).
4923 : Initialize the global scope.
4924 : Make definitions for built-in primitive functions. */
4925 :
4926 : void
4927 110128 : c_init_decl_processing (void)
4928 : {
4929 110128 : location_t save_loc = input_location;
4930 :
4931 : /* Initialize reserved words for parser. */
4932 110128 : c_parse_init ();
4933 :
4934 110128 : current_function_decl = NULL_TREE;
4935 :
4936 110128 : gcc_obstack_init (&parser_obstack);
4937 :
4938 : /* Make the externals scope. */
4939 110128 : push_scope ();
4940 110128 : external_scope = current_scope;
4941 :
4942 : /* Declarations from c_common_nodes_and_builtins must not be associated
4943 : with this input file, lest we get differences between using and not
4944 : using preprocessed headers. */
4945 110128 : input_location = BUILTINS_LOCATION;
4946 :
4947 110128 : c_common_nodes_and_builtins ();
4948 :
4949 : /* In C, comparisons and TRUTH_* expressions have type int. */
4950 110128 : truthvalue_type_node = integer_type_node;
4951 110128 : truthvalue_true_node = integer_one_node;
4952 110128 : truthvalue_false_node = integer_zero_node;
4953 :
4954 : /* Even in C99, which has a real boolean type. */
4955 110128 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4956 : boolean_type_node));
4957 :
4958 : /* C-specific nullptr initialization. */
4959 110128 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4960 : /* The size and alignment of nullptr_t is the same as for a pointer to
4961 : character type. */
4962 110128 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4963 :
4964 110128 : input_location = save_loc;
4965 :
4966 110128 : make_fname_decl = c_make_fname_decl;
4967 110128 : start_fname_decls ();
4968 :
4969 110128 : if (warn_keyword_macro)
4970 : {
4971 1165 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4972 : /* For C register keywords which don't start with underscore
4973 : or start with just single underscore. Don't complain about
4974 : ObjC or Transactional Memory keywords. */
4975 1160 : if (c_common_reswords[i].word[0] == '_'
4976 530 : && c_common_reswords[i].word[1] == '_')
4977 390 : continue;
4978 995 : else if (c_common_reswords[i].disable
4979 770 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4980 225 : continue;
4981 : else
4982 : {
4983 545 : tree id = get_identifier (c_common_reswords[i].word);
4984 545 : if (C_IS_RESERVED_WORD (id)
4985 545 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4986 332 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4987 332 : IDENTIFIER_LENGTH (id));
4988 : }
4989 : }
4990 110128 : }
4991 :
4992 : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4993 : give the decl, NAME is the initialization string and TYPE_DEP
4994 : indicates whether NAME depended on the type of the function. As we
4995 : don't yet implement delayed emission of static data, we mark the
4996 : decl as emitted so it is not placed in the output. Anything using
4997 : it must therefore pull out the STRING_CST initializer directly.
4998 : FIXME. */
4999 :
5000 : static tree
5001 2935 : c_make_fname_decl (location_t loc, tree id, int type_dep)
5002 : {
5003 2935 : const char *name = fname_as_string (type_dep);
5004 2935 : tree decl, type, init;
5005 2935 : size_t length = strlen (name);
5006 :
5007 2935 : type = c_build_array_type (char_type_node,
5008 2935 : build_index_type (size_int (length)));
5009 2935 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
5010 :
5011 2935 : decl = build_decl (loc, VAR_DECL, id, type);
5012 :
5013 2935 : TREE_STATIC (decl) = 1;
5014 2935 : TREE_READONLY (decl) = 1;
5015 2935 : DECL_ARTIFICIAL (decl) = 1;
5016 :
5017 2935 : init = build_string (length + 1, name);
5018 2935 : free (const_cast<char *> (name));
5019 2935 : TREE_TYPE (init) = type;
5020 2935 : DECL_INITIAL (decl) = init;
5021 :
5022 2935 : TREE_USED (decl) = 1;
5023 :
5024 2935 : if (current_function_decl
5025 : /* For invalid programs like this:
5026 :
5027 : void foo()
5028 : const char* p = __FUNCTION__;
5029 :
5030 : the __FUNCTION__ is believed to appear in K&R style function
5031 : parameter declarator. In that case we still don't have
5032 : function_scope. */
5033 2928 : && current_function_scope)
5034 : {
5035 2920 : DECL_CONTEXT (decl) = current_function_decl;
5036 2920 : bind (id, decl, current_function_scope,
5037 : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
5038 : }
5039 :
5040 2935 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
5041 :
5042 2935 : return decl;
5043 : }
5044 :
5045 : tree
5046 321562236 : c_builtin_function (tree decl)
5047 : {
5048 321562236 : tree type = TREE_TYPE (decl);
5049 321562236 : tree id = DECL_NAME (decl);
5050 :
5051 321562236 : const char *name = IDENTIFIER_POINTER (id);
5052 321562236 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5053 :
5054 : /* Should never be called on a symbol with a preexisting meaning. */
5055 321562236 : gcc_assert (!I_SYMBOL_BINDING (id));
5056 :
5057 321562236 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
5058 : UNKNOWN_LOCATION);
5059 :
5060 : /* Builtins in the implementation namespace are made visible without
5061 : needing to be explicitly declared. See push_file_scope. */
5062 321562236 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5063 : {
5064 219427727 : DECL_CHAIN (decl) = visible_builtins;
5065 219427727 : visible_builtins = decl;
5066 : }
5067 :
5068 321562236 : return decl;
5069 : }
5070 :
5071 : tree
5072 10498078 : c_builtin_function_ext_scope (tree decl)
5073 : {
5074 10498078 : tree type = TREE_TYPE (decl);
5075 10498078 : tree id = DECL_NAME (decl);
5076 :
5077 10498078 : const char *name = IDENTIFIER_POINTER (id);
5078 10498078 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5079 :
5080 10498078 : if (external_scope)
5081 10311069 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
5082 : UNKNOWN_LOCATION);
5083 :
5084 : /* Builtins in the implementation namespace are made visible without
5085 : needing to be explicitly declared. See push_file_scope. */
5086 10498078 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5087 : {
5088 10498078 : DECL_CHAIN (decl) = visible_builtins;
5089 10498078 : visible_builtins = decl;
5090 : }
5091 :
5092 10498078 : return decl;
5093 : }
5094 :
5095 : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5096 :
5097 : tree
5098 0 : c_simulate_builtin_function_decl (tree decl)
5099 : {
5100 0 : tree type = TREE_TYPE (decl);
5101 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5102 0 : return pushdecl (decl);
5103 : }
5104 :
5105 : /* Warn about attributes in a context where they are unused
5106 : (attribute-declarations, except for the "fallthrough" case, and
5107 : attributes on statements). */
5108 :
5109 : void
5110 41573731 : c_warn_unused_attributes (tree attrs)
5111 : {
5112 41573776 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
5113 45 : if (get_attribute_namespace (t) == NULL_TREE)
5114 : /* The specifications of standard attributes mean this is a
5115 : constraint violation. */
5116 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5117 : get_attribute_name (t));
5118 16 : else if (!attribute_ignored_p (t))
5119 12 : warning (OPT_Wattributes, "%qE attribute ignored",
5120 : get_attribute_name (t));
5121 41573731 : }
5122 :
5123 : /* Warn for standard attributes being applied to a type that is not
5124 : being defined, where that is a constraint violation, and return a
5125 : list of attributes with them removed. */
5126 :
5127 : tree
5128 435620074 : c_warn_type_attributes (tree type, tree attrs)
5129 : {
5130 435620074 : tree *attr_ptr = &attrs;
5131 435643316 : while (*attr_ptr)
5132 23242 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
5133 : {
5134 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
5135 : {
5136 65 : tree name = get_attribute_name (*attr_ptr);
5137 : /* [[unsequenced]] and [[reproducible]] is fine on function
5138 : types that aren't being defined. */
5139 65 : if (is_attribute_p ("unsequenced", name)
5140 65 : || is_attribute_p ("reproducible", name))
5141 : {
5142 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5143 60 : continue;
5144 : }
5145 : }
5146 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5147 : get_attribute_name (*attr_ptr));
5148 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5149 : }
5150 : else
5151 23153 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5152 435620074 : return attrs;
5153 : }
5154 :
5155 : /* Called when a declaration is seen that contains no names to declare.
5156 : If its type is a reference to a structure, union or enum inherited
5157 : from a containing scope, shadow that tag name for the current scope
5158 : with a forward reference.
5159 : If its type defines a new named structure or union
5160 : or defines an enum, it is valid but we need not do anything here.
5161 : Otherwise, it is an error. */
5162 :
5163 : void
5164 502194 : shadow_tag (const struct c_declspecs *declspecs)
5165 : {
5166 502194 : shadow_tag_warned (declspecs, 0);
5167 502194 : }
5168 :
5169 : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5170 : but no pedwarn. */
5171 : void
5172 502264 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5173 : {
5174 502264 : bool found_tag = false;
5175 :
5176 502264 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5177 : {
5178 502150 : tree value = declspecs->type;
5179 502150 : enum tree_code code = TREE_CODE (value);
5180 :
5181 502150 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5182 : /* Used to test also that TYPE_SIZE (value) != 0.
5183 : That caused warning for `struct foo;' at top level in the file. */
5184 : {
5185 502099 : tree name = TYPE_NAME (value);
5186 502099 : tree t;
5187 :
5188 502099 : found_tag = true;
5189 :
5190 502099 : if (declspecs->restrict_p)
5191 : {
5192 2 : error ("invalid use of %<restrict%>");
5193 2 : warned = 1;
5194 : }
5195 :
5196 502099 : if (in_underspecified_init)
5197 : {
5198 : /* This can only occur with extensions such as statement
5199 : expressions, but is still appropriate as an error to
5200 : avoid types declared in such a context escaping to
5201 : the type of an auto variable. */
5202 1 : error ("%qT declared in underspecified object initializer",
5203 : value);
5204 1 : warned = 1;
5205 : }
5206 :
5207 502099 : if (name == NULL_TREE)
5208 : {
5209 59947 : if (warned != 1 && code != ENUMERAL_TYPE)
5210 : /* Empty unnamed enum OK */
5211 : {
5212 19 : pedwarn (input_location, 0,
5213 : "unnamed struct/union that defines no instances");
5214 19 : warned = 1;
5215 : }
5216 : }
5217 442152 : else if (declspecs->typespec_kind != ctsk_tagdef
5218 88139 : && declspecs->typespec_kind != ctsk_tagfirstref
5219 26222 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5220 26214 : && declspecs->storage_class != csc_none)
5221 : {
5222 2 : if (warned != 1)
5223 2 : pedwarn (input_location, 0,
5224 : "empty declaration with storage class specifier "
5225 : "does not redeclare tag");
5226 2 : warned = 1;
5227 2 : pending_xref_error ();
5228 : }
5229 442150 : else if (declspecs->typespec_kind != ctsk_tagdef
5230 88137 : && declspecs->typespec_kind != ctsk_tagfirstref
5231 26220 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5232 26212 : && (declspecs->const_p
5233 26209 : || declspecs->volatile_p
5234 26209 : || declspecs->atomic_p
5235 26209 : || declspecs->restrict_p
5236 26209 : || declspecs->address_space))
5237 : {
5238 3 : if (warned != 1)
5239 3 : pedwarn (input_location, 0,
5240 : "empty declaration with type qualifier "
5241 : "does not redeclare tag");
5242 3 : warned = 1;
5243 3 : pending_xref_error ();
5244 : }
5245 442147 : else if (declspecs->typespec_kind != ctsk_tagdef
5246 88134 : && declspecs->typespec_kind != ctsk_tagfirstref
5247 26217 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5248 26209 : && declspecs->alignas_p)
5249 : {
5250 1 : if (warned != 1)
5251 1 : pedwarn (input_location, 0,
5252 : "empty declaration with %<_Alignas%> "
5253 : "does not redeclare tag");
5254 1 : warned = 1;
5255 1 : pending_xref_error ();
5256 : }
5257 442146 : else if (declspecs->typespec_kind != ctsk_tagdef
5258 88133 : && declspecs->typespec_kind != ctsk_tagfirstref
5259 26216 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5260 26208 : && code == ENUMERAL_TYPE
5261 15 : && !declspecs->enum_type_specifier_ref_p)
5262 : {
5263 3 : bool warned_enum = false;
5264 3 : if (warned != 1)
5265 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5266 : "empty declaration of %<enum%> type "
5267 : "does not redeclare tag");
5268 3 : if (warned_enum)
5269 2 : warned = 1;
5270 3 : pending_xref_error ();
5271 3 : }
5272 : else
5273 : {
5274 442143 : pending_invalid_xref = NULL_TREE;
5275 442143 : t = lookup_tag (code, name, true, NULL);
5276 :
5277 442143 : if (t == NULL_TREE)
5278 : {
5279 2 : t = make_node (code);
5280 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5281 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5282 2 : pushtag (input_location, name, t);
5283 : }
5284 : }
5285 : }
5286 : else
5287 : {
5288 51 : if (warned != 1 && !in_system_header_at (input_location))
5289 : {
5290 44 : pedwarn (input_location, 0,
5291 : "useless type name in empty declaration");
5292 44 : warned = 1;
5293 : }
5294 : }
5295 : }
5296 71 : else if (warned != 1 && !in_system_header_at (input_location)
5297 185 : && declspecs->typedef_p)
5298 : {
5299 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5300 11 : warned = 1;
5301 : }
5302 :
5303 502264 : pending_invalid_xref = NULL_TREE;
5304 :
5305 502264 : if (declspecs->inline_p)
5306 : {
5307 5 : error ("%<inline%> in empty declaration");
5308 5 : warned = 1;
5309 : }
5310 :
5311 502264 : if (declspecs->noreturn_p)
5312 : {
5313 2 : error ("%<_Noreturn%> in empty declaration");
5314 2 : warned = 1;
5315 : }
5316 :
5317 502264 : if (declspecs->constexpr_p)
5318 : {
5319 7 : error ("%<constexpr%> in empty declaration");
5320 7 : warned = 1;
5321 : }
5322 :
5323 502264 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5324 : {
5325 2 : error ("%<auto%> in file-scope empty declaration");
5326 2 : warned = 1;
5327 : }
5328 :
5329 502264 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5330 : {
5331 2 : error ("%<register%> in file-scope empty declaration");
5332 2 : warned = 1;
5333 : }
5334 :
5335 502264 : if (declspecs->enum_type_specifier_ref_p && !warned)
5336 : {
5337 39 : if (declspecs->storage_class != csc_none)
5338 : {
5339 1 : error ("storage class specifier in empty declaration with %<enum%> "
5340 : "underlying type");
5341 1 : warned = 1;
5342 : }
5343 38 : else if (declspecs->thread_p)
5344 : {
5345 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5346 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5347 1 : warned = 1;
5348 : }
5349 37 : else if (declspecs->const_p
5350 36 : || declspecs->volatile_p
5351 36 : || declspecs->atomic_p
5352 36 : || declspecs->restrict_p
5353 36 : || declspecs->address_space)
5354 : {
5355 1 : error ("type qualifier in empty declaration with %<enum%> "
5356 : "underlying type");
5357 1 : warned = 1;
5358 : }
5359 36 : else if (declspecs->alignas_p)
5360 : {
5361 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5362 : "underlying type");
5363 1 : warned = 1;
5364 : }
5365 : }
5366 :
5367 502096 : if (!warned && !in_system_header_at (input_location)
5368 620432 : && declspecs->storage_class != csc_none)
5369 : {
5370 12 : warning (0, "useless storage class specifier in empty declaration");
5371 12 : warned = 2;
5372 : }
5373 :
5374 502264 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5375 : {
5376 3 : warning (0, "useless %qs in empty declaration",
5377 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5378 3 : warned = 2;
5379 : }
5380 :
5381 3 : if (!warned
5382 502077 : && !in_system_header_at (input_location)
5383 620422 : && (declspecs->const_p
5384 118153 : || declspecs->volatile_p
5385 118153 : || declspecs->atomic_p
5386 118153 : || declspecs->restrict_p
5387 118153 : || declspecs->address_space))
5388 : {
5389 8 : warning (0, "useless type qualifier in empty declaration");
5390 8 : warned = 2;
5391 : }
5392 :
5393 502077 : if (!warned && !in_system_header_at (input_location)
5394 620409 : && declspecs->alignas_p)
5395 : {
5396 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5397 4 : warned = 2;
5398 : }
5399 :
5400 502264 : if (found_tag
5401 502264 : && warned == 2
5402 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5403 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5404 : {
5405 : /* Standard attributes after the "struct" or "union" keyword are
5406 : only permitted when the contents of the type are defined, or
5407 : in the form "struct-or-union attribute-specifier-sequence
5408 : identifier;". If the ';' was not present, attributes were
5409 : diagnosed in the parser. Here, ensure that any other useless
5410 : elements of the declaration result in a pedwarn, not just a
5411 : warning. Forward declarations of enum types are not part of
5412 : standard C, but handle them the same. */
5413 2 : pedwarn (input_location, 0,
5414 : "invalid use of attributes in empty declaration");
5415 2 : warned = 1;
5416 : }
5417 :
5418 502264 : if (warned != 1)
5419 : {
5420 502090 : if (declspecs->declspecs_seen_p
5421 502090 : && !declspecs->non_std_attrs_seen_p)
5422 : /* An attribute declaration (but not a fallthrough attribute
5423 : declaration, which was handled separately); warn if there
5424 : are any attributes being ignored (but not if the attributes
5425 : were empty). */
5426 48 : c_warn_unused_attributes (declspecs->attrs);
5427 502042 : else if (!found_tag)
5428 10 : pedwarn (input_location, 0, "empty declaration");
5429 : }
5430 502264 : }
5431 :
5432 :
5433 : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5434 : bits. SPECS represents declaration specifiers that the grammar
5435 : only permits to contain type qualifiers and attributes. */
5436 :
5437 : int
5438 18237655 : quals_from_declspecs (const struct c_declspecs *specs)
5439 : {
5440 18237655 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5441 : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5442 : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5443 18237655 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5444 18237655 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5445 18237655 : gcc_assert (!specs->type
5446 : && !specs->decl_attr
5447 : && specs->typespec_word == cts_none
5448 : && specs->storage_class == csc_none
5449 : && !specs->typedef_p
5450 : && !specs->explicit_signed_p
5451 : && !specs->deprecated_p
5452 : && !specs->unavailable_p
5453 : && !specs->long_p
5454 : && !specs->long_long_p
5455 : && !specs->short_p
5456 : && !specs->signed_p
5457 : && !specs->unsigned_p
5458 : && !specs->complex_p
5459 : && !specs->inline_p
5460 : && !specs->noreturn_p
5461 : && !specs->thread_p);
5462 18237655 : return quals;
5463 : }
5464 :
5465 : /* Construct an array declarator. LOC is the location of the
5466 : beginning of the array (usually the opening brace). EXPR is the
5467 : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5468 : inside the [] (to be applied to the pointer to which a parameter
5469 : array is converted). STATIC_P is true if "static" is inside the
5470 : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5471 : VLA of unspecified length which is nevertheless a complete type,
5472 : false otherwise. The field for the contained declarator is left to
5473 : be filled in by set_array_declarator_inner. */
5474 :
5475 : struct c_declarator *
5476 1153555 : build_array_declarator (location_t loc,
5477 : tree expr, struct c_declspecs *quals, bool static_p,
5478 : bool vla_unspec_p)
5479 : {
5480 1153555 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5481 : struct c_declarator);
5482 1153555 : declarator->id_loc = loc;
5483 1153555 : declarator->kind = cdk_array;
5484 1153555 : declarator->declarator = 0;
5485 1153555 : declarator->u.array.dimen = expr;
5486 1153555 : if (quals)
5487 : {
5488 966 : declarator->u.array.attrs = quals->attrs;
5489 966 : declarator->u.array.quals = quals_from_declspecs (quals);
5490 : }
5491 : else
5492 : {
5493 1152589 : declarator->u.array.attrs = NULL_TREE;
5494 1152589 : declarator->u.array.quals = 0;
5495 : }
5496 1153555 : declarator->u.array.static_p = static_p;
5497 1153555 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5498 1153555 : if (static_p || quals != NULL)
5499 1069 : pedwarn_c90 (loc, OPT_Wpedantic,
5500 : "ISO C90 does not support %<static%> or type "
5501 : "qualifiers in parameter array declarators");
5502 1153555 : if (vla_unspec_p)
5503 : {
5504 151 : pedwarn_c90 (loc, OPT_Wpedantic,
5505 : "ISO C90 does not support %<[*]%> array declarators");
5506 151 : if (current_scope->parm_flag)
5507 143 : current_scope->had_vla_unspec = true;
5508 : }
5509 1153555 : return declarator;
5510 : }
5511 :
5512 : /* Set the contained declarator of an array declarator. DECL is the
5513 : declarator, as constructed by build_array_declarator; INNER is what
5514 : appears on the left of the []. */
5515 :
5516 : struct c_declarator *
5517 1153555 : set_array_declarator_inner (struct c_declarator *decl,
5518 : struct c_declarator *inner)
5519 : {
5520 1153555 : decl->declarator = inner;
5521 1153555 : return decl;
5522 : }
5523 :
5524 : /* Determine whether TYPE is a one-element array type "[1]". */
5525 : static bool
5526 534302 : one_element_array_type_p (const_tree type)
5527 : {
5528 534302 : if (TREE_CODE (type) != ARRAY_TYPE)
5529 : return false;
5530 534302 : return integer_zerop (array_type_nelts_minus_one (type));
5531 : }
5532 :
5533 : /* Determine whether TYPE is a zero-length array type "[0]". */
5534 : static bool
5535 534302 : zero_length_array_type_p (const_tree type)
5536 : {
5537 534302 : if (TREE_CODE (type) == ARRAY_TYPE)
5538 534302 : if (tree type_size = TYPE_SIZE_UNIT (type))
5539 448315 : if ((integer_zerop (type_size))
5540 1824 : && TYPE_DOMAIN (type) != NULL_TREE
5541 450139 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5542 : return true;
5543 : return false;
5544 : }
5545 :
5546 : /* INIT is a constructor that forms DECL's initializer. If the final
5547 : element initializes a flexible array field, adjust the size of the
5548 : DECL with the initializer based on whether the DECL is a union or
5549 : a structure. */
5550 :
5551 : static void
5552 104984 : add_flexible_array_elts_to_size (tree decl, tree init)
5553 : {
5554 104984 : tree elt, type;
5555 :
5556 104984 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5557 1461 : return;
5558 :
5559 103523 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5560 103523 : type = TREE_TYPE (elt);
5561 103523 : if (c_flexible_array_member_type_p (type))
5562 : {
5563 221 : complete_array_type (&type, elt, false);
5564 : /* For a structure, add the size of the initializer to the DECL's
5565 : size. */
5566 221 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5567 : {
5568 211 : DECL_SIZE (decl)
5569 211 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5570 211 : DECL_SIZE_UNIT (decl)
5571 422 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5572 : TYPE_SIZE_UNIT (type));
5573 : }
5574 : /* For a union, the DECL's size is the maximum of the current size
5575 : and the size of the initializer. */
5576 : else
5577 : {
5578 10 : DECL_SIZE (decl)
5579 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5580 10 : DECL_SIZE_UNIT (decl)
5581 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5582 : TYPE_SIZE_UNIT (type));
5583 : }
5584 : }
5585 : }
5586 :
5587 : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5588 : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5589 : before the type name, and set *EXPR_CONST_OPERANDS, if
5590 : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5591 : appear in a constant expression. */
5592 :
5593 : tree
5594 121498942 : groktypename (struct c_type_name *type_name, tree *expr,
5595 : bool *expr_const_operands)
5596 : {
5597 121498942 : tree type;
5598 121498942 : tree attrs = type_name->specs->attrs;
5599 :
5600 121498942 : type_name->specs->attrs = NULL_TREE;
5601 :
5602 121498942 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5603 : false, NULL, &attrs, expr, expr_const_operands,
5604 : DEPRECATED_NORMAL);
5605 :
5606 : /* Apply attributes. */
5607 121498942 : attrs = c_warn_type_attributes (type, attrs);
5608 121498942 : decl_attributes (&type, attrs, 0);
5609 :
5610 121498942 : return type;
5611 : }
5612 :
5613 :
5614 : /* Decode a "typename", such as "int **", returning a ..._TYPE node,
5615 : as for groktypename but setting the context to GENERIC_ASSOC. */
5616 :
5617 : tree
5618 820 : grokgenassoc (struct c_type_name *type_name)
5619 : {
5620 820 : tree type;
5621 820 : tree attrs = type_name->specs->attrs;
5622 :
5623 820 : type_name->specs->attrs = NULL_TREE;
5624 :
5625 820 : type = grokdeclarator (type_name->declarator, type_name->specs, GENERIC_ASSOC,
5626 : false, NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
5627 :
5628 : /* Apply attributes. */
5629 820 : attrs = c_warn_type_attributes (type, attrs);
5630 820 : decl_attributes (&type, attrs, 0);
5631 :
5632 820 : return type;
5633 : }
5634 :
5635 :
5636 : /* Looks up the most recent pushed declaration corresponding to DECL. */
5637 :
5638 : static tree
5639 92182007 : lookup_last_decl (tree decl)
5640 : {
5641 92182007 : tree last_decl = lookup_name (DECL_NAME (decl));
5642 92182007 : if (!last_decl)
5643 89620702 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5644 92182007 : return last_decl;
5645 : }
5646 :
5647 : /* Wrapper for decl_attributes that adds some implicit attributes
5648 : to VAR_DECLs or FUNCTION_DECLs. */
5649 :
5650 : static tree
5651 64241720 : c_decl_attributes (tree *node, tree attributes, int flags)
5652 : {
5653 : /* Add implicit "omp declare target" attribute if requested. */
5654 64241720 : if (vec_safe_length (current_omp_declare_target_attribute)
5655 9166 : && ((VAR_P (*node) && is_global_var (*node))
5656 2728 : || TREE_CODE (*node) == FUNCTION_DECL))
5657 : {
5658 1130 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5659 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5660 : NULL_TREE, attributes);
5661 : else
5662 1117 : attributes = tree_cons (get_identifier ("omp declare target"),
5663 : NULL_TREE, attributes);
5664 1130 : if (TREE_CODE (*node) == FUNCTION_DECL)
5665 : {
5666 1000 : int device_type
5667 1000 : = current_omp_declare_target_attribute->last ().device_type;
5668 1000 : device_type = MAX (device_type, 0);
5669 1000 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5670 1000 : && !lookup_attribute ("omp declare target host", attributes))
5671 2 : attributes
5672 2 : = tree_cons (get_identifier ("omp declare target host"),
5673 : NULL_TREE, attributes);
5674 1000 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5675 1000 : && !lookup_attribute ("omp declare target nohost", attributes))
5676 2 : attributes
5677 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5678 : NULL_TREE, attributes);
5679 :
5680 1000 : int indirect
5681 1000 : = current_omp_declare_target_attribute->last ().indirect;
5682 1000 : if (indirect && !lookup_attribute ("omp declare target indirect",
5683 : attributes))
5684 10 : attributes
5685 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5686 : NULL_TREE, attributes);
5687 : }
5688 : }
5689 :
5690 64241720 : if (flag_openmp || flag_openmp_simd)
5691 : {
5692 : bool diagnosed = false;
5693 1367154 : for (tree *pa = &attributes; *pa; )
5694 : {
5695 881588 : if (is_attribute_namespace_p ("omp", *pa))
5696 : {
5697 65 : tree name = get_attribute_name (*pa);
5698 65 : if (is_attribute_p ("directive", name)
5699 0 : || is_attribute_p ("sequence", name)
5700 65 : || is_attribute_p ("decl", name))
5701 : {
5702 65 : const char *p = NULL;
5703 65 : if (TREE_VALUE (*pa) == NULL_TREE)
5704 12 : p = IDENTIFIER_POINTER (name);
5705 118 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5706 : {
5707 53 : tree d = TREE_VALUE (a);
5708 53 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5709 96 : if (TREE_PUBLIC (d)
5710 43 : && (VAR_P (*node)
5711 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5712 96 : && c_maybe_parse_omp_decl (*node, d))
5713 43 : continue;
5714 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5715 : }
5716 65 : if (p && !diagnosed)
5717 : {
5718 22 : error ("%<omp::%s%> not allowed to be specified in "
5719 : "this context", p);
5720 22 : diagnosed = true;
5721 : }
5722 65 : if (p)
5723 : {
5724 22 : *pa = TREE_CHAIN (*pa);
5725 22 : continue;
5726 : }
5727 : }
5728 : }
5729 881566 : pa = &TREE_CHAIN (*pa);
5730 : }
5731 : }
5732 :
5733 : /* Look up the current declaration with all the attributes merged
5734 : so far so that attributes on the current declaration that's
5735 : about to be pushed that conflict with the former can be detected,
5736 : diagnosed, and rejected as appropriate. To match the C++ FE, do
5737 : not pass an error_mark_node when we found an undeclared variable. */
5738 64241720 : tree last_decl = lookup_last_decl (*node);
5739 64241720 : if (last_decl == error_mark_node)
5740 20 : last_decl = NULL_TREE;
5741 64241720 : tree attr = decl_attributes (node, attributes, flags, last_decl);
5742 64241719 : if (VAR_P (*node) && DECL_THREAD_LOCAL_P (*node))
5743 : {
5744 : // tls_model attribute can set a stronger TLS access model.
5745 2831 : tls_model model = DECL_TLS_MODEL (*node);
5746 2831 : tls_model default_model = decl_default_tls_model (*node);
5747 2831 : if (default_model > model)
5748 1577 : set_decl_tls_model (*node, default_model);
5749 : }
5750 64241719 : return attr;
5751 : }
5752 :
5753 :
5754 : /* Decode a declarator in an ordinary declaration or data definition.
5755 : This is called as soon as the type information and variable name
5756 : have been parsed, before parsing the initializer if any.
5757 : Here we create the ..._DECL node, fill in its type,
5758 : and (if DO_PUSH) put it on the list of decls for the current context.
5759 : When nonnull, set *LASTLOC to the location of the prior declaration
5760 : of the same entity if one exists.
5761 : The ..._DECL node is returned as the value.
5762 :
5763 : Exception: for arrays where the length is not specified,
5764 : the type is left null, to be filled in by `finish_decl'.
5765 :
5766 : Function definitions do not come here; they go to start_function
5767 : instead. However, external and forward declarations of functions
5768 : do go through here. Structure field declarations are done by
5769 : grokfield and not through here. */
5770 :
5771 : tree
5772 27940311 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5773 : bool initialized, tree attributes, bool do_push /* = true */,
5774 : location_t *lastloc /* = NULL */)
5775 : {
5776 27940311 : tree decl;
5777 27940311 : tree old_decl;
5778 27940311 : tree tem;
5779 27940311 : tree expr = NULL_TREE;
5780 27940311 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5781 :
5782 : /* An object declared as __attribute__((unavailable)) suppresses
5783 : warnings and errors from __attribute__((deprecated/unavailable))
5784 : components.
5785 : An object declared as __attribute__((deprecated)) suppresses
5786 : warnings of uses of other deprecated items. */
5787 27940311 : if (lookup_attribute ("unavailable", attributes))
5788 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5789 27940287 : else if (lookup_attribute ("deprecated", attributes))
5790 32308 : deprecated_state = DEPRECATED_SUPPRESS;
5791 :
5792 27940311 : decl = grokdeclarator (declarator, declspecs,
5793 : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5794 : deprecated_state);
5795 27940311 : if (!decl || decl == error_mark_node)
5796 : return NULL_TREE;
5797 :
5798 27940287 : old_decl = lookup_last_decl (decl);
5799 :
5800 27940287 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5801 4750792 : if (lastdecl != error_mark_node)
5802 4750783 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5803 :
5804 : /* Make sure the size expression is evaluated at this point. */
5805 27940287 : if (expr && !current_scope->parm_flag)
5806 12970 : add_stmt (fold_convert (void_type_node, expr));
5807 :
5808 13257746 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5809 27940289 : && TREE_PUBLIC (decl))
5810 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5811 :
5812 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5813 27940300 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5814 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5815 : "no previous declaration for %qD", decl);
5816 :
5817 27940287 : if (initialized)
5818 : /* Is it valid for this decl to have an initializer at all?
5819 : If not, set INITIALIZED to zero, which will indirectly
5820 : tell 'finish_decl' to ignore the initializer once it is parsed. */
5821 6345276 : switch (TREE_CODE (decl))
5822 : {
5823 6 : case TYPE_DECL:
5824 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5825 6 : initialized = false;
5826 6 : break;
5827 :
5828 9 : case FUNCTION_DECL:
5829 9 : error ("function %qD is initialized like a variable", decl);
5830 9 : initialized = false;
5831 9 : break;
5832 :
5833 49 : case PARM_DECL:
5834 : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5835 49 : error ("parameter %qD is initialized", decl);
5836 49 : initialized = false;
5837 49 : break;
5838 :
5839 6345212 : default:
5840 : /* Don't allow initializations for incomplete types except for
5841 : arrays which might be completed by the initialization. */
5842 :
5843 : /* This can happen if the array size is an undefined macro.
5844 : We already gave a warning, so we don't need another one. */
5845 6345212 : if (TREE_TYPE (decl) == error_mark_node)
5846 : initialized = false;
5847 6345190 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5848 : {
5849 : /* A complete type is ok if size is fixed. If the size is
5850 : variable, an empty initializer is OK and nonempty
5851 : initializers will be diagnosed in the parser. */
5852 : }
5853 12240 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5854 : {
5855 3 : error ("variable %qD has initializer but incomplete type", decl);
5856 3 : initialized = false;
5857 : }
5858 : }
5859 :
5860 67 : if (initialized)
5861 : {
5862 6345187 : if (current_scope == file_scope)
5863 155666 : TREE_STATIC (decl) = 1;
5864 :
5865 : /* Tell 'pushdecl' this is an initialized decl
5866 : even though we don't yet have the initializer expression.
5867 : Also tell 'finish_decl' it may store the real initializer. */
5868 6345187 : DECL_INITIAL (decl) = error_mark_node;
5869 : }
5870 :
5871 : /* If this is a function declaration, write a record describing it to the
5872 : prototypes file (if requested). */
5873 :
5874 27940287 : if (TREE_CODE (decl) == FUNCTION_DECL)
5875 14682541 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5876 :
5877 : /* ANSI specifies that a tentative definition which is not merged with
5878 : a non-tentative definition behaves exactly like a definition with an
5879 : initializer equal to zero. (Section 3.7.2)
5880 :
5881 : -fno-common gives strict ANSI behavior, though this tends to break
5882 : a large body of code that grew up without this rule.
5883 :
5884 : Thread-local variables are never common, since there's no entrenched
5885 : body of code to break, and it allows more efficient variable references
5886 : in the presence of dynamic linking. */
5887 :
5888 27940287 : if (VAR_P (decl)
5889 8918152 : && !initialized
5890 2572965 : && TREE_PUBLIC (decl)
5891 990670 : && !DECL_THREAD_LOCAL_P (decl)
5892 28928594 : && !flag_no_common)
5893 128 : DECL_COMMON (decl) = 1;
5894 :
5895 : /* Set attributes here so if duplicate decl, will have proper attributes. */
5896 27940287 : c_decl_attributes (&decl, attributes, 0);
5897 :
5898 : /* Handle gnu_inline attribute. */
5899 27940286 : if (declspecs->inline_p
5900 1029 : && !flag_gnu89_inline
5901 997 : && TREE_CODE (decl) == FUNCTION_DECL
5902 27941275 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5903 966 : || current_function_decl))
5904 : {
5905 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5906 : ;
5907 37 : else if (declspecs->storage_class != csc_static)
5908 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5909 : }
5910 :
5911 27940286 : if (TREE_CODE (decl) == FUNCTION_DECL
5912 14682540 : && DECL_DECLARED_INLINE_P (decl)
5913 1019 : && DECL_UNINLINABLE (decl)
5914 27940288 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5915 : {
5916 2 : auto_urlify_attributes sentinel;
5917 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5918 : decl, "noinline");
5919 2 : }
5920 :
5921 : /* C99 6.7.4p3: An inline definition of a function with external
5922 : linkage shall not contain a definition of a modifiable object
5923 : with static storage duration... */
5924 27940286 : if (VAR_P (decl)
5925 8918152 : && current_scope != file_scope
5926 7764881 : && TREE_STATIC (decl)
5927 78808 : && !TREE_READONLY (decl)
5928 76236 : && DECL_DECLARED_INLINE_P (current_function_decl)
5929 27940356 : && DECL_EXTERNAL (current_function_decl))
5930 7 : record_inline_static (input_location, current_function_decl,
5931 : decl, csi_modifiable);
5932 :
5933 27940286 : if (c_dialect_objc ()
5934 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5935 0 : objc_check_global_decl (decl);
5936 :
5937 : /* To enable versions to be created across TU's we mark and mangle all
5938 : non-default versioned functions. */
5939 27940286 : if (TREE_CODE (decl) == FUNCTION_DECL
5940 : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
5941 : && get_target_version (decl).is_valid ())
5942 : {
5943 : maybe_mark_function_versioned (decl);
5944 : if (current_scope != file_scope)
5945 : error ("versioned declarations are only allowed at file scope");
5946 : }
5947 :
5948 : /* Add this decl to the current scope.
5949 : TEM may equal DECL or it may be a previous decl of the same name. */
5950 27940286 : if (do_push)
5951 : {
5952 27939938 : tem = pushdecl (decl);
5953 :
5954 27939938 : if (initialized && DECL_EXTERNAL (tem))
5955 : {
5956 27 : DECL_EXTERNAL (tem) = 0;
5957 27 : TREE_STATIC (tem) = 1;
5958 : }
5959 :
5960 27939938 : return tem;
5961 : }
5962 : else
5963 348 : return decl;
5964 : }
5965 :
5966 : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5967 : DECL or the non-array element type if DECL is an uninitialized array.
5968 : If that type has a const member, diagnose this. */
5969 :
5970 : static void
5971 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5972 : {
5973 7 : tree field;
5974 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5975 : {
5976 10 : tree field_type;
5977 10 : if (TREE_CODE (field) != FIELD_DECL)
5978 0 : continue;
5979 10 : field_type = strip_array_types (TREE_TYPE (field));
5980 :
5981 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5982 : {
5983 5 : auto_diagnostic_group d;
5984 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5985 : "uninitialized const member in %qT is invalid in C++",
5986 5 : strip_array_types (TREE_TYPE (decl))))
5987 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5988 5 : }
5989 :
5990 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5991 2 : diagnose_uninitialized_cst_member (decl, field_type);
5992 : }
5993 7 : }
5994 :
5995 : /* Finish processing of a declaration;
5996 : install its initial value.
5997 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5998 : If the length of an array type is not known before,
5999 : it must be determined now, from the initial value, or it is an error.
6000 :
6001 : INIT_LOC is the location of the initial value. */
6002 :
6003 : void
6004 156295589 : finish_decl (tree decl, location_t init_loc, tree init,
6005 : tree origtype, tree asmspec_tree)
6006 : {
6007 156295589 : tree type;
6008 156295589 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
6009 156295589 : const char *asmspec = 0;
6010 :
6011 : /* If a name was specified, get the string. */
6012 147374449 : if (VAR_OR_FUNCTION_DECL_P (decl)
6013 170978129 : && DECL_FILE_SCOPE_P (decl))
6014 15837328 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6015 156295589 : if (asmspec_tree)
6016 871356 : asmspec = TREE_STRING_POINTER (asmspec_tree);
6017 :
6018 156295589 : if (VAR_P (decl)
6019 8921140 : && TREE_STATIC (decl)
6020 157357225 : && global_bindings_p ())
6021 : /* So decl is a global variable. Record the types it uses
6022 : so that we can decide later to emit debug info for them. */
6023 979623 : record_types_used_by_current_var_decl (decl);
6024 :
6025 : /* If `start_decl' didn't like having an initialization, ignore it now. */
6026 156295589 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
6027 : init = NULL_TREE;
6028 :
6029 : /* Don't crash if parm is initialized. */
6030 156295589 : if (TREE_CODE (decl) == PARM_DECL)
6031 : init = NULL_TREE;
6032 :
6033 32246512 : if (init)
6034 6348176 : store_init_value (init_loc, decl, init, origtype);
6035 :
6036 156295589 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
6037 0 : || TREE_CODE (decl) == FIELD_DECL))
6038 0 : objc_check_decl (decl);
6039 :
6040 156295589 : type = TREE_TYPE (decl);
6041 :
6042 : /* Deduce size of array from initialization, if not already known.
6043 : This is only needed for an initialization in the current scope;
6044 : it must not be done for a file-scope initialization of a
6045 : declaration with external linkage, redeclared in an inner scope
6046 : with the outer declaration shadowed in an intermediate scope. */
6047 156295589 : if (TREE_CODE (type) == ARRAY_TYPE
6048 868758 : && TYPE_DOMAIN (type) == NULL_TREE
6049 19868 : && TREE_CODE (decl) != TYPE_DECL
6050 156315359 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
6051 : {
6052 19583 : bool do_default
6053 19583 : = !TREE_STATIC (decl) && !DECL_EXTERNAL (decl);
6054 19583 : int failure
6055 19583 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
6056 : do_default);
6057 :
6058 : /* Get the completed type made by complete_array_type. */
6059 19583 : type = TREE_TYPE (decl);
6060 :
6061 19583 : switch (failure)
6062 : {
6063 0 : case 1:
6064 0 : error ("initializer fails to determine size of %q+D", decl);
6065 0 : break;
6066 :
6067 7371 : case 2:
6068 7371 : if (do_default)
6069 1 : error ("array size missing in %q+D", decl);
6070 7370 : else if (!TREE_PUBLIC (decl))
6071 37 : pedwarn_c23 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
6072 : "array size missing in %q+D", decl);
6073 : break;
6074 :
6075 3 : case 3:
6076 3 : error ("zero or negative size array %q+D", decl);
6077 3 : break;
6078 :
6079 12209 : case 0:
6080 : /* For global variables, update the copy of the type that
6081 : exists in the binding. */
6082 12209 : if (TREE_PUBLIC (decl))
6083 : {
6084 3823 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
6085 7646 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
6086 3823 : b_ext = b_ext->shadowed;
6087 3823 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
6088 : {
6089 3823 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
6090 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
6091 : else
6092 3647 : b_ext->u.type = type;
6093 : }
6094 : }
6095 : break;
6096 :
6097 0 : default:
6098 0 : gcc_unreachable ();
6099 : }
6100 :
6101 19583 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
6102 11340 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6103 :
6104 19583 : relayout_decl (decl);
6105 : }
6106 :
6107 : /* Look for braced array initializers for character arrays and
6108 : recursively convert them into STRING_CSTs. */
6109 156295589 : if (tree init = DECL_INITIAL (decl))
6110 130399660 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
6111 :
6112 156295589 : if (VAR_P (decl))
6113 : {
6114 8921140 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6115 104984 : add_flexible_array_elts_to_size (decl, init);
6116 :
6117 8921140 : complete_flexible_array_elts (DECL_INITIAL (decl));
6118 :
6119 8921140 : if (is_global_var (decl))
6120 : {
6121 1236592 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
6122 1236592 : ? TCTX_THREAD_STORAGE
6123 : : TCTX_STATIC_STORAGE);
6124 1236592 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
6125 0 : TREE_TYPE (decl) = error_mark_node;
6126 : }
6127 :
6128 8929253 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
6129 8929150 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
6130 107 : layout_decl (decl, 0);
6131 :
6132 8921140 : if (DECL_SIZE (decl) == NULL_TREE
6133 : /* Don't give an error if we already gave one earlier. */
6134 8006 : && TREE_TYPE (decl) != error_mark_node
6135 8929043 : && (TREE_STATIC (decl)
6136 : /* A static variable with an incomplete type
6137 : is an error if it is initialized.
6138 : Also if it is not file scope.
6139 : Also if it is thread-local (in C23).
6140 : Otherwise, let it through, but if it is not `extern'
6141 : then it may cause an error message later. */
6142 309 : ? (DECL_INITIAL (decl) != NULL_TREE
6143 309 : || !DECL_FILE_SCOPE_P (decl)
6144 306 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
6145 : /* An automatic variable with an incomplete type
6146 : is an error. */
6147 7594 : : !DECL_EXTERNAL (decl)))
6148 : {
6149 19 : error ("storage size of %q+D isn%'t known", decl);
6150 19 : TREE_TYPE (decl) = error_mark_node;
6151 : }
6152 :
6153 17652659 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6154 8647358 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6155 287358 : && DECL_SIZE (decl) == NULL_TREE
6156 8921428 : && TREE_STATIC (decl))
6157 135 : incomplete_record_decls.safe_push (decl);
6158 :
6159 8921140 : if (is_global_var (decl)
6160 1236592 : && DECL_SIZE (decl) != NULL_TREE
6161 10149769 : && TREE_TYPE (decl) != error_mark_node)
6162 : {
6163 1228610 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6164 1228607 : constant_expression_warning (DECL_SIZE (decl));
6165 : else
6166 : {
6167 3 : error ("storage size of %q+D isn%'t constant", decl);
6168 3 : TREE_TYPE (decl) = error_mark_node;
6169 : }
6170 : }
6171 :
6172 8921140 : if (TREE_USED (type))
6173 : {
6174 4 : TREE_USED (decl) = 1;
6175 4 : DECL_READ_P (decl) = 1;
6176 : }
6177 : }
6178 :
6179 : /* If this is a function and an assembler name is specified, reset DECL_RTL
6180 : so we can give it its new name. Also, update builtin_decl if it
6181 : was a normal built-in. */
6182 156295589 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6183 : {
6184 858169 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6185 71986 : set_builtin_user_assembler_name (decl, asmspec);
6186 858169 : set_user_assembler_name (decl, asmspec);
6187 : }
6188 :
6189 : /* If #pragma weak was used, mark the decl weak now. */
6190 156295589 : maybe_apply_pragma_weak (decl);
6191 :
6192 : /* Output the assembler code and/or RTL code for variables and functions,
6193 : unless the type is an undefined structure or union.
6194 : If not, it will get done when the type is completed. */
6195 :
6196 156295589 : if (VAR_OR_FUNCTION_DECL_P (decl))
6197 : {
6198 : /* Determine the ELF visibility. */
6199 23603680 : if (TREE_PUBLIC (decl))
6200 15812400 : c_determine_visibility (decl);
6201 :
6202 : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6203 23603680 : if (c_dialect_objc ())
6204 0 : objc_check_decl (decl);
6205 :
6206 23603680 : if (asmspec)
6207 : {
6208 : /* If this is not a static variable, issue a warning.
6209 : It doesn't make any sense to give an ASMSPEC for an
6210 : ordinary, non-register local variable. Historically,
6211 : GCC has accepted -- but ignored -- the ASMSPEC in
6212 : this case. */
6213 872394 : if (!DECL_FILE_SCOPE_P (decl)
6214 1038 : && VAR_P (decl)
6215 1038 : && !C_DECL_REGISTER (decl)
6216 871364 : && !TREE_STATIC (decl))
6217 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6218 : "variable %q+D", decl);
6219 : else
6220 871355 : set_user_assembler_name (decl, asmspec);
6221 : }
6222 :
6223 23603680 : if (DECL_FILE_SCOPE_P (decl))
6224 : {
6225 15837328 : if (DECL_INITIAL (decl) == NULL_TREE
6226 15837328 : || DECL_INITIAL (decl) == error_mark_node)
6227 : /* Don't output anything
6228 : when a tentative file-scope definition is seen.
6229 : But at end of compilation, do output code for them. */
6230 15681422 : DECL_DEFER_OUTPUT (decl) = 1;
6231 15837328 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6232 68 : DECL_HARD_REGISTER (decl) = 1;
6233 15837328 : rest_of_decl_compilation (decl, true, 0);
6234 :
6235 15837328 : if (TREE_CODE (decl) == FUNCTION_DECL)
6236 : {
6237 14682473 : tree parms = DECL_ARGUMENTS (decl);
6238 14682473 : const bool builtin = fndecl_built_in_p (decl);
6239 14682473 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6240 344514 : decl_attributes (&decl, access, 0);
6241 : }
6242 : }
6243 : else
6244 : {
6245 : /* In conjunction with an ASMSPEC, the `register'
6246 : keyword indicates that we should place the variable
6247 : in a particular register. */
6248 7766352 : if (asmspec && C_DECL_REGISTER (decl))
6249 : {
6250 1030 : DECL_HARD_REGISTER (decl) = 1;
6251 : /* This cannot be done for a structure with volatile
6252 : fields, on which DECL_REGISTER will have been
6253 : reset. */
6254 1030 : if (!DECL_REGISTER (decl))
6255 1 : error ("cannot put object with volatile field into register");
6256 : }
6257 :
6258 7766352 : if (TREE_CODE (decl) != FUNCTION_DECL)
6259 : {
6260 : /* If we're building a variable sized type, and we might be
6261 : reachable other than via the top of the current binding
6262 : level, then create a new BIND_EXPR so that we deallocate
6263 : the object at the right time. */
6264 : /* Note that DECL_SIZE can be null due to errors. */
6265 7766285 : if (DECL_SIZE (decl)
6266 7766239 : && !TREE_CONSTANT (DECL_SIZE (decl))
6267 7773664 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6268 : {
6269 1337 : tree bind;
6270 1337 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6271 1337 : TREE_SIDE_EFFECTS (bind) = 1;
6272 1337 : add_stmt (bind);
6273 1337 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6274 : }
6275 7766285 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6276 : DECL_EXPR, decl));
6277 : }
6278 : }
6279 :
6280 :
6281 23603680 : if (!DECL_FILE_SCOPE_P (decl))
6282 : {
6283 : /* Recompute the RTL of a local array now
6284 : if it used to be an incomplete type. */
6285 7766352 : if (was_incomplete && !is_global_var (decl))
6286 : {
6287 : /* If we used it already as memory, it must stay in memory. */
6288 5284 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6289 : /* If it's still incomplete now, no init will save it. */
6290 5284 : if (DECL_SIZE (decl) == NULL_TREE)
6291 109 : DECL_INITIAL (decl) = NULL_TREE;
6292 : }
6293 : }
6294 : }
6295 :
6296 156295589 : if (TREE_CODE (decl) == TYPE_DECL)
6297 : {
6298 4445131 : if (!DECL_FILE_SCOPE_P (decl)
6299 4445131 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6300 5321 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6301 :
6302 8505662 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6303 : }
6304 :
6305 : /* Install a cleanup (aka destructor) if one was given. */
6306 156295589 : if (VAR_P (decl) && !TREE_STATIC (decl))
6307 : {
6308 7859501 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6309 7859501 : if (attr)
6310 : {
6311 131 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6312 131 : tree cleanup_decl = lookup_name (cleanup_id);
6313 131 : tree cleanup;
6314 131 : vec<tree, va_gc> *v;
6315 :
6316 : /* Build "cleanup(&decl)" for the destructor. */
6317 131 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6318 131 : vec_alloc (v, 1);
6319 131 : v->quick_push (cleanup);
6320 131 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6321 131 : vNULL, cleanup_decl, v, NULL);
6322 131 : vec_free (v);
6323 :
6324 : /* Don't warn about decl unused; the cleanup uses it. */
6325 131 : TREE_USED (decl) = 1;
6326 131 : TREE_USED (cleanup_decl) = 1;
6327 131 : DECL_READ_P (decl) = 1;
6328 :
6329 131 : push_cleanup (decl, cleanup, false);
6330 : }
6331 : }
6332 :
6333 156295589 : if (warn_cxx_compat
6334 22413 : && VAR_P (decl)
6335 7549 : && !DECL_EXTERNAL (decl)
6336 156302695 : && DECL_INITIAL (decl) == NULL_TREE)
6337 : {
6338 2333 : type = strip_array_types (type);
6339 2333 : if (TREE_READONLY (decl))
6340 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6341 : "uninitialized %<const %D%> is invalid in C++", decl);
6342 2327 : else if (RECORD_OR_UNION_TYPE_P (type)
6343 2327 : && C_TYPE_FIELDS_READONLY (type))
6344 5 : diagnose_uninitialized_cst_member (decl, type);
6345 : }
6346 :
6347 156295589 : if (flag_openmp
6348 459617 : && VAR_P (decl)
6349 156319608 : && lookup_attribute ("omp declare target implicit",
6350 24019 : DECL_ATTRIBUTES (decl)))
6351 : {
6352 13 : DECL_ATTRIBUTES (decl)
6353 13 : = remove_attribute ("omp declare target implicit",
6354 13 : DECL_ATTRIBUTES (decl));
6355 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6356 9 : error ("%q+D in declare target directive does not have mappable type",
6357 : decl);
6358 4 : else if (!lookup_attribute ("omp declare target",
6359 4 : DECL_ATTRIBUTES (decl))
6360 8 : && !lookup_attribute ("omp declare target link",
6361 4 : DECL_ATTRIBUTES (decl)))
6362 : {
6363 4 : DECL_ATTRIBUTES (decl)
6364 4 : = tree_cons (get_identifier ("omp declare target"),
6365 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6366 4 : symtab_node *node = symtab_node::get (decl);
6367 4 : if (node != NULL)
6368 : {
6369 4 : node->offloadable = 1;
6370 4 : if (ENABLE_OFFLOADING)
6371 : {
6372 : g->have_offload = true;
6373 : if (is_a <varpool_node *> (node))
6374 : vec_safe_push (offload_vars, decl);
6375 : }
6376 : }
6377 : }
6378 : }
6379 :
6380 : /* This is the last point we can lower alignment so give the target the
6381 : chance to do so. */
6382 156295589 : if (VAR_P (decl)
6383 8921140 : && !is_global_var (decl)
6384 163980137 : && !DECL_HARD_REGISTER (decl))
6385 7683518 : targetm.lower_local_decl_alignment (decl);
6386 :
6387 156295589 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6388 156295589 : }
6389 :
6390 : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6391 : EXPR is NULL or a pointer to an expression that needs to be
6392 : evaluated for the side effects of array size expressions in the
6393 : parameters. */
6394 :
6395 : tree
6396 0 : grokparm (const struct c_parm *parm, tree *expr)
6397 : {
6398 0 : tree attrs = parm->attrs;
6399 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6400 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6401 :
6402 0 : decl_attributes (&decl, attrs, 0);
6403 :
6404 0 : return decl;
6405 : }
6406 :
6407 :
6408 :
6409 : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6410 : and push that on the current scope. EXPR is a pointer to an
6411 : expression that needs to be evaluated for the side effects of array
6412 : size expressions in the parameters. */
6413 :
6414 : void
6415 124026414 : push_parm_decl (const struct c_parm *parm, tree *expr)
6416 : {
6417 124026414 : tree attrs = parm->attrs;
6418 124026414 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6419 124026414 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6420 124026414 : if (decl && DECL_P (decl))
6421 124026414 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6422 :
6423 124026414 : decl_attributes (&decl, attrs, 0);
6424 :
6425 124026414 : decl = pushdecl (decl);
6426 :
6427 124026414 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6428 124026414 : }
6429 :
6430 : /* Mark all the parameter declarations to date as forward decls.
6431 : Also diagnose use of this extension. */
6432 :
6433 : void
6434 42 : mark_forward_parm_decls (void)
6435 : {
6436 42 : struct c_binding *b;
6437 :
6438 42 : if (current_scope->had_forward_parm_decls)
6439 12 : warning_at (input_location, OPT_Wmultiple_parameter_fwd_decl_lists,
6440 : "more than one list of forward declarations of parameters");
6441 42 : if (pedantic && !current_scope->had_forward_parm_decls)
6442 4 : pedwarn (input_location, OPT_Wpedantic,
6443 : "ISO C forbids forward parameter declarations");
6444 :
6445 42 : current_scope->had_forward_parm_decls = true;
6446 :
6447 95 : for (b = current_scope->bindings; b; b = b->prev)
6448 53 : if (TREE_CODE (b->decl) == PARM_DECL)
6449 53 : TREE_ASM_WRITTEN (b->decl) = 1;
6450 42 : }
6451 :
6452 : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6453 : literal, which may be an incomplete array type completed by the
6454 : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6455 : literal. NON_CONST is true if the initializers contain something
6456 : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6457 : it is the (valid) alignment for this compound literal, as specified
6458 : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6459 : compound literal. */
6460 :
6461 : tree
6462 919196 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6463 : unsigned int alignas_align,
6464 : struct c_declspecs *scspecs)
6465 : {
6466 : /* We do not use start_decl here because we have a type, not a declarator;
6467 : and do not use finish_decl because the decl should be stored inside
6468 : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6469 919196 : tree decl;
6470 919196 : tree complit;
6471 919196 : tree stmt;
6472 919196 : bool threadp = scspecs ? scspecs->thread_p : false;
6473 307 : enum c_storage_class storage_class = (scspecs
6474 : ? scspecs->storage_class
6475 : : csc_none);
6476 :
6477 919196 : if (type == error_mark_node
6478 919189 : || init == error_mark_node)
6479 : return error_mark_node;
6480 :
6481 919136 : if (current_scope == file_scope && storage_class == csc_register)
6482 : {
6483 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6484 1 : storage_class = csc_none;
6485 : }
6486 :
6487 919136 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6488 : {
6489 6 : error_at (loc, "compound literal implicitly auto and declared %qs",
6490 3 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6491 3 : threadp = false;
6492 : }
6493 :
6494 919136 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6495 919136 : DECL_EXTERNAL (decl) = 0;
6496 919136 : TREE_PUBLIC (decl) = 0;
6497 1838272 : TREE_STATIC (decl) = (current_scope == file_scope
6498 919136 : || storage_class == csc_static);
6499 919136 : DECL_CONTEXT (decl) = current_function_decl;
6500 919136 : TREE_USED (decl) = 1;
6501 919136 : DECL_READ_P (decl) = 1;
6502 919136 : DECL_ARTIFICIAL (decl) = 1;
6503 919136 : DECL_IGNORED_P (decl) = 1;
6504 919136 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6505 1838041 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6506 919136 : TREE_TYPE (decl) = type;
6507 919136 : if (threadp)
6508 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6509 919136 : if (storage_class == csc_register)
6510 : {
6511 25 : C_DECL_REGISTER (decl) = 1;
6512 25 : DECL_REGISTER (decl) = 1;
6513 : }
6514 919136 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6515 919136 : if (alignas_align)
6516 : {
6517 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6518 3 : DECL_USER_ALIGN (decl) = 1;
6519 : }
6520 919136 : store_init_value (loc, decl, init, NULL_TREE);
6521 919136 : if (current_scope != file_scope
6522 918885 : && TREE_STATIC (decl)
6523 28 : && !TREE_READONLY (decl)
6524 17 : && DECL_DECLARED_INLINE_P (current_function_decl)
6525 919142 : && DECL_EXTERNAL (current_function_decl))
6526 4 : record_inline_static (input_location, current_function_decl,
6527 : decl, csi_modifiable);
6528 :
6529 919136 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6530 : {
6531 267 : int failure = complete_array_type (&TREE_TYPE (decl),
6532 267 : DECL_INITIAL (decl), true);
6533 : /* If complete_array_type returns 3, it means that the initial value of
6534 : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6535 : modes, the empty initializer itself has been diagnosed if pedantic so
6536 : does not need to be diagnosed again here. */
6537 267 : gcc_assert (failure == 0 || failure == 3);
6538 267 : if (failure == 3 && flag_isoc23)
6539 1 : pedwarn (loc, OPT_Wpedantic,
6540 : "array of unknown size with empty initializer");
6541 :
6542 267 : type = TREE_TYPE (decl);
6543 267 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6544 267 : relayout_decl (decl);
6545 : }
6546 :
6547 919136 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6548 : {
6549 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6550 19 : return error_mark_node;
6551 : }
6552 :
6553 918838 : if ((TREE_STATIC (decl) || C_DECL_DECLARED_CONSTEXPR (decl))
6554 919338 : && C_TYPE_VARIABLE_SIZE (type))
6555 2 : error_at (loc, "storage size isn%'t constant");
6556 :
6557 919117 : if (TREE_STATIC (decl)
6558 919117 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6559 0 : return error_mark_node;
6560 :
6561 919117 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6562 919117 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6563 919117 : TREE_SIDE_EFFECTS (complit) = 1;
6564 :
6565 919117 : layout_decl (decl, 0);
6566 :
6567 919117 : if (TREE_STATIC (decl))
6568 : {
6569 : /* This decl needs a name for the assembler output. */
6570 279 : set_compound_literal_name (decl);
6571 279 : DECL_DEFER_OUTPUT (decl) = 1;
6572 279 : DECL_COMDAT (decl) = 1;
6573 279 : pushdecl (decl);
6574 279 : rest_of_decl_compilation (decl, 1, 0);
6575 : }
6576 918838 : else if (current_function_decl && !current_scope->parm_flag)
6577 918829 : pushdecl (decl);
6578 :
6579 919117 : if (non_const)
6580 : {
6581 221 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6582 221 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6583 : }
6584 :
6585 : return complit;
6586 : }
6587 :
6588 : /* Check the type of a compound literal. Here we just check that it
6589 : is valid for C++. */
6590 :
6591 : void
6592 919193 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6593 : {
6594 919193 : if (warn_cxx_compat
6595 165 : && (type_name->specs->typespec_kind == ctsk_tagdef
6596 164 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6597 163 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6598 2 : warning_at (loc, OPT_Wc___compat,
6599 : "defining a type in a compound literal is invalid in C++");
6600 919193 : }
6601 :
6602 : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6603 : replacing with appropriate values if they are invalid. */
6604 :
6605 : static void
6606 52618 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6607 : tree orig_name)
6608 : {
6609 52618 : tree type_mv;
6610 52618 : unsigned int max_width;
6611 52618 : unsigned HOST_WIDE_INT w;
6612 52618 : const char *name = (orig_name
6613 95474 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6614 9762 : : _("<anonymous>"));
6615 :
6616 : /* Detect and ignore out of range field width and process valid
6617 : field widths. */
6618 52618 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6619 : {
6620 8 : error_at (loc, "bit-field %qs width not an integer constant", name);
6621 8 : *width = integer_one_node;
6622 : }
6623 : else
6624 : {
6625 52610 : if (TREE_CODE (*width) != INTEGER_CST)
6626 : {
6627 12 : *width = c_fully_fold (*width, false, NULL);
6628 12 : if (TREE_CODE (*width) == INTEGER_CST)
6629 6 : pedwarn (loc, OPT_Wpedantic,
6630 : "bit-field %qs width not an integer constant expression",
6631 : name);
6632 : }
6633 52610 : if (TREE_CODE (*width) != INTEGER_CST)
6634 : {
6635 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6636 6 : *width = integer_one_node;
6637 : }
6638 52610 : constant_expression_warning (*width);
6639 52610 : if (tree_int_cst_sgn (*width) < 0)
6640 : {
6641 2 : error_at (loc, "negative width in bit-field %qs", name);
6642 2 : *width = integer_one_node;
6643 : }
6644 52608 : else if (integer_zerop (*width) && orig_name)
6645 : {
6646 4 : error_at (loc, "zero width for bit-field %qs", name);
6647 4 : *width = integer_one_node;
6648 : }
6649 : }
6650 :
6651 : /* Detect invalid bit-field type. */
6652 52618 : if (TREE_CODE (*type) != INTEGER_TYPE
6653 : && TREE_CODE (*type) != BOOLEAN_TYPE
6654 : && TREE_CODE (*type) != ENUMERAL_TYPE
6655 52618 : && TREE_CODE (*type) != BITINT_TYPE)
6656 : {
6657 8 : error_at (loc, "bit-field %qs has invalid type", name);
6658 8 : *type = unsigned_type_node;
6659 : }
6660 :
6661 52618 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6662 : {
6663 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6664 : name);
6665 1 : *type = unsigned_type_node;
6666 : }
6667 :
6668 52618 : type_mv = TYPE_MAIN_VARIANT (*type);
6669 52618 : if (!in_system_header_at (input_location)
6670 29374 : && type_mv != integer_type_node
6671 26635 : && type_mv != unsigned_type_node
6672 65223 : && type_mv != boolean_type_node)
6673 12023 : pedwarn_c90 (loc, OPT_Wpedantic,
6674 : "type of bit-field %qs is a GCC extension", name);
6675 :
6676 52618 : max_width = TYPE_PRECISION (*type);
6677 :
6678 52618 : if (compare_tree_int (*width, max_width) > 0)
6679 : {
6680 4 : error_at (loc, "width of %qs exceeds its type", name);
6681 4 : w = max_width;
6682 4 : *width = build_int_cst (integer_type_node, w);
6683 : }
6684 : else
6685 52614 : w = tree_to_uhwi (*width);
6686 :
6687 : /* Truncation of hardbool false and true representation values is always safe:
6688 : either the values remain different, or we'll report a problem when creating
6689 : the narrower type. */
6690 52618 : if (c_hardbool_type_attr (*type))
6691 52618 : return;
6692 :
6693 52337 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6694 : {
6695 4130 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6696 4130 : if (!lt
6697 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6698 8246 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6699 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6700 : }
6701 : }
6702 :
6703 :
6704 :
6705 : /* Print warning about variable length array if necessary. */
6706 :
6707 : static void
6708 22486 : warn_variable_length_array (tree name, tree size)
6709 : {
6710 22486 : if (TREE_CONSTANT (size))
6711 : {
6712 181 : if (name)
6713 168 : pedwarn_c90 (input_location, OPT_Wvla,
6714 : "ISO C90 forbids array %qE whose size "
6715 : "cannot be evaluated", name);
6716 : else
6717 13 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6718 : "whose size cannot be evaluated");
6719 : }
6720 : else
6721 : {
6722 22305 : if (name)
6723 13822 : pedwarn_c90 (input_location, OPT_Wvla,
6724 : "ISO C90 forbids variable length array %qE", name);
6725 : else
6726 8483 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6727 : "length array");
6728 : }
6729 22486 : }
6730 :
6731 : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6732 : considering only those c_declspec_words found in LIST, which
6733 : must be terminated by cdw_number_of_elements. */
6734 :
6735 : static location_t
6736 235 : smallest_type_quals_location (const location_t *locations,
6737 : const c_declspec_word *list)
6738 : {
6739 235 : location_t loc = UNKNOWN_LOCATION;
6740 1410 : while (*list != cdw_number_of_elements)
6741 : {
6742 1175 : location_t newloc = locations[*list];
6743 1175 : if (loc == UNKNOWN_LOCATION
6744 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6745 887 : loc = newloc;
6746 1175 : list++;
6747 : }
6748 :
6749 235 : return loc;
6750 : }
6751 :
6752 :
6753 : /* We attach an artificial TYPE_DECL to pointed-to type
6754 : and arrange for it to be included in a DECL_EXPR. This
6755 : forces the sizes evaluation at a safe point and ensures it
6756 : is not deferred until e.g. within a deeper conditional context.
6757 :
6758 : PARM contexts have no enclosing statement list that
6759 : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6760 : instead, and add it to the list of expressions that
6761 : need to be evaluated.
6762 :
6763 : TYPENAME contexts do have an enclosing statement list,
6764 : but it would be incorrect to use it, as the size should
6765 : only be evaluated if the containing expression is
6766 : evaluated. We might also be in the middle of an
6767 : expression with side effects on the pointed-to type size
6768 : "arguments" prior to the pointer declaration point and
6769 : the fake TYPE_DECL in the enclosing context would force
6770 : the size evaluation prior to the side effects. We therefore
6771 : use BIND_EXPRs in TYPENAME contexts too. */
6772 : static void
6773 7029 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6774 : {
6775 7029 : tree bind = NULL_TREE;
6776 7029 : if (expr)
6777 : {
6778 1589 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6779 : NULL_TREE);
6780 1589 : TREE_SIDE_EFFECTS (bind) = 1;
6781 1589 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6782 1589 : push_scope ();
6783 : }
6784 :
6785 7029 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6786 7029 : pushdecl (decl);
6787 7029 : DECL_ARTIFICIAL (decl) = 1;
6788 7029 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6789 7029 : if (set_name_p)
6790 6319 : TYPE_NAME (type) = decl;
6791 :
6792 7029 : if (bind)
6793 : {
6794 1589 : pop_scope ();
6795 1589 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6796 1589 : if (*expr)
6797 1468 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6798 : else
6799 121 : *expr = bind;
6800 : }
6801 7029 : }
6802 :
6803 :
6804 : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6805 : declared with type TYPE. The attribute has two arguments. The first is
6806 : a string that encodes the presence of the static keyword. The second is
6807 : the declared type of the array before adjustment, i.e. as an array type
6808 : including the outermost bound. */
6809 :
6810 : static tree
6811 436584 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6812 : {
6813 436584 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6814 436584 : tree acsstr = static_p ? build_string (7, "static") :
6815 436462 : build_string (1, "");
6816 436584 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6817 436584 : tree name = get_identifier ("arg spec");
6818 436584 : return tree_cons (name, args, attrs);
6819 : }
6820 :
6821 :
6822 : /* Given declspecs and a declarator,
6823 : determine the name and type of the object declared
6824 : and construct a ..._DECL node for it.
6825 : (In one case we can return a ..._TYPE node instead.
6826 : For invalid input we sometimes return NULL_TREE.)
6827 :
6828 : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6829 :
6830 : DECL_CONTEXT says which syntactic context this declaration is in:
6831 : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6832 : FUNCDEF for a function definition. Like NORMAL but a few different
6833 : error messages in each case. Return value may be zero meaning
6834 : this definition is too screwy to try to parse.
6835 : PARM for a parameter declaration (either within a function prototype
6836 : or before a function body). Make a PARM_DECL, or return void_type_node.
6837 : TYPENAME if for a typename (in a cast or sizeof).
6838 : Don't make a DECL node; just return the ..._TYPE node.
6839 : GENERIC_ASSOC for typenames in a generic association.
6840 : FIELD for a struct or union field; make a FIELD_DECL.
6841 : INITIALIZED is true if the decl has an initializer.
6842 : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6843 : representing the width of the bit-field.
6844 : DECL_ATTRS points to the list of attributes that should be added to this
6845 : decl. Any nested attributes that belong on the decl itself will be
6846 : added to this list.
6847 : If EXPR is not NULL, any expressions that need to be evaluated as
6848 : part of evaluating variably modified types will be stored in *EXPR.
6849 : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6850 : set to indicate whether operands in *EXPR can be used in constant
6851 : expressions.
6852 : DEPRECATED_STATE is a deprecated_states value indicating whether
6853 : deprecation/unavailability warnings should be suppressed.
6854 :
6855 : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6856 : It may also be so in the PARM case, for a prototype where the
6857 : argument type is specified but not the name.
6858 :
6859 : This function is where the complicated C meanings of `static'
6860 : and `extern' are interpreted. */
6861 :
6862 : static tree
6863 314093668 : grokdeclarator (const struct c_declarator *declarator,
6864 : struct c_declspecs *declspecs,
6865 : enum decl_context decl_context, bool initialized, tree *width,
6866 : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6867 : enum deprecated_states deprecated_state)
6868 : {
6869 314093668 : tree type = declspecs->type;
6870 314093668 : bool threadp = declspecs->thread_p;
6871 314093668 : bool constexprp = declspecs->constexpr_p;
6872 314093668 : enum c_storage_class storage_class = declspecs->storage_class;
6873 314093668 : int constp;
6874 314093668 : int restrictp;
6875 314093668 : int volatilep;
6876 314093668 : int atomicp;
6877 314093668 : int type_quals = TYPE_UNQUALIFIED;
6878 314093668 : tree name = NULL_TREE;
6879 314093668 : bool funcdef_flag = false;
6880 314093668 : bool funcdef_syntax = false;
6881 314093668 : bool size_varies = false;
6882 314093668 : bool size_error = false;
6883 314093668 : tree decl_attr = declspecs->decl_attr;
6884 314093668 : int array_ptr_quals = TYPE_UNQUALIFIED;
6885 314093668 : tree array_ptr_attrs = NULL_TREE;
6886 314093668 : bool array_parm_static = false;
6887 314093668 : bool array_parm_vla_unspec_p = false;
6888 314093668 : tree returned_attrs = NULL_TREE;
6889 314093668 : tree decl_id_attrs = NULL_TREE;
6890 314093668 : bool bitfield = width != NULL;
6891 314093668 : tree element_type;
6892 314093668 : tree orig_qual_type = NULL;
6893 314093668 : size_t orig_qual_indirect = 0;
6894 314093668 : struct c_arg_info *arg_info = 0;
6895 314093668 : addr_space_t as1, as2, address_space;
6896 314093668 : location_t loc = UNKNOWN_LOCATION;
6897 314093668 : tree expr_dummy;
6898 314093668 : bool expr_const_operands_dummy;
6899 314093668 : enum c_declarator_kind first_non_attr_kind;
6900 314093668 : unsigned int alignas_align = 0;
6901 :
6902 314093668 : if (type == NULL_TREE)
6903 : {
6904 : /* This can occur for auto on a parameter in C23 mode. Set a
6905 : dummy type here so subsequent code can give diagnostics for
6906 : this case. */
6907 2 : gcc_assert (declspecs->c23_auto_p);
6908 2 : gcc_assert (decl_context == PARM);
6909 2 : type = declspecs->type = integer_type_node;
6910 : }
6911 314093668 : if (TREE_CODE (type) == ERROR_MARK)
6912 29 : return error_mark_node;
6913 314093639 : if (expr == NULL)
6914 : {
6915 38200 : expr = &expr_dummy;
6916 38200 : expr_dummy = NULL_TREE;
6917 : }
6918 314093639 : if (expr_const_operands == NULL)
6919 192653267 : expr_const_operands = &expr_const_operands_dummy;
6920 :
6921 314093639 : if (declspecs->expr)
6922 : {
6923 948 : if (*expr)
6924 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6925 : declspecs->expr);
6926 : else
6927 941 : *expr = declspecs->expr;
6928 : }
6929 314093639 : *expr_const_operands = declspecs->expr_const_operands;
6930 :
6931 314093639 : if (decl_context == FUNCDEF)
6932 36301471 : funcdef_flag = true, decl_context = NORMAL;
6933 :
6934 : /* Look inside a declarator for the name being declared
6935 : and get it as an IDENTIFIER_NODE, for an error message. */
6936 314093639 : {
6937 314093639 : const struct c_declarator *decl = declarator;
6938 :
6939 314093639 : first_non_attr_kind = cdk_attrs;
6940 384049542 : while (decl)
6941 384049542 : switch (decl->kind)
6942 : {
6943 1153555 : case cdk_array:
6944 1153555 : loc = decl->id_loc;
6945 : /* FALL THRU. */
6946 :
6947 69949124 : case cdk_function:
6948 69949124 : case cdk_pointer:
6949 69949124 : funcdef_syntax = (decl->kind == cdk_function);
6950 69949124 : if (first_non_attr_kind == cdk_attrs)
6951 67840104 : first_non_attr_kind = decl->kind;
6952 69949124 : decl = decl->declarator;
6953 69949124 : break;
6954 :
6955 6779 : case cdk_attrs:
6956 6779 : decl = decl->declarator;
6957 6779 : break;
6958 :
6959 314093639 : case cdk_id:
6960 314093639 : loc = decl->id_loc;
6961 314093639 : if (decl->u.id.id)
6962 : name = decl->u.id.id;
6963 314093639 : decl_id_attrs = decl->u.id.attrs;
6964 314093639 : if (first_non_attr_kind == cdk_attrs)
6965 246253535 : first_non_attr_kind = decl->kind;
6966 : decl = 0;
6967 : break;
6968 :
6969 0 : default:
6970 0 : gcc_unreachable ();
6971 : }
6972 314093639 : if (name == NULL_TREE)
6973 : {
6974 126108110 : gcc_assert (decl_context == PARM
6975 : || decl_context == TYPENAME
6976 : || decl_context == GENERIC_ASSOC
6977 : || (decl_context == FIELD
6978 : && declarator->kind == cdk_id));
6979 126108110 : gcc_assert (!initialized);
6980 : }
6981 : }
6982 :
6983 : /* An enum type specifier (": specifier-qualifier-list") may only be
6984 : specified when the enum is being defined or in an empty
6985 : declaration of the form "enum identifier enum-type-specifier;".
6986 : Except for the case of an empty declaration that has additional
6987 : declaration specifiers, all invalid contexts (declarations that
6988 : aren't empty, type names, parameter declarations, member
6989 : declarations) pass through grokdeclarator. */
6990 314093639 : if (declspecs->enum_type_specifier_ref_p)
6991 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6992 :
6993 : /* A function definition's declarator must have the form of
6994 : a function declarator. */
6995 :
6996 314093639 : if (funcdef_flag && !funcdef_syntax)
6997 : return NULL_TREE;
6998 :
6999 : /* If this looks like a function definition, make it one,
7000 : even if it occurs where parms are expected.
7001 : Then store_parm_decls will reject it and not use it as a parm. */
7002 314093608 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
7003 22663 : decl_context = PARM;
7004 :
7005 314093608 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
7006 : {
7007 314093584 : if (declspecs->unavailable_p)
7008 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
7009 314093556 : else if (declspecs->deprecated_p
7010 46 : && deprecated_state != DEPRECATED_SUPPRESS)
7011 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
7012 : }
7013 :
7014 314093608 : if ((decl_context == NORMAL || decl_context == FIELD)
7015 68544786 : && current_scope == file_scope
7016 374572744 : && c_type_variably_modified_p (type))
7017 : {
7018 3 : if (name)
7019 3 : error_at (loc, "variably modified %qE at file scope", name);
7020 : else
7021 0 : error_at (loc, "variably modified field at file scope");
7022 3 : type = integer_type_node;
7023 : }
7024 :
7025 314093608 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
7026 :
7027 : /* Diagnose defaulting to "int". */
7028 :
7029 314093608 : if (declspecs->default_int_p)
7030 : {
7031 : /* Issue a warning if this is an ISO C 99 program or if
7032 : -Wreturn-type and this is a function, or if -Wimplicit;
7033 : prefer the former warning since it is more explicit. */
7034 9764 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
7035 1229 : && funcdef_flag)
7036 702 : warn_about_return_type = 1;
7037 : else
7038 : {
7039 9062 : if (name)
7040 9052 : permerror_opt (loc, OPT_Wimplicit_int,
7041 : "type defaults to %<int%> in declaration "
7042 : "of %qE", name);
7043 : else
7044 10 : permerror_opt (loc, OPT_Wimplicit_int,
7045 : "type defaults to %<int%> in type name");
7046 : }
7047 : }
7048 :
7049 : /* Adjust the type if a bit-field is being declared,
7050 : -funsigned-bitfields applied and the type is not explicitly
7051 : "signed". */
7052 314093608 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
7053 40 : && TREE_CODE (type) == INTEGER_TYPE)
7054 38 : type = c_common_unsigned_type (type);
7055 :
7056 : /* Figure out the type qualifiers for the declaration. There are
7057 : two ways a declaration can become qualified. One is something
7058 : like `const int i' where the `const' is explicit. Another is
7059 : something like `typedef const int CI; CI i' where the type of the
7060 : declaration contains the `const'. A third possibility is that
7061 : there is a type qualifier on the element type of a typedefed
7062 : array type, in which case we should extract that qualifier so
7063 : that c_apply_type_quals_to_decl receives the full list of
7064 : qualifiers to work with (C90 is not entirely clear about whether
7065 : duplicate qualifiers should be diagnosed in this case, but it
7066 : seems most appropriate to do so). */
7067 314093608 : element_type = strip_array_types (type);
7068 314093608 : constp = declspecs->const_p + TYPE_READONLY (element_type);
7069 314093608 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7070 314093608 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7071 314093608 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7072 314093608 : as1 = declspecs->address_space;
7073 314093608 : as2 = TYPE_ADDR_SPACE (element_type);
7074 314093608 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7075 :
7076 314093608 : if (constp > 1)
7077 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7078 314093608 : if (restrictp > 1)
7079 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7080 314093608 : if (volatilep > 1)
7081 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7082 314093608 : if (atomicp > 1)
7083 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7084 :
7085 314093608 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7086 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
7087 : c_addr_space_name (as1), c_addr_space_name (as2));
7088 :
7089 314093608 : if ((TREE_CODE (type) == ARRAY_TYPE
7090 313918051 : || first_non_attr_kind == cdk_array)
7091 315179253 : && TYPE_QUALS (element_type))
7092 : {
7093 73 : orig_qual_type = type;
7094 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7095 : }
7096 314093608 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7097 314093608 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7098 314093608 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7099 314093608 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7100 314093608 : | ENCODE_QUAL_ADDR_SPACE (address_space));
7101 314093608 : if (type_quals != TYPE_QUALS (element_type))
7102 12812906 : orig_qual_type = NULL_TREE;
7103 :
7104 : /* Applying the _Atomic qualifier to an array type (through the use
7105 : of typedefs or typeof) must be detected here. If the qualifier
7106 : is introduced later, any appearance of applying it to an array is
7107 : actually applying it to an element of that array. */
7108 314093608 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7109 6 : error_at (loc, "%<_Atomic%>-qualified array type");
7110 :
7111 : /* Warn about storage classes that are invalid for certain
7112 : kinds of declarations (parameters, typenames, etc.). */
7113 :
7114 314093608 : if (funcdef_flag
7115 36301440 : && (threadp
7116 : || constexprp
7117 36301438 : || storage_class == csc_auto
7118 36301438 : || storage_class == csc_register
7119 36301393 : || storage_class == csc_typedef))
7120 : {
7121 47 : if (storage_class == csc_auto)
7122 42 : pedwarn (loc,
7123 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7124 : "function definition declared %<auto%>");
7125 50 : if (storage_class == csc_register)
7126 3 : error_at (loc, "function definition declared %<register%>");
7127 50 : if (storage_class == csc_typedef)
7128 3 : error_at (loc, "function definition declared %<typedef%>");
7129 50 : if (threadp)
7130 2 : error_at (loc, "function definition declared %qs",
7131 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7132 50 : threadp = false;
7133 : /* The parser ensures a constexpr function definition never
7134 : reaches here. */
7135 50 : gcc_assert (!constexprp);
7136 50 : if (storage_class == csc_auto
7137 50 : || storage_class == csc_register
7138 : || storage_class == csc_typedef)
7139 55 : storage_class = csc_none;
7140 : }
7141 314093558 : else if (decl_context != NORMAL && (storage_class != csc_none
7142 249874532 : || threadp
7143 249873949 : || constexprp
7144 249873947 : || declspecs->c23_auto_p))
7145 : {
7146 587 : if (decl_context == PARM
7147 587 : && storage_class == csc_register
7148 568 : && !constexprp
7149 566 : && !declspecs->c23_auto_p)
7150 : ;
7151 : else
7152 : {
7153 21 : switch (decl_context)
7154 : {
7155 0 : case FIELD:
7156 0 : if (name)
7157 0 : error_at (loc, "storage class specified for structure "
7158 : "field %qE", name);
7159 : else
7160 0 : error_at (loc, "storage class specified for structure field");
7161 : break;
7162 21 : case PARM:
7163 21 : if (name)
7164 7 : error_at (loc, "storage class specified for parameter %qE",
7165 : name);
7166 : else
7167 14 : error_at (loc, "storage class specified for unnamed parameter");
7168 : break;
7169 0 : default:
7170 0 : error_at (loc, "storage class specified for typename");
7171 0 : break;
7172 : }
7173 314093608 : storage_class = csc_none;
7174 314093608 : threadp = false;
7175 314093608 : constexprp = false;
7176 : }
7177 : }
7178 314092971 : else if (storage_class == csc_extern
7179 314092971 : && initialized
7180 35449104 : && !funcdef_flag)
7181 : {
7182 : /* 'extern' with initialization is invalid if not at file scope. */
7183 28 : if (current_scope == file_scope)
7184 : {
7185 : /* It is fine to have 'extern const' when compiling at C
7186 : and C++ intersection. */
7187 19 : if (!(warn_cxx_compat && constp))
7188 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7189 : name);
7190 : }
7191 : else
7192 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7193 : }
7194 314092943 : else if (current_scope == file_scope)
7195 : {
7196 61352984 : if (storage_class == csc_auto)
7197 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7198 : name);
7199 61352984 : if (pedantic && storage_class == csc_register)
7200 4 : pedwarn (input_location, OPT_Wpedantic,
7201 : "file-scope declaration of %qE specifies %<register%>", name);
7202 : }
7203 : else
7204 : {
7205 252739959 : if (storage_class == csc_extern && funcdef_flag)
7206 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7207 252739956 : else if (threadp && storage_class == csc_none)
7208 : {
7209 14 : error_at (loc, "function-scope %qE implicitly auto and declared "
7210 : "%qs", name,
7211 7 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7212 7 : threadp = false;
7213 : }
7214 : }
7215 :
7216 : /* Now figure out the structure of the declarator proper.
7217 : Descend through it, creating more complex types, until we reach
7218 : the declared identifier (or NULL_TREE, in an absolute declarator).
7219 : At each stage we maintain an unqualified version of the type
7220 : together with any qualifiers that should be applied to it with
7221 : c_build_qualified_type; this way, array types including
7222 : multidimensional array types are first built up in unqualified
7223 : form and then the qualified form is created with
7224 : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7225 :
7226 384049510 : while (declarator && declarator->kind != cdk_id)
7227 : {
7228 69955902 : if (type == error_mark_node)
7229 : {
7230 39 : declarator = declarator->declarator;
7231 39 : continue;
7232 : }
7233 :
7234 : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7235 : a cdk_pointer (for *...),
7236 : a cdk_function (for ...(...)),
7237 : a cdk_attrs (for nested attributes),
7238 : or a cdk_id (for the name being declared
7239 : or the place in an absolute declarator
7240 : where the name was omitted).
7241 : For the last case, we have just exited the loop.
7242 :
7243 : At this point, TYPE is the type of elements of an array,
7244 : or for a function to return, or for a pointer to point to.
7245 : After this sequence of ifs, TYPE is the type of the
7246 : array or function or pointer, and DECLARATOR has had its
7247 : outermost layer removed. */
7248 :
7249 69955863 : if (array_ptr_quals != TYPE_UNQUALIFIED
7250 69955863 : || array_ptr_attrs != NULL_TREE
7251 69955863 : || array_parm_static)
7252 : {
7253 : /* Only the innermost declarator (making a parameter be of
7254 : array type which is converted to pointer type)
7255 : may have static or type qualifiers. */
7256 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7257 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7258 1 : array_ptr_attrs = NULL_TREE;
7259 1 : array_parm_static = false;
7260 : }
7261 :
7262 69955863 : switch (declarator->kind)
7263 : {
7264 6779 : case cdk_attrs:
7265 6779 : {
7266 : /* A declarator with embedded attributes. */
7267 6779 : tree attrs = declarator->u.attrs;
7268 6779 : const struct c_declarator *inner_decl;
7269 6779 : int attr_flags = 0;
7270 6779 : declarator = declarator->declarator;
7271 : /* Standard attribute syntax precisely defines what entity
7272 : an attribute in each position appertains to, so only
7273 : apply laxity about positioning to GNU attribute syntax.
7274 : Standard attributes applied to a function or array
7275 : declarator apply exactly to that type; standard
7276 : attributes applied to the identifier apply to the
7277 : declaration rather than to the type, and are specified
7278 : using a cdk_id declarator rather than using
7279 : cdk_attrs. */
7280 6779 : inner_decl = declarator;
7281 6779 : while (inner_decl->kind == cdk_attrs)
7282 0 : inner_decl = inner_decl->declarator;
7283 6779 : if (!cxx11_attribute_p (attrs))
7284 : {
7285 6681 : if (inner_decl->kind == cdk_id)
7286 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7287 : else if (inner_decl->kind == cdk_function)
7288 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7289 : else if (inner_decl->kind == cdk_array)
7290 6779 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7291 : }
7292 6779 : attrs = c_warn_type_attributes (type, attrs);
7293 6779 : returned_attrs = decl_attributes (&type,
7294 : chainon (returned_attrs, attrs),
7295 : attr_flags);
7296 6779 : break;
7297 : }
7298 1153545 : case cdk_array:
7299 1153545 : {
7300 1153545 : tree itype = NULL_TREE;
7301 1153545 : tree size = declarator->u.array.dimen;
7302 : /* The index is a signed object `sizetype' bits wide. */
7303 1153545 : tree index_type = c_common_signed_type (sizetype);
7304 :
7305 1153545 : array_ptr_quals = declarator->u.array.quals;
7306 1153545 : array_ptr_attrs = declarator->u.array.attrs;
7307 1153545 : array_parm_static = declarator->u.array.static_p;
7308 1153545 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7309 :
7310 1153545 : declarator = declarator->declarator;
7311 :
7312 : /* Check for some types that there cannot be arrays of. */
7313 :
7314 1153545 : if (VOID_TYPE_P (type))
7315 : {
7316 11 : if (name)
7317 9 : error_at (loc, "declaration of %qE as array of voids", name);
7318 : else
7319 2 : error_at (loc, "declaration of type name as array of voids");
7320 11 : type = error_mark_node;
7321 : }
7322 :
7323 1153545 : if (TREE_CODE (type) == FUNCTION_TYPE)
7324 : {
7325 3 : if (name)
7326 2 : error_at (loc, "declaration of %qE as array of functions",
7327 : name);
7328 : else
7329 1 : error_at (loc, "declaration of type name as array of "
7330 : "functions");
7331 3 : type = error_mark_node;
7332 : }
7333 :
7334 22018 : if (pedantic && !in_system_header_at (input_location)
7335 1168392 : && flexible_array_type_p (type))
7336 20 : pedwarn (loc, OPT_Wpedantic,
7337 : "invalid use of structure with flexible array member");
7338 :
7339 1153545 : if (size == error_mark_node)
7340 119 : type = error_mark_node;
7341 :
7342 1153545 : if (type == error_mark_node)
7343 133 : continue;
7344 :
7345 1153412 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7346 : {
7347 0 : type = error_mark_node;
7348 0 : continue;
7349 : }
7350 :
7351 : /* If size was specified, set ITYPE to a range-type for
7352 : that size. Otherwise, ITYPE remains null. finish_decl
7353 : may figure it out from an initial value. */
7354 :
7355 1153412 : if (size)
7356 : {
7357 982099 : bool size_maybe_const = true;
7358 982099 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7359 982099 : && !TREE_OVERFLOW (size));
7360 982099 : bool this_size_varies = false;
7361 :
7362 : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7363 : lvalue. */
7364 982108 : STRIP_TYPE_NOPS (size);
7365 :
7366 982099 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7367 : {
7368 16 : if (name)
7369 14 : error_at (loc, "size of array %qE has non-integer type",
7370 : name);
7371 : else
7372 2 : error_at (loc,
7373 : "size of unnamed array has non-integer type");
7374 16 : size = integer_one_node;
7375 16 : size_int_const = true;
7376 16 : size_error = true;
7377 : }
7378 : /* This can happen with enum forward declaration. */
7379 982083 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7380 : {
7381 0 : if (name)
7382 0 : error_at (loc, "size of array %qE has incomplete type",
7383 : name);
7384 : else
7385 0 : error_at (loc, "size of unnamed array has incomplete "
7386 : "type");
7387 0 : size = integer_one_node;
7388 0 : size_int_const = true;
7389 0 : size_error = true;
7390 : }
7391 :
7392 982099 : size = c_fully_fold (size, false, &size_maybe_const);
7393 :
7394 982099 : if (pedantic && size_maybe_const && integer_zerop (size))
7395 : {
7396 3 : if (name)
7397 3 : pedwarn (loc, OPT_Wpedantic,
7398 : "ISO C forbids zero-size array %qE", name);
7399 : else
7400 0 : pedwarn (loc, OPT_Wpedantic,
7401 : "ISO C forbids zero-size array");
7402 : }
7403 :
7404 982099 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7405 : {
7406 959747 : constant_expression_warning (size);
7407 959747 : if (tree_int_cst_sgn (size) < 0)
7408 : {
7409 404 : if (name)
7410 401 : error_at (loc, "size of array %qE is negative", name);
7411 : else
7412 3 : error_at (loc, "size of unnamed array is negative");
7413 404 : size = integer_one_node;
7414 404 : size_int_const = true;
7415 404 : size_error = true;
7416 : }
7417 : /* Handle a size folded to an integer constant but
7418 : not an integer constant expression. */
7419 959747 : if (!size_int_const)
7420 : {
7421 : /* If this is a file scope declaration of an
7422 : ordinary identifier, this is invalid code;
7423 : diagnosing it here and not subsequently
7424 : treating the type as variable-length avoids
7425 : more confusing diagnostics later. */
7426 155 : if ((decl_context == NORMAL || decl_context == FIELD)
7427 142 : && current_scope == file_scope)
7428 14 : pedwarn (input_location, 0,
7429 : "variably modified %qE at file scope",
7430 : name);
7431 : else
7432 : this_size_varies = size_varies = true;
7433 155 : warn_variable_length_array (name, size);
7434 : }
7435 : }
7436 22352 : else if ((decl_context == NORMAL || decl_context == FIELD)
7437 13252 : && current_scope == file_scope)
7438 : {
7439 21 : error_at (loc, "variably modified %qE at file scope", name);
7440 21 : size = integer_one_node;
7441 : }
7442 : else
7443 : {
7444 : /* Make sure the array size remains visibly
7445 : nonconstant even if it is (eg) a const variable
7446 : with known value. */
7447 22331 : this_size_varies = size_varies = true;
7448 22331 : warn_variable_length_array (name, size);
7449 22331 : if (sanitize_flags_p (SANITIZE_VLA)
7450 181 : && current_function_decl != NULL_TREE
7451 22494 : && decl_context == NORMAL)
7452 : {
7453 : /* Evaluate the array size only once. */
7454 155 : size = save_expr (size);
7455 155 : size = c_fully_fold (size, false, NULL);
7456 155 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7457 : ubsan_instrument_vla (loc, size),
7458 : size);
7459 : }
7460 : }
7461 :
7462 982099 : if (integer_zerop (size) && !this_size_varies)
7463 : {
7464 : /* A zero-length array cannot be represented with
7465 : an unsigned index type, which is what we'll
7466 : get with build_index_type. Create an
7467 : open-ended range instead. */
7468 2603 : itype = build_range_type (sizetype, size, NULL_TREE);
7469 : }
7470 : else
7471 : {
7472 : /* Arrange for the SAVE_EXPR on the inside of the
7473 : MINUS_EXPR, which allows the -1 to get folded
7474 : with the +1 that happens when building TYPE_SIZE. */
7475 979496 : if (size_varies)
7476 22845 : size = save_expr (size);
7477 979496 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7478 141 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7479 : integer_zero_node, size);
7480 :
7481 : /* Compute the maximum valid index, that is, size
7482 : - 1. Do the calculation in index_type, so that
7483 : if it is a variable the computations will be
7484 : done in the proper mode. */
7485 979496 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7486 : convert (index_type, size),
7487 : convert (index_type,
7488 : size_one_node));
7489 :
7490 : /* The above overflows when size does not fit
7491 : in index_type.
7492 : ??? While a size of INT_MAX+1 technically shouldn't
7493 : cause an overflow (because we subtract 1), handling
7494 : this case seems like an unnecessary complication. */
7495 979496 : if (TREE_CODE (size) == INTEGER_CST
7496 957024 : && !int_fits_type_p (size, index_type))
7497 : {
7498 6 : if (name)
7499 5 : error_at (loc, "size of array %qE is too large",
7500 : name);
7501 : else
7502 1 : error_at (loc, "size of unnamed array is too large");
7503 6 : type = error_mark_node;
7504 6 : continue;
7505 : }
7506 :
7507 979490 : itype = build_index_type (itype);
7508 : }
7509 982093 : if (this_size_varies)
7510 : {
7511 22472 : if (TREE_SIDE_EFFECTS (size))
7512 : {
7513 22109 : if (*expr)
7514 8178 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7515 : *expr, size);
7516 : else
7517 13931 : *expr = size;
7518 : }
7519 22472 : *expr_const_operands &= size_maybe_const;
7520 : }
7521 : }
7522 171313 : else if (decl_context == FIELD)
7523 : {
7524 85998 : bool flexible_array_member = false;
7525 85998 : if (array_parm_vla_unspec_p)
7526 : /* Field names can in fact have function prototype
7527 : scope so [*] is disallowed here through making
7528 : the field variably modified, not through being
7529 : something other than a declaration with function
7530 : prototype scope. */
7531 : size_varies = true;
7532 : else
7533 : {
7534 : const struct c_declarator *t = declarator;
7535 85995 : while (t->kind == cdk_attrs)
7536 0 : t = t->declarator;
7537 85995 : flexible_array_member = (t->kind == cdk_id);
7538 : }
7539 85995 : if (flexible_array_member
7540 85995 : && !in_system_header_at (input_location))
7541 84965 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7542 : "support flexible array members");
7543 :
7544 : /* ISO C99 Flexible array members are effectively
7545 : identical to GCC's zero-length array extension. */
7546 85998 : if (flexible_array_member)
7547 85975 : itype = build_index_type (NULL_TREE);
7548 : }
7549 :
7550 : /* Complain about arrays of incomplete types. */
7551 1153406 : if (!COMPLETE_TYPE_P (type))
7552 : {
7553 58 : auto_diagnostic_group d;
7554 58 : error_at (loc, "array type has incomplete element type %qT",
7555 : type);
7556 : /* See if we can be more helpful. */
7557 58 : if (TREE_CODE (type) == ARRAY_TYPE)
7558 : {
7559 29 : if (name)
7560 24 : inform (loc, "declaration of %qE as multidimensional "
7561 : "array must have bounds for all dimensions "
7562 : "except the first", name);
7563 : else
7564 5 : inform (loc, "declaration of multidimensional array "
7565 : "must have bounds for all dimensions except "
7566 : "the first");
7567 : }
7568 58 : type = error_mark_node;
7569 58 : }
7570 : else
7571 : /* When itype is NULL, a shared incomplete array type is
7572 : returned for all array of a given type. Elsewhere we
7573 : make sure we don't complete that type before copying
7574 : it, but here we want to make sure we don't ever
7575 : modify the shared type, so we gcc_assert (itype)
7576 : below. */
7577 : {
7578 1153348 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7579 1153348 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7580 0 : type = c_build_qualified_type (type,
7581 : ENCODE_QUAL_ADDR_SPACE (as));
7582 1153348 : if (array_parm_vla_unspec_p)
7583 150 : type = c_build_array_type_unspecified (type);
7584 : else
7585 1153198 : type = c_build_array_type (type, itype);
7586 : }
7587 :
7588 1153406 : if (array_parm_vla_unspec_p)
7589 : {
7590 : /* C99 6.7.5.2p4 */
7591 150 : if (decl_context == TYPENAME)
7592 6 : warning (0, "%<[*]%> not in a declaration");
7593 144 : else if (decl_context != GENERIC_ASSOC
7594 144 : && decl_context != PARM
7595 7 : && decl_context != FIELD)
7596 : {
7597 4 : error ("%<[*]%> not allowed in other than function prototype scope "
7598 : "or generic association");
7599 4 : type = error_mark_node;
7600 : }
7601 : size_varies = true;
7602 : }
7603 :
7604 1153406 : if (type != error_mark_node)
7605 : {
7606 : /* The GCC extension for zero-length arrays differs from
7607 : ISO flexible array members in that sizeof yields
7608 : zero. */
7609 1153344 : if (size && integer_zerop (size))
7610 : {
7611 2596 : gcc_assert (itype);
7612 2596 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7613 2596 : TYPE_SIZE (type) = bitsize_zero_node;
7614 2596 : TYPE_SIZE_UNIT (type) = size_zero_node;
7615 2596 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7616 : }
7617 :
7618 1153344 : if (!valid_array_size_p (loc, type, name))
7619 33 : type = error_mark_node;
7620 : }
7621 :
7622 1153406 : if (decl_context != PARM
7623 834791 : && (array_ptr_quals != TYPE_UNQUALIFIED
7624 834791 : || array_ptr_attrs != NULL_TREE
7625 834790 : || array_parm_static))
7626 : {
7627 2 : error_at (loc, "static or type qualifiers in non-parameter "
7628 : "array declarator");
7629 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7630 2 : array_ptr_attrs = NULL_TREE;
7631 2 : array_parm_static = false;
7632 : }
7633 1153406 : orig_qual_indirect++;
7634 1153406 : break;
7635 : }
7636 50559001 : case cdk_function:
7637 50559001 : {
7638 : /* Say it's a definition only for the declarator closest
7639 : to the identifier, apart possibly from some
7640 : attributes. */
7641 50559001 : bool really_funcdef = false;
7642 50559001 : tree arg_types;
7643 50559001 : orig_qual_type = NULL_TREE;
7644 50559001 : if (funcdef_flag)
7645 : {
7646 36301463 : const struct c_declarator *t = declarator->declarator;
7647 36301481 : while (t->kind == cdk_attrs)
7648 18 : t = t->declarator;
7649 36301463 : really_funcdef = (t->kind == cdk_id);
7650 : }
7651 :
7652 : /* Declaring a function type. Make sure we have a valid
7653 : type for the function to return. */
7654 50559001 : if (type == error_mark_node)
7655 0 : continue;
7656 :
7657 50559001 : size_varies = false;
7658 :
7659 : /* Warn about some types functions can't return. */
7660 50559001 : if (TREE_CODE (type) == FUNCTION_TYPE)
7661 : {
7662 0 : if (name)
7663 0 : error_at (loc, "%qE declared as function returning a "
7664 : "function", name);
7665 : else
7666 0 : error_at (loc, "type name declared as function "
7667 : "returning a function");
7668 0 : type = integer_type_node;
7669 : }
7670 50559001 : if (TREE_CODE (type) == ARRAY_TYPE)
7671 : {
7672 0 : if (name)
7673 0 : error_at (loc, "%qE declared as function returning an array",
7674 : name);
7675 : else
7676 0 : error_at (loc, "type name declared as function returning "
7677 : "an array");
7678 0 : type = integer_type_node;
7679 : }
7680 :
7681 : /* Construct the function type and go to the next
7682 : inner layer of declarator. */
7683 50559001 : arg_info = declarator->u.arg_info;
7684 50559001 : arg_types = grokparms (arg_info, really_funcdef);
7685 :
7686 : /* Type qualifiers before the return type of the function
7687 : qualify the return type, not the function type. */
7688 50559001 : if (type_quals)
7689 : {
7690 235 : const enum c_declspec_word ignored_quals_list[] =
7691 : {
7692 : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7693 : cdw_atomic, cdw_number_of_elements
7694 : };
7695 235 : location_t specs_loc
7696 235 : = smallest_type_quals_location (declspecs->locations,
7697 : ignored_quals_list);
7698 235 : if (specs_loc == UNKNOWN_LOCATION)
7699 139 : specs_loc = declspecs->locations[cdw_typedef];
7700 139 : if (specs_loc == UNKNOWN_LOCATION)
7701 15 : specs_loc = loc;
7702 :
7703 : /* Type qualifiers on a function return type are
7704 : normally permitted by the standard but have no
7705 : effect, so give a warning at -Wreturn-type.
7706 : Qualifiers on a void return type are banned on
7707 : function definitions in ISO C; GCC used to used
7708 : them for noreturn functions. The resolution of C11
7709 : DR#423 means qualifiers (other than _Atomic) are
7710 : actually removed from the return type when
7711 : determining the function type. For C23, _Atomic is
7712 : removed as well. */
7713 235 : int quals_used = type_quals;
7714 235 : if (flag_isoc23)
7715 : quals_used = 0;
7716 65 : else if (flag_isoc11)
7717 31 : quals_used &= TYPE_QUAL_ATOMIC;
7718 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7719 5 : pedwarn (specs_loc, 0,
7720 : "function definition has qualified void "
7721 : "return type");
7722 : else
7723 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7724 : "type qualifiers ignored on function "
7725 : "return type");
7726 :
7727 : /* Ensure an error for restrict on invalid types; the
7728 : DR#423 resolution is not entirely clear about
7729 : this. */
7730 235 : if (flag_isoc11
7731 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7732 241 : && (!POINTER_TYPE_P (type)
7733 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7734 4 : error_at (loc, "invalid use of %<restrict%>");
7735 235 : type = c_build_qualified_type (type, quals_used);
7736 : }
7737 50559001 : type_quals = TYPE_UNQUALIFIED;
7738 :
7739 101118002 : type = c_build_function_type (type, arg_types,
7740 50559001 : arg_info->no_named_args_stdarg_p);
7741 50559001 : declarator = declarator->declarator;
7742 :
7743 : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7744 : the formal parameter list of this FUNCTION_TYPE to point to
7745 : the FUNCTION_TYPE node itself. */
7746 50559001 : {
7747 50559001 : c_arg_tag *tag;
7748 50559001 : unsigned ix;
7749 :
7750 434608790 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7751 142 : TYPE_CONTEXT (tag->type) = type;
7752 : }
7753 : break;
7754 : }
7755 18236538 : case cdk_pointer:
7756 18236538 : {
7757 : /* Merge any constancy or volatility into the target type
7758 : for the pointer. */
7759 18236538 : if ((type_quals & TYPE_QUAL_ATOMIC)
7760 1988 : && TREE_CODE (type) == FUNCTION_TYPE)
7761 : {
7762 2 : error_at (loc,
7763 : "%<_Atomic%>-qualified function type");
7764 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7765 : }
7766 18236536 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7767 1701 : && type_quals)
7768 0 : pedwarn (loc, OPT_Wpedantic,
7769 : "ISO C forbids qualified function types");
7770 18234837 : if (type_quals)
7771 6004560 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7772 : orig_qual_indirect);
7773 18236538 : orig_qual_type = NULL_TREE;
7774 18236538 : size_varies = false;
7775 :
7776 : /* When the pointed-to type involves components of variable size,
7777 : care must be taken to ensure that the size evaluation code is
7778 : emitted early enough to dominate all the possible later uses
7779 : and late enough for the variables on which it depends to have
7780 : been assigned.
7781 :
7782 : This is expected to happen automatically when the pointed-to
7783 : type has a name/declaration of it's own, but special attention
7784 : is required if the type is anonymous. */
7785 18236538 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7786 : {
7787 5946 : bool bind_p = decl_context == TYPENAME
7788 : || decl_context == FIELD
7789 5946 : || decl_context == PARM;
7790 11386 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7791 : }
7792 :
7793 18236538 : type = c_build_pointer_type (type);
7794 :
7795 : /* Process type qualifiers (such as const or volatile)
7796 : that were given inside the `*'. */
7797 18236538 : type_quals = declarator->u.pointer_quals;
7798 :
7799 18236538 : declarator = declarator->declarator;
7800 18236538 : break;
7801 : }
7802 0 : default:
7803 0 : gcc_unreachable ();
7804 : }
7805 : }
7806 314093608 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7807 314093608 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7808 :
7809 : /* Now TYPE has the actual type, apart from any qualifiers in
7810 : TYPE_QUALS. */
7811 :
7812 : /* Warn about address space used for things other than static memory or
7813 : pointers. */
7814 314093608 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7815 314093608 : if (!ADDR_SPACE_GENERIC_P (address_space))
7816 : {
7817 10 : if (decl_context == NORMAL)
7818 : {
7819 10 : switch (storage_class)
7820 : {
7821 0 : case csc_auto:
7822 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7823 : c_addr_space_name (address_space), name);
7824 0 : break;
7825 0 : case csc_register:
7826 0 : error ("%qs combined with %<register%> qualifier for %qE",
7827 : c_addr_space_name (address_space), name);
7828 0 : break;
7829 6 : case csc_none:
7830 6 : if (current_function_scope)
7831 : {
7832 0 : error ("%qs specified for auto variable %qE",
7833 : c_addr_space_name (address_space), name);
7834 0 : break;
7835 : }
7836 : break;
7837 : case csc_static:
7838 : case csc_extern:
7839 : case csc_typedef:
7840 : break;
7841 0 : default:
7842 0 : gcc_unreachable ();
7843 : }
7844 : }
7845 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7846 : {
7847 0 : if (name)
7848 0 : error ("%qs specified for parameter %qE",
7849 : c_addr_space_name (address_space), name);
7850 : else
7851 0 : error ("%qs specified for unnamed parameter",
7852 : c_addr_space_name (address_space));
7853 : }
7854 0 : else if (decl_context == FIELD)
7855 : {
7856 0 : if (name)
7857 0 : error ("%qs specified for structure field %qE",
7858 : c_addr_space_name (address_space), name);
7859 : else
7860 0 : error ("%qs specified for structure field",
7861 : c_addr_space_name (address_space));
7862 : }
7863 : }
7864 :
7865 : /* Check the type and width of a bit-field. */
7866 314093608 : if (bitfield)
7867 : {
7868 52618 : check_bitfield_type_and_width (loc, &type, width, name);
7869 : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7870 : atomic types are permitted for bit-fields; we have no code to
7871 : make bit-field accesses atomic, so disallow them. */
7872 52618 : if (type_quals & TYPE_QUAL_ATOMIC)
7873 : {
7874 2 : if (name)
7875 1 : error_at (loc, "bit-field %qE has atomic type", name);
7876 : else
7877 1 : error_at (loc, "bit-field has atomic type");
7878 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7879 : }
7880 : }
7881 :
7882 : /* Reject invalid uses of _Alignas. */
7883 314093608 : if (declspecs->alignas_p)
7884 : {
7885 190 : if (storage_class == csc_typedef)
7886 1 : error_at (loc, "alignment specified for typedef %qE", name);
7887 189 : else if (storage_class == csc_register)
7888 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7889 : name);
7890 188 : else if (decl_context == PARM)
7891 : {
7892 2 : if (name)
7893 1 : error_at (loc, "alignment specified for parameter %qE", name);
7894 : else
7895 1 : error_at (loc, "alignment specified for unnamed parameter");
7896 : }
7897 186 : else if (bitfield)
7898 : {
7899 0 : if (name)
7900 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7901 : else
7902 0 : error_at (loc, "alignment specified for unnamed bit-field");
7903 : }
7904 186 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7905 1 : error_at (loc, "alignment specified for function %qE", name);
7906 185 : else if (declspecs->align_log != -1 && TYPE_P (type))
7907 : {
7908 158 : alignas_align = 1U << declspecs->align_log;
7909 158 : if (alignas_align < min_align_of_type (type))
7910 : {
7911 26 : if (name)
7912 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7913 : "alignment of %qE", name);
7914 : else
7915 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7916 : "alignment of unnamed field");
7917 : alignas_align = 0;
7918 : }
7919 : }
7920 : }
7921 :
7922 : /* If this is declaring a typedef name, return a TYPE_DECL. */
7923 :
7924 314093608 : if (storage_class == csc_typedef)
7925 : {
7926 4316931 : tree decl;
7927 4316931 : if ((type_quals & TYPE_QUAL_ATOMIC)
7928 12630 : && TREE_CODE (type) == FUNCTION_TYPE)
7929 : {
7930 0 : error_at (loc,
7931 : "%<_Atomic%>-qualified function type");
7932 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7933 : }
7934 4316931 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7935 609 : && type_quals)
7936 0 : pedwarn (loc, OPT_Wpedantic,
7937 : "ISO C forbids qualified function types");
7938 4316322 : if (type_quals)
7939 31590 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7940 : orig_qual_indirect);
7941 8633862 : decl = build_decl (declarator->id_loc,
7942 4316931 : TYPE_DECL, declarator->u.id.id, type);
7943 4316931 : if (declspecs->explicit_signed_p)
7944 356363 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7945 4316931 : if (declspecs->inline_p)
7946 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7947 4316931 : if (declspecs->noreturn_p)
7948 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7949 :
7950 4316931 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7951 : {
7952 1826 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7953 :
7954 1826 : if (b != NULL
7955 40 : && b->decl != NULL_TREE
7956 40 : && (B_IN_CURRENT_SCOPE (b)
7957 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7958 1862 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7959 : {
7960 7 : auto_diagnostic_group d;
7961 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7962 : "using %qD as both a typedef and a tag is "
7963 : "invalid in C++", decl)
7964 7 : && b->locus != UNKNOWN_LOCATION)
7965 6 : inform (b->locus, "originally defined here");
7966 7 : }
7967 : }
7968 :
7969 4316931 : return decl;
7970 : }
7971 :
7972 : /* If this is a type name (such as, in a cast or sizeof),
7973 : compute the type and return it now. */
7974 :
7975 309776677 : if (decl_context == TYPENAME || decl_context == GENERIC_ASSOC)
7976 : {
7977 : /* Note that the grammar rejects storage classes in typenames
7978 : and fields. */
7979 121499745 : gcc_assert (storage_class == csc_none && !threadp
7980 : && !declspecs->inline_p && !declspecs->noreturn_p);
7981 121499745 : if ((type_quals & TYPE_QUAL_ATOMIC)
7982 257 : && TREE_CODE (type) == FUNCTION_TYPE)
7983 : {
7984 0 : error_at (loc,
7985 : "%<_Atomic%>-qualified function type");
7986 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7987 : }
7988 121499745 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7989 22 : && type_quals)
7990 0 : pedwarn (loc, OPT_Wpedantic,
7991 : "ISO C forbids const or volatile function types");
7992 121499723 : if (type_quals)
7993 847 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7994 : orig_qual_indirect);
7995 121499745 : return type;
7996 : }
7997 :
7998 375291 : if (pedantic && decl_context == FIELD
7999 188301731 : && c_type_variably_modified_p (type))
8000 : {
8001 : /* C99 6.7.2.1p8 */
8002 7 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
8003 : "have a variably modified type");
8004 : }
8005 :
8006 : /* Aside from typedefs and type names (handle above),
8007 : `void' at top level (not within pointer)
8008 : is allowed only in public variables.
8009 : We don't complain about parms either, but that is because
8010 : a better error message can be made later. */
8011 :
8012 188276932 : if (VOID_TYPE_P (type) && decl_context != PARM
8013 64 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
8014 : && (storage_class == csc_extern
8015 27 : || (current_scope == file_scope
8016 18 : && !(storage_class == csc_static
8017 : || storage_class == csc_register)))))
8018 : {
8019 15 : error_at (loc, "variable or field %qE declared void", name);
8020 15 : type = integer_type_node;
8021 : }
8022 :
8023 : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8024 : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8025 :
8026 187322496 : {
8027 187322496 : tree decl;
8028 :
8029 187322496 : if (decl_context == PARM)
8030 : {
8031 124049077 : tree promoted_type;
8032 124049077 : bool array_parameter_p = false;
8033 :
8034 : /* A parameter declared as an array of T is really a pointer to T.
8035 : One declared as a function is really a pointer to a function. */
8036 :
8037 124049077 : if (TREE_CODE (type) == ARRAY_TYPE)
8038 : {
8039 436598 : if (!size_error)
8040 436584 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
8041 : *decl_attrs);
8042 :
8043 : /* Transfer const-ness of array into that of type pointed to. */
8044 436598 : type = TREE_TYPE (type);
8045 436598 : if (orig_qual_type != NULL_TREE)
8046 : {
8047 7 : if (orig_qual_indirect == 0)
8048 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
8049 : else
8050 2 : orig_qual_indirect--;
8051 : }
8052 436598 : if (type_quals)
8053 48194 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8054 : orig_qual_indirect);
8055 :
8056 : /* The pointed-to type may need a decl expr (see above). */
8057 436598 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
8058 : {
8059 373 : bool bind_p = decl_context == TYPENAME
8060 : || decl_context == FIELD
8061 : || decl_context == PARM;
8062 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
8063 : }
8064 :
8065 436598 : type = c_build_pointer_type (type);
8066 436598 : type_quals = array_ptr_quals;
8067 436598 : if (type_quals)
8068 965 : type = c_build_qualified_type (type, type_quals);
8069 :
8070 : /* We don't yet implement attributes in this context. */
8071 436598 : if (array_ptr_attrs != NULL_TREE)
8072 0 : warning_at (loc, OPT_Wattributes,
8073 : "attributes in parameter array declarator ignored");
8074 :
8075 : size_varies = false;
8076 : array_parameter_p = true;
8077 : }
8078 123612479 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8079 : {
8080 301 : if (type_quals & TYPE_QUAL_ATOMIC)
8081 : {
8082 1 : error_at (loc,
8083 : "%<_Atomic%>-qualified function type");
8084 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8085 : }
8086 300 : else if (type_quals)
8087 0 : pedwarn (loc, OPT_Wpedantic,
8088 : "ISO C forbids qualified function types");
8089 1 : if (type_quals)
8090 0 : type = c_build_qualified_type (type, type_quals);
8091 301 : type = c_build_pointer_type (type);
8092 301 : type_quals = TYPE_UNQUALIFIED;
8093 : }
8094 123612178 : else if (type_quals)
8095 9937565 : type = c_build_qualified_type (type, type_quals);
8096 :
8097 248098154 : decl = build_decl (declarator->id_loc,
8098 124049077 : PARM_DECL, declarator->u.id.id, type);
8099 124049077 : if (size_varies)
8100 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
8101 124049077 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
8102 :
8103 : /* Compute the type actually passed in the parmlist,
8104 : for the case where there is no prototype.
8105 : (For example, shorts and chars are passed as ints.)
8106 : When there is a prototype, this is overridden later. */
8107 :
8108 124049077 : if (type == error_mark_node)
8109 : promoted_type = type;
8110 : else
8111 124048993 : promoted_type = c_type_promotes_to (type);
8112 :
8113 124049077 : DECL_ARG_TYPE (decl) = promoted_type;
8114 124049077 : if (declspecs->inline_p)
8115 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8116 124049077 : if (declspecs->noreturn_p)
8117 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8118 : }
8119 64227855 : else if (decl_context == FIELD)
8120 : {
8121 : /* Note that the grammar rejects storage classes in typenames
8122 : and fields. */
8123 4325710 : gcc_assert (storage_class == csc_none && !threadp
8124 : && !declspecs->inline_p && !declspecs->noreturn_p);
8125 :
8126 : /* Structure field. It may not be a function. */
8127 :
8128 4325710 : if (TREE_CODE (type) == FUNCTION_TYPE)
8129 : {
8130 0 : error_at (loc, "field %qE declared as a function", name);
8131 0 : type = c_build_pointer_type (type);
8132 : }
8133 4325710 : else if (TREE_CODE (type) != ERROR_MARK
8134 4325710 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8135 : {
8136 24 : if (name)
8137 17 : error_at (loc, "field %qE has incomplete type", name);
8138 : else
8139 7 : error_at (loc, "unnamed field has incomplete type");
8140 24 : type = error_mark_node;
8141 : }
8142 4325686 : else if (TREE_CODE (type) == ARRAY_TYPE
8143 4325686 : && TYPE_DOMAIN (type) == NULL_TREE)
8144 : {
8145 : /* We have a flexible array member through a typedef.
8146 : Set suitable range. Whether this is a correct position
8147 : for a flexible array member will be determined elsewhere. */
8148 14 : if (!in_system_header_at (input_location))
8149 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8150 : "support flexible array members");
8151 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8152 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8153 : NULL_TREE);
8154 14 : if (orig_qual_indirect == 0)
8155 4325710 : orig_qual_type = NULL_TREE;
8156 : }
8157 4325710 : if (type != error_mark_node
8158 4325710 : && !verify_type_context (loc, TCTX_FIELD, type))
8159 0 : type = error_mark_node;
8160 :
8161 4325710 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8162 : orig_qual_indirect);
8163 8651420 : decl = build_decl (declarator->id_loc,
8164 4325710 : FIELD_DECL, declarator->u.id.id, type);
8165 4325710 : DECL_NONADDRESSABLE_P (decl) = bitfield;
8166 4325710 : if (bitfield && !declarator->u.id.id)
8167 9762 : DECL_PADDING_P (decl) = 1;
8168 :
8169 4325710 : if (size_varies)
8170 663 : C_DECL_VARIABLE_SIZE (decl) = 1;
8171 : }
8172 59902145 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8173 : {
8174 50983986 : if (storage_class == csc_register || threadp || constexprp)
8175 : {
8176 12 : error_at (loc, "invalid storage class for function %qE", name);
8177 : }
8178 50983974 : else if (current_scope != file_scope)
8179 : {
8180 : /* Function declaration not at file scope. Storage
8181 : classes other than `extern' are not allowed, C99
8182 : 6.7.1p5, and `extern' makes no difference. However,
8183 : GCC allows 'auto', perhaps with 'inline', to support
8184 : nested functions. */
8185 10556 : if (storage_class == csc_auto)
8186 66 : pedwarn (loc, OPT_Wpedantic,
8187 : "invalid storage class for function %qE", name);
8188 10490 : else if (storage_class == csc_static)
8189 : {
8190 20 : error_at (loc, "invalid storage class for function %qE", name);
8191 20 : if (funcdef_flag)
8192 8 : storage_class = declspecs->storage_class = csc_none;
8193 : else
8194 : return NULL_TREE;
8195 : }
8196 : }
8197 :
8198 101967948 : decl = build_decl (declarator->id_loc,
8199 50983974 : FUNCTION_DECL, declarator->u.id.id, type);
8200 50983974 : decl = build_decl_attribute_variant (decl, decl_attr);
8201 :
8202 50983974 : if (type_quals & TYPE_QUAL_ATOMIC)
8203 : {
8204 2 : error_at (loc,
8205 : "%<_Atomic%>-qualified function type");
8206 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
8207 : }
8208 50983972 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8209 9 : pedwarn (loc, OPT_Wpedantic,
8210 : "ISO C forbids qualified function types");
8211 :
8212 : /* Every function declaration is an external reference
8213 : (DECL_EXTERNAL) except for those which are not at file
8214 : scope and are explicitly declared "auto". This is
8215 : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8216 : GCC to signify a forward declaration of a nested function. */
8217 50983974 : if (storage_class == csc_auto && current_scope != file_scope)
8218 66 : DECL_EXTERNAL (decl) = 0;
8219 : /* In C99, a function which is declared 'inline' with 'extern'
8220 : is not an external reference (which is confusing). It
8221 : means that the later definition of the function must be output
8222 : in this file, C99 6.7.4p6. In GNU C89, a function declared
8223 : 'extern inline' is an external reference. */
8224 50983908 : else if (declspecs->inline_p && storage_class != csc_static)
8225 35452824 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8226 35452824 : == flag_gnu89_inline);
8227 : else
8228 15531084 : DECL_EXTERNAL (decl) = !initialized;
8229 :
8230 : /* Record absence of global scope for `static' or `auto'. */
8231 50983974 : TREE_PUBLIC (decl)
8232 50983974 : = !(storage_class == csc_static || storage_class == csc_auto);
8233 :
8234 : /* For a function definition, record the argument information
8235 : block where store_parm_decls will look for it. */
8236 50983974 : if (funcdef_flag)
8237 36301433 : current_function_arg_info = arg_info;
8238 :
8239 50983974 : if (declspecs->default_int_p)
8240 9220 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8241 :
8242 : /* Record presence of `inline' and `_Noreturn', if it is
8243 : reasonable. */
8244 50983974 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8245 : {
8246 47241 : if (declspecs->inline_p)
8247 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8248 47241 : if (declspecs->noreturn_p)
8249 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8250 : }
8251 : else
8252 : {
8253 50936733 : if (declspecs->inline_p)
8254 : /* Record that the function is declared `inline'. */
8255 35612562 : DECL_DECLARED_INLINE_P (decl) = 1;
8256 50936733 : if (declspecs->noreturn_p)
8257 : {
8258 23307 : if (flag_isoc99)
8259 23304 : pedwarn_c99 (loc, OPT_Wpedantic,
8260 : "ISO C99 does not support %<_Noreturn%>");
8261 : else
8262 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8263 : "ISO C90 does not support %<_Noreturn%>");
8264 23307 : TREE_THIS_VOLATILE (decl) = 1;
8265 : }
8266 : }
8267 :
8268 : /* C99 6.2.2p7: It is invalid (compile-time undefined
8269 : behavior) to create an 'extern' declaration for a
8270 : function if there is a global declaration that is
8271 : 'static' and the global declaration is not visible.
8272 : (If the static declaration _is_ currently visible,
8273 : the 'extern' declaration is taken to refer to that decl.) */
8274 50983974 : if (!initialized
8275 14682532 : && TREE_PUBLIC (decl)
8276 14674587 : && current_scope != file_scope)
8277 : {
8278 8889 : tree global_decl = identifier_global_value (declarator->u.id.id);
8279 8889 : tree visible_decl = lookup_name (declarator->u.id.id);
8280 :
8281 8889 : if (global_decl
8282 8889 : && global_decl != visible_decl
8283 1713 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8284 1713 : && !TREE_PUBLIC (global_decl))
8285 2 : error_at (loc, "function previously declared %<static%> "
8286 : "redeclared %<extern%>");
8287 : }
8288 : }
8289 : else
8290 : {
8291 : /* It's a variable. */
8292 : /* An uninitialized decl with `extern' is a reference. */
8293 8918159 : int extern_ref = !initialized && storage_class == csc_extern;
8294 :
8295 8918159 : if (constexprp)
8296 : {
8297 : /* The type of a constexpr variable must not be variably
8298 : modified, volatile, atomic or restrict qualified or
8299 : have a member with such a qualifier. const
8300 : qualification is implicitly added, and, at file scope,
8301 : has internal linkage. */
8302 356 : if (c_type_variably_modified_p (type))
8303 1 : error_at (loc, "%<constexpr%> object has variably modified "
8304 : "type");
8305 356 : if (type_quals
8306 356 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8307 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8308 : else
8309 : {
8310 347 : tree type_no_array = strip_array_types (type);
8311 347 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8312 347 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8313 8 : error_at (loc, "invalid qualifiers for field of "
8314 : "%<constexpr%> object");
8315 : }
8316 356 : type_quals |= TYPE_QUAL_CONST;
8317 356 : if (current_scope == file_scope)
8318 294 : storage_class = csc_static;
8319 : }
8320 :
8321 8918159 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8322 : orig_qual_indirect);
8323 :
8324 : /* C99 6.2.2p7: It is invalid (compile-time undefined
8325 : behavior) to create an 'extern' declaration for a
8326 : variable if there is a global declaration that is
8327 : 'static' and the global declaration is not visible.
8328 : (If the static declaration _is_ currently visible,
8329 : the 'extern' declaration is taken to refer to that decl.) */
8330 8918159 : if (extern_ref && current_scope != file_scope)
8331 : {
8332 1571 : tree global_decl = identifier_global_value (declarator->u.id.id);
8333 1571 : tree visible_decl = lookup_name (declarator->u.id.id);
8334 :
8335 1571 : if (global_decl
8336 1571 : && global_decl != visible_decl
8337 282 : && VAR_P (global_decl)
8338 282 : && !TREE_PUBLIC (global_decl))
8339 8 : error_at (loc, "variable previously declared %<static%> "
8340 : "redeclared %<extern%>");
8341 : }
8342 :
8343 17836318 : decl = build_decl (declarator->id_loc,
8344 8918159 : VAR_DECL, declarator->u.id.id, type);
8345 8918159 : if (size_varies)
8346 7439 : C_DECL_VARIABLE_SIZE (decl) = 1;
8347 8918159 : if (constexprp)
8348 356 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8349 :
8350 8918159 : if (declspecs->inline_p)
8351 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8352 8918159 : if (declspecs->noreturn_p)
8353 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8354 :
8355 : /* At file scope, an initialized extern declaration may follow
8356 : a static declaration. In that case, DECL_EXTERNAL will be
8357 : reset later in start_decl. */
8358 8918159 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8359 :
8360 : /* At file scope, the presence of a `static' or `register' storage
8361 : class specifier, or the absence of all storage class specifiers
8362 : makes this declaration a definition (perhaps tentative). Also,
8363 : the absence of `static' makes it public. */
8364 8918159 : if (current_scope == file_scope)
8365 : {
8366 1153278 : TREE_PUBLIC (decl) = storage_class != csc_static;
8367 1153278 : TREE_STATIC (decl) = !extern_ref;
8368 : }
8369 : /* Not at file scope, only `static' makes a static definition. */
8370 : else
8371 : {
8372 7764881 : TREE_STATIC (decl) = (storage_class == csc_static);
8373 7764881 : TREE_PUBLIC (decl) = extern_ref;
8374 : }
8375 :
8376 : // NB: Set a tentative TLS model to avoid tls_model attribute
8377 : // warnings due to lack of thread storage duration. It will
8378 : // be updated by c_decl_attributes later.
8379 8918159 : if (threadp)
8380 2816 : set_decl_tls_model (decl, TLS_MODEL_REAL);
8381 : }
8382 :
8383 188276920 : if ((storage_class == csc_extern
8384 138288933 : || (storage_class == csc_none
8385 137827352 : && TREE_CODE (type) == FUNCTION_TYPE
8386 809428 : && !funcdef_flag))
8387 188588364 : && c_type_variably_modified_p (type))
8388 : {
8389 : /* C99 6.7.5.2p2 */
8390 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8391 4 : error_at (loc, "non-nested function with variably modified type");
8392 : else
8393 2 : error_at (loc, "object with variably modified type must have "
8394 : "no linkage");
8395 : }
8396 :
8397 : /* For nested functions disqualify ones taking VLAs by value
8398 : from inlining since the middle-end cannot deal with this.
8399 : ??? We should arrange for those to be passed by reference
8400 : with emitting the copy on the caller side in the frontend. */
8401 188276920 : if (storage_class == csc_none
8402 137827352 : && TREE_CODE (type) == FUNCTION_TYPE)
8403 4202115 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8404 : {
8405 3392727 : tree arg = TREE_VALUE (al);
8406 3392727 : if (arg != error_mark_node
8407 3392727 : && C_TYPE_VARIABLE_SIZE (arg))
8408 : {
8409 40 : DECL_UNINLINABLE (decl) = 1;
8410 40 : break;
8411 : }
8412 : }
8413 :
8414 : /* Record `register' declaration for warnings on &
8415 : and in case doing stupid register allocation. */
8416 :
8417 188276920 : if (storage_class == csc_register
8418 3608 : && TREE_CODE (type) != FUNCTION_TYPE)
8419 : {
8420 3602 : C_DECL_REGISTER (decl) = 1;
8421 3602 : DECL_REGISTER (decl) = 1;
8422 : }
8423 :
8424 : /* Record constancy and volatility. */
8425 188276920 : c_apply_type_quals_to_decl (type_quals, decl);
8426 :
8427 : /* Apply _Alignas specifiers. */
8428 188276920 : if (alignas_align)
8429 : {
8430 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8431 123 : DECL_USER_ALIGN (decl) = 1;
8432 : }
8433 :
8434 : /* If a type has volatile components, it should be stored in memory.
8435 : Otherwise, the fact that those components are volatile
8436 : will be ignored, and would even crash the compiler.
8437 : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8438 188276920 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8439 188276920 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8440 : || TREE_CODE (decl) == RESULT_DECL))
8441 : {
8442 : /* It is not an error for a structure with volatile fields to
8443 : be declared register, but reset DECL_REGISTER since it
8444 : cannot actually go in a register. */
8445 184 : int was_reg = C_DECL_REGISTER (decl);
8446 184 : C_DECL_REGISTER (decl) = 0;
8447 184 : DECL_REGISTER (decl) = 0;
8448 184 : c_mark_addressable (decl);
8449 184 : C_DECL_REGISTER (decl) = was_reg;
8450 : }
8451 :
8452 : /* This is the earliest point at which we might know the assembler
8453 : name of a variable. Thus, if it's known before this, die horribly. */
8454 188276920 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8455 : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8456 :
8457 188276920 : if (warn_cxx_compat
8458 24549 : && VAR_P (decl)
8459 7548 : && TREE_PUBLIC (decl)
8460 2805 : && TREE_STATIC (decl)
8461 2356 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8462 2224 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8463 188277067 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8464 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8465 : "non-local variable %qD with anonymous type is "
8466 : "questionable in C++", decl);
8467 :
8468 : return decl;
8469 : }
8470 : }
8471 :
8472 : /* Decode the parameter-list info for a function type or function definition.
8473 : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8474 : if there is an identifier list instead of a parameter decl list).
8475 : These two functions are separate because when a function returns
8476 : or receives functions then each is called multiple times but the order
8477 : of calls is different. The last call to `grokparms' is always the one
8478 : that contains the formal parameter names of a function definition.
8479 :
8480 : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8481 :
8482 : FUNCDEF_FLAG is true for a function definition, false for
8483 : a mere declaration. A nonempty identifier-list gets an error message
8484 : when FUNCDEF_FLAG is false. */
8485 :
8486 : static tree
8487 50559001 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8488 : {
8489 50559001 : tree arg_types = arg_info->types;
8490 :
8491 50559001 : if (funcdef_flag && arg_info->had_vla_unspec)
8492 : {
8493 : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8494 : /* C99 6.7.5.2p4 */
8495 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8496 : }
8497 :
8498 715595 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8499 50562043 : && !in_system_header_at (input_location))
8500 3042 : warning (OPT_Wstrict_prototypes,
8501 : "function declaration isn%'t a prototype");
8502 :
8503 50559001 : if (arg_types == error_mark_node)
8504 : /* Don't set TYPE_ARG_TYPES in this case. */
8505 : return NULL_TREE;
8506 :
8507 100345898 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8508 : {
8509 8685 : if (!funcdef_flag)
8510 : {
8511 13 : permerror_opt (input_location,
8512 13 : OPT_Wdeclaration_missing_parameter_type,
8513 : "parameter names (without types) in "
8514 : "function declaration");
8515 13 : arg_info->parms = NULL_TREE;
8516 : }
8517 : else
8518 8672 : arg_info->parms = arg_info->types;
8519 :
8520 8685 : arg_info->types = NULL_TREE;
8521 8685 : return NULL_TREE;
8522 : }
8523 : else
8524 : {
8525 50550316 : tree parm, type, typelt;
8526 50550316 : unsigned int parmno;
8527 :
8528 : /* In C23, convert () to (void). */
8529 50550316 : if (flag_isoc23
8530 41501579 : && !arg_types
8531 764606 : && !arg_info->parms
8532 764606 : && !arg_info->no_named_args_stdarg_p)
8533 : {
8534 764464 : arg_types = arg_info->types = void_list_node;
8535 764464 : arg_info->c23_empty_parens = 1;
8536 : }
8537 :
8538 : /* If there is a parameter of incomplete type in a definition,
8539 : this is an error. In a declaration this is valid, and a
8540 : struct or union type may be completed later, before any calls
8541 : or definition of the function. In the case where the tag was
8542 : first declared within the parameter list, a warning has
8543 : already been given. If a parameter has void type, then
8544 : this has already received an error (constraint violation in C2Y,
8545 : previously implicitly undefined behavior). */
8546 :
8547 50550316 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8548 173622122 : parm;
8549 123071806 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8550 : {
8551 123071806 : type = TREE_VALUE (typelt);
8552 123071806 : if (type == error_mark_node)
8553 61 : continue;
8554 :
8555 123071745 : if (!COMPLETE_TYPE_P (type))
8556 : {
8557 40 : if (funcdef_flag)
8558 : {
8559 13 : if (DECL_NAME (parm))
8560 13 : error_at (input_location,
8561 : "parameter %u (%q+D) has incomplete type",
8562 : parmno, parm);
8563 : else
8564 0 : error_at (DECL_SOURCE_LOCATION (parm),
8565 : "parameter %u has incomplete type",
8566 : parmno);
8567 :
8568 13 : TREE_VALUE (typelt) = error_mark_node;
8569 13 : TREE_TYPE (parm) = error_mark_node;
8570 13 : arg_types = NULL_TREE;
8571 : }
8572 : }
8573 :
8574 123071745 : if (DECL_NAME (parm) && TREE_USED (parm))
8575 17655 : warn_if_shadowing (parm);
8576 : }
8577 : return arg_types;
8578 : }
8579 : }
8580 :
8581 : /* Allocate and initialize a c_arg_info structure from the parser's
8582 : obstack. */
8583 :
8584 : struct c_arg_info *
8585 50559019 : build_arg_info (void)
8586 : {
8587 50559019 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8588 50559019 : ret->parms = NULL_TREE;
8589 50559019 : ret->tags = NULL;
8590 50559019 : ret->types = NULL_TREE;
8591 50559019 : ret->others = NULL_TREE;
8592 50559019 : ret->pending_sizes = NULL;
8593 50559019 : ret->had_vla_unspec = 0;
8594 50559019 : ret->no_named_args_stdarg_p = 0;
8595 50559019 : ret->c23_empty_parens = 0;
8596 50559019 : return ret;
8597 : }
8598 :
8599 : /* Take apart the current scope and return a c_arg_info structure with
8600 : info on a parameter list just parsed.
8601 :
8602 : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8603 :
8604 : ELLIPSIS being true means the argument list ended in '...' so don't
8605 : append a sentinel (void_list_node) to the end of the type-list.
8606 :
8607 : EXPR is NULL or an expression that needs to be evaluated for the
8608 : side effects of array size expressions in the parameters. */
8609 :
8610 : struct c_arg_info *
8611 49778241 : get_parm_info (bool ellipsis, tree expr)
8612 : {
8613 49778241 : struct c_binding *b = current_scope->bindings;
8614 49778241 : struct c_arg_info *arg_info = build_arg_info ();
8615 :
8616 49778241 : tree parms = NULL_TREE;
8617 49778241 : vec<c_arg_tag, va_gc> *tags = NULL;
8618 49778241 : tree types = NULL_TREE;
8619 49778241 : tree others = NULL_TREE;
8620 :
8621 49778241 : bool gave_void_only_once_err = false;
8622 :
8623 49778241 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8624 :
8625 : /* The bindings in this scope must not get put into a block.
8626 : We will take care of deleting the binding nodes. */
8627 49778241 : current_scope->bindings = 0;
8628 :
8629 : /* This function is only called if there was *something* on the
8630 : parameter list. */
8631 49778241 : gcc_assert (b);
8632 :
8633 : /* A parameter list consisting solely of 'void' indicates that the
8634 : function takes no arguments. But if the 'void' is qualified
8635 : (by 'const' or 'volatile'), or has a storage class specifier
8636 : ('register'), then the behavior is undefined; issue an error.
8637 : Typedefs for 'void' are OK (see DR#157). */
8638 49778241 : if (b->prev == 0 /* one binding */
8639 13244699 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8640 13244699 : && !DECL_NAME (b->decl) /* anonymous */
8641 51591274 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8642 : {
8643 954322 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8644 954322 : || C_DECL_REGISTER (b->decl))
8645 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8646 :
8647 : /* There cannot be an ellipsis. */
8648 954322 : if (ellipsis)
8649 17 : error_at (b->locus, "%<void%> must be the only parameter");
8650 :
8651 954322 : arg_info->types = void_list_node;
8652 954322 : return arg_info;
8653 : }
8654 :
8655 48823919 : if (!ellipsis)
8656 48610991 : types = void_list_node;
8657 :
8658 : /* Break up the bindings list into parms, tags, types, and others;
8659 : apply sanity checks; purge the name-to-decl bindings. */
8660 171896188 : while (b)
8661 : {
8662 123072269 : tree decl = b->decl;
8663 123072269 : tree type = TREE_TYPE (decl);
8664 123072269 : c_arg_tag tag;
8665 123072269 : const char *keyword;
8666 :
8667 123072269 : switch (TREE_CODE (decl))
8668 : {
8669 123071929 : case PARM_DECL:
8670 123071929 : if (b->id)
8671 : {
8672 119438625 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8673 119438625 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8674 : }
8675 :
8676 : /* Check for forward decls that never got their actual decl. */
8677 123071929 : if (TREE_ASM_WRITTEN (decl))
8678 4 : error_at (b->locus,
8679 : "parameter %q+D has just a forward declaration", decl);
8680 : /* Check for (..., void, ...) and named void parameters and issue an
8681 : error. */
8682 123071925 : else if (VOID_TYPE_P (type))
8683 : {
8684 114 : if (!gave_void_only_once_err)
8685 : {
8686 114 : error_at (b->locus,
8687 : "%<void%> must be the only parameter and unnamed");
8688 114 : gave_void_only_once_err = true;
8689 : }
8690 : }
8691 : else
8692 : {
8693 : /* Valid parameter, add it to the list. */
8694 123071811 : DECL_CHAIN (decl) = parms;
8695 123071811 : parms = decl;
8696 :
8697 : /* Since there is a prototype, args are passed in their
8698 : declared types. The back end may override this later. */
8699 123071811 : DECL_ARG_TYPE (decl) = type;
8700 123071811 : types = tree_cons (0, type, types);
8701 : }
8702 : break;
8703 :
8704 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8705 20 : case UNION_TYPE: keyword = "union"; goto tag;
8706 101 : case RECORD_TYPE: keyword = "struct"; goto tag;
8707 142 : tag:
8708 : /* Types may not have tag-names, in which case the type
8709 : appears in the bindings list with b->id NULL. */
8710 142 : if (b->id)
8711 : {
8712 97 : gcc_assert (I_TAG_BINDING (b->id) == b);
8713 97 : I_TAG_BINDING (b->id) = b->shadowed;
8714 : }
8715 :
8716 : /* Warn about any struct, union or enum tags defined in a
8717 : parameter list. The scope of such types is limited to
8718 : the parameter list, which is rarely if ever desirable
8719 : (it's impossible to call such a function with type-
8720 : correct arguments). An anonymous union parm type is
8721 : meaningful as a GNU extension, so don't warn for that. */
8722 142 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8723 : {
8724 122 : if (b->id)
8725 : {
8726 : /* The %s will be one of 'struct', 'union', or 'enum'. */
8727 97 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8728 73 : warning_at (b->locus, 0,
8729 : "%<%s %E%> declared inside parameter list"
8730 : " will not be visible outside of this definition or"
8731 : " declaration", keyword, b->id);
8732 : }
8733 : else
8734 : /* The %s will be one of 'struct', 'union', or 'enum'. */
8735 25 : warning_at (b->locus, 0,
8736 : "anonymous %s declared inside parameter list"
8737 : " will not be visible outside of this definition or"
8738 : " declaration", keyword);
8739 : }
8740 :
8741 142 : tag.id = b->id;
8742 142 : tag.type = decl;
8743 142 : vec_safe_push (tags, tag);
8744 142 : break;
8745 :
8746 20 : case FUNCTION_DECL:
8747 : /* FUNCTION_DECLs appear when there is an implicit function
8748 : declaration in the parameter list. */
8749 20 : gcc_assert (b->nested || seen_error ());
8750 20 : goto set_shadowed;
8751 :
8752 144 : case CONST_DECL:
8753 144 : case TYPE_DECL:
8754 : /* CONST_DECLs appear here when we have an embedded enum,
8755 : and TYPE_DECLs appear here when we have an embedded struct
8756 : or union. No warnings for this - we already warned about the
8757 : type itself. */
8758 :
8759 : /* When we reinsert this decl in the function body, we need
8760 : to reconstruct whether it was marked as nested. */
8761 144 : gcc_assert (!b->nested);
8762 144 : DECL_CHAIN (decl) = others;
8763 144 : others = decl;
8764 : /* fall through */
8765 :
8766 198 : case ERROR_MARK:
8767 198 : set_shadowed:
8768 : /* error_mark_node appears here when we have an undeclared
8769 : variable. Just throw it away. */
8770 198 : if (b->id)
8771 : {
8772 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8773 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8774 : }
8775 : break;
8776 :
8777 : /* Other things that might be encountered. */
8778 0 : case LABEL_DECL:
8779 0 : case VAR_DECL:
8780 0 : default:
8781 0 : gcc_unreachable ();
8782 : }
8783 :
8784 123072269 : b = free_binding_and_advance (b);
8785 : }
8786 :
8787 48823919 : arg_info->parms = parms;
8788 48823919 : arg_info->tags = tags;
8789 48823919 : arg_info->types = types;
8790 48823919 : arg_info->others = others;
8791 48823919 : arg_info->pending_sizes = expr;
8792 48823919 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8793 48823919 : return arg_info;
8794 : }
8795 :
8796 : /* Get the struct, enum or union (CODE says which) with tag NAME.
8797 : Define the tag as a forward-reference with location LOC if it is
8798 : not defined. HAVE_STD_ATTRS says whether any standard attributes
8799 : were present after the struct, union or enum keyword; ATTRS are the
8800 : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8801 : whether an enum type specifier (": specifier-qualifier-list") is
8802 : present; if so, this is called before that specifier is parsed, so
8803 : that the tag is in scope for that specifier. Return a c_typespec
8804 : structure for the type specifier. */
8805 :
8806 : struct c_typespec
8807 1084825 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8808 : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8809 : {
8810 1084825 : struct c_typespec ret;
8811 1084825 : tree ref;
8812 1084825 : location_t refloc;
8813 :
8814 1084825 : ret.expr = NULL_TREE;
8815 1084825 : ret.expr_const_operands = true;
8816 1084825 : ret.has_enum_type_specifier = has_enum_type_specifier;
8817 :
8818 : /* If a cross reference is requested, look up the type already
8819 : defined for this tag and return it. If an enum type specifier is
8820 : present, only a definition in the current scope is relevant. */
8821 :
8822 1084825 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8823 :
8824 : /* If the visble type is still being defined, see if there is
8825 : an earlier definition (which may be complete). We do not
8826 : have to loop because nested redefinitions are not allowed. */
8827 1084825 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8828 : {
8829 85970 : tree vis = previous_tag (ref);
8830 85970 : if (vis)
8831 16 : ref = vis;
8832 : }
8833 :
8834 : /* If this is the right type of tag, return what we found.
8835 : (This reference will be shadowed by shadow_tag later if appropriate.)
8836 : If this is the wrong type of tag, do not return it. If it was the
8837 : wrong type in the same scope, we will have had an error
8838 : message already; if in a different scope and declaring
8839 : a name, pending_xref_error will give an error message; but if in a
8840 : different scope and not declaring a name, this tag should
8841 : shadow the previous declaration of a different type of tag, and
8842 : this would not work properly if we return the reference found.
8843 : (For example, with "struct foo" in an outer scope, "union foo;"
8844 : must shadow that tag with a new one of union type.) */
8845 2169650 : ret.kind = (ref
8846 1084825 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8847 94676 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8848 1084825 : if (ref && TREE_CODE (ref) == code)
8849 : {
8850 990115 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8851 990115 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8852 5 : && loc != UNKNOWN_LOCATION
8853 990120 : && warn_cxx_compat)
8854 : {
8855 5 : auto_diagnostic_group d;
8856 5 : switch (code)
8857 : {
8858 2 : case ENUMERAL_TYPE:
8859 2 : if (warning_at (loc, OPT_Wc___compat,
8860 : ("enum type defined in struct or union "
8861 : "is not visible in C++")))
8862 2 : inform (refloc, "enum type defined here");
8863 : break;
8864 2 : case RECORD_TYPE:
8865 2 : if (warning_at (loc, OPT_Wc___compat,
8866 : ("struct defined in struct or union "
8867 : "is not visible in C++")))
8868 2 : inform (refloc, "struct defined here");
8869 : break;
8870 1 : case UNION_TYPE:
8871 1 : if (warning_at (loc, OPT_Wc___compat,
8872 : ("union defined in struct or union "
8873 : "is not visible in C++")))
8874 1 : inform (refloc, "union defined here");
8875 : break;
8876 0 : default:
8877 0 : gcc_unreachable();
8878 : }
8879 5 : }
8880 :
8881 990115 : ret.spec = ref;
8882 990115 : return ret;
8883 : }
8884 :
8885 : /* If no such tag is yet defined, create a forward-reference node
8886 : and record it as the "definition".
8887 : When a real declaration of this type is found,
8888 : the forward-reference will be altered into a real type. */
8889 :
8890 94710 : ref = make_node (code);
8891 94710 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8892 72444 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8893 72444 : if (code == ENUMERAL_TYPE)
8894 : {
8895 : /* Give the type a default layout like unsigned int
8896 : to avoid crashing if it does not get defined. */
8897 252 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8898 252 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8899 252 : TYPE_USER_ALIGN (ref) = 0;
8900 252 : TYPE_UNSIGNED (ref) = 1;
8901 252 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8902 252 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8903 252 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8904 252 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8905 : }
8906 :
8907 94710 : pushtag (loc, name, ref);
8908 94710 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8909 94710 : if (in_underspecified_init)
8910 9 : error_at (loc, "%qT declared in underspecified object initializer",
8911 : ref);
8912 :
8913 94710 : ret.spec = ref;
8914 94710 : return ret;
8915 : }
8916 :
8917 : /* Get the struct, enum or union (CODE says which) with tag NAME.
8918 : Define the tag as a forward-reference if it is not defined.
8919 : Return a tree for the type. */
8920 :
8921 : tree
8922 0 : xref_tag (enum tree_code code, tree name)
8923 : {
8924 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8925 0 : false).spec;
8926 : }
8927 :
8928 : /* Make sure that the tag NAME is defined *in the current scope*
8929 : at least as a forward reference.
8930 : LOC is the location of the struct's definition.
8931 : CODE says which kind of tag NAME ought to be.
8932 :
8933 : This stores the current value of the file static STRUCT_PARSE_INFO
8934 : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8935 : new c_struct_parse_info structure. The old value of
8936 : STRUCT_PARSE_INFO is restored in finish_struct. */
8937 :
8938 : tree
8939 1179714 : start_struct (location_t loc, enum tree_code code, tree name,
8940 : class c_struct_parse_info **enclosing_struct_parse_info)
8941 : {
8942 : /* If there is already a tag defined at this scope
8943 : (as a forward reference), just return it. */
8944 :
8945 1179714 : tree ref = NULL_TREE;
8946 1179714 : location_t refloc = UNKNOWN_LOCATION;
8947 :
8948 1179714 : if (name != NULL_TREE)
8949 485154 : ref = lookup_tag (code, name, true, &refloc);
8950 :
8951 : /* For C23, even if we already have a completed definition,
8952 : we do not use it. We will check for consistency later.
8953 : If we are in a nested redefinition the type is not
8954 : complete. We will then detect this below. */
8955 1179714 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8956 : ref = NULL_TREE;
8957 :
8958 1179606 : if (ref && TREE_CODE (ref) == code)
8959 : {
8960 25147 : if (TYPE_STUB_DECL (ref))
8961 25147 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8962 :
8963 25147 : if (TYPE_SIZE (ref))
8964 : {
8965 11 : auto_diagnostic_group d;
8966 11 : if (code == UNION_TYPE)
8967 2 : error_at (loc, "redefinition of %<union %E%>", name);
8968 : else
8969 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8970 11 : if (refloc != UNKNOWN_LOCATION)
8971 11 : inform (refloc, "originally defined here");
8972 : /* Don't create structures using a name already in use. */
8973 11 : ref = NULL_TREE;
8974 11 : }
8975 25136 : else if (C_TYPE_BEING_DEFINED (ref))
8976 : {
8977 17 : if (code == UNION_TYPE)
8978 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8979 : else
8980 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8981 : /* Don't bother to report "originally defined here" for a
8982 : nested redefinition; the original definition should be
8983 : obvious. */
8984 : /* Don't create structures that contain themselves. */
8985 : ref = NULL_TREE;
8986 : }
8987 : }
8988 :
8989 : /* Otherwise create a forward-reference just so the tag is in scope. */
8990 :
8991 25133 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8992 : {
8993 1154595 : ref = make_node (code);
8994 1154595 : if (flag_isoc23)
8995 925633 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8996 1154595 : pushtag (loc, name, ref);
8997 : }
8998 :
8999 1179714 : C_TYPE_BEING_DEFINED (ref) = 1;
9000 2389996 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
9001 1210282 : TYPE_PACKED (v) = flag_pack_struct;
9002 :
9003 1179714 : *enclosing_struct_parse_info = struct_parse_info;
9004 1179714 : struct_parse_info = new c_struct_parse_info ();
9005 1179714 : struct_parse_info->refloc = refloc;
9006 :
9007 : /* FIXME: This will issue a warning for a use of a type defined
9008 : within a statement expr used within sizeof, et. al. This is not
9009 : terribly serious as C++ doesn't permit statement exprs within
9010 : sizeof anyhow. */
9011 1179714 : if (warn_cxx_compat
9012 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
9013 10 : warning_at (loc, OPT_Wc___compat,
9014 : "defining type in %qs expression is invalid in C++",
9015 : (in_sizeof ? "sizeof"
9016 4 : : in_typeof ? "typeof"
9017 1 : : in_alignof ? "alignof"
9018 : : "_Countof"));
9019 :
9020 1179714 : if (in_underspecified_init)
9021 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
9022 :
9023 1179714 : return ref;
9024 : }
9025 :
9026 : /* Process the specs, declarator and width (NULL if omitted)
9027 : of a structure component, returning a FIELD_DECL node.
9028 : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
9029 : DECL_ATTRS is as for grokdeclarator.
9030 :
9031 : LOC is the location of the structure component.
9032 :
9033 : This is done during the parsing of the struct declaration.
9034 : The FIELD_DECL nodes are chained together and the lot of them
9035 : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
9036 :
9037 : tree
9038 4325739 : grokfield (location_t loc,
9039 : struct c_declarator *declarator, struct c_declspecs *declspecs,
9040 : tree width, tree *decl_attrs, tree *expr)
9041 : {
9042 4325739 : tree value;
9043 :
9044 4325739 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
9045 20757 : && width == NULL_TREE)
9046 : {
9047 : /* This is an unnamed decl.
9048 :
9049 : If we have something of the form "union { list } ;" then this
9050 : is the anonymous union extension. Similarly for struct.
9051 :
9052 : If this is something of the form "struct foo;", then
9053 : If MS or Plan 9 extensions are enabled, this is handled as
9054 : an anonymous struct.
9055 : Otherwise this is a forward declaration of a structure tag.
9056 :
9057 : If this is something of the form "foo;" and foo is a TYPE_DECL, then
9058 : If foo names a structure or union without a tag, then this
9059 : is an anonymous struct (this is permitted by C11).
9060 : If MS or Plan 9 extensions are enabled and foo names a
9061 : structure, then again this is an anonymous struct.
9062 : Otherwise this is an error.
9063 :
9064 : Oh what a horrid tangled web we weave. I wonder if MS consciously
9065 : took this from Plan 9 or if it was an accident of implementation
9066 : that took root before someone noticed the bug... */
9067 :
9068 10995 : tree type = declspecs->type;
9069 10995 : bool ok = false;
9070 :
9071 10995 : if (RECORD_OR_UNION_TYPE_P (type)
9072 10989 : && (flag_ms_extensions
9073 10960 : || flag_plan9_extensions
9074 10929 : || !declspecs->typedef_p))
9075 : {
9076 10982 : if (flag_ms_extensions || flag_plan9_extensions)
9077 : ok = true;
9078 10922 : else if (TYPE_NAME (type) == NULL)
9079 : ok = true;
9080 : else
9081 : ok = false;
9082 : }
9083 : if (!ok)
9084 : {
9085 29 : pedwarn (loc, 0, "declaration does not declare anything");
9086 29 : return NULL_TREE;
9087 : }
9088 10966 : if (flag_isoc99)
9089 10944 : pedwarn_c99 (loc, OPT_Wpedantic,
9090 : "ISO C99 doesn%'t support unnamed structs/unions");
9091 : else
9092 22 : pedwarn_c99 (loc, OPT_Wpedantic,
9093 : "ISO C90 doesn%'t support unnamed structs/unions");
9094 : }
9095 :
9096 4325710 : value = grokdeclarator (declarator, declspecs, FIELD, false,
9097 4325710 : width ? &width : NULL, decl_attrs, expr, NULL,
9098 : DEPRECATED_NORMAL);
9099 :
9100 : /* When this field has name, its type is a top level type, we should
9101 : call verify_counted_by_for_top_anonymous_type. */
9102 4325710 : if (DECL_NAME (value) != NULL_TREE
9103 4325710 : && declspecs->typespec_kind == ctsk_tagdef)
9104 153187 : verify_counted_by_for_top_anonymous_type (declspecs->type);
9105 :
9106 4325710 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9107 4325710 : DECL_INITIAL (value) = width;
9108 4325710 : if (width)
9109 52618 : SET_DECL_C_BIT_FIELD (value);
9110 :
9111 4325710 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9112 : {
9113 : /* If we currently have a binding for this field, set the
9114 : in_struct field in the binding, so that we warn about lookups
9115 : which find it. */
9116 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9117 1312 : if (b != NULL)
9118 : {
9119 : /* If the in_struct field is not yet set, push it on a list
9120 : to be cleared when this struct is finished. */
9121 93 : if (!b->in_struct)
9122 : {
9123 91 : struct_parse_info->fields.safe_push (b);
9124 91 : b->in_struct = 1;
9125 : }
9126 : }
9127 : }
9128 :
9129 : return value;
9130 : }
9131 :
9132 : /* Subroutine of detect_field_duplicates: return whether X and Y,
9133 : which are both fields in the same struct, have duplicate field
9134 : names. */
9135 :
9136 : static bool
9137 4313038 : is_duplicate_field (tree x, tree y)
9138 : {
9139 4313038 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9140 : return true;
9141 :
9142 : /* When using -fplan9-extensions, an anonymous field whose name is a
9143 : typedef can duplicate a field name. */
9144 4313037 : if (flag_plan9_extensions
9145 4313037 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9146 : {
9147 0 : tree xt, xn, yt, yn;
9148 :
9149 0 : xt = TREE_TYPE (x);
9150 0 : if (DECL_NAME (x) != NULL_TREE)
9151 0 : xn = DECL_NAME (x);
9152 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
9153 0 : && TYPE_NAME (xt) != NULL_TREE
9154 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9155 0 : xn = DECL_NAME (TYPE_NAME (xt));
9156 : else
9157 : xn = NULL_TREE;
9158 :
9159 0 : yt = TREE_TYPE (y);
9160 0 : if (DECL_NAME (y) != NULL_TREE)
9161 0 : yn = DECL_NAME (y);
9162 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
9163 0 : && TYPE_NAME (yt) != NULL_TREE
9164 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9165 0 : yn = DECL_NAME (TYPE_NAME (yt));
9166 : else
9167 : yn = NULL_TREE;
9168 :
9169 0 : if (xn != NULL_TREE && xn == yn)
9170 0 : return true;
9171 : }
9172 :
9173 : return false;
9174 : }
9175 :
9176 : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9177 : to HTAB, giving errors for any duplicates. */
9178 :
9179 : static void
9180 80955 : detect_field_duplicates_hash (tree fieldlist,
9181 : hash_table<nofree_ptr_hash <tree_node> > *htab)
9182 : {
9183 80955 : tree x, y;
9184 80955 : tree_node **slot;
9185 :
9186 1417291 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
9187 1336336 : if ((y = DECL_NAME (x)) != NULL_TREE)
9188 : {
9189 1315557 : slot = htab->find_slot (y, INSERT);
9190 1315557 : if (*slot)
9191 : {
9192 15 : error ("duplicate member %q+D", x);
9193 15 : DECL_NAME (x) = NULL_TREE;
9194 : }
9195 1315557 : *slot = y;
9196 : }
9197 20779 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9198 : {
9199 11733 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9200 :
9201 : /* When using -fplan9-extensions, an anonymous field whose
9202 : name is a typedef can duplicate a field name. */
9203 11733 : if (flag_plan9_extensions
9204 31 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9205 11763 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9206 : {
9207 10 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9208 10 : slot = htab->find_slot (xn, INSERT);
9209 10 : if (*slot)
9210 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9211 10 : *slot = xn;
9212 : }
9213 : }
9214 80955 : }
9215 :
9216 : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9217 : the list such that this does not present a problem later. */
9218 :
9219 : static void
9220 1179844 : detect_field_duplicates (tree fieldlist)
9221 : {
9222 1179844 : tree x, y;
9223 1179844 : int timeout = 10;
9224 :
9225 : /* If the struct is the list of instance variables of an Objective-C
9226 : class, then we need to check all the instance variables of
9227 : superclasses when checking for duplicates (since you can't have
9228 : an instance variable in a subclass with the same name as an
9229 : instance variable in a superclass). We pass on this job to the
9230 : Objective-C compiler. objc_detect_field_duplicates() will return
9231 : false if we are not checking the list of instance variables and
9232 : the C frontend should proceed with the standard field duplicate
9233 : checks. If we are checking the list of instance variables, the
9234 : ObjC frontend will do the check, emit the errors if needed, and
9235 : then return true. */
9236 1179844 : if (c_dialect_objc ())
9237 0 : if (objc_detect_field_duplicates (false))
9238 : return;
9239 :
9240 : /* First, see if there are more than "a few" fields.
9241 : This is trivially true if there are zero or one fields. */
9242 1179844 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9243 : return;
9244 : x = fieldlist;
9245 3477647 : do {
9246 3477647 : timeout--;
9247 3477647 : if (DECL_NAME (x) == NULL_TREE
9248 3477647 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9249 : timeout = 0;
9250 3477647 : x = DECL_CHAIN (x);
9251 3477647 : } while (timeout > 0 && x);
9252 :
9253 : /* If there were "few" fields and no anonymous structures or unions,
9254 : avoid the overhead of allocating a hash table. Instead just do
9255 : the nested traversal thing. */
9256 974081 : if (timeout > 0)
9257 : {
9258 2822520 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9259 : /* When using -fplan9-extensions, we can have duplicates
9260 : between typedef names and fields. */
9261 1917661 : if (DECL_NAME (x)
9262 1917661 : || (flag_plan9_extensions
9263 0 : && DECL_NAME (x) == NULL_TREE
9264 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9265 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9266 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9267 : {
9268 6230163 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9269 4313038 : if (is_duplicate_field (y, x))
9270 : {
9271 1 : error ("duplicate member %q+D", x);
9272 1 : DECL_NAME (x) = NULL_TREE;
9273 : }
9274 : }
9275 : }
9276 : else
9277 : {
9278 69222 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9279 69222 : detect_field_duplicates_hash (fieldlist, &htab);
9280 69222 : }
9281 : }
9282 :
9283 : /* Finish up struct info used by -Wc++-compat. */
9284 :
9285 : static void
9286 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9287 : location_t record_loc)
9288 : {
9289 575 : unsigned int ix;
9290 575 : tree x;
9291 575 : struct c_binding *b;
9292 :
9293 575 : if (fieldlist == NULL_TREE)
9294 : {
9295 9 : if (code == RECORD_TYPE)
9296 6 : warning_at (record_loc, OPT_Wc___compat,
9297 : "empty struct has size 0 in C, size 1 in C++");
9298 : else
9299 3 : warning_at (record_loc, OPT_Wc___compat,
9300 : "empty union has size 0 in C, size 1 in C++");
9301 : }
9302 :
9303 : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9304 : the current struct. We do this now at the end of the struct
9305 : because the flag is used to issue visibility warnings, and we
9306 : only want to issue those warnings if the type is referenced
9307 : outside of the struct declaration. */
9308 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9309 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9310 :
9311 : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9312 : typedefs used when declaring fields in this struct. If the name
9313 : of any of the fields is also a typedef name then the struct would
9314 : not parse in C++, because the C++ lookup rules say that the
9315 : typedef name would be looked up in the context of the struct, and
9316 : would thus be the field rather than the typedef. */
9317 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9318 67 : && fieldlist != NULL_TREE)
9319 : {
9320 : /* Use a hash_set<tree> using the name of the typedef. We can use
9321 : a hash_set<tree> because identifiers are interned. */
9322 67 : hash_set<tree> tset;
9323 :
9324 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9325 120 : tset.add (DECL_NAME (x));
9326 :
9327 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9328 : {
9329 320 : if (DECL_NAME (x) != NULL_TREE
9330 320 : && tset.contains (DECL_NAME (x)))
9331 : {
9332 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9333 : "using %qD as both field and typedef name is "
9334 : "invalid in C++", x);
9335 : /* FIXME: It would be nice to report the location where
9336 : the typedef name is used. */
9337 : }
9338 : }
9339 67 : }
9340 :
9341 : /* For each field which has a binding and which was not defined in
9342 : an enclosing struct, clear the in_struct field. */
9343 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9344 91 : b->in_struct = 0;
9345 575 : }
9346 :
9347 : /* Function to help qsort sort FIELD_DECLs by name order. */
9348 :
9349 : static int
9350 20121446 : field_decl_cmp (const void *x_p, const void *y_p)
9351 : {
9352 20121446 : const tree *const x = (const tree *) x_p;
9353 20121446 : const tree *const y = (const tree *) y_p;
9354 :
9355 20121446 : if (DECL_NAME (*x) == DECL_NAME (*y))
9356 : /* A nontype is "greater" than a type. */
9357 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9358 20121446 : if (DECL_NAME (*x) == NULL_TREE)
9359 : return -1;
9360 20121446 : if (DECL_NAME (*y) == NULL_TREE)
9361 : return 1;
9362 20121446 : if (DECL_NAME (*x) < DECL_NAME (*y))
9363 9908904 : return -1;
9364 : return 1;
9365 : }
9366 :
9367 : /* If this structure or union completes the type of any previous
9368 : variable declaration, lay it out and output its rtl. */
9369 : static void
9370 1360365 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9371 : {
9372 1360514 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9373 : {
9374 149 : tree decl = TREE_VALUE (x);
9375 149 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9376 0 : layout_array_type (TREE_TYPE (decl));
9377 149 : if (TREE_CODE (decl) != TYPE_DECL)
9378 : {
9379 149 : relayout_decl (decl);
9380 149 : if (c_dialect_objc ())
9381 0 : objc_check_decl (decl);
9382 149 : rest_of_decl_compilation (decl, toplevel, 0);
9383 : }
9384 : }
9385 1360365 : }
9386 :
9387 : /* Determine whether the FIELD_DECL X is a flexible array member according to
9388 : the following info:
9389 : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9390 : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9391 : or "[1]";
9392 : C. flag_strict_flex_arrays;
9393 : D. the attribute strict_flex_array that is attached to the field
9394 : if presenting.
9395 : Return TRUE when it's a flexible array member, FALSE otherwise. */
9396 :
9397 : static bool
9398 4325861 : is_flexible_array_member_p (bool is_last_field,
9399 : tree x)
9400 : {
9401 : /* If not the last field, return false. */
9402 4325861 : if (!is_last_field)
9403 : return false;
9404 :
9405 : /* If not an array field, return false. */
9406 1654832 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9407 : return false;
9408 :
9409 534302 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9410 534302 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9411 534302 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9412 :
9413 534302 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9414 :
9415 534302 : switch (strict_flex_array_level)
9416 : {
9417 : case 0:
9418 : /* Default, all trailing arrays are flexible array members. */
9419 : return true;
9420 77 : case 1:
9421 : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9422 77 : if (is_one_element_array)
9423 : return true;
9424 : /* FALLTHROUGH. */
9425 141 : case 2:
9426 : /* Level 2: all "[0]", and "[]" are flexible array members. */
9427 141 : if (is_zero_length_array)
9428 : return true;
9429 : /* FALLTHROUGH. */
9430 201 : case 3:
9431 : /* Level 3: Only "[]" are flexible array members. */
9432 201 : if (is_flexible_array)
9433 : return true;
9434 : break;
9435 0 : default:
9436 0 : gcc_unreachable ();
9437 : }
9438 : return false;
9439 : }
9440 :
9441 : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9442 : versions of the type and related pointer types after an aggregate type
9443 : has been finalized.
9444 : Will not update array types, pointers to array types, function
9445 : types and other derived types created while the type was still
9446 : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9447 :
9448 : static void
9449 1196299 : c_update_type_canonical (tree t)
9450 : {
9451 1196299 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9452 2419292 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9453 : {
9454 1222993 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9455 : {
9456 26694 : if (!TYPE_QUALS (x))
9457 26068 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9458 : else
9459 : {
9460 626 : tree
9461 626 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9462 626 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9463 : {
9464 205 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9465 205 : if (c == x)
9466 205 : TYPE_CANONICAL (x) = x;
9467 : else
9468 : {
9469 : /* build_qualified_type for this function unhelpfully
9470 : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9471 : chain to right after t (or created it there). Move
9472 : it right before x and process c and then x. */
9473 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9474 0 : if (l != t)
9475 : {
9476 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9477 0 : TYPE_NEXT_VARIANT (l) = c;
9478 0 : TYPE_NEXT_VARIANT (c) = x;
9479 : }
9480 0 : TYPE_CANONICAL (c) = c;
9481 0 : x = c;
9482 : }
9483 : }
9484 : else
9485 421 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9486 : }
9487 : }
9488 1196299 : else if (x != t)
9489 0 : continue;
9490 1292063 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9491 : {
9492 69070 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9493 0 : continue;
9494 69070 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9495 66971 : TYPE_CANONICAL (p)
9496 133942 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9497 : false);
9498 : else
9499 2099 : TYPE_CANONICAL (p) = p;
9500 69070 : c_update_type_canonical (p);
9501 : }
9502 : }
9503 1196299 : }
9504 :
9505 :
9506 : /* We set C_TYPE_VARIABLY_MODIFIED for derived types. We will not update
9507 : array types, pointers to array types, function types and other derived
9508 : types created while the type was still incomplete. We need to update
9509 : at least all types for which TYPE_CANONICAL will bet set, because for
9510 : those we later assume (in c_variably_modified_p) that the bit is
9511 : up-to-date. */
9512 :
9513 : static void
9514 728 : c_update_variably_modified (tree t)
9515 : {
9516 1465 : for (tree x = t; x; x = TYPE_NEXT_VARIANT (x))
9517 : {
9518 737 : C_TYPE_VARIABLY_MODIFIED (x) = 1;
9519 755 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9520 18 : c_update_variably_modified (p);
9521 : }
9522 728 : }
9523 :
9524 :
9525 : /* Verify the argument of the counted_by attribute of each field of
9526 : the containing structure, OUTMOST_STRUCT_TYPE, including its inner
9527 : anonymous struct/union, Report error and remove the corresponding
9528 : attribute when it's not. */
9529 :
9530 : static void
9531 514 : verify_counted_by_attribute (tree outmost_struct_type,
9532 : tree cur_struct_type)
9533 : {
9534 1658 : for (tree field = TYPE_FIELDS (cur_struct_type); field;
9535 1144 : field = TREE_CHAIN (field))
9536 : {
9537 1144 : if (c_flexible_array_member_type_p (TREE_TYPE (field))
9538 1144 : || TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
9539 : {
9540 384 : tree attr_counted_by = lookup_attribute ("counted_by",
9541 384 : DECL_ATTRIBUTES (field));
9542 :
9543 384 : if (!attr_counted_by)
9544 1 : continue;
9545 :
9546 : /* If there is an counted_by attribute attached to the field,
9547 : verify it. */
9548 :
9549 383 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9550 :
9551 : /* Verify the argument of the attrbute is a valid field of the
9552 : containing structure. */
9553 :
9554 383 : tree counted_by_field = lookup_field (outmost_struct_type,
9555 : fieldname);
9556 :
9557 : /* Error when the field is not found in the containing structure
9558 : and remove the corresponding counted_by attribute from the
9559 : field_decl. */
9560 383 : if (!counted_by_field)
9561 : {
9562 24 : error_at (DECL_SOURCE_LOCATION (field),
9563 : "argument %qE to the %<counted_by%> attribute"
9564 : " is not a field declaration in the same structure"
9565 : " as %qD", fieldname, field);
9566 24 : DECL_ATTRIBUTES (field)
9567 48 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field));
9568 : }
9569 : else
9570 : /* Error when the field is not with an integer type. */
9571 : {
9572 560 : while (TREE_CHAIN (counted_by_field))
9573 201 : counted_by_field = TREE_CHAIN (counted_by_field);
9574 359 : tree real_field = TREE_VALUE (counted_by_field);
9575 :
9576 359 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9577 : {
9578 6 : error_at (DECL_SOURCE_LOCATION (field),
9579 : "argument %qE to the %<counted_by%> attribute"
9580 : " is not a field declaration with an integer type",
9581 : fieldname);
9582 6 : DECL_ATTRIBUTES (field)
9583 12 : = remove_attribute ("counted_by",
9584 6 : DECL_ATTRIBUTES (field));
9585 : }
9586 : }
9587 : }
9588 1423 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
9589 851 : && (DECL_NAME (field) == NULL_TREE))
9590 188 : verify_counted_by_attribute (outmost_struct_type, TREE_TYPE (field));
9591 : }
9592 514 : }
9593 :
9594 : /* Caller should make sure the TYPE is a top-level type (i.e. not being
9595 : nested in other structure/uniona). For such type, verify its counted_by
9596 : if it is an anonymous structure/union. */
9597 :
9598 : void
9599 1349302 : verify_counted_by_for_top_anonymous_type (tree type)
9600 : {
9601 1349302 : if (!RECORD_OR_UNION_TYPE_P (type))
9602 : return;
9603 :
9604 1168788 : if (C_TYPE_FIELDS_HAS_COUNTED_BY (type)
9605 1168788 : && c_type_tag (type) == NULL_TREE)
9606 48 : verify_counted_by_attribute (type, type);
9607 : }
9608 :
9609 : /* TYPE is a struct or union that we're applying may_alias to after the body is
9610 : parsed. Fixup any POINTER_TO types. */
9611 :
9612 : static void
9613 353 : c_fixup_may_alias (tree type)
9614 : {
9615 418 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9616 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9617 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9618 353 : }
9619 :
9620 : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9621 : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9622 : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9623 : ATTRIBUTES are attributes to be applied to the structure.
9624 :
9625 : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9626 : the struct was started. */
9627 :
9628 : tree
9629 1179844 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9630 : class c_struct_parse_info *enclosing_struct_parse_info,
9631 : tree *expr)
9632 : {
9633 1179844 : tree x;
9634 1179844 : bool toplevel = file_scope == current_scope;
9635 :
9636 : /* If this type was previously laid out as a forward reference,
9637 : make sure we lay it out again. */
9638 :
9639 1179844 : TYPE_SIZE (t) = NULL_TREE;
9640 :
9641 1179844 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9642 :
9643 1179844 : if (pedantic)
9644 : {
9645 7386 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9646 : {
9647 7367 : if (DECL_NAME (x) != NULL_TREE)
9648 : break;
9649 25 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9650 : break;
9651 : }
9652 :
9653 7368 : if (x == NULL_TREE)
9654 : {
9655 19 : if (TREE_CODE (t) == UNION_TYPE)
9656 : {
9657 5 : if (fieldlist)
9658 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9659 : else
9660 3 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9661 : }
9662 : else
9663 : {
9664 14 : if (fieldlist)
9665 3 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9666 : else
9667 11 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9668 : }
9669 : }
9670 : }
9671 :
9672 : /* Install struct as DECL_CONTEXT of each field decl.
9673 : Also process specified field sizes, found in the DECL_INITIAL,
9674 : storing 0 there after the type has been changed to precision equal
9675 : to its width, rather than the precision of the specified standard
9676 : type. (Correct layout requires the original type to have been preserved
9677 : until now.) */
9678 :
9679 : bool saw_named_field = false;
9680 5505745 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9681 : {
9682 : /* Whether this field is the last field of the structure or union.
9683 : for UNION, any field is the last field of it. */
9684 4325901 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9685 4325901 : || (TREE_CODE (t) == UNION_TYPE);
9686 :
9687 4325901 : if (TREE_TYPE (x) == error_mark_node)
9688 40 : continue;
9689 :
9690 4325861 : DECL_CONTEXT (x) = t;
9691 :
9692 4325861 : tree t1 = strip_array_types (TREE_TYPE (x));
9693 : /* If any field is const, the structure type is pseudo-const. */
9694 4325861 : if (TREE_READONLY (x))
9695 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9696 : else
9697 : {
9698 : /* A field that is pseudo-const makes the structure likewise. */
9699 4261617 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9700 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9701 : }
9702 :
9703 : /* Any field that is volatile means variables of this type must be
9704 : treated in some ways as volatile. */
9705 4325861 : if (TREE_THIS_VOLATILE (x))
9706 : {
9707 475 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9708 475 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9709 : }
9710 :
9711 : /* Any field that is volatile, restrict-qualified or atomic
9712 : means the type cannot be used for a constexpr object. */
9713 4325861 : if (TYPE_QUALS (t1)
9714 4325861 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9715 1461 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9716 4324400 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9717 179 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9718 :
9719 : /* Any field of nominal variable size implies structure is too. */
9720 4325861 : if (C_DECL_VARIABLE_SIZE (x))
9721 692 : C_TYPE_VARIABLE_SIZE (t) = 1;
9722 :
9723 : /* If any field is variably modified, record this fact. */
9724 4325861 : if (c_type_variably_modified_p (TREE_TYPE (x)))
9725 771 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9726 :
9727 4325861 : if (DECL_C_BIT_FIELD (x))
9728 : {
9729 52627 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9730 52627 : DECL_SIZE (x) = bitsize_int (width);
9731 52627 : DECL_BIT_FIELD (x) = 1;
9732 : }
9733 :
9734 4325861 : if (TYPE_PACKED (t)
9735 4331414 : && (DECL_BIT_FIELD (x)
9736 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9737 4442 : DECL_PACKED (x) = 1;
9738 :
9739 : /* Detect flexible array member in an invalid context. */
9740 4325861 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9741 : {
9742 85990 : if (TREE_CODE (t) == UNION_TYPE)
9743 31 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9744 : "flexible array member in union is a GCC extension");
9745 85959 : else if (!is_last_field)
9746 : {
9747 3 : error_at (DECL_SOURCE_LOCATION (x),
9748 : "flexible array member not at end of struct");
9749 3 : TREE_TYPE (x) = error_mark_node;
9750 : }
9751 85956 : else if (!saw_named_field)
9752 35 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9753 : "flexible array member in a struct with no named "
9754 : "members is a GCC extension");
9755 85990 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9756 133 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9757 : }
9758 :
9759 4325861 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE
9760 4325861 : && lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9761 250 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9762 :
9763 : /* If the field is an anonymous structure that includes a field
9764 : with counted_by attribute, this structure should also be marked
9765 : too. */
9766 8275463 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9767 466855 : && C_TYPE_FIELDS_HAS_COUNTED_BY (TREE_TYPE (x))
9768 18 : && DECL_NAME (x) == NULL_TREE
9769 4325874 : && c_type_tag (TREE_TYPE (x)) == NULL_TREE)
9770 12 : C_TYPE_FIELDS_HAS_COUNTED_BY (t) = 1;
9771 :
9772 25344 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9773 4346942 : && flexible_array_type_p (TREE_TYPE (x)))
9774 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9775 : "invalid use of structure with flexible array member");
9776 :
9777 : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9778 4325861 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9779 :
9780 : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9781 : when x is an array and is the last field.
9782 : There is only one last_field for a structure type, but there might
9783 : be multiple last_fields for a union type, therefore we should ORed
9784 : the result for multiple last_fields. */
9785 4325861 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9786 707234 : TYPE_INCLUDES_FLEXARRAY (t)
9787 1414468 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9788 : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9789 : when x is an union or record and is the last field. */
9790 3618627 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9791 466855 : TYPE_INCLUDES_FLEXARRAY (t)
9792 933710 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9793 :
9794 4325861 : if (warn_flex_array_member_not_at_end
9795 56 : && !is_last_field
9796 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9797 4325873 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9798 5 : warning_at (DECL_SOURCE_LOCATION (x),
9799 5 : OPT_Wflex_array_member_not_at_end,
9800 : "structure containing a flexible array member"
9801 : " is not at the end of another structure");
9802 :
9803 4325861 : if (DECL_NAME (x)
9804 4325861 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9805 : saw_named_field = true;
9806 :
9807 7944488 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9808 4792716 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9809 449889 : TYPE_TYPELESS_STORAGE (t) = true;
9810 : }
9811 :
9812 1179844 : detect_field_duplicates (fieldlist);
9813 :
9814 : /* Now we have the nearly final fieldlist. Record it,
9815 : then lay out the structure or union (including the fields). */
9816 :
9817 1179844 : TYPE_FIELDS (t) = fieldlist;
9818 :
9819 1179844 : maybe_apply_pragma_scalar_storage_order (t);
9820 :
9821 1179844 : layout_type (t);
9822 :
9823 1179844 : if (TYPE_SIZE_UNIT (t)
9824 1179844 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9825 1179207 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9826 2359051 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9827 1 : error ("type %qT is too large", t);
9828 :
9829 : /* Give bit-fields their proper types and rewrite the type of array fields
9830 : with scalar component if the enclosing type has reverse storage order. */
9831 5505745 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9832 : {
9833 4325901 : if (TREE_CODE (field) == FIELD_DECL
9834 4325901 : && DECL_INITIAL (field)
9835 4378538 : && TREE_TYPE (field) != error_mark_node)
9836 : {
9837 52627 : unsigned HOST_WIDE_INT width
9838 52627 : = tree_to_uhwi (DECL_INITIAL (field));
9839 52627 : tree type = TREE_TYPE (field);
9840 52627 : if (VECTOR_TYPE_P (type))
9841 : {
9842 2 : error_at (DECL_SOURCE_LOCATION (field),
9843 : "bit-field %qD has invalid type", field);
9844 2 : type = TREE_TYPE (type);
9845 2 : TREE_TYPE (field) = type;
9846 : }
9847 52627 : if (width != TYPE_PRECISION (type))
9848 : {
9849 37905 : if (TREE_CODE (type) == BITINT_TYPE
9850 37975 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9851 161 : TREE_TYPE (field)
9852 322 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9853 : else
9854 37744 : TREE_TYPE (field)
9855 37744 : = c_build_bitfield_integer_type (width,
9856 37744 : TYPE_UNSIGNED (type));
9857 37905 : if (tree attr = c_hardbool_type_attr (type))
9858 281 : decl_attributes (&TREE_TYPE (field),
9859 : copy_list (attr),
9860 : 0, NULL_TREE);
9861 37905 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9862 : }
9863 52627 : DECL_INITIAL (field) = NULL_TREE;
9864 : }
9865 4273274 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9866 693 : && TREE_CODE (field) == FIELD_DECL
9867 4273967 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9868 : {
9869 106 : tree ftype = TREE_TYPE (field);
9870 106 : tree ctype = strip_array_types (ftype);
9871 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9872 : {
9873 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9874 93 : tree *typep = &fmain_type;
9875 95 : do {
9876 95 : *typep = build_distinct_type_copy (*typep);
9877 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9878 95 : typep = &TREE_TYPE (*typep);
9879 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9880 93 : TREE_TYPE (field)
9881 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9882 : }
9883 : }
9884 :
9885 : /* Warn on problematic type punning for storage order purposes. */
9886 4325901 : if (TREE_CODE (t) == UNION_TYPE
9887 854922 : && TREE_CODE (field) == FIELD_DECL
9888 5180823 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9889 : {
9890 424687 : tree ftype = TREE_TYPE (field);
9891 424687 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9892 312844 : ftype = strip_array_types (ftype);
9893 424687 : if (RECORD_OR_UNION_TYPE_P (ftype)
9894 424687 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9895 112337 : != TYPE_REVERSE_STORAGE_ORDER (t))
9896 1 : warning_at (DECL_SOURCE_LOCATION (field),
9897 1 : OPT_Wscalar_storage_order,
9898 : "type punning toggles scalar storage order");
9899 : }
9900 : }
9901 :
9902 : /* Now we have the truly final field list.
9903 : Store it in this type and in the variants. */
9904 :
9905 1179844 : TYPE_FIELDS (t) = fieldlist;
9906 :
9907 : /* If there are lots of fields, sort so we can look through them fast.
9908 : We arbitrarily consider 16 or more elts to be "a lot". */
9909 :
9910 1179844 : {
9911 1179844 : int len = 0;
9912 :
9913 5063782 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9914 : {
9915 3911826 : if (len > 15 || DECL_NAME (x) == NULL)
9916 : break;
9917 3883938 : len += 1;
9918 : }
9919 :
9920 1179844 : if (len > 15)
9921 : {
9922 22650 : tree *field_array;
9923 22650 : struct lang_type *space;
9924 22650 : struct sorted_fields_type *space2;
9925 :
9926 22650 : len += list_length (x);
9927 :
9928 : /* Use the same allocation policy here that make_node uses, to
9929 : ensure that this lives as long as the rest of the struct decl.
9930 : All decls in an inline function need to be saved. */
9931 :
9932 22650 : space = ((struct lang_type *)
9933 22650 : ggc_internal_cleared_alloc (c_dialect_objc ()
9934 : ? sizeof (struct lang_type)
9935 : : offsetof (struct lang_type,
9936 : info)));
9937 22650 : space2 = ((sorted_fields_type *)
9938 45300 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9939 22650 : + len * sizeof (tree)));
9940 :
9941 22650 : len = 0;
9942 22650 : space->s = space2;
9943 22650 : field_array = &space2->elts[0];
9944 792132 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9945 : {
9946 772854 : field_array[len++] = x;
9947 :
9948 : /* If there is anonymous struct or union, break out of the loop. */
9949 772854 : if (DECL_NAME (x) == NULL)
9950 : break;
9951 : }
9952 : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9953 22650 : if (x == NULL)
9954 : {
9955 19278 : TYPE_LANG_SPECIFIC (t) = space;
9956 19278 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9957 19278 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9958 19278 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9959 : }
9960 : }
9961 : }
9962 :
9963 : /* If this was supposed to be a transparent union, but we can't
9964 : make it one, warn and turn off the flag. */
9965 1179844 : if (TREE_CODE (t) == UNION_TYPE
9966 377708 : && TYPE_TRANSPARENT_AGGR (t)
9967 1179887 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9968 : {
9969 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9970 13 : warning_at (loc, 0, "union cannot be made transparent");
9971 : }
9972 :
9973 : /* Check for consistency with previous definition. */
9974 1179844 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9975 : {
9976 911138 : tree vistype = previous_tag (t);
9977 911138 : if (vistype
9978 127 : && TREE_CODE (vistype) == TREE_CODE (t)
9979 911265 : && !C_TYPE_BEING_DEFINED (vistype))
9980 : {
9981 107 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9982 107 : if (c_type_variably_modified_p (t))
9983 : {
9984 7 : error ("redefinition of struct or union %qT with variably "
9985 : "modified type", t);
9986 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9987 7 : inform (struct_parse_info->refloc, "originally defined here");
9988 : }
9989 100 : else if (!comptypes_same_p (t, vistype))
9990 : {
9991 38 : error ("redefinition of struct or union %qT", t);
9992 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9993 38 : inform (struct_parse_info->refloc, "originally defined here");
9994 : }
9995 : }
9996 : }
9997 :
9998 1179844 : C_TYPE_BEING_DEFINED (t) = 0;
9999 :
10000 1179844 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
10001 674 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
10002 353 : c_fixup_may_alias (x);
10003 :
10004 : /* Set type canonical based on equivalence class. */
10005 1179844 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
10006 : {
10007 946815 : if (c_struct_htab == NULL)
10008 35907 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
10009 :
10010 946815 : hashval_t hash = c_struct_hasher::hash (t);
10011 :
10012 946815 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
10013 946815 : gcc_checking_assert (!TYPE_NAME (t)
10014 : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
10015 :
10016 946815 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
10017 946815 : if (*e)
10018 62932 : TYPE_CANONICAL (t) = TYPE_CANONICAL (*e);
10019 : else
10020 : {
10021 883883 : TYPE_CANONICAL (t) = c_type_canonical (t);
10022 883883 : *e = t;
10023 : }
10024 946815 : c_update_type_canonical (t);
10025 : }
10026 :
10027 1179844 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
10028 2390433 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
10029 : {
10030 1210589 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
10031 1210589 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
10032 1210589 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
10033 1210589 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
10034 1210589 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
10035 1210589 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
10036 1210589 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
10037 1210589 : C_TYPE_FIELDS_HAS_COUNTED_BY (x) = C_TYPE_FIELDS_HAS_COUNTED_BY (t);
10038 1210589 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
10039 1210589 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
10040 1210589 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
10041 1210589 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
10042 : }
10043 :
10044 : /* Update type location to the one of the definition, instead of e.g.
10045 : a forward declaration. */
10046 1179844 : if (TYPE_STUB_DECL (t))
10047 1179844 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
10048 :
10049 : /* Finish debugging output for this type. */
10050 1179844 : rest_of_type_compilation (t, toplevel);
10051 :
10052 1179844 : finish_incomplete_vars (incomplete_vars, toplevel);
10053 :
10054 :
10055 1179844 : if (c_type_variably_modified_p (t))
10056 : {
10057 710 : c_update_variably_modified (t);
10058 : /* Make sure a DECL_EXPR is created for structs with VLA members.
10059 : Because we do not know the context, we always pass expr
10060 : to force creation of a BIND_EXPR which is required in some
10061 : contexts. */
10062 710 : add_decl_expr (loc, t, expr, false);
10063 : }
10064 :
10065 1179844 : if (warn_cxx_compat)
10066 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
10067 :
10068 1179844 : if (NULL != enclosing_struct_parse_info)
10069 : {
10070 1138460 : delete struct_parse_info;
10071 :
10072 1138460 : struct_parse_info = enclosing_struct_parse_info;
10073 :
10074 : /* If this struct is defined inside a struct, add it to
10075 : struct_types. */
10076 1138460 : if (warn_cxx_compat
10077 : && struct_parse_info != NULL
10078 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10079 345 : struct_parse_info->struct_types.safe_push (t);
10080 : }
10081 :
10082 : /* Only when the enclosing struct/union type is not anonymous, do more
10083 : verification on the fields with counted_by attributes. */
10084 1179844 : if (C_TYPE_FIELDS_HAS_COUNTED_BY (t) && c_type_tag (t) != NULL_TREE)
10085 278 : verify_counted_by_attribute (t, t);
10086 :
10087 1179844 : return t;
10088 : }
10089 :
10090 : static struct {
10091 : gt_pointer_operator new_value;
10092 : void *cookie;
10093 : } resort_data;
10094 :
10095 : /* This routine compares two fields like field_decl_cmp but using the
10096 : pointer operator in resort_data. */
10097 :
10098 : static int
10099 5372 : resort_field_decl_cmp (const void *x_p, const void *y_p)
10100 : {
10101 5372 : const tree *const x = (const tree *) x_p;
10102 5372 : const tree *const y = (const tree *) y_p;
10103 :
10104 5372 : if (DECL_NAME (*x) == DECL_NAME (*y))
10105 : /* A nontype is "greater" than a type. */
10106 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10107 5372 : if (DECL_NAME (*x) == NULL_TREE)
10108 : return -1;
10109 5372 : if (DECL_NAME (*y) == NULL_TREE)
10110 : return 1;
10111 5372 : {
10112 5372 : tree d1 = DECL_NAME (*x);
10113 5372 : tree d2 = DECL_NAME (*y);
10114 5372 : resort_data.new_value (&d1, &d1, resort_data.cookie);
10115 5372 : resort_data.new_value (&d2, &d2, resort_data.cookie);
10116 5372 : if (d1 < d2)
10117 2683 : return -1;
10118 : }
10119 2689 : return 1;
10120 : }
10121 :
10122 : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10123 :
10124 : void
10125 6 : resort_sorted_fields (void *obj,
10126 : void * ARG_UNUSED (orig_obj),
10127 : gt_pointer_operator new_value,
10128 : void *cookie)
10129 : {
10130 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10131 6 : resort_data.new_value = new_value;
10132 6 : resort_data.cookie = cookie;
10133 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
10134 : resort_field_decl_cmp);
10135 6 : }
10136 :
10137 : /* Lay out the type T, and its element type, and so on. */
10138 :
10139 : static void
10140 0 : layout_array_type (tree t)
10141 : {
10142 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10143 0 : layout_array_type (TREE_TYPE (t));
10144 0 : layout_type (t);
10145 0 : }
10146 :
10147 : /* Begin compiling the definition of an enumeration type.
10148 : NAME is its name (or null if anonymous).
10149 : LOC is the enum's location.
10150 : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10151 : definition.
10152 : Returns the type object, as yet incomplete.
10153 : Also records info about it so that build_enumerator
10154 : may be used to declare the individual values as they are read. */
10155 :
10156 : tree
10157 180521 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10158 : tree fixed_underlying_type, bool potential_nesting_p)
10159 : {
10160 180521 : tree enumtype = NULL_TREE;
10161 180521 : location_t enumloc = UNKNOWN_LOCATION;
10162 :
10163 : /* If this is the real definition for a previous forward reference,
10164 : fill in the contents in the same object that used to be the
10165 : forward reference. */
10166 :
10167 180521 : if (name != NULL_TREE)
10168 71733 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10169 :
10170 71733 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10171 : {
10172 : /* If the type is currently being defined or if we have seen an
10173 : incomplete version which is now complete, this is a nested
10174 : redefinition. The later happens if the redefinition occurs
10175 : inside the enum specifier itself. */
10176 178 : if (C_TYPE_BEING_DEFINED (enumtype)
10177 178 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10178 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
10179 :
10180 : /* For C23 we allow redefinitions. We set to zero and check for
10181 : consistency later. */
10182 339 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10183 : enumtype = NULL_TREE;
10184 : }
10185 :
10186 108940 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10187 : {
10188 180371 : enumtype = make_node (ENUMERAL_TYPE);
10189 180371 : TYPE_SIZE (enumtype) = NULL_TREE;
10190 180371 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10191 180371 : pushtag (loc, name, enumtype);
10192 180371 : if (fixed_underlying_type != NULL_TREE)
10193 : {
10194 : /* For an enum definition with a fixed underlying type, the
10195 : type is complete during the definition and the
10196 : enumeration constants have that type. If there was a
10197 : tag, the type was completed in c_parser_enum_specifier.
10198 : If not, it must be completed here. */
10199 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10200 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10201 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10202 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10203 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10204 12 : TYPE_SIZE (enumtype) = NULL_TREE;
10205 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10206 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10207 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10208 12 : c_update_type_canonical (enumtype);
10209 12 : layout_type (enumtype);
10210 : }
10211 : }
10212 : /* Update type location to the one of the definition, instead of e.g.
10213 : a forward declaration. */
10214 150 : else if (TYPE_STUB_DECL (enumtype))
10215 : {
10216 150 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10217 150 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10218 : }
10219 :
10220 180521 : C_TYPE_BEING_DEFINED (enumtype) = 1;
10221 :
10222 180521 : if (TYPE_VALUES (enumtype) != NULL_TREE)
10223 : {
10224 : /* This enum is a named one that has been declared already. */
10225 6 : auto_diagnostic_group d;
10226 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
10227 6 : if (enumloc != UNKNOWN_LOCATION)
10228 6 : inform (enumloc, "originally defined here");
10229 :
10230 : /* Completely replace its old definition.
10231 : The old enumerators remain defined, however. */
10232 6 : TYPE_VALUES (enumtype) = NULL_TREE;
10233 6 : }
10234 :
10235 180521 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10236 180521 : && fixed_underlying_type == NULL_TREE)
10237 : {
10238 3 : error_at (loc, "%<enum%> declared with but defined without "
10239 : "fixed underlying type");
10240 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10241 : }
10242 :
10243 180521 : the_enum->enum_next_value = integer_zero_node;
10244 180521 : the_enum->enum_type = enumtype;
10245 180521 : the_enum->enum_overflow = 0;
10246 :
10247 180521 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10248 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10249 53 : TYPE_PACKED (v) = 1;
10250 :
10251 : /* FIXME: This will issue a warning for a use of a type defined
10252 : within sizeof in a statement expr. This is not terribly serious
10253 : as C++ doesn't permit statement exprs within sizeof anyhow. */
10254 180521 : if (warn_cxx_compat
10255 127 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10256 0 : warning_at (loc, OPT_Wc___compat,
10257 : "defining type in %qs expression is invalid in C++",
10258 : (in_sizeof ? "sizeof"
10259 0 : : in_typeof ? "typeof"
10260 0 : : in_alignof ? "alignof"
10261 : : "_Countof"));
10262 :
10263 180521 : if (in_underspecified_init)
10264 6 : error_at (loc, "%qT defined in underspecified object initializer",
10265 : enumtype);
10266 :
10267 180521 : return enumtype;
10268 : }
10269 :
10270 : /* After processing and defining all the values of an enumeration type,
10271 : install their decls in the enumeration type and finish it off.
10272 : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10273 : and ATTRIBUTES are the specified attributes.
10274 : Returns ENUMTYPE. */
10275 :
10276 : tree
10277 180521 : finish_enum (tree enumtype, tree values, tree attributes)
10278 : {
10279 180521 : tree pair, tem;
10280 180521 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10281 180521 : int precision;
10282 180521 : signop sign;
10283 180521 : bool toplevel = (file_scope == current_scope);
10284 180521 : struct lang_type *lt;
10285 :
10286 180521 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10287 :
10288 : /* Calculate the maximum value of any enumerator in this type. */
10289 :
10290 180521 : if (values == error_mark_node)
10291 26 : minnode = maxnode = integer_zero_node;
10292 : else
10293 : {
10294 180495 : minnode = maxnode = TREE_VALUE (values);
10295 5716672 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10296 : {
10297 5536177 : tree value = TREE_VALUE (pair);
10298 5536177 : if (tree_int_cst_lt (maxnode, value))
10299 5387150 : maxnode = value;
10300 5536177 : if (tree_int_cst_lt (value, minnode))
10301 65144 : minnode = value;
10302 : }
10303 : }
10304 :
10305 : /* Construct the final type of this enumeration. It is the same
10306 : as one of the integral types - the narrowest one that fits, except
10307 : that normally we only go as narrow as int - and signed iff any of
10308 : the values are negative. */
10309 180521 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10310 180521 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10311 : tree_int_cst_min_precision (maxnode, sign));
10312 :
10313 180521 : bool wider_than_int =
10314 180521 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10315 180521 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10316 :
10317 :
10318 180521 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10319 : {
10320 : /* If the precision of the type was specified with an attribute and it
10321 : was too small, give an error. Otherwise, use it. */
10322 180402 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10323 : {
10324 23 : if (precision > TYPE_PRECISION (enumtype))
10325 : {
10326 6 : TYPE_PRECISION (enumtype) = 0;
10327 6 : error ("specified mode too small for enumerated values");
10328 : }
10329 : else
10330 17 : precision = TYPE_PRECISION (enumtype);
10331 : }
10332 : else
10333 180379 : TYPE_PRECISION (enumtype) = 0;
10334 :
10335 180402 : if (TYPE_PACKED (enumtype)
10336 180323 : || precision > TYPE_PRECISION (integer_type_node)
10337 357711 : || TYPE_PRECISION (enumtype))
10338 : {
10339 3220 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10340 3105 : if (tem == NULL)
10341 : {
10342 : /* This should only occur when both signed and unsigned
10343 : values of maximum precision occur among the
10344 : enumerators. */
10345 3 : pedwarn (input_location, 0,
10346 : "enumeration values exceed range of largest integer");
10347 3 : tem = widest_integer_literal_type_node;
10348 : }
10349 3102 : else if (precision > TYPE_PRECISION (intmax_type_node)
10350 4 : && !tree_int_cst_lt (minnode,
10351 4 : TYPE_MIN_VALUE (intmax_type_node))
10352 3105 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10353 : maxnode))
10354 2 : pedwarn (input_location, OPT_Wpedantic,
10355 : "enumeration values exceed range of %qs",
10356 : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10357 : }
10358 : else
10359 177297 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10360 :
10361 180402 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10362 180402 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10363 180402 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10364 180402 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10365 180402 : TYPE_SIZE (enumtype) = NULL_TREE;
10366 180402 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10367 180402 : ENUM_UNDERLYING_TYPE (enumtype) =
10368 180402 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10369 :
10370 541206 : TYPE_CANONICAL (enumtype) =
10371 180402 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10372 180402 : c_update_type_canonical (enumtype);
10373 :
10374 180402 : layout_type (enumtype);
10375 : }
10376 :
10377 180521 : if (values != error_mark_node)
10378 : {
10379 : /* Change the type of the enumerators to be the enum type. We
10380 : need to do this irrespective of the size of the enum, for
10381 : proper type checking. Replace the DECL_INITIALs of the
10382 : enumerators, and the value slots of the list, with copies
10383 : that have the enum type; they cannot be modified in place
10384 : because they may be shared (e.g. integer_zero_node) Finally,
10385 : change the purpose slots to point to the names of the decls. */
10386 5897167 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10387 : {
10388 5716672 : tree enu = TREE_PURPOSE (pair);
10389 5716672 : tree ini = DECL_INITIAL (enu);
10390 :
10391 5716672 : TREE_TYPE (enu) = enumtype;
10392 :
10393 : /* Before C23, the ISO C Standard mandates enumerators to
10394 : have type int, even though the underlying type of an enum
10395 : type is unspecified. However, C23 allows enumerators of
10396 : any integer type, and if an enumeration has any
10397 : enumerators wider than int, all enumerators have the
10398 : enumerated type after it is parsed. Any enumerators that
10399 : fit in int are given type int in build_enumerator (which
10400 : is the correct type while the enumeration is being
10401 : parsed), so no conversions are needed here if all
10402 : enumerators fit in int. If the enum has a fixed
10403 : underlying type, the correct type was also given in
10404 : build_enumerator. */
10405 5716672 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10406 33875 : ini = convert (enumtype, ini);
10407 :
10408 5716672 : DECL_INITIAL (enu) = ini;
10409 5716672 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10410 : /* To match the C++ FE, store the CONST_DECL rather than just its
10411 : value. */
10412 5716672 : TREE_VALUE (pair) = enu;
10413 : }
10414 :
10415 180495 : TYPE_VALUES (enumtype) = values;
10416 : }
10417 :
10418 : /* Record the min/max values so that we can warn about bit-field
10419 : enumerations that are too small for the values. */
10420 180521 : lt = ((struct lang_type *)
10421 180521 : ggc_internal_cleared_alloc (c_dialect_objc ()
10422 : ? sizeof (struct lang_type)
10423 : : offsetof (struct lang_type, info)));
10424 180521 : lt->enum_min = minnode;
10425 180521 : lt->enum_max = maxnode;
10426 180521 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10427 :
10428 : /* Fix up all variant types of this enum type. */
10429 180521 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10430 361052 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10431 : {
10432 180531 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10433 180531 : if (tem == enumtype)
10434 180521 : continue;
10435 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10436 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10437 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10438 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10439 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10440 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10441 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10442 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10443 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10444 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10445 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10446 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10447 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10448 : }
10449 :
10450 : /* Finish debugging output for this type. */
10451 180521 : rest_of_type_compilation (enumtype, toplevel);
10452 :
10453 180521 : finish_incomplete_vars (incomplete_vars, toplevel);
10454 :
10455 : /* If this enum is defined inside a struct, add it to
10456 : struct_types. */
10457 180521 : if (warn_cxx_compat
10458 127 : && struct_parse_info != NULL
10459 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10460 42 : struct_parse_info->struct_types.safe_push (enumtype);
10461 :
10462 : /* Check for consistency with previous definition */
10463 180521 : if (flag_isoc23)
10464 : {
10465 142415 : tree vistype = previous_tag (enumtype);
10466 142415 : if (vistype
10467 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10468 142444 : && !C_TYPE_BEING_DEFINED (vistype))
10469 : {
10470 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10471 29 : if (!comptypes_same_p (enumtype, vistype))
10472 3 : error("conflicting redefinition of enum %qT", enumtype);
10473 : }
10474 : }
10475 :
10476 180521 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10477 :
10478 180521 : return enumtype;
10479 : }
10480 :
10481 : /* Build and install a CONST_DECL for one value of the
10482 : current enumeration type (one that was begun with start_enum).
10483 : DECL_LOC is the location of the enumerator.
10484 : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10485 : Return a tree-list containing the CONST_DECL and its value.
10486 : Assignment of sequential values by default is handled here. */
10487 :
10488 : tree
10489 5716689 : build_enumerator (location_t decl_loc, location_t loc,
10490 : struct c_enum_contents *the_enum, tree name, tree value)
10491 : {
10492 5716689 : tree decl;
10493 5716689 : tree old_decl;
10494 :
10495 : /* Validate and default VALUE. */
10496 :
10497 5716689 : if (value != NULL_TREE)
10498 : {
10499 : /* Don't issue more errors for error_mark_node (i.e. an
10500 : undeclared identifier) - just ignore the value expression. */
10501 3553417 : if (value == error_mark_node)
10502 : value = NULL_TREE;
10503 3552752 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10504 : {
10505 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10506 : name);
10507 6 : value = NULL_TREE;
10508 : }
10509 : else
10510 : {
10511 3552746 : if (TREE_CODE (value) != INTEGER_CST)
10512 : {
10513 97 : value = c_fully_fold (value, false, NULL);
10514 97 : if (TREE_CODE (value) == INTEGER_CST)
10515 48 : pedwarn (loc, OPT_Wpedantic,
10516 : "enumerator value for %qE is not an integer "
10517 : "constant expression", name);
10518 : }
10519 3552746 : if (TREE_CODE (value) != INTEGER_CST)
10520 : {
10521 49 : error ("enumerator value for %qE is not an integer constant",
10522 : name);
10523 49 : value = NULL_TREE;
10524 : }
10525 : else
10526 : {
10527 3552697 : value = default_conversion (value);
10528 3552697 : constant_expression_warning (value);
10529 : }
10530 : }
10531 : }
10532 :
10533 : /* Default based on previous value. */
10534 : /* It should no longer be possible to have NON_LVALUE_EXPR
10535 : in the default. */
10536 3552752 : if (value == NULL_TREE)
10537 : {
10538 2163992 : value = the_enum->enum_next_value;
10539 2163992 : if (the_enum->enum_overflow)
10540 8 : error_at (loc, "overflow in enumeration values");
10541 : }
10542 5716689 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10543 : {
10544 : /* Enumeration constants must fit in the fixed underlying type. */
10545 164 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10546 4 : error_at (loc,
10547 : "enumerator value outside the range of underlying type");
10548 : /* Enumeration constants for an enum with fixed underlying type
10549 : have the enum type, both inside and outside the
10550 : definition. */
10551 164 : value = convert (the_enum->enum_type, value);
10552 : }
10553 5716525 : else if (flag_isoc23
10554 5299988 : && (old_decl = lookup_name_in_scope (name, current_scope))
10555 35 : && old_decl != error_mark_node
10556 35 : && TREE_TYPE (old_decl)
10557 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10558 5716559 : && TREE_CODE (old_decl) == CONST_DECL)
10559 : {
10560 : /* Enumeration constants in a redeclaration have the previous type. */
10561 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10562 34 : if (!int_fits_type_p (value, previous_type))
10563 : {
10564 2 : error_at (loc, "value of redeclared enumerator outside the range "
10565 : "of %qT", previous_type);
10566 2 : locate_old_decl (old_decl);
10567 : }
10568 34 : value = convert (previous_type, value);
10569 : }
10570 : else
10571 : {
10572 : /* Even though the underlying type of an enum is unspecified, the
10573 : type of enumeration constants is explicitly defined as int
10574 : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10575 : GCC allows such types for older standards as an extension. */
10576 5716491 : bool warned_range = false;
10577 5716491 : if (!int_fits_type_p (value,
10578 5716491 : (TYPE_UNSIGNED (TREE_TYPE (value))
10579 : ? uintmax_type_node
10580 : : intmax_type_node)))
10581 : /* GCC does not consider its types larger than intmax_t to be
10582 : extended integer types (although C23 would permit such types to
10583 : be considered extended integer types if all the features
10584 : required by <stdint.h> and <inttypes.h> macros, such as support
10585 : for integer constants and I/O, were present), so diagnose if
10586 : such a wider type is used. (If the wider type arose from a
10587 : constant of such a type, that will also have been diagnosed,
10588 : but this is the only diagnostic in the case where it arises
10589 : from choosing a wider type automatically when adding 1
10590 : overflows.) */
10591 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10592 : "enumerator value outside the range of %qs",
10593 11 : TYPE_UNSIGNED (TREE_TYPE (value))
10594 : ? "uintmax_t"
10595 : : "intmax_t");
10596 5716491 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10597 5200 : pedwarn_c11 (loc, OPT_Wpedantic,
10598 : "ISO C restricts enumerator values to range of %<int%> "
10599 : "before C23");
10600 :
10601 : /* The ISO C Standard mandates enumerators to have type int before
10602 : C23, even though the underlying type of an enum type is
10603 : unspecified. C23 allows enumerators of any integer type. During
10604 : the parsing of the enumeration, C23 specifies that constants
10605 : representable in int have type int, constants not representable
10606 : in int have the type of the given expression if any, and
10607 : constants not representable in int and derived by adding 1 to the
10608 : previous constant have the type of that constant unless the
10609 : addition would overflow or wraparound, in which case a wider type
10610 : of the same signedness is chosen automatically; after the
10611 : enumeration is parsed, all the constants have the type of the
10612 : enumeration if any do not fit in int. */
10613 5716491 : if (int_fits_type_p (value, integer_type_node))
10614 5711287 : value = convert (integer_type_node, value);
10615 : }
10616 :
10617 : /* Set basis for default for next value. */
10618 5716689 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10619 : {
10620 164 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10621 164 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10622 : /* A value of 2 following a value of 1 overflows bool, but we
10623 : cannot carry out addition directly on bool without
10624 : promotion, and converting the result of arithmetic in a
10625 : wider type back to bool would not produce the right result
10626 : for this overflow check. */
10627 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10628 : else
10629 103 : the_enum->enum_next_value
10630 103 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10631 : PLUS_EXPR, convert (underlying_type, value),
10632 : convert (underlying_type, integer_one_node),
10633 : false);
10634 : }
10635 : else
10636 : {
10637 : /* In a redeclaration the type can already be the enumeral type. */
10638 5716525 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10639 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10640 5716525 : the_enum->enum_next_value
10641 5716525 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10642 : PLUS_EXPR, value, integer_one_node, false);
10643 : }
10644 5716689 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10645 5716689 : if (the_enum->enum_overflow
10646 5716689 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10647 : {
10648 : /* Choose a wider type with the same signedness if
10649 : available. */
10650 3736 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10651 3736 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10652 3736 : tree new_type = (unsignedp
10653 3736 : ? long_unsigned_type_node
10654 : : long_integer_type_node);
10655 3736 : if (prec > TYPE_PRECISION (new_type))
10656 3369 : new_type = (unsignedp
10657 3369 : ? long_long_unsigned_type_node
10658 : : long_long_integer_type_node);
10659 3736 : if (prec > TYPE_PRECISION (new_type))
10660 2897 : new_type = (unsignedp
10661 2897 : ? widest_unsigned_literal_type_node
10662 : : widest_integer_literal_type_node);
10663 3736 : if (prec <= TYPE_PRECISION (new_type))
10664 : {
10665 3731 : the_enum->enum_overflow = false;
10666 3731 : the_enum->enum_next_value
10667 3731 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10668 : PLUS_EXPR, convert (new_type, value),
10669 : integer_one_node, false);
10670 3731 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10671 : }
10672 : }
10673 :
10674 : /* Now create a declaration for the enum value name. */
10675 :
10676 5716689 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10677 5716689 : DECL_INITIAL (decl) = value;
10678 5716689 : DECL_CONTEXT (decl) = the_enum->enum_type;
10679 5716689 : pushdecl (decl);
10680 :
10681 5716689 : return tree_cons (decl, value, NULL_TREE);
10682 : }
10683 :
10684 : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10685 :
10686 : tree
10687 0 : c_simulate_enum_decl (location_t loc, const char *name,
10688 : vec<string_int_pair> *values_ptr)
10689 : {
10690 0 : location_t saved_loc = input_location;
10691 0 : input_location = loc;
10692 :
10693 0 : struct c_enum_contents the_enum;
10694 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10695 : NULL_TREE, false);
10696 :
10697 0 : tree value_chain = NULL_TREE;
10698 0 : string_int_pair *value;
10699 0 : vec<string_int_pair> values = *values_ptr;
10700 0 : unsigned int i;
10701 0 : FOR_EACH_VEC_ELT (values, i, value)
10702 : {
10703 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10704 : get_identifier (value->first),
10705 : build_int_cst (integer_type_node,
10706 0 : value->second));
10707 0 : TREE_CHAIN (decl) = value_chain;
10708 0 : value_chain = decl;
10709 : }
10710 :
10711 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10712 :
10713 0 : input_location = saved_loc;
10714 0 : return enumtype;
10715 : }
10716 :
10717 : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10718 :
10719 : tree
10720 0 : c_simulate_record_decl (location_t loc, const char *name,
10721 : array_slice<const tree> fields)
10722 : {
10723 0 : location_t saved_loc = input_location;
10724 0 : input_location = loc;
10725 :
10726 0 : class c_struct_parse_info *struct_info;
10727 0 : tree ident = get_identifier (name);
10728 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10729 :
10730 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10731 : {
10732 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10733 0 : if (i > 0)
10734 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10735 : }
10736 :
10737 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10738 :
10739 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10740 0 : set_underlying_type (decl);
10741 0 : lang_hooks.decls.pushdecl (decl);
10742 :
10743 0 : input_location = saved_loc;
10744 0 : return type;
10745 : }
10746 :
10747 : /* Create the FUNCTION_DECL for a function definition.
10748 : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10749 : the declaration; they describe the function's name and the type it returns,
10750 : but twisted together in a fashion that parallels the syntax of C.
10751 :
10752 : This function creates a binding context for the function body
10753 : as well as setting up the FUNCTION_DECL in current_function_decl.
10754 :
10755 : Returns true on success. If the DECLARATOR is not suitable for a function
10756 : (it defines a datum instead), we return false to report a parse error. */
10757 :
10758 : bool
10759 36301471 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10760 : tree attributes)
10761 : {
10762 36301471 : tree decl1, old_decl;
10763 36301471 : tree restype, resdecl;
10764 36301471 : location_t loc;
10765 36301471 : location_t result_loc;
10766 36301471 : tree expr = NULL;
10767 :
10768 36301471 : current_function_returns_value = 0; /* Assume, until we see it does. */
10769 36301471 : current_function_returns_null = 0;
10770 36301471 : current_function_returns_abnormally = 0;
10771 36301471 : warn_about_return_type = 0;
10772 36301471 : c_switch_stack = NULL;
10773 :
10774 : /* Indicate no valid break/continue context. */
10775 36301471 : in_statement = 0;
10776 :
10777 36301471 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10778 : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10779 36301471 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10780 :
10781 : /* If the declarator is not suitable for a function definition,
10782 : cause a syntax error. */
10783 36301471 : if (decl1 == NULL_TREE
10784 36301440 : || TREE_CODE (decl1) != FUNCTION_DECL)
10785 : return false;
10786 :
10787 : /* Nested functions may have variably modified (return) type.
10788 : Make sure the size expression is evaluated at this point. */
10789 36301433 : if (expr && !current_scope->parm_flag)
10790 11 : add_stmt (fold_convert (void_type_node, expr));
10791 :
10792 36301433 : loc = DECL_SOURCE_LOCATION (decl1);
10793 :
10794 : /* A nested function is not global. */
10795 36301433 : if (current_function_decl != NULL_TREE)
10796 1588 : TREE_PUBLIC (decl1) = 0;
10797 :
10798 36301433 : c_decl_attributes (&decl1, attributes, 0);
10799 :
10800 36301433 : if (DECL_DECLARED_INLINE_P (decl1)
10801 35611543 : && DECL_UNINLINABLE (decl1)
10802 36301444 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10803 : {
10804 3 : auto_urlify_attributes sentinel;
10805 3 : warning_at (loc, OPT_Wattributes,
10806 : "inline function %qD given attribute %qs",
10807 : decl1, "noinline");
10808 3 : }
10809 :
10810 : /* Handle gnu_inline attribute. */
10811 36301433 : if (declspecs->inline_p
10812 35611546 : && !flag_gnu89_inline
10813 35577449 : && TREE_CODE (decl1) == FUNCTION_DECL
10814 71878882 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10815 158348 : || current_function_decl))
10816 : {
10817 35419173 : if (declspecs->storage_class != csc_static)
10818 35419167 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10819 : }
10820 :
10821 36301433 : announce_function (decl1);
10822 :
10823 36301433 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10824 : {
10825 1 : error_at (loc, "return type is an incomplete type");
10826 : /* Make it return void instead. */
10827 1 : TREE_TYPE (decl1)
10828 2 : = c_build_function_type (void_type_node,
10829 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10830 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10831 : }
10832 :
10833 36301433 : if (warn_about_return_type)
10834 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10835 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10836 : : OPT_Wimplicit_int),
10837 : "return type defaults to %<int%>");
10838 :
10839 : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10840 : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10841 36301433 : DECL_INITIAL (decl1) = error_mark_node;
10842 :
10843 : /* If this definition isn't a prototype and we had a prototype declaration
10844 : before, copy the arg type info from that prototype. */
10845 36301433 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10846 36301433 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10847 36071834 : old_decl = NULL_TREE;
10848 :
10849 36301433 : current_function_prototype_locus = UNKNOWN_LOCATION;
10850 36301433 : current_function_prototype_built_in = false;
10851 36301433 : current_function_prototype_arg_types = NULL_TREE;
10852 36301433 : tree newtype = TREE_TYPE (decl1);
10853 36301433 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10854 36301433 : if (!prototype_p (newtype))
10855 : {
10856 13138 : tree oldrt = TREE_TYPE (oldtype);
10857 13138 : tree newrt = TREE_TYPE (newtype);
10858 13138 : if (old_decl != NULL_TREE
10859 317 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10860 317 : && comptypes (oldrt, newrt)
10861 13436 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype))
10862 : {
10863 297 : if (stdarg_p (oldtype))
10864 : {
10865 1 : auto_diagnostic_group d;
10866 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10867 : "without prototype", decl1);
10868 1 : locate_old_decl (old_decl);
10869 1 : }
10870 297 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10871 297 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10872 297 : current_function_prototype_built_in
10873 297 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10874 297 : current_function_prototype_arg_types
10875 297 : = TYPE_ARG_TYPES (newtype);
10876 : }
10877 13138 : if (TREE_PUBLIC (decl1))
10878 : {
10879 : /* If there is an external prototype declaration of this
10880 : function, record its location but do not copy information
10881 : to this decl. This may be an invisible declaration
10882 : (built-in or in a scope which has finished) or simply
10883 : have more refined argument types than any declaration
10884 : found above. */
10885 12723 : struct c_binding *b;
10886 12999 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10887 713 : if (B_IN_SCOPE (b, external_scope))
10888 : break;
10889 12723 : if (b)
10890 : {
10891 437 : tree ext_decl, ext_type;
10892 437 : ext_decl = b->decl;
10893 437 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10894 437 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10895 874 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10896 437 : TREE_TYPE (ext_type)))
10897 : {
10898 417 : current_function_prototype_locus
10899 417 : = DECL_SOURCE_LOCATION (ext_decl);
10900 417 : current_function_prototype_built_in
10901 417 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10902 417 : current_function_prototype_arg_types
10903 417 : = TYPE_ARG_TYPES (ext_type);
10904 : }
10905 : }
10906 : }
10907 : }
10908 :
10909 : /* Optionally warn about C23 compatibility. */
10910 36301433 : if (warn_deprecated_non_prototype
10911 34 : && old_decl != NULL_TREE
10912 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10913 5 : && !TYPE_ARG_TYPES (oldtype)
10914 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10915 36301438 : && (TYPE_ARG_TYPES (newtype)
10916 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10917 : {
10918 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10919 : "ISO C23 does not allow defining"
10920 : " parameters for function %qE declared"
10921 : " without parameters",
10922 : decl1);
10923 3 : if (warned)
10924 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10925 : }
10926 :
10927 : /* Optionally warn of old-fashioned def with no previous prototype. */
10928 36301433 : if (warn_strict_prototypes
10929 107520 : && old_decl != error_mark_node
10930 107520 : && !prototype_p (TREE_TYPE (decl1))
10931 36301434 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10932 1 : warning_at (loc, OPT_Wstrict_prototypes,
10933 : "function declaration isn%'t a prototype");
10934 : /* Optionally warn of any global def with no previous prototype. */
10935 36301432 : else if (warn_missing_prototypes
10936 107221 : && old_decl != error_mark_node
10937 107221 : && TREE_PUBLIC (decl1)
10938 74072 : && !MAIN_NAME_P (DECL_NAME (decl1))
10939 74036 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10940 36302073 : && !DECL_DECLARED_INLINE_P (decl1))
10941 4 : warning_at (loc, OPT_Wmissing_prototypes,
10942 : "no previous prototype for %qD", decl1);
10943 : /* Optionally warn of any def with no previous prototype
10944 : if the function has already been used. */
10945 36301428 : else if (warn_missing_prototypes
10946 107217 : && old_decl != NULL_TREE
10947 74399 : && old_decl != error_mark_node
10948 74399 : && TREE_USED (old_decl)
10949 36308525 : && !prototype_p (TREE_TYPE (old_decl)))
10950 0 : warning_at (loc, OPT_Wmissing_prototypes,
10951 : "%qD was used with no prototype before its definition", decl1);
10952 : /* Optionally warn of any global def with no previous declaration. */
10953 36301428 : else if (warn_missing_declarations
10954 4 : && TREE_PUBLIC (decl1)
10955 3 : && old_decl == NULL_TREE
10956 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10957 36301431 : && !DECL_DECLARED_INLINE_P (decl1))
10958 2 : warning_at (loc, OPT_Wmissing_declarations,
10959 : "no previous declaration for %qD",
10960 : decl1);
10961 : /* Optionally warn of any def with no previous declaration
10962 : if the function has already been used. */
10963 36301426 : else if (warn_missing_declarations
10964 2 : && old_decl != NULL_TREE
10965 0 : && old_decl != error_mark_node
10966 0 : && TREE_USED (old_decl)
10967 36301426 : && C_DECL_IMPLICIT (old_decl))
10968 0 : warning_at (loc, OPT_Wmissing_declarations,
10969 : "%qD was used with no declaration before its definition", decl1);
10970 :
10971 : /* This function exists in static storage.
10972 : (This does not mean `static' in the C sense!) */
10973 36301433 : TREE_STATIC (decl1) = 1;
10974 :
10975 : /* This is the earliest point at which we might know the assembler
10976 : name of the function. Thus, if it's set before this, die horribly. */
10977 36301433 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10978 :
10979 : /* If #pragma weak was used, mark the decl weak now. */
10980 36301433 : if (current_scope == file_scope)
10981 36299845 : maybe_apply_pragma_weak (decl1);
10982 :
10983 : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10984 36301433 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10985 : {
10986 3208 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10987 3208 : != integer_type_node)
10988 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10989 3203 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10990 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10991 : decl1);
10992 :
10993 3208 : check_main_parameter_types (decl1);
10994 :
10995 3208 : if (!TREE_PUBLIC (decl1))
10996 0 : pedwarn (loc, OPT_Wmain,
10997 : "%qD is normally a non-static function", decl1);
10998 : }
10999 :
11000 36301433 : tree parms = current_function_arg_info->parms;
11001 36301433 : if (old_decl)
11002 : {
11003 229599 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
11004 229599 : warn_parms_array_mismatch (origloc, old_decl, parms);
11005 : }
11006 :
11007 : /* To enable versions to be created across TU's we mark and mangle all
11008 : non-default versioned functions. */
11009 36301433 : if (TREE_CODE (decl1) == FUNCTION_DECL
11010 : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
11011 : && get_target_version (decl1).is_valid ())
11012 : {
11013 : maybe_mark_function_versioned (decl1);
11014 : if (current_scope != file_scope)
11015 : error ("versioned definitions are only allowed at file scope");
11016 : }
11017 :
11018 : /* Record the decl so that the function name is defined.
11019 : If we already have a decl for this name, and it is a FUNCTION_DECL,
11020 : use the old decl. */
11021 :
11022 36301433 : current_function_decl = pushdecl (decl1);
11023 :
11024 36301433 : if (tree access = build_attr_access_from_parms (parms, false))
11025 57603 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
11026 : old_decl);
11027 :
11028 36301433 : push_scope ();
11029 36301433 : declare_parm_level ();
11030 :
11031 : /* Set the result decl source location to the location of the typespec. */
11032 4099026 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
11033 36301433 : ? loc : declspecs->locations[cdw_typespec]);
11034 36301433 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
11035 36301433 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
11036 36301433 : DECL_ARTIFICIAL (resdecl) = 1;
11037 36301433 : DECL_IGNORED_P (resdecl) = 1;
11038 36301433 : DECL_RESULT (current_function_decl) = resdecl;
11039 :
11040 36301433 : start_fname_decls ();
11041 :
11042 36301433 : return true;
11043 : }
11044 :
11045 : /* Subroutine of store_parm_decls which handles new-style function
11046 : definitions (prototype format). The parms already have decls, so we
11047 : need only record them as in effect and complain if any redundant
11048 : old-style parm decls were written. */
11049 : static void
11050 36288308 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
11051 : {
11052 36288308 : tree decl;
11053 36288308 : c_arg_tag *tag;
11054 36288308 : unsigned ix;
11055 :
11056 36288308 : if (current_scope->bindings)
11057 : {
11058 8 : error_at (DECL_SOURCE_LOCATION (fndecl),
11059 : "old-style parameter declarations in prototyped "
11060 : "function definition");
11061 :
11062 : /* Get rid of the old-style declarations. */
11063 8 : pop_scope ();
11064 8 : push_scope ();
11065 : }
11066 : /* Don't issue this warning for nested functions, and don't issue this
11067 : warning if we got here because ARG_INFO_TYPES was error_mark_node
11068 : (this happens when a function definition has just an ellipsis in
11069 : its parameter list). */
11070 36288300 : else if (!in_system_header_at (input_location)
11071 640097 : && !current_function_scope
11072 638657 : && arg_info->types != error_mark_node
11073 36926957 : && !arg_info->c23_empty_parens)
11074 587016 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
11075 : "traditional C rejects ISO C style function definitions");
11076 :
11077 : /* Now make all the parameter declarations visible in the function body.
11078 : We can bypass most of the grunt work of pushdecl. */
11079 136273943 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
11080 : {
11081 99985635 : DECL_CONTEXT (decl) = current_function_decl;
11082 99985635 : if (DECL_NAME (decl))
11083 : {
11084 99984062 : bind (DECL_NAME (decl), decl, current_scope,
11085 : /*invisible=*/false, /*nested=*/false,
11086 : UNKNOWN_LOCATION);
11087 99984062 : if (!TREE_USED (decl))
11088 99966665 : warn_if_shadowing (decl);
11089 : }
11090 : else
11091 1573 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
11092 : "ISO C does not support omitting parameter names in "
11093 : "function definitions before C23");
11094 : }
11095 :
11096 : /* Record the parameter list in the function declaration. */
11097 36288308 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
11098 :
11099 : /* Now make all the ancillary declarations visible, likewise. */
11100 36288369 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
11101 : {
11102 61 : DECL_CONTEXT (decl) = current_function_decl;
11103 61 : if (DECL_NAME (decl))
11104 0 : bind (DECL_NAME (decl), decl, current_scope,
11105 : /*invisible=*/false,
11106 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
11107 : UNKNOWN_LOCATION);
11108 : }
11109 :
11110 : /* And all the tag declarations. */
11111 36288425 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
11112 61 : if (tag->id)
11113 28 : bind (tag->id, tag->type, current_scope,
11114 : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
11115 36288308 : }
11116 :
11117 : /* Subroutine of store_parm_decls which handles old-style function
11118 : definitions (separate parameter list and declarations). */
11119 :
11120 : static void
11121 13125 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
11122 : {
11123 13125 : struct c_binding *b;
11124 13125 : tree parm, decl, last;
11125 13125 : tree parmids = arg_info->parms;
11126 13125 : hash_set<tree> seen_args;
11127 :
11128 13125 : if (!in_system_header_at (input_location))
11129 : {
11130 13122 : if (flag_isoc23)
11131 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
11132 1349 : OPT_Wold_style_definition, "old-style function definition");
11133 : else
11134 11773 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11135 11773 : OPT_Wold_style_definition,
11136 : "old-style function definition");
11137 : }
11138 :
11139 13125 : if (current_scope->had_vla_unspec)
11140 1 : error ("%<[*]%> not allowed in other than function prototype scope");
11141 :
11142 : /* Match each formal parameter name with its declaration. Save each
11143 : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
11144 47400 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11145 : {
11146 34275 : if (TREE_VALUE (parm) == NULL_TREE)
11147 : {
11148 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
11149 : "parameter name missing from parameter list");
11150 0 : TREE_PURPOSE (parm) = NULL_TREE;
11151 0 : continue;
11152 : }
11153 :
11154 34275 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11155 34275 : if (b && B_IN_CURRENT_SCOPE (b))
11156 : {
11157 22655 : decl = b->decl;
11158 : /* Skip erroneous parameters. */
11159 22655 : if (decl == error_mark_node)
11160 2 : continue;
11161 : /* If we got something other than a PARM_DECL it is an error. */
11162 22653 : if (TREE_CODE (decl) != PARM_DECL)
11163 : {
11164 7 : error_at (DECL_SOURCE_LOCATION (decl),
11165 : "%qD declared as a non-parameter", decl);
11166 7 : continue;
11167 : }
11168 : /* If the declaration is already marked, we have a duplicate
11169 : name. Complain and ignore the duplicate. */
11170 22646 : else if (seen_args.contains (decl))
11171 : {
11172 0 : error_at (DECL_SOURCE_LOCATION (decl),
11173 : "multiple parameters named %qD", decl);
11174 0 : TREE_PURPOSE (parm) = NULL_TREE;
11175 0 : continue;
11176 : }
11177 : /* If the declaration says "void", complain and turn it into
11178 : an int. */
11179 22646 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
11180 : {
11181 0 : error_at (DECL_SOURCE_LOCATION (decl),
11182 : "parameter %qD declared with void type", decl);
11183 0 : TREE_TYPE (decl) = integer_type_node;
11184 0 : DECL_ARG_TYPE (decl) = integer_type_node;
11185 0 : layout_decl (decl, 0);
11186 : }
11187 22646 : warn_if_shadowing (decl);
11188 : }
11189 : /* If no declaration found, default to int. */
11190 : else
11191 : {
11192 : /* FIXME diagnostics: This should be the location of the argument,
11193 : not the FNDECL. E.g., for an old-style declaration
11194 :
11195 : int f10(v) { blah; }
11196 :
11197 : We should use the location of the V, not the F10.
11198 : Unfortunately, the V is an IDENTIFIER_NODE which has no
11199 : location. In the future we need locations for c_arg_info
11200 : entries.
11201 :
11202 : See gcc.dg/Wshadow-3.c for an example of this problem. */
11203 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11204 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
11205 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11206 11620 : pushdecl (decl);
11207 11620 : warn_if_shadowing (decl);
11208 :
11209 11620 : if (flag_isoc99)
11210 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
11211 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11212 : decl);
11213 : else
11214 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
11215 11502 : OPT_Wmissing_parameter_type,
11216 : "type of %qD defaults to %<int%>", decl);
11217 : }
11218 :
11219 34266 : TREE_PURPOSE (parm) = decl;
11220 34266 : seen_args.add (decl);
11221 : }
11222 :
11223 : /* Now examine the parms chain for incomplete declarations
11224 : and declarations with no corresponding names. */
11225 :
11226 47479 : for (b = current_scope->bindings; b; b = b->prev)
11227 : {
11228 34354 : parm = b->decl;
11229 34354 : if (TREE_CODE (parm) != PARM_DECL)
11230 79 : continue;
11231 :
11232 34275 : if (TREE_TYPE (parm) != error_mark_node
11233 34275 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11234 : {
11235 0 : error_at (DECL_SOURCE_LOCATION (parm),
11236 : "parameter %qD has incomplete type", parm);
11237 0 : TREE_TYPE (parm) = error_mark_node;
11238 : }
11239 :
11240 34275 : if (!seen_args.contains (parm))
11241 : {
11242 9 : error_at (DECL_SOURCE_LOCATION (parm),
11243 : "declaration for parameter %qD but no such parameter",
11244 : parm);
11245 :
11246 : /* Pretend the parameter was not missing.
11247 : This gets us to a standard state and minimizes
11248 : further error messages. */
11249 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
11250 : }
11251 : }
11252 :
11253 : /* Chain the declarations together in the order of the list of
11254 : names. Store that chain in the function decl, replacing the
11255 : list of names. Update the current scope to match. */
11256 13125 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
11257 :
11258 13133 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11259 8681 : if (TREE_PURPOSE (parm))
11260 : break;
11261 13125 : if (parm && TREE_PURPOSE (parm))
11262 : {
11263 8673 : last = TREE_PURPOSE (parm);
11264 8673 : DECL_ARGUMENTS (fndecl) = last;
11265 :
11266 34276 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11267 25603 : if (TREE_PURPOSE (parm))
11268 : {
11269 25602 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11270 25603 : last = TREE_PURPOSE (parm);
11271 : }
11272 8673 : DECL_CHAIN (last) = NULL_TREE;
11273 : }
11274 :
11275 : /* If there was a previous prototype,
11276 : set the DECL_ARG_TYPE of each argument according to
11277 : the type previously specified, and report any mismatches. */
11278 :
11279 13125 : if (current_function_prototype_arg_types)
11280 : {
11281 171 : tree type;
11282 171 : for (parm = DECL_ARGUMENTS (fndecl),
11283 171 : type = current_function_prototype_arg_types;
11284 373 : parm || (type != NULL_TREE
11285 166 : && TREE_VALUE (type) != error_mark_node
11286 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11287 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11288 : {
11289 212 : if (parm == NULL_TREE
11290 206 : || type == NULL_TREE
11291 418 : || (TREE_VALUE (type) != error_mark_node
11292 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11293 : {
11294 10 : if (current_function_prototype_built_in)
11295 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11296 6 : 0, "number of arguments doesn%'t match "
11297 : "built-in prototype");
11298 : else
11299 : {
11300 : /* FIXME diagnostics: This should be the location of
11301 : FNDECL, but there is bug when a prototype is
11302 : declared inside function context, but defined
11303 : outside of it (e.g., gcc.dg/pr15698-2.c). In
11304 : which case FNDECL gets the location of the
11305 : prototype, not the definition. */
11306 4 : error_at (input_location,
11307 : "number of arguments doesn%'t match prototype");
11308 :
11309 4 : error_at (current_function_prototype_locus,
11310 : "prototype declaration");
11311 : }
11312 : break;
11313 : }
11314 : /* Type for passing arg must be consistent with that
11315 : declared for the arg. ISO C says we take the unqualified
11316 : type for parameters declared with qualified type. */
11317 202 : if (TREE_TYPE (parm) != error_mark_node
11318 201 : && TREE_VALUE (type) != error_mark_node
11319 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11320 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11321 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11322 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11323 : {
11324 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11325 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11326 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11327 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11328 : {
11329 : /* Adjust argument to match prototype. E.g. a previous
11330 : `int foo(float);' prototype causes
11331 : `int foo(x) float x; {...}' to be treated like
11332 : `int foo(float x) {...}'. This is particularly
11333 : useful for argument types like uid_t. */
11334 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11335 :
11336 : /* ??? Is it possible to get here with a
11337 : built-in prototype or will it always have
11338 : been diagnosed as conflicting with an
11339 : old-style definition and discarded? */
11340 17 : if (current_function_prototype_built_in)
11341 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11342 2 : OPT_Wpedantic, "promoted argument %qD "
11343 : "doesn%'t match built-in prototype", parm);
11344 : else
11345 : {
11346 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11347 15 : OPT_Wpedantic, "promoted argument %qD "
11348 : "doesn%'t match prototype", parm);
11349 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11350 : "prototype declaration");
11351 : }
11352 : }
11353 : else
11354 : {
11355 19 : if (current_function_prototype_built_in)
11356 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11357 10 : 0, "argument %qD doesn%'t match "
11358 : "built-in prototype", parm);
11359 : else
11360 : {
11361 9 : error_at (DECL_SOURCE_LOCATION (parm),
11362 : "argument %qD doesn%'t match prototype", parm);
11363 9 : error_at (current_function_prototype_locus,
11364 : "prototype declaration");
11365 : }
11366 : }
11367 : }
11368 : }
11369 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11370 : }
11371 :
11372 : /* Otherwise, create a prototype that would match. */
11373 :
11374 : else
11375 : {
11376 12954 : tree actual = NULL_TREE, last = NULL_TREE, type;
11377 :
11378 47023 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11379 : {
11380 34069 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11381 34069 : if (last)
11382 25552 : TREE_CHAIN (last) = type;
11383 : else
11384 : actual = type;
11385 34069 : last = type;
11386 : }
11387 12954 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11388 12954 : if (last)
11389 8517 : TREE_CHAIN (last) = type;
11390 : else
11391 : actual = type;
11392 :
11393 : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11394 : of the type of this function, but we need to avoid having this
11395 : affect the types of other similarly-typed functions, so we must
11396 : first force the generation of an identical (but separate) type
11397 : node for the relevant function type. The new node we create
11398 : will be a variant of the main variant of the original function
11399 : type. */
11400 :
11401 12954 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11402 :
11403 12954 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11404 : }
11405 13125 : }
11406 :
11407 : /* Store parameter declarations passed in ARG_INFO into the current
11408 : function declaration. */
11409 :
11410 : void
11411 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11412 : {
11413 0 : current_function_arg_info = arg_info;
11414 0 : store_parm_decls ();
11415 0 : }
11416 :
11417 : /* Called by walk_tree to look for and update context-less labels
11418 : or labels with context in the parent function. */
11419 :
11420 : static tree
11421 8135 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11422 : {
11423 8135 : tree ctx = static_cast<tree>(data);
11424 8135 : if (TREE_CODE (*tp) == LABEL_EXPR
11425 8135 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11426 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11427 : {
11428 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11429 72 : *walk_subtrees = 0;
11430 : }
11431 :
11432 8135 : return NULL_TREE;
11433 : }
11434 :
11435 : /* Store the parameter declarations into the current function declaration.
11436 : This is called after parsing the parameter declarations, before
11437 : digesting the body of the function.
11438 :
11439 : For an old-style definition, construct a prototype out of the old-style
11440 : parameter declarations and inject it into the function's type. */
11441 :
11442 : void
11443 36301433 : store_parm_decls (void)
11444 : {
11445 36301433 : tree fndecl = current_function_decl;
11446 36301433 : bool proto;
11447 :
11448 : /* The argument information block for FNDECL. */
11449 36301433 : struct c_arg_info *arg_info = current_function_arg_info;
11450 36301433 : current_function_arg_info = 0;
11451 :
11452 : /* True if this definition is written with a prototype. In C23, an
11453 : empty argument list was converted to (void) in grokparms; in
11454 : older C standard versions, it does not give the function a type
11455 : with a prototype for future calls. */
11456 36301433 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11457 :
11458 13125 : if (proto)
11459 36288308 : store_parm_decls_newstyle (fndecl, arg_info);
11460 : else
11461 13125 : store_parm_decls_oldstyle (fndecl, arg_info);
11462 :
11463 : /* The next call to push_scope will be a function body. */
11464 :
11465 36301433 : next_is_function_body = true;
11466 :
11467 : /* Write a record describing this function definition to the prototypes
11468 : file (if requested). */
11469 :
11470 36301433 : gen_aux_info_record (fndecl, 1, 0, proto);
11471 :
11472 : /* Initialize the RTL code for the function. */
11473 36301433 : allocate_struct_function (fndecl, false);
11474 :
11475 36301433 : if (warn_unused_local_typedefs)
11476 3115061 : cfun->language = ggc_cleared_alloc<language_function> ();
11477 :
11478 : /* Begin the statement tree for this function. */
11479 36301433 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11480 :
11481 : /* ??? Insert the contents of the pending sizes list into the function
11482 : to be evaluated. The only reason left to have this is
11483 : void foo(int n, int array[n++])
11484 : because we throw away the array type in favor of a pointer type, and
11485 : thus won't naturally see the SAVE_EXPR containing the increment. All
11486 : other pending sizes would be handled by gimplify_parameters. */
11487 36301433 : if (arg_info->pending_sizes)
11488 : {
11489 : /* In very special circumstances, e.g. for code like
11490 : _Atomic int i = 5;
11491 : void f (int a[i += 2]) {}
11492 : we need to execute the atomic assignment on function entry.
11493 : But in this case, it is not just a straight store, it has the
11494 : op= form, which means that build_atomic_assign has generated
11495 : gotos, labels, etc. Because at that time the function decl
11496 : for F has not been created yet, those labels do not have any
11497 : function context. But we have the fndecl now, so update the
11498 : labels accordingly. gimplify_expr would crash otherwise.
11499 : Or with nested functions the labels could be created with parent
11500 : function's context, while when the statement is emitted at the
11501 : start of the nested function, it needs the nested function's
11502 : context. */
11503 297 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11504 : set_labels_context_r, fndecl);
11505 297 : add_stmt (arg_info->pending_sizes);
11506 : }
11507 36301433 : }
11508 :
11509 : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11510 : c_finish_omp_declare_simd for function prototypes. No diagnostics
11511 : should be done. */
11512 :
11513 : void
11514 249 : temp_store_parm_decls (tree fndecl, tree parms)
11515 : {
11516 249 : push_scope ();
11517 699 : for (tree p = parms; p; p = DECL_CHAIN (p))
11518 : {
11519 450 : DECL_CONTEXT (p) = fndecl;
11520 450 : if (DECL_NAME (p))
11521 328 : bind (DECL_NAME (p), p, current_scope,
11522 : /*invisible=*/false, /*nested=*/false,
11523 : UNKNOWN_LOCATION);
11524 : }
11525 249 : }
11526 :
11527 : /* Undo what temp_store_parm_decls did. */
11528 :
11529 : void
11530 249 : temp_pop_parm_decls (void)
11531 : {
11532 : /* Clear all bindings in this temporary scope, so that
11533 : pop_scope doesn't create a BLOCK. */
11534 249 : struct c_binding *b = current_scope->bindings;
11535 249 : current_scope->bindings = NULL;
11536 582 : for (; b; b = free_binding_and_advance (b))
11537 : {
11538 333 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11539 : || b->decl == error_mark_node);
11540 333 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11541 333 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11542 333 : if (b->shadowed && b->shadowed->u.type)
11543 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11544 : }
11545 249 : pop_scope ();
11546 249 : }
11547 :
11548 :
11549 : /* Finish up a function declaration and compile that function
11550 : all the way to assembler language output. Then free the storage
11551 : for the function definition.
11552 :
11553 : This is called after parsing the body of the function definition. */
11554 :
11555 : void
11556 36301431 : finish_function (location_t end_loc)
11557 : {
11558 36301431 : tree fndecl = current_function_decl;
11559 :
11560 36301431 : if (c_dialect_objc ())
11561 0 : objc_finish_function ();
11562 :
11563 36301431 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11564 36301429 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11565 :
11566 : /* Must mark the RESULT_DECL as being in this function. */
11567 :
11568 36301431 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11569 36301431 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11570 :
11571 36348643 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11572 47211 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11573 36348642 : == integer_type_node && flag_isoc99)
11574 : {
11575 : /* Hack. We don't want the middle-end to warn that this return
11576 : is unreachable, so we mark its location as special. Using
11577 : UNKNOWN_LOCATION has the problem that it gets clobbered in
11578 : annotate_one_with_locus. A cleaner solution might be to
11579 : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11580 : */
11581 45828 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11582 : }
11583 :
11584 : /* Tie off the statement tree for this function. */
11585 36301431 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11586 :
11587 36301431 : finish_fname_decls ();
11588 :
11589 : /* Complain if there's no return statement only if option specified on
11590 : command line. */
11591 36301431 : if (warn_return_type > 0
11592 3115084 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11593 2919184 : && !current_function_returns_value && !current_function_returns_null
11594 : /* Don't complain if we are no-return. */
11595 72 : && !current_function_returns_abnormally
11596 : /* Don't complain if we are declared noreturn. */
11597 21 : && !TREE_THIS_VOLATILE (fndecl)
11598 : /* Don't warn for main(). */
11599 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11600 : /* Or if they didn't actually specify a return type. */
11601 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11602 : /* Normally, with -Wreturn-type, flow will complain, but we might
11603 : optimize out static functions. */
11604 17 : && !TREE_PUBLIC (fndecl)
11605 6 : && targetm.warn_func_return (fndecl)
11606 39416515 : && warning (OPT_Wreturn_type,
11607 : "no return statement in function returning non-void"))
11608 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11609 :
11610 : /* Complain about parameters that are only set, but never otherwise used. */
11611 36301431 : if (warn_unused_but_set_parameter)
11612 : {
11613 3050234 : tree decl;
11614 :
11615 3050234 : for (decl = DECL_ARGUMENTS (fndecl);
11616 11397920 : decl;
11617 8347686 : decl = DECL_CHAIN (decl))
11618 8347686 : if (TREE_USED (decl)
11619 8286983 : && TREE_CODE (decl) == PARM_DECL
11620 8286983 : && !DECL_READ_P (decl)
11621 47 : && DECL_NAME (decl)
11622 47 : && !DECL_ARTIFICIAL (decl)
11623 8347733 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11624 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11625 47 : OPT_Wunused_but_set_parameter_,
11626 : "parameter %qD set but not used", decl);
11627 : }
11628 :
11629 : /* Complain about locally defined typedefs that are not used in this
11630 : function. */
11631 36301431 : maybe_warn_unused_local_typedefs ();
11632 :
11633 : /* Possibly warn about unused parameters. */
11634 36301431 : if (warn_unused_parameter)
11635 2956345 : do_warn_unused_parameter (fndecl);
11636 :
11637 : /* Store the end of the function, so that we get good line number
11638 : info for the epilogue. */
11639 36301431 : cfun->function_end_locus = end_loc;
11640 :
11641 : /* Finalize the ELF visibility for the function. */
11642 36301431 : c_determine_visibility (fndecl);
11643 :
11644 : /* For GNU C extern inline functions disregard inline limits. */
11645 36301431 : if (DECL_EXTERNAL (fndecl)
11646 35447766 : && DECL_DECLARED_INLINE_P (fndecl)
11647 71749194 : && (flag_gnu89_inline
11648 35416759 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11649 35447281 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11650 :
11651 : /* Genericize before inlining. Delay genericizing nested functions
11652 : until their parent function is genericized. Since finalizing
11653 : requires GENERIC, delay that as well. */
11654 :
11655 72602862 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11656 72602860 : && !undef_nested_function)
11657 : {
11658 36301423 : if (!decl_function_context (fndecl))
11659 : {
11660 36299835 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11661 36299835 : c_genericize (fndecl);
11662 :
11663 : /* ??? Objc emits functions after finalizing the compilation unit.
11664 : This should be cleaned up later and this conditional removed. */
11665 36299835 : if (symtab->global_info_ready)
11666 : {
11667 0 : cgraph_node::add_new_function (fndecl, false);
11668 0 : return;
11669 : }
11670 36299835 : cgraph_node::finalize_function (fndecl, false);
11671 : }
11672 : else
11673 : {
11674 : /* Register this function with cgraph just far enough to get it
11675 : added to our parent's nested function list. Handy, since the
11676 : C front end doesn't have such a list. */
11677 1588 : (void) cgraph_node::get_create (fndecl);
11678 : }
11679 : }
11680 :
11681 36301431 : if (!decl_function_context (fndecl))
11682 36299843 : undef_nested_function = false;
11683 :
11684 36301431 : if (cfun->language != NULL)
11685 : {
11686 3115060 : ggc_free (cfun->language);
11687 3115060 : cfun->language = NULL;
11688 : }
11689 :
11690 : /* We're leaving the context of this function, so zap cfun.
11691 : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11692 : tree_rest_of_compilation. */
11693 36301431 : set_cfun (NULL);
11694 36301431 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11695 36301431 : current_function_decl = NULL;
11696 : }
11697 :
11698 : /* Check the declarations given in a for-loop for satisfying the C99
11699 : constraints. If exactly one such decl is found, return it. LOC is
11700 : the location of the opening parenthesis of the for loop. The last
11701 : parameter allows you to control the "for loop initial declarations
11702 : are only allowed in C99 mode". Normally, you should pass
11703 : flag_isoc99 as that parameter. But in some cases (Objective-C
11704 : foreach loop, for example) we want to run the checks in this
11705 : function even if not in C99 mode, so we allow the caller to turn
11706 : off the error about not being in C99 mode.
11707 : */
11708 :
11709 : tree
11710 25783 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11711 : {
11712 25783 : struct c_binding *b;
11713 25783 : tree one_decl = NULL_TREE;
11714 25783 : int n_decls = 0;
11715 :
11716 25783 : if (!turn_off_iso_c99_error)
11717 : {
11718 1 : static bool hint = true;
11719 : /* If we get here, declarations have been used in a for loop without
11720 : the C99 for loop scope. This doesn't make much sense, so don't
11721 : allow it. */
11722 1 : auto_diagnostic_group d;
11723 1 : error_at (loc, "%<for%> loop initial declarations "
11724 : "are only allowed in C99 or C11 mode");
11725 1 : if (hint)
11726 : {
11727 1 : inform (loc,
11728 : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11729 : "%<-std=gnu11%> to compile your code");
11730 1 : hint = false;
11731 : }
11732 1 : return NULL_TREE;
11733 1 : }
11734 : else
11735 25782 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11736 : "initial declarations");
11737 :
11738 : /* C99 subclause 6.8.5 paragraph 3:
11739 :
11740 : [#3] The declaration part of a for statement shall only
11741 : declare identifiers for objects having storage class auto or
11742 : register.
11743 :
11744 : It isn't clear whether, in this sentence, "identifiers" binds to
11745 : "shall only declare" or to "objects" - that is, whether all identifiers
11746 : declared must be identifiers for objects, or whether the restriction
11747 : only applies to those that are. (A question on this in comp.std.c
11748 : in November 2000 received no answer.) We implement the strictest
11749 : interpretation, to avoid creating an extension which later causes
11750 : problems.
11751 :
11752 : This constraint was removed in C23. */
11753 :
11754 51639 : for (b = current_scope->bindings; b; b = b->prev)
11755 : {
11756 25857 : tree id = b->id;
11757 25857 : tree decl = b->decl;
11758 :
11759 25857 : if (!id)
11760 27 : continue;
11761 :
11762 25830 : switch (TREE_CODE (decl))
11763 : {
11764 25782 : case VAR_DECL:
11765 25782 : {
11766 25782 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11767 25782 : if (TREE_STATIC (decl))
11768 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11769 : "declaration of static variable %qD in %<for%> "
11770 : "loop initial declaration", decl);
11771 25777 : else if (DECL_EXTERNAL (decl))
11772 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11773 : "declaration of %<extern%> variable %qD in %<for%> "
11774 : "loop initial declaration", decl);
11775 : }
11776 : break;
11777 :
11778 6 : case RECORD_TYPE:
11779 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11780 : "%<struct %E%> declared in %<for%> loop initial "
11781 : "declaration", id);
11782 6 : break;
11783 5 : case UNION_TYPE:
11784 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11785 : "%<union %E%> declared in %<for%> loop initial "
11786 : "declaration",
11787 : id);
11788 5 : break;
11789 5 : case ENUMERAL_TYPE:
11790 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11791 : "%<enum %E%> declared in %<for%> loop "
11792 : "initial declaration", id);
11793 5 : break;
11794 32 : default:
11795 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11796 : "%qD in %<for%> loop initial declaration", decl);
11797 : }
11798 :
11799 25830 : n_decls++;
11800 25830 : one_decl = decl;
11801 : }
11802 :
11803 25782 : return n_decls == 1 ? one_decl : NULL_TREE;
11804 : }
11805 :
11806 : /* Save and reinitialize the variables
11807 : used during compilation of a C function. */
11808 :
11809 : void
11810 1616 : c_push_function_context (void)
11811 : {
11812 1616 : struct language_function *p = cfun->language;
11813 : /* cfun->language might have been already allocated by the use of
11814 : -Wunused-local-typedefs. In that case, just re-use it. */
11815 1616 : if (p == NULL)
11816 1560 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11817 :
11818 1616 : p->base.x_stmt_tree = c_stmt_tree;
11819 1616 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11820 1616 : p->x_in_statement = in_statement;
11821 1616 : p->x_switch_stack = c_switch_stack;
11822 1616 : p->loop_names = loop_names;
11823 1616 : loop_names = vNULL;
11824 1616 : p->loop_names_hash = loop_names_hash;
11825 1616 : loop_names_hash = NULL;
11826 1616 : p->arg_info = current_function_arg_info;
11827 1616 : p->returns_value = current_function_returns_value;
11828 1616 : p->returns_null = current_function_returns_null;
11829 1616 : p->returns_abnormally = current_function_returns_abnormally;
11830 1616 : p->warn_about_return_type = warn_about_return_type;
11831 :
11832 1616 : push_function_context ();
11833 1616 : }
11834 :
11835 : /* Restore the variables used during compilation of a C function. */
11836 :
11837 : void
11838 1616 : c_pop_function_context (void)
11839 : {
11840 1616 : struct language_function *p;
11841 :
11842 1616 : pop_function_context ();
11843 1616 : p = cfun->language;
11844 :
11845 : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11846 : used to store data throughout the life time of the current cfun,
11847 : So don't deallocate it. */
11848 1616 : if (!warn_unused_local_typedefs)
11849 1560 : cfun->language = NULL;
11850 :
11851 1616 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11852 1616 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11853 : {
11854 : /* Stop pointing to the local nodes about to be freed. */
11855 : /* But DECL_INITIAL must remain nonzero so we know this
11856 : was an actual function definition. */
11857 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11858 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11859 : }
11860 :
11861 1616 : c_stmt_tree = p->base.x_stmt_tree;
11862 1616 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11863 1616 : in_statement = p->x_in_statement;
11864 1616 : c_switch_stack = p->x_switch_stack;
11865 1616 : loop_names.release ();
11866 1616 : loop_names = p->loop_names;
11867 1616 : p->loop_names = vNULL;
11868 1616 : delete loop_names_hash;
11869 1616 : loop_names_hash = p->loop_names_hash;
11870 1616 : p->loop_names_hash = NULL;
11871 1616 : current_function_arg_info = p->arg_info;
11872 1616 : current_function_returns_value = p->returns_value;
11873 1616 : current_function_returns_null = p->returns_null;
11874 1616 : current_function_returns_abnormally = p->returns_abnormally;
11875 1616 : warn_about_return_type = p->warn_about_return_type;
11876 1616 : }
11877 :
11878 : /* The functions below are required for functionality of doing
11879 : function at once processing in the C front end. Currently these
11880 : functions are not called from anywhere in the C front end, but as
11881 : these changes continue, that will change. */
11882 :
11883 : /* Returns the stmt_tree (if any) to which statements are currently
11884 : being added. If there is no active statement-tree, NULL is
11885 : returned. */
11886 :
11887 : stmt_tree
11888 985191417 : current_stmt_tree (void)
11889 : {
11890 985191417 : return &c_stmt_tree;
11891 : }
11892 :
11893 : /* Return the global value of T as a symbol. */
11894 :
11895 : tree
11896 3890112 : identifier_global_value (tree t)
11897 : {
11898 3890112 : struct c_binding *b;
11899 :
11900 3891079 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11901 3883848 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11902 3882881 : return b->decl;
11903 :
11904 : return NULL_TREE;
11905 : }
11906 :
11907 : /* Return the global value of tag T as a symbol. */
11908 :
11909 : tree
11910 12 : identifier_global_tag (tree t)
11911 : {
11912 12 : struct c_binding *b;
11913 :
11914 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11915 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11916 11 : return b->decl;
11917 :
11918 : return NULL_TREE;
11919 : }
11920 :
11921 : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11922 : function or function-like operator. */
11923 :
11924 : int
11925 25004 : names_builtin_p (const char *name)
11926 : {
11927 25004 : tree id = get_identifier (name);
11928 25004 : if (tree decl = identifier_global_value (id))
11929 24916 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11930 :
11931 : /* Also detect common reserved C words that aren't strictly built-in
11932 : functions. */
11933 88 : switch (C_RID_CODE (id))
11934 : {
11935 : case RID_BUILTIN_ASSOC_BARRIER:
11936 : case RID_BUILTIN_CONVERTVECTOR:
11937 : case RID_BUILTIN_HAS_ATTRIBUTE:
11938 : case RID_BUILTIN_SHUFFLE:
11939 : case RID_BUILTIN_SHUFFLEVECTOR:
11940 : case RID_BUILTIN_STDC:
11941 : case RID_BUILTIN_COUNTED_BY_REF:
11942 : case RID_CHOOSE_EXPR:
11943 : case RID_OFFSETOF:
11944 : case RID_TYPES_COMPATIBLE_P:
11945 : case RID_C23_VA_START:
11946 : case RID_VA_ARG:
11947 : return 1;
11948 67 : default:
11949 67 : break;
11950 : }
11951 :
11952 67 : return 0;
11953 : }
11954 :
11955 : /* In C, the only C-linkage public declaration is at file scope. */
11956 :
11957 : tree
11958 5 : c_linkage_bindings (tree name)
11959 : {
11960 5 : return identifier_global_value (name);
11961 : }
11962 :
11963 : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11964 : otherwise the name is found in ridpointers from RID_INDEX. */
11965 :
11966 : void
11967 3303840 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11968 : {
11969 3303840 : tree id, decl;
11970 3303840 : if (name == 0)
11971 1541792 : id = ridpointers[(int) rid_index];
11972 : else
11973 1762048 : id = get_identifier (name);
11974 3303840 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11975 3303840 : pushdecl (decl);
11976 3303840 : if (debug_hooks->type_decl)
11977 3303840 : debug_hooks->type_decl (decl, false);
11978 3303840 : }
11979 :
11980 : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11981 :
11982 : struct c_parm *
11983 124026414 : build_c_parm (struct c_declspecs *specs, tree attrs,
11984 : struct c_declarator *declarator,
11985 : location_t loc)
11986 : {
11987 124026414 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11988 124026414 : ret->specs = specs;
11989 124026414 : ret->attrs = attrs;
11990 124026414 : ret->declarator = declarator;
11991 124026414 : ret->loc = loc;
11992 124026414 : return ret;
11993 : }
11994 :
11995 : /* Return a declarator with nested attributes. TARGET is the inner
11996 : declarator to which these attributes apply. ATTRS are the
11997 : attributes. */
11998 :
11999 : struct c_declarator *
12000 6779 : build_attrs_declarator (tree attrs, struct c_declarator *target)
12001 : {
12002 6779 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12003 6779 : ret->kind = cdk_attrs;
12004 6779 : ret->declarator = target;
12005 6779 : ret->u.attrs = attrs;
12006 6779 : return ret;
12007 : }
12008 :
12009 : /* Return a declarator for a function with arguments specified by ARGS
12010 : and return type specified by TARGET. */
12011 :
12012 : struct c_declarator *
12013 50559019 : build_function_declarator (struct c_arg_info *args,
12014 : struct c_declarator *target)
12015 : {
12016 50559019 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12017 50559019 : ret->kind = cdk_function;
12018 50559019 : ret->declarator = target;
12019 50559019 : ret->u.arg_info = args;
12020 50559019 : return ret;
12021 : }
12022 :
12023 : /* Return a declarator for the identifier IDENT (which may be
12024 : NULL_TREE for an abstract declarator). */
12025 :
12026 : struct c_declarator *
12027 314095312 : build_id_declarator (tree ident)
12028 : {
12029 314095312 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12030 314095312 : ret->kind = cdk_id;
12031 314095312 : ret->declarator = 0;
12032 314095312 : ret->u.id.id = ident;
12033 314095312 : ret->u.id.attrs = NULL_TREE;
12034 : /* Default value - may get reset to a more precise location. */
12035 314095312 : ret->id_loc = input_location;
12036 314095312 : return ret;
12037 : }
12038 :
12039 : /* Return something to represent absolute declarators containing a *.
12040 : TARGET is the absolute declarator that the * contains.
12041 : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
12042 : to apply to the pointer type. */
12043 :
12044 : struct c_declarator *
12045 18236689 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
12046 : struct c_declarator *target)
12047 : {
12048 18236689 : tree attrs;
12049 18236689 : int quals = 0;
12050 18236689 : struct c_declarator *itarget = target;
12051 18236689 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
12052 18236689 : if (type_quals_attrs)
12053 : {
12054 18236689 : attrs = type_quals_attrs->attrs;
12055 18236689 : quals = quals_from_declspecs (type_quals_attrs);
12056 18236689 : if (attrs != NULL_TREE)
12057 6507 : itarget = build_attrs_declarator (attrs, target);
12058 : }
12059 18236689 : ret->kind = cdk_pointer;
12060 18236689 : ret->declarator = itarget;
12061 18236689 : ret->u.pointer_quals = quals;
12062 18236689 : return ret;
12063 : }
12064 :
12065 : /* Return a pointer to a structure for an empty list of declaration
12066 : specifiers. */
12067 :
12068 : struct c_declspecs *
12069 454963933 : build_null_declspecs (void)
12070 : {
12071 454963933 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
12072 454963933 : memset (ret, 0, sizeof *ret);
12073 454963933 : ret->align_log = -1;
12074 454963933 : ret->typespec_word = cts_none;
12075 454963933 : ret->storage_class = csc_none;
12076 454963933 : ret->expr_const_operands = true;
12077 454963933 : ret->typespec_kind = ctsk_none;
12078 454963933 : ret->address_space = ADDR_SPACE_GENERIC;
12079 454963933 : return ret;
12080 : }
12081 :
12082 : /* Add the address space ADDRSPACE to the declaration specifiers
12083 : SPECS, returning SPECS. */
12084 :
12085 : struct c_declspecs *
12086 177 : declspecs_add_addrspace (location_t location,
12087 : struct c_declspecs *specs, addr_space_t as)
12088 : {
12089 177 : specs->non_sc_seen_p = true;
12090 177 : specs->declspecs_seen_p = true;
12091 177 : specs->non_std_attrs_seen_p = true;
12092 :
12093 177 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
12094 0 : && specs->address_space != as)
12095 0 : error ("incompatible address space qualifiers %qs and %qs",
12096 : c_addr_space_name (as),
12097 : c_addr_space_name (specs->address_space));
12098 : else
12099 : {
12100 177 : specs->address_space = as;
12101 177 : specs->locations[cdw_address_space] = location;
12102 : }
12103 177 : return specs;
12104 : }
12105 :
12106 : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
12107 : returning SPECS. */
12108 :
12109 : struct c_declspecs *
12110 16584237 : declspecs_add_qual (location_t loc,
12111 : struct c_declspecs *specs, tree qual)
12112 : {
12113 16584237 : enum rid i;
12114 16584237 : bool dupe = false;
12115 16584237 : specs->non_sc_seen_p = true;
12116 16584237 : specs->declspecs_seen_p = true;
12117 16584237 : specs->non_std_attrs_seen_p = true;
12118 16584237 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
12119 : && C_IS_RESERVED_WORD (qual));
12120 16584237 : i = C_RID_CODE (qual);
12121 16584237 : location_t prev_loc = UNKNOWN_LOCATION;
12122 16584237 : switch (i)
12123 : {
12124 13085125 : case RID_CONST:
12125 13085125 : dupe = specs->const_p;
12126 13085125 : specs->const_p = true;
12127 13085125 : prev_loc = specs->locations[cdw_const];
12128 13085125 : specs->locations[cdw_const] = loc;
12129 13085125 : break;
12130 96055 : case RID_VOLATILE:
12131 96055 : dupe = specs->volatile_p;
12132 96055 : specs->volatile_p = true;
12133 96055 : prev_loc = specs->locations[cdw_volatile];
12134 96055 : specs->locations[cdw_volatile] = loc;
12135 96055 : break;
12136 3382657 : case RID_RESTRICT:
12137 3382657 : dupe = specs->restrict_p;
12138 3382657 : specs->restrict_p = true;
12139 3382657 : prev_loc = specs->locations[cdw_restrict];
12140 3382657 : specs->locations[cdw_restrict] = loc;
12141 3382657 : break;
12142 20400 : case RID_ATOMIC:
12143 20400 : dupe = specs->atomic_p;
12144 20400 : specs->atomic_p = true;
12145 20400 : prev_loc = specs->locations[cdw_atomic];
12146 20400 : specs->locations[cdw_atomic] = loc;
12147 20400 : break;
12148 0 : default:
12149 0 : gcc_unreachable ();
12150 : }
12151 16584237 : if (dupe)
12152 : {
12153 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
12154 : "duplicate %qE declaration specifier", qual);
12155 56 : if (!warned
12156 52 : && warn_duplicate_decl_specifier
12157 33 : && prev_loc >= RESERVED_LOCATION_COUNT
12158 33 : && !from_macro_expansion_at (prev_loc)
12159 77 : && !from_macro_expansion_at (loc))
12160 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
12161 : "duplicate %qE declaration specifier", qual);
12162 : }
12163 16584237 : return specs;
12164 : }
12165 :
12166 : /* Add the type specifier TYPE to the declaration specifiers SPECS,
12167 : returning SPECS. */
12168 :
12169 : struct c_declspecs *
12170 331700693 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
12171 : struct c_typespec spec)
12172 : {
12173 331700693 : tree type = spec.spec;
12174 331700693 : specs->non_sc_seen_p = true;
12175 331700693 : specs->declspecs_seen_p = true;
12176 331700693 : specs->non_std_attrs_seen_p = true;
12177 331700693 : specs->typespec_kind = spec.kind;
12178 331700693 : if (TREE_DEPRECATED (type))
12179 56 : specs->deprecated_p = true;
12180 331700693 : if (TREE_UNAVAILABLE (type))
12181 40 : specs->unavailable_p = true;
12182 :
12183 : /* As a type specifier is present, "auto" must be used as a storage
12184 : class specifier, not for type deduction. */
12185 331700693 : if (specs->c23_auto_p)
12186 : {
12187 115 : specs->c23_auto_p = false;
12188 115 : if (specs->storage_class != csc_none)
12189 1 : error ("multiple storage classes in declaration specifiers");
12190 114 : else if (specs->thread_p)
12191 1 : error ("%qs used with %<auto%>",
12192 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
12193 113 : else if (specs->constexpr_p)
12194 : /* auto may only be used with another storage class specifier,
12195 : such as constexpr, if the type is inferred. */
12196 2 : error ("%<auto%> used with %<constexpr%>");
12197 : else
12198 111 : specs->storage_class = csc_auto;
12199 : }
12200 :
12201 : /* Handle type specifier keywords. */
12202 331700693 : if (TREE_CODE (type) == IDENTIFIER_NODE
12203 82549044 : && C_IS_RESERVED_WORD (type)
12204 414249737 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12205 : {
12206 82549044 : enum rid i = C_RID_CODE (type);
12207 82549044 : if (specs->type)
12208 : {
12209 58 : error_at (loc, "two or more data types in declaration specifiers");
12210 58 : return specs;
12211 : }
12212 82548986 : if ((int) i <= (int) RID_LAST_MODIFIER)
12213 : {
12214 : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12215 21767081 : bool dupe = false;
12216 21767081 : switch (i)
12217 : {
12218 10730132 : case RID_LONG:
12219 10730132 : if (specs->long_long_p)
12220 : {
12221 104 : error_at (loc, "%<long long long%> is too long for GCC");
12222 104 : break;
12223 : }
12224 10730028 : if (specs->long_p)
12225 : {
12226 2950326 : if (specs->typespec_word == cts_double)
12227 : {
12228 15 : error_at (loc,
12229 : "both %qs and %qs in declaration specifiers",
12230 : "long long", "double");
12231 15 : break;
12232 : }
12233 2950311 : pedwarn_c90 (loc, OPT_Wlong_long,
12234 : "ISO C90 does not support %<long long%>");
12235 2950311 : specs->long_long_p = 1;
12236 2950311 : specs->locations[cdw_long_long] = loc;
12237 2950311 : break;
12238 : }
12239 7779702 : if (specs->short_p)
12240 77 : error_at (loc,
12241 : "both %qs and %qs in declaration specifiers",
12242 : "long", "short");
12243 7779625 : else if (specs->typespec_word == cts_auto_type)
12244 1 : error_at (loc,
12245 : "both %qs and %qs in declaration specifiers",
12246 : "long", "__auto_type");
12247 : else if (specs->typespec_word == cts_void)
12248 5 : error_at (loc,
12249 : "both %qs and %qs in declaration specifiers",
12250 : "long", "void");
12251 : else if (specs->typespec_word == cts_int_n)
12252 19 : error_at (loc,
12253 : "both %qs and %<__int%d%> in declaration specifiers",
12254 19 : "long", int_n_data[specs->u.int_n_idx].bitsize);
12255 : else if (specs->typespec_word == cts_bool)
12256 3 : error_at (loc,
12257 : "both %qs and %qs in declaration specifiers",
12258 : "long", "_Bool");
12259 : else if (specs->typespec_word == cts_bitint)
12260 3 : error_at (loc,
12261 : "both %qs and %qs in declaration specifiers",
12262 : "long", "_BitInt");
12263 : else if (specs->typespec_word == cts_char)
12264 21 : error_at (loc,
12265 : "both %qs and %qs in declaration specifiers",
12266 : "long", "char");
12267 : else if (specs->typespec_word == cts_float)
12268 7 : error_at (loc,
12269 : "both %qs and %qs in declaration specifiers",
12270 : "long", "float");
12271 : else if (specs->typespec_word == cts_floatn_nx)
12272 0 : error_at (loc,
12273 : "both %qs and %<_Float%d%s%> in declaration "
12274 : "specifiers", "long",
12275 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12276 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12277 : ? "x"
12278 : : "");
12279 : else if (specs->typespec_word == cts_dfloat32)
12280 4 : error_at (loc,
12281 : "both %qs and %qs in declaration specifiers",
12282 : "long", "_Decimal32");
12283 : else if (specs->typespec_word == cts_dfloat64)
12284 4 : error_at (loc,
12285 : "both %qs and %qs in declaration specifiers",
12286 : "long", "_Decimal64");
12287 : else if (specs->typespec_word == cts_dfloat128)
12288 4 : error_at (loc,
12289 : "both %qs and %qs in declaration specifiers",
12290 : "long", "_Decimal128");
12291 : else if (specs->typespec_word == cts_dfloat64x)
12292 0 : error_at (loc,
12293 : "both %qs and %qs in declaration specifiers",
12294 : "long", "_Decimal64x");
12295 : else
12296 : {
12297 7779554 : specs->long_p = true;
12298 7779554 : specs->locations[cdw_long] = loc;
12299 : }
12300 : break;
12301 1702694 : case RID_SHORT:
12302 1702694 : dupe = specs->short_p;
12303 1702694 : if (specs->long_p)
12304 197 : error_at (loc,
12305 : "both %qs and %qs in declaration specifiers",
12306 : "long", "short");
12307 1702497 : else if (specs->typespec_word == cts_auto_type)
12308 1 : error_at (loc,
12309 : "both %qs and %qs in declaration specifiers",
12310 : "short", "__auto_type");
12311 : else if (specs->typespec_word == cts_void)
12312 5 : error_at (loc,
12313 : "both %qs and %qs in declaration specifiers",
12314 : "short", "void");
12315 : else if (specs->typespec_word == cts_int_n)
12316 19 : error_at (loc,
12317 : "both %qs and %<__int%d%> in declaration specifiers",
12318 19 : "short", int_n_data[specs->u.int_n_idx].bitsize);
12319 : else if (specs->typespec_word == cts_bool)
12320 3 : error_at (loc,
12321 : "both %qs and %qs in declaration specifiers",
12322 : "short", "_Bool");
12323 : else if (specs->typespec_word == cts_bitint)
12324 1 : error_at (loc,
12325 : "both %qs and %qs in declaration specifiers",
12326 : "short", "_BitInt");
12327 : else if (specs->typespec_word == cts_char)
12328 21 : error_at (loc,
12329 : "both %qs and %qs in declaration specifiers",
12330 : "short", "char");
12331 : else if (specs->typespec_word == cts_float)
12332 7 : error_at (loc,
12333 : "both %qs and %qs in declaration specifiers",
12334 : "short", "float");
12335 : else if (specs->typespec_word == cts_double)
12336 7 : error_at (loc,
12337 : "both %qs and %qs in declaration specifiers",
12338 : "short", "double");
12339 : else if (specs->typespec_word == cts_floatn_nx)
12340 0 : error_at (loc,
12341 : "both %qs and %<_Float%d%s%> in declaration "
12342 : "specifiers", "short",
12343 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12344 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12345 : ? "x"
12346 : : "");
12347 : else if (specs->typespec_word == cts_dfloat32)
12348 4 : error_at (loc,
12349 : "both %qs and %qs in declaration specifiers",
12350 : "short", "_Decimal32");
12351 : else if (specs->typespec_word == cts_dfloat64)
12352 4 : error_at (loc,
12353 : "both %qs and %qs in declaration specifiers",
12354 : "short", "_Decimal64");
12355 : else if (specs->typespec_word == cts_dfloat128)
12356 4 : error_at (loc,
12357 : "both %qs and %qs in declaration specifiers",
12358 : "short", "_Decimal128");
12359 : else if (specs->typespec_word == cts_dfloat64x)
12360 0 : error_at (loc,
12361 : "both %qs and %qs in declaration specifiers",
12362 : "short", "_Decimal64x");
12363 : else
12364 : {
12365 1702421 : specs->short_p = true;
12366 1702421 : specs->locations[cdw_short] = loc;
12367 : }
12368 : break;
12369 574041 : case RID_SIGNED:
12370 574041 : dupe = specs->signed_p;
12371 574041 : if (specs->unsigned_p)
12372 138 : error_at (loc,
12373 : "both %qs and %qs in declaration specifiers",
12374 : "signed", "unsigned");
12375 573903 : else if (specs->typespec_word == cts_auto_type)
12376 1 : error_at (loc,
12377 : "both %qs and %qs in declaration specifiers",
12378 : "signed", "__auto_type");
12379 : else if (specs->typespec_word == cts_void)
12380 5 : error_at (loc,
12381 : "both %qs and %qs in declaration specifiers",
12382 : "signed", "void");
12383 : else if (specs->typespec_word == cts_bool)
12384 3 : error_at (loc,
12385 : "both %qs and %qs in declaration specifiers",
12386 : "signed", "_Bool");
12387 : else if (specs->typespec_word == cts_float)
12388 7 : error_at (loc,
12389 : "both %qs and %qs in declaration specifiers",
12390 : "signed", "float");
12391 : else if (specs->typespec_word == cts_double)
12392 21 : error_at (loc,
12393 : "both %qs and %qs in declaration specifiers",
12394 : "signed", "double");
12395 : else if (specs->typespec_word == cts_floatn_nx)
12396 0 : error_at (loc,
12397 : "both %qs and %<_Float%d%s%> in declaration "
12398 : "specifiers", "signed",
12399 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12400 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12401 : ? "x"
12402 : : "");
12403 : else if (specs->typespec_word == cts_dfloat32)
12404 4 : error_at (loc,
12405 : "both %qs and %qs in declaration specifiers",
12406 : "signed", "_Decimal32");
12407 : else if (specs->typespec_word == cts_dfloat64)
12408 4 : error_at (loc,
12409 : "both %qs and %qs in declaration specifiers",
12410 : "signed", "_Decimal64");
12411 : else if (specs->typespec_word == cts_dfloat128)
12412 4 : error_at (loc,
12413 : "both %qs and %qs in declaration specifiers",
12414 : "signed", "_Decimal128");
12415 : else if (specs->typespec_word == cts_dfloat64x)
12416 0 : error_at (loc,
12417 : "both %qs and %qs in declaration specifiers",
12418 : "signed", "_Decimal64x");
12419 : else
12420 : {
12421 573854 : specs->signed_p = true;
12422 573854 : specs->locations[cdw_signed] = loc;
12423 : }
12424 : break;
12425 6181747 : case RID_UNSIGNED:
12426 6181747 : dupe = specs->unsigned_p;
12427 6181747 : if (specs->signed_p)
12428 139 : error_at (loc,
12429 : "both %qs and %qs in declaration specifiers",
12430 : "signed", "unsigned");
12431 6181608 : else if (specs->typespec_word == cts_auto_type)
12432 1 : error_at (loc,
12433 : "both %qs and %qs in declaration specifiers",
12434 : "unsigned", "__auto_type");
12435 : else if (specs->typespec_word == cts_void)
12436 5 : error_at (loc,
12437 : "both %qs and %qs in declaration specifiers",
12438 : "unsigned", "void");
12439 : else if (specs->typespec_word == cts_bool)
12440 3 : error_at (loc,
12441 : "both %qs and %qs in declaration specifiers",
12442 : "unsigned", "_Bool");
12443 : else if (specs->typespec_word == cts_float)
12444 7 : error_at (loc,
12445 : "both %qs and %qs in declaration specifiers",
12446 : "unsigned", "float");
12447 : else if (specs->typespec_word == cts_double)
12448 21 : error_at (loc,
12449 : "both %qs and %qs in declaration specifiers",
12450 : "unsigned", "double");
12451 : else if (specs->typespec_word == cts_floatn_nx)
12452 0 : error_at (loc,
12453 : "both %qs and %<_Float%d%s%> in declaration "
12454 : "specifiers", "unsigned",
12455 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12456 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12457 : ? "x"
12458 : : "");
12459 : else if (specs->typespec_word == cts_dfloat32)
12460 2 : error_at (loc,
12461 : "both %qs and %qs in declaration specifiers",
12462 : "unsigned", "_Decimal32");
12463 : else if (specs->typespec_word == cts_dfloat64)
12464 2 : error_at (loc,
12465 : "both %qs and %qs in declaration specifiers",
12466 : "unsigned", "_Decimal64");
12467 : else if (specs->typespec_word == cts_dfloat128)
12468 2 : error_at (loc,
12469 : "both %qs and %qs in declaration specifiers",
12470 : "unsigned", "_Decimal128");
12471 : else if (specs->typespec_word == cts_dfloat64x)
12472 0 : error_at (loc,
12473 : "both %qs and %qs in declaration specifiers",
12474 : "unsigned", "_Decimal64x");
12475 : else
12476 : {
12477 6181565 : specs->unsigned_p = true;
12478 6181565 : specs->locations[cdw_unsigned] = loc;
12479 : }
12480 : break;
12481 2578424 : case RID_COMPLEX:
12482 2578424 : dupe = specs->complex_p;
12483 2578424 : if (!in_system_header_at (loc))
12484 69038 : pedwarn_c90 (loc, OPT_Wpedantic,
12485 : "ISO C90 does not support complex types");
12486 2578424 : if (specs->typespec_word == cts_auto_type)
12487 1 : error_at (loc,
12488 : "both %qs and %qs in declaration specifiers",
12489 : "complex", "__auto_type");
12490 : else if (specs->typespec_word == cts_void)
12491 2 : error_at (loc,
12492 : "both %qs and %qs in declaration specifiers",
12493 : "complex", "void");
12494 : else if (specs->typespec_word == cts_bool)
12495 2 : error_at (loc,
12496 : "both %qs and %qs in declaration specifiers",
12497 : "complex", "_Bool");
12498 : else if (specs->typespec_word == cts_bitint)
12499 1 : error_at (loc,
12500 : "both %qs and %qs in declaration specifiers",
12501 : "complex", "_BitInt");
12502 : else if (specs->typespec_word == cts_dfloat32)
12503 1 : error_at (loc,
12504 : "both %qs and %qs in declaration specifiers",
12505 : "complex", "_Decimal32");
12506 : else if (specs->typespec_word == cts_dfloat64)
12507 1 : error_at (loc,
12508 : "both %qs and %qs in declaration specifiers",
12509 : "complex", "_Decimal64");
12510 : else if (specs->typespec_word == cts_dfloat128)
12511 1 : error_at (loc,
12512 : "both %qs and %qs in declaration specifiers",
12513 : "complex", "_Decimal128");
12514 : else if (specs->typespec_word == cts_dfloat64x)
12515 0 : error_at (loc,
12516 : "both %qs and %qs in declaration specifiers",
12517 : "complex", "_Decimal64x");
12518 : else if (specs->typespec_word == cts_fract)
12519 0 : error_at (loc,
12520 : "both %qs and %qs in declaration specifiers",
12521 : "complex", "_Fract");
12522 : else if (specs->typespec_word == cts_accum)
12523 0 : error_at (loc,
12524 : "both %qs and %qs in declaration specifiers",
12525 : "complex", "_Accum");
12526 2578415 : else if (specs->saturating_p)
12527 0 : error_at (loc,
12528 : "both %qs and %qs in declaration specifiers",
12529 : "complex", "_Sat");
12530 : else
12531 : {
12532 2578415 : specs->complex_p = true;
12533 2578415 : specs->locations[cdw_complex] = loc;
12534 : }
12535 : break;
12536 43 : case RID_SAT:
12537 43 : dupe = specs->saturating_p;
12538 43 : pedwarn (loc, OPT_Wpedantic,
12539 : "ISO C does not support saturating types");
12540 43 : if (specs->typespec_word == cts_int_n)
12541 0 : error_at (loc,
12542 : "both %qs and %<__int%d%> in declaration specifiers",
12543 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12544 : else if (specs->typespec_word == cts_auto_type)
12545 0 : error_at (loc,
12546 : "both %qs and %qs in declaration specifiers",
12547 : "_Sat", "__auto_type");
12548 : else if (specs->typespec_word == cts_void)
12549 0 : error_at (loc,
12550 : "both %qs and %qs in declaration specifiers",
12551 : "_Sat", "void");
12552 : else if (specs->typespec_word == cts_bool)
12553 0 : error_at (loc,
12554 : "both %qs and %qs in declaration specifiers",
12555 : "_Sat", "_Bool");
12556 : else if (specs->typespec_word == cts_bitint)
12557 0 : error_at (loc,
12558 : "both %qs and %qs in declaration specifiers",
12559 : "_Sat", "_BitInt");
12560 : else if (specs->typespec_word == cts_char)
12561 0 : error_at (loc,
12562 : "both %qs and %qs in declaration specifiers",
12563 : "_Sat", "char");
12564 : else if (specs->typespec_word == cts_int)
12565 0 : error_at (loc,
12566 : "both %qs and %qs in declaration specifiers",
12567 : "_Sat", "int");
12568 : else if (specs->typespec_word == cts_float)
12569 0 : error_at (loc,
12570 : "both %qs and %qs in declaration specifiers",
12571 : "_Sat", "float");
12572 : else if (specs->typespec_word == cts_double)
12573 0 : error_at (loc,
12574 : "both %qs and %qs in declaration specifiers",
12575 : "_Sat", "double");
12576 : else if (specs->typespec_word == cts_floatn_nx)
12577 0 : error_at (loc,
12578 : "both %qs and %<_Float%d%s%> in declaration "
12579 : "specifiers", "_Sat",
12580 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12581 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12582 : ? "x"
12583 : : "");
12584 : else if (specs->typespec_word == cts_dfloat32)
12585 0 : error_at (loc,
12586 : "both %qs and %qs in declaration specifiers",
12587 : "_Sat", "_Decimal32");
12588 : else if (specs->typespec_word == cts_dfloat64)
12589 0 : error_at (loc,
12590 : "both %qs and %qs in declaration specifiers",
12591 : "_Sat", "_Decimal64");
12592 : else if (specs->typespec_word == cts_dfloat128)
12593 0 : error_at (loc,
12594 : "both %qs and %qs in declaration specifiers",
12595 : "_Sat", "_Decimal128");
12596 : else if (specs->typespec_word == cts_dfloat64x)
12597 0 : error_at (loc,
12598 : "both %qs and %qs in declaration specifiers",
12599 : "_Sat", "_Decimal64x");
12600 43 : else if (specs->complex_p)
12601 0 : error_at (loc,
12602 : "both %qs and %qs in declaration specifiers",
12603 : "_Sat", "complex");
12604 : else
12605 : {
12606 43 : specs->saturating_p = true;
12607 43 : specs->locations[cdw_saturating] = loc;
12608 : }
12609 : break;
12610 0 : default:
12611 0 : gcc_unreachable ();
12612 : }
12613 :
12614 21767081 : if (dupe)
12615 378 : error_at (loc, "duplicate %qE", type);
12616 :
12617 21767081 : return specs;
12618 : }
12619 : else
12620 : {
12621 : /* "void", "_Bool", "char", "int", "float", "double",
12622 : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12623 : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12624 : "__auto_type". */
12625 60781905 : if (specs->typespec_word != cts_none)
12626 : {
12627 2171 : if (i == RID_BOOL)
12628 : {
12629 175 : auto_diagnostic_group d;
12630 175 : if (specs->storage_class == csc_typedef)
12631 4 : error_at (loc,
12632 : "%qs cannot be defined via %<typedef%>",
12633 2 : IDENTIFIER_POINTER (type));
12634 : else
12635 346 : error_at (loc,
12636 : "%qs cannot be used here",
12637 173 : IDENTIFIER_POINTER (type));
12638 175 : add_note_about_new_keyword (loc, type);
12639 175 : }
12640 : else
12641 1996 : error_at (loc,
12642 : "two or more data types in declaration specifiers");
12643 2171 : return specs;
12644 : }
12645 60779734 : switch (i)
12646 : {
12647 1890 : case RID_AUTO_TYPE:
12648 1890 : if (specs->long_p)
12649 1 : error_at (loc,
12650 : "both %qs and %qs in declaration specifiers",
12651 : "long", "__auto_type");
12652 1889 : else if (specs->short_p)
12653 1 : error_at (loc,
12654 : "both %qs and %qs in declaration specifiers",
12655 : "short", "__auto_type");
12656 1888 : else if (specs->signed_p)
12657 1 : error_at (loc,
12658 : "both %qs and %qs in declaration specifiers",
12659 : "signed", "__auto_type");
12660 1887 : else if (specs->unsigned_p)
12661 1 : error_at (loc,
12662 : "both %qs and %qs in declaration specifiers",
12663 : "unsigned", "__auto_type");
12664 1886 : else if (specs->complex_p)
12665 1 : error_at (loc,
12666 : "both %qs and %qs in declaration specifiers",
12667 : "complex", "__auto_type");
12668 1885 : else if (specs->saturating_p)
12669 0 : error_at (loc,
12670 : "both %qs and %qs in declaration specifiers",
12671 : "_Sat", "__auto_type");
12672 : else
12673 : {
12674 1885 : specs->typespec_word = cts_auto_type;
12675 1885 : specs->locations[cdw_typespec] = loc;
12676 : }
12677 1890 : return specs;
12678 51755 : case RID_INT_N_0:
12679 51755 : case RID_INT_N_1:
12680 51755 : case RID_INT_N_2:
12681 51755 : case RID_INT_N_3:
12682 51755 : specs->u.int_n_idx = i - RID_INT_N_0;
12683 51755 : if (!in_system_header_at (input_location)
12684 : /* If the INT_N type ends in "__", and so is of the format
12685 : "__intN__", don't pedwarn. */
12686 51755 : && (strncmp (IDENTIFIER_POINTER (type)
12687 41141 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12688 41141 : pedwarn (loc, OPT_Wpedantic,
12689 : "ISO C does not support %<__int%d%> types",
12690 41141 : int_n_data[specs->u.int_n_idx].bitsize);
12691 :
12692 51755 : if (specs->long_p)
12693 53 : error_at (loc,
12694 : "both %<__int%d%> and %qs in declaration specifiers",
12695 53 : int_n_data[specs->u.int_n_idx].bitsize, "long");
12696 51702 : else if (specs->saturating_p)
12697 0 : error_at (loc,
12698 : "both %qs and %<__int%d%> in declaration specifiers",
12699 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12700 51702 : else if (specs->short_p)
12701 19 : error_at (loc,
12702 : "both %<__int%d%> and %qs in declaration specifiers",
12703 19 : int_n_data[specs->u.int_n_idx].bitsize, "short");
12704 51683 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12705 : {
12706 0 : specs->typespec_word = cts_int_n;
12707 0 : error_at (loc,
12708 : "%<__int%d%> is not supported on this target",
12709 0 : int_n_data[specs->u.int_n_idx].bitsize);
12710 : }
12711 : else
12712 : {
12713 51683 : specs->typespec_word = cts_int_n;
12714 51683 : specs->locations[cdw_typespec] = loc;
12715 : }
12716 51755 : return specs;
12717 7852818 : case RID_VOID:
12718 7852818 : if (specs->long_p)
12719 44 : error_at (loc,
12720 : "both %qs and %qs in declaration specifiers",
12721 : "long", "void");
12722 7852774 : else if (specs->short_p)
12723 21 : error_at (loc,
12724 : "both %qs and %qs in declaration specifiers",
12725 : "short", "void");
12726 7852753 : else if (specs->signed_p)
12727 5 : error_at (loc,
12728 : "both %qs and %qs in declaration specifiers",
12729 : "signed", "void");
12730 7852748 : else if (specs->unsigned_p)
12731 5 : error_at (loc,
12732 : "both %qs and %qs in declaration specifiers",
12733 : "unsigned", "void");
12734 7852743 : else if (specs->complex_p)
12735 2 : error_at (loc,
12736 : "both %qs and %qs in declaration specifiers",
12737 : "complex", "void");
12738 7852741 : else if (specs->saturating_p)
12739 0 : error_at (loc,
12740 : "both %qs and %qs in declaration specifiers",
12741 : "_Sat", "void");
12742 : else
12743 : {
12744 7852741 : specs->typespec_word = cts_void;
12745 7852741 : specs->locations[cdw_typespec] = loc;
12746 : }
12747 7852818 : return specs;
12748 85918 : case RID_BOOL:
12749 85918 : if (!in_system_header_at (loc))
12750 71435 : pedwarn_c90 (loc, OPT_Wpedantic,
12751 : "ISO C90 does not support boolean types");
12752 85918 : if (specs->long_p)
12753 27 : error_at (loc,
12754 : "both %qs and %qs in declaration specifiers",
12755 : "long", "_Bool");
12756 85891 : else if (specs->short_p)
12757 11 : error_at (loc,
12758 : "both %qs and %qs in declaration specifiers",
12759 : "short", "_Bool");
12760 85880 : else if (specs->signed_p)
12761 3 : error_at (loc,
12762 : "both %qs and %qs in declaration specifiers",
12763 : "signed", "_Bool");
12764 85877 : else if (specs->unsigned_p)
12765 3 : error_at (loc,
12766 : "both %qs and %qs in declaration specifiers",
12767 : "unsigned", "_Bool");
12768 85874 : else if (specs->complex_p)
12769 2 : error_at (loc,
12770 : "both %qs and %qs in declaration specifiers",
12771 : "complex", "_Bool");
12772 85872 : else if (specs->saturating_p)
12773 0 : error_at (loc,
12774 : "both %qs and %qs in declaration specifiers",
12775 : "_Sat", "_Bool");
12776 : else
12777 : {
12778 85872 : specs->typespec_word = cts_bool;
12779 85872 : specs->locations[cdw_typespec] = loc;
12780 : }
12781 85918 : return specs;
12782 9055880 : case RID_CHAR:
12783 9055880 : if (specs->long_p)
12784 44 : error_at (loc,
12785 : "both %qs and %qs in declaration specifiers",
12786 : "long", "char");
12787 9055836 : else if (specs->short_p)
12788 21 : error_at (loc,
12789 : "both %qs and %qs in declaration specifiers",
12790 : "short", "char");
12791 9055815 : else if (specs->saturating_p)
12792 0 : error_at (loc,
12793 : "both %qs and %qs in declaration specifiers",
12794 : "_Sat", "char");
12795 : else
12796 : {
12797 9055815 : specs->typespec_word = cts_char;
12798 9055815 : specs->locations[cdw_typespec] = loc;
12799 : }
12800 9055880 : return specs;
12801 23855457 : case RID_INT:
12802 23855457 : if (specs->saturating_p)
12803 0 : error_at (loc,
12804 : "both %qs and %qs in declaration specifiers",
12805 : "_Sat", "int");
12806 : else
12807 : {
12808 23855457 : specs->typespec_word = cts_int;
12809 23855457 : specs->locations[cdw_typespec] = loc;
12810 : }
12811 23855457 : return specs;
12812 3392293 : case RID_FLOAT:
12813 3392293 : if (specs->long_p)
12814 44 : error_at (loc,
12815 : "both %qs and %qs in declaration specifiers",
12816 : "long", "float");
12817 3392249 : else if (specs->short_p)
12818 21 : error_at (loc,
12819 : "both %qs and %qs in declaration specifiers",
12820 : "short", "float");
12821 3392228 : else if (specs->signed_p)
12822 5 : error_at (loc,
12823 : "both %qs and %qs in declaration specifiers",
12824 : "signed", "float");
12825 3392223 : else if (specs->unsigned_p)
12826 5 : error_at (loc,
12827 : "both %qs and %qs in declaration specifiers",
12828 : "unsigned", "float");
12829 3392218 : else if (specs->saturating_p)
12830 0 : error_at (loc,
12831 : "both %qs and %qs in declaration specifiers",
12832 : "_Sat", "float");
12833 : else
12834 : {
12835 3392218 : specs->typespec_word = cts_float;
12836 3392218 : specs->locations[cdw_typespec] = loc;
12837 : }
12838 3392293 : return specs;
12839 6108855 : case RID_DOUBLE:
12840 6108855 : if (specs->long_long_p)
12841 22 : error_at (loc,
12842 : "both %qs and %qs in declaration specifiers",
12843 : "long long", "double");
12844 6108833 : else if (specs->short_p)
12845 21 : error_at (loc,
12846 : "both %qs and %qs in declaration specifiers",
12847 : "short", "double");
12848 6108812 : else if (specs->signed_p)
12849 13 : error_at (loc,
12850 : "both %qs and %qs in declaration specifiers",
12851 : "signed", "double");
12852 6108799 : else if (specs->unsigned_p)
12853 13 : error_at (loc,
12854 : "both %qs and %qs in declaration specifiers",
12855 : "unsigned", "double");
12856 6108786 : else if (specs->saturating_p)
12857 0 : error_at (loc,
12858 : "both %qs and %qs in declaration specifiers",
12859 : "_Sat", "double");
12860 : else
12861 : {
12862 6108786 : specs->typespec_word = cts_double;
12863 6108786 : specs->locations[cdw_typespec] = loc;
12864 : }
12865 6108855 : return specs;
12866 10281310 : CASE_RID_FLOATN_NX:
12867 10281310 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12868 10281310 : if (!in_system_header_at (input_location))
12869 53739 : pedwarn_c11 (loc, OPT_Wpedantic,
12870 : "ISO C does not support the %<_Float%d%s%> type"
12871 : " before C23",
12872 53739 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12873 53739 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12874 : ? "x"
12875 : : "");
12876 :
12877 10281310 : if (specs->long_p)
12878 0 : error_at (loc,
12879 : "both %qs and %<_Float%d%s%> in declaration "
12880 : "specifiers", "long",
12881 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12882 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12883 : ? "x"
12884 : : "");
12885 10281310 : else if (specs->short_p)
12886 0 : error_at (loc,
12887 : "both %qs and %<_Float%d%s%> in declaration "
12888 : "specifiers", "short",
12889 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12890 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12891 : ? "x"
12892 : : "");
12893 10281310 : else if (specs->signed_p)
12894 0 : error_at (loc,
12895 : "both %qs and %<_Float%d%s%> in declaration "
12896 : "specifiers", "signed",
12897 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12898 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12899 : ? "x"
12900 : : "");
12901 10281310 : else if (specs->unsigned_p)
12902 0 : error_at (loc,
12903 : "both %qs and %<_Float%d%s%> in declaration "
12904 : "specifiers", "unsigned",
12905 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12906 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12907 : ? "x"
12908 : : "");
12909 10281310 : else if (specs->saturating_p)
12910 0 : error_at (loc,
12911 : "both %qs and %<_Float%d%s%> in declaration "
12912 : "specifiers", "_Sat",
12913 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12914 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12915 : ? "x"
12916 : : "");
12917 10281310 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12918 : {
12919 88 : specs->typespec_word = cts_floatn_nx;
12920 176 : error_at (loc,
12921 : "%<_Float%d%s%> is not supported on this target",
12922 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12923 88 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12924 : ? "x"
12925 : : "");
12926 : }
12927 : else
12928 : {
12929 10281222 : specs->typespec_word = cts_floatn_nx;
12930 10281222 : specs->locations[cdw_typespec] = loc;
12931 : }
12932 10281310 : return specs;
12933 47640 : case RID_DFLOAT32:
12934 47640 : case RID_DFLOAT64:
12935 47640 : case RID_DFLOAT128:
12936 47640 : case RID_DFLOAT64X:
12937 47640 : {
12938 47640 : const char *str;
12939 47640 : if (i == RID_DFLOAT32)
12940 : str = "_Decimal32";
12941 : else if (i == RID_DFLOAT64)
12942 : str = "_Decimal64";
12943 : else if (i == RID_DFLOAT128)
12944 : str = "_Decimal128";
12945 : else
12946 47640 : str = "_Decimal64x";
12947 47640 : if (specs->long_long_p)
12948 18 : error_at (loc,
12949 : "both %qs and %qs in declaration specifiers",
12950 : "long long", str);
12951 47640 : if (specs->long_p)
12952 33 : error_at (loc,
12953 : "both %qs and %qs in declaration specifiers",
12954 : "long", str);
12955 47607 : else if (specs->short_p)
12956 18 : error_at (loc,
12957 : "both %qs and %qs in declaration specifiers",
12958 : "short", str);
12959 47589 : else if (specs->signed_p)
12960 6 : error_at (loc,
12961 : "both %qs and %qs in declaration specifiers",
12962 : "signed", str);
12963 47583 : else if (specs->unsigned_p)
12964 3 : error_at (loc,
12965 : "both %qs and %qs in declaration specifiers",
12966 : "unsigned", str);
12967 47580 : else if (specs->complex_p)
12968 3 : error_at (loc,
12969 : "both %qs and %qs in declaration specifiers",
12970 : "complex", str);
12971 47577 : else if (specs->saturating_p)
12972 0 : error_at (loc,
12973 : "both %qs and %qs in declaration specifiers",
12974 : "_Sat", str);
12975 47577 : else if (i == RID_DFLOAT32)
12976 15957 : specs->typespec_word = cts_dfloat32;
12977 31620 : else if (i == RID_DFLOAT64)
12978 15839 : specs->typespec_word = cts_dfloat64;
12979 15781 : else if (i == RID_DFLOAT128)
12980 15734 : specs->typespec_word = cts_dfloat128;
12981 : else
12982 47 : specs->typespec_word = cts_dfloat64x;
12983 47640 : specs->locations[cdw_typespec] = loc;
12984 : }
12985 47640 : if (!targetm.decimal_float_supported_p ())
12986 0 : error_at (loc,
12987 : "decimal floating-point not supported "
12988 : "for this target");
12989 47640 : pedwarn_c11 (loc, OPT_Wpedantic,
12990 : "ISO C does not support decimal floating-point "
12991 : "before C23");
12992 47640 : return specs;
12993 45 : case RID_FRACT:
12994 45 : case RID_ACCUM:
12995 45 : {
12996 45 : const char *str;
12997 45 : if (i == RID_FRACT)
12998 : str = "_Fract";
12999 : else
13000 22 : str = "_Accum";
13001 45 : if (specs->complex_p)
13002 0 : error_at (loc,
13003 : "both %qs and %qs in declaration specifiers",
13004 : "complex", str);
13005 45 : else if (i == RID_FRACT)
13006 23 : specs->typespec_word = cts_fract;
13007 : else
13008 22 : specs->typespec_word = cts_accum;
13009 45 : specs->locations[cdw_typespec] = loc;
13010 : }
13011 45 : if (!targetm.fixed_point_supported_p ())
13012 45 : error_at (loc,
13013 : "fixed-point types not supported for this target");
13014 45 : pedwarn (loc, OPT_Wpedantic,
13015 : "ISO C does not support fixed-point types");
13016 45 : return specs;
13017 45873 : case RID_BITINT:
13018 45873 : if (specs->long_p)
13019 2 : error_at (loc,
13020 : "both %qs and %qs in declaration specifiers",
13021 : "long", "_BitInt");
13022 45871 : else if (specs->short_p)
13023 1 : error_at (loc,
13024 : "both %qs and %qs in declaration specifiers",
13025 : "short", "_BitInt");
13026 45870 : else if (specs->complex_p)
13027 1 : error_at (loc,
13028 : "both %qs and %qs in declaration specifiers",
13029 : "complex", "_BitInt");
13030 45869 : else if (specs->saturating_p)
13031 0 : error_at (loc,
13032 : "both %qs and %qs in declaration specifiers",
13033 : "_Sat", "_BitInt");
13034 : else
13035 : {
13036 45869 : specs->typespec_word = cts_bitint;
13037 45869 : specs->locations[cdw_typespec] = loc;
13038 45869 : specs->u.bitint_prec = -1;
13039 45869 : if (error_operand_p (spec.expr))
13040 7 : return specs;
13041 45869 : if (TREE_CODE (spec.expr) != INTEGER_CST
13042 45869 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
13043 : {
13044 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
13045 : "constant expression");
13046 1 : return specs;
13047 : }
13048 45868 : if (tree_int_cst_sgn (spec.expr) <= 0)
13049 : {
13050 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
13051 : "positive integer constant expression",
13052 : spec.expr);
13053 4 : return specs;
13054 : }
13055 45864 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
13056 : {
13057 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
13058 : "%<BITINT_MAXWIDTH%> %qd",
13059 : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
13060 2 : return specs;
13061 : }
13062 45862 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
13063 45862 : struct bitint_info info;
13064 45862 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
13065 : &info))
13066 : {
13067 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
13068 : "this target", specs->u.bitint_prec);
13069 0 : specs->u.bitint_prec = -1;
13070 0 : return specs;
13071 : }
13072 : }
13073 45866 : return specs;
13074 : default:
13075 : /* ObjC reserved word "id", handled below. */
13076 : break;
13077 : }
13078 : }
13079 : }
13080 :
13081 : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
13082 : form of ObjC type, cases such as "int" and "long" being handled
13083 : above), a TYPE (struct, union, enum and typeof specifiers) or an
13084 : ERROR_MARK. In none of these cases may there have previously
13085 : been any type specifiers. */
13086 249151649 : if (specs->type || specs->typespec_word != cts_none
13087 249151637 : || specs->long_p || specs->short_p || specs->signed_p
13088 249151634 : || specs->unsigned_p || specs->complex_p)
13089 21 : error_at (loc, "two or more data types in declaration specifiers");
13090 249151628 : else if (TREE_CODE (type) == TYPE_DECL)
13091 : {
13092 245873251 : specs->type = TREE_TYPE (type);
13093 245873251 : if (TREE_TYPE (type) != error_mark_node)
13094 : {
13095 245873236 : mark_decl_used (type, false);
13096 245873236 : specs->decl_attr = DECL_ATTRIBUTES (type);
13097 245873236 : specs->typedef_p = true;
13098 245873236 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
13099 245873236 : specs->locations[cdw_typedef] = loc;
13100 :
13101 : /* If this typedef name is defined in a struct, then a C++
13102 : lookup would return a different value. */
13103 245873236 : if (warn_cxx_compat
13104 245873236 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
13105 2 : warning_at (loc, OPT_Wc___compat,
13106 : "C++ lookup of %qD would return a field, not a type",
13107 : type);
13108 :
13109 : /* If we are parsing a struct, record that a struct field
13110 : used a typedef. */
13111 245873236 : if (warn_cxx_compat && struct_parse_info != NULL)
13112 1751 : struct_parse_info->typedefs_seen.safe_push (type);
13113 : }
13114 : }
13115 3278377 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
13116 : {
13117 0 : tree t = lookup_name (type);
13118 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
13119 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
13120 0 : else if (TREE_TYPE (t) == error_mark_node)
13121 : ;
13122 : else
13123 : {
13124 0 : specs->type = TREE_TYPE (t);
13125 0 : specs->locations[cdw_typespec] = loc;
13126 : }
13127 : }
13128 : else
13129 : {
13130 3278377 : if (TREE_CODE (type) != ERROR_MARK)
13131 : {
13132 3278171 : if (spec.kind == ctsk_typeof)
13133 : {
13134 833248 : specs->typedef_p = true;
13135 833248 : specs->locations[cdw_typedef] = loc;
13136 : }
13137 3278171 : if (spec.expr)
13138 : {
13139 948 : if (specs->expr)
13140 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
13141 : specs->expr, spec.expr);
13142 : else
13143 948 : specs->expr = spec.expr;
13144 948 : specs->expr_const_operands &= spec.expr_const_operands;
13145 : }
13146 : }
13147 3278377 : specs->type = type;
13148 3278377 : if (spec.has_enum_type_specifier
13149 169 : && spec.kind != ctsk_tagdef)
13150 49 : specs->enum_type_specifier_ref_p = true;
13151 : }
13152 :
13153 : return specs;
13154 : }
13155 :
13156 : /* Add the storage class specifier or function specifier SCSPEC to the
13157 : declaration specifiers SPECS, returning SPECS. */
13158 :
13159 : struct c_declspecs *
13160 90358142 : declspecs_add_scspec (location_t loc,
13161 : struct c_declspecs *specs,
13162 : tree scspec)
13163 : {
13164 90358142 : enum rid i;
13165 90358142 : enum c_storage_class n = csc_none;
13166 90358142 : bool dupe = false;
13167 90358142 : specs->declspecs_seen_p = true;
13168 90358142 : specs->non_std_attrs_seen_p = true;
13169 90358142 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13170 : && C_IS_RESERVED_WORD (scspec));
13171 90358142 : i = C_RID_CODE (scspec);
13172 90358142 : if (specs->non_sc_seen_p)
13173 2096 : warning (OPT_Wold_style_declaration,
13174 : "%qE is not at beginning of declaration", scspec);
13175 90358142 : switch (i)
13176 : {
13177 35612586 : case RID_INLINE:
13178 : /* C99 permits duplicate inline. Although of doubtful utility,
13179 : it seems simplest to permit it in gnu89 mode as well, as
13180 : there is also little utility in maintaining this as a
13181 : difference between gnu89 and C99 inline. */
13182 35612586 : dupe = false;
13183 35612586 : specs->inline_p = true;
13184 35612586 : specs->locations[cdw_inline] = loc;
13185 35612586 : break;
13186 23317 : case RID_NORETURN:
13187 : /* Duplicate _Noreturn is permitted. */
13188 23317 : dupe = false;
13189 23317 : specs->noreturn_p = true;
13190 23317 : specs->locations[cdw_noreturn] = loc;
13191 23317 : break;
13192 2858 : case RID_THREAD:
13193 2858 : dupe = specs->thread_p;
13194 2858 : if (specs->storage_class == csc_auto)
13195 2 : error ("%qE used with %<auto%>", scspec);
13196 2856 : else if (specs->storage_class == csc_register)
13197 3 : error ("%qE used with %<register%>", scspec);
13198 2853 : else if (specs->storage_class == csc_typedef)
13199 2 : error ("%qE used with %<typedef%>", scspec);
13200 2851 : else if (specs->constexpr_p)
13201 2 : error ("%qE used with %<constexpr%>", scspec);
13202 : else
13203 : {
13204 2849 : specs->thread_p = true;
13205 2849 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13206 2849 : "__thread") == 0);
13207 : /* A diagnostic is not required for the use of this
13208 : identifier in the implementation namespace; only diagnose
13209 : it for the C11 spelling because of existing code using
13210 : the other spelling. */
13211 2849 : if (!specs->thread_gnu_p)
13212 : {
13213 116 : if (flag_isoc99)
13214 113 : pedwarn_c99 (loc, OPT_Wpedantic,
13215 : "ISO C99 does not support %qE", scspec);
13216 : else
13217 3 : pedwarn_c99 (loc, OPT_Wpedantic,
13218 : "ISO C90 does not support %qE", scspec);
13219 : }
13220 2849 : specs->locations[cdw_thread] = loc;
13221 : }
13222 : break;
13223 262 : case RID_AUTO:
13224 262 : if (flag_isoc23
13225 223 : && specs->typespec_kind == ctsk_none
13226 212 : && specs->storage_class != csc_typedef)
13227 : {
13228 : /* "auto" potentially used for type deduction. */
13229 211 : if (specs->c23_auto_p)
13230 2 : error ("duplicate %qE", scspec);
13231 211 : specs->c23_auto_p = true;
13232 211 : return specs;
13233 : }
13234 51 : n = csc_auto;
13235 : /* auto may only be used with another storage class specifier,
13236 : such as constexpr, if the type is inferred. */
13237 51 : if (specs->constexpr_p)
13238 2 : error ("%qE used with %<constexpr%>", scspec);
13239 : break;
13240 49972647 : case RID_EXTERN:
13241 49972647 : n = csc_extern;
13242 : /* Diagnose "__thread extern". */
13243 49972647 : if (specs->thread_p && specs->thread_gnu_p)
13244 2 : error ("%<__thread%> before %<extern%>");
13245 : break;
13246 : case RID_REGISTER:
13247 : n = csc_register;
13248 : break;
13249 426033 : case RID_STATIC:
13250 426033 : n = csc_static;
13251 : /* Diagnose "__thread static". */
13252 426033 : if (specs->thread_p && specs->thread_gnu_p)
13253 1 : error ("%<__thread%> before %<static%>");
13254 : break;
13255 4316692 : case RID_TYPEDEF:
13256 4316692 : n = csc_typedef;
13257 4316692 : if (specs->c23_auto_p)
13258 : {
13259 1 : error ("%<typedef%> used with %<auto%>");
13260 1 : specs->c23_auto_p = false;
13261 : }
13262 : break;
13263 616 : case RID_CONSTEXPR:
13264 616 : dupe = specs->constexpr_p;
13265 616 : if (specs->storage_class == csc_extern)
13266 1 : error ("%qE used with %<extern%>", scspec);
13267 615 : else if (specs->storage_class == csc_typedef)
13268 1 : error ("%qE used with %<typedef%>", scspec);
13269 614 : else if (specs->storage_class == csc_auto)
13270 : /* auto may only be used with another storage class specifier,
13271 : such as constexpr, if the type is inferred. */
13272 2 : error ("%qE used with %<auto%>", scspec);
13273 612 : else if (specs->thread_p)
13274 4 : error ("%qE used with %qs", scspec,
13275 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13276 : else
13277 610 : specs->constexpr_p = true;
13278 : break;
13279 0 : default:
13280 0 : gcc_unreachable ();
13281 : }
13282 90357937 : if (n != csc_none && n == specs->storage_class)
13283 : dupe = true;
13284 90357923 : if (dupe)
13285 : {
13286 12 : if (i == RID_THREAD)
13287 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13288 : else
13289 10 : error ("duplicate %qE", scspec);
13290 : }
13291 90357931 : if (n != csc_none)
13292 : {
13293 54718554 : if (specs->storage_class != csc_none && n != specs->storage_class)
13294 : {
13295 7 : error ("multiple storage classes in declaration specifiers");
13296 : }
13297 : else
13298 : {
13299 54718547 : specs->storage_class = n;
13300 54718547 : specs->locations[cdw_storage_class] = loc;
13301 54718547 : if (n != csc_extern && n != csc_static && specs->thread_p)
13302 : {
13303 8 : error ("%qs used with %qE",
13304 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13305 : scspec);
13306 8 : specs->thread_p = false;
13307 : }
13308 54718547 : if (n != csc_auto && n != csc_register && n != csc_static
13309 54289336 : && specs->constexpr_p)
13310 : {
13311 2 : error ("%<constexpr%> used with %qE", scspec);
13312 2 : specs->constexpr_p = false;
13313 : }
13314 : }
13315 : }
13316 : return specs;
13317 : }
13318 :
13319 : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13320 : returning SPECS. */
13321 :
13322 : struct c_declspecs *
13323 35924520 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13324 : {
13325 35924520 : specs->attrs = chainon (attrs, specs->attrs);
13326 35924520 : specs->locations[cdw_attributes] = loc;
13327 35924520 : specs->declspecs_seen_p = true;
13328 : /* In the case of standard attributes at the start of the
13329 : declaration, the caller will reset this. */
13330 35924520 : specs->non_std_attrs_seen_p = true;
13331 35924520 : return specs;
13332 : }
13333 :
13334 : /* Add an _Alignas specifier (expression ALIGN, or type whose
13335 : alignment is ALIGN) to the declaration specifiers SPECS, returning
13336 : SPECS. */
13337 : struct c_declspecs *
13338 202 : declspecs_add_alignas (location_t loc,
13339 : struct c_declspecs *specs, tree align)
13340 : {
13341 202 : specs->alignas_p = true;
13342 202 : specs->locations[cdw_alignas] = loc;
13343 202 : if (align == error_mark_node)
13344 : return specs;
13345 :
13346 : /* Only accept the alignment if it's valid and greater than
13347 : the current one. Zero is invalid but by C11 required to
13348 : be silently ignored. */
13349 200 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13350 200 : if (align_log > specs->align_log)
13351 167 : specs->align_log = align_log;
13352 : return specs;
13353 : }
13354 :
13355 : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13356 : specifiers with any other type specifier to determine the resulting
13357 : type. This is where ISO C checks on complex types are made, since
13358 : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13359 : double". Also apply postfix standard attributes to modify the type. */
13360 :
13361 : struct c_declspecs *
13362 314115509 : finish_declspecs (struct c_declspecs *specs)
13363 : {
13364 : /* If a type was specified as a whole, we have no modifiers and are
13365 : done. */
13366 314115509 : if (specs->type != NULL_TREE)
13367 : {
13368 249151582 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13369 : && !specs->signed_p && !specs->unsigned_p
13370 : && !specs->complex_p && !specs->c23_auto_p);
13371 :
13372 : /* Set a dummy type. */
13373 249151582 : if (TREE_CODE (specs->type) == ERROR_MARK)
13374 184 : specs->type = integer_type_node;
13375 249151582 : goto handle_postfix_attrs;
13376 : }
13377 :
13378 : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13379 : has been specified, treat it as "int" unless "_Complex" is
13380 : present and there are no other specifiers. If we just have
13381 : "_Complex", it is equivalent to "_Complex double", but e.g.
13382 : "_Complex short" is equivalent to "_Complex short int". */
13383 64963927 : if (specs->typespec_word == cts_none)
13384 : {
13385 4184671 : if (specs->saturating_p)
13386 : {
13387 1 : error_at (specs->locations[cdw_saturating],
13388 : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13389 1 : if (!targetm.fixed_point_supported_p ())
13390 1 : error_at (specs->locations[cdw_saturating],
13391 : "fixed-point types not supported for this target");
13392 1 : specs->typespec_word = cts_fract;
13393 : }
13394 4184670 : else if (specs->long_p || specs->short_p
13395 193753 : || specs->signed_p || specs->unsigned_p)
13396 : {
13397 4174230 : specs->typespec_word = cts_int;
13398 : }
13399 10440 : else if (specs->complex_p)
13400 : {
13401 130 : specs->typespec_word = cts_double;
13402 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13403 : "ISO C does not support plain %<complex%> meaning "
13404 : "%<double complex%>");
13405 : }
13406 10310 : else if (specs->c23_auto_p)
13407 : {
13408 : /* Type to be filled in later, including applying postfix
13409 : attributes. This warning only actually appears for
13410 : -Wc11-c23-compat in C23 mode; in older modes, there may
13411 : be a warning or pedwarn for implicit "int" instead, or
13412 : other errors for use of auto at file scope. */
13413 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13414 : "ISO C does not support %<auto%> type deduction "
13415 : "before C23");
13416 93 : return specs;
13417 : }
13418 : else
13419 : {
13420 10217 : specs->typespec_word = cts_int;
13421 10217 : specs->default_int_p = true;
13422 : /* We don't diagnose this here because grokdeclarator will
13423 : give more specific diagnostics according to whether it is
13424 : a function definition. */
13425 : }
13426 : }
13427 :
13428 : /* If "signed" was specified, record this to distinguish "int" and
13429 : "signed int" in the case of a bit-field with
13430 : -funsigned-bitfields. */
13431 64963834 : specs->explicit_signed_p = specs->signed_p;
13432 :
13433 : /* Now compute the actual type. */
13434 64963834 : gcc_assert (!specs->c23_auto_p);
13435 64963834 : switch (specs->typespec_word)
13436 : {
13437 1885 : case cts_auto_type:
13438 1885 : gcc_assert (!specs->long_p && !specs->short_p
13439 : && !specs->signed_p && !specs->unsigned_p
13440 : && !specs->complex_p);
13441 : /* Type to be filled in later. */
13442 1885 : if (specs->postfix_attrs)
13443 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13444 : break;
13445 7852741 : case cts_void:
13446 7852741 : gcc_assert (!specs->long_p && !specs->short_p
13447 : && !specs->signed_p && !specs->unsigned_p
13448 : && !specs->complex_p);
13449 7852741 : specs->type = void_type_node;
13450 7852741 : break;
13451 85872 : case cts_bool:
13452 85872 : gcc_assert (!specs->long_p && !specs->short_p
13453 : && !specs->signed_p && !specs->unsigned_p
13454 : && !specs->complex_p);
13455 85872 : specs->type = boolean_type_node;
13456 85872 : break;
13457 9055815 : case cts_char:
13458 9055815 : gcc_assert (!specs->long_p && !specs->short_p);
13459 9055815 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13460 9055815 : if (specs->signed_p)
13461 191945 : specs->type = signed_char_type_node;
13462 8863870 : else if (specs->unsigned_p)
13463 897044 : specs->type = unsigned_char_type_node;
13464 : else
13465 7966826 : specs->type = char_type_node;
13466 9055815 : if (specs->complex_p)
13467 : {
13468 3855 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13469 : "ISO C does not support complex integer types");
13470 3855 : specs->type = build_complex_type (specs->type);
13471 : }
13472 : break;
13473 51683 : case cts_int_n:
13474 51683 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13475 51683 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13476 51683 : if (! int_n_enabled_p[specs->u.int_n_idx])
13477 0 : specs->type = integer_type_node;
13478 : else
13479 51683 : specs->type = (specs->unsigned_p
13480 51683 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13481 : : int_n_trees[specs->u.int_n_idx].signed_type);
13482 51683 : if (specs->complex_p)
13483 : {
13484 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13485 : "ISO C does not support complex integer types");
13486 199 : specs->type = build_complex_type (specs->type);
13487 : }
13488 : break;
13489 28039902 : case cts_int:
13490 28039902 : gcc_assert (!(specs->long_p && specs->short_p));
13491 28039902 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13492 28039902 : if (specs->long_long_p)
13493 2950311 : specs->type = (specs->unsigned_p
13494 2950311 : ? long_long_unsigned_type_node
13495 : : long_long_integer_type_node);
13496 25089591 : else if (specs->long_p)
13497 2201414 : specs->type = (specs->unsigned_p
13498 2201414 : ? long_unsigned_type_node
13499 : : long_integer_type_node);
13500 22888177 : else if (specs->short_p)
13501 1702344 : specs->type = (specs->unsigned_p
13502 1702344 : ? short_unsigned_type_node
13503 : : short_integer_type_node);
13504 : else
13505 21185833 : specs->type = (specs->unsigned_p
13506 21185833 : ? unsigned_type_node
13507 : : integer_type_node);
13508 28039902 : if (specs->complex_p)
13509 : {
13510 14933 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13511 : "ISO C does not support complex integer types");
13512 14933 : specs->type = build_complex_type (specs->type);
13513 : }
13514 : break;
13515 3392218 : case cts_float:
13516 3392218 : gcc_assert (!specs->long_p && !specs->short_p
13517 : && !specs->signed_p && !specs->unsigned_p);
13518 6784436 : specs->type = (specs->complex_p
13519 3392218 : ? complex_float_type_node
13520 : : float_type_node);
13521 3392218 : break;
13522 6108916 : case cts_double:
13523 6108916 : gcc_assert (!specs->long_long_p && !specs->short_p
13524 : && !specs->signed_p && !specs->unsigned_p);
13525 6108916 : if (specs->long_p)
13526 : {
13527 2627829 : specs->type = (specs->complex_p
13528 2627829 : ? complex_long_double_type_node
13529 : : long_double_type_node);
13530 : }
13531 : else
13532 : {
13533 3481087 : specs->type = (specs->complex_p
13534 3481087 : ? complex_double_type_node
13535 : : double_type_node);
13536 : }
13537 : break;
13538 10281310 : case cts_floatn_nx:
13539 10281310 : gcc_assert (!specs->long_p && !specs->short_p
13540 : && !specs->signed_p && !specs->unsigned_p);
13541 10281310 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13542 88 : specs->type = integer_type_node;
13543 10281222 : else if (specs->complex_p)
13544 1555489 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13545 : else
13546 8725733 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13547 : break;
13548 47577 : case cts_dfloat32:
13549 47577 : case cts_dfloat64:
13550 47577 : case cts_dfloat128:
13551 47577 : case cts_dfloat64x:
13552 47577 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13553 : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13554 47577 : if (!targetm.decimal_float_supported_p ())
13555 0 : specs->type = integer_type_node;
13556 47577 : else if (specs->typespec_word == cts_dfloat32)
13557 15957 : specs->type = dfloat32_type_node;
13558 31620 : else if (specs->typespec_word == cts_dfloat64)
13559 15839 : specs->type = dfloat64_type_node;
13560 15781 : else if (specs->typespec_word == cts_dfloat128)
13561 15734 : specs->type = dfloat128_type_node;
13562 : else
13563 47 : specs->type = dfloat64x_type_node;
13564 : break;
13565 24 : case cts_fract:
13566 24 : gcc_assert (!specs->complex_p);
13567 24 : if (!targetm.fixed_point_supported_p ())
13568 24 : specs->type = integer_type_node;
13569 0 : else if (specs->saturating_p)
13570 : {
13571 0 : if (specs->long_long_p)
13572 0 : specs->type = specs->unsigned_p
13573 0 : ? sat_unsigned_long_long_fract_type_node
13574 : : sat_long_long_fract_type_node;
13575 0 : else if (specs->long_p)
13576 0 : specs->type = specs->unsigned_p
13577 0 : ? sat_unsigned_long_fract_type_node
13578 : : sat_long_fract_type_node;
13579 0 : else if (specs->short_p)
13580 0 : specs->type = specs->unsigned_p
13581 0 : ? sat_unsigned_short_fract_type_node
13582 : : sat_short_fract_type_node;
13583 : else
13584 0 : specs->type = specs->unsigned_p
13585 0 : ? sat_unsigned_fract_type_node
13586 : : sat_fract_type_node;
13587 : }
13588 : else
13589 : {
13590 0 : if (specs->long_long_p)
13591 0 : specs->type = specs->unsigned_p
13592 0 : ? unsigned_long_long_fract_type_node
13593 : : long_long_fract_type_node;
13594 0 : else if (specs->long_p)
13595 0 : specs->type = specs->unsigned_p
13596 0 : ? unsigned_long_fract_type_node
13597 : : long_fract_type_node;
13598 0 : else if (specs->short_p)
13599 0 : specs->type = specs->unsigned_p
13600 0 : ? unsigned_short_fract_type_node
13601 : : short_fract_type_node;
13602 : else
13603 0 : specs->type = specs->unsigned_p
13604 0 : ? unsigned_fract_type_node
13605 : : fract_type_node;
13606 : }
13607 : break;
13608 22 : case cts_accum:
13609 22 : gcc_assert (!specs->complex_p);
13610 22 : if (!targetm.fixed_point_supported_p ())
13611 22 : specs->type = integer_type_node;
13612 0 : else if (specs->saturating_p)
13613 : {
13614 0 : if (specs->long_long_p)
13615 0 : specs->type = specs->unsigned_p
13616 0 : ? sat_unsigned_long_long_accum_type_node
13617 : : sat_long_long_accum_type_node;
13618 0 : else if (specs->long_p)
13619 0 : specs->type = specs->unsigned_p
13620 0 : ? sat_unsigned_long_accum_type_node
13621 : : sat_long_accum_type_node;
13622 0 : else if (specs->short_p)
13623 0 : specs->type = specs->unsigned_p
13624 0 : ? sat_unsigned_short_accum_type_node
13625 : : sat_short_accum_type_node;
13626 : else
13627 0 : specs->type = specs->unsigned_p
13628 0 : ? sat_unsigned_accum_type_node
13629 : : sat_accum_type_node;
13630 : }
13631 : else
13632 : {
13633 0 : if (specs->long_long_p)
13634 0 : specs->type = specs->unsigned_p
13635 0 : ? unsigned_long_long_accum_type_node
13636 : : long_long_accum_type_node;
13637 0 : else if (specs->long_p)
13638 0 : specs->type = specs->unsigned_p
13639 0 : ? unsigned_long_accum_type_node
13640 : : long_accum_type_node;
13641 0 : else if (specs->short_p)
13642 0 : specs->type = specs->unsigned_p
13643 0 : ? unsigned_short_accum_type_node
13644 : : short_accum_type_node;
13645 : else
13646 0 : specs->type = specs->unsigned_p
13647 0 : ? unsigned_accum_type_node
13648 : : accum_type_node;
13649 : }
13650 : break;
13651 45869 : case cts_bitint:
13652 45869 : gcc_assert (!specs->long_p && !specs->short_p
13653 : && !specs->complex_p);
13654 45869 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13655 : {
13656 2 : error_at (specs->locations[cdw_typespec],
13657 : "%<signed _BitInt%> argument must be at least 2");
13658 2 : specs->type = integer_type_node;
13659 2 : break;
13660 : }
13661 45867 : if (specs->u.bitint_prec == -1)
13662 7 : specs->type = integer_type_node;
13663 : else
13664 : {
13665 67724 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13666 : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13667 : specs->unsigned_p ? "unsigned "
13668 21864 : : specs->signed_p ? "signed " : "",
13669 : specs->u.bitint_prec);
13670 45860 : specs->type = build_bitint_type (specs->u.bitint_prec,
13671 45860 : specs->unsigned_p);
13672 : }
13673 : break;
13674 0 : default:
13675 0 : gcc_unreachable ();
13676 : }
13677 314115416 : handle_postfix_attrs:
13678 314115416 : if (specs->type != NULL)
13679 : {
13680 314113531 : specs->postfix_attrs
13681 314113531 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13682 314113531 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13683 314113531 : specs->postfix_attrs = NULL_TREE;
13684 : }
13685 :
13686 : return specs;
13687 : }
13688 :
13689 : /* Perform final processing on one file scope's declarations (or the
13690 : external scope's declarations), GLOBALS. */
13691 :
13692 : static void
13693 208462 : c_write_global_declarations_1 (tree globals)
13694 : {
13695 208462 : tree decl;
13696 208462 : bool reconsider;
13697 :
13698 : /* Process the decls in the order they were written. */
13699 381609674 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13700 : {
13701 : /* Check for used but undefined static functions using the C
13702 : standard's definition of "used", and set TREE_NO_WARNING so
13703 : that check_global_declaration doesn't repeat the check. */
13704 381401212 : if (TREE_CODE (decl) == FUNCTION_DECL
13705 363345772 : && DECL_INITIAL (decl) == NULL_TREE
13706 327046323 : && DECL_EXTERNAL (decl)
13707 327046318 : && !TREE_PUBLIC (decl)
13708 381401348 : && !warning_suppressed_p (decl, OPT_Wunused))
13709 : {
13710 131 : if (C_DECL_USED (decl))
13711 : {
13712 30 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13713 : decl))
13714 29 : suppress_warning (decl, OPT_Wunused);
13715 : }
13716 : /* For -Wunused-function warn about unused static prototypes. */
13717 101 : else if (warn_unused_function
13718 2 : && ! DECL_ARTIFICIAL (decl)
13719 103 : && warning (OPT_Wunused_function,
13720 : "%q+F declared %<static%> but never defined",
13721 : decl))
13722 2 : suppress_warning (decl, OPT_Wunused);
13723 : }
13724 :
13725 381401212 : wrapup_global_declaration_1 (decl);
13726 : }
13727 :
13728 242233 : do
13729 : {
13730 242233 : reconsider = false;
13731 494517553 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13732 494275320 : reconsider |= wrapup_global_declaration_2 (decl);
13733 : }
13734 : while (reconsider);
13735 208462 : }
13736 :
13737 : /* Preserve the external declarations scope across a garbage collect. */
13738 : static GTY(()) tree ext_block;
13739 :
13740 : /* Collect all references relevant to SOURCE_FILE. */
13741 :
13742 : static void
13743 15 : collect_all_refs (const char *source_file)
13744 : {
13745 15 : tree t;
13746 15 : unsigned i;
13747 :
13748 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13749 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13750 :
13751 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13752 15 : }
13753 :
13754 : /* Collect source file references at global level. */
13755 :
13756 : static void
13757 15 : collect_source_refs (void)
13758 : {
13759 15 : tree t;
13760 15 : tree decls;
13761 15 : tree decl;
13762 15 : unsigned i;
13763 :
13764 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13765 : {
13766 15 : decls = DECL_INITIAL (t);
13767 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13768 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13769 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13770 : }
13771 :
13772 44186 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13773 44171 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13774 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13775 15 : }
13776 :
13777 : /* Free attribute access data that are not needed by the middle end. */
13778 :
13779 : static void
13780 104231 : free_attr_access_data ()
13781 : {
13782 104231 : struct cgraph_node *n;
13783 :
13784 : /* Iterate over all functions declared in the translation unit. */
13785 36405317 : FOR_EACH_FUNCTION (n)
13786 : {
13787 136319675 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13788 100018589 : if (tree attrs = DECL_ATTRIBUTES (parm))
13789 97519 : attr_access::free_lang_data (attrs);
13790 :
13791 36301086 : tree fntype = TREE_TYPE (n->decl);
13792 36301086 : if (!fntype || fntype == error_mark_node)
13793 0 : continue;
13794 36301086 : tree attrs = TYPE_ATTRIBUTES (fntype);
13795 36301086 : if (!attrs)
13796 36065990 : continue;
13797 :
13798 235096 : attr_access::free_lang_data (attrs);
13799 : }
13800 104231 : }
13801 :
13802 : /* Perform any final parser cleanups and generate initial debugging
13803 : information. */
13804 :
13805 : void
13806 104574 : c_parse_final_cleanups (void)
13807 : {
13808 104574 : tree t;
13809 104574 : unsigned i;
13810 :
13811 : /* We don't want to do this if generating a PCH. */
13812 104574 : if (pch_file)
13813 104574 : return;
13814 :
13815 104231 : timevar_stop (TV_PHASE_PARSING);
13816 104231 : timevar_start (TV_PHASE_DEFERRED);
13817 :
13818 : /* Do the Objective-C stuff. This is where all the Objective-C
13819 : module stuff gets generated (symtab, class/protocol/selector
13820 : lists etc). */
13821 104231 : if (c_dialect_objc ())
13822 0 : objc_write_global_declarations ();
13823 :
13824 : /* Close the external scope. */
13825 104231 : ext_block = pop_scope ();
13826 104231 : external_scope = 0;
13827 104231 : gcc_assert (!current_scope);
13828 :
13829 : /* Handle -fdump-ada-spec[-slim]. */
13830 104231 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13831 : {
13832 : /* Build a table of files to generate specs for */
13833 15 : collect_source_ref (main_input_filename);
13834 15 : if (!flag_dump_ada_spec_slim)
13835 15 : collect_source_refs ();
13836 :
13837 15 : dump_ada_specs (collect_all_refs, NULL);
13838 : }
13839 :
13840 : /* Process all file scopes in this compilation, and the external_scope,
13841 : through wrapup_global_declarations. */
13842 208462 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13843 104231 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13844 104231 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13845 :
13846 : /* Call this to set cpp_implicit_aliases_done on all nodes. This is
13847 : important for function multiversioning aliases to get resolved. */
13848 104231 : symtab->process_same_body_aliases ();
13849 :
13850 104231 : if (!in_lto_p)
13851 104231 : free_attr_access_data ();
13852 :
13853 104231 : timevar_stop (TV_PHASE_DEFERRED);
13854 104231 : timevar_start (TV_PHASE_PARSING);
13855 :
13856 104231 : ext_block = NULL;
13857 : }
13858 :
13859 : /* Register reserved keyword WORD as qualifier for address space AS. */
13860 :
13861 : void
13862 220256 : c_register_addr_space (const char *word, addr_space_t as)
13863 : {
13864 220256 : int rid = RID_FIRST_ADDR_SPACE + as;
13865 220256 : tree id;
13866 :
13867 : /* Address space qualifiers are only supported
13868 : in C with GNU extensions enabled. */
13869 220256 : if (c_dialect_objc () || flag_no_asm)
13870 : return;
13871 :
13872 207898 : id = get_identifier (word);
13873 207898 : C_SET_RID_CODE (id, rid);
13874 207898 : C_IS_RESERVED_WORD (id) = 1;
13875 207898 : ridpointers [rid] = id;
13876 : }
13877 :
13878 : /* Return identifier to look up for omp declare reduction. */
13879 :
13880 : tree
13881 3254 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13882 : {
13883 3254 : const char *p = NULL;
13884 3254 : switch (reduction_code)
13885 : {
13886 : case PLUS_EXPR: p = "+"; break;
13887 267 : case MULT_EXPR: p = "*"; break;
13888 157 : case MINUS_EXPR: p = "-"; break;
13889 31 : case BIT_AND_EXPR: p = "&"; break;
13890 14 : case BIT_XOR_EXPR: p = "^"; break;
13891 92 : case BIT_IOR_EXPR: p = "|"; break;
13892 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13893 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13894 46 : case MIN_EXPR: p = "min"; break;
13895 83 : case MAX_EXPR: p = "max"; break;
13896 : default:
13897 : break;
13898 : }
13899 :
13900 835 : if (p == NULL)
13901 : {
13902 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13903 0 : return error_mark_node;
13904 213 : p = IDENTIFIER_POINTER (reduction_id);
13905 : }
13906 :
13907 3254 : const char prefix[] = "omp declare reduction ";
13908 3254 : size_t lenp = sizeof (prefix);
13909 3254 : size_t len = strlen (p);
13910 3254 : char *name = XALLOCAVEC (char, lenp + len);
13911 3254 : memcpy (name, prefix, lenp - 1);
13912 3254 : memcpy (name + lenp - 1, p, len + 1);
13913 3254 : return get_identifier (name);
13914 : }
13915 :
13916 : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13917 : VAR_DECL, bind it into the current scope and return it. */
13918 :
13919 : tree
13920 161 : c_omp_reduction_decl (tree reduction_id)
13921 : {
13922 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13923 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13924 47 : return b->decl;
13925 :
13926 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13927 : reduction_id, integer_type_node);
13928 114 : DECL_ARTIFICIAL (decl) = 1;
13929 114 : DECL_EXTERNAL (decl) = 1;
13930 114 : TREE_STATIC (decl) = 1;
13931 114 : TREE_PUBLIC (decl) = 0;
13932 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13933 114 : return decl;
13934 : }
13935 :
13936 : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13937 :
13938 : tree
13939 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13940 : {
13941 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13942 276 : while (b)
13943 : {
13944 274 : tree t;
13945 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13946 291 : if (comptypes (TREE_PURPOSE (t), type))
13947 271 : return TREE_VALUE (t);
13948 3 : b = b->shadowed;
13949 : }
13950 2 : return error_mark_node;
13951 : }
13952 :
13953 : /* Helper function called via walk_tree, to diagnose invalid
13954 : #pragma omp declare reduction combiners or initializers. */
13955 :
13956 : tree
13957 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13958 : {
13959 1401 : tree *vars = (tree *) data;
13960 1401 : if (SSA_VAR_P (*tp)
13961 413 : && !DECL_ARTIFICIAL (*tp)
13962 24 : && *tp != vars[0]
13963 24 : && *tp != vars[1])
13964 : {
13965 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13966 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13967 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13968 : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13969 : *tp);
13970 : else
13971 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13972 : "to variable %qD which is not %<omp_priv%> nor "
13973 : "%<omp_orig%>",
13974 : *tp);
13975 24 : return *tp;
13976 : }
13977 : return NULL_TREE;
13978 : }
13979 :
13980 : /* Return identifier to look up for omp declare mapper. */
13981 :
13982 : tree
13983 617 : c_omp_mapper_id (tree mapper_id)
13984 : {
13985 617 : const char *p = NULL;
13986 :
13987 617 : const char prefix[] = "omp declare mapper ";
13988 :
13989 617 : if (mapper_id == NULL_TREE)
13990 : p = "<default>";
13991 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13992 19 : p = IDENTIFIER_POINTER (mapper_id);
13993 : else
13994 0 : return error_mark_node;
13995 :
13996 617 : size_t lenp = sizeof (prefix);
13997 617 : size_t len = strlen (p);
13998 617 : char *name = XALLOCAVEC (char, lenp + len);
13999 617 : memcpy (name, prefix, lenp - 1);
14000 617 : memcpy (name + lenp - 1, p, len + 1);
14001 617 : return get_identifier (name);
14002 : }
14003 :
14004 : /* Lookup MAPPER_ID in the current scope, or create an artificial
14005 : VAR_DECL, bind it into the current scope and return it. */
14006 :
14007 : tree
14008 63 : c_omp_mapper_decl (tree mapper_id)
14009 : {
14010 63 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
14011 63 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
14012 22 : return b->decl;
14013 :
14014 41 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
14015 : mapper_id, integer_type_node);
14016 41 : DECL_ARTIFICIAL (decl) = 1;
14017 41 : DECL_EXTERNAL (decl) = 1;
14018 41 : TREE_STATIC (decl) = 1;
14019 41 : TREE_PUBLIC (decl) = 0;
14020 41 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
14021 41 : return decl;
14022 : }
14023 :
14024 : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
14025 :
14026 : tree
14027 1786 : c_omp_mapper_lookup (tree mapper_id, tree type)
14028 : {
14029 1786 : if (!RECORD_OR_UNION_TYPE_P (type))
14030 : return NULL_TREE;
14031 :
14032 554 : mapper_id = c_omp_mapper_id (mapper_id);
14033 :
14034 554 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
14035 560 : while (b)
14036 : {
14037 92 : tree t;
14038 111 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
14039 105 : if (comptypes (TREE_PURPOSE (t), type))
14040 86 : return TREE_VALUE (t);
14041 6 : b = b->shadowed;
14042 : }
14043 : return NULL_TREE;
14044 : }
14045 :
14046 : /* For C, we record a pointer to the mapper itself without wrapping it in an
14047 : artificial function or similar. So, just return it. */
14048 :
14049 : tree
14050 84 : c_omp_extract_mapper_directive (tree mapper)
14051 : {
14052 84 : return mapper;
14053 : }
14054 :
14055 : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
14056 : nothing more complicated. */
14057 :
14058 : tree
14059 365 : c_omp_map_array_section (location_t loc, tree t)
14060 : {
14061 365 : tree low = TREE_OPERAND (t, 1);
14062 365 : tree len = TREE_OPERAND (t, 2);
14063 :
14064 365 : if (len && integer_onep (len))
14065 : {
14066 63 : t = TREE_OPERAND (t, 0);
14067 :
14068 63 : if (!low)
14069 0 : low = integer_zero_node;
14070 :
14071 63 : t = build_array_ref (loc, t, low);
14072 : }
14073 :
14074 365 : return t;
14075 : }
14076 :
14077 : /* Helper function for below function. */
14078 :
14079 : static tree
14080 51344 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
14081 : {
14082 51344 : tree t = *tp;
14083 51344 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
14084 51344 : tree aggr_type = NULL_TREE;
14085 :
14086 51344 : if (TREE_CODE (t) == SIZEOF_EXPR
14087 51344 : || TREE_CODE (t) == ALIGNOF_EXPR)
14088 : {
14089 0 : *walk_subtrees = 0;
14090 0 : return NULL_TREE;
14091 : }
14092 :
14093 51344 : if (TREE_CODE (t) == OMP_CLAUSE)
14094 : return NULL_TREE;
14095 :
14096 48055 : if (TREE_CODE (t) == COMPONENT_REF
14097 48055 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
14098 362 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
14099 47693 : else if ((TREE_CODE (t) == VAR_DECL
14100 : || TREE_CODE (t) == PARM_DECL
14101 : || TREE_CODE (t) == RESULT_DECL)
14102 5333 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
14103 119 : aggr_type = TREE_TYPE (t);
14104 :
14105 481 : if (aggr_type)
14106 : {
14107 481 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
14108 481 : if (mapper_fn)
14109 66 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
14110 : }
14111 :
14112 : return NULL_TREE;
14113 : }
14114 :
14115 : /* Scan an offload region's body, and record uses of struct- or union-typed
14116 : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
14117 :
14118 : void
14119 2042 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
14120 : {
14121 2042 : hash_set<omp_name_type<tree>> seen_types;
14122 2042 : auto_vec<tree> mappers;
14123 2042 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
14124 :
14125 2042 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
14126 :
14127 2042 : unsigned int i;
14128 2042 : tree mapper;
14129 4124 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14130 40 : c_omp_find_nested_mappers (&mlist, mapper);
14131 :
14132 2109 : FOR_EACH_VEC_ELT (mappers, i, mapper)
14133 : {
14134 40 : if (mapper == error_mark_node)
14135 0 : continue;
14136 40 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
14137 40 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
14138 :
14139 40 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
14140 40 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
14141 40 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
14142 40 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
14143 :
14144 40 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
14145 40 : *clauses_ptr = c;
14146 : }
14147 2042 : }
14148 :
14149 : bool
14150 225 : c_check_in_current_scope (tree decl)
14151 : {
14152 225 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
14153 225 : return b != NULL && B_IN_CURRENT_SCOPE (b);
14154 : }
14155 :
14156 : /* Search for loop or switch names. BEFORE_LABELS is last statement before
14157 : possible labels and SWITCH_P true for a switch, false for loops.
14158 : Searches through last statements in cur_stmt_list, stops when seeing
14159 : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
14160 : Returns number of loop/switch names found and if any are found, sets
14161 : *LAST_P to the canonical loop/switch name LABEL_DECL. */
14162 :
14163 : int
14164 512053 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
14165 : {
14166 512053 : *last_p = NULL_TREE;
14167 1024106 : if (!building_stmt_list_p ()
14168 512053 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
14169 532643 : || before_labels == void_list_node)
14170 491463 : return 0;
14171 :
14172 20590 : int ret = 0;
14173 20590 : tree last = NULL_TREE;
14174 20590 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
14175 28786 : !tsi_end_p (tsi); tsi_prev (&tsi))
14176 : {
14177 27929 : tree stmt = tsi_stmt (tsi);
14178 27929 : if (stmt == before_labels)
14179 : break;
14180 8196 : else if (TREE_CODE (stmt) == LABEL_EXPR)
14181 : {
14182 1101 : if (last == NULL_TREE)
14183 907 : last = LABEL_EXPR_LABEL (stmt);
14184 : else
14185 : {
14186 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
14187 194 : ++ret;
14188 : }
14189 : }
14190 7095 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
14191 4768 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
14192 : break;
14193 : }
14194 20590 : if (last)
14195 : {
14196 907 : if (switch_p)
14197 129 : C_DECL_SWITCH_NAME (last) = 1;
14198 : else
14199 778 : C_DECL_LOOP_NAME (last) = 1;
14200 907 : loop_names.safe_push (last);
14201 907 : ++ret;
14202 907 : if (loop_names.length () > 16)
14203 : {
14204 20 : unsigned int first = 0, i;
14205 20 : tree l, c = NULL_TREE;
14206 20 : if (loop_names_hash == NULL)
14207 8 : loop_names_hash = new decl_tree_map (ret);
14208 : else
14209 12 : first = loop_names.length () - ret;
14210 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14211 : {
14212 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14213 48 : c = l;
14214 170 : gcc_checking_assert (c);
14215 170 : loop_names_hash->put (l, c);
14216 170 : if (i == first)
14217 : break;
14218 : }
14219 : }
14220 907 : *last_p = last;
14221 : }
14222 : return ret;
14223 : }
14224 :
14225 : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
14226 :
14227 : void
14228 907 : c_release_loop_names (int num_names)
14229 : {
14230 907 : unsigned len = loop_names.length () - num_names;
14231 907 : if (loop_names_hash)
14232 : {
14233 20 : if (len <= 16)
14234 : {
14235 8 : delete loop_names_hash;
14236 8 : loop_names_hash = NULL;
14237 : }
14238 : else
14239 : {
14240 12 : unsigned int i;
14241 12 : tree l;
14242 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14243 : {
14244 30 : loop_names_hash->remove (l);
14245 30 : if (i == len)
14246 : break;
14247 : }
14248 : }
14249 : }
14250 907 : loop_names.truncate (len);
14251 907 : }
14252 :
14253 : /* Finish processing of break or continue identifier operand.
14254 : NAME is the identifier operand of break or continue and
14255 : IS_BREAK is true iff it is break stmt. Returns the operand
14256 : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14257 : canonical loop/switch name LABEL_DECL. */
14258 :
14259 : tree
14260 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14261 : {
14262 316 : tree label = NULL_TREE, lab;
14263 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14264 : "ISO C does not support %qs statement with an identifier "
14265 : "operand before C2Y", is_break ? "break" : "continue");
14266 :
14267 : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14268 : trying to find it among loop_names, it can't be there. */
14269 316 : if (!loop_names.is_empty ()
14270 296 : && current_function_scope
14271 296 : && (lab = I_LABEL_DECL (name))
14272 290 : && DECL_CONTEXT (lab) == current_function_decl)
14273 : {
14274 280 : unsigned int i;
14275 280 : tree l, c = NULL_TREE;
14276 280 : if (loop_names_hash)
14277 : {
14278 86 : if (tree *val = loop_names_hash->get (lab))
14279 86 : label = *val;
14280 : }
14281 : else
14282 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14283 : {
14284 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14285 : c = l;
14286 363 : gcc_checking_assert (c);
14287 363 : if (l == lab)
14288 : {
14289 : label = c;
14290 : break;
14291 : }
14292 : }
14293 280 : if (label)
14294 272 : TREE_USED (lab) = 1;
14295 : }
14296 272 : if (label == NULL_TREE)
14297 : {
14298 44 : auto_vec<const char *> candidates;
14299 44 : unsigned int i;
14300 44 : tree l, c = NULL_TREE;
14301 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14302 : {
14303 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14304 : c = l;
14305 28 : gcc_checking_assert (c);
14306 42 : if (is_break || C_DECL_LOOP_NAME (c))
14307 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14308 : }
14309 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14310 : &candidates);
14311 44 : if (hint)
14312 : {
14313 22 : gcc_rich_location richloc (loc);
14314 22 : richloc.add_fixit_replace (hint);
14315 22 : if (is_break)
14316 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14317 : "refer to a named loop or %<switch%>; "
14318 : "did you mean %qs?", name, hint);
14319 : else
14320 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14321 : "refer to a named loop; did you mean %qs?",
14322 : name, hint);
14323 22 : }
14324 22 : else if (is_break)
14325 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14326 : "named loop or %<switch%>", name);
14327 : else
14328 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14329 : "a named loop", name);
14330 44 : }
14331 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14332 : {
14333 2 : auto_diagnostic_group d;
14334 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14335 : "%<switch%>", name);
14336 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14337 2 : label = NULL_TREE;
14338 2 : }
14339 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14340 : {
14341 18 : auto_diagnostic_group d;
14342 18 : if (C_DECL_LOOP_NAME (label))
14343 : {
14344 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14345 : "of its body", is_break ? "break" : "continue", name);
14346 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14347 : }
14348 : else
14349 : {
14350 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14351 : "%<switch%> outside of its body", name);
14352 2 : inform (DECL_SOURCE_LOCATION (label),
14353 : "%<switch%> name defined here");
14354 : }
14355 18 : label = NULL_TREE;
14356 18 : }
14357 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14358 : /* If it is just a fancy reference to the innermost construct, handle it
14359 : just like break; or continue; though tracking cheaply what is the
14360 : innermost loop for continue when nested in switches would require
14361 : another global variable and updating it. */
14362 : label = NULL_TREE;
14363 : else
14364 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14365 316 : return label;
14366 : }
14367 :
14368 : #include "gt-c-c-decl.h"
|