Branch data Line data Source code
1 : : /* Process declarations and variables for C compiler.
2 : : Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : /* Process declarations and symbol lookup for C front end.
21 : : Also constructs types; the standard scalar types at initialization,
22 : : and structure, union, array and enum types when they are declared. */
23 : :
24 : : /* ??? not all decl nodes are given the most useful possible
25 : : line numbers. For example, the CONST_DECLs for enum values. */
26 : :
27 : : #include "config.h"
28 : : #define INCLUDE_STRING
29 : : #include "system.h"
30 : : #include "coretypes.h"
31 : : #include "target.h"
32 : : #include "function.h"
33 : : #include "c-tree.h"
34 : : #include "timevar.h"
35 : : #include "stringpool.h"
36 : : #include "cgraph.h"
37 : : #include "intl.h"
38 : : #include "print-tree.h"
39 : : #include "stor-layout.h"
40 : : #include "varasm.h"
41 : : #include "attribs.h"
42 : : #include "toplev.h"
43 : : #include "debug.h"
44 : : #include "c-family/c-objc.h"
45 : : #include "c-family/c-pragma.h"
46 : : #include "c-family/c-ubsan.h"
47 : : #include "c-lang.h"
48 : : #include "langhooks.h"
49 : : #include "tree-iterator.h"
50 : : #include "dumpfile.h"
51 : : #include "plugin.h"
52 : : #include "c-family/c-ada-spec.h"
53 : : #include "builtins.h"
54 : : #include "spellcheck-tree.h"
55 : : #include "gcc-rich-location.h"
56 : : #include "asan.h"
57 : : #include "c-family/name-hint.h"
58 : : #include "c-family/known-headers.h"
59 : : #include "c-family/c-spellcheck.h"
60 : : #include "context.h" /* For 'g'. */
61 : : #include "omp-general.h"
62 : : #include "omp-offload.h" /* For offload_vars. */
63 : : #include "c-parser.h"
64 : : #include "gcc-urlifier.h"
65 : :
66 : : #include "tree-pretty-print.h"
67 : :
68 : : /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 : : enum decl_context
70 : : { NORMAL, /* Ordinary declaration */
71 : : FUNCDEF, /* Function definition */
72 : : PARM, /* Declaration of parm before function body */
73 : : FIELD, /* Declaration inside struct or union */
74 : : TYPENAME}; /* Typename (inside cast or sizeof) */
75 : :
76 : : /* States indicating how grokdeclarator() should handle declspecs marked
77 : : with __attribute__((deprecated)) or __attribute__((unavailable)).
78 : : An object declared as __attribute__((unavailable)) should suppress
79 : : any reports of being declared with unavailable or deprecated items.
80 : : An object declared as __attribute__((deprecated)) should suppress
81 : : warnings of uses of other deprecated items. */
82 : :
83 : : enum deprecated_states {
84 : : DEPRECATED_NORMAL,
85 : : DEPRECATED_SUPPRESS,
86 : : UNAVAILABLE_DEPRECATED_SUPPRESS
87 : : };
88 : :
89 : :
90 : : /* Nonzero if we have seen an invalid cross reference
91 : : to a struct, union, or enum, but not yet printed the message. */
92 : : tree pending_invalid_xref;
93 : :
94 : : /* File and line to appear in the eventual error message. */
95 : : location_t pending_invalid_xref_location;
96 : :
97 : : /* The file and line that the prototype came from if this is an
98 : : old-style definition; used for diagnostics in
99 : : store_parm_decls_oldstyle. */
100 : :
101 : : static location_t current_function_prototype_locus;
102 : :
103 : : /* Whether this prototype was built-in. */
104 : :
105 : : static bool current_function_prototype_built_in;
106 : :
107 : : /* The argument type information of this prototype. */
108 : :
109 : : static tree current_function_prototype_arg_types;
110 : :
111 : : /* The argument information structure for the function currently being
112 : : defined. */
113 : :
114 : : static struct c_arg_info *current_function_arg_info;
115 : :
116 : : /* The obstack on which parser and related data structures, which are
117 : : not live beyond their top-level declaration or definition, are
118 : : allocated. */
119 : : struct obstack parser_obstack;
120 : :
121 : : /* The current statement tree. */
122 : :
123 : : static GTY(()) struct stmt_tree_s c_stmt_tree;
124 : :
125 : : /* Zero if we are not in an iteration or switch statement, otherwise
126 : : a bitmask. See bitmask definitions in c-tree.h. */
127 : : unsigned char in_statement;
128 : :
129 : : /* A list of decls to be made automatically visible in each file scope. */
130 : : static GTY(()) tree visible_builtins;
131 : :
132 : : /* Set to 0 at beginning of a function definition, set to 1 if
133 : : a return statement that specifies a return value is seen. */
134 : :
135 : : int current_function_returns_value;
136 : :
137 : : /* Set to 0 at beginning of a function definition, set to 1 if
138 : : a return statement with no argument is seen. */
139 : :
140 : : int current_function_returns_null;
141 : :
142 : : /* Set to 0 at beginning of a function definition, set to 1 if
143 : : a call to a noreturn function is seen. */
144 : :
145 : : int current_function_returns_abnormally;
146 : :
147 : : /* Set to nonzero by `grokdeclarator' for a function
148 : : whose return type is defaulted, if warnings for this are desired. */
149 : :
150 : : static int warn_about_return_type;
151 : :
152 : : /* Nonzero when the current toplevel function contains a declaration
153 : : of a nested function which is never defined. */
154 : :
155 : : static bool undef_nested_function;
156 : :
157 : : /* Vector of implicit "omp declare target" attributes to be added into
158 : : the attribute lists. */
159 : : vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160 : :
161 : : /* Vector of
162 : : #pragma omp begin assumes ... #pragma omp end assumes regions
163 : : we are in. */
164 : : vec<c_omp_begin_assumes_data, va_gc> *current_omp_begin_assumes;
165 : :
166 : : /* Vector of loop names with C_DECL_LOOP_NAME or C_DECL_SWITCH_NAME marked
167 : : LABEL_DECL as the last and canonical for each loop or switch. */
168 : : static vec<tree> loop_names;
169 : :
170 : : /* Hash table mapping LABEL_DECLs to the canonical LABEL_DECLs if LOOP_NAMES
171 : : vector becomes too long. */
172 : : static decl_tree_map *loop_names_hash;
173 : :
174 : : /* Each c_binding structure describes one binding of an identifier to
175 : : a decl. All the decls in a scope - irrespective of namespace - are
176 : : chained together by the ->prev field, which (as the name implies)
177 : : runs in reverse order. All the decls in a given namespace bound to
178 : : a given identifier are chained by the ->shadowed field, which runs
179 : : from inner to outer scopes.
180 : :
181 : : The ->decl field usually points to a DECL node, but there are two
182 : : exceptions. In the namespace of type tags, the bound entity is a
183 : : RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
184 : : identifier is encountered, it is bound to error_mark_node to
185 : : suppress further errors about that identifier in the current
186 : : function.
187 : :
188 : : The ->u.type field stores the type of the declaration in this scope;
189 : : if NULL, the type is the type of the ->decl field. This is only of
190 : : relevance for objects with external or internal linkage which may
191 : : be redeclared in inner scopes, forming composite types that only
192 : : persist for the duration of those scopes. In the external scope,
193 : : this stores the composite of all the types declared for this
194 : : object, visible or not. The ->inner_comp field (used only at file
195 : : scope) stores whether an incomplete array type at file scope was
196 : : completed at an inner scope to an array size other than 1.
197 : :
198 : : The ->u.label field is used for labels. It points to a structure
199 : : which stores additional information used for warnings.
200 : :
201 : : The depth field is copied from the scope structure that holds this
202 : : decl. It is used to preserve the proper ordering of the ->shadowed
203 : : field (see bind()) and also for a handful of special-case checks.
204 : : Finally, the invisible bit is true for a decl which should be
205 : : ignored for purposes of normal name lookup, and the nested bit is
206 : : true for a decl that's been bound a second time in an inner scope;
207 : : in all such cases, the binding in the outer scope will have its
208 : : invisible bit true. */
209 : :
210 : : struct GTY((chain_next ("%h.prev"))) c_binding {
211 : : union GTY(()) { /* first so GTY desc can use decl */
212 : : tree GTY((tag ("0"))) type; /* the type in this scope */
213 : : struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
214 : : } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
215 : : tree decl; /* the decl bound */
216 : : tree id; /* the identifier it's bound to */
217 : : struct c_binding *prev; /* the previous decl in this scope */
218 : : struct c_binding *shadowed; /* the innermost decl shadowed by this one */
219 : : unsigned int depth : 28; /* depth of this scope */
220 : : BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
221 : : BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
222 : : BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
223 : : BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
224 : : location_t locus; /* location for nested bindings */
225 : : };
226 : : #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
227 : : #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
228 : : #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
229 : : #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
230 : :
231 : : /* Each C symbol points to three linked lists of c_binding structures.
232 : : These describe the values of the identifier in the three different
233 : : namespaces defined by the language. */
234 : :
235 : : struct GTY(()) lang_identifier {
236 : : struct c_common_identifier common_id;
237 : : struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
238 : : struct c_binding *tag_binding; /* struct/union/enum tags */
239 : : struct c_binding *label_binding; /* labels */
240 : : };
241 : :
242 : : /* Validate c-lang.cc's assumptions. */
243 : : extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
244 : : [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
245 : :
246 : : /* The binding oracle; see c-tree.h. */
247 : : void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
248 : :
249 : : /* This flag is set on an identifier if we have previously asked the
250 : : binding oracle for this identifier's symbol binding. */
251 : : #define I_SYMBOL_CHECKED(node) \
252 : : (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
253 : :
254 : : static inline struct c_binding* *
255 : 4518557713 : i_symbol_binding (tree node)
256 : : {
257 : 4518557713 : struct lang_identifier *lid
258 : 4518557713 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
259 : :
260 : 4518557713 : if (lid->symbol_binding == NULL
261 : 1661270250 : && c_binding_oracle != NULL
262 : 4518557713 : && !I_SYMBOL_CHECKED (node))
263 : : {
264 : : /* Set the "checked" flag first, to avoid infinite recursion
265 : : when the binding oracle calls back into gcc. */
266 : 0 : I_SYMBOL_CHECKED (node) = 1;
267 : 0 : c_binding_oracle (C_ORACLE_SYMBOL, node);
268 : : }
269 : :
270 : 4518557713 : return &lid->symbol_binding;
271 : : }
272 : :
273 : : #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
274 : :
275 : : #define I_SYMBOL_DECL(node) \
276 : : (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
277 : :
278 : : /* This flag is set on an identifier if we have previously asked the
279 : : binding oracle for this identifier's tag binding. */
280 : : #define I_TAG_CHECKED(node) \
281 : : (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
282 : :
283 : : static inline struct c_binding **
284 : 4457132 : i_tag_binding (tree node)
285 : : {
286 : 4457132 : struct lang_identifier *lid
287 : 4457132 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
288 : :
289 : 4457132 : if (lid->tag_binding == NULL
290 : 1234273 : && c_binding_oracle != NULL
291 : 4457132 : && !I_TAG_CHECKED (node))
292 : : {
293 : : /* Set the "checked" flag first, to avoid infinite recursion
294 : : when the binding oracle calls back into gcc. */
295 : 0 : I_TAG_CHECKED (node) = 1;
296 : 0 : c_binding_oracle (C_ORACLE_TAG, node);
297 : : }
298 : :
299 : 4457132 : return &lid->tag_binding;
300 : : }
301 : :
302 : : #define I_TAG_BINDING(node) (*i_tag_binding (node))
303 : :
304 : : #define I_TAG_DECL(node) \
305 : : (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
306 : :
307 : : /* This flag is set on an identifier if we have previously asked the
308 : : binding oracle for this identifier's label binding. */
309 : : #define I_LABEL_CHECKED(node) \
310 : : (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
311 : :
312 : : static inline struct c_binding **
313 : 358133 : i_label_binding (tree node)
314 : : {
315 : 358133 : struct lang_identifier *lid
316 : 358133 : = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
317 : :
318 : 358133 : if (lid->label_binding == NULL
319 : 47476 : && c_binding_oracle != NULL
320 : 358133 : && !I_LABEL_CHECKED (node))
321 : : {
322 : : /* Set the "checked" flag first, to avoid infinite recursion
323 : : when the binding oracle calls back into gcc. */
324 : 0 : I_LABEL_CHECKED (node) = 1;
325 : 0 : c_binding_oracle (C_ORACLE_LABEL, node);
326 : : }
327 : :
328 : 358133 : return &lid->label_binding;
329 : : }
330 : :
331 : : #define I_LABEL_BINDING(node) (*i_label_binding (node))
332 : :
333 : : #define I_LABEL_DECL(node) \
334 : : (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
335 : :
336 : : /* Used by C_TOKEN_VEC tree. */
337 : : struct GTY (()) c_tree_token_vec {
338 : : struct tree_base base;
339 : : vec<c_token, va_gc> *tokens;
340 : : };
341 : :
342 : : STATIC_ASSERT (sizeof (c_tree_token_vec) == sizeof (c_tree_token_vec_struct));
343 : : STATIC_ASSERT (offsetof (c_tree_token_vec, tokens)
344 : : == offsetof (c_tree_token_vec_struct, tokens));
345 : :
346 : : /* The resulting tree type. */
347 : :
348 : : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE + 2 * (TREE_CODE (&%h.generic) == C_TOKEN_VEC)"),
349 : : chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
350 : : {
351 : : union tree_node GTY ((tag ("0"),
352 : : desc ("tree_node_structure (&%h)")))
353 : : generic;
354 : : struct lang_identifier GTY ((tag ("1"))) identifier;
355 : : struct c_tree_token_vec GTY ((tag ("2"))) c_token_vec;
356 : : };
357 : :
358 : : /* Langhook for tree_size. */
359 : : size_t
360 : 901 : c_tree_size (enum tree_code code)
361 : : {
362 : 901 : gcc_checking_assert (code >= NUM_TREE_CODES);
363 : 901 : switch (code)
364 : : {
365 : : case C_TOKEN_VEC: return sizeof (c_tree_token_vec);
366 : 0 : default:
367 : 0 : switch (TREE_CODE_CLASS (code))
368 : : {
369 : : case tcc_declaration: return sizeof (tree_decl_non_common);
370 : 0 : case tcc_type: return sizeof (tree_type_non_common);
371 : 0 : default: gcc_unreachable ();
372 : : }
373 : : }
374 : : }
375 : :
376 : : /* Track bindings and other things that matter for goto warnings. For
377 : : efficiency, we do not gather all the decls at the point of
378 : : definition. Instead, we point into the bindings structure. As
379 : : scopes are popped, we update these structures and gather the decls
380 : : that matter at that time. */
381 : :
382 : : struct GTY(()) c_spot_bindings {
383 : : /* The currently open scope which holds bindings defined when the
384 : : label was defined or the goto statement was found. */
385 : : struct c_scope *scope;
386 : : /* The bindings in the scope field which were defined at the point
387 : : of the label or goto. This lets us look at older or newer
388 : : bindings in the scope, as appropriate. */
389 : : struct c_binding *bindings_in_scope;
390 : : /* The number of statement expressions that have started since this
391 : : label or goto statement was defined. This is zero if we are at
392 : : the same statement expression level. It is positive if we are in
393 : : a statement expression started since this spot. It is negative
394 : : if this spot was in a statement expression and we have left
395 : : it. */
396 : : int stmt_exprs;
397 : : /* Whether we started in a statement expression but are no longer in
398 : : it. This is set to true if stmt_exprs ever goes negative. */
399 : : bool left_stmt_expr;
400 : : };
401 : :
402 : : /* This structure is used to keep track of bindings seen when a goto
403 : : statement is defined. This is only used if we see the goto
404 : : statement before we see the label. */
405 : :
406 : : struct GTY(()) c_goto_bindings {
407 : : /* The location of the goto statement. */
408 : : location_t loc;
409 : : /* The bindings of the goto statement. */
410 : : struct c_spot_bindings goto_bindings;
411 : : };
412 : :
413 : : typedef struct c_goto_bindings *c_goto_bindings_p;
414 : :
415 : : /* The additional information we keep track of for a label binding.
416 : : These fields are updated as scopes are popped. */
417 : :
418 : : struct GTY(()) c_label_vars {
419 : : /* The shadowed c_label_vars, when one label shadows another (which
420 : : can only happen using a __label__ declaration). */
421 : : struct c_label_vars *shadowed;
422 : : /* The bindings when the label was defined. */
423 : : struct c_spot_bindings label_bindings;
424 : : /* A list of decls that we care about: decls about which we should
425 : : warn if a goto branches to this label from later in the function.
426 : : Decls are added to this list as scopes are popped. We only add
427 : : the decls that matter. */
428 : : vec<tree, va_gc> *decls_in_scope;
429 : : /* A list of goto statements to this label. This is only used for
430 : : goto statements seen before the label was defined, so that we can
431 : : issue appropriate warnings for them. */
432 : : vec<c_goto_bindings_p, va_gc> *gotos;
433 : : };
434 : :
435 : : /* Each c_scope structure describes the complete contents of one
436 : : scope. Four scopes are distinguished specially: the innermost or
437 : : current scope, the innermost function scope, the file scope (always
438 : : the second to outermost) and the outermost or external scope.
439 : :
440 : : Most declarations are recorded in the current scope.
441 : :
442 : : All normal label declarations are recorded in the innermost
443 : : function scope, as are bindings of undeclared identifiers to
444 : : error_mark_node. (GCC permits nested functions as an extension,
445 : : hence the 'innermost' qualifier.) Explicitly declared labels
446 : : (using the __label__ extension) appear in the current scope.
447 : :
448 : : Being in the file scope (current_scope == file_scope) causes
449 : : special behavior in several places below. Also, under some
450 : : conditions the Objective-C front end records declarations in the
451 : : file scope even though that isn't the current scope.
452 : :
453 : : All declarations with external linkage are recorded in the external
454 : : scope, even if they aren't visible there; this models the fact that
455 : : such declarations are visible to the entire program, and (with a
456 : : bit of cleverness, see pushdecl) allows diagnosis of some violations
457 : : of C99 6.2.2p7 and 6.2.7p2:
458 : :
459 : : If, within the same translation unit, the same identifier appears
460 : : with both internal and external linkage, the behavior is
461 : : undefined.
462 : :
463 : : All declarations that refer to the same object or function shall
464 : : have compatible type; otherwise, the behavior is undefined.
465 : :
466 : : Initially only the built-in declarations, which describe compiler
467 : : intrinsic functions plus a subset of the standard library, are in
468 : : this scope.
469 : :
470 : : The order of the blocks list matters, and it is frequently appended
471 : : to. To avoid having to walk all the way to the end of the list on
472 : : each insertion, or reverse the list later, we maintain a pointer to
473 : : the last list entry. (FIXME: It should be feasible to use a reversed
474 : : list here.)
475 : :
476 : : The bindings list is strictly in reverse order of declarations;
477 : : pop_scope relies on this. */
478 : :
479 : :
480 : : struct GTY((chain_next ("%h.outer"))) c_scope {
481 : : /* The scope containing this one. */
482 : : struct c_scope *outer;
483 : :
484 : : /* The next outermost function scope. */
485 : : struct c_scope *outer_function;
486 : :
487 : : /* All bindings in this scope. */
488 : : struct c_binding *bindings;
489 : :
490 : : /* For each scope (except the global one), a chain of BLOCK nodes
491 : : for all the scopes that were entered and exited one level down. */
492 : : tree blocks;
493 : : tree blocks_last;
494 : :
495 : : /* The depth of this scope. Used to keep the ->shadowed chain of
496 : : bindings sorted innermost to outermost. */
497 : : unsigned int depth : 28;
498 : :
499 : : /* True if we are currently filling this scope with parameter
500 : : declarations. */
501 : : BOOL_BITFIELD parm_flag : 1;
502 : :
503 : : /* True if we saw [*] in this scope. Used to give an error messages
504 : : if these appears in a function definition. */
505 : : BOOL_BITFIELD had_vla_unspec : 1;
506 : :
507 : : /* True if we parsed a list of forward parameter decls in this scope. */
508 : : BOOL_BITFIELD had_forward_parm_decls : 1;
509 : :
510 : : /* True if this is the outermost block scope of a function body.
511 : : This scope contains the parameters, the local variables declared
512 : : in the outermost block, and all the labels (except those in
513 : : nested functions, or declared at block scope with __label__). */
514 : : BOOL_BITFIELD function_body : 1;
515 : :
516 : : /* True means make a BLOCK for this scope no matter what. */
517 : : BOOL_BITFIELD keep : 1;
518 : :
519 : : /* True means that an unsuffixed float constant is _Decimal64. */
520 : : BOOL_BITFIELD float_const_decimal64 : 1;
521 : :
522 : : /* True if this scope has any label bindings. This is used to speed
523 : : up searching for labels when popping scopes, particularly since
524 : : labels are normally only found at function scope. */
525 : : BOOL_BITFIELD has_label_bindings : 1;
526 : :
527 : : /* True if we should issue a warning if a goto statement crosses any
528 : : of the bindings. We still need to check the list of bindings to
529 : : find the specific ones we need to warn about. This is true if
530 : : decl_jump_unsafe would return true for any of the bindings. This
531 : : is used to avoid looping over all the bindings unnecessarily. */
532 : : BOOL_BITFIELD has_jump_unsafe_decl : 1;
533 : : };
534 : :
535 : : /* The scope currently in effect. */
536 : :
537 : : static GTY(()) struct c_scope *current_scope;
538 : :
539 : : /* The innermost function scope. Ordinary (not explicitly declared)
540 : : labels, bindings to error_mark_node, and the lazily-created
541 : : bindings of __func__ and its friends get this scope. */
542 : :
543 : : static GTY(()) struct c_scope *current_function_scope;
544 : :
545 : : /* The C file scope. This is reset for each input translation unit. */
546 : :
547 : : static GTY(()) struct c_scope *file_scope;
548 : :
549 : : /* The outermost scope. This is used for all declarations with
550 : : external linkage, and only these, hence the name. */
551 : :
552 : : static GTY(()) struct c_scope *external_scope;
553 : :
554 : : /* A chain of c_scope structures awaiting reuse. */
555 : :
556 : : static GTY((deletable)) struct c_scope *scope_freelist;
557 : :
558 : : /* A chain of c_binding structures awaiting reuse. */
559 : :
560 : : static GTY((deletable)) struct c_binding *binding_freelist;
561 : :
562 : : /* Append VAR to LIST in scope SCOPE. */
563 : : #define SCOPE_LIST_APPEND(scope, list, decl) do { \
564 : : struct c_scope *s_ = (scope); \
565 : : tree d_ = (decl); \
566 : : if (s_->list##_last) \
567 : : BLOCK_CHAIN (s_->list##_last) = d_; \
568 : : else \
569 : : s_->list = d_; \
570 : : s_->list##_last = d_; \
571 : : } while (0)
572 : :
573 : : /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
574 : : #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
575 : : struct c_scope *t_ = (tscope); \
576 : : struct c_scope *f_ = (fscope); \
577 : : if (t_->to##_last) \
578 : : BLOCK_CHAIN (t_->to##_last) = f_->from; \
579 : : else \
580 : : t_->to = f_->from; \
581 : : t_->to##_last = f_->from##_last; \
582 : : } while (0)
583 : :
584 : : /* A c_inline_static structure stores details of a static identifier
585 : : referenced in a definition of a function that may be an inline
586 : : definition if no subsequent declaration of that function uses
587 : : "extern" or does not use "inline". */
588 : :
589 : : struct GTY((chain_next ("%h.next"))) c_inline_static {
590 : : /* The location for a diagnostic. */
591 : : location_t location;
592 : :
593 : : /* The function that may be an inline definition. */
594 : : tree function;
595 : :
596 : : /* The object or function referenced. */
597 : : tree static_decl;
598 : :
599 : : /* What sort of reference this is. */
600 : : enum c_inline_static_type type;
601 : :
602 : : /* The next such structure or NULL. */
603 : : struct c_inline_static *next;
604 : : };
605 : :
606 : : /* List of static identifiers used or referenced in functions that may
607 : : be inline definitions. */
608 : : static GTY(()) struct c_inline_static *c_inline_statics;
609 : :
610 : : /* True means unconditionally make a BLOCK for the next scope pushed. */
611 : :
612 : : static bool keep_next_level_flag;
613 : :
614 : : /* True means the next call to push_scope will be the outermost scope
615 : : of a function body, so do not push a new scope, merely cease
616 : : expecting parameter decls. */
617 : :
618 : : static bool next_is_function_body;
619 : :
620 : : /* A vector of pointers to c_binding structures. */
621 : :
622 : : typedef struct c_binding *c_binding_ptr;
623 : :
624 : : /* Information that we keep for a struct or union while it is being
625 : : parsed. */
626 : :
627 : 1157722 : class c_struct_parse_info
628 : : {
629 : : public:
630 : : /* If warn_cxx_compat, a list of types defined within this
631 : : struct. */
632 : : auto_vec<tree> struct_types;
633 : : /* If warn_cxx_compat, a list of field names which have bindings,
634 : : and which are defined in this struct, but which are not defined
635 : : in any enclosing struct. This is used to clear the in_struct
636 : : field of the c_bindings structure. */
637 : : auto_vec<c_binding_ptr> fields;
638 : : /* If warn_cxx_compat, a list of typedef names used when defining
639 : : fields in this struct. */
640 : : auto_vec<tree> typedefs_seen;
641 : : /* The location of a previous definition of this struct. */
642 : : location_t refloc;
643 : : };
644 : :
645 : :
646 : : /* Hash table for structs and unions. */
647 : : struct c_struct_hasher : ggc_ptr_hash<tree_node>
648 : : {
649 : : static hashval_t hash (tree t);
650 : : static bool equal (tree, tree);
651 : : };
652 : :
653 : : /* Hash an RECORD OR UNION. */
654 : : hashval_t
655 : 4218831 : c_struct_hasher::hash (tree type)
656 : : {
657 : 4218831 : inchash::hash hstate;
658 : :
659 : 4218831 : hstate.add_int (TREE_CODE (type));
660 : 4218831 : tree tag = c_type_tag (type);
661 : 4218831 : hstate.add_object (tag);
662 : :
663 : 4218831 : return hstate.end ();
664 : : }
665 : :
666 : : /* Compare two RECORD or UNION types. */
667 : : bool
668 : 14313466 : c_struct_hasher::equal (tree t1, tree t2)
669 : : {
670 : 14313466 : return comptypes_equiv_p (t1, t2);
671 : : }
672 : :
673 : : /* All tagged typed so that TYPE_CANONICAL can be set correctly. */
674 : : static GTY (()) hash_table<c_struct_hasher> *c_struct_htab;
675 : :
676 : : /* Information for the struct or union currently being parsed, or
677 : : NULL if not parsing a struct or union. */
678 : : static class c_struct_parse_info *struct_parse_info;
679 : :
680 : : /* Forward declarations. */
681 : : static tree lookup_name_in_scope (tree, struct c_scope *);
682 : : static tree c_make_fname_decl (location_t, tree, int);
683 : : static tree grokdeclarator (const struct c_declarator *,
684 : : struct c_declspecs *,
685 : : enum decl_context, bool, tree *, tree *, tree *,
686 : : bool *, enum deprecated_states);
687 : : static tree grokparms (struct c_arg_info *, bool);
688 : : static void layout_array_type (tree);
689 : : static const char *header_for_builtin_fn (tree);
690 : :
691 : : /* T is a statement. Add it to the statement-tree. This is the
692 : : C/ObjC version--C++ has a slightly different version of this
693 : : function. */
694 : :
695 : : tree
696 : 91512874 : add_stmt (tree t)
697 : : {
698 : 91512874 : enum tree_code code = TREE_CODE (t);
699 : :
700 : 91512874 : if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
701 : : {
702 : 90814299 : if (!EXPR_HAS_LOCATION (t))
703 : 154790 : SET_EXPR_LOCATION (t, input_location);
704 : : }
705 : :
706 : : /* Add T to the statement-tree. Non-side-effect statements need to be
707 : : recorded during statement expressions. */
708 : 91512874 : if (!building_stmt_list_p ())
709 : 1 : push_stmt_list ();
710 : :
711 : 91512874 : if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
712 : 1073756 : STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
713 : :
714 : 91512874 : append_to_statement_list_force (t, &cur_stmt_list);
715 : :
716 : 91512874 : return t;
717 : : }
718 : :
719 : :
720 : :
721 : :
722 : :
723 : : /* Return true if we will want to say something if a goto statement
724 : : crosses DECL. */
725 : :
726 : : static bool
727 : 896366250 : decl_jump_unsafe (tree decl)
728 : : {
729 : 896366250 : if (error_operand_p (decl))
730 : : return false;
731 : :
732 : : /* Don't warn for compound literals. If a goto statement crosses
733 : : their initialization, it should cross also all the places where
734 : : the complit is used or where the complit address might be saved
735 : : into some variable, so code after the label to which goto jumps
736 : : should not be able to refer to the compound literal. */
737 : 896364048 : if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
738 : : return false;
739 : :
740 : 895466216 : if (flag_openmp
741 : 11879766 : && VAR_P (decl)
742 : 895497210 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
743 : : return true;
744 : :
745 : : /* Always warn about crossing variably modified types. */
746 : 885600731 : if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
747 : 907503790 : && c_type_variably_modified_p (TREE_TYPE (decl)))
748 : : return true;
749 : :
750 : : /* Otherwise, only warn if -Wgoto-misses-init and this is an
751 : : initialized automatic decl. */
752 : 895425829 : if (warn_jump_misses_init
753 : 5958139 : && VAR_P (decl)
754 : 10205 : && !TREE_STATIC (decl)
755 : 895431274 : && DECL_INITIAL (decl) != NULL_TREE)
756 : : return true;
757 : :
758 : : return false;
759 : : }
760 : :
761 : :
762 : : void
763 : 0 : c_print_identifier (FILE *file, tree node, int indent)
764 : : {
765 : 0 : void (*save) (enum c_oracle_request, tree identifier);
766 : :
767 : : /* Temporarily hide any binding oracle. Without this, calls to
768 : : debug_tree from the debugger will end up calling into the oracle,
769 : : making for a confusing debug session. As the oracle isn't needed
770 : : here for normal operation, it's simplest to suppress it. */
771 : 0 : save = c_binding_oracle;
772 : 0 : c_binding_oracle = NULL;
773 : :
774 : 0 : print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
775 : 0 : print_node (file, "tag", I_TAG_DECL (node), indent + 4);
776 : 0 : print_node (file, "label", I_LABEL_DECL (node), indent + 4);
777 : 0 : if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
778 : : {
779 : 0 : tree rid = ridpointers[C_RID_CODE (node)];
780 : 0 : indent_to (file, indent + 4);
781 : 0 : fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
782 : 0 : (void *) rid, IDENTIFIER_POINTER (rid));
783 : : }
784 : :
785 : 0 : c_binding_oracle = save;
786 : 0 : }
787 : :
788 : : /* Establish that the scope contains declarations that are sensitive to
789 : : jumps that cross a binding. Together with decl_jump_unsafe, this is
790 : : used to diagnose such jumps. */
791 : : void
792 : 69 : c_mark_decl_jump_unsafe_in_current_scope ()
793 : : {
794 : 69 : current_scope->has_jump_unsafe_decl = 1;
795 : 69 : }
796 : :
797 : : /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
798 : : which may be any of several kinds of DECL or TYPE or error_mark_node,
799 : : in the scope SCOPE. */
800 : : static void
801 : 896217150 : bind (tree name, tree decl, struct c_scope *scope, bool invisible,
802 : : bool nested, location_t locus)
803 : : {
804 : 896217150 : struct c_binding *b, **here;
805 : :
806 : 896217150 : if (binding_freelist)
807 : : {
808 : 224800821 : b = binding_freelist;
809 : 224800821 : binding_freelist = b->prev;
810 : : }
811 : : else
812 : 671416329 : b = ggc_alloc<c_binding> ();
813 : :
814 : 896217150 : b->shadowed = 0;
815 : 896217150 : b->decl = decl;
816 : 896217150 : b->id = name;
817 : 896217150 : b->depth = scope->depth;
818 : 896217150 : b->invisible = invisible;
819 : 896217150 : b->nested = nested;
820 : 896217150 : b->inner_comp = 0;
821 : 896217150 : b->in_struct = 0;
822 : 896217150 : b->locus = locus;
823 : :
824 : 896217150 : b->u.type = NULL;
825 : :
826 : 896217150 : b->prev = scope->bindings;
827 : 896217150 : scope->bindings = b;
828 : :
829 : 896217150 : if (decl_jump_unsafe (decl))
830 : 24095 : scope->has_jump_unsafe_decl = 1;
831 : :
832 : 896217150 : if (!name)
833 : : return;
834 : :
835 : 887735341 : switch (TREE_CODE (decl))
836 : : {
837 : 23763 : case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
838 : 616430 : case ENUMERAL_TYPE:
839 : 616430 : case UNION_TYPE:
840 : 616430 : case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
841 : 887095148 : case VAR_DECL:
842 : 887095148 : case FUNCTION_DECL:
843 : 887095148 : case TYPE_DECL:
844 : 887095148 : case CONST_DECL:
845 : 887095148 : case PARM_DECL:
846 : 887095148 : case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
847 : :
848 : 0 : default:
849 : 0 : gcc_unreachable ();
850 : : }
851 : :
852 : : /* Locate the appropriate place in the chain of shadowed decls
853 : : to insert this binding. Normally, scope == current_scope and
854 : : this does nothing. */
855 : 887735385 : while (*here && (*here)->depth > scope->depth)
856 : 44 : here = &(*here)->shadowed;
857 : :
858 : 887735341 : b->shadowed = *here;
859 : 887735341 : *here = b;
860 : : }
861 : :
862 : : /* Clear the binding structure B, stick it on the binding_freelist,
863 : : and return the former value of b->prev. This is used by pop_scope
864 : : and get_parm_info to iterate destructively over all the bindings
865 : : from a given scope. */
866 : : static struct c_binding *
867 : 879201969 : free_binding_and_advance (struct c_binding *b)
868 : : {
869 : 879201969 : struct c_binding *prev = b->prev;
870 : :
871 : 879201969 : memset (b, 0, sizeof (struct c_binding));
872 : 879201969 : b->prev = binding_freelist;
873 : 879201969 : binding_freelist = b;
874 : :
875 : 879201969 : return prev;
876 : : }
877 : :
878 : : /* Bind a label. Like bind, but skip fields which aren't used for
879 : : labels, and add the LABEL_VARS value. */
880 : : static void
881 : 23763 : bind_label (tree name, tree label, struct c_scope *scope,
882 : : struct c_label_vars *label_vars)
883 : : {
884 : 23763 : struct c_binding *b;
885 : :
886 : 23763 : bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
887 : : UNKNOWN_LOCATION);
888 : :
889 : 23763 : scope->has_label_bindings = true;
890 : :
891 : 23763 : b = scope->bindings;
892 : 23763 : gcc_assert (b->decl == label);
893 : 23763 : label_vars->shadowed = b->u.label;
894 : 23763 : b->u.label = label_vars;
895 : 23763 : }
896 : :
897 : : /* Hook called at end of compilation to assume 1 elt
898 : : for a file-scope tentative array defn that wasn't complete before. */
899 : :
900 : : void
901 : 7477 : c_finish_incomplete_decl (tree decl)
902 : : {
903 : 7477 : if (VAR_P (decl))
904 : : {
905 : 7477 : tree type = TREE_TYPE (decl);
906 : 7477 : if (type != error_mark_node
907 : 7402 : && TREE_CODE (type) == ARRAY_TYPE
908 : 7207 : && !DECL_EXTERNAL (decl)
909 : 7565 : && TYPE_DOMAIN (type) == NULL_TREE)
910 : : {
911 : 88 : if (flag_isoc2y && !TREE_PUBLIC (decl))
912 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
913 : : "array size missing in %q+D", decl);
914 : : else
915 : 86 : warning_at (DECL_SOURCE_LOCATION (decl),
916 : 86 : 0, "array %q+D assumed to have one element", decl);
917 : :
918 : 88 : complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
919 : :
920 : 88 : relayout_decl (decl);
921 : : }
922 : : }
923 : 7477 : }
924 : :
925 : : /* Record that inline function FUNC contains a reference (location
926 : : LOC) to static DECL (file-scope or function-local according to
927 : : TYPE). */
928 : :
929 : : void
930 : 36 : record_inline_static (location_t loc, tree func, tree decl,
931 : : enum c_inline_static_type type)
932 : : {
933 : 36 : c_inline_static *csi = ggc_alloc<c_inline_static> ();
934 : 36 : csi->location = loc;
935 : 36 : csi->function = func;
936 : 36 : csi->static_decl = decl;
937 : 36 : csi->type = type;
938 : 36 : csi->next = c_inline_statics;
939 : 36 : c_inline_statics = csi;
940 : 36 : }
941 : :
942 : : /* Check for references to static declarations in inline functions at
943 : : the end of the translation unit and diagnose them if the functions
944 : : are still inline definitions. */
945 : :
946 : : static void
947 : 107523 : check_inline_statics (void)
948 : : {
949 : 107523 : struct c_inline_static *csi;
950 : 107559 : for (csi = c_inline_statics; csi; csi = csi->next)
951 : : {
952 : 36 : if (DECL_EXTERNAL (csi->function))
953 : 30 : switch (csi->type)
954 : : {
955 : 23 : case csi_internal:
956 : 23 : pedwarn (csi->location, 0,
957 : : "%qD is static but used in inline function %qD "
958 : : "which is not static", csi->static_decl, csi->function);
959 : 23 : break;
960 : 7 : case csi_modifiable:
961 : 7 : pedwarn (csi->location, 0,
962 : : "%q+D is static but declared in inline function %qD "
963 : : "which is not static", csi->static_decl, csi->function);
964 : 7 : break;
965 : 0 : default:
966 : 0 : gcc_unreachable ();
967 : : }
968 : : }
969 : 107523 : c_inline_statics = NULL;
970 : 107523 : }
971 : :
972 : : /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
973 : : for the current state, otherwise set it to uninitialized. */
974 : :
975 : : static void
976 : 133728 : set_spot_bindings (struct c_spot_bindings *p, bool defining)
977 : : {
978 : 0 : if (defining)
979 : : {
980 : 126691 : p->scope = current_scope;
981 : 16726 : p->bindings_in_scope = current_scope->bindings;
982 : : }
983 : : else
984 : : {
985 : 7037 : p->scope = NULL;
986 : 7037 : p->bindings_in_scope = NULL;
987 : : }
988 : 133728 : p->stmt_exprs = 0;
989 : 133728 : p->left_stmt_expr = false;
990 : 0 : }
991 : :
992 : : /* Update spot bindings P as we pop out of SCOPE. Return true if we
993 : : should push decls for a label. */
994 : :
995 : : static bool
996 : 470605652 : update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
997 : : {
998 : 0 : if (p->scope != scope)
999 : : {
1000 : : /* This label or goto is defined in some other scope, or it is a
1001 : : label which is not yet defined. There is nothing to
1002 : : update. */
1003 : : return false;
1004 : : }
1005 : :
1006 : : /* Adjust the spot bindings to refer to the bindings already defined
1007 : : in the enclosing scope. */
1008 : 204926 : p->scope = scope->outer;
1009 : 204926 : p->bindings_in_scope = p->scope->bindings;
1010 : :
1011 : 151226 : return true;
1012 : : }
1013 : :
1014 : : /* The Objective-C front-end often needs to determine the current scope. */
1015 : :
1016 : : void *
1017 : 0 : objc_get_current_scope (void)
1018 : : {
1019 : 0 : return current_scope;
1020 : : }
1021 : :
1022 : : /* The following function is used only by Objective-C. It needs to live here
1023 : : because it accesses the innards of c_scope. */
1024 : :
1025 : : void
1026 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
1027 : : {
1028 : 0 : struct c_scope *scope;
1029 : 0 : struct c_binding *b;
1030 : :
1031 : 0 : for (scope = current_scope;
1032 : 0 : scope && scope != enclosing_blk;
1033 : 0 : scope = scope->outer)
1034 : : {
1035 : 0 : for (b = scope->bindings; b; b = b->prev)
1036 : 0 : objc_volatilize_decl (b->decl);
1037 : :
1038 : : /* Do not climb up past the current function. */
1039 : 0 : if (scope->function_body)
1040 : : break;
1041 : : }
1042 : 0 : }
1043 : :
1044 : : /* Return true if we are in the global binding level. */
1045 : :
1046 : : bool
1047 : 2066700 : global_bindings_p (void)
1048 : : {
1049 : 2066700 : return current_scope == file_scope;
1050 : : }
1051 : :
1052 : : /* Return true if we're declaring parameters in an old-style function
1053 : : declaration. */
1054 : :
1055 : : bool
1056 : 34746 : old_style_parameter_scope (void)
1057 : : {
1058 : : /* If processing parameters and there is no function statement list, we
1059 : : * have an old-style function declaration. */
1060 : 34746 : return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
1061 : : }
1062 : :
1063 : : void
1064 : 51927 : keep_next_level (void)
1065 : : {
1066 : 51927 : keep_next_level_flag = true;
1067 : 51927 : }
1068 : :
1069 : : /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
1070 : :
1071 : : void
1072 : 20 : set_float_const_decimal64 (void)
1073 : : {
1074 : 20 : current_scope->float_const_decimal64 = true;
1075 : 20 : }
1076 : :
1077 : : /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1078 : :
1079 : : void
1080 : 28 : clear_float_const_decimal64 (void)
1081 : : {
1082 : 28 : current_scope->float_const_decimal64 = false;
1083 : 28 : }
1084 : :
1085 : : /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1086 : :
1087 : : bool
1088 : 589849 : float_const_decimal64_p (void)
1089 : : {
1090 : 589849 : return current_scope->float_const_decimal64;
1091 : : }
1092 : :
1093 : : /* Identify this scope as currently being filled with parameters. */
1094 : :
1095 : : void
1096 : 84806755 : declare_parm_level (void)
1097 : : {
1098 : 84806755 : current_scope->parm_flag = true;
1099 : 84806755 : }
1100 : :
1101 : : void
1102 : 125066604 : push_scope (void)
1103 : : {
1104 : 125066604 : if (next_is_function_body)
1105 : : {
1106 : : /* This is the transition from the parameters to the top level
1107 : : of the function body. These are the same scope
1108 : : (C99 6.2.1p4,6) so we do not push another scope structure.
1109 : : next_is_function_body is set only by store_parm_decls, which
1110 : : in turn is called when and only when we are about to
1111 : : encounter the opening curly brace for the function body.
1112 : :
1113 : : The outermost block of a function always gets a BLOCK node,
1114 : : because the debugging output routines expect that each
1115 : : function has at least one BLOCK. */
1116 : 35377253 : current_scope->parm_flag = false;
1117 : 35377253 : current_scope->function_body = true;
1118 : 35377253 : current_scope->keep = true;
1119 : 35377253 : current_scope->outer_function = current_function_scope;
1120 : 35377253 : current_function_scope = current_scope;
1121 : :
1122 : 35377253 : keep_next_level_flag = false;
1123 : 35377253 : next_is_function_body = false;
1124 : :
1125 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1126 : 35377253 : if (current_scope->outer)
1127 : 35377253 : current_scope->float_const_decimal64
1128 : 35377253 : = current_scope->outer->float_const_decimal64;
1129 : : else
1130 : 0 : current_scope->float_const_decimal64 = false;
1131 : : }
1132 : : else
1133 : : {
1134 : 89689351 : struct c_scope *scope;
1135 : 89689351 : if (scope_freelist)
1136 : : {
1137 : 88845773 : scope = scope_freelist;
1138 : 88845773 : scope_freelist = scope->outer;
1139 : : }
1140 : : else
1141 : 843578 : scope = ggc_cleared_alloc<c_scope> ();
1142 : :
1143 : : /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1144 : 89689351 : if (current_scope)
1145 : 89576284 : scope->float_const_decimal64 = current_scope->float_const_decimal64;
1146 : : else
1147 : 113067 : scope->float_const_decimal64 = false;
1148 : :
1149 : 89689351 : scope->keep = keep_next_level_flag;
1150 : 89689351 : scope->outer = current_scope;
1151 : 89689351 : scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1152 : :
1153 : : /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1154 : : possible. */
1155 : 89689351 : if (current_scope && scope->depth == 0)
1156 : : {
1157 : 0 : scope->depth--;
1158 : 0 : sorry ("GCC supports only %u nested scopes", scope->depth);
1159 : : }
1160 : :
1161 : 89689351 : current_scope = scope;
1162 : 89689351 : keep_next_level_flag = false;
1163 : : }
1164 : 125066604 : }
1165 : :
1166 : : /* This is called when we are leaving SCOPE. For each label defined
1167 : : in SCOPE, add any appropriate decls to its decls_in_scope fields.
1168 : : These are the decls whose initialization will be skipped by a goto
1169 : : later in the function. */
1170 : :
1171 : : static void
1172 : 89682906 : update_label_decls (struct c_scope *scope)
1173 : : {
1174 : 89682906 : struct c_scope *s;
1175 : :
1176 : 89682906 : s = scope;
1177 : 325350510 : while (s != NULL)
1178 : : {
1179 : 275722021 : if (s->has_label_bindings)
1180 : : {
1181 : 231098 : struct c_binding *b;
1182 : :
1183 : 2132760 : for (b = s->bindings; b != NULL; b = b->prev)
1184 : : {
1185 : 1901662 : struct c_label_vars *label_vars;
1186 : 1901662 : struct c_binding *b1;
1187 : 1901662 : bool hjud;
1188 : 1901662 : unsigned int ix;
1189 : 1901662 : struct c_goto_bindings *g;
1190 : :
1191 : 1901662 : if (TREE_CODE (b->decl) != LABEL_DECL)
1192 : 1399687 : continue;
1193 : 501975 : label_vars = b->u.label;
1194 : :
1195 : 501975 : b1 = label_vars->label_bindings.bindings_in_scope;
1196 : 501975 : if (label_vars->label_bindings.scope == NULL)
1197 : : hjud = false;
1198 : : else
1199 : 193011 : hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1200 : 501975 : if (update_spot_bindings (scope, &label_vars->label_bindings))
1201 : : {
1202 : : /* This label is defined in this scope. */
1203 : 53700 : if (hjud)
1204 : : {
1205 : 169637 : for (; b1 != NULL; b1 = b1->prev)
1206 : : {
1207 : : /* A goto from later in the function to this
1208 : : label will never see the initialization
1209 : : of B1, if any. Save it to issue a
1210 : : warning if needed. */
1211 : 148220 : if (decl_jump_unsafe (b1->decl))
1212 : 19448 : vec_safe_push(label_vars->decls_in_scope, b1->decl);
1213 : : }
1214 : : }
1215 : : }
1216 : :
1217 : : /* Update the bindings of any goto statements associated
1218 : : with this label. */
1219 : 472298026 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1220 : 470254903 : update_spot_bindings (scope, &g->goto_bindings);
1221 : : }
1222 : : }
1223 : :
1224 : : /* Don't search beyond the current function. */
1225 : 275722021 : if (s == current_function_scope)
1226 : : break;
1227 : :
1228 : 235667604 : s = s->outer;
1229 : : }
1230 : 89682906 : }
1231 : :
1232 : : /* Exit a scope. Restore the state of the identifier-decl mappings
1233 : : that were in effect when this scope was entered. Return a BLOCK
1234 : : node containing all the DECLs in this scope that are of interest
1235 : : to debug info generation. */
1236 : :
1237 : : tree
1238 : 89682906 : pop_scope (void)
1239 : : {
1240 : 89682906 : struct c_scope *scope = current_scope;
1241 : 89682906 : tree block, context, p;
1242 : 89682906 : struct c_binding *b;
1243 : :
1244 : 89682906 : bool functionbody = scope->function_body;
1245 : 89682906 : bool keep = functionbody || scope->keep || scope->bindings;
1246 : :
1247 : 89682906 : update_label_decls (scope);
1248 : :
1249 : : /* If appropriate, create a BLOCK to record the decls for the life
1250 : : of this function. */
1251 : 89682906 : block = NULL_TREE;
1252 : 89682906 : if (keep)
1253 : : {
1254 : 35867481 : block = make_node (BLOCK);
1255 : 35867481 : BLOCK_SUBBLOCKS (block) = scope->blocks;
1256 : 35867481 : TREE_USED (block) = 1;
1257 : :
1258 : : /* In each subblock, record that this is its superior. */
1259 : 36143412 : for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1260 : 275931 : BLOCK_SUPERCONTEXT (p) = block;
1261 : :
1262 : 35867481 : BLOCK_VARS (block) = NULL_TREE;
1263 : : }
1264 : :
1265 : : /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1266 : : scope must be set so that they point to the appropriate
1267 : : construct, i.e. either to the current FUNCTION_DECL node, or
1268 : : else to the BLOCK node we just constructed.
1269 : :
1270 : : Note that for tagged types whose scope is just the formal
1271 : : parameter list for some function type specification, we can't
1272 : : properly set their TYPE_CONTEXTs here, because we don't have a
1273 : : pointer to the appropriate FUNCTION_TYPE node readily available
1274 : : to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1275 : : type nodes get set in `grokdeclarator' as soon as we have created
1276 : : the FUNCTION_TYPE node which will represent the "scope" for these
1277 : : "parameter list local" tagged types. */
1278 : 89682906 : if (scope->function_body)
1279 : 35377252 : context = current_function_decl;
1280 : 54305654 : else if (scope == file_scope)
1281 : : {
1282 : 107150 : tree file_decl
1283 : 107150 : = build_translation_unit_decl (get_identifier (main_input_filename));
1284 : 107150 : context = file_decl;
1285 : 107150 : debug_hooks->register_main_translation_unit (file_decl);
1286 : : }
1287 : : else
1288 : : context = block;
1289 : :
1290 : : /* Clear all bindings in this scope. */
1291 : 848695501 : for (b = scope->bindings; b; b = free_binding_and_advance (b))
1292 : : {
1293 : 759012595 : p = b->decl;
1294 : 759012595 : switch (TREE_CODE (p))
1295 : : {
1296 : 23763 : case LABEL_DECL:
1297 : : /* Warnings for unused labels, errors for undefined labels. */
1298 : 23763 : if (TREE_USED (p) && !DECL_INITIAL (p))
1299 : : {
1300 : 63 : error ("label %q+D used but not defined", p);
1301 : 63 : DECL_INITIAL (p) = error_mark_node;
1302 : : }
1303 : : else
1304 : 23700 : warn_for_unused_label (p);
1305 : :
1306 : : /* Labels go in BLOCK_VARS. */
1307 : 23763 : DECL_CHAIN (p) = BLOCK_VARS (block);
1308 : 23763 : BLOCK_VARS (block) = p;
1309 : 23763 : gcc_assert (I_LABEL_BINDING (b->id) == b);
1310 : 23763 : I_LABEL_BINDING (b->id) = b->shadowed;
1311 : :
1312 : : /* Also pop back to the shadowed label_vars. */
1313 : 23763 : release_tree_vector (b->u.label->decls_in_scope);
1314 : 23763 : b->u.label = b->u.label->shadowed;
1315 : 23763 : break;
1316 : :
1317 : 1398145 : case ENUMERAL_TYPE:
1318 : 1398145 : case UNION_TYPE:
1319 : 1398145 : case RECORD_TYPE:
1320 : :
1321 : : /* Types may not have tag-names, in which case the type
1322 : : appears in the bindings list with b->id NULL. */
1323 : 1398145 : if (b->id)
1324 : : {
1325 : 614609 : gcc_assert (I_TAG_BINDING (b->id) == b);
1326 : 614609 : I_TAG_BINDING (b->id) = b->shadowed;
1327 : : }
1328 : : break;
1329 : :
1330 : 632126223 : case FUNCTION_DECL:
1331 : : /* Propagate TREE_ADDRESSABLE from nested functions to their
1332 : : containing functions. */
1333 : 632126223 : if (!TREE_ASM_WRITTEN (p)
1334 : 632126223 : && DECL_INITIAL (p) != NULL_TREE
1335 : 70408254 : && TREE_ADDRESSABLE (p)
1336 : 3608710 : && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1337 : 632126223 : && DECL_ABSTRACT_ORIGIN (p) != p)
1338 : 0 : TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1339 : 632126223 : if (!TREE_PUBLIC (p)
1340 : 345611 : && !DECL_INITIAL (p)
1341 : 160 : && !b->nested
1342 : 129 : && scope != file_scope
1343 : 632126231 : && scope != external_scope)
1344 : : {
1345 : 8 : error ("nested function %q+D declared but never defined", p);
1346 : 8 : undef_nested_function = true;
1347 : : }
1348 : 632126215 : else if (DECL_DECLARED_INLINE_P (p)
1349 : 69237016 : && TREE_PUBLIC (p)
1350 : 701207191 : && !DECL_INITIAL (p))
1351 : : {
1352 : : /* C99 6.7.4p6: "a function with external linkage... declared
1353 : : with an inline function specifier ... shall also be defined
1354 : : in the same translation unit." */
1355 : 76 : if (!flag_gnu89_inline
1356 : 51 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1357 : 111 : && scope == external_scope)
1358 : 13 : pedwarn (input_location, 0,
1359 : : "inline function %q+D declared but never defined", p);
1360 : 76 : DECL_EXTERNAL (p) = 1;
1361 : : }
1362 : :
1363 : 632126223 : goto common_symbol;
1364 : :
1365 : 10755104 : case VAR_DECL:
1366 : : /* Warnings for unused variables. */
1367 : 8192456 : if ((!TREE_USED (p) || !DECL_READ_P (p))
1368 : 2591046 : && !warning_suppressed_p (p, OPT_Wunused_but_set_variable_)
1369 : 2590540 : && !DECL_IN_SYSTEM_HEADER (p)
1370 : 2512561 : && DECL_NAME (p)
1371 : 2512561 : && !DECL_ARTIFICIAL (p)
1372 : 2512196 : && scope != file_scope
1373 : 12379339 : && scope != external_scope)
1374 : : {
1375 : 737913 : if (!TREE_USED (p))
1376 : : {
1377 : 732613 : warning (OPT_Wunused_variable, "unused variable %q+D", p);
1378 : 732613 : suppress_warning (p, OPT_Wunused_variable);
1379 : : }
1380 : 5300 : else if (DECL_CONTEXT (p) == current_function_decl)
1381 : 5281 : warning_at (DECL_SOURCE_LOCATION (p),
1382 : 5281 : OPT_Wunused_but_set_variable_,
1383 : : "variable %qD set but not used", p);
1384 : : }
1385 : :
1386 : 10755104 : if (b->inner_comp)
1387 : : {
1388 : 2 : error ("type of array %q+D completed incompatibly with"
1389 : : " implicit initialization", p);
1390 : : }
1391 : :
1392 : : /* Fall through. */
1393 : 660094541 : case TYPE_DECL:
1394 : 660094541 : case CONST_DECL:
1395 : 10755102 : common_symbol:
1396 : : /* All of these go in BLOCK_VARS, but only if this is the
1397 : : binding in the home scope. */
1398 : 660094541 : if (!b->nested)
1399 : : {
1400 : 397395331 : DECL_CHAIN (p) = BLOCK_VARS (block);
1401 : 397395331 : BLOCK_VARS (block) = p;
1402 : : }
1403 : 262699210 : else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1404 : : {
1405 : : /* For block local externs add a special
1406 : : DECL_EXTERNAL decl for debug info generation. */
1407 : 14578 : tree extp = copy_node (p);
1408 : :
1409 : 14578 : DECL_EXTERNAL (extp) = 1;
1410 : 14578 : TREE_STATIC (extp) = 0;
1411 : 14578 : TREE_PUBLIC (extp) = 1;
1412 : 14578 : DECL_INITIAL (extp) = NULL_TREE;
1413 : 14578 : DECL_LANG_SPECIFIC (extp) = NULL;
1414 : 14578 : DECL_CONTEXT (extp) = current_function_decl;
1415 : 14578 : if (TREE_CODE (p) == FUNCTION_DECL)
1416 : : {
1417 : 13041 : DECL_RESULT (extp) = NULL_TREE;
1418 : 13041 : DECL_SAVED_TREE (extp) = NULL_TREE;
1419 : 13041 : DECL_STRUCT_FUNCTION (extp) = NULL;
1420 : : }
1421 : 14578 : if (b->locus != UNKNOWN_LOCATION)
1422 : 14578 : DECL_SOURCE_LOCATION (extp) = b->locus;
1423 : 14578 : DECL_CHAIN (extp) = BLOCK_VARS (block);
1424 : 14578 : BLOCK_VARS (block) = extp;
1425 : : }
1426 : : /* If this is the file scope set DECL_CONTEXT of each decl to
1427 : : the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1428 : : work. */
1429 : 660094541 : if (scope == file_scope)
1430 : 273789179 : DECL_CONTEXT (p) = context;
1431 : :
1432 : 757590687 : gcc_fallthrough ();
1433 : : /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1434 : : already been put there by store_parm_decls. Unused-
1435 : : parameter warnings are handled by function.cc.
1436 : : error_mark_node obviously does not go in BLOCK_VARS and
1437 : : does not get unused-variable warnings. */
1438 : 757590687 : case PARM_DECL:
1439 : 757590687 : case ERROR_MARK:
1440 : : /* It is possible for a decl not to have a name. We get
1441 : : here with b->id NULL in this case. */
1442 : 757590687 : if (b->id)
1443 : : {
1444 : 754430732 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1445 : 754430732 : I_SYMBOL_BINDING (b->id) = b->shadowed;
1446 : 754430732 : if (b->shadowed && b->shadowed->u.type)
1447 : 4587976 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1448 : : }
1449 : : break;
1450 : :
1451 : 0 : default:
1452 : 0 : gcc_unreachable ();
1453 : : }
1454 : : }
1455 : :
1456 : :
1457 : : /* Dispose of the block that we just made inside some higher level. */
1458 : 89682906 : if ((scope->function_body || scope == file_scope) && context)
1459 : : {
1460 : 35484400 : DECL_INITIAL (context) = block;
1461 : 35484400 : BLOCK_SUPERCONTEXT (block) = context;
1462 : : }
1463 : 54198506 : else if (scope->outer)
1464 : : {
1465 : 54091356 : if (block)
1466 : 275931 : SCOPE_LIST_APPEND (scope->outer, blocks, block);
1467 : : /* If we did not make a block for the scope just exited, any
1468 : : blocks made for inner scopes must be carried forward so they
1469 : : will later become subblocks of something else. */
1470 : 53815425 : else if (scope->blocks)
1471 : 354023 : SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1472 : : }
1473 : :
1474 : : /* Pop the current scope, and free the structure for reuse. */
1475 : 89682906 : current_scope = scope->outer;
1476 : 89682906 : if (scope->function_body)
1477 : 35377252 : current_function_scope = scope->outer_function;
1478 : :
1479 : 89682906 : memset (scope, 0, sizeof (struct c_scope));
1480 : 89682906 : scope->outer = scope_freelist;
1481 : 89682906 : scope_freelist = scope;
1482 : :
1483 : 89682906 : return block;
1484 : : }
1485 : :
1486 : : void
1487 : 107676 : push_file_scope (void)
1488 : : {
1489 : 107676 : tree decl;
1490 : :
1491 : 107676 : if (file_scope)
1492 : : return;
1493 : :
1494 : 107676 : push_scope ();
1495 : 107676 : file_scope = current_scope;
1496 : :
1497 : 107676 : start_fname_decls ();
1498 : :
1499 : 214531373 : for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1500 : 214423697 : bind (DECL_NAME (decl), decl, file_scope,
1501 : 214423697 : /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1502 : : }
1503 : :
1504 : : void
1505 : 107523 : pop_file_scope (void)
1506 : : {
1507 : : /* In case there were missing closebraces, get us back to the global
1508 : : binding level. */
1509 : 107528 : while (current_scope != file_scope)
1510 : 5 : pop_scope ();
1511 : :
1512 : : /* __FUNCTION__ is defined at file scope (""). This
1513 : : call may not be necessary as my tests indicate it
1514 : : still works without it. */
1515 : 107523 : finish_fname_decls ();
1516 : :
1517 : 107523 : check_inline_statics ();
1518 : :
1519 : : /* This is the point to write out a PCH if we're doing that.
1520 : : In that case we do not want to do anything else. */
1521 : 107523 : if (pch_file)
1522 : : {
1523 : 373 : c_common_write_pch ();
1524 : : /* Ensure even the callers don't try to finalize the CU. */
1525 : 373 : flag_syntax_only = 1;
1526 : 373 : return;
1527 : : }
1528 : :
1529 : : /* Pop off the file scope and close this translation unit. */
1530 : 107150 : pop_scope ();
1531 : 107150 : file_scope = 0;
1532 : :
1533 : 107150 : maybe_apply_pending_pragma_weaks ();
1534 : : }
1535 : :
1536 : : /* Whether we are curently inside the initializer for an
1537 : : underspecified object definition (C23 auto or constexpr). */
1538 : : static bool in_underspecified_init;
1539 : :
1540 : : /* Start an underspecified object definition for NAME at LOC. This
1541 : : means that NAME is shadowed inside its initializer, so neither the
1542 : : definition being initialized, nor any definition from an outer
1543 : : scope, may be referenced during that initializer. Return state to
1544 : : be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1545 : : underspecified object is a (constexpr) compound literal; there is
1546 : : no shadowing in that case, but all the other restrictions on
1547 : : underspecified object definitions still apply. */
1548 : : unsigned int
1549 : 655 : start_underspecified_init (location_t loc, tree name)
1550 : : {
1551 : 655 : bool prev = in_underspecified_init;
1552 : 655 : bool ok;
1553 : 655 : if (name == NULL_TREE)
1554 : : ok = true;
1555 : : else
1556 : : {
1557 : 423 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1558 : 423 : C_DECL_UNDERSPECIFIED (decl) = 1;
1559 : 423 : struct c_scope *scope = current_scope;
1560 : 423 : struct c_binding *b = I_SYMBOL_BINDING (name);
1561 : 423 : if (b && B_IN_SCOPE (b, scope))
1562 : : {
1563 : 4 : error_at (loc, "underspecified declaration of %qE, which is already "
1564 : : "declared in this scope", name);
1565 : 4 : ok = false;
1566 : : }
1567 : : else
1568 : : {
1569 : 419 : bind (name, decl, scope, false, false, loc);
1570 : 419 : ok = true;
1571 : : }
1572 : : }
1573 : 655 : in_underspecified_init = true;
1574 : 655 : return ok | (prev << 1);
1575 : : }
1576 : :
1577 : : /* Finish an underspecified object definition for NAME, before that
1578 : : name is bound to the real declaration instead of a placeholder.
1579 : : PREV_STATE is the value returned by the call to
1580 : : start_underspecified_init. If NAME is NULL_TREE, this means a
1581 : : compound literal, as for start_underspecified_init. */
1582 : : void
1583 : 655 : finish_underspecified_init (tree name, unsigned int prev_state)
1584 : : {
1585 : 655 : if (name != NULL_TREE && (prev_state & 1))
1586 : : {
1587 : : /* A VAR_DECL was bound to the name to shadow any previous
1588 : : declarations for the name; remove that binding now. */
1589 : 419 : struct c_scope *scope = current_scope;
1590 : 419 : struct c_binding *b = I_SYMBOL_BINDING (name);
1591 : 419 : gcc_assert (b);
1592 : 419 : gcc_assert (B_IN_SCOPE (b, scope));
1593 : 419 : gcc_assert (VAR_P (b->decl));
1594 : 419 : gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1595 : 419 : I_SYMBOL_BINDING (name) = b->shadowed;
1596 : : /* In erroneous cases there may be other bindings added to this
1597 : : scope during the initializer. */
1598 : 419 : struct c_binding **p = &scope->bindings;
1599 : 474 : while (*p != b)
1600 : 55 : p = &((*p)->prev);
1601 : 419 : *p = free_binding_and_advance (*p);
1602 : : }
1603 : 655 : in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1604 : 655 : }
1605 : :
1606 : : /* Adjust the bindings for the start of a statement expression. */
1607 : :
1608 : : void
1609 : 34725 : c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1610 : : {
1611 : 34725 : struct c_scope *scope;
1612 : :
1613 : 234616 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1614 : : {
1615 : 199891 : struct c_binding *b;
1616 : :
1617 : 199891 : if (!scope->has_label_bindings)
1618 : 197492 : continue;
1619 : :
1620 : 12608 : for (b = scope->bindings; b != NULL; b = b->prev)
1621 : : {
1622 : 10209 : struct c_label_vars *label_vars;
1623 : 10209 : unsigned int ix;
1624 : 10209 : struct c_goto_bindings *g;
1625 : :
1626 : 10209 : if (TREE_CODE (b->decl) != LABEL_DECL)
1627 : 1460 : continue;
1628 : 8749 : label_vars = b->u.label;
1629 : 8749 : ++label_vars->label_bindings.stmt_exprs;
1630 : 12745 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1631 : 1795 : ++g->goto_bindings.stmt_exprs;
1632 : : }
1633 : : }
1634 : :
1635 : 34725 : if (switch_bindings != NULL)
1636 : 173 : ++switch_bindings->stmt_exprs;
1637 : 34725 : }
1638 : :
1639 : : /* Adjust the bindings for the end of a statement expression. */
1640 : :
1641 : : void
1642 : 34725 : c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1643 : : {
1644 : 34725 : struct c_scope *scope;
1645 : :
1646 : 199891 : for (scope = current_scope; scope != NULL; scope = scope->outer)
1647 : : {
1648 : 165166 : struct c_binding *b;
1649 : :
1650 : 165166 : if (!scope->has_label_bindings)
1651 : 162532 : continue;
1652 : :
1653 : 16084 : for (b = scope->bindings; b != NULL; b = b->prev)
1654 : : {
1655 : 13450 : struct c_label_vars *label_vars;
1656 : 13450 : unsigned int ix;
1657 : 13450 : struct c_goto_bindings *g;
1658 : :
1659 : 13450 : if (TREE_CODE (b->decl) != LABEL_DECL)
1660 : 1520 : continue;
1661 : 11930 : label_vars = b->u.label;
1662 : 11930 : --label_vars->label_bindings.stmt_exprs;
1663 : 11930 : if (label_vars->label_bindings.stmt_exprs < 0)
1664 : : {
1665 : 3322 : label_vars->label_bindings.left_stmt_expr = true;
1666 : 3322 : label_vars->label_bindings.stmt_exprs = 0;
1667 : : }
1668 : 15978 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1669 : : {
1670 : 1791 : --g->goto_bindings.stmt_exprs;
1671 : 1791 : if (g->goto_bindings.stmt_exprs < 0)
1672 : : {
1673 : 128 : g->goto_bindings.left_stmt_expr = true;
1674 : 128 : g->goto_bindings.stmt_exprs = 0;
1675 : : }
1676 : : }
1677 : : }
1678 : : }
1679 : :
1680 : 34725 : if (switch_bindings != NULL)
1681 : : {
1682 : 173 : --switch_bindings->stmt_exprs;
1683 : 173 : gcc_assert (switch_bindings->stmt_exprs >= 0);
1684 : : }
1685 : 34725 : }
1686 : :
1687 : : /* Push a definition or a declaration of struct, union or enum tag "name".
1688 : : "type" should be the type node.
1689 : : We assume that the tag "name" is not already defined, and has a location
1690 : : of LOC.
1691 : :
1692 : : Note that the definition may really be just a forward reference.
1693 : : In that case, the TYPE_SIZE will be zero. */
1694 : :
1695 : : static void
1696 : 1402099 : pushtag (location_t loc, tree name, tree type)
1697 : : {
1698 : : /* Record the identifier as the type's name if it has none. */
1699 : 1402099 : if (name && !TYPE_NAME (type))
1700 : 616407 : TYPE_NAME (type) = name;
1701 : 1402099 : bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1702 : :
1703 : : /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1704 : : tagged type we just added to the current scope. This fake
1705 : : NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1706 : : to output a representation of a tagged type, and it also gives
1707 : : us a convenient place to record the "scope start" address for the
1708 : : tagged type. */
1709 : :
1710 : 1402099 : TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1711 : : TYPE_DECL, NULL_TREE, type));
1712 : :
1713 : : /* An approximation for now, so we can tell this is a function-scope tag.
1714 : : This will be updated in pop_scope. */
1715 : 1402099 : TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1716 : :
1717 : 1402099 : if (warn_cxx_compat && name != NULL_TREE)
1718 : : {
1719 : 628 : struct c_binding *b = I_SYMBOL_BINDING (name);
1720 : :
1721 : 628 : if (b != NULL
1722 : 12 : && b->decl != NULL_TREE
1723 : 12 : && TREE_CODE (b->decl) == TYPE_DECL
1724 : 11 : && (B_IN_CURRENT_SCOPE (b)
1725 : 6 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1726 : 633 : && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1727 : 5 : != TYPE_MAIN_VARIANT (type)))
1728 : : {
1729 : 5 : auto_diagnostic_group d;
1730 : 6 : if (warning_at (loc, OPT_Wc___compat,
1731 : : "using %qD as both a typedef and a tag is "
1732 : : "invalid in C++", b->decl)
1733 : 5 : && b->locus != UNKNOWN_LOCATION)
1734 : 4 : inform (b->locus, "originally defined here");
1735 : 5 : }
1736 : : }
1737 : 1402099 : }
1738 : :
1739 : : /* An exported interface to pushtag. This is used by the gdb plugin's
1740 : : binding oracle to introduce a new tag binding. */
1741 : :
1742 : : void
1743 : 0 : c_pushtag (location_t loc, tree name, tree type)
1744 : : {
1745 : 0 : pushtag (loc, name, type);
1746 : 0 : }
1747 : :
1748 : : /* An exported interface to bind a declaration. LOC is the location
1749 : : to use. DECL is the declaration to bind. The decl's name is used
1750 : : to determine how it is bound. If DECL is a VAR_DECL, then
1751 : : IS_GLOBAL determines whether the decl is put into the global (file
1752 : : and external) scope or the current function's scope; if DECL is not
1753 : : a VAR_DECL then it is always put into the file scope. */
1754 : :
1755 : : void
1756 : 0 : c_bind (location_t loc, tree decl, bool is_global)
1757 : : {
1758 : 0 : struct c_scope *scope;
1759 : 0 : bool nested = false;
1760 : :
1761 : 0 : if (!VAR_P (decl) || current_function_scope == NULL)
1762 : : {
1763 : : /* Types and functions are always considered to be global. */
1764 : 0 : scope = file_scope;
1765 : 0 : DECL_EXTERNAL (decl) = 1;
1766 : 0 : TREE_PUBLIC (decl) = 1;
1767 : : }
1768 : 0 : else if (is_global)
1769 : : {
1770 : : /* Also bind it into the external scope. */
1771 : 0 : bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1772 : 0 : nested = true;
1773 : 0 : scope = file_scope;
1774 : 0 : DECL_EXTERNAL (decl) = 1;
1775 : 0 : TREE_PUBLIC (decl) = 1;
1776 : : }
1777 : : else
1778 : : {
1779 : 0 : DECL_CONTEXT (decl) = current_function_decl;
1780 : 0 : TREE_PUBLIC (decl) = 0;
1781 : 0 : scope = current_function_scope;
1782 : : }
1783 : :
1784 : 0 : bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1785 : 0 : }
1786 : :
1787 : :
1788 : : /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1789 : : it is) seen in a declaration of a file I/O etc. built-in, corresponding
1790 : : to the builtin_structptr_types array. Subsequent declarations of such
1791 : : built-ins are expected to refer to it rather than to fileptr_type_node,
1792 : : etc. which is just void* (or to any other type).
1793 : : Used only by match_builtin_function_types. */
1794 : :
1795 : : static const unsigned builtin_structptr_type_count
1796 : : = ARRAY_SIZE (builtin_structptr_types);
1797 : :
1798 : : static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1799 : :
1800 : : /* Returns true if types T1 and T2 representing return types or types
1801 : : of function arguments are close enough to be considered interchangeable
1802 : : in redeclarations of built-in functions. */
1803 : :
1804 : : static bool
1805 : 616474 : types_close_enough_to_match (tree t1, tree t2)
1806 : : {
1807 : 616474 : return (TYPE_MODE (t1) == TYPE_MODE (t2)
1808 : 616019 : && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1809 : 2464378 : && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1810 : : }
1811 : :
1812 : : /* Subroutine of compare_decls. Allow harmless mismatches in return
1813 : : and argument types provided that the type modes match. Set *STRICT
1814 : : and *ARGNO to the expected argument type and number in case of
1815 : : an argument type mismatch or null and zero otherwise. Return
1816 : : a unified type given a suitable match, and 0 otherwise. */
1817 : :
1818 : : static tree
1819 : 142990 : match_builtin_function_types (tree newtype, tree oldtype,
1820 : : tree *strict, unsigned *argno)
1821 : : {
1822 : 142990 : *argno = 0;
1823 : 142990 : *strict = NULL_TREE;
1824 : :
1825 : : /* Accept the return type of the new declaration if it has the same
1826 : : mode and if they're both pointers or if neither is. */
1827 : 142990 : tree oldrettype = TREE_TYPE (oldtype);
1828 : 142990 : tree newrettype = TREE_TYPE (newtype);
1829 : :
1830 : 142990 : if (!types_close_enough_to_match (oldrettype, newrettype))
1831 : : return NULL_TREE;
1832 : :
1833 : : /* Check that the return types are compatible but don't fail if they
1834 : : are not (e.g., int vs long in ILP32) and just let the caller know. */
1835 : 142628 : if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1836 : 142628 : TYPE_MAIN_VARIANT (newrettype)))
1837 : 39 : *strict = oldrettype;
1838 : :
1839 : 142628 : tree oldargs = TYPE_ARG_TYPES (oldtype);
1840 : 142628 : tree newargs = TYPE_ARG_TYPES (newtype);
1841 : 142628 : tree tryargs = newargs;
1842 : :
1843 : 142628 : const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1844 : 142628 : const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1845 : :
1846 : 142628 : gcc_checking_assert (nlst == nbst);
1847 : :
1848 : 615915 : for (unsigned i = 1; oldargs || newargs; ++i)
1849 : : {
1850 : 473543 : if (!oldargs
1851 : 473543 : || !newargs
1852 : 473488 : || !TREE_VALUE (oldargs)
1853 : 947031 : || !TREE_VALUE (newargs))
1854 : : return NULL_TREE;
1855 : :
1856 : 473488 : tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1857 : 473488 : tree newtype = TREE_VALUE (newargs);
1858 : 473488 : if (newtype == error_mark_node)
1859 : : return NULL_TREE;
1860 : 473484 : newtype = TYPE_MAIN_VARIANT (newtype);
1861 : :
1862 : 473484 : if (!types_close_enough_to_match (oldtype, newtype))
1863 : : return NULL_TREE;
1864 : :
1865 : 473340 : unsigned j = nbst;
1866 : 473340 : if (POINTER_TYPE_P (oldtype))
1867 : : /* Iterate over well-known struct types like FILE (whose types
1868 : : aren't known to us) and compare the pointer to each to
1869 : : the pointer argument. */
1870 : 967015 : for (j = 0; j < nbst; ++j)
1871 : : {
1872 : 851930 : if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1873 : 709774 : continue;
1874 : : /* Store the first FILE* etc. argument type (whatever it is), and
1875 : : expect any subsequent declarations of file I/O etc. built-ins
1876 : : to refer to it rather than to fileptr_type_node etc. which is
1877 : : just void* (or const void*). */
1878 : 142156 : if (last_structptr_types[j])
1879 : : {
1880 : 124875 : if (!comptypes (last_structptr_types[j], newtype))
1881 : : {
1882 : 2 : *argno = i;
1883 : 2 : *strict = last_structptr_types[j];
1884 : : }
1885 : : }
1886 : : else
1887 : 17281 : last_structptr_types[j] = newtype;
1888 : : break;
1889 : : }
1890 : :
1891 : 473340 : if (j == nbst && !comptypes (oldtype, newtype))
1892 : : {
1893 : 267 : if (POINTER_TYPE_P (oldtype))
1894 : : {
1895 : : /* For incompatible pointers, only reject differences in
1896 : : the unqualified variants of the referenced types but
1897 : : consider differences in qualifiers as benign (report
1898 : : those to caller via *STRICT below). */
1899 : 220 : tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1900 : 220 : tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1901 : 220 : if (!comptypes (oldref, newref))
1902 : : return NULL_TREE;
1903 : : }
1904 : :
1905 : 214 : if (!*strict)
1906 : : {
1907 : 194 : *argno = i;
1908 : 194 : *strict = oldtype;
1909 : : }
1910 : : }
1911 : :
1912 : 473287 : oldargs = TREE_CHAIN (oldargs);
1913 : 473287 : newargs = TREE_CHAIN (newargs);
1914 : : }
1915 : :
1916 : 142372 : tree trytype = c_build_function_type (newrettype, tryargs);
1917 : :
1918 : : /* Allow declaration to change transaction_safe attribute. */
1919 : 142372 : tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1920 : 142372 : tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1921 : 142372 : tree newattrs = TYPE_ATTRIBUTES (newtype);
1922 : 142372 : tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1923 : 142372 : if (oldtsafe && !newtsafe)
1924 : 0 : oldattrs = remove_attribute ("transaction_safe", oldattrs);
1925 : 142372 : else if (newtsafe && !oldtsafe)
1926 : 7 : oldattrs = tree_cons (get_identifier ("transaction_safe"),
1927 : : NULL_TREE, oldattrs);
1928 : :
1929 : 142372 : return c_build_type_attribute_variant (trytype, oldattrs);
1930 : : }
1931 : :
1932 : : /* Subroutine of diagnose_mismatched_decls. Check for function type
1933 : : mismatch involving an empty arglist vs a nonempty one and give clearer
1934 : : diagnostics. */
1935 : : static void
1936 : 170 : diagnose_arglist_conflict (tree newdecl, tree olddecl,
1937 : : tree newtype, tree oldtype)
1938 : : {
1939 : 170 : tree t;
1940 : :
1941 : 170 : if (TREE_CODE (olddecl) != FUNCTION_DECL
1942 : 95 : || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1943 : 291 : || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1944 : 64 : || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1945 : 153 : return;
1946 : :
1947 : 17 : t = TYPE_ARG_TYPES (oldtype);
1948 : 17 : if (t == NULL_TREE)
1949 : 9 : t = TYPE_ARG_TYPES (newtype);
1950 : 18 : for (; t; t = TREE_CHAIN (t))
1951 : : {
1952 : 18 : tree type = TREE_VALUE (t);
1953 : :
1954 : 18 : if (TREE_CHAIN (t) == NULL_TREE
1955 : 18 : && TYPE_MAIN_VARIANT (type) != void_type_node)
1956 : : {
1957 : 10 : inform (input_location, "a parameter list with an ellipsis "
1958 : : "cannot match an empty parameter name list declaration");
1959 : 10 : break;
1960 : : }
1961 : :
1962 : 8 : if (!error_operand_p (type)
1963 : 8 : && c_type_promotes_to (type) != type)
1964 : : {
1965 : 7 : inform (input_location, "an argument type that has a default "
1966 : : "promotion cannot match an empty parameter name list "
1967 : : "declaration");
1968 : 7 : break;
1969 : : }
1970 : : }
1971 : : }
1972 : :
1973 : : /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1974 : : old-style function definition, NEWDECL is a prototype declaration.
1975 : : Diagnose inconsistencies in the argument list. Returns TRUE if
1976 : : the prototype is compatible, FALSE if not. */
1977 : : static bool
1978 : 19 : validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1979 : : {
1980 : 19 : tree newargs, oldargs;
1981 : 19 : int i;
1982 : :
1983 : : #define END_OF_ARGLIST(t) ((t) == void_type_node)
1984 : :
1985 : 19 : oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1986 : 19 : newargs = TYPE_ARG_TYPES (newtype);
1987 : 19 : i = 1;
1988 : :
1989 : 45 : for (;;)
1990 : : {
1991 : 32 : tree oldargtype = TREE_VALUE (oldargs);
1992 : 32 : tree newargtype = TREE_VALUE (newargs);
1993 : :
1994 : 32 : if (oldargtype == error_mark_node || newargtype == error_mark_node)
1995 : : return false;
1996 : :
1997 : 58 : oldargtype = (TYPE_ATOMIC (oldargtype)
1998 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1999 : : TYPE_QUAL_ATOMIC)
2000 : 27 : : TYPE_MAIN_VARIANT (oldargtype));
2001 : 60 : newargtype = (TYPE_ATOMIC (newargtype)
2002 : 31 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
2003 : : TYPE_QUAL_ATOMIC)
2004 : 29 : : TYPE_MAIN_VARIANT (newargtype));
2005 : :
2006 : 31 : if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
2007 : : break;
2008 : :
2009 : : /* Reaching the end of just one list means the two decls don't
2010 : : agree on the number of arguments. */
2011 : 22 : if (END_OF_ARGLIST (oldargtype))
2012 : : {
2013 : 2 : error ("prototype for %q+D declares more arguments "
2014 : : "than previous old-style definition", newdecl);
2015 : 2 : return false;
2016 : : }
2017 : 20 : else if (END_OF_ARGLIST (newargtype))
2018 : : {
2019 : 2 : error ("prototype for %q+D declares fewer arguments "
2020 : : "than previous old-style definition", newdecl);
2021 : 2 : return false;
2022 : : }
2023 : :
2024 : : /* Type for passing arg must be consistent with that declared
2025 : : for the arg. */
2026 : 18 : else if (!comptypes (oldargtype, newargtype))
2027 : : {
2028 : 5 : error ("prototype for %q+D declares argument %d"
2029 : : " with incompatible type",
2030 : : newdecl, i);
2031 : 5 : return false;
2032 : : }
2033 : :
2034 : 13 : oldargs = TREE_CHAIN (oldargs);
2035 : 13 : newargs = TREE_CHAIN (newargs);
2036 : 13 : i++;
2037 : 13 : }
2038 : :
2039 : : /* If we get here, no errors were found, but do issue a warning
2040 : : for this poor-style construct. */
2041 : 9 : warning (0, "prototype for %q+D follows non-prototype definition",
2042 : : newdecl);
2043 : 9 : return true;
2044 : : #undef END_OF_ARGLIST
2045 : : }
2046 : :
2047 : : /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
2048 : : first in a pair of mismatched declarations, using the diagnostic
2049 : : function DIAG. */
2050 : : static void
2051 : 409 : locate_old_decl (tree decl)
2052 : : {
2053 : 409 : if (TREE_CODE (decl) == FUNCTION_DECL
2054 : 195 : && fndecl_built_in_p (decl)
2055 : 412 : && !C_DECL_DECLARED_BUILTIN (decl))
2056 : : ;
2057 : 409 : else if (DECL_INITIAL (decl))
2058 : 118 : inform (input_location,
2059 : : "previous definition of %q+D with type %qT",
2060 : 118 : decl, TREE_TYPE (decl));
2061 : 291 : else if (C_DECL_IMPLICIT (decl))
2062 : 16 : inform (input_location,
2063 : : "previous implicit declaration of %q+D with type %qT",
2064 : 16 : decl, TREE_TYPE (decl));
2065 : : else
2066 : 275 : inform (input_location,
2067 : : "previous declaration of %q+D with type %qT",
2068 : 275 : decl, TREE_TYPE (decl));
2069 : 409 : }
2070 : :
2071 : :
2072 : : /* Helper function. For a tagged type, it finds the declaration
2073 : : for a visible tag declared in the same scope if such a
2074 : : declaration exists. */
2075 : : static tree
2076 : 1113329 : previous_tag (tree type)
2077 : : {
2078 : 1113329 : struct c_binding *b = NULL;
2079 : 1113329 : tree name = c_type_tag (type);
2080 : :
2081 : 1113329 : if (name)
2082 : 556229 : b = I_TAG_BINDING (name);
2083 : :
2084 : 556229 : if (b)
2085 : 556229 : b = b->shadowed;
2086 : :
2087 : 1113329 : if (b && B_IN_CURRENT_SCOPE (b))
2088 : 173 : return b->decl;
2089 : :
2090 : : return NULL_TREE;
2091 : : }
2092 : :
2093 : : /* Subroutine to mark functions as versioned when using the attribute
2094 : : 'target_version'. */
2095 : :
2096 : : static void
2097 : 0 : maybe_mark_function_versioned (tree decl)
2098 : : {
2099 : 0 : if (!DECL_FUNCTION_VERSIONED (decl))
2100 : : {
2101 : : /* Check if the name of the function has been overridden. */
2102 : 0 : if (DECL_ASSEMBLER_NAME_SET_P (decl)
2103 : 0 : && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))[0] == '*')
2104 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
2105 : : "cannot use function multiversioning on a renamed function");
2106 : :
2107 : : /* We need to insert function version now to make sure the correct
2108 : : pre-mangled assembler name is recorded. */
2109 : 0 : cgraph_node *node = cgraph_node::get_create (decl);
2110 : :
2111 : 0 : if (!node->function_version ())
2112 : 0 : node->insert_new_function_version ();
2113 : :
2114 : 0 : DECL_FUNCTION_VERSIONED (decl) = 1;
2115 : :
2116 : 0 : tree mangled_name
2117 : 0 : = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
2118 : 0 : SET_DECL_ASSEMBLER_NAME (decl, mangled_name);
2119 : : }
2120 : 0 : }
2121 : :
2122 : : /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2123 : : Returns true if the caller should proceed to merge the two, false
2124 : : if OLDDECL should simply be discarded. As a side effect, issues
2125 : : all necessary diagnostics for invalid or poor-style combinations.
2126 : : If it returns true, writes the types of NEWDECL and OLDDECL to
2127 : : *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2128 : : TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2129 : :
2130 : : static bool
2131 : 4909484 : diagnose_mismatched_decls (tree newdecl, tree olddecl,
2132 : : tree *newtypep, tree *oldtypep)
2133 : : {
2134 : 4909484 : tree newtype, oldtype;
2135 : 4909484 : bool retval = true;
2136 : :
2137 : : #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2138 : : && DECL_EXTERNAL (DECL))
2139 : :
2140 : : /* If we have error_mark_node for either decl or type, just discard
2141 : : the previous decl - we're in an error cascade already. */
2142 : 4909484 : if (olddecl == error_mark_node || newdecl == error_mark_node)
2143 : : return false;
2144 : 4909464 : *oldtypep = oldtype = TREE_TYPE (olddecl);
2145 : 4909464 : *newtypep = newtype = TREE_TYPE (newdecl);
2146 : 4909464 : if (oldtype == error_mark_node || newtype == error_mark_node)
2147 : : return false;
2148 : :
2149 : : /* Two different categories of symbol altogether. This is an error
2150 : : unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2151 : 4909456 : if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2152 : : {
2153 : 29 : if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2154 : 18 : && fndecl_built_in_p (olddecl)
2155 : 15 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2156 : : {
2157 : 15 : auto_diagnostic_group d;
2158 : 15 : error ("%q+D redeclared as different kind of symbol", newdecl);
2159 : 15 : locate_old_decl (olddecl);
2160 : 15 : }
2161 : 14 : else if (TREE_PUBLIC (newdecl))
2162 : 3 : warning (OPT_Wbuiltin_declaration_mismatch,
2163 : : "built-in function %q+D declared as non-function",
2164 : : newdecl);
2165 : : else
2166 : 11 : warning (OPT_Wshadow, "declaration of %q+D shadows "
2167 : : "a built-in function", newdecl);
2168 : 29 : return false;
2169 : : }
2170 : :
2171 : : /* Enumerators have no linkage, so may only be declared once in a
2172 : : given scope. */
2173 : 4909427 : if (TREE_CODE (olddecl) == CONST_DECL)
2174 : : {
2175 : 46 : if (flag_isoc23
2176 : 45 : && TYPE_NAME (DECL_CONTEXT (newdecl))
2177 : 43 : && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl)
2178 : 87 : && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl)))
2179 : : {
2180 : 38 : if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl)))
2181 : : {
2182 : 1 : auto_diagnostic_group d;
2183 : 1 : error ("conflicting redeclaration of enumerator %q+D", newdecl);
2184 : 1 : locate_old_decl (olddecl);
2185 : 1 : }
2186 : : }
2187 : : else
2188 : : {
2189 : 8 : auto_diagnostic_group d;
2190 : 8 : error ("redeclaration of enumerator %q+D", newdecl);
2191 : 8 : locate_old_decl (olddecl);
2192 : 8 : }
2193 : 46 : return false;
2194 : : }
2195 : :
2196 : 4909381 : bool pedwarned = false;
2197 : 4909381 : bool warned = false;
2198 : 4909381 : bool enum_and_int_p = false;
2199 : 4909381 : auto_diagnostic_group d;
2200 : :
2201 : 4909381 : int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2202 : : &enum_and_int_p);
2203 : 4909381 : if (!comptypes_result)
2204 : : {
2205 : 143184 : if (TREE_CODE (olddecl) == FUNCTION_DECL
2206 : 143109 : && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2207 : 286175 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2208 : : {
2209 : : /* Accept "harmless" mismatches in function types such
2210 : : as missing qualifiers or int vs long when they're the same
2211 : : size. However, diagnose return and argument types that are
2212 : : incompatible according to language rules. */
2213 : 142990 : tree mismatch_expect;
2214 : 142990 : unsigned mismatch_argno;
2215 : :
2216 : 142990 : tree trytype = match_builtin_function_types (newtype, oldtype,
2217 : : &mismatch_expect,
2218 : : &mismatch_argno);
2219 : :
2220 : 142990 : if (trytype && comptypes (newtype, trytype))
2221 : 142372 : *oldtypep = oldtype = trytype;
2222 : : else
2223 : : {
2224 : : /* If types don't match for a built-in, throw away the
2225 : : built-in. No point in calling locate_old_decl here, it
2226 : : won't print anything. */
2227 : 618 : const char *header = header_for_builtin_fn (olddecl);
2228 : 618 : location_t loc = DECL_SOURCE_LOCATION (newdecl);
2229 : 1019 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2230 : : "conflicting types for built-in function %q+D; "
2231 : : "expected %qT",
2232 : : newdecl, oldtype)
2233 : 618 : && header)
2234 : : {
2235 : : /* Suggest the right header to include as the preferred
2236 : : solution rather than the spelling of the declaration. */
2237 : 217 : rich_location richloc (line_table, loc);
2238 : 217 : maybe_add_include_fixit (&richloc, header, true);
2239 : 217 : inform (&richloc,
2240 : : "%qD is declared in header %qs", olddecl, header);
2241 : 217 : }
2242 : 618 : return false;
2243 : : }
2244 : :
2245 : 142372 : if (mismatch_expect && extra_warnings)
2246 : : {
2247 : 5 : location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2248 : 5 : bool warned = false;
2249 : 5 : if (mismatch_argno)
2250 : 5 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2251 : : "mismatch in argument %u type of built-in "
2252 : : "function %qD; expected %qT",
2253 : : mismatch_argno, newdecl, mismatch_expect);
2254 : : else
2255 : 0 : warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2256 : : "mismatch in return type of built-in "
2257 : : "function %qD; expected %qT",
2258 : : newdecl, mismatch_expect);
2259 : 5 : const char *header = header_for_builtin_fn (olddecl);
2260 : 5 : if (warned && header)
2261 : : {
2262 : 5 : rich_location richloc (line_table, newloc);
2263 : 5 : maybe_add_include_fixit (&richloc, header, true);
2264 : 5 : inform (&richloc,
2265 : : "%qD is declared in header %qs", olddecl, header);
2266 : 5 : }
2267 : : }
2268 : : }
2269 : 194 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2270 : 194 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2271 : : {
2272 : : /* A conflicting function declaration for a predeclared
2273 : : function that isn't actually built in. Objective C uses
2274 : : these. The new declaration silently overrides everything
2275 : : but the volatility (i.e. noreturn) indication. See also
2276 : : below. FIXME: Make Objective C use normal builtins. */
2277 : 0 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2278 : 0 : return false;
2279 : : }
2280 : : /* Permit void foo (...) to match int foo (...) if the latter is
2281 : : the definition and implicit int was used. See
2282 : : c-torture/compile/920625-2.c. */
2283 : 119 : else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2284 : 59 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2285 : 28 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2286 : 200 : && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2287 : : {
2288 : 5 : pedwarned = pedwarn (input_location, 0,
2289 : : "conflicting types for %q+D", newdecl);
2290 : : /* Make sure we keep void as the return type. */
2291 : 5 : TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2292 : 5 : C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2293 : : }
2294 : : /* Permit void foo (...) to match an earlier call to foo (...) with
2295 : : no declared type (thus, implicitly int). */
2296 : 189 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2297 : 114 : && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2298 : 83 : && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2299 : 212 : && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2300 : : {
2301 : 19 : pedwarned = pedwarn (input_location, 0,
2302 : : "conflicting types for %q+D; have %qT",
2303 : : newdecl, newtype);
2304 : : /* Make sure we keep void as the return type. */
2305 : 19 : TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2306 : : }
2307 : : else
2308 : : {
2309 : 170 : int new_quals = TYPE_QUALS (newtype);
2310 : 170 : int old_quals = TYPE_QUALS (oldtype);
2311 : :
2312 : 170 : if (new_quals != old_quals)
2313 : : {
2314 : 20 : addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2315 : 20 : addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2316 : 20 : if (new_addr != old_addr)
2317 : : {
2318 : 0 : if (ADDR_SPACE_GENERIC_P (new_addr))
2319 : 0 : error ("conflicting named address spaces (generic vs %s) "
2320 : : "for %q+D",
2321 : : c_addr_space_name (old_addr), newdecl);
2322 : 0 : else if (ADDR_SPACE_GENERIC_P (old_addr))
2323 : 0 : error ("conflicting named address spaces (%s vs generic) "
2324 : : "for %q+D",
2325 : : c_addr_space_name (new_addr), newdecl);
2326 : : else
2327 : 0 : error ("conflicting named address spaces (%s vs %s) "
2328 : : "for %q+D",
2329 : : c_addr_space_name (new_addr),
2330 : : c_addr_space_name (old_addr),
2331 : : newdecl);
2332 : : }
2333 : :
2334 : 20 : if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2335 : 20 : != CLEAR_QUAL_ADDR_SPACE (old_quals))
2336 : 20 : error ("conflicting type qualifiers for %q+D", newdecl);
2337 : : }
2338 : : else
2339 : 150 : error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2340 : 170 : diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2341 : 170 : locate_old_decl (olddecl);
2342 : 170 : return false;
2343 : : }
2344 : : }
2345 : : /* Warn about enum/integer type mismatches. They are compatible types
2346 : : (C23 6.7.2.2/5), but may pose portability problems. */
2347 : 4766197 : else if (enum_and_int_p
2348 : 191 : && TREE_CODE (newdecl) != TYPE_DECL
2349 : : /* Don't warn about acc_on_device built-in redeclaration,
2350 : : the built-in is declared with int rather than enum because
2351 : : the enum isn't intrinsic. */
2352 : 4766384 : && !(TREE_CODE (olddecl) == FUNCTION_DECL
2353 : 150 : && fndecl_built_in_p (olddecl, BUILT_IN_ACC_ON_DEVICE)
2354 : 120 : && !C_DECL_DECLARED_BUILTIN (olddecl)))
2355 : 67 : warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2356 : 67 : OPT_Wenum_int_mismatch,
2357 : : "conflicting types for %q+D due to enum/integer "
2358 : : "mismatch; have %qT", newdecl, newtype);
2359 : :
2360 : : /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2361 : : but silently ignore the redeclaration if either is in a system
2362 : : header. (Conflicting redeclarations were handled above.) This
2363 : : is allowed for C11 if the types are the same, not just
2364 : : compatible. */
2365 : 4908593 : if (TREE_CODE (newdecl) == TYPE_DECL)
2366 : : {
2367 : 44755 : bool types_different = false;
2368 : :
2369 : 44755 : comptypes_result
2370 : 44755 : = comptypes_check_different_types (oldtype, newtype, &types_different);
2371 : :
2372 : 44755 : if (comptypes_result != 1 || types_different)
2373 : : {
2374 : 11 : error ("redefinition of typedef %q+D with different type", newdecl);
2375 : 11 : locate_old_decl (olddecl);
2376 : 11 : return false;
2377 : : }
2378 : :
2379 : 44744 : if (DECL_IN_SYSTEM_HEADER (newdecl)
2380 : 2461 : || DECL_IN_SYSTEM_HEADER (olddecl)
2381 : 2242 : || warning_suppressed_p (newdecl, OPT_Wpedantic)
2382 : 46986 : || warning_suppressed_p (olddecl, OPT_Wpedantic))
2383 : 42502 : return true; /* Allow OLDDECL to continue in use. */
2384 : :
2385 : 2242 : if (c_type_variably_modified_p (newtype))
2386 : : {
2387 : 3 : error ("redefinition of typedef %q+D with variably modified type",
2388 : : newdecl);
2389 : 3 : locate_old_decl (olddecl);
2390 : : }
2391 : 2239 : else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2392 : : "redefinition of typedef %q+D", newdecl))
2393 : 8 : locate_old_decl (olddecl);
2394 : :
2395 : 2242 : return true;
2396 : : }
2397 : :
2398 : : /* Function declarations can either be 'static' or 'extern' (no
2399 : : qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2400 : : can never conflict with each other on account of linkage
2401 : : (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2402 : : gnu89 mode permits two definitions if one is 'extern inline' and
2403 : : one is not. The non- extern-inline definition supersedes the
2404 : : extern-inline definition. */
2405 : :
2406 : 4863838 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2407 : : {
2408 : : /* If you declare a built-in function name as static, or
2409 : : define the built-in with an old-style definition (so we
2410 : : can't validate the argument list) the built-in definition is
2411 : : overridden, but optionally warn this was a bad choice of name. */
2412 : 4845396 : if (fndecl_built_in_p (olddecl)
2413 : 8663558 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2414 : : {
2415 : 3563036 : if (!TREE_PUBLIC (newdecl)
2416 : 3563036 : || (DECL_INITIAL (newdecl)
2417 : 4855 : && !prototype_p (TREE_TYPE (newdecl))))
2418 : : {
2419 : 103 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2420 : 103 : OPT_Wshadow, "declaration of %qD shadows "
2421 : : "a built-in function", newdecl);
2422 : : /* Discard the old built-in function. */
2423 : 103 : return false;
2424 : : }
2425 : :
2426 : 3562933 : if (!prototype_p (TREE_TYPE (newdecl)))
2427 : : {
2428 : : /* Set for built-ins that take no arguments. */
2429 : 342 : bool func_void_args = false;
2430 : 342 : if (tree at = TYPE_ARG_TYPES (oldtype))
2431 : 342 : func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2432 : :
2433 : 342 : if (extra_warnings && !func_void_args)
2434 : 47 : warning_at (DECL_SOURCE_LOCATION (newdecl),
2435 : 47 : OPT_Wbuiltin_declaration_mismatch,
2436 : : "declaration of built-in function %qD without "
2437 : : "a prototype; expected %qT",
2438 : 47 : newdecl, TREE_TYPE (olddecl));
2439 : : }
2440 : : }
2441 : :
2442 : 4845293 : if (DECL_INITIAL (newdecl))
2443 : : {
2444 : 228878 : if (DECL_INITIAL (olddecl))
2445 : : {
2446 : : /* If the new declaration isn't overriding an extern inline
2447 : : reject the new decl. In c99, no overriding is allowed
2448 : : in the same translation unit. */
2449 : 209 : if (!DECL_EXTERN_INLINE (olddecl)
2450 : 91 : || DECL_EXTERN_INLINE (newdecl)
2451 : 204 : || (!flag_gnu89_inline
2452 : 15 : && (!DECL_DECLARED_INLINE_P (olddecl)
2453 : 15 : || !lookup_attribute ("gnu_inline",
2454 : 15 : DECL_ATTRIBUTES (olddecl)))
2455 : 0 : && (!DECL_DECLARED_INLINE_P (newdecl)
2456 : 0 : || !lookup_attribute ("gnu_inline",
2457 : 0 : DECL_ATTRIBUTES (newdecl)))))
2458 : : {
2459 : 26 : auto_diagnostic_group d;
2460 : 26 : error ("redefinition of %q+D", newdecl);
2461 : 26 : locate_old_decl (olddecl);
2462 : 26 : return false;
2463 : 26 : }
2464 : : }
2465 : : }
2466 : : /* If we have a prototype after an old-style function definition,
2467 : : the argument types must be checked specially. */
2468 : 4616415 : else if (DECL_INITIAL (olddecl)
2469 : 806 : && !prototype_p (oldtype) && prototype_p (newtype)
2470 : 4616435 : && TYPE_ACTUAL_ARG_TYPES (oldtype))
2471 : : {
2472 : 19 : auto_diagnostic_group d;
2473 : 19 : if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2474 : : {
2475 : 10 : locate_old_decl (olddecl);
2476 : 10 : return false;
2477 : : }
2478 : 19 : }
2479 : : /* A non-static declaration (even an "extern") followed by a
2480 : : static declaration is undefined behavior per C99 6.2.2p3-5,7.
2481 : : The same is true for a static forward declaration at block
2482 : : scope followed by a non-static declaration/definition at file
2483 : : scope. Static followed by non-static at the same scope is
2484 : : not undefined behavior, and is the most convenient way to get
2485 : : some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2486 : : the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2487 : : we do diagnose it if -Wtraditional. */
2488 : 4845257 : if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2489 : : {
2490 : : /* Two exceptions to the rule. If olddecl is an extern
2491 : : inline, or a predeclared function that isn't actually
2492 : : built in, newdecl silently overrides olddecl. The latter
2493 : : occur only in Objective C; see also above. (FIXME: Make
2494 : : Objective C use normal builtins.) */
2495 : 19 : if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2496 : 38 : && !DECL_EXTERN_INLINE (olddecl))
2497 : : {
2498 : 5 : auto_diagnostic_group d;
2499 : 5 : error ("static declaration of %q+D follows "
2500 : : "non-static declaration", newdecl);
2501 : 5 : locate_old_decl (olddecl);
2502 : 5 : }
2503 : 19 : return false;
2504 : : }
2505 : 4845238 : else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2506 : : {
2507 : 2099 : if (DECL_CONTEXT (olddecl))
2508 : : {
2509 : 0 : auto_diagnostic_group d;
2510 : 0 : error ("non-static declaration of %q+D follows "
2511 : : "static declaration", newdecl);
2512 : 0 : locate_old_decl (olddecl);
2513 : 0 : return false;
2514 : 0 : }
2515 : 2099 : else if (warn_traditional)
2516 : : {
2517 : 2 : warned |= warning (OPT_Wtraditional,
2518 : : "non-static declaration of %q+D "
2519 : : "follows static declaration", newdecl);
2520 : : }
2521 : : }
2522 : :
2523 : : /* Make sure gnu_inline attribute is either not present, or
2524 : : present on all inline decls. */
2525 : 4845238 : if (DECL_DECLARED_INLINE_P (olddecl)
2526 : 4846301 : && DECL_DECLARED_INLINE_P (newdecl))
2527 : : {
2528 : 808 : bool newa = lookup_attribute ("gnu_inline",
2529 : 808 : DECL_ATTRIBUTES (newdecl)) != NULL;
2530 : 808 : bool olda = lookup_attribute ("gnu_inline",
2531 : 808 : DECL_ATTRIBUTES (olddecl)) != NULL;
2532 : 808 : if (newa != olda)
2533 : : {
2534 : 0 : auto_diagnostic_group d;
2535 : 0 : error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2536 : : newa ? newdecl : olddecl);
2537 : 0 : error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2538 : : "but not here");
2539 : 0 : }
2540 : : }
2541 : : /* Check if these are unmergable overlapping FMV declarations. */
2542 : : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2543 : : && diagnose_versioned_decls (olddecl, newdecl))
2544 : : return false;
2545 : : }
2546 : 18442 : else if (VAR_P (newdecl))
2547 : : {
2548 : : /* Only variables can be thread-local, and all declarations must
2549 : : agree on this property. */
2550 : 18405 : if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2551 : : {
2552 : : /* Nothing to check. Since OLDDECL is marked threadprivate
2553 : : and NEWDECL does not have a thread-local attribute, we
2554 : : will merge the threadprivate attribute into NEWDECL. */
2555 : : ;
2556 : : }
2557 : 55002 : else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2558 : : {
2559 : 6 : auto_diagnostic_group d;
2560 : 6 : if (DECL_THREAD_LOCAL_P (newdecl))
2561 : 3 : error ("thread-local declaration of %q+D follows "
2562 : : "non-thread-local declaration", newdecl);
2563 : : else
2564 : 3 : error ("non-thread-local declaration of %q+D follows "
2565 : : "thread-local declaration", newdecl);
2566 : :
2567 : 6 : locate_old_decl (olddecl);
2568 : 6 : return false;
2569 : 6 : }
2570 : :
2571 : : /* Multiple initialized definitions are not allowed (6.9p3,5).
2572 : : For this purpose, C23 makes it clear that thread-local
2573 : : declarations without extern are definitions, not tentative
2574 : : definitions, whether or not they have initializers. The
2575 : : wording before C23 was unclear; literally it would have made
2576 : : uninitialized thread-local declarations into tentative
2577 : : definitions only if they also used static, but without saying
2578 : : explicitly whether or not other cases count as
2579 : : definitions at all. */
2580 : 18932 : if ((DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2581 : 18931 : || (flag_isoc23
2582 : 6249 : && DECL_THREAD_LOCAL_P (newdecl)
2583 : 25 : && !DECL_EXTERNAL (newdecl)
2584 : 21 : && !DECL_EXTERNAL (olddecl)))
2585 : : {
2586 : 7 : auto_diagnostic_group d;
2587 : 7 : error ("redefinition of %q+D", newdecl);
2588 : 7 : locate_old_decl (olddecl);
2589 : 7 : return false;
2590 : 7 : }
2591 : :
2592 : : /* Objects declared at file scope: if the first declaration had
2593 : : external linkage (even if it was an external reference) the
2594 : : second must have external linkage as well, or the behavior is
2595 : : undefined. If the first declaration had internal linkage, then
2596 : : the second must too, or else be an external reference (in which
2597 : : case the composite declaration still has internal linkage).
2598 : : As for function declarations, we warn about the static-then-
2599 : : extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2600 : 18409 : if (DECL_FILE_SCOPE_P (newdecl)
2601 : 18392 : && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2602 : : {
2603 : 147 : if (DECL_EXTERNAL (newdecl))
2604 : : {
2605 : 144 : if (!DECL_FILE_SCOPE_P (olddecl))
2606 : : {
2607 : 2 : auto_diagnostic_group d;
2608 : 2 : error ("extern declaration of %q+D follows "
2609 : : "declaration with no linkage", newdecl);
2610 : 2 : locate_old_decl (olddecl);
2611 : 2 : return false;
2612 : 2 : }
2613 : 142 : else if (warn_traditional)
2614 : : {
2615 : 0 : warned |= warning (OPT_Wtraditional,
2616 : : "non-static declaration of %q+D "
2617 : : "follows static declaration", newdecl);
2618 : : }
2619 : : }
2620 : : else
2621 : : {
2622 : 3 : auto_diagnostic_group d;
2623 : 3 : if (TREE_PUBLIC (newdecl))
2624 : 2 : error ("non-static declaration of %q+D follows "
2625 : : "static declaration", newdecl);
2626 : : else
2627 : 1 : error ("static declaration of %q+D follows "
2628 : : "non-static declaration", newdecl);
2629 : :
2630 : 3 : locate_old_decl (olddecl);
2631 : 3 : return false;
2632 : 3 : }
2633 : : }
2634 : : /* Two objects with the same name declared at the same block
2635 : : scope must both be external references (6.7p3). */
2636 : 18245 : else if (!DECL_FILE_SCOPE_P (newdecl))
2637 : : {
2638 : 17 : if (DECL_EXTERNAL (newdecl))
2639 : : {
2640 : : /* Extern with initializer at block scope, which will
2641 : : already have received an error. */
2642 : : }
2643 : 15 : else if (DECL_EXTERNAL (olddecl))
2644 : : {
2645 : 4 : auto_diagnostic_group d;
2646 : 4 : error ("declaration of %q+D with no linkage follows "
2647 : : "extern declaration", newdecl);
2648 : 4 : locate_old_decl (olddecl);
2649 : 4 : }
2650 : : else
2651 : : {
2652 : 11 : auto_diagnostic_group d;
2653 : 11 : error ("redeclaration of %q+D with no linkage", newdecl);
2654 : 11 : locate_old_decl (olddecl);
2655 : 11 : }
2656 : :
2657 : 17 : return false;
2658 : : }
2659 : :
2660 : : /* C++ does not permit a decl to appear multiple times at file
2661 : : scope. */
2662 : 18370 : if (warn_cxx_compat
2663 : 68 : && DECL_FILE_SCOPE_P (newdecl)
2664 : 68 : && !DECL_EXTERNAL (newdecl)
2665 : 18388 : && !DECL_EXTERNAL (olddecl))
2666 : 5 : warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2667 : 5 : OPT_Wc___compat,
2668 : : "duplicate declaration of %qD is "
2669 : : "invalid in C++", newdecl);
2670 : : }
2671 : :
2672 : : /* warnings */
2673 : : /* All decls must agree on a visibility. */
2674 : 4863645 : if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2675 : 4863608 : && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2676 : 4864380 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2677 : : {
2678 : 1 : warned |= warning (0, "redeclaration of %q+D with different visibility "
2679 : : "(old visibility preserved)", newdecl);
2680 : : }
2681 : :
2682 : 4863645 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2683 : 4845238 : warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2684 : : else /* PARM_DECL, VAR_DECL */
2685 : : {
2686 : : /* Redeclaration of a parameter is a constraint violation (this is
2687 : : not explicitly stated, but follows from C99 6.7p3 [no more than
2688 : : one declaration of the same identifier with no linkage in the
2689 : : same scope, except type tags] and 6.2.2p6 [parameters have no
2690 : : linkage]). We must check for a forward parameter declaration,
2691 : : indicated by TREE_ASM_WRITTEN on the old declaration - this is
2692 : : an extension, the mandatory diagnostic for which is handled by
2693 : : mark_forward_parm_decls. */
2694 : :
2695 : 18407 : if (TREE_CODE (newdecl) == PARM_DECL
2696 : 37 : && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2697 : : {
2698 : 2 : auto_diagnostic_group d;
2699 : 2 : error ("redefinition of parameter %q+D", newdecl);
2700 : 2 : locate_old_decl (olddecl);
2701 : 2 : return false;
2702 : 2 : }
2703 : : }
2704 : :
2705 : : /* Optional warning for completely redundant decls. */
2706 : 4863643 : if (!warned && !pedwarned
2707 : 4863569 : && warn_redundant_decls
2708 : : /* Don't warn about a function declaration followed by a
2709 : : definition. */
2710 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2711 : 2 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2712 : : /* Don't warn about redundant redeclarations of builtins. */
2713 : 18 : && !(TREE_CODE (newdecl) == FUNCTION_DECL
2714 : 2 : && !fndecl_built_in_p (newdecl)
2715 : 2 : && fndecl_built_in_p (olddecl)
2716 : 2 : && !C_DECL_DECLARED_BUILTIN (olddecl))
2717 : : /* Don't warn about an extern followed by a definition. */
2718 : 17 : && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2719 : : /* Don't warn about forward parameter decls. */
2720 : 17 : && !(TREE_CODE (newdecl) == PARM_DECL
2721 : 6 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2722 : : /* Don't warn about a variable definition following a declaration. */
2723 : 4863654 : && !(VAR_P (newdecl)
2724 : 10 : && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2725 : : {
2726 : 6 : warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2727 : : newdecl);
2728 : : }
2729 : :
2730 : : /* Report location of previous decl/defn. */
2731 : 4863643 : if (warned || pedwarned)
2732 : 80 : locate_old_decl (olddecl);
2733 : :
2734 : : #undef DECL_EXTERN_INLINE
2735 : :
2736 : : return retval;
2737 : 4909381 : }
2738 : :
2739 : : /* Subroutine of duplicate_decls. NEWDECL has been found to be
2740 : : consistent with OLDDECL, but carries new information. Merge the
2741 : : new information into OLDDECL. This function issues no
2742 : : diagnostics. */
2743 : :
2744 : : static void
2745 : 4908387 : merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2746 : : {
2747 : 4908387 : bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2748 : 4908387 : && DECL_INITIAL (newdecl) != NULL_TREE);
2749 : 4908387 : bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2750 : 4908387 : && prototype_p (TREE_TYPE (newdecl)));
2751 : 4908387 : bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2752 : 4908387 : && prototype_p (TREE_TYPE (olddecl)));
2753 : :
2754 : : /* For real parm decl following a forward decl, rechain the old decl
2755 : : in its new location and clear TREE_ASM_WRITTEN (it's not a
2756 : : forward decl anymore). */
2757 : 4908387 : if (TREE_CODE (newdecl) == PARM_DECL
2758 : 35 : && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2759 : : {
2760 : 35 : struct c_binding *b, **here;
2761 : :
2762 : 54 : for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2763 : 54 : if ((*here)->decl == olddecl)
2764 : 35 : goto found;
2765 : 0 : gcc_unreachable ();
2766 : :
2767 : 35 : found:
2768 : 35 : b = *here;
2769 : 35 : *here = b->prev;
2770 : 35 : b->prev = current_scope->bindings;
2771 : 35 : current_scope->bindings = b;
2772 : :
2773 : 35 : TREE_ASM_WRITTEN (olddecl) = 0;
2774 : : }
2775 : :
2776 : 4908387 : DECL_ATTRIBUTES (newdecl)
2777 : 4908387 : = targetm.merge_decl_attributes (olddecl, newdecl);
2778 : :
2779 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2780 : : at newdecl, which will be ggc_freed. */
2781 : 4908387 : if (TREE_CODE (newdecl) == TYPE_DECL)
2782 : : {
2783 : : /* But NEWTYPE might have an attribute, honor that. */
2784 : 44744 : tree tem = newtype;
2785 : 44744 : newtype = oldtype;
2786 : :
2787 : 44744 : if (TYPE_USER_ALIGN (tem))
2788 : : {
2789 : 12 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2790 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2791 : 12 : TYPE_USER_ALIGN (newtype) = true;
2792 : : }
2793 : :
2794 : : /* And remove the new type from the variants list. */
2795 : 44744 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2796 : : {
2797 : 12 : tree remove = TREE_TYPE (newdecl);
2798 : 12 : if (TYPE_MAIN_VARIANT (remove) == remove)
2799 : : {
2800 : 2 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2801 : : /* If remove is the main variant, no need to remove that
2802 : : from the list. One of the DECL_ORIGINAL_TYPE
2803 : : variants, e.g. created for aligned attribute, might still
2804 : : refer to the newdecl TYPE_DECL though, so remove that one
2805 : : in that case. */
2806 : 2 : if (DECL_ORIGINAL_TYPE (newdecl)
2807 : 2 : && DECL_ORIGINAL_TYPE (newdecl) != remove)
2808 : 2 : for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2809 : 2 : t; t = TYPE_MAIN_VARIANT (t))
2810 : 2 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2811 : : {
2812 : 4 : TYPE_NEXT_VARIANT (t)
2813 : 2 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2814 : 2 : break;
2815 : : }
2816 : : }
2817 : : else
2818 : 10 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2819 : 0 : t = TYPE_NEXT_VARIANT (t))
2820 : 10 : if (TYPE_NEXT_VARIANT (t) == remove)
2821 : : {
2822 : 10 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2823 : 10 : break;
2824 : : }
2825 : : }
2826 : :
2827 : : /* Make sure we refer to the same type as the olddecl. */
2828 : 44744 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2829 : : }
2830 : :
2831 : : /* Merge the data types specified in the two decls. */
2832 : 9816774 : TREE_TYPE (newdecl)
2833 : 4908387 : = TREE_TYPE (olddecl)
2834 : 9816774 : = composite_type (newtype, oldtype);
2835 : :
2836 : : /* Lay the type out, unless already done. */
2837 : 4908387 : if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2838 : : {
2839 : 0 : if (TREE_TYPE (newdecl) != error_mark_node)
2840 : 0 : layout_type (TREE_TYPE (newdecl));
2841 : 0 : if (TREE_CODE (newdecl) != FUNCTION_DECL
2842 : : && TREE_CODE (newdecl) != TYPE_DECL
2843 : : && TREE_CODE (newdecl) != CONST_DECL)
2844 : 0 : layout_decl (newdecl, 0);
2845 : : }
2846 : : else
2847 : : {
2848 : : /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2849 : 4908387 : DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2850 : 4908387 : DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2851 : 4908387 : SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2852 : 4908387 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2853 : : {
2854 : 44 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2855 : 44 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2856 : : }
2857 : 4908343 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2858 : 4908343 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2859 : 3 : DECL_USER_ALIGN (newdecl) = 1;
2860 : 9816774 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2861 : 4908387 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
2862 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2863 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
2864 : : }
2865 : :
2866 : : /* Keep the old rtl since we can safely use it. */
2867 : 4908387 : if (HAS_RTL_P (olddecl))
2868 : 4908387 : COPY_DECL_RTL (olddecl, newdecl);
2869 : :
2870 : : /* Merge the type qualifiers. */
2871 : 4908387 : if (TREE_READONLY (newdecl))
2872 : 387204 : TREE_READONLY (olddecl) = 1;
2873 : :
2874 : 4908387 : if (TREE_THIS_VOLATILE (newdecl))
2875 : 48693 : TREE_THIS_VOLATILE (olddecl) = 1;
2876 : :
2877 : : /* Merge deprecatedness. */
2878 : 4908387 : if (TREE_DEPRECATED (newdecl))
2879 : 331 : TREE_DEPRECATED (olddecl) = 1;
2880 : :
2881 : : /* Merge unavailability. */
2882 : 4908387 : if (TREE_UNAVAILABLE (newdecl))
2883 : 2 : TREE_UNAVAILABLE (olddecl) = 1;
2884 : :
2885 : : /* If a decl is in a system header and the other isn't, keep the one on the
2886 : : system header. Otherwise, keep source location of definition rather than
2887 : : declaration and of prototype rather than non-prototype unless that
2888 : : prototype is built-in. */
2889 : 4908387 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2890 : 4908352 : && DECL_IN_SYSTEM_HEADER (olddecl)
2891 : 5582280 : && !DECL_IN_SYSTEM_HEADER (newdecl) )
2892 : 1035 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2893 : 4907352 : else if (HAS_DECL_ASSEMBLER_NAME_P (olddecl)
2894 : 4907317 : && DECL_IN_SYSTEM_HEADER (newdecl)
2895 : 9090938 : && !DECL_IN_SYSTEM_HEADER (olddecl))
2896 : 3510728 : DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2897 : 1396624 : else if ((DECL_INITIAL (newdecl) == NULL_TREE
2898 : 1167308 : && DECL_INITIAL (olddecl) != NULL_TREE)
2899 : 2563028 : || (old_is_prototype && !new_is_prototype
2900 : 372 : && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2901 : 932 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2902 : :
2903 : : /* Merge the initialization information. */
2904 : 4908387 : if (DECL_INITIAL (newdecl) == NULL_TREE)
2905 : 4678999 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2906 : :
2907 : : /* Merge 'constexpr' information. */
2908 : 4908387 : if (VAR_P (olddecl) && VAR_P (newdecl))
2909 : : {
2910 : 18370 : if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2911 : 2 : C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2912 : 18368 : else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2913 : 1 : C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2914 : : }
2915 : :
2916 : : /* Merge the threadprivate attribute. */
2917 : 4908387 : if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2918 : 7 : C_DECL_THREADPRIVATE_P (newdecl) = 1;
2919 : :
2920 : 4908387 : if (HAS_DECL_ASSEMBLER_NAME_P (olddecl))
2921 : : {
2922 : : /* Copy the assembler name.
2923 : : Currently, it can only be defined in the prototype. */
2924 : 4908352 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2925 : :
2926 : : /* Use visibility of whichever declaration had it specified */
2927 : 4908352 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
2928 : : {
2929 : 4741 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2930 : 4741 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2931 : : }
2932 : :
2933 : 4908352 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2934 : : {
2935 : 4845238 : DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2936 : 4845238 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2937 : 4845238 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2938 : 4845238 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2939 : 4845238 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2940 : 4845238 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2941 : 4845238 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2942 : 4845238 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2943 : 0 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2944 : 4845238 : if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2945 : 0 : DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2946 : 4845238 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2947 : 4845238 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2948 : 4845238 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2949 : : }
2950 : :
2951 : : /* Merge the storage class information. */
2952 : 4908352 : merge_weak (newdecl, olddecl);
2953 : :
2954 : : /* For functions, static overrides non-static. */
2955 : 4908352 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2956 : : {
2957 : 4845238 : TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2958 : : /* This is since we don't automatically
2959 : : copy the attributes of NEWDECL into OLDDECL. */
2960 : 4845238 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2961 : : /* If this clears `static', clear it in the identifier too. */
2962 : 4845238 : if (!TREE_PUBLIC (olddecl))
2963 : 7528 : TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2964 : : }
2965 : : }
2966 : :
2967 : : /* In c99, 'extern' declaration before (or after) 'inline' means this
2968 : : function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2969 : : is present. */
2970 : 4908387 : if (TREE_CODE (newdecl) == FUNCTION_DECL
2971 : 4845238 : && !flag_gnu89_inline
2972 : 4821156 : && (DECL_DECLARED_INLINE_P (newdecl)
2973 : 4634190 : || DECL_DECLARED_INLINE_P (olddecl))
2974 : 187160 : && (!DECL_DECLARED_INLINE_P (newdecl)
2975 : 186966 : || !DECL_DECLARED_INLINE_P (olddecl)
2976 : 795 : || !DECL_EXTERNAL (olddecl))
2977 : 186383 : && DECL_EXTERNAL (newdecl)
2978 : 186175 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2979 : 4908485 : && !current_function_decl)
2980 : 81 : DECL_EXTERNAL (newdecl) = 0;
2981 : :
2982 : : /* An inline definition following a static declaration is not
2983 : : DECL_EXTERNAL. */
2984 : 4908387 : if (new_is_definition
2985 : 228835 : && (DECL_DECLARED_INLINE_P (newdecl)
2986 : 41647 : || DECL_DECLARED_INLINE_P (olddecl))
2987 : 5095789 : && !TREE_PUBLIC (olddecl))
2988 : 888 : DECL_EXTERNAL (newdecl) = 0;
2989 : :
2990 : 4908387 : if (DECL_EXTERNAL (newdecl))
2991 : : {
2992 : 4818846 : TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2993 : 4818846 : DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2994 : :
2995 : : /* An extern decl does not override previous storage class. */
2996 : 4818846 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2997 : 4818846 : if (!DECL_EXTERNAL (newdecl))
2998 : : {
2999 : 1419 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3000 : 1419 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3001 : : }
3002 : : }
3003 : : else
3004 : : {
3005 : 89541 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
3006 : 89541 : TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
3007 : : }
3008 : :
3009 : 4908387 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3010 : : {
3011 : 4845238 : if (DECL_FUNCTION_VERSIONED (olddecl)
3012 : 4845238 : || DECL_FUNCTION_VERSIONED (newdecl))
3013 : : {
3014 : 0 : maybe_mark_function_versioned (olddecl);
3015 : 0 : maybe_mark_function_versioned (newdecl);
3016 : : }
3017 : : /* If we're redefining a function previously defined as extern
3018 : : inline, make sure we emit debug info for the inline before we
3019 : : throw it away, in case it was inlined into a function that
3020 : : hasn't been written out yet. */
3021 : 4845238 : if (new_is_definition && DECL_INITIAL (olddecl))
3022 : : /* The new defn must not be inline. */
3023 : 75 : DECL_UNINLINABLE (newdecl) = 1;
3024 : : else
3025 : : {
3026 : : /* If either decl says `inline', this fn is inline, unless
3027 : : its definition was passed already. */
3028 : 4845163 : if (DECL_DECLARED_INLINE_P (newdecl)
3029 : 9503086 : || DECL_DECLARED_INLINE_P (olddecl))
3030 : 187425 : DECL_DECLARED_INLINE_P (newdecl) = 1;
3031 : :
3032 : 14535489 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3033 : 9691985 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3034 : :
3035 : 9690326 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3036 : 4845163 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3037 : 4845163 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3038 : 9690188 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3039 : : }
3040 : :
3041 : 4845238 : if (fndecl_built_in_p (olddecl))
3042 : : {
3043 : : /* If redeclaring a builtin function, it stays built in.
3044 : : But it gets tagged as having been declared. */
3045 : 3818059 : copy_decl_built_in_function (newdecl, olddecl);
3046 : 3818059 : C_DECL_DECLARED_BUILTIN (newdecl) = 1;
3047 : 3818059 : if (new_is_prototype)
3048 : : {
3049 : 3817700 : C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
3050 : 3817700 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3051 : : {
3052 : 3817700 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3053 : 3817700 : switch (fncode)
3054 : : {
3055 : : /* If a compatible prototype of these builtin functions
3056 : : is seen, assume the runtime implements it with the
3057 : : expected semantics. */
3058 : 6440 : case BUILT_IN_STPCPY:
3059 : 6440 : if (builtin_decl_explicit_p (fncode))
3060 : 6440 : set_builtin_decl_implicit_p (fncode, true);
3061 : : break;
3062 : 3811260 : default:
3063 : 3811260 : if (builtin_decl_explicit_p (fncode))
3064 : 3811260 : set_builtin_decl_declared_p (fncode, true);
3065 : : break;
3066 : : }
3067 : :
3068 : 3817700 : copy_attributes_to_builtin (newdecl);
3069 : : }
3070 : : }
3071 : : else
3072 : 718 : C_DECL_BUILTIN_PROTOTYPE (newdecl)
3073 : 718 : = C_DECL_BUILTIN_PROTOTYPE (olddecl);
3074 : : }
3075 : :
3076 : : /* Preserve function specific target and optimization options */
3077 : 4845238 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3078 : 4845744 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3079 : 470 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3080 : 470 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3081 : :
3082 : 4845238 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3083 : 4868660 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3084 : 9 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3085 : 9 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3086 : :
3087 : : /* Also preserve various other info from the definition. */
3088 : 4845238 : if (!new_is_definition)
3089 : : {
3090 : 4616403 : tree t;
3091 : 4616403 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3092 : 4616403 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3093 : 4616403 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3094 : 4616403 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3095 : 4616403 : DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
3096 : 7232329 : for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
3097 : 2615926 : DECL_CONTEXT (t) = newdecl;
3098 : :
3099 : : /* See if we've got a function to instantiate from. */
3100 : 4616403 : if (DECL_SAVED_TREE (olddecl))
3101 : 1584 : DECL_ABSTRACT_ORIGIN (newdecl)
3102 : 792 : = DECL_ABSTRACT_ORIGIN (olddecl);
3103 : : }
3104 : : }
3105 : :
3106 : : /* Merge the USED information. */
3107 : 4908387 : if (TREE_USED (olddecl))
3108 : 703224 : TREE_USED (newdecl) = 1;
3109 : 4205163 : else if (TREE_USED (newdecl))
3110 : 4 : TREE_USED (olddecl) = 1;
3111 : 4908387 : if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
3112 : 18405 : DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
3113 : 4908387 : if (DECL_PRESERVE_P (olddecl))
3114 : 25 : DECL_PRESERVE_P (newdecl) = 1;
3115 : 4908362 : else if (DECL_PRESERVE_P (newdecl))
3116 : 4 : DECL_PRESERVE_P (olddecl) = 1;
3117 : :
3118 : : /* Merge DECL_COMMON */
3119 : 18370 : if (VAR_P (olddecl) && VAR_P (newdecl)
3120 : 18370 : && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
3121 : 4926755 : && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
3122 : 36732 : DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
3123 : :
3124 : : /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
3125 : : But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
3126 : : DECL_ARGUMENTS (if appropriate). */
3127 : 4908387 : {
3128 : 4908387 : unsigned olddecl_uid = DECL_UID (olddecl);
3129 : 4908387 : tree olddecl_context = DECL_CONTEXT (olddecl);
3130 : 4908387 : tree olddecl_arguments = NULL;
3131 : 4908387 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3132 : 4845238 : olddecl_arguments = DECL_ARGUMENTS (olddecl);
3133 : :
3134 : 4908387 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3135 : : (char *) newdecl + sizeof (struct tree_common),
3136 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3137 : 4908387 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3138 : 4908387 : switch (TREE_CODE (olddecl))
3139 : : {
3140 : 4863608 : case FUNCTION_DECL:
3141 : 4863608 : case VAR_DECL:
3142 : 4863608 : {
3143 : 4863608 : struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
3144 : :
3145 : 9727216 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3146 : : (char *) newdecl + sizeof (struct tree_decl_common),
3147 : 4863608 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3148 : 4863608 : olddecl->decl_with_vis.symtab_node = snode;
3149 : :
3150 : 4863608 : if ((DECL_EXTERNAL (olddecl)
3151 : 46181 : || TREE_PUBLIC (olddecl)
3152 : 7795 : || TREE_STATIC (olddecl))
3153 : 4909782 : && DECL_SECTION_NAME (newdecl) != NULL)
3154 : 8 : set_decl_section_name (olddecl, newdecl);
3155 : :
3156 : : /* This isn't quite correct for something like
3157 : : int __thread x attribute ((tls_model ("local-exec")));
3158 : : extern int __thread x;
3159 : : as we'll lose the "local-exec" model. */
3160 : 4863608 : if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3161 : 89 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3162 : : break;
3163 : : }
3164 : :
3165 : 44779 : case FIELD_DECL:
3166 : 44779 : case PARM_DECL:
3167 : 44779 : case LABEL_DECL:
3168 : 44779 : case RESULT_DECL:
3169 : 44779 : case CONST_DECL:
3170 : 44779 : case TYPE_DECL:
3171 : 89558 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3172 : : (char *) newdecl + sizeof (struct tree_decl_common),
3173 : 44779 : tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3174 : 44779 : break;
3175 : :
3176 : 0 : default:
3177 : :
3178 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3179 : : (char *) newdecl + sizeof (struct tree_decl_common),
3180 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3181 : : }
3182 : 4908387 : DECL_UID (olddecl) = olddecl_uid;
3183 : 4908387 : DECL_CONTEXT (olddecl) = olddecl_context;
3184 : 4908387 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3185 : 4845238 : DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3186 : : }
3187 : :
3188 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3189 : : so that encode_section_info has a chance to look at the new decl
3190 : : flags and attributes. */
3191 : 4908387 : if (DECL_RTL_SET_P (olddecl)
3192 : 4908387 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3193 : 0 : || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3194 : 0 : make_decl_rtl (olddecl);
3195 : 4908387 : }
3196 : :
3197 : : /* Handle when a new declaration NEWDECL has the same name as an old
3198 : : one OLDDECL in the same binding contour. Prints an error message
3199 : : if appropriate.
3200 : :
3201 : : If safely possible, alter OLDDECL to look like NEWDECL, and return
3202 : : true. Otherwise, return false. */
3203 : :
3204 : : static bool
3205 : 4909484 : duplicate_decls (tree newdecl, tree olddecl)
3206 : : {
3207 : 4909484 : tree newtype = NULL, oldtype = NULL;
3208 : :
3209 : 4909484 : if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3210 : : {
3211 : : /* Avoid `unused variable' and other warnings for OLDDECL. */
3212 : 1097 : suppress_warning (olddecl, OPT_Wunused);
3213 : : /* If the types are completely different, poison them both with
3214 : : error_mark_node. */
3215 : 1097 : if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3216 : 114 : && olddecl != error_mark_node
3217 : 1191 : && seen_error ())
3218 : : {
3219 : 60 : if (TREE_CODE (olddecl) != FUNCTION_DECL)
3220 : 48 : TREE_TYPE (olddecl) = error_mark_node;
3221 : 60 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
3222 : 57 : TREE_TYPE (newdecl) = error_mark_node;
3223 : : }
3224 : 1097 : return false;
3225 : : }
3226 : :
3227 : 4908387 : merge_decls (newdecl, olddecl, newtype, oldtype);
3228 : :
3229 : : /* The NEWDECL will no longer be needed.
3230 : :
3231 : : Before releasing the node, be sure to remove function from symbol
3232 : : table that might have been inserted there to record comdat group.
3233 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3234 : : structure is shared in between NEWDECL and OLDECL. */
3235 : 4908387 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3236 : 4845238 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3237 : 4908387 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3238 : : {
3239 : 4863608 : struct symtab_node *snode = symtab_node::get (newdecl);
3240 : 4863608 : if (snode)
3241 : 104 : snode->remove ();
3242 : : }
3243 : 4908387 : ggc_free (newdecl);
3244 : 4908387 : return true;
3245 : : }
3246 : :
3247 : :
3248 : : /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3249 : : static void
3250 : 166516381 : warn_if_shadowing (tree new_decl)
3251 : : {
3252 : 166516381 : struct c_binding *b;
3253 : :
3254 : : /* Shadow warnings wanted? */
3255 : 333031846 : if (!(warn_shadow
3256 : 166515645 : || warn_shadow_local
3257 : 166515465 : || warn_shadow_compatible_local)
3258 : : /* No shadow warnings for internally generated vars. */
3259 : 166516624 : || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3260 : : return;
3261 : :
3262 : : /* Is anything being shadowed? Invisible decls do not count. */
3263 : 253 : for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3264 : 157 : if (b->decl && b->decl != new_decl && !b->invisible
3265 : 214 : && (b->decl == error_mark_node
3266 : 43 : || diagnostic_report_warnings_p (global_dc,
3267 : : DECL_SOURCE_LOCATION (b->decl))))
3268 : : {
3269 : 57 : tree old_decl = b->decl;
3270 : :
3271 : 57 : if (old_decl == error_mark_node)
3272 : : {
3273 : 14 : warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3274 : : "non-variable", new_decl);
3275 : 50 : break;
3276 : : }
3277 : :
3278 : 43 : bool warned = false;
3279 : 43 : auto_diagnostic_group d;
3280 : 43 : if (TREE_CODE (old_decl) == PARM_DECL)
3281 : : {
3282 : 5 : enum opt_code warning_code;
3283 : :
3284 : : /* If '-Wshadow=compatible-local' is specified without other
3285 : : -Wshadow= flags, we will warn only when the types of the
3286 : : shadowing variable (i.e. new_decl) and the shadowed variable
3287 : : (old_decl) are compatible. */
3288 : 5 : if (warn_shadow)
3289 : : warning_code = OPT_Wshadow;
3290 : 2 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3291 : : warning_code = OPT_Wshadow_compatible_local;
3292 : : else
3293 : 2 : warning_code = OPT_Wshadow_local;
3294 : 5 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3295 : : "declaration of %qD shadows a parameter",
3296 : : new_decl);
3297 : : }
3298 : 38 : else if (DECL_FILE_SCOPE_P (old_decl))
3299 : : {
3300 : : /* Do not warn if a variable shadows a function, unless
3301 : : the variable is a function or a pointer-to-function. */
3302 : 34 : if (TREE_CODE (old_decl) == FUNCTION_DECL
3303 : 11 : && TREE_CODE (new_decl) != FUNCTION_DECL
3304 : 36 : && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3305 : 7 : continue;
3306 : :
3307 : 20 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3308 : : "declaration of %qD shadows a global "
3309 : : "declaration",
3310 : : new_decl);
3311 : : }
3312 : 11 : else if (TREE_CODE (old_decl) == FUNCTION_DECL
3313 : 11 : && fndecl_built_in_p (old_decl))
3314 : : {
3315 : 0 : warning (OPT_Wshadow, "declaration of %q+D shadows "
3316 : : "a built-in function", new_decl);
3317 : 0 : break;
3318 : : }
3319 : : else
3320 : : {
3321 : 11 : enum opt_code warning_code;
3322 : :
3323 : : /* If '-Wshadow=compatible-local' is specified without other
3324 : : -Wshadow= flags, we will warn only when the types of the
3325 : : shadowing variable (i.e. new_decl) and the shadowed variable
3326 : : (old_decl) are compatible. */
3327 : 11 : if (warn_shadow)
3328 : : warning_code = OPT_Wshadow;
3329 : 8 : else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3330 : : warning_code = OPT_Wshadow_compatible_local;
3331 : : else
3332 : 2 : warning_code = OPT_Wshadow_local;
3333 : 11 : warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3334 : : "declaration of %qD shadows a previous local",
3335 : : new_decl);
3336 : : }
3337 : :
3338 : 36 : if (warned)
3339 : 26 : inform (DECL_SOURCE_LOCATION (old_decl),
3340 : : "shadowed declaration is here");
3341 : :
3342 : : break;
3343 : 43 : }
3344 : : }
3345 : :
3346 : : /* Record a decl-node X as belonging to the current lexical scope.
3347 : : Check for errors (such as an incompatible declaration for the same
3348 : : name already seen in the same scope).
3349 : :
3350 : : Returns either X or an old decl for the same name.
3351 : : If an old decl is returned, it may have been smashed
3352 : : to agree with what X says. */
3353 : :
3354 : : tree
3355 : 198300289 : pushdecl (tree x)
3356 : : {
3357 : 198300289 : tree name = DECL_NAME (x);
3358 : 198300289 : struct c_scope *scope = current_scope;
3359 : 198300289 : struct c_binding *b;
3360 : 198300289 : bool nested = false;
3361 : 198300289 : location_t locus = DECL_SOURCE_LOCATION (x);
3362 : :
3363 : : /* Must set DECL_CONTEXT for everything not at file scope or
3364 : : DECL_FILE_SCOPE_P won't work. Local externs don't count
3365 : : unless they have initializers (which generate code). We
3366 : : also exclude CONST_DECLs because enumerators will get the
3367 : : type of the enum as context. */
3368 : 198300289 : if (current_function_decl
3369 : 8943382 : && TREE_CODE (x) != CONST_DECL
3370 : 207090381 : && (!VAR_OR_FUNCTION_DECL_P (x)
3371 : 8514081 : || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3372 : 8778082 : DECL_CONTEXT (x) = current_function_decl;
3373 : :
3374 : : /* Anonymous decls are just inserted in the scope. */
3375 : 198300289 : if (!name)
3376 : : {
3377 : 7696117 : bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3378 : : locus);
3379 : 7696117 : return x;
3380 : : }
3381 : :
3382 : : /* First, see if there is another declaration with the same name in
3383 : : the current scope. If there is, duplicate_decls may do all the
3384 : : work for us. If duplicate_decls returns false, that indicates
3385 : : two incompatible decls in the same scope; we are to silently
3386 : : replace the old one (duplicate_decls has issued all appropriate
3387 : : diagnostics). In particular, we should not consider possible
3388 : : duplicates in the external scope, or shadowing. */
3389 : 190604172 : b = I_SYMBOL_BINDING (name);
3390 : 190604172 : if (b && B_IN_SCOPE (b, scope))
3391 : : {
3392 : 1365593 : struct c_binding *b_ext, *b_use;
3393 : 1365593 : tree type = TREE_TYPE (x);
3394 : 1365593 : tree visdecl = b->decl;
3395 : 1365593 : tree vistype = TREE_TYPE (visdecl);
3396 : 1365593 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3397 : 1365593 : && COMPLETE_TYPE_P (TREE_TYPE (x)))
3398 : 1365 : b->inner_comp = false;
3399 : 1365593 : b_use = b;
3400 : 1365593 : b_ext = b;
3401 : : /* If this is an external linkage declaration, we should check
3402 : : for compatibility with the type in the external scope before
3403 : : setting the type at this scope based on the visible
3404 : : information only. */
3405 : 1365593 : if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3406 : : {
3407 : 2625841 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3408 : 1312930 : b_ext = b_ext->shadowed;
3409 : 1312911 : if (b_ext)
3410 : : {
3411 : 1312910 : b_use = b_ext;
3412 : 1312910 : if (b_use->u.type)
3413 : 266853 : TREE_TYPE (b_use->decl) = b_use->u.type;
3414 : : }
3415 : : }
3416 : :
3417 : : /* Check if x is part of a FMV set with b_use.
3418 : : FMV is only supported in c for targets with target_version
3419 : : attributes. */
3420 : 1365593 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
3421 : : && b_use && TREE_CODE (b_use->decl) == FUNCTION_DECL
3422 : : && TREE_CODE (x) == FUNCTION_DECL && DECL_FILE_SCOPE_P (b_use->decl)
3423 : : && DECL_FILE_SCOPE_P (x)
3424 : : && disjoint_version_decls (x, b_use->decl)
3425 : : && comptypes (vistype, type) != 0)
3426 : : {
3427 : : maybe_mark_function_versioned (b_use->decl);
3428 : : maybe_mark_function_versioned (b->decl);
3429 : : maybe_mark_function_versioned (x);
3430 : :
3431 : : cgraph_node *b_node = cgraph_node::get_create (b_use->decl);
3432 : : cgraph_function_version_info *b_v = b_node->function_version ();
3433 : : if (!b_v)
3434 : : b_v = b_node->insert_new_function_version ();
3435 : :
3436 : : /* Check if this new node conflicts with any previous functions
3437 : : in the set. */
3438 : : cgraph_function_version_info *version = b_v;
3439 : : for (; version; version = version->next)
3440 : : if (!disjoint_version_decls (version->this_node->decl, x))
3441 : : {
3442 : : /* The decls define overlapping version, so attempt to merge
3443 : : or diagnose the conflict. */
3444 : : if (duplicate_decls (x, version->this_node->decl))
3445 : : return version->this_node->decl;
3446 : : else
3447 : : return error_mark_node;
3448 : : }
3449 : :
3450 : : /* This is a new version to be added to FMV structure. */
3451 : : cgraph_node::add_function_version (b_v, x);
3452 : :
3453 : : /* Get the first node from the structure. */
3454 : : cgraph_function_version_info *default_v = b_v;
3455 : : while (default_v->prev)
3456 : : default_v = default_v->prev;
3457 : : /* Always use the default node for the bindings. */
3458 : : b_use->decl = default_v->this_node->decl;
3459 : : b->decl = default_v->this_node->decl;
3460 : :
3461 : : /* Node is not a duplicate, so no need to do the rest of the
3462 : : checks. */
3463 : : return x;
3464 : : }
3465 : :
3466 : 1365593 : if (duplicate_decls (x, b_use->decl))
3467 : : {
3468 : 1365230 : if (b_use != b)
3469 : : {
3470 : : /* Save the updated type in the external scope and
3471 : : restore the proper type for this scope. */
3472 : 1312709 : tree thistype;
3473 : 1312709 : if (comptypes (vistype, type))
3474 : 1312675 : thistype = composite_type (vistype, type);
3475 : : else
3476 : 34 : thistype = TREE_TYPE (b_use->decl);
3477 : 1312709 : b_use->u.type = TREE_TYPE (b_use->decl);
3478 : 1312709 : if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3479 : 1312709 : && fndecl_built_in_p (b_use->decl))
3480 : 276625 : thistype
3481 : 276625 : = c_build_type_attribute_variant (thistype,
3482 : 276625 : TYPE_ATTRIBUTES
3483 : : (b_use->u.type));
3484 : 1312709 : TREE_TYPE (b_use->decl) = thistype;
3485 : : }
3486 : 1365230 : return b_use->decl;
3487 : : }
3488 : : else
3489 : 363 : goto skip_external_and_shadow_checks;
3490 : : }
3491 : :
3492 : : /* All declarations with external linkage, and all external
3493 : : references, go in the external scope, no matter what scope is
3494 : : current. However, the binding in that scope is ignored for
3495 : : purposes of normal name lookup. A separate binding structure is
3496 : : created in the requested scope; this governs the normal
3497 : : visibility of the symbol.
3498 : :
3499 : : The binding in the externals scope is used exclusively for
3500 : : detecting duplicate declarations of the same object, no matter
3501 : : what scope they are in; this is what we do here. (C99 6.2.7p2:
3502 : : All declarations that refer to the same object or function shall
3503 : : have compatible type; otherwise, the behavior is undefined.)
3504 : : However, in Objective-C, we also want to detect declarations
3505 : : conflicting with those of the basic types. */
3506 : 330572703 : if ((DECL_EXTERNAL (x) || scope == file_scope)
3507 : 200491498 : && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3508 : : {
3509 : 49680164 : tree type = TREE_TYPE (x);
3510 : 49680164 : tree vistype = NULL_TREE;
3511 : 49680164 : tree visdecl = NULL_TREE;
3512 : 49680164 : bool type_saved = false;
3513 : 3543918 : if (b && !B_IN_EXTERNAL_SCOPE (b)
3514 : 1024 : && VAR_OR_FUNCTION_DECL_P (b->decl)
3515 : 49681179 : && DECL_FILE_SCOPE_P (b->decl))
3516 : : {
3517 : 789 : visdecl = b->decl;
3518 : 789 : vistype = TREE_TYPE (visdecl);
3519 : : }
3520 : 49680164 : if (scope != file_scope
3521 : 49680164 : && !DECL_IN_SYSTEM_HEADER (x))
3522 : 11460 : warning_at (locus, OPT_Wnested_externs,
3523 : : "nested extern declaration of %qD", x);
3524 : :
3525 : 49681574 : while (b && !B_IN_EXTERNAL_SCOPE (b))
3526 : : {
3527 : : /* If this decl might be modified, save its type. This is
3528 : : done here rather than when the decl is first bound
3529 : : because the type may change after first binding, through
3530 : : being completed or through attributes being added. If we
3531 : : encounter multiple such decls, only the first should have
3532 : : its type saved; the others will already have had their
3533 : : proper types saved and the types will not have changed as
3534 : : their scopes will not have been re-entered. */
3535 : 1410 : if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3536 : : {
3537 : 1008 : b->u.type = TREE_TYPE (b->decl);
3538 : 1008 : type_saved = true;
3539 : : }
3540 : 1410 : if (B_IN_FILE_SCOPE (b)
3541 : 998 : && VAR_P (b->decl)
3542 : 572 : && TREE_STATIC (b->decl)
3543 : 281 : && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3544 : 138 : && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3545 : 47 : && TREE_CODE (type) == ARRAY_TYPE
3546 : 47 : && TYPE_DOMAIN (type)
3547 : 28 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3548 : 1438 : && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3549 : : {
3550 : : /* Array type completed in inner scope, which should be
3551 : : diagnosed if the completion does not have size 1 and
3552 : : it does not get completed in the file scope. */
3553 : 6 : b->inner_comp = true;
3554 : : }
3555 : 1410 : b = b->shadowed;
3556 : : }
3557 : :
3558 : : /* If a matching external declaration has been found, set its
3559 : : type to the composite of all the types of that declaration.
3560 : : After the consistency checks, it will be reset to the
3561 : : composite of the visible types only. */
3562 : 49680164 : if (b && b->u.type)
3563 : 761 : TREE_TYPE (b->decl) = b->u.type;
3564 : :
3565 : : /* the static does not go in the externals scope. */
3566 : 3543774 : if (b && duplicate_decls (x, b->decl))
3567 : : {
3568 : 3543040 : tree thistype;
3569 : 3543040 : if (vistype)
3570 : : {
3571 : 661 : if (comptypes (vistype, type))
3572 : 624 : thistype = composite_type (vistype, type);
3573 : : else
3574 : 37 : thistype = TREE_TYPE (b->decl);
3575 : : }
3576 : : else
3577 : : thistype = type;
3578 : 3543040 : b->u.type = TREE_TYPE (b->decl);
3579 : : /* Propagate the type attributes to the decl. */
3580 : 3543040 : thistype
3581 : 3543040 : = c_build_type_attribute_variant (thistype,
3582 : 3543040 : TYPE_ATTRIBUTES (b->u.type));
3583 : 3543040 : TREE_TYPE (b->decl) = thistype;
3584 : 3543040 : bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3585 : : locus);
3586 : 3543040 : return b->decl;
3587 : : }
3588 : 46137124 : else if (TREE_PUBLIC (x))
3589 : : {
3590 : 45775877 : if (visdecl && !b && duplicate_decls (x, visdecl))
3591 : : {
3592 : : /* An external declaration at block scope referring to a
3593 : : visible entity with internal linkage. The composite
3594 : : type will already be correct for this scope, so we
3595 : : just need to fall through to make the declaration in
3596 : : this scope. */
3597 : : nested = true;
3598 : : x = visdecl;
3599 : : }
3600 : : else
3601 : : {
3602 : 45775760 : bind (name, x, external_scope, /*invisible=*/true,
3603 : : /*nested=*/false, locus);
3604 : 45775760 : nested = true;
3605 : : }
3606 : : }
3607 : : }
3608 : :
3609 : 185695539 : if (TREE_CODE (x) != PARM_DECL)
3610 : 69021519 : warn_if_shadowing (x);
3611 : :
3612 : 116674020 : skip_external_and_shadow_checks:
3613 : 185695902 : if (TREE_CODE (x) == TYPE_DECL)
3614 : : {
3615 : : /* So this is a typedef, set its underlying type. */
3616 : 9710500 : set_underlying_type (x);
3617 : :
3618 : : /* If X is a typedef defined in the current function, record it
3619 : : for the purpose of implementing the -Wunused-local-typedefs
3620 : : warning. */
3621 : 9710500 : record_locally_defined_typedef (x);
3622 : : }
3623 : :
3624 : 185695902 : bind (name, x, scope, /*invisible=*/false, nested, locus);
3625 : :
3626 : : /* If x's type is incomplete because it's based on a
3627 : : structure or union which has not yet been fully declared,
3628 : : attach it to that structure or union type, so we can go
3629 : : back and complete the variable declaration later, if the
3630 : : structure or union gets fully declared.
3631 : :
3632 : : If the input is erroneous, we can have error_mark in the type
3633 : : slot (e.g. "f(void a, ...)") - that doesn't count as an
3634 : : incomplete type. */
3635 : 185695902 : if (TREE_TYPE (x) != error_mark_node
3636 : 185695902 : && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3637 : : {
3638 : 185013 : tree element = TREE_TYPE (x);
3639 : :
3640 : 204959 : while (TREE_CODE (element) == ARRAY_TYPE)
3641 : 19946 : element = TREE_TYPE (element);
3642 : 185013 : element = TYPE_MAIN_VARIANT (element);
3643 : :
3644 : 185013 : if ((RECORD_OR_UNION_TYPE_P (element)
3645 : 141616 : || TREE_CODE (element) == ENUMERAL_TYPE)
3646 : 43468 : && (TREE_CODE (x) != TYPE_DECL
3647 : 40012 : || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3648 : 188477 : && !COMPLETE_TYPE_P (element))
3649 : 321 : C_TYPE_INCOMPLETE_VARS (element)
3650 : 642 : = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3651 : : }
3652 : : return x;
3653 : : }
3654 : :
3655 : :
3656 : : /* Issue a permerror about implicit function declaration. ID is the function
3657 : : identifier, OLDDECL is a declaration of the function in a different scope,
3658 : : or NULL_TREE. */
3659 : :
3660 : : static void
3661 : 3776 : implicit_decl_permerror (location_t loc, tree id, tree olddecl)
3662 : : {
3663 : 3776 : if (!warn_implicit_function_declaration)
3664 : 2617 : return;
3665 : :
3666 : 1159 : bool warned;
3667 : 1159 : auto_diagnostic_group d;
3668 : 1159 : name_hint hint;
3669 : 1159 : if (!olddecl)
3670 : 553 : hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3671 : :
3672 : 1159 : if (flag_isoc99)
3673 : : {
3674 : 1153 : if (const char *suggestion = hint.suggestion ())
3675 : : {
3676 : 107 : gcc_rich_location richloc (loc);
3677 : 107 : richloc.add_fixit_replace (suggestion);
3678 : 107 : warned = permerror_opt (&richloc, OPT_Wimplicit_function_declaration,
3679 : : "implicit declaration of function %qE;"
3680 : : " did you mean %qs?",
3681 : : id, suggestion);
3682 : 107 : }
3683 : : else
3684 : 1046 : warned = permerror_opt (loc, OPT_Wimplicit_function_declaration,
3685 : : "implicit declaration of function %qE", id);
3686 : : }
3687 : 6 : else if (const char *suggestion = hint.suggestion ())
3688 : : {
3689 : 2 : gcc_rich_location richloc (loc);
3690 : 2 : richloc.add_fixit_replace (suggestion);
3691 : 2 : warned = warning_at
3692 : 2 : (&richloc, OPT_Wimplicit_function_declaration,
3693 : : G_("implicit declaration of function %qE; did you mean %qs?"),
3694 : : id, suggestion);
3695 : 2 : }
3696 : : else
3697 : 4 : warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3698 : : G_("implicit declaration of function %qE"), id);
3699 : :
3700 : 1159 : if (warned)
3701 : : {
3702 : : /* Whether the olddecl is an undeclared builtin function.
3703 : : locate_old_decl will not generate a diagnostic for those,
3704 : : so in that case we want to look elsewhere. */
3705 : 90 : bool undeclared_builtin = (olddecl
3706 : 28 : && TREE_CODE (olddecl) == FUNCTION_DECL
3707 : 28 : && fndecl_built_in_p (olddecl)
3708 : 117 : && !C_DECL_DECLARED_BUILTIN (olddecl));
3709 : 90 : if (undeclared_builtin)
3710 : : {
3711 : 27 : const char *header = header_for_builtin_fn (olddecl);
3712 : 27 : if (header)
3713 : : {
3714 : 20 : rich_location richloc (line_table, loc);
3715 : 20 : maybe_add_include_fixit (&richloc, header, true);
3716 : 20 : inform (&richloc,
3717 : : "include %qs or provide a declaration of %qE",
3718 : : header, id);
3719 : 20 : }
3720 : : }
3721 : 63 : else if (olddecl)
3722 : 1 : locate_old_decl (olddecl);
3723 : : }
3724 : :
3725 : 1069 : if (!warned)
3726 : 1159 : hint.suppress ();
3727 : 1159 : }
3728 : :
3729 : : /* Return the name of the header file that declares built-in function
3730 : : FNDECL, or null if either we don't know or don't expect to see an
3731 : : explicit declaration. */
3732 : :
3733 : : static const char *
3734 : 3178 : header_for_builtin_fn (tree fndecl)
3735 : : {
3736 : 3178 : if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3737 : : return NULL;
3738 : :
3739 : 3178 : switch (DECL_FUNCTION_CODE (fndecl))
3740 : : {
3741 : : CASE_FLT_FN (BUILT_IN_ACOS):
3742 : : CASE_FLT_FN (BUILT_IN_ACOSH):
3743 : : CASE_FLT_FN (BUILT_IN_ASIN):
3744 : : CASE_FLT_FN (BUILT_IN_ASINH):
3745 : : CASE_FLT_FN (BUILT_IN_ATAN):
3746 : : CASE_FLT_FN (BUILT_IN_ATANH):
3747 : : CASE_FLT_FN (BUILT_IN_ATAN2):
3748 : : CASE_FLT_FN (BUILT_IN_CBRT):
3749 : : CASE_FLT_FN (BUILT_IN_CEIL):
3750 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3751 : : CASE_FLT_FN (BUILT_IN_COPYSIGN):
3752 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3753 : : CASE_FLT_FN (BUILT_IN_COS):
3754 : : CASE_FLT_FN (BUILT_IN_COSH):
3755 : : CASE_FLT_FN (BUILT_IN_ERF):
3756 : : CASE_FLT_FN (BUILT_IN_ERFC):
3757 : : CASE_FLT_FN (BUILT_IN_EXP):
3758 : : CASE_FLT_FN (BUILT_IN_EXP2):
3759 : : CASE_FLT_FN (BUILT_IN_EXPM1):
3760 : : CASE_FLT_FN (BUILT_IN_FABS):
3761 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3762 : : CASE_FLT_FN (BUILT_IN_FDIM):
3763 : : CASE_FLT_FN (BUILT_IN_FLOOR):
3764 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3765 : : CASE_FLT_FN (BUILT_IN_FMA):
3766 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3767 : : CASE_FLT_FN (BUILT_IN_FMAX):
3768 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3769 : : CASE_FLT_FN (BUILT_IN_FMIN):
3770 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3771 : : CASE_FLT_FN (BUILT_IN_FMOD):
3772 : : CASE_FLT_FN (BUILT_IN_FREXP):
3773 : : CASE_FLT_FN (BUILT_IN_HYPOT):
3774 : : CASE_FLT_FN (BUILT_IN_ILOGB):
3775 : : CASE_FLT_FN (BUILT_IN_LDEXP):
3776 : : CASE_FLT_FN (BUILT_IN_LGAMMA):
3777 : : CASE_FLT_FN (BUILT_IN_LLRINT):
3778 : : CASE_FLT_FN (BUILT_IN_LLROUND):
3779 : : CASE_FLT_FN (BUILT_IN_LOG):
3780 : : CASE_FLT_FN (BUILT_IN_LOG10):
3781 : : CASE_FLT_FN (BUILT_IN_LOG1P):
3782 : : CASE_FLT_FN (BUILT_IN_LOG2):
3783 : : CASE_FLT_FN (BUILT_IN_LOGB):
3784 : : CASE_FLT_FN (BUILT_IN_LRINT):
3785 : : CASE_FLT_FN (BUILT_IN_LROUND):
3786 : : CASE_FLT_FN (BUILT_IN_MODF):
3787 : : CASE_FLT_FN (BUILT_IN_NAN):
3788 : : CASE_FLT_FN (BUILT_IN_NEARBYINT):
3789 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3790 : : CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3791 : : CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3792 : : CASE_FLT_FN (BUILT_IN_POW):
3793 : : CASE_FLT_FN (BUILT_IN_REMAINDER):
3794 : : CASE_FLT_FN (BUILT_IN_REMQUO):
3795 : : CASE_FLT_FN (BUILT_IN_RINT):
3796 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3797 : : CASE_FLT_FN (BUILT_IN_ROUND):
3798 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3799 : : CASE_FLT_FN (BUILT_IN_SCALBLN):
3800 : : CASE_FLT_FN (BUILT_IN_SCALBN):
3801 : : CASE_FLT_FN (BUILT_IN_SIN):
3802 : : CASE_FLT_FN (BUILT_IN_SINH):
3803 : : CASE_FLT_FN (BUILT_IN_SINCOS):
3804 : : CASE_FLT_FN (BUILT_IN_SQRT):
3805 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3806 : : CASE_FLT_FN (BUILT_IN_TAN):
3807 : : CASE_FLT_FN (BUILT_IN_TANH):
3808 : : CASE_FLT_FN (BUILT_IN_TGAMMA):
3809 : : CASE_FLT_FN (BUILT_IN_TRUNC):
3810 : : CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3811 : : case BUILT_IN_ISINF:
3812 : : case BUILT_IN_ISNAN:
3813 : : return "<math.h>";
3814 : 29 : CASE_FLT_FN (BUILT_IN_CABS):
3815 : 29 : CASE_FLT_FN (BUILT_IN_CACOS):
3816 : 29 : CASE_FLT_FN (BUILT_IN_CACOSH):
3817 : 29 : CASE_FLT_FN (BUILT_IN_CARG):
3818 : 29 : CASE_FLT_FN (BUILT_IN_CASIN):
3819 : 29 : CASE_FLT_FN (BUILT_IN_CASINH):
3820 : 29 : CASE_FLT_FN (BUILT_IN_CATAN):
3821 : 29 : CASE_FLT_FN (BUILT_IN_CATANH):
3822 : 29 : CASE_FLT_FN (BUILT_IN_CCOS):
3823 : 29 : CASE_FLT_FN (BUILT_IN_CCOSH):
3824 : 29 : CASE_FLT_FN (BUILT_IN_CEXP):
3825 : 29 : CASE_FLT_FN (BUILT_IN_CIMAG):
3826 : 29 : CASE_FLT_FN (BUILT_IN_CLOG):
3827 : 29 : CASE_FLT_FN (BUILT_IN_CONJ):
3828 : 29 : CASE_FLT_FN (BUILT_IN_CPOW):
3829 : 29 : CASE_FLT_FN (BUILT_IN_CPROJ):
3830 : 29 : CASE_FLT_FN (BUILT_IN_CREAL):
3831 : 29 : CASE_FLT_FN (BUILT_IN_CSIN):
3832 : 29 : CASE_FLT_FN (BUILT_IN_CSINH):
3833 : 29 : CASE_FLT_FN (BUILT_IN_CSQRT):
3834 : 29 : CASE_FLT_FN (BUILT_IN_CTAN):
3835 : 29 : CASE_FLT_FN (BUILT_IN_CTANH):
3836 : 29 : return "<complex.h>";
3837 : 228 : case BUILT_IN_MEMCHR:
3838 : 228 : case BUILT_IN_MEMCMP:
3839 : 228 : case BUILT_IN_MEMCPY:
3840 : 228 : case BUILT_IN_MEMMOVE:
3841 : 228 : case BUILT_IN_MEMSET:
3842 : 228 : case BUILT_IN_STRCAT:
3843 : 228 : case BUILT_IN_STRCHR:
3844 : 228 : case BUILT_IN_STRCMP:
3845 : 228 : case BUILT_IN_STRCPY:
3846 : 228 : case BUILT_IN_STRCSPN:
3847 : 228 : case BUILT_IN_STRLEN:
3848 : 228 : case BUILT_IN_STRNCAT:
3849 : 228 : case BUILT_IN_STRNCMP:
3850 : 228 : case BUILT_IN_STRNCPY:
3851 : 228 : case BUILT_IN_STRPBRK:
3852 : 228 : case BUILT_IN_STRRCHR:
3853 : 228 : case BUILT_IN_STRSPN:
3854 : 228 : case BUILT_IN_STRSTR:
3855 : 228 : return "<string.h>";
3856 : 544 : case BUILT_IN_FPRINTF:
3857 : 544 : case BUILT_IN_PUTC:
3858 : 544 : case BUILT_IN_FPUTC:
3859 : 544 : case BUILT_IN_FPUTS:
3860 : 544 : case BUILT_IN_FSCANF:
3861 : 544 : case BUILT_IN_FWRITE:
3862 : 544 : case BUILT_IN_PRINTF:
3863 : 544 : case BUILT_IN_PUTCHAR:
3864 : 544 : case BUILT_IN_PUTS:
3865 : 544 : case BUILT_IN_SCANF:
3866 : 544 : case BUILT_IN_SNPRINTF:
3867 : 544 : case BUILT_IN_SPRINTF:
3868 : 544 : case BUILT_IN_SSCANF:
3869 : 544 : case BUILT_IN_VFPRINTF:
3870 : 544 : case BUILT_IN_VFSCANF:
3871 : 544 : case BUILT_IN_VPRINTF:
3872 : 544 : case BUILT_IN_VSCANF:
3873 : 544 : case BUILT_IN_VSNPRINTF:
3874 : 544 : case BUILT_IN_VSPRINTF:
3875 : 544 : case BUILT_IN_VSSCANF:
3876 : 544 : return "<stdio.h>";
3877 : 2 : case BUILT_IN_ISALNUM:
3878 : 2 : case BUILT_IN_ISALPHA:
3879 : 2 : case BUILT_IN_ISBLANK:
3880 : 2 : case BUILT_IN_ISCNTRL:
3881 : 2 : case BUILT_IN_ISDIGIT:
3882 : 2 : case BUILT_IN_ISGRAPH:
3883 : 2 : case BUILT_IN_ISLOWER:
3884 : 2 : case BUILT_IN_ISPRINT:
3885 : 2 : case BUILT_IN_ISPUNCT:
3886 : 2 : case BUILT_IN_ISSPACE:
3887 : 2 : case BUILT_IN_ISUPPER:
3888 : 2 : case BUILT_IN_ISXDIGIT:
3889 : 2 : case BUILT_IN_TOLOWER:
3890 : 2 : case BUILT_IN_TOUPPER:
3891 : 2 : return "<ctype.h>";
3892 : 0 : case BUILT_IN_ISWALNUM:
3893 : 0 : case BUILT_IN_ISWALPHA:
3894 : 0 : case BUILT_IN_ISWBLANK:
3895 : 0 : case BUILT_IN_ISWCNTRL:
3896 : 0 : case BUILT_IN_ISWDIGIT:
3897 : 0 : case BUILT_IN_ISWGRAPH:
3898 : 0 : case BUILT_IN_ISWLOWER:
3899 : 0 : case BUILT_IN_ISWPRINT:
3900 : 0 : case BUILT_IN_ISWPUNCT:
3901 : 0 : case BUILT_IN_ISWSPACE:
3902 : 0 : case BUILT_IN_ISWUPPER:
3903 : 0 : case BUILT_IN_ISWXDIGIT:
3904 : 0 : case BUILT_IN_TOWLOWER:
3905 : 0 : case BUILT_IN_TOWUPPER:
3906 : 0 : return "<wctype.h>";
3907 : 1818 : case BUILT_IN_ABORT:
3908 : 1818 : case BUILT_IN_ABS:
3909 : 1818 : case BUILT_IN_CALLOC:
3910 : 1818 : case BUILT_IN_EXIT:
3911 : 1818 : case BUILT_IN_FREE:
3912 : 1818 : case BUILT_IN_LABS:
3913 : 1818 : case BUILT_IN_LLABS:
3914 : 1818 : case BUILT_IN_MALLOC:
3915 : 1818 : case BUILT_IN_REALLOC:
3916 : 1818 : case BUILT_IN__EXIT2:
3917 : 1818 : case BUILT_IN_ALIGNED_ALLOC:
3918 : 1818 : return "<stdlib.h>";
3919 : 1 : case BUILT_IN_IMAXABS:
3920 : 1 : return "<inttypes.h>";
3921 : 3 : case BUILT_IN_STRFTIME:
3922 : 3 : return "<time.h>";
3923 : : default:
3924 : : return NULL;
3925 : : }
3926 : : }
3927 : :
3928 : : /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3929 : : function of type int (). */
3930 : :
3931 : : tree
3932 : 4729 : implicitly_declare (location_t loc, tree functionid)
3933 : : {
3934 : 4729 : struct c_binding *b;
3935 : 4729 : tree decl = NULL_TREE;
3936 : 4729 : tree asmspec_tree;
3937 : :
3938 : 4743 : for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3939 : : {
3940 : 3158 : if (B_IN_SCOPE (b, external_scope))
3941 : : {
3942 : 3144 : decl = b->decl;
3943 : 3144 : break;
3944 : : }
3945 : : }
3946 : :
3947 : 4729 : if (decl)
3948 : : {
3949 : 3144 : if (TREE_CODE (decl) != FUNCTION_DECL)
3950 : : return decl;
3951 : :
3952 : : /* FIXME: Objective-C has weird not-really-builtin functions
3953 : : which are supposed to be visible automatically. They wind up
3954 : : in the external scope because they're pushed before the file
3955 : : scope gets created. Catch this here and rebind them into the
3956 : : file scope. */
3957 : 3137 : if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3958 : : {
3959 : 0 : bind (functionid, decl, file_scope,
3960 : : /*invisible=*/false, /*nested=*/true,
3961 : 0 : DECL_SOURCE_LOCATION (decl));
3962 : 0 : return decl;
3963 : : }
3964 : : else
3965 : : {
3966 : 3137 : tree newtype = default_function_type;
3967 : 3137 : if (b->u.type)
3968 : 753 : TREE_TYPE (decl) = b->u.type;
3969 : : /* Implicit declaration of a function already declared
3970 : : (somehow) in a different scope, or as a built-in.
3971 : : If this is the first time this has happened, warn;
3972 : : then recycle the old declaration but with the new type. */
3973 : 3137 : if (!C_DECL_IMPLICIT (decl))
3974 : : {
3975 : 2191 : implicit_decl_permerror (loc, functionid, decl);
3976 : 2191 : C_DECL_IMPLICIT (decl) = 1;
3977 : : }
3978 : 3137 : if (fndecl_built_in_p (decl))
3979 : : {
3980 : 2705 : newtype = c_build_type_attribute_variant (newtype,
3981 : 2705 : TYPE_ATTRIBUTES
3982 : : (TREE_TYPE (decl)));
3983 : 2705 : if (!comptypes (newtype, TREE_TYPE (decl)))
3984 : : {
3985 : 2528 : auto_diagnostic_group d;
3986 : 2528 : bool warned = warning_at (loc,
3987 : 2528 : OPT_Wbuiltin_declaration_mismatch,
3988 : : "incompatible implicit "
3989 : : "declaration of built-in "
3990 : : "function %qD", decl);
3991 : : /* See if we can hint which header to include. */
3992 : 2528 : const char *header = header_for_builtin_fn (decl);
3993 : 2528 : if (header != NULL && warned)
3994 : : {
3995 : 133 : rich_location richloc (line_table, loc);
3996 : 133 : maybe_add_include_fixit (&richloc, header, true);
3997 : 133 : inform (&richloc,
3998 : : "include %qs or provide a declaration of %qD",
3999 : : header, decl);
4000 : 133 : }
4001 : 2528 : newtype = TREE_TYPE (decl);
4002 : 2528 : }
4003 : : }
4004 : : else
4005 : : {
4006 : 432 : if (!comptypes (newtype, TREE_TYPE (decl)))
4007 : : {
4008 : 2 : auto_diagnostic_group d;
4009 : 2 : error_at (loc, "incompatible implicit declaration of "
4010 : : "function %qD", decl);
4011 : 2 : locate_old_decl (decl);
4012 : 2 : }
4013 : : }
4014 : 3137 : b->u.type = TREE_TYPE (decl);
4015 : 3137 : TREE_TYPE (decl) = newtype;
4016 : 3137 : bind (functionid, decl, current_scope,
4017 : : /*invisible=*/false, /*nested=*/true,
4018 : 3137 : DECL_SOURCE_LOCATION (decl));
4019 : 3137 : return decl;
4020 : : }
4021 : : }
4022 : :
4023 : : /* Not seen before. */
4024 : 1585 : decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
4025 : 1585 : DECL_EXTERNAL (decl) = 1;
4026 : 1585 : TREE_PUBLIC (decl) = 1;
4027 : 1585 : C_DECL_IMPLICIT (decl) = 1;
4028 : 1585 : implicit_decl_permerror (loc, functionid, 0);
4029 : 1585 : asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
4030 : 1585 : if (asmspec_tree)
4031 : 1 : set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
4032 : :
4033 : : /* C89 says implicit declarations are in the innermost block.
4034 : : So we record the decl in the standard fashion. */
4035 : 1585 : decl = pushdecl (decl);
4036 : :
4037 : : /* No need to call objc_check_decl here - it's a function type. */
4038 : 1585 : rest_of_decl_compilation (decl, 0, 0);
4039 : :
4040 : : /* Write a record describing this implicit function declaration
4041 : : to the prototypes file (if requested). */
4042 : 1585 : gen_aux_info_record (decl, 0, 1, 0);
4043 : :
4044 : : /* Possibly apply some default attributes to this implicit declaration. */
4045 : 1585 : decl_attributes (&decl, NULL_TREE, 0);
4046 : :
4047 : 1585 : return decl;
4048 : : }
4049 : :
4050 : : /* Issue an error message for a reference to an undeclared variable
4051 : : ID, including a reference to a builtin outside of function-call
4052 : : context. Establish a binding of the identifier to error_mark_node
4053 : : in an appropriate scope, which will suppress further errors for the
4054 : : same identifier. The error message should be given location LOC. */
4055 : : void
4056 : 1438 : undeclared_variable (location_t loc, tree id)
4057 : : {
4058 : 1438 : static bool already = false;
4059 : 1438 : struct c_scope *scope;
4060 : :
4061 : 1438 : auto_diagnostic_group d;
4062 : 1438 : if (current_function_decl == NULL_TREE)
4063 : : {
4064 : 624 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4065 : 624 : if (const char *suggestion = guessed_id.suggestion ())
4066 : : {
4067 : 86 : gcc_rich_location richloc (loc);
4068 : 86 : richloc.add_fixit_replace (suggestion);
4069 : 86 : error_at (&richloc,
4070 : : "%qE undeclared here (not in a function);"
4071 : : " did you mean %qs?",
4072 : : id, suggestion);
4073 : 86 : }
4074 : : else
4075 : 538 : error_at (loc, "%qE undeclared here (not in a function)", id);
4076 : 624 : scope = current_scope;
4077 : 624 : }
4078 : : else
4079 : : {
4080 : 814 : if (!objc_diagnose_private_ivar (id))
4081 : : {
4082 : 814 : name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
4083 : 814 : if (const char *suggestion = guessed_id.suggestion ())
4084 : : {
4085 : 36 : gcc_rich_location richloc (loc);
4086 : 36 : richloc.add_fixit_replace (suggestion);
4087 : 36 : error_at (&richloc,
4088 : : "%qE undeclared (first use in this function);"
4089 : : " did you mean %qs?",
4090 : : id, suggestion);
4091 : 36 : }
4092 : : else
4093 : 778 : error_at (loc, "%qE undeclared (first use in this function)", id);
4094 : 814 : }
4095 : 814 : if (!already)
4096 : : {
4097 : 197 : inform (loc, "each undeclared identifier is reported only"
4098 : : " once for each function it appears in");
4099 : 197 : already = true;
4100 : : }
4101 : :
4102 : : /* If we are parsing old-style parameter decls, current_function_decl
4103 : : will be nonnull but current_function_scope will be null. */
4104 : 814 : scope = current_function_scope ? current_function_scope : current_scope;
4105 : : }
4106 : 1438 : bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
4107 : : UNKNOWN_LOCATION);
4108 : 1438 : }
4109 : :
4110 : : /* Subroutine of lookup_label, declare_label, define_label: construct a
4111 : : LABEL_DECL with all the proper frills. Also create a struct
4112 : : c_label_vars initialized for the current scope. */
4113 : :
4114 : : static tree
4115 : 23763 : make_label (location_t location, tree name, bool defining,
4116 : : struct c_label_vars **p_label_vars)
4117 : : {
4118 : 23763 : tree label = build_decl (location, LABEL_DECL, name, void_type_node);
4119 : 23763 : DECL_CONTEXT (label) = current_function_decl;
4120 : 23763 : SET_DECL_MODE (label, VOIDmode);
4121 : :
4122 : 23763 : c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
4123 : 23763 : label_vars->shadowed = NULL;
4124 : 23763 : set_spot_bindings (&label_vars->label_bindings, defining);
4125 : 23763 : label_vars->decls_in_scope = make_tree_vector ();
4126 : 23763 : label_vars->gotos = NULL;
4127 : 23763 : *p_label_vars = label_vars;
4128 : :
4129 : 23763 : return label;
4130 : : }
4131 : :
4132 : : /* Get the LABEL_DECL corresponding to identifier NAME as a label.
4133 : : Create one if none exists so far for the current function.
4134 : : This is called when a label is used in a goto expression or
4135 : : has its address taken. */
4136 : :
4137 : : tree
4138 : 85205 : lookup_label (tree name)
4139 : : {
4140 : 85205 : tree label;
4141 : 85205 : struct c_label_vars *label_vars;
4142 : :
4143 : 85205 : if (current_function_scope == 0)
4144 : : {
4145 : 2 : error ("label %qE referenced outside of any function", name);
4146 : 2 : return NULL_TREE;
4147 : : }
4148 : :
4149 : : /* Use a label already defined or ref'd with this name, but not if
4150 : : it is inherited from a containing function and wasn't declared
4151 : : using __label__. */
4152 : 85203 : label = I_LABEL_DECL (name);
4153 : 79354 : if (label && (DECL_CONTEXT (label) == current_function_decl
4154 : 606 : || C_DECLARED_LABEL_FLAG (label)))
4155 : : {
4156 : : /* If the label has only been declared, update its apparent
4157 : : location to point here, for better diagnostics if it
4158 : : turns out not to have been defined. */
4159 : 79347 : if (DECL_INITIAL (label) == NULL_TREE)
4160 : 62657 : DECL_SOURCE_LOCATION (label) = input_location;
4161 : 79347 : return label;
4162 : : }
4163 : :
4164 : : /* No label binding for that identifier; make one. */
4165 : 5856 : label = make_label (input_location, name, false, &label_vars);
4166 : :
4167 : : /* Ordinary labels go in the current function scope. */
4168 : 5856 : bind_label (name, label, current_function_scope, label_vars);
4169 : :
4170 : 5856 : return label;
4171 : : }
4172 : :
4173 : : /* Issue a warning about DECL for a goto statement at GOTO_LOC going
4174 : : to LABEL. */
4175 : :
4176 : : static void
4177 : 1482 : warn_about_goto (location_t goto_loc, tree label, tree decl)
4178 : : {
4179 : 1482 : auto_diagnostic_group d;
4180 : 1482 : if (c_type_variably_modified_p (TREE_TYPE (decl)))
4181 : 1476 : error_at (goto_loc,
4182 : : "jump into scope of identifier with variably modified type");
4183 : 6 : else if (flag_openmp
4184 : 6 : && lookup_attribute ("omp allocate", DECL_ATTRIBUTES (decl)))
4185 : 2 : error_at (goto_loc, "jump skips OpenMP %<allocate%> allocation");
4186 : : else
4187 : 4 : if (!warning_at (goto_loc, OPT_Wjump_misses_init,
4188 : : "jump skips variable initialization"))
4189 : 0 : return;
4190 : 1482 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4191 : 1482 : inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
4192 : 1482 : }
4193 : :
4194 : : /* Look up a label because of a goto statement. This is like
4195 : : lookup_label, but also issues any appropriate warnings. */
4196 : :
4197 : : tree
4198 : 83376 : lookup_label_for_goto (location_t loc, tree name)
4199 : : {
4200 : 83376 : tree label;
4201 : 83376 : struct c_label_vars *label_vars;
4202 : 83376 : unsigned int ix;
4203 : 83376 : tree decl;
4204 : :
4205 : 83376 : label = lookup_label (name);
4206 : 83376 : if (label == NULL_TREE)
4207 : : return NULL_TREE;
4208 : :
4209 : : /* If we are jumping to a different function, we can't issue any
4210 : : useful warnings. */
4211 : 83376 : if (DECL_CONTEXT (label) != current_function_decl)
4212 : : {
4213 : 520 : gcc_assert (C_DECLARED_LABEL_FLAG (label));
4214 : : return label;
4215 : : }
4216 : :
4217 : 82856 : label_vars = I_LABEL_BINDING (name)->u.label;
4218 : :
4219 : : /* If the label has not yet been defined, then push this goto on a
4220 : : list for possible later warnings. */
4221 : 82856 : if (label_vars->label_bindings.scope == NULL)
4222 : : {
4223 : 66391 : c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4224 : :
4225 : 66391 : g->loc = loc;
4226 : 66391 : set_spot_bindings (&g->goto_bindings, true);
4227 : 66391 : vec_safe_push (label_vars->gotos, g);
4228 : 66391 : return label;
4229 : : }
4230 : :
4231 : : /* If there are any decls in label_vars->decls_in_scope, then this
4232 : : goto has missed the declaration of the decl. This happens for a
4233 : : case like
4234 : : int i = 1;
4235 : : lab:
4236 : : ...
4237 : : goto lab;
4238 : : Issue a warning or error. */
4239 : 17240 : FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4240 : 775 : warn_about_goto (loc, label, decl);
4241 : :
4242 : 16465 : if (label_vars->label_bindings.left_stmt_expr)
4243 : : {
4244 : 120 : auto_diagnostic_group d;
4245 : 120 : error_at (loc, "jump into statement expression");
4246 : 120 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4247 : 120 : }
4248 : :
4249 : : return label;
4250 : : }
4251 : :
4252 : : /* Make a label named NAME in the current function, shadowing silently
4253 : : any that may be inherited from containing functions or containing
4254 : : scopes. This is called for __label__ declarations. */
4255 : :
4256 : : tree
4257 : 1183 : declare_label (tree name)
4258 : : {
4259 : 1183 : struct c_binding *b = I_LABEL_BINDING (name);
4260 : 1183 : tree label;
4261 : 1183 : struct c_label_vars *label_vars;
4262 : :
4263 : : /* Check to make sure that the label hasn't already been declared
4264 : : at this scope */
4265 : 1183 : if (b && B_IN_CURRENT_SCOPE (b))
4266 : : {
4267 : 2 : auto_diagnostic_group d;
4268 : 2 : error ("duplicate label declaration %qE", name);
4269 : 2 : locate_old_decl (b->decl);
4270 : :
4271 : : /* Just use the previous declaration. */
4272 : 2 : return b->decl;
4273 : 2 : }
4274 : :
4275 : 1181 : label = make_label (input_location, name, false, &label_vars);
4276 : 1181 : C_DECLARED_LABEL_FLAG (label) = 1;
4277 : :
4278 : : /* Declared labels go in the current scope. */
4279 : 1181 : bind_label (name, label, current_scope, label_vars);
4280 : :
4281 : 1181 : return label;
4282 : : }
4283 : :
4284 : : /* When we define a label, issue any appropriate warnings if there are
4285 : : any gotos earlier in the function which jump to this label. */
4286 : :
4287 : : static void
4288 : 6957 : check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4289 : : {
4290 : 6957 : unsigned int ix;
4291 : 6957 : struct c_goto_bindings *g;
4292 : :
4293 : 73282 : FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4294 : : {
4295 : 66325 : struct c_binding *b;
4296 : 66325 : struct c_scope *scope;
4297 : :
4298 : : /* We have a goto to this label. The goto is going forward. In
4299 : : g->scope, the goto is going to skip any binding which was
4300 : : defined after g->bindings_in_scope. */
4301 : 66325 : if (g->goto_bindings.scope->has_jump_unsafe_decl)
4302 : : {
4303 : 251 : for (b = g->goto_bindings.scope->bindings;
4304 : 581 : b != g->goto_bindings.bindings_in_scope;
4305 : 330 : b = b->prev)
4306 : : {
4307 : 330 : if (decl_jump_unsafe (b->decl))
4308 : 168 : warn_about_goto (g->loc, label, b->decl);
4309 : : }
4310 : : }
4311 : :
4312 : : /* We also need to warn about decls defined in any scopes
4313 : : between the scope of the label and the scope of the goto. */
4314 : 66325 : for (scope = label_vars->label_bindings.scope;
4315 : 68971 : scope != g->goto_bindings.scope;
4316 : 2646 : scope = scope->outer)
4317 : : {
4318 : 2646 : gcc_assert (scope != NULL);
4319 : 2646 : if (scope->has_jump_unsafe_decl)
4320 : : {
4321 : 325 : if (scope == label_vars->label_bindings.scope)
4322 : 249 : b = label_vars->label_bindings.bindings_in_scope;
4323 : : else
4324 : 76 : b = scope->bindings;
4325 : 864 : for (; b != NULL; b = b->prev)
4326 : : {
4327 : 539 : if (decl_jump_unsafe (b->decl))
4328 : 539 : warn_about_goto (g->loc, label, b->decl);
4329 : : }
4330 : : }
4331 : : }
4332 : :
4333 : 66325 : if (g->goto_bindings.stmt_exprs > 0)
4334 : : {
4335 : 100 : auto_diagnostic_group d;
4336 : 100 : error_at (g->loc, "jump into statement expression");
4337 : 100 : inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4338 : : label);
4339 : 100 : }
4340 : : }
4341 : :
4342 : : /* Now that the label is defined, we will issue warnings about
4343 : : subsequent gotos to this label when we see them. */
4344 : 6957 : vec_safe_truncate (label_vars->gotos, 0);
4345 : 6957 : label_vars->gotos = NULL;
4346 : 6957 : }
4347 : :
4348 : : /* Define a label, specifying the location in the source file.
4349 : : Return the LABEL_DECL node for the label, if the definition is valid.
4350 : : Otherwise return NULL_TREE. */
4351 : :
4352 : : tree
4353 : 23712 : define_label (location_t location, tree name)
4354 : : {
4355 : : /* Find any preexisting label with this name. It is an error
4356 : : if that label has already been defined in this function, or
4357 : : if there is a containing function with a declared label with
4358 : : the same name. */
4359 : 23712 : tree label = I_LABEL_DECL (name);
4360 : :
4361 : 6993 : if (label
4362 : 6993 : && ((DECL_CONTEXT (label) == current_function_decl
4363 : 6979 : && DECL_INITIAL (label) != NULL_TREE)
4364 : 6971 : || (DECL_CONTEXT (label) != current_function_decl
4365 : 14 : && C_DECLARED_LABEL_FLAG (label))))
4366 : : {
4367 : 29 : auto_diagnostic_group d;
4368 : 29 : error_at (location, "duplicate label %qD", label);
4369 : 29 : locate_old_decl (label);
4370 : 29 : return NULL_TREE;
4371 : 29 : }
4372 : 23683 : else if (label && DECL_CONTEXT (label) == current_function_decl)
4373 : : {
4374 : 6957 : struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4375 : :
4376 : : /* The label has been used or declared already in this function,
4377 : : but not defined. Update its location to point to this
4378 : : definition. */
4379 : 6957 : DECL_SOURCE_LOCATION (label) = location;
4380 : 6957 : set_spot_bindings (&label_vars->label_bindings, true);
4381 : :
4382 : : /* Issue warnings as required about any goto statements from
4383 : : earlier in the function. */
4384 : 6957 : check_earlier_gotos (label, label_vars);
4385 : : }
4386 : : else
4387 : : {
4388 : 16726 : struct c_label_vars *label_vars;
4389 : :
4390 : : /* No label binding for that identifier; make one. */
4391 : 16726 : label = make_label (location, name, true, &label_vars);
4392 : :
4393 : : /* Ordinary labels go in the current function scope. */
4394 : 16726 : bind_label (name, label, current_function_scope, label_vars);
4395 : : }
4396 : :
4397 : 23683 : if (!in_system_header_at (input_location) && lookup_name (name))
4398 : 131 : warning_at (location, OPT_Wtraditional,
4399 : : "traditional C lacks a separate namespace "
4400 : : "for labels, identifier %qE conflicts", name);
4401 : :
4402 : : /* Mark label as having been defined. */
4403 : 23683 : DECL_INITIAL (label) = error_mark_node;
4404 : 23683 : return label;
4405 : : }
4406 : :
4407 : : /* Get the bindings for a new switch statement. This is used to issue
4408 : : warnings as appropriate for jumps from the switch to case or
4409 : : default labels. */
4410 : :
4411 : : struct c_spot_bindings *
4412 : 36617 : c_get_switch_bindings (void)
4413 : : {
4414 : 36617 : struct c_spot_bindings *switch_bindings;
4415 : :
4416 : 36617 : switch_bindings = XNEW (struct c_spot_bindings);
4417 : 36617 : set_spot_bindings (switch_bindings, true);
4418 : 36617 : return switch_bindings;
4419 : : }
4420 : :
4421 : : void
4422 : 36617 : c_release_switch_bindings (struct c_spot_bindings *bindings)
4423 : : {
4424 : 36617 : gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4425 : 36617 : XDELETE (bindings);
4426 : 36617 : }
4427 : :
4428 : : /* This is called at the point of a case or default label to issue
4429 : : warnings about decls as needed. It returns true if it found an
4430 : : error, not just a warning. */
4431 : :
4432 : : bool
4433 : 1024578 : c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4434 : : location_t switch_loc, location_t case_loc)
4435 : : {
4436 : 1024578 : bool saw_error;
4437 : 1024578 : struct c_scope *scope;
4438 : :
4439 : 1024578 : saw_error = false;
4440 : 1024578 : for (scope = current_scope;
4441 : 3073765 : scope != switch_bindings->scope;
4442 : 2049187 : scope = scope->outer)
4443 : : {
4444 : 2049187 : struct c_binding *b;
4445 : :
4446 : 2049187 : gcc_assert (scope != NULL);
4447 : :
4448 : 2049187 : if (!scope->has_jump_unsafe_decl)
4449 : 2049176 : continue;
4450 : :
4451 : 22 : for (b = scope->bindings; b != NULL; b = b->prev)
4452 : : {
4453 : 11 : if (decl_jump_unsafe (b->decl))
4454 : : {
4455 : 11 : auto_diagnostic_group d;
4456 : 11 : bool emitted;
4457 : 11 : if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4458 : : {
4459 : 6 : saw_error = true;
4460 : 6 : error_at (case_loc,
4461 : : "switch jumps into scope of identifier with "
4462 : : "variably modified type");
4463 : 6 : emitted = true;
4464 : : }
4465 : 5 : else if (flag_openmp
4466 : 7 : && lookup_attribute ("omp allocate",
4467 : 2 : DECL_ATTRIBUTES (b->decl)))
4468 : : {
4469 : 2 : saw_error = true;
4470 : 2 : error_at (case_loc,
4471 : : "switch jumps over OpenMP %<allocate%> allocation");
4472 : 2 : emitted = true;
4473 : : }
4474 : : else
4475 : 3 : emitted
4476 : 3 : = warning_at (case_loc, OPT_Wjump_misses_init,
4477 : : "switch jumps over variable initialization");
4478 : 11 : if (emitted)
4479 : : {
4480 : 11 : inform (switch_loc, "switch starts here");
4481 : 11 : inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4482 : : b->decl);
4483 : : }
4484 : 11 : }
4485 : : }
4486 : : }
4487 : :
4488 : 1024578 : if (switch_bindings->stmt_exprs > 0)
4489 : : {
4490 : 4 : saw_error = true;
4491 : 4 : auto_diagnostic_group d;
4492 : 4 : error_at (case_loc, "switch jumps into statement expression");
4493 : 4 : inform (switch_loc, "switch starts here");
4494 : 4 : }
4495 : :
4496 : 1024578 : return saw_error;
4497 : : }
4498 : :
4499 : : /* Given NAME, an IDENTIFIER_NODE,
4500 : : return the structure (or union or enum) definition for that name.
4501 : : If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4502 : : CODE says which kind of type the caller wants;
4503 : : it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4504 : : If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4505 : : location where the tag was defined.
4506 : : If the wrong kind of type is found, an error is reported. */
4507 : :
4508 : : static tree
4509 : 2052850 : lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4510 : : location_t *ploc)
4511 : : {
4512 : 2052850 : struct c_binding *b = I_TAG_BINDING (name);
4513 : 2052850 : bool thislevel = false;
4514 : :
4515 : 2052850 : if (!b || !b->decl)
4516 : : return NULL_TREE;
4517 : :
4518 : : /* We only care about whether it's in this level if
4519 : : thislevel_only was set or it might be a type clash. */
4520 : 1436801 : if (thislevel_only || TREE_CODE (b->decl) != code)
4521 : : {
4522 : : /* For our purposes, a tag in the external scope is the same as
4523 : : a tag in the file scope. (Primarily relevant to Objective-C
4524 : : and its builtin structure tags, which get pushed before the
4525 : : file scope is created.) */
4526 : 460723 : if (B_IN_CURRENT_SCOPE (b)
4527 : 179 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4528 : 1436801 : thislevel = true;
4529 : : }
4530 : :
4531 : 1436801 : if (thislevel_only && !thislevel)
4532 : : return NULL_TREE;
4533 : :
4534 : 1436643 : if (TREE_CODE (b->decl) != code)
4535 : : {
4536 : : /* Definition isn't the kind we were looking for. */
4537 : 39 : pending_invalid_xref = name;
4538 : 39 : pending_invalid_xref_location = input_location;
4539 : :
4540 : : /* If in the same binding level as a declaration as a tag
4541 : : of a different type, this must not be allowed to
4542 : : shadow that tag, so give the error immediately.
4543 : : (For example, "struct foo; union foo;" is invalid.) */
4544 : 39 : if (thislevel)
4545 : 18 : pending_xref_error ();
4546 : : }
4547 : :
4548 : 1436643 : if (ploc != NULL)
4549 : 1001186 : *ploc = b->locus;
4550 : :
4551 : 1436643 : return b->decl;
4552 : : }
4553 : :
4554 : : /* Return true if a definition exists for NAME with code CODE. */
4555 : :
4556 : : bool
4557 : 384 : tag_exists_p (enum tree_code code, tree name)
4558 : : {
4559 : 384 : struct c_binding *b = I_TAG_BINDING (name);
4560 : :
4561 : 384 : if (b == NULL || b->decl == NULL_TREE)
4562 : : return false;
4563 : 18 : return TREE_CODE (b->decl) == code;
4564 : : }
4565 : :
4566 : : /* Print an error message now
4567 : : for a recent invalid struct, union or enum cross reference.
4568 : : We don't print them immediately because they are not invalid
4569 : : when used in the `struct foo;' construct for shadowing. */
4570 : :
4571 : : void
4572 : 306092638 : pending_xref_error (void)
4573 : : {
4574 : 306092638 : if (pending_invalid_xref != NULL_TREE)
4575 : 27 : error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4576 : : pending_invalid_xref);
4577 : 306092638 : pending_invalid_xref = NULL_TREE;
4578 : 306092638 : }
4579 : :
4580 : :
4581 : : /* Look up NAME in the current scope and its superiors
4582 : : in the namespace of variables, functions and typedefs.
4583 : : Return a ..._DECL node of some kind representing its definition,
4584 : : or return NULL_TREE if it is undefined. */
4585 : :
4586 : : tree
4587 : 1236328769 : lookup_name (tree name)
4588 : : {
4589 : 1236328769 : struct c_binding *b = I_SYMBOL_BINDING (name);
4590 : : /* Do not resolve non-default function versions. */
4591 : 1236328769 : if (b
4592 : 835119757 : && TREE_CODE (b->decl) == FUNCTION_DECL
4593 : 114419151 : && DECL_FUNCTION_VERSIONED (b->decl)
4594 : 1236328769 : && !is_function_default_version (b->decl))
4595 : : return NULL_TREE;
4596 : 1236328769 : if (b && !b->invisible)
4597 : : {
4598 : 824468378 : maybe_record_typedef_use (b->decl);
4599 : 824468378 : return b->decl;
4600 : : }
4601 : : return NULL_TREE;
4602 : : }
4603 : :
4604 : : /* Similar to `lookup_name' but look only at the indicated scope. */
4605 : :
4606 : : static tree
4607 : 128347061 : lookup_name_in_scope (tree name, struct c_scope *scope)
4608 : : {
4609 : 128347061 : struct c_binding *b;
4610 : :
4611 : 128350288 : for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4612 : 7313534 : if (B_IN_SCOPE (b, scope))
4613 : 7310307 : return b->decl;
4614 : : return NULL_TREE;
4615 : : }
4616 : :
4617 : : /* Look for the closest match for NAME within the currently valid
4618 : : scopes.
4619 : :
4620 : : This finds the identifier with the lowest Levenshtein distance to
4621 : : NAME. If there are multiple candidates with equal minimal distance,
4622 : : the first one found is returned. Scopes are searched from innermost
4623 : : outwards, and within a scope in reverse order of declaration, thus
4624 : : benefiting candidates "near" to the current scope.
4625 : :
4626 : : The function also looks for similar macro names to NAME, since a
4627 : : misspelled macro name will not be expanded, and hence looks like an
4628 : : identifier to the C frontend.
4629 : :
4630 : : It also looks for start_typename keywords, to detect "singed" vs "signed"
4631 : : typos.
4632 : :
4633 : : Use LOC for any deferred diagnostics. */
4634 : :
4635 : : name_hint
4636 : 2243 : lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4637 : : {
4638 : 2243 : gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4639 : :
4640 : : /* First, try some well-known names in the C standard library, in case
4641 : : the user forgot a #include. */
4642 : 2243 : const char *header_hint
4643 : 2243 : = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4644 : :
4645 : 2243 : if (header_hint)
4646 : 56 : return name_hint
4647 : : (nullptr,
4648 : 56 : std::make_unique<suggest_missing_header> (loc,
4649 : 112 : IDENTIFIER_POINTER (name),
4650 : 56 : header_hint));
4651 : :
4652 : : /* Next, look for exact matches for builtin defines that would have been
4653 : : defined if the user had passed a command-line option (e.g. -fopenmp
4654 : : for "_OPENMP"). */
4655 : 2187 : diagnostics::option_id option_id
4656 : 2187 : = get_option_for_builtin_define (IDENTIFIER_POINTER (name));
4657 : 2187 : if (option_id.m_idx > 0)
4658 : 2 : return name_hint
4659 : : (nullptr,
4660 : 2 : std::make_unique<suggest_missing_option> (loc,
4661 : 4 : IDENTIFIER_POINTER (name),
4662 : 2 : option_id));
4663 : :
4664 : : /* Only suggest names reserved for the implementation if NAME begins
4665 : : with an underscore. */
4666 : 2185 : bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4667 : :
4668 : 2185 : best_match<tree, tree> bm (name);
4669 : :
4670 : : /* Look within currently valid scopes. */
4671 : 9648 : for (c_scope *scope = current_scope; scope; scope = scope->outer)
4672 : 12243791 : for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4673 : : {
4674 : 12236328 : if (!binding->id || binding->invisible)
4675 : 6945729 : continue;
4676 : 5290599 : if (binding->decl == error_mark_node)
4677 : 358 : continue;
4678 : : /* Don't use bindings from implicitly declared functions,
4679 : : as they were likely misspellings themselves. */
4680 : 5290241 : if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4681 : 4877505 : if (C_DECL_IMPLICIT (binding->decl))
4682 : 229 : continue;
4683 : : /* Don't suggest names that are reserved for use by the
4684 : : implementation, unless NAME began with an underscore. */
4685 : 5290012 : if (!consider_implementation_names)
4686 : : {
4687 : 2326036 : const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4688 : 2326036 : if (name_reserved_for_implementation_p (suggestion_str))
4689 : 2268161 : continue;
4690 : : }
4691 : 3021851 : switch (kind)
4692 : : {
4693 : 24310 : case FUZZY_LOOKUP_TYPENAME:
4694 : 24310 : if (TREE_CODE (binding->decl) != TYPE_DECL)
4695 : 17005 : continue;
4696 : : break;
4697 : :
4698 : 128872 : case FUZZY_LOOKUP_FUNCTION_NAME:
4699 : 128872 : if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4700 : : {
4701 : : /* Allow function pointers. */
4702 : 26502 : if ((VAR_P (binding->decl)
4703 : 21977 : || TREE_CODE (binding->decl) == PARM_DECL)
4704 : 5400 : && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4705 : 27782 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4706 : : == FUNCTION_TYPE))
4707 : : break;
4708 : 26483 : continue;
4709 : : }
4710 : : break;
4711 : :
4712 : : default:
4713 : : break;
4714 : : }
4715 : 2978363 : bm.consider (binding->id);
4716 : : }
4717 : :
4718 : : /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4719 : : as:
4720 : : x = SOME_OTHER_MACRO (y);
4721 : : then "SOME_OTHER_MACRO" will survive to the frontend and show up
4722 : : as a misspelled identifier.
4723 : :
4724 : : Use the best distance so far so that a candidate is only set if
4725 : : a macro is better than anything so far. This allows early rejection
4726 : : (without calculating the edit distance) of macro names that must have
4727 : : distance >= bm.get_best_distance (), and means that we only get a
4728 : : non-NULL result for best_macro_match if it's better than any of
4729 : : the identifiers already checked, which avoids needless creation
4730 : : of identifiers for macro hashnodes. */
4731 : 2185 : best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4732 : 2185 : cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4733 : : /* If a macro is the closest so far to NAME, use it, creating an
4734 : : identifier tree node for it. */
4735 : 2185 : if (best_macro)
4736 : : {
4737 : 11 : const char *id = (const char *)best_macro->ident.str;
4738 : 11 : tree macro_as_identifier
4739 : 11 : = get_identifier_with_length (id, best_macro->ident.len);
4740 : 11 : bm.set_best_so_far (macro_as_identifier,
4741 : : bmm.get_best_distance (),
4742 : : bmm.get_best_candidate_length ());
4743 : : }
4744 : :
4745 : : /* Try the "start_typename" keywords to detect
4746 : : "singed" vs "signed" typos. */
4747 : 2185 : if (kind == FUZZY_LOOKUP_TYPENAME)
4748 : : {
4749 : 54731 : for (unsigned i = 0; i < num_c_common_reswords; i++)
4750 : : {
4751 : 54492 : const c_common_resword *resword = &c_common_reswords[i];
4752 : 54492 : if (!c_keyword_starts_typename (resword->rid))
4753 : 41347 : continue;
4754 : 13145 : tree resword_identifier = ridpointers [resword->rid];
4755 : 13145 : if (!resword_identifier)
4756 : 30 : continue;
4757 : 13115 : gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4758 : 13115 : bm.consider (resword_identifier);
4759 : : }
4760 : : }
4761 : :
4762 : 2185 : tree best = bm.get_best_meaningful_candidate ();
4763 : 2185 : if (best)
4764 : 275 : return name_hint (IDENTIFIER_POINTER (best), NULL);
4765 : : else
4766 : 1910 : return name_hint (NULL, NULL);
4767 : : }
4768 : :
4769 : :
4770 : : /* Handle the standard [[nodiscard]] attribute. */
4771 : :
4772 : : static tree
4773 : 34 : handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4774 : : int /*flags*/, bool *no_add_attrs)
4775 : : {
4776 : 34 : if (TREE_CODE (*node) == FUNCTION_DECL)
4777 : : {
4778 : 18 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4779 : 1 : warning_at (DECL_SOURCE_LOCATION (*node),
4780 : 1 : OPT_Wattributes, "%qE attribute applied to %qD with void "
4781 : : "return type", name, *node);
4782 : : }
4783 : 16 : else if (RECORD_OR_UNION_TYPE_P (*node)
4784 : 12 : || TREE_CODE (*node) == ENUMERAL_TYPE)
4785 : : /* OK */;
4786 : : else
4787 : : {
4788 : 10 : pedwarn (input_location,
4789 : 10 : OPT_Wattributes, "%qE attribute can only be applied to "
4790 : : "functions or to structure, union or enumeration types", name);
4791 : 10 : *no_add_attrs = true;
4792 : : }
4793 : 34 : return NULL_TREE;
4794 : : }
4795 : :
4796 : : /* Handle the standard [[noreturn]] attribute. */
4797 : :
4798 : : static tree
4799 : 44 : handle_std_noreturn_attribute (tree *node, tree name, tree args,
4800 : : int flags, bool *no_add_attrs)
4801 : : {
4802 : : /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4803 : : only applies to functions, not function pointers. */
4804 : 44 : if (TREE_CODE (*node) == FUNCTION_DECL)
4805 : 22 : return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4806 : : else
4807 : : {
4808 : 22 : pedwarn (input_location, OPT_Wattributes,
4809 : : "standard %qE attribute can only be applied to functions",
4810 : : name);
4811 : 22 : *no_add_attrs = true;
4812 : 22 : return NULL_TREE;
4813 : : }
4814 : : }
4815 : :
4816 : : /* Handle the standard [[unsequenced]] attribute. */
4817 : :
4818 : : static tree
4819 : 68 : handle_std_unsequenced_attribute (tree *node, tree name, tree args,
4820 : : int flags, bool *no_add_attrs)
4821 : : {
4822 : : /* Unlike GNU __attribute__ ((unsequenced)), the standard [[unsequenced]]
4823 : : should be only applied to function declarators or type specifiers which
4824 : : have function type. */
4825 : 68 : if (node[2])
4826 : : {
4827 : 8 : auto_diagnostic_group d;
4828 : 8 : if (pedwarn (input_location, OPT_Wattributes,
4829 : : "standard %qE attribute can only be applied to function "
4830 : : "declarators or type specifiers with function type", name))
4831 : 8 : inform (input_location, "did you mean to specify it after %<)%> "
4832 : : "following function parameters?");
4833 : 8 : *no_add_attrs = true;
4834 : 8 : return NULL_TREE;
4835 : 8 : }
4836 : 60 : return handle_unsequenced_attribute (node, name, args, flags, no_add_attrs);
4837 : : }
4838 : :
4839 : : /* Handle the standard [[reproducible]] attribute. */
4840 : :
4841 : : static tree
4842 : 34 : handle_std_reproducible_attribute (tree *node, tree name, tree args,
4843 : : int flags, bool *no_add_attrs)
4844 : : {
4845 : 34 : return handle_std_unsequenced_attribute (node, name, args, flags,
4846 : 34 : no_add_attrs);
4847 : : }
4848 : :
4849 : : /* Table of supported standard (C23) attributes. */
4850 : : static const attribute_spec std_attributes[] =
4851 : : {
4852 : : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4853 : : affects_type_identity, handler, exclude } */
4854 : : { "_Noreturn", 0, 0, false, false, false, false,
4855 : : handle_std_noreturn_attribute, NULL },
4856 : : { "deprecated", 0, 1, false, false, false, false,
4857 : : handle_deprecated_attribute, NULL },
4858 : : { "fallthrough", 0, 0, false, false, false, false,
4859 : : handle_fallthrough_attribute, NULL },
4860 : : { "maybe_unused", 0, 0, false, false, false, false,
4861 : : handle_unused_attribute, NULL },
4862 : : { "nodiscard", 0, 1, false, false, false, false,
4863 : : handle_nodiscard_attribute, NULL },
4864 : : { "noreturn", 0, 0, false, false, false, false,
4865 : : handle_std_noreturn_attribute, NULL },
4866 : : { "reproducible", 0, 0, false, true, true, false,
4867 : : handle_std_reproducible_attribute, NULL },
4868 : : { "unsequenced", 0, 0, false, true, true, false,
4869 : : handle_std_unsequenced_attribute, NULL }
4870 : : };
4871 : :
4872 : : const scoped_attribute_specs std_attribute_table =
4873 : : {
4874 : : nullptr, { std_attributes }
4875 : : };
4876 : :
4877 : : /* Create the predefined scalar types of C,
4878 : : and some nodes representing standard constants (0, 1, (void *) 0).
4879 : : Initialize the global scope.
4880 : : Make definitions for built-in primitive functions. */
4881 : :
4882 : : void
4883 : 113067 : c_init_decl_processing (void)
4884 : : {
4885 : 113067 : location_t save_loc = input_location;
4886 : :
4887 : : /* Initialize reserved words for parser. */
4888 : 113067 : c_parse_init ();
4889 : :
4890 : 113067 : current_function_decl = NULL_TREE;
4891 : :
4892 : 113067 : gcc_obstack_init (&parser_obstack);
4893 : :
4894 : : /* Make the externals scope. */
4895 : 113067 : push_scope ();
4896 : 113067 : external_scope = current_scope;
4897 : :
4898 : : /* Declarations from c_common_nodes_and_builtins must not be associated
4899 : : with this input file, lest we get differences between using and not
4900 : : using preprocessed headers. */
4901 : 113067 : input_location = BUILTINS_LOCATION;
4902 : :
4903 : 113067 : c_common_nodes_and_builtins ();
4904 : :
4905 : : /* In C, comparisons and TRUTH_* expressions have type int. */
4906 : 113067 : truthvalue_type_node = integer_type_node;
4907 : 113067 : truthvalue_true_node = integer_one_node;
4908 : 113067 : truthvalue_false_node = integer_zero_node;
4909 : :
4910 : : /* Even in C99, which has a real boolean type. */
4911 : 113067 : pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4912 : : boolean_type_node));
4913 : :
4914 : : /* C-specific nullptr initialization. */
4915 : 113067 : record_builtin_type (RID_MAX, "typeof (nullptr)", nullptr_type_node);
4916 : : /* The size and alignment of nullptr_t is the same as for a pointer to
4917 : : character type. */
4918 : 113067 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4919 : :
4920 : 113067 : input_location = save_loc;
4921 : :
4922 : 113067 : make_fname_decl = c_make_fname_decl;
4923 : 113067 : start_fname_decls ();
4924 : :
4925 : 113067 : if (warn_keyword_macro)
4926 : : {
4927 : 1145 : for (unsigned int i = 0; i < num_c_common_reswords; ++i)
4928 : : /* For C register keywords which don't start with underscore
4929 : : or start with just single underscore. Don't complain about
4930 : : ObjC or Transactional Memory keywords. */
4931 : 1140 : if (c_common_reswords[i].word[0] == '_'
4932 : 515 : && c_common_reswords[i].word[1] == '_')
4933 : 385 : continue;
4934 : 980 : else if (c_common_reswords[i].disable
4935 : 755 : & (D_TRANSMEM | D_OBJC | D_CXX_OBJC))
4936 : 225 : continue;
4937 : : else
4938 : : {
4939 : 530 : tree id = get_identifier (c_common_reswords[i].word);
4940 : 530 : if (C_IS_RESERVED_WORD (id)
4941 : 530 : && C_RID_CODE (id) != RID_CXX_COMPAT_WARN)
4942 : 322 : cpp_warn (parse_in, IDENTIFIER_POINTER (id),
4943 : 322 : IDENTIFIER_LENGTH (id));
4944 : : }
4945 : : }
4946 : 113067 : }
4947 : :
4948 : : /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4949 : : give the decl, NAME is the initialization string and TYPE_DEP
4950 : : indicates whether NAME depended on the type of the function. As we
4951 : : don't yet implement delayed emission of static data, we mark the
4952 : : decl as emitted so it is not placed in the output. Anything using
4953 : : it must therefore pull out the STRING_CST initializer directly.
4954 : : FIXME. */
4955 : :
4956 : : static tree
4957 : 2865 : c_make_fname_decl (location_t loc, tree id, int type_dep)
4958 : : {
4959 : 2865 : const char *name = fname_as_string (type_dep);
4960 : 2865 : tree decl, type, init;
4961 : 2865 : size_t length = strlen (name);
4962 : :
4963 : 2865 : type = c_build_array_type (char_type_node,
4964 : 2865 : build_index_type (size_int (length)));
4965 : 2865 : type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4966 : :
4967 : 2865 : decl = build_decl (loc, VAR_DECL, id, type);
4968 : :
4969 : 2865 : TREE_STATIC (decl) = 1;
4970 : 2865 : TREE_READONLY (decl) = 1;
4971 : 2865 : DECL_ARTIFICIAL (decl) = 1;
4972 : :
4973 : 2865 : init = build_string (length + 1, name);
4974 : 2865 : free (CONST_CAST (char *, name));
4975 : 2865 : TREE_TYPE (init) = type;
4976 : 2865 : DECL_INITIAL (decl) = init;
4977 : :
4978 : 2865 : TREE_USED (decl) = 1;
4979 : :
4980 : 2865 : if (current_function_decl
4981 : : /* For invalid programs like this:
4982 : :
4983 : : void foo()
4984 : : const char* p = __FUNCTION__;
4985 : :
4986 : : the __FUNCTION__ is believed to appear in K&R style function
4987 : : parameter declarator. In that case we still don't have
4988 : : function_scope. */
4989 : 2858 : && current_function_scope)
4990 : : {
4991 : 2850 : DECL_CONTEXT (decl) = current_function_decl;
4992 : 2850 : bind (id, decl, current_function_scope,
4993 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4994 : : }
4995 : :
4996 : 2865 : finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4997 : :
4998 : 2865 : return decl;
4999 : : }
5000 : :
5001 : : tree
5002 : 329985816 : c_builtin_function (tree decl)
5003 : : {
5004 : 329985816 : tree type = TREE_TYPE (decl);
5005 : 329985816 : tree id = DECL_NAME (decl);
5006 : :
5007 : 329985816 : const char *name = IDENTIFIER_POINTER (id);
5008 : 329985816 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5009 : :
5010 : : /* Should never be called on a symbol with a preexisting meaning. */
5011 : 329985816 : gcc_assert (!I_SYMBOL_BINDING (id));
5012 : :
5013 : 329985816 : bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
5014 : : UNKNOWN_LOCATION);
5015 : :
5016 : : /* Builtins in the implementation namespace are made visible without
5017 : : needing to be explicitly declared. See push_file_scope. */
5018 : 329985816 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5019 : : {
5020 : 224949018 : DECL_CHAIN (decl) = visible_builtins;
5021 : 224949018 : visible_builtins = decl;
5022 : : }
5023 : :
5024 : 329985816 : return decl;
5025 : : }
5026 : :
5027 : : tree
5028 : 10387365 : c_builtin_function_ext_scope (tree decl)
5029 : : {
5030 : 10387365 : tree type = TREE_TYPE (decl);
5031 : 10387365 : tree id = DECL_NAME (decl);
5032 : :
5033 : 10387365 : const char *name = IDENTIFIER_POINTER (id);
5034 : 10387365 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5035 : :
5036 : 10387365 : if (external_scope)
5037 : 10202270 : bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
5038 : : UNKNOWN_LOCATION);
5039 : :
5040 : : /* Builtins in the implementation namespace are made visible without
5041 : : needing to be explicitly declared. See push_file_scope. */
5042 : 10387365 : if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
5043 : : {
5044 : 10387365 : DECL_CHAIN (decl) = visible_builtins;
5045 : 10387365 : visible_builtins = decl;
5046 : : }
5047 : :
5048 : 10387365 : return decl;
5049 : : }
5050 : :
5051 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5052 : :
5053 : : tree
5054 : 0 : c_simulate_builtin_function_decl (tree decl)
5055 : : {
5056 : 0 : tree type = TREE_TYPE (decl);
5057 : 0 : C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
5058 : 0 : return pushdecl (decl);
5059 : : }
5060 : :
5061 : : /* Warn about attributes in a context where they are unused
5062 : : (attribute-declarations, except for the "fallthrough" case, and
5063 : : attributes on statements). */
5064 : :
5065 : : void
5066 : 40570142 : c_warn_unused_attributes (tree attrs)
5067 : : {
5068 : 40570187 : for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
5069 : 45 : if (get_attribute_namespace (t) == NULL_TREE)
5070 : : /* The specifications of standard attributes mean this is a
5071 : : constraint violation. */
5072 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5073 : : get_attribute_name (t));
5074 : 16 : else if (!attribute_ignored_p (t))
5075 : 12 : warning (OPT_Wattributes, "%qE attribute ignored",
5076 : : get_attribute_name (t));
5077 : 40570142 : }
5078 : :
5079 : : /* Warn for standard attributes being applied to a type that is not
5080 : : being defined, where that is a constraint violation, and return a
5081 : : list of attributes with them removed. */
5082 : :
5083 : : tree
5084 : 424956486 : c_warn_type_attributes (tree type, tree attrs)
5085 : : {
5086 : 424956486 : tree *attr_ptr = &attrs;
5087 : 424979133 : while (*attr_ptr)
5088 : 22647 : if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
5089 : : {
5090 : 89 : if (TREE_CODE (type) == FUNCTION_TYPE)
5091 : : {
5092 : 65 : tree name = get_attribute_name (*attr_ptr);
5093 : : /* [[unsequenced]] and [[reproducible]] is fine on function
5094 : : types that aren't being defined. */
5095 : 65 : if (is_attribute_p ("unsequenced", name)
5096 : 65 : || is_attribute_p ("reproducible", name))
5097 : : {
5098 : 60 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5099 : 60 : continue;
5100 : : }
5101 : : }
5102 : 29 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
5103 : : get_attribute_name (*attr_ptr));
5104 : 29 : *attr_ptr = TREE_CHAIN (*attr_ptr);
5105 : : }
5106 : : else
5107 : 22558 : attr_ptr = &TREE_CHAIN (*attr_ptr);
5108 : 424956486 : return attrs;
5109 : : }
5110 : :
5111 : : /* Called when a declaration is seen that contains no names to declare.
5112 : : If its type is a reference to a structure, union or enum inherited
5113 : : from a containing scope, shadow that tag name for the current scope
5114 : : with a forward reference.
5115 : : If its type defines a new named structure or union
5116 : : or defines an enum, it is valid but we need not do anything here.
5117 : : Otherwise, it is an error. */
5118 : :
5119 : : void
5120 : 493809 : shadow_tag (const struct c_declspecs *declspecs)
5121 : : {
5122 : 493809 : shadow_tag_warned (declspecs, 0);
5123 : 493809 : }
5124 : :
5125 : : /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
5126 : : but no pedwarn. */
5127 : : void
5128 : 493879 : shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
5129 : : {
5130 : 493879 : bool found_tag = false;
5131 : :
5132 : 493879 : if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
5133 : : {
5134 : 493765 : tree value = declspecs->type;
5135 : 493765 : enum tree_code code = TREE_CODE (value);
5136 : :
5137 : 493765 : if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
5138 : : /* Used to test also that TYPE_SIZE (value) != 0.
5139 : : That caused warning for `struct foo;' at top level in the file. */
5140 : : {
5141 : 493714 : tree name = TYPE_NAME (value);
5142 : 493714 : tree t;
5143 : :
5144 : 493714 : found_tag = true;
5145 : :
5146 : 493714 : if (declspecs->restrict_p)
5147 : : {
5148 : 2 : error ("invalid use of %<restrict%>");
5149 : 2 : warned = 1;
5150 : : }
5151 : :
5152 : 493714 : if (in_underspecified_init)
5153 : : {
5154 : : /* This can only occur with extensions such as statement
5155 : : expressions, but is still appropriate as an error to
5156 : : avoid types declared in such a context escaping to
5157 : : the type of an auto variable. */
5158 : 1 : error ("%qT declared in underspecified object initializer",
5159 : : value);
5160 : 1 : warned = 1;
5161 : : }
5162 : :
5163 : 493714 : if (name == NULL_TREE)
5164 : : {
5165 : 58246 : if (warned != 1 && code != ENUMERAL_TYPE)
5166 : : /* Empty unnamed enum OK */
5167 : : {
5168 : 19 : pedwarn (input_location, 0,
5169 : : "unnamed struct/union that defines no instances");
5170 : 19 : warned = 1;
5171 : : }
5172 : : }
5173 : 435468 : else if (declspecs->typespec_kind != ctsk_tagdef
5174 : 87165 : && declspecs->typespec_kind != ctsk_tagfirstref
5175 : 25935 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5176 : 25927 : && declspecs->storage_class != csc_none)
5177 : : {
5178 : 2 : if (warned != 1)
5179 : 2 : pedwarn (input_location, 0,
5180 : : "empty declaration with storage class specifier "
5181 : : "does not redeclare tag");
5182 : 2 : warned = 1;
5183 : 2 : pending_xref_error ();
5184 : : }
5185 : 435466 : else if (declspecs->typespec_kind != ctsk_tagdef
5186 : 87163 : && declspecs->typespec_kind != ctsk_tagfirstref
5187 : 25933 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5188 : 25925 : && (declspecs->const_p
5189 : 25922 : || declspecs->volatile_p
5190 : 25922 : || declspecs->atomic_p
5191 : 25922 : || declspecs->restrict_p
5192 : 25922 : || declspecs->address_space))
5193 : : {
5194 : 3 : if (warned != 1)
5195 : 3 : pedwarn (input_location, 0,
5196 : : "empty declaration with type qualifier "
5197 : : "does not redeclare tag");
5198 : 3 : warned = 1;
5199 : 3 : pending_xref_error ();
5200 : : }
5201 : 435463 : else if (declspecs->typespec_kind != ctsk_tagdef
5202 : 87160 : && declspecs->typespec_kind != ctsk_tagfirstref
5203 : 25930 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5204 : 25922 : && declspecs->alignas_p)
5205 : : {
5206 : 1 : if (warned != 1)
5207 : 1 : pedwarn (input_location, 0,
5208 : : "empty declaration with %<_Alignas%> "
5209 : : "does not redeclare tag");
5210 : 1 : warned = 1;
5211 : 1 : pending_xref_error ();
5212 : : }
5213 : 435462 : else if (declspecs->typespec_kind != ctsk_tagdef
5214 : 87159 : && declspecs->typespec_kind != ctsk_tagfirstref
5215 : 25929 : && declspecs->typespec_kind != ctsk_tagfirstref_attrs
5216 : 25921 : && code == ENUMERAL_TYPE
5217 : 15 : && !declspecs->enum_type_specifier_ref_p)
5218 : : {
5219 : 3 : bool warned_enum = false;
5220 : 3 : if (warned != 1)
5221 : 3 : warned_enum = pedwarn (input_location, OPT_Wpedantic,
5222 : : "empty declaration of %<enum%> type "
5223 : : "does not redeclare tag");
5224 : 3 : if (warned_enum)
5225 : 2 : warned = 1;
5226 : 3 : pending_xref_error ();
5227 : 3 : }
5228 : : else
5229 : : {
5230 : 435459 : pending_invalid_xref = NULL_TREE;
5231 : 435459 : t = lookup_tag (code, name, true, NULL);
5232 : :
5233 : 435459 : if (t == NULL_TREE)
5234 : : {
5235 : 2 : t = make_node (code);
5236 : 2 : if (flag_isoc23 || code == ENUMERAL_TYPE)
5237 : 1 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5238 : 2 : pushtag (input_location, name, t);
5239 : : }
5240 : : }
5241 : : }
5242 : : else
5243 : : {
5244 : 51 : if (warned != 1 && !in_system_header_at (input_location))
5245 : : {
5246 : 44 : pedwarn (input_location, 0,
5247 : : "useless type name in empty declaration");
5248 : 44 : warned = 1;
5249 : : }
5250 : : }
5251 : : }
5252 : 71 : else if (warned != 1 && !in_system_header_at (input_location)
5253 : 185 : && declspecs->typedef_p)
5254 : : {
5255 : 11 : pedwarn (input_location, 0, "useless type name in empty declaration");
5256 : 11 : warned = 1;
5257 : : }
5258 : :
5259 : 493879 : pending_invalid_xref = NULL_TREE;
5260 : :
5261 : 493879 : if (declspecs->inline_p)
5262 : : {
5263 : 5 : error ("%<inline%> in empty declaration");
5264 : 5 : warned = 1;
5265 : : }
5266 : :
5267 : 493879 : if (declspecs->noreturn_p)
5268 : : {
5269 : 2 : error ("%<_Noreturn%> in empty declaration");
5270 : 2 : warned = 1;
5271 : : }
5272 : :
5273 : 493879 : if (declspecs->constexpr_p)
5274 : : {
5275 : 7 : error ("%<constexpr%> in empty declaration");
5276 : 7 : warned = 1;
5277 : : }
5278 : :
5279 : 493879 : if (current_scope == file_scope && declspecs->storage_class == csc_auto)
5280 : : {
5281 : 2 : error ("%<auto%> in file-scope empty declaration");
5282 : 2 : warned = 1;
5283 : : }
5284 : :
5285 : 493879 : if (current_scope == file_scope && declspecs->storage_class == csc_register)
5286 : : {
5287 : 2 : error ("%<register%> in file-scope empty declaration");
5288 : 2 : warned = 1;
5289 : : }
5290 : :
5291 : 493879 : if (declspecs->enum_type_specifier_ref_p && !warned)
5292 : : {
5293 : 39 : if (declspecs->storage_class != csc_none)
5294 : : {
5295 : 1 : error ("storage class specifier in empty declaration with %<enum%> "
5296 : : "underlying type");
5297 : 1 : warned = 1;
5298 : : }
5299 : 38 : else if (declspecs->thread_p)
5300 : : {
5301 : 2 : error ("%qs in empty declaration with %<enum%> underlying type",
5302 : 1 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5303 : 1 : warned = 1;
5304 : : }
5305 : 37 : else if (declspecs->const_p
5306 : 36 : || declspecs->volatile_p
5307 : 36 : || declspecs->atomic_p
5308 : 36 : || declspecs->restrict_p
5309 : 36 : || declspecs->address_space)
5310 : : {
5311 : 1 : error ("type qualifier in empty declaration with %<enum%> "
5312 : : "underlying type");
5313 : 1 : warned = 1;
5314 : : }
5315 : 36 : else if (declspecs->alignas_p)
5316 : : {
5317 : 1 : error ("%<alignas%> in empty declaration with %<enum%> "
5318 : : "underlying type");
5319 : 1 : warned = 1;
5320 : : }
5321 : : }
5322 : :
5323 : 493711 : if (!warned && !in_system_header_at (input_location)
5324 : 611230 : && declspecs->storage_class != csc_none)
5325 : : {
5326 : 12 : warning (0, "useless storage class specifier in empty declaration");
5327 : 12 : warned = 2;
5328 : : }
5329 : :
5330 : 493879 : if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5331 : : {
5332 : 3 : warning (0, "useless %qs in empty declaration",
5333 : 3 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5334 : 3 : warned = 2;
5335 : : }
5336 : :
5337 : 3 : if (!warned
5338 : 493692 : && !in_system_header_at (input_location)
5339 : 611220 : && (declspecs->const_p
5340 : 117336 : || declspecs->volatile_p
5341 : 117336 : || declspecs->atomic_p
5342 : 117336 : || declspecs->restrict_p
5343 : 117336 : || declspecs->address_space))
5344 : : {
5345 : 8 : warning (0, "useless type qualifier in empty declaration");
5346 : 8 : warned = 2;
5347 : : }
5348 : :
5349 : 493692 : if (!warned && !in_system_header_at (input_location)
5350 : 611207 : && declspecs->alignas_p)
5351 : : {
5352 : 4 : warning (0, "useless %<_Alignas%> in empty declaration");
5353 : 4 : warned = 2;
5354 : : }
5355 : :
5356 : 493879 : if (found_tag
5357 : 493879 : && warned == 2
5358 : 23 : && (declspecs->typespec_kind == ctsk_tagref_attrs
5359 : 23 : || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5360 : : {
5361 : : /* Standard attributes after the "struct" or "union" keyword are
5362 : : only permitted when the contents of the type are defined, or
5363 : : in the form "struct-or-union attribute-specifier-sequence
5364 : : identifier;". If the ';' was not present, attributes were
5365 : : diagnosed in the parser. Here, ensure that any other useless
5366 : : elements of the declaration result in a pedwarn, not just a
5367 : : warning. Forward declarations of enum types are not part of
5368 : : standard C, but handle them the same. */
5369 : 2 : pedwarn (input_location, 0,
5370 : : "invalid use of attributes in empty declaration");
5371 : 2 : warned = 1;
5372 : : }
5373 : :
5374 : 493879 : if (warned != 1)
5375 : : {
5376 : 493705 : if (declspecs->declspecs_seen_p
5377 : 493705 : && !declspecs->non_std_attrs_seen_p)
5378 : : /* An attribute declaration (but not a fallthrough attribute
5379 : : declaration, which was handled separately); warn if there
5380 : : are any attributes being ignored (but not if the attributes
5381 : : were empty). */
5382 : 48 : c_warn_unused_attributes (declspecs->attrs);
5383 : 493657 : else if (!found_tag)
5384 : 10 : pedwarn (input_location, 0, "empty declaration");
5385 : : }
5386 : 493879 : }
5387 : :
5388 : :
5389 : : /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5390 : : bits. SPECS represents declaration specifiers that the grammar
5391 : : only permits to contain type qualifiers and attributes. */
5392 : :
5393 : : int
5394 : 17916820 : quals_from_declspecs (const struct c_declspecs *specs)
5395 : : {
5396 : 17916820 : int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5397 : : | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5398 : : | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5399 : 17916820 : | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5400 : 17916820 : | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5401 : 17916820 : gcc_assert (!specs->type
5402 : : && !specs->decl_attr
5403 : : && specs->typespec_word == cts_none
5404 : : && specs->storage_class == csc_none
5405 : : && !specs->typedef_p
5406 : : && !specs->explicit_signed_p
5407 : : && !specs->deprecated_p
5408 : : && !specs->unavailable_p
5409 : : && !specs->long_p
5410 : : && !specs->long_long_p
5411 : : && !specs->short_p
5412 : : && !specs->signed_p
5413 : : && !specs->unsigned_p
5414 : : && !specs->complex_p
5415 : : && !specs->inline_p
5416 : : && !specs->noreturn_p
5417 : : && !specs->thread_p);
5418 : 17916820 : return quals;
5419 : : }
5420 : :
5421 : : /* Construct an array declarator. LOC is the location of the
5422 : : beginning of the array (usually the opening brace). EXPR is the
5423 : : expression inside [], or NULL_TREE. QUALS are the type qualifiers
5424 : : inside the [] (to be applied to the pointer to which a parameter
5425 : : array is converted). STATIC_P is true if "static" is inside the
5426 : : [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5427 : : VLA of unspecified length which is nevertheless a complete type,
5428 : : false otherwise. The field for the contained declarator is left to
5429 : : be filled in by set_array_declarator_inner. */
5430 : :
5431 : : struct c_declarator *
5432 : 1133530 : build_array_declarator (location_t loc,
5433 : : tree expr, struct c_declspecs *quals, bool static_p,
5434 : : bool vla_unspec_p)
5435 : : {
5436 : 1133530 : struct c_declarator *declarator = XOBNEW (&parser_obstack,
5437 : : struct c_declarator);
5438 : 1133530 : declarator->id_loc = loc;
5439 : 1133530 : declarator->kind = cdk_array;
5440 : 1133530 : declarator->declarator = 0;
5441 : 1133530 : declarator->u.array.dimen = expr;
5442 : 1133530 : if (quals)
5443 : : {
5444 : 933 : declarator->u.array.attrs = quals->attrs;
5445 : 933 : declarator->u.array.quals = quals_from_declspecs (quals);
5446 : : }
5447 : : else
5448 : : {
5449 : 1132597 : declarator->u.array.attrs = NULL_TREE;
5450 : 1132597 : declarator->u.array.quals = 0;
5451 : : }
5452 : 1133530 : declarator->u.array.static_p = static_p;
5453 : 1133530 : declarator->u.array.vla_unspec_p = vla_unspec_p;
5454 : 1133530 : if (static_p || quals != NULL)
5455 : 1036 : pedwarn_c90 (loc, OPT_Wpedantic,
5456 : : "ISO C90 does not support %<static%> or type "
5457 : : "qualifiers in parameter array declarators");
5458 : 1133530 : if (vla_unspec_p)
5459 : 147 : pedwarn_c90 (loc, OPT_Wpedantic,
5460 : : "ISO C90 does not support %<[*]%> array declarators");
5461 : 147 : if (vla_unspec_p)
5462 : : {
5463 : 147 : if (!current_scope->parm_flag)
5464 : : {
5465 : : /* C99 6.7.5.2p4 */
5466 : 4 : error_at (loc, "%<[*]%> not allowed in other than "
5467 : : "function prototype scope");
5468 : 4 : declarator->u.array.vla_unspec_p = false;
5469 : 4 : return NULL;
5470 : : }
5471 : 143 : current_scope->had_vla_unspec = true;
5472 : : }
5473 : : return declarator;
5474 : : }
5475 : :
5476 : : /* Set the contained declarator of an array declarator. DECL is the
5477 : : declarator, as constructed by build_array_declarator; INNER is what
5478 : : appears on the left of the []. */
5479 : :
5480 : : struct c_declarator *
5481 : 1133526 : set_array_declarator_inner (struct c_declarator *decl,
5482 : : struct c_declarator *inner)
5483 : : {
5484 : 1133526 : decl->declarator = inner;
5485 : 1133526 : return decl;
5486 : : }
5487 : :
5488 : : /* Determine whether TYPE is a one-element array type "[1]". */
5489 : : static bool
5490 : 522588 : one_element_array_type_p (const_tree type)
5491 : : {
5492 : 522588 : if (TREE_CODE (type) != ARRAY_TYPE)
5493 : : return false;
5494 : 522588 : return integer_zerop (array_type_nelts_minus_one (type));
5495 : : }
5496 : :
5497 : : /* Determine whether TYPE is a zero-length array type "[0]". */
5498 : : static bool
5499 : 522588 : zero_length_array_type_p (const_tree type)
5500 : : {
5501 : 522588 : if (TREE_CODE (type) == ARRAY_TYPE)
5502 : 522588 : if (tree type_size = TYPE_SIZE_UNIT (type))
5503 : 438011 : if ((integer_zerop (type_size))
5504 : 1816 : && TYPE_DOMAIN (type) != NULL_TREE
5505 : 439827 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5506 : : return true;
5507 : : return false;
5508 : : }
5509 : :
5510 : : /* INIT is a constructor that forms DECL's initializer. If the final
5511 : : element initializes a flexible array field, adjust the size of the
5512 : : DECL with the initializer based on whether the DECL is a union or
5513 : : a structure. */
5514 : :
5515 : : static void
5516 : 103622 : add_flexible_array_elts_to_size (tree decl, tree init)
5517 : : {
5518 : 103622 : tree elt, type;
5519 : :
5520 : 103622 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5521 : 1428 : return;
5522 : :
5523 : 102194 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
5524 : 102194 : type = TREE_TYPE (elt);
5525 : 102194 : if (c_flexible_array_member_type_p (type))
5526 : : {
5527 : 220 : complete_array_type (&type, elt, false);
5528 : : /* For a structure, add the size of the initializer to the DECL's
5529 : : size. */
5530 : 220 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5531 : : {
5532 : 210 : DECL_SIZE (decl)
5533 : 210 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5534 : 210 : DECL_SIZE_UNIT (decl)
5535 : 420 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
5536 : : TYPE_SIZE_UNIT (type));
5537 : : }
5538 : : /* For a union, the DECL's size is the maximum of the current size
5539 : : and the size of the initializer. */
5540 : : else
5541 : : {
5542 : 10 : DECL_SIZE (decl)
5543 : 10 : = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5544 : 10 : DECL_SIZE_UNIT (decl)
5545 : 20 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
5546 : : TYPE_SIZE_UNIT (type));
5547 : : }
5548 : : }
5549 : : }
5550 : :
5551 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5552 : : Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5553 : : before the type name, and set *EXPR_CONST_OPERANDS, if
5554 : : EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5555 : : appear in a constant expression. */
5556 : :
5557 : : tree
5558 : 118352385 : groktypename (struct c_type_name *type_name, tree *expr,
5559 : : bool *expr_const_operands)
5560 : : {
5561 : 118352385 : tree type;
5562 : 118352385 : tree attrs = type_name->specs->attrs;
5563 : :
5564 : 118352385 : type_name->specs->attrs = NULL_TREE;
5565 : :
5566 : 118352385 : type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5567 : : false, NULL, &attrs, expr, expr_const_operands,
5568 : : DEPRECATED_NORMAL);
5569 : :
5570 : : /* Apply attributes. */
5571 : 118352385 : attrs = c_warn_type_attributes (type, attrs);
5572 : 118352385 : decl_attributes (&type, attrs, 0);
5573 : :
5574 : 118352385 : return type;
5575 : : }
5576 : :
5577 : : /* Looks up the most recent pushed declaration corresponding to DECL. */
5578 : :
5579 : : static tree
5580 : 90330512 : lookup_last_decl (tree decl)
5581 : : {
5582 : 90330512 : tree last_decl = lookup_name (DECL_NAME (decl));
5583 : 90330512 : if (!last_decl)
5584 : 87812750 : last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5585 : 90330512 : return last_decl;
5586 : : }
5587 : :
5588 : : /* Wrapper for decl_attributes that adds some implicit attributes
5589 : : to VAR_DECLs or FUNCTION_DECLs. */
5590 : :
5591 : : static tree
5592 : 62853883 : c_decl_attributes (tree *node, tree attributes, int flags)
5593 : : {
5594 : : /* Add implicit "omp declare target" attribute if requested. */
5595 : 62853883 : if (vec_safe_length (current_omp_declare_target_attribute)
5596 : 9177 : && ((VAR_P (*node) && is_global_var (*node))
5597 : 2729 : || TREE_CODE (*node) == FUNCTION_DECL))
5598 : : {
5599 : 1131 : if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5600 : 13 : attributes = tree_cons (get_identifier ("omp declare target implicit"),
5601 : : NULL_TREE, attributes);
5602 : : else
5603 : 1118 : attributes = tree_cons (get_identifier ("omp declare target"),
5604 : : NULL_TREE, attributes);
5605 : 1131 : if (TREE_CODE (*node) == FUNCTION_DECL)
5606 : : {
5607 : 1001 : int device_type
5608 : 1001 : = current_omp_declare_target_attribute->last ().device_type;
5609 : 1001 : device_type = MAX (device_type, 0);
5610 : 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5611 : 1001 : && !lookup_attribute ("omp declare target host", attributes))
5612 : 2 : attributes
5613 : 2 : = tree_cons (get_identifier ("omp declare target host"),
5614 : : NULL_TREE, attributes);
5615 : 1001 : if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5616 : 1001 : && !lookup_attribute ("omp declare target nohost", attributes))
5617 : 2 : attributes
5618 : 2 : = tree_cons (get_identifier ("omp declare target nohost"),
5619 : : NULL_TREE, attributes);
5620 : :
5621 : 1001 : int indirect
5622 : 1001 : = current_omp_declare_target_attribute->last ().indirect;
5623 : 1001 : if (indirect && !lookup_attribute ("omp declare target indirect",
5624 : : attributes))
5625 : 10 : attributes
5626 : 10 : = tree_cons (get_identifier ("omp declare target indirect"),
5627 : : NULL_TREE, attributes);
5628 : : }
5629 : : }
5630 : :
5631 : 62853883 : if (flag_openmp || flag_openmp_simd)
5632 : : {
5633 : : bool diagnosed = false;
5634 : 1340854 : for (tree *pa = &attributes; *pa; )
5635 : : {
5636 : 867831 : if (is_attribute_namespace_p ("omp", *pa))
5637 : : {
5638 : 60 : tree name = get_attribute_name (*pa);
5639 : 60 : if (is_attribute_p ("directive", name)
5640 : 0 : || is_attribute_p ("sequence", name)
5641 : 60 : || is_attribute_p ("decl", name))
5642 : : {
5643 : 60 : const char *p = NULL;
5644 : 60 : if (TREE_VALUE (*pa) == NULL_TREE)
5645 : 12 : p = IDENTIFIER_POINTER (name);
5646 : 108 : for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
5647 : : {
5648 : 48 : tree d = TREE_VALUE (a);
5649 : 48 : gcc_assert (TREE_CODE (d) == C_TOKEN_VEC);
5650 : 86 : if (TREE_PUBLIC (d)
5651 : 38 : && (VAR_P (*node)
5652 : 2 : || TREE_CODE (*node) == FUNCTION_DECL)
5653 : 86 : && c_maybe_parse_omp_decl (*node, d))
5654 : 38 : continue;
5655 : 10 : p = TREE_PUBLIC (d) ? "decl" : "directive";
5656 : : }
5657 : 60 : if (p && !diagnosed)
5658 : : {
5659 : 22 : error ("%<omp::%s%> not allowed to be specified in "
5660 : : "this context", p);
5661 : 22 : diagnosed = true;
5662 : : }
5663 : 60 : if (p)
5664 : : {
5665 : 22 : *pa = TREE_CHAIN (*pa);
5666 : 22 : continue;
5667 : : }
5668 : : }
5669 : : }
5670 : 867809 : pa = &TREE_CHAIN (*pa);
5671 : : }
5672 : : }
5673 : :
5674 : : /* Look up the current declaration with all the attributes merged
5675 : : so far so that attributes on the current declaration that's
5676 : : about to be pushed that conflict with the former can be detected,
5677 : : diagnosed, and rejected as appropriate. To match the C++ FE, do
5678 : : not pass an error_mark_node when we found an undeclared variable. */
5679 : 62853883 : tree last_decl = lookup_last_decl (*node);
5680 : 62853883 : if (last_decl == error_mark_node)
5681 : 20 : last_decl = NULL_TREE;
5682 : 62853883 : tree attr = decl_attributes (node, attributes, flags, last_decl);
5683 : 62853882 : if (VAR_P (*node) && DECL_THREAD_LOCAL_P (*node))
5684 : : {
5685 : : // tls_model attribute can set a stronger TLS access model.
5686 : 2851 : tls_model model = DECL_TLS_MODEL (*node);
5687 : 2851 : tls_model default_model = decl_default_tls_model (*node);
5688 : 2851 : if (default_model > model)
5689 : 1597 : set_decl_tls_model (*node, default_model);
5690 : : }
5691 : 62853882 : return attr;
5692 : : }
5693 : :
5694 : :
5695 : : /* Decode a declarator in an ordinary declaration or data definition.
5696 : : This is called as soon as the type information and variable name
5697 : : have been parsed, before parsing the initializer if any.
5698 : : Here we create the ..._DECL node, fill in its type,
5699 : : and (if DO_PUSH) put it on the list of decls for the current context.
5700 : : When nonnull, set *LASTLOC to the location of the prior declaration
5701 : : of the same entity if one exists.
5702 : : The ..._DECL node is returned as the value.
5703 : :
5704 : : Exception: for arrays where the length is not specified,
5705 : : the type is left null, to be filled in by `finish_decl'.
5706 : :
5707 : : Function definitions do not come here; they go to start_function
5708 : : instead. However, external and forward declarations of functions
5709 : : do go through here. Structure field declarations are done by
5710 : : grokfield and not through here. */
5711 : :
5712 : : tree
5713 : 27476653 : start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5714 : : bool initialized, tree attributes, bool do_push /* = true */,
5715 : : location_t *lastloc /* = NULL */)
5716 : : {
5717 : 27476653 : tree decl;
5718 : 27476653 : tree old_decl;
5719 : 27476653 : tree tem;
5720 : 27476653 : tree expr = NULL_TREE;
5721 : 27476653 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5722 : :
5723 : : /* An object declared as __attribute__((unavailable)) suppresses
5724 : : warnings and errors from __attribute__((deprecated/unavailable))
5725 : : components.
5726 : : An object declared as __attribute__((deprecated)) suppresses
5727 : : warnings of uses of other deprecated items. */
5728 : 27476653 : if (lookup_attribute ("unavailable", attributes))
5729 : : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5730 : 27476629 : else if (lookup_attribute ("deprecated", attributes))
5731 : 29983 : deprecated_state = DEPRECATED_SUPPRESS;
5732 : :
5733 : 27476653 : decl = grokdeclarator (declarator, declspecs,
5734 : : NORMAL, initialized, NULL, &attributes, &expr, NULL,
5735 : : deprecated_state);
5736 : 27476653 : if (!decl || decl == error_mark_node)
5737 : : return NULL_TREE;
5738 : :
5739 : 27476629 : old_decl = lookup_last_decl (decl);
5740 : :
5741 : 27476629 : if (tree lastdecl = lastloc ? old_decl : NULL_TREE)
5742 : 4683120 : if (lastdecl != error_mark_node)
5743 : 4683111 : *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5744 : :
5745 : : /* Make sure the size expression is evaluated at this point. */
5746 : 27476629 : if (expr && !current_scope->parm_flag)
5747 : 12926 : add_stmt (fold_convert (void_type_node, expr));
5748 : :
5749 : 13006049 : if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5750 : 27476631 : && TREE_PUBLIC (decl))
5751 : 1 : warning (OPT_Wmain, "%q+D is usually a function", decl);
5752 : :
5753 : 14 : if (warn_missing_variable_declarations && VAR_P (decl)
5754 : 27476642 : && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE)
5755 : 5 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations,
5756 : : "no previous declaration for %qD", decl);
5757 : :
5758 : 27476629 : if (initialized)
5759 : : /* Is it valid for this decl to have an initializer at all?
5760 : : If not, set INITIALIZED to zero, which will indirectly
5761 : : tell 'finish_decl' to ignore the initializer once it is parsed. */
5762 : 6198495 : switch (TREE_CODE (decl))
5763 : : {
5764 : 6 : case TYPE_DECL:
5765 : 6 : error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5766 : 6 : initialized = false;
5767 : 6 : break;
5768 : :
5769 : 9 : case FUNCTION_DECL:
5770 : 9 : error ("function %qD is initialized like a variable", decl);
5771 : 9 : initialized = false;
5772 : 9 : break;
5773 : :
5774 : 49 : case PARM_DECL:
5775 : : /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5776 : 49 : error ("parameter %qD is initialized", decl);
5777 : 49 : initialized = false;
5778 : 49 : break;
5779 : :
5780 : 6198431 : default:
5781 : : /* Don't allow initializations for incomplete types except for
5782 : : arrays which might be completed by the initialization. */
5783 : :
5784 : : /* This can happen if the array size is an undefined macro.
5785 : : We already gave a warning, so we don't need another one. */
5786 : 6198431 : if (TREE_TYPE (decl) == error_mark_node)
5787 : : initialized = false;
5788 : 6198409 : else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5789 : : {
5790 : : /* A complete type is ok if size is fixed. If the size is
5791 : : variable, an empty initializer is OK and nonempty
5792 : : initializers will be diagnosed in the parser. */
5793 : : }
5794 : 12184 : else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5795 : : {
5796 : 3 : error ("variable %qD has initializer but incomplete type", decl);
5797 : 3 : initialized = false;
5798 : : }
5799 : : }
5800 : :
5801 : 67 : if (initialized)
5802 : : {
5803 : 6198406 : if (current_scope == file_scope)
5804 : 155862 : TREE_STATIC (decl) = 1;
5805 : :
5806 : : /* Tell 'pushdecl' this is an initialized decl
5807 : : even though we don't yet have the initializer expression.
5808 : : Also tell 'finish_decl' it may store the real initializer. */
5809 : 6198406 : DECL_INITIAL (decl) = error_mark_node;
5810 : : }
5811 : :
5812 : : /* If this is a function declaration, write a record describing it to the
5813 : : prototypes file (if requested). */
5814 : :
5815 : 27476629 : if (TREE_CODE (decl) == FUNCTION_DECL)
5816 : 14470580 : gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5817 : :
5818 : : /* ANSI specifies that a tentative definition which is not merged with
5819 : : a non-tentative definition behaves exactly like a definition with an
5820 : : initializer equal to zero. (Section 3.7.2)
5821 : :
5822 : : -fno-common gives strict ANSI behavior, though this tends to break
5823 : : a large body of code that grew up without this rule.
5824 : :
5825 : : Thread-local variables are never common, since there's no entrenched
5826 : : body of code to break, and it allows more efficient variable references
5827 : : in the presence of dynamic linking. */
5828 : :
5829 : 27476629 : if (VAR_P (decl)
5830 : 8750288 : && !initialized
5831 : 2551882 : && TREE_PUBLIC (decl)
5832 : 988366 : && !DECL_THREAD_LOCAL_P (decl)
5833 : 28462645 : && !flag_no_common)
5834 : 128 : DECL_COMMON (decl) = 1;
5835 : :
5836 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5837 : 27476629 : c_decl_attributes (&decl, attributes, 0);
5838 : :
5839 : : /* Handle gnu_inline attribute. */
5840 : 27476628 : if (declspecs->inline_p
5841 : 1017 : && !flag_gnu89_inline
5842 : 985 : && TREE_CODE (decl) == FUNCTION_DECL
5843 : 27477605 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5844 : 954 : || current_function_decl))
5845 : : {
5846 : 46 : if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5847 : : ;
5848 : 37 : else if (declspecs->storage_class != csc_static)
5849 : 37 : DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5850 : : }
5851 : :
5852 : 27476628 : if (TREE_CODE (decl) == FUNCTION_DECL
5853 : 14470579 : && DECL_DECLARED_INLINE_P (decl)
5854 : 1007 : && DECL_UNINLINABLE (decl)
5855 : 27476630 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5856 : : {
5857 : 2 : auto_urlify_attributes sentinel;
5858 : 2 : warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5859 : : decl, "noinline");
5860 : 2 : }
5861 : :
5862 : : /* C99 6.7.4p3: An inline definition of a function with external
5863 : : linkage shall not contain a definition of a modifiable object
5864 : : with static storage duration... */
5865 : 27476628 : if (VAR_P (decl)
5866 : 8750288 : && current_scope != file_scope
5867 : 7599074 : && TREE_STATIC (decl)
5868 : 78746 : && !TREE_READONLY (decl)
5869 : 76174 : && DECL_DECLARED_INLINE_P (current_function_decl)
5870 : 27476698 : && DECL_EXTERNAL (current_function_decl))
5871 : 7 : record_inline_static (input_location, current_function_decl,
5872 : : decl, csi_modifiable);
5873 : :
5874 : 27476628 : if (c_dialect_objc ()
5875 : 0 : && VAR_OR_FUNCTION_DECL_P (decl))
5876 : 0 : objc_check_global_decl (decl);
5877 : :
5878 : : /* To enable versions to be created across TU's we mark and mangle all
5879 : : non-default versioned functions. */
5880 : 27476628 : if (TREE_CODE (decl) == FUNCTION_DECL
5881 : : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
5882 : : && get_target_version (decl).is_valid ())
5883 : : {
5884 : : maybe_mark_function_versioned (decl);
5885 : : if (current_scope != file_scope)
5886 : : error ("versioned declarations are only allowed at file scope");
5887 : : }
5888 : :
5889 : : /* Add this decl to the current scope.
5890 : : TEM may equal DECL or it may be a previous decl of the same name. */
5891 : 27476628 : if (do_push)
5892 : : {
5893 : 27476284 : tem = pushdecl (decl);
5894 : :
5895 : 27476284 : if (initialized && DECL_EXTERNAL (tem))
5896 : : {
5897 : 27 : DECL_EXTERNAL (tem) = 0;
5898 : 27 : TREE_STATIC (tem) = 1;
5899 : : }
5900 : :
5901 : 27476284 : return tem;
5902 : : }
5903 : : else
5904 : 344 : return decl;
5905 : : }
5906 : :
5907 : : /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5908 : : DECL or the non-array element type if DECL is an uninitialized array.
5909 : : If that type has a const member, diagnose this. */
5910 : :
5911 : : static void
5912 : 7 : diagnose_uninitialized_cst_member (tree decl, tree type)
5913 : : {
5914 : 7 : tree field;
5915 : 17 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5916 : : {
5917 : 10 : tree field_type;
5918 : 10 : if (TREE_CODE (field) != FIELD_DECL)
5919 : 0 : continue;
5920 : 10 : field_type = strip_array_types (TREE_TYPE (field));
5921 : :
5922 : 10 : if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5923 : : {
5924 : 5 : auto_diagnostic_group d;
5925 : 5 : if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5926 : : "uninitialized const member in %qT is invalid in C++",
5927 : 5 : strip_array_types (TREE_TYPE (decl))))
5928 : 5 : inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5929 : 5 : }
5930 : :
5931 : 10 : if (RECORD_OR_UNION_TYPE_P (field_type))
5932 : 2 : diagnose_uninitialized_cst_member (decl, field_type);
5933 : : }
5934 : 7 : }
5935 : :
5936 : : /* Finish processing of a declaration;
5937 : : install its initial value.
5938 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5939 : : If the length of an array type is not known before,
5940 : : it must be determined now, from the initial value, or it is an error.
5941 : :
5942 : : INIT_LOC is the location of the initial value. */
5943 : :
5944 : : void
5945 : 152854159 : finish_decl (tree decl, location_t init_loc, tree init,
5946 : : tree origtype, tree asmspec_tree)
5947 : : {
5948 : 152854159 : tree type;
5949 : 152854159 : bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5950 : 152854159 : const char *asmspec = 0;
5951 : :
5952 : : /* If a name was specified, get the string. */
5953 : 144100953 : if (VAR_OR_FUNCTION_DECL_P (decl)
5954 : 167324738 : && DECL_FILE_SCOPE_P (decl))
5955 : 15623308 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5956 : 152854159 : if (asmspec_tree)
5957 : 856066 : asmspec = TREE_STRING_POINTER (asmspec_tree);
5958 : :
5959 : 152854159 : if (VAR_P (decl)
5960 : 8753206 : && TREE_STATIC (decl)
5961 : 153915176 : && global_bindings_p ())
5962 : : /* So decl is a global variable. Record the types it uses
5963 : : so that we can decide later to emit debug info for them. */
5964 : 979136 : record_types_used_by_current_var_decl (decl);
5965 : :
5966 : : /* If `start_decl' didn't like having an initialization, ignore it now. */
5967 : 152854159 : if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5968 : : init = NULL_TREE;
5969 : :
5970 : : /* Don't crash if parm is initialized. */
5971 : 152854159 : if (TREE_CODE (decl) == PARM_DECL)
5972 : : init = NULL_TREE;
5973 : :
5974 : 31706854 : if (init)
5975 : 6201325 : store_init_value (init_loc, decl, init, origtype);
5976 : :
5977 : 152854159 : if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5978 : 0 : || TREE_CODE (decl) == FIELD_DECL))
5979 : 0 : objc_check_decl (decl);
5980 : :
5981 : 152854159 : type = TREE_TYPE (decl);
5982 : :
5983 : : /* Deduce size of array from initialization, if not already known.
5984 : : This is only needed for an initialization in the current scope;
5985 : : it must not be done for a file-scope initialization of a
5986 : : declaration with external linkage, redeclared in an inner scope
5987 : : with the outer declaration shadowed in an intermediate scope. */
5988 : 152854159 : if (TREE_CODE (type) == ARRAY_TYPE
5989 : 853056 : && TYPE_DOMAIN (type) == NULL_TREE
5990 : 19791 : && TREE_CODE (decl) != TYPE_DECL
5991 : 152873854 : && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5992 : : {
5993 : 19509 : bool do_default
5994 : 19509 : = !TREE_STATIC (decl) && !DECL_EXTERNAL (decl);
5995 : 19509 : int failure
5996 : 19509 : = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5997 : : do_default);
5998 : :
5999 : : /* Get the completed type made by complete_array_type. */
6000 : 19509 : type = TREE_TYPE (decl);
6001 : :
6002 : 19509 : switch (failure)
6003 : : {
6004 : 0 : case 1:
6005 : 0 : error ("initializer fails to determine size of %q+D", decl);
6006 : 0 : break;
6007 : :
6008 : 7353 : case 2:
6009 : 7353 : if (do_default)
6010 : 1 : error ("array size missing in %q+D", decl);
6011 : 7352 : else if (!TREE_PUBLIC (decl))
6012 : 37 : pedwarn_c23 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
6013 : : "array size missing in %q+D", decl);
6014 : : break;
6015 : :
6016 : 3 : case 3:
6017 : 3 : error ("zero or negative size array %q+D", decl);
6018 : 3 : break;
6019 : :
6020 : 12153 : case 0:
6021 : : /* For global variables, update the copy of the type that
6022 : : exists in the binding. */
6023 : 12153 : if (TREE_PUBLIC (decl))
6024 : : {
6025 : 3820 : struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
6026 : 7640 : while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
6027 : 3820 : b_ext = b_ext->shadowed;
6028 : 3820 : if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
6029 : : {
6030 : 3820 : if (b_ext->u.type && comptypes (b_ext->u.type, type))
6031 : 176 : b_ext->u.type = composite_type (b_ext->u.type, type);
6032 : : else
6033 : 3644 : b_ext->u.type = type;
6034 : : }
6035 : : }
6036 : : break;
6037 : :
6038 : 0 : default:
6039 : 0 : gcc_unreachable ();
6040 : : }
6041 : :
6042 : 19509 : if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
6043 : 11284 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6044 : :
6045 : 19509 : relayout_decl (decl);
6046 : : }
6047 : :
6048 : : /* Look for braced array initializers for character arrays and
6049 : : recursively convert them into STRING_CSTs. */
6050 : 152854159 : if (tree init = DECL_INITIAL (decl))
6051 : 127351026 : DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
6052 : :
6053 : 152854159 : if (VAR_P (decl))
6054 : : {
6055 : 8753206 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6056 : 103622 : add_flexible_array_elts_to_size (decl, init);
6057 : :
6058 : 8753206 : complete_flexible_array_elts (DECL_INITIAL (decl));
6059 : :
6060 : 8753206 : if (is_global_var (decl))
6061 : : {
6062 : 1234401 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
6063 : 1234401 : ? TCTX_THREAD_STORAGE
6064 : : : TCTX_STATIC_STORAGE);
6065 : 1234401 : if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
6066 : 0 : TREE_TYPE (decl) = error_mark_node;
6067 : : }
6068 : :
6069 : 8761296 : if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
6070 : 8761197 : && COMPLETE_TYPE_P (TREE_TYPE (decl)))
6071 : 107 : layout_decl (decl, 0);
6072 : :
6073 : 8753206 : if (DECL_SIZE (decl) == NULL_TREE
6074 : : /* Don't give an error if we already gave one earlier. */
6075 : 7983 : && TREE_TYPE (decl) != error_mark_node
6076 : 8761090 : && (TREE_STATIC (decl)
6077 : : /* A static variable with an incomplete type
6078 : : is an error if it is initialized.
6079 : : Also if it is not file scope.
6080 : : Also if it is thread-local (in C23).
6081 : : Otherwise, let it through, but if it is not `extern'
6082 : : then it may cause an error message later. */
6083 : 308 : ? (DECL_INITIAL (decl) != NULL_TREE
6084 : 308 : || !DECL_FILE_SCOPE_P (decl)
6085 : 305 : || (flag_isoc23 && DECL_THREAD_LOCAL_P (decl)))
6086 : : /* An automatic variable with an incomplete type
6087 : : is an error. */
6088 : 7576 : : !DECL_EXTERNAL (decl)))
6089 : : {
6090 : 19 : error ("storage size of %q+D isn%'t known", decl);
6091 : 19 : TREE_TYPE (decl) = error_mark_node;
6092 : : }
6093 : :
6094 : 17317395 : if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6095 : 8483766 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6096 : 282728 : && DECL_SIZE (decl) == NULL_TREE
6097 : 8753494 : && TREE_STATIC (decl))
6098 : 135 : incomplete_record_decls.safe_push (decl);
6099 : :
6100 : 8753206 : if (is_global_var (decl)
6101 : 1234401 : && DECL_SIZE (decl) != NULL_TREE
6102 : 9979665 : && TREE_TYPE (decl) != error_mark_node)
6103 : : {
6104 : 1226441 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
6105 : 1226438 : constant_expression_warning (DECL_SIZE (decl));
6106 : : else
6107 : : {
6108 : 3 : error ("storage size of %q+D isn%'t constant", decl);
6109 : 3 : TREE_TYPE (decl) = error_mark_node;
6110 : : }
6111 : : }
6112 : :
6113 : 8753206 : if (TREE_USED (type))
6114 : : {
6115 : 4 : TREE_USED (decl) = 1;
6116 : 4 : DECL_READ_P (decl) = 1;
6117 : : }
6118 : : }
6119 : :
6120 : : /* If this is a function and an assembler name is specified, reset DECL_RTL
6121 : : so we can give it its new name. Also, update builtin_decl if it
6122 : : was a normal built-in. */
6123 : 152854159 : if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
6124 : : {
6125 : 843056 : if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6126 : 71164 : set_builtin_user_assembler_name (decl, asmspec);
6127 : 843056 : set_user_assembler_name (decl, asmspec);
6128 : : }
6129 : :
6130 : : /* If #pragma weak was used, mark the decl weak now. */
6131 : 152854159 : maybe_apply_pragma_weak (decl);
6132 : :
6133 : : /* Output the assembler code and/or RTL code for variables and functions,
6134 : : unless the type is an undefined structure or union.
6135 : : If not, it will get done when the type is completed. */
6136 : :
6137 : 152854159 : if (VAR_OR_FUNCTION_DECL_P (decl))
6138 : : {
6139 : : /* Determine the ELF visibility. */
6140 : 23223785 : if (TREE_PUBLIC (decl))
6141 : 15598766 : c_determine_visibility (decl);
6142 : :
6143 : : /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
6144 : 23223785 : if (c_dialect_objc ())
6145 : 0 : objc_check_decl (decl);
6146 : :
6147 : 23223785 : if (asmspec)
6148 : : {
6149 : : /* If this is not a static variable, issue a warning.
6150 : : It doesn't make any sense to give an ASMSPEC for an
6151 : : ordinary, non-register local variable. Historically,
6152 : : GCC has accepted -- but ignored -- the ASMSPEC in
6153 : : this case. */
6154 : 857103 : if (!DECL_FILE_SCOPE_P (decl)
6155 : 1037 : && VAR_P (decl)
6156 : 1037 : && !C_DECL_REGISTER (decl)
6157 : 856074 : && !TREE_STATIC (decl))
6158 : 1 : warning (0, "ignoring %<asm%> specifier for non-static local "
6159 : : "variable %q+D", decl);
6160 : : else
6161 : 856065 : set_user_assembler_name (decl, asmspec);
6162 : : }
6163 : :
6164 : 23223785 : if (DECL_FILE_SCOPE_P (decl))
6165 : : {
6166 : 15623308 : if (DECL_INITIAL (decl) == NULL_TREE
6167 : 15623308 : || DECL_INITIAL (decl) == error_mark_node)
6168 : : /* Don't output anything
6169 : : when a tentative file-scope definition is seen.
6170 : : But at end of compilation, do output code for them. */
6171 : 15467458 : DECL_DEFER_OUTPUT (decl) = 1;
6172 : 15623308 : if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
6173 : 68 : DECL_HARD_REGISTER (decl) = 1;
6174 : 15623308 : rest_of_decl_compilation (decl, true, 0);
6175 : :
6176 : 15623308 : if (TREE_CODE (decl) == FUNCTION_DECL)
6177 : : {
6178 : 14470512 : tree parms = DECL_ARGUMENTS (decl);
6179 : 14470512 : const bool builtin = fndecl_built_in_p (decl);
6180 : 14470512 : if (tree access = build_attr_access_from_parms (parms, !builtin))
6181 : 340083 : decl_attributes (&decl, access, 0);
6182 : : }
6183 : : }
6184 : : else
6185 : : {
6186 : : /* In conjunction with an ASMSPEC, the `register'
6187 : : keyword indicates that we should place the variable
6188 : : in a particular register. */
6189 : 7600477 : if (asmspec && C_DECL_REGISTER (decl))
6190 : : {
6191 : 1029 : DECL_HARD_REGISTER (decl) = 1;
6192 : : /* This cannot be done for a structure with volatile
6193 : : fields, on which DECL_REGISTER will have been
6194 : : reset. */
6195 : 1029 : if (!DECL_REGISTER (decl))
6196 : 1 : error ("cannot put object with volatile field into register");
6197 : : }
6198 : :
6199 : 7600477 : if (TREE_CODE (decl) != FUNCTION_DECL)
6200 : : {
6201 : : /* If we're building a variable sized type, and we might be
6202 : : reachable other than via the top of the current binding
6203 : : level, then create a new BIND_EXPR so that we deallocate
6204 : : the object at the right time. */
6205 : : /* Note that DECL_SIZE can be null due to errors. */
6206 : 7600410 : if (DECL_SIZE (decl)
6207 : 7600366 : && !TREE_CONSTANT (DECL_SIZE (decl))
6208 : 7607765 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6209 : : {
6210 : 1336 : tree bind;
6211 : 1336 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6212 : 1336 : TREE_SIDE_EFFECTS (bind) = 1;
6213 : 1336 : add_stmt (bind);
6214 : 1336 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6215 : : }
6216 : 7600410 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
6217 : : DECL_EXPR, decl));
6218 : : }
6219 : : }
6220 : :
6221 : :
6222 : 23223785 : if (!DECL_FILE_SCOPE_P (decl))
6223 : : {
6224 : : /* Recompute the RTL of a local array now
6225 : : if it used to be an incomplete type. */
6226 : 7600477 : if (was_incomplete && !is_global_var (decl))
6227 : : {
6228 : : /* If we used it already as memory, it must stay in memory. */
6229 : 5230 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6230 : : /* If it's still incomplete now, no init will save it. */
6231 : 5230 : if (DECL_SIZE (decl) == NULL_TREE)
6232 : 107 : DECL_INITIAL (decl) = NULL_TREE;
6233 : : }
6234 : : }
6235 : : }
6236 : :
6237 : 152854159 : if (TREE_CODE (decl) == TYPE_DECL)
6238 : : {
6239 : 4361057 : if (!DECL_FILE_SCOPE_P (decl)
6240 : 4361057 : && c_type_variably_modified_p (TREE_TYPE (decl)))
6241 : 5321 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6242 : :
6243 : 8338258 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
6244 : : }
6245 : :
6246 : : /* Install a cleanup (aka destructor) if one was given. */
6247 : 152854159 : if (VAR_P (decl) && !TREE_STATIC (decl))
6248 : : {
6249 : 7692186 : tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
6250 : 7692186 : if (attr)
6251 : : {
6252 : 95 : tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
6253 : 95 : tree cleanup_decl = lookup_name (cleanup_id);
6254 : 95 : tree cleanup;
6255 : 95 : vec<tree, va_gc> *v;
6256 : :
6257 : : /* Build "cleanup(&decl)" for the destructor. */
6258 : 95 : cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
6259 : 95 : vec_alloc (v, 1);
6260 : 95 : v->quick_push (cleanup);
6261 : 95 : cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
6262 : 95 : vNULL, cleanup_decl, v, NULL);
6263 : 95 : vec_free (v);
6264 : :
6265 : : /* Don't warn about decl unused; the cleanup uses it. */
6266 : 95 : TREE_USED (decl) = 1;
6267 : 95 : TREE_USED (cleanup_decl) = 1;
6268 : 95 : DECL_READ_P (decl) = 1;
6269 : :
6270 : 95 : push_cleanup (decl, cleanup, false);
6271 : : }
6272 : : }
6273 : :
6274 : 152854159 : if (warn_cxx_compat
6275 : 22389 : && VAR_P (decl)
6276 : 7546 : && !DECL_EXTERNAL (decl)
6277 : 152861262 : && DECL_INITIAL (decl) == NULL_TREE)
6278 : : {
6279 : 2330 : type = strip_array_types (type);
6280 : 2330 : if (TREE_READONLY (decl))
6281 : 6 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6282 : : "uninitialized %<const %D%> is invalid in C++", decl);
6283 : 2324 : else if (RECORD_OR_UNION_TYPE_P (type)
6284 : 2324 : && C_TYPE_FIELDS_READONLY (type))
6285 : 5 : diagnose_uninitialized_cst_member (decl, type);
6286 : : }
6287 : :
6288 : 152854159 : if (flag_openmp
6289 : 430679 : && VAR_P (decl)
6290 : 152877853 : && lookup_attribute ("omp declare target implicit",
6291 : 23694 : DECL_ATTRIBUTES (decl)))
6292 : : {
6293 : 13 : DECL_ATTRIBUTES (decl)
6294 : 13 : = remove_attribute ("omp declare target implicit",
6295 : 13 : DECL_ATTRIBUTES (decl));
6296 : 13 : if (!omp_mappable_type (TREE_TYPE (decl)))
6297 : 9 : error ("%q+D in declare target directive does not have mappable type",
6298 : : decl);
6299 : 4 : else if (!lookup_attribute ("omp declare target",
6300 : 4 : DECL_ATTRIBUTES (decl))
6301 : 8 : && !lookup_attribute ("omp declare target link",
6302 : 4 : DECL_ATTRIBUTES (decl)))
6303 : : {
6304 : 4 : DECL_ATTRIBUTES (decl)
6305 : 4 : = tree_cons (get_identifier ("omp declare target"),
6306 : 4 : NULL_TREE, DECL_ATTRIBUTES (decl));
6307 : 4 : symtab_node *node = symtab_node::get (decl);
6308 : 4 : if (node != NULL)
6309 : : {
6310 : 4 : node->offloadable = 1;
6311 : 4 : if (ENABLE_OFFLOADING)
6312 : : {
6313 : : g->have_offload = true;
6314 : : if (is_a <varpool_node *> (node))
6315 : : vec_safe_push (offload_vars, decl);
6316 : : }
6317 : : }
6318 : : }
6319 : : }
6320 : :
6321 : : /* This is the last point we can lower alignment so give the target the
6322 : : chance to do so. */
6323 : 152854159 : if (VAR_P (decl)
6324 : 8753206 : && !is_global_var (decl)
6325 : 160372964 : && !DECL_HARD_REGISTER (decl))
6326 : 7517776 : targetm.lower_local_decl_alignment (decl);
6327 : :
6328 : 152854159 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
6329 : 152854159 : }
6330 : :
6331 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL.
6332 : : EXPR is NULL or a pointer to an expression that needs to be
6333 : : evaluated for the side effects of array size expressions in the
6334 : : parameters. */
6335 : :
6336 : : tree
6337 : 0 : grokparm (const struct c_parm *parm, tree *expr)
6338 : : {
6339 : 0 : tree attrs = parm->attrs;
6340 : 0 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
6341 : 0 : NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
6342 : :
6343 : 0 : decl_attributes (&decl, attrs, 0);
6344 : :
6345 : 0 : return decl;
6346 : : }
6347 : :
6348 : :
6349 : :
6350 : : /* Given a parsed parameter declaration, decode it into a PARM_DECL
6351 : : and push that on the current scope. EXPR is a pointer to an
6352 : : expression that needs to be evaluated for the side effects of array
6353 : : size expressions in the parameters. */
6354 : :
6355 : : void
6356 : 121124649 : push_parm_decl (const struct c_parm *parm, tree *expr)
6357 : : {
6358 : 121124649 : tree attrs = parm->attrs;
6359 : 121124649 : tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6360 : 121124649 : &attrs, expr, NULL, DEPRECATED_NORMAL);
6361 : 121124649 : if (decl && DECL_P (decl))
6362 : 121124649 : DECL_SOURCE_LOCATION (decl) = parm->loc;
6363 : :
6364 : 121124649 : decl_attributes (&decl, attrs, 0);
6365 : :
6366 : 121124649 : decl = pushdecl (decl);
6367 : :
6368 : 121124649 : finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6369 : 121124649 : }
6370 : :
6371 : : /* Mark all the parameter declarations to date as forward decls.
6372 : : Also diagnose use of this extension. */
6373 : :
6374 : : void
6375 : 42 : mark_forward_parm_decls (void)
6376 : : {
6377 : 42 : struct c_binding *b;
6378 : :
6379 : 42 : if (current_scope->had_forward_parm_decls)
6380 : 12 : warning_at (input_location, OPT_Wmultiple_parameter_fwd_decl_lists,
6381 : : "more than one list of forward declarations of parameters");
6382 : 42 : if (pedantic && !current_scope->had_forward_parm_decls)
6383 : 4 : pedwarn (input_location, OPT_Wpedantic,
6384 : : "ISO C forbids forward parameter declarations");
6385 : :
6386 : 42 : current_scope->had_forward_parm_decls = true;
6387 : :
6388 : 95 : for (b = current_scope->bindings; b; b = b->prev)
6389 : 53 : if (TREE_CODE (b->decl) == PARM_DECL)
6390 : 53 : TREE_ASM_WRITTEN (b->decl) = 1;
6391 : 42 : }
6392 : :
6393 : : /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6394 : : literal, which may be an incomplete array type completed by the
6395 : : initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6396 : : literal. NON_CONST is true if the initializers contain something
6397 : : that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6398 : : it is the (valid) alignment for this compound literal, as specified
6399 : : with _Alignas. SCSPECS are the storage class specifiers (C23) from the
6400 : : compound literal. */
6401 : :
6402 : : tree
6403 : 897924 : build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6404 : : unsigned int alignas_align,
6405 : : struct c_declspecs *scspecs)
6406 : : {
6407 : : /* We do not use start_decl here because we have a type, not a declarator;
6408 : : and do not use finish_decl because the decl should be stored inside
6409 : : the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6410 : 897924 : tree decl;
6411 : 897924 : tree complit;
6412 : 897924 : tree stmt;
6413 : 897924 : bool threadp = scspecs ? scspecs->thread_p : false;
6414 : 299 : enum c_storage_class storage_class = (scspecs
6415 : : ? scspecs->storage_class
6416 : : : csc_none);
6417 : :
6418 : 897924 : if (type == error_mark_node
6419 : 897908 : || init == error_mark_node)
6420 : : return error_mark_node;
6421 : :
6422 : 897855 : if (current_scope == file_scope && storage_class == csc_register)
6423 : : {
6424 : 1 : error_at (loc, "file-scope compound literal specifies %<register%>");
6425 : 1 : storage_class = csc_none;
6426 : : }
6427 : :
6428 : 897855 : if (current_scope != file_scope && threadp && storage_class == csc_none)
6429 : : {
6430 : 2 : error_at (loc, "compound literal implicitly auto and declared %qs",
6431 : 1 : scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6432 : 1 : threadp = false;
6433 : : }
6434 : :
6435 : 897855 : decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6436 : 897855 : DECL_EXTERNAL (decl) = 0;
6437 : 897855 : TREE_PUBLIC (decl) = 0;
6438 : 1795710 : TREE_STATIC (decl) = (current_scope == file_scope
6439 : 897855 : || storage_class == csc_static);
6440 : 897855 : DECL_CONTEXT (decl) = current_function_decl;
6441 : 897855 : TREE_USED (decl) = 1;
6442 : 897855 : DECL_READ_P (decl) = 1;
6443 : 897855 : DECL_ARTIFICIAL (decl) = 1;
6444 : 897855 : DECL_IGNORED_P (decl) = 1;
6445 : 897855 : C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6446 : 1795481 : C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6447 : 897855 : TREE_TYPE (decl) = type;
6448 : 897855 : if (threadp)
6449 : 16 : set_decl_tls_model (decl, decl_default_tls_model (decl));
6450 : 897855 : if (storage_class == csc_register)
6451 : : {
6452 : 23 : C_DECL_REGISTER (decl) = 1;
6453 : 23 : DECL_REGISTER (decl) = 1;
6454 : : }
6455 : 897855 : c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6456 : 897855 : if (alignas_align)
6457 : : {
6458 : 3 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6459 : 3 : DECL_USER_ALIGN (decl) = 1;
6460 : : }
6461 : 897855 : store_init_value (loc, decl, init, NULL_TREE);
6462 : 897855 : if (current_scope != file_scope
6463 : 897604 : && TREE_STATIC (decl)
6464 : 26 : && !TREE_READONLY (decl)
6465 : 15 : && DECL_DECLARED_INLINE_P (current_function_decl)
6466 : 897861 : && DECL_EXTERNAL (current_function_decl))
6467 : 4 : record_inline_static (input_location, current_function_decl,
6468 : : decl, csi_modifiable);
6469 : :
6470 : 897855 : if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6471 : : {
6472 : 251 : int failure = complete_array_type (&TREE_TYPE (decl),
6473 : 251 : DECL_INITIAL (decl), true);
6474 : : /* If complete_array_type returns 3, it means that the initial value of
6475 : : the compound literal is empty. Allow it with a pedwarn; in pre-C23
6476 : : modes, the empty initializer itself has been diagnosed if pedantic so
6477 : : does not need to be diagnosed again here. */
6478 : 251 : gcc_assert (failure == 0 || failure == 3);
6479 : 251 : if (failure == 3 && flag_isoc23)
6480 : 1 : pedwarn (loc, OPT_Wpedantic,
6481 : : "array of unknown size with empty initializer");
6482 : :
6483 : 251 : type = TREE_TYPE (decl);
6484 : 251 : TREE_TYPE (DECL_INITIAL (decl)) = type;
6485 : : }
6486 : :
6487 : 897855 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6488 : : {
6489 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
6490 : 19 : return error_mark_node;
6491 : : }
6492 : :
6493 : 897836 : if (TREE_STATIC (decl)
6494 : 897836 : && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6495 : 0 : return error_mark_node;
6496 : :
6497 : 897836 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6498 : 897836 : complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6499 : 897836 : TREE_SIDE_EFFECTS (complit) = 1;
6500 : :
6501 : 897836 : layout_decl (decl, 0);
6502 : :
6503 : 897836 : if (TREE_STATIC (decl))
6504 : : {
6505 : : /* This decl needs a name for the assembler output. */
6506 : 277 : set_compound_literal_name (decl);
6507 : 277 : DECL_DEFER_OUTPUT (decl) = 1;
6508 : 277 : DECL_COMDAT (decl) = 1;
6509 : 277 : pushdecl (decl);
6510 : 277 : rest_of_decl_compilation (decl, 1, 0);
6511 : : }
6512 : 897559 : else if (current_function_decl && !current_scope->parm_flag)
6513 : 897550 : pushdecl (decl);
6514 : :
6515 : 897836 : if (non_const)
6516 : : {
6517 : 193 : complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6518 : 193 : C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6519 : : }
6520 : :
6521 : : return complit;
6522 : : }
6523 : :
6524 : : /* Check the type of a compound literal. Here we just check that it
6525 : : is valid for C++. */
6526 : :
6527 : : void
6528 : 897924 : check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6529 : : {
6530 : 897924 : if (warn_cxx_compat
6531 : 165 : && (type_name->specs->typespec_kind == ctsk_tagdef
6532 : 164 : || type_name->specs->typespec_kind == ctsk_tagfirstref
6533 : 163 : || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6534 : 2 : warning_at (loc, OPT_Wc___compat,
6535 : : "defining a type in a compound literal is invalid in C++");
6536 : 897924 : }
6537 : :
6538 : : /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6539 : : replacing with appropriate values if they are invalid. */
6540 : :
6541 : : static void
6542 : 51882 : check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6543 : : tree orig_name)
6544 : : {
6545 : 51882 : tree type_mv;
6546 : 51882 : unsigned int max_width;
6547 : 51882 : unsigned HOST_WIDE_INT w;
6548 : 51882 : const char *name = (orig_name
6549 : 94576 : ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6550 : 9188 : : _("<anonymous>"));
6551 : :
6552 : : /* Detect and ignore out of range field width and process valid
6553 : : field widths. */
6554 : 51882 : if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6555 : : {
6556 : 8 : error_at (loc, "bit-field %qs width not an integer constant", name);
6557 : 8 : *width = integer_one_node;
6558 : : }
6559 : : else
6560 : : {
6561 : 51874 : if (TREE_CODE (*width) != INTEGER_CST)
6562 : : {
6563 : 12 : *width = c_fully_fold (*width, false, NULL);
6564 : 12 : if (TREE_CODE (*width) == INTEGER_CST)
6565 : 6 : pedwarn (loc, OPT_Wpedantic,
6566 : : "bit-field %qs width not an integer constant expression",
6567 : : name);
6568 : : }
6569 : 51874 : if (TREE_CODE (*width) != INTEGER_CST)
6570 : : {
6571 : 6 : error_at (loc, "bit-field %qs width not an integer constant", name);
6572 : 6 : *width = integer_one_node;
6573 : : }
6574 : 51874 : constant_expression_warning (*width);
6575 : 51874 : if (tree_int_cst_sgn (*width) < 0)
6576 : : {
6577 : 2 : error_at (loc, "negative width in bit-field %qs", name);
6578 : 2 : *width = integer_one_node;
6579 : : }
6580 : 51872 : else if (integer_zerop (*width) && orig_name)
6581 : : {
6582 : 4 : error_at (loc, "zero width for bit-field %qs", name);
6583 : 4 : *width = integer_one_node;
6584 : : }
6585 : : }
6586 : :
6587 : : /* Detect invalid bit-field type. */
6588 : 51882 : if (TREE_CODE (*type) != INTEGER_TYPE
6589 : : && TREE_CODE (*type) != BOOLEAN_TYPE
6590 : : && TREE_CODE (*type) != ENUMERAL_TYPE
6591 : 51882 : && TREE_CODE (*type) != BITINT_TYPE)
6592 : : {
6593 : 6 : error_at (loc, "bit-field %qs has invalid type", name);
6594 : 6 : *type = unsigned_type_node;
6595 : : }
6596 : :
6597 : 51882 : if (TYPE_WARN_IF_NOT_ALIGN (*type))
6598 : : {
6599 : 1 : error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6600 : : name);
6601 : 1 : *type = unsigned_type_node;
6602 : : }
6603 : :
6604 : 51882 : type_mv = TYPE_MAIN_VARIANT (*type);
6605 : 51882 : if (!in_system_header_at (input_location)
6606 : 29369 : && type_mv != integer_type_node
6607 : 26633 : && type_mv != unsigned_type_node
6608 : 64487 : && type_mv != boolean_type_node)
6609 : 12023 : pedwarn_c90 (loc, OPT_Wpedantic,
6610 : : "type of bit-field %qs is a GCC extension", name);
6611 : :
6612 : 51882 : max_width = TYPE_PRECISION (*type);
6613 : :
6614 : 51882 : if (compare_tree_int (*width, max_width) > 0)
6615 : : {
6616 : 4 : error_at (loc, "width of %qs exceeds its type", name);
6617 : 4 : w = max_width;
6618 : 4 : *width = build_int_cst (integer_type_node, w);
6619 : : }
6620 : : else
6621 : 51878 : w = tree_to_uhwi (*width);
6622 : :
6623 : : /* Truncation of hardbool false and true representation values is always safe:
6624 : : either the values remain different, or we'll report a problem when creating
6625 : : the narrower type. */
6626 : 51882 : if (c_hardbool_type_attr (*type))
6627 : 51882 : return;
6628 : :
6629 : 51601 : if (TREE_CODE (*type) == ENUMERAL_TYPE)
6630 : : {
6631 : 4130 : struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6632 : 4130 : if (!lt
6633 : 4120 : || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6634 : 8246 : || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6635 : 15 : warning_at (loc, 0, "%qs is narrower than values of its type", name);
6636 : : }
6637 : : }
6638 : :
6639 : :
6640 : :
6641 : : /* Print warning about variable length array if necessary. */
6642 : :
6643 : : static void
6644 : 22433 : warn_variable_length_array (tree name, tree size)
6645 : : {
6646 : 22433 : if (TREE_CONSTANT (size))
6647 : : {
6648 : 180 : if (name)
6649 : 168 : pedwarn_c90 (input_location, OPT_Wvla,
6650 : : "ISO C90 forbids array %qE whose size "
6651 : : "cannot be evaluated", name);
6652 : : else
6653 : 12 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6654 : : "whose size cannot be evaluated");
6655 : : }
6656 : : else
6657 : : {
6658 : 22253 : if (name)
6659 : 13784 : pedwarn_c90 (input_location, OPT_Wvla,
6660 : : "ISO C90 forbids variable length array %qE", name);
6661 : : else
6662 : 8469 : pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6663 : : "length array");
6664 : : }
6665 : 22433 : }
6666 : :
6667 : : /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6668 : : considering only those c_declspec_words found in LIST, which
6669 : : must be terminated by cdw_number_of_elements. */
6670 : :
6671 : : static location_t
6672 : 235 : smallest_type_quals_location (const location_t *locations,
6673 : : const c_declspec_word *list)
6674 : : {
6675 : 235 : location_t loc = UNKNOWN_LOCATION;
6676 : 1410 : while (*list != cdw_number_of_elements)
6677 : : {
6678 : 1175 : location_t newloc = locations[*list];
6679 : 1175 : if (loc == UNKNOWN_LOCATION
6680 : 288 : || (newloc != UNKNOWN_LOCATION && newloc < loc))
6681 : 887 : loc = newloc;
6682 : 1175 : list++;
6683 : : }
6684 : :
6685 : 235 : return loc;
6686 : : }
6687 : :
6688 : :
6689 : : /* We attach an artificial TYPE_DECL to pointed-to type
6690 : : and arrange for it to be included in a DECL_EXPR. This
6691 : : forces the sizes evaluation at a safe point and ensures it
6692 : : is not deferred until e.g. within a deeper conditional context.
6693 : :
6694 : : PARM contexts have no enclosing statement list that
6695 : : can hold the DECL_EXPR, so we need to use a BIND_EXPR
6696 : : instead, and add it to the list of expressions that
6697 : : need to be evaluated.
6698 : :
6699 : : TYPENAME contexts do have an enclosing statement list,
6700 : : but it would be incorrect to use it, as the size should
6701 : : only be evaluated if the containing expression is
6702 : : evaluated. We might also be in the middle of an
6703 : : expression with side effects on the pointed-to type size
6704 : : "arguments" prior to the pointer declaration point and
6705 : : the fake TYPE_DECL in the enclosing context would force
6706 : : the size evaluation prior to the side effects. We therefore
6707 : : use BIND_EXPRs in TYPENAME contexts too. */
6708 : : static void
6709 : 6978 : add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p)
6710 : : {
6711 : 6978 : tree bind = NULL_TREE;
6712 : 6978 : if (expr)
6713 : : {
6714 : 1545 : bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE,
6715 : : NULL_TREE);
6716 : 1545 : TREE_SIDE_EFFECTS (bind) = 1;
6717 : 1545 : BIND_EXPR_BODY (bind) = push_stmt_list ();
6718 : 1545 : push_scope ();
6719 : : }
6720 : :
6721 : 6978 : tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6722 : 6978 : pushdecl (decl);
6723 : 6978 : DECL_ARTIFICIAL (decl) = 1;
6724 : 6978 : add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
6725 : 6978 : if (set_name_p)
6726 : 6313 : TYPE_NAME (type) = decl;
6727 : :
6728 : 6978 : if (bind)
6729 : : {
6730 : 1545 : pop_scope ();
6731 : 1545 : BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
6732 : 1545 : if (*expr)
6733 : 1451 : *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
6734 : : else
6735 : 94 : *expr = bind;
6736 : : }
6737 : 6978 : }
6738 : :
6739 : :
6740 : : /* Add attribute "arg spec" to ATTRS corresponding to an array/VLA parameter
6741 : : declared with type TYPE. The attribute has two arguments. The first is
6742 : : a string that encodes the presence of the static keyword. The second is
6743 : : the declared type of the array before adjustment, i.e. as an array type
6744 : : including the outermost bound. */
6745 : :
6746 : : static tree
6747 : 430293 : build_arg_spec_attribute (tree type, bool static_p, tree attrs)
6748 : : {
6749 : 430293 : tree vbchain = tree_cons (NULL_TREE, type, NULL_TREE);
6750 : 430293 : tree acsstr = static_p ? build_string (7, "static") :
6751 : 430171 : build_string (1, "");
6752 : 430293 : tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6753 : 430293 : tree name = get_identifier ("arg spec");
6754 : 430293 : return tree_cons (name, args, attrs);
6755 : : }
6756 : :
6757 : :
6758 : : /* Given declspecs and a declarator,
6759 : : determine the name and type of the object declared
6760 : : and construct a ..._DECL node for it.
6761 : : (In one case we can return a ..._TYPE node instead.
6762 : : For invalid input we sometimes return NULL_TREE.)
6763 : :
6764 : : DECLSPECS is a c_declspecs structure for the declaration specifiers.
6765 : :
6766 : : DECL_CONTEXT says which syntactic context this declaration is in:
6767 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6768 : : FUNCDEF for a function definition. Like NORMAL but a few different
6769 : : error messages in each case. Return value may be zero meaning
6770 : : this definition is too screwy to try to parse.
6771 : : PARM for a parameter declaration (either within a function prototype
6772 : : or before a function body). Make a PARM_DECL, or return void_type_node.
6773 : : TYPENAME if for a typename (in a cast or sizeof).
6774 : : Don't make a DECL node; just return the ..._TYPE node.
6775 : : FIELD for a struct or union field; make a FIELD_DECL.
6776 : : INITIALIZED is true if the decl has an initializer.
6777 : : WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6778 : : representing the width of the bit-field.
6779 : : DECL_ATTRS points to the list of attributes that should be added to this
6780 : : decl. Any nested attributes that belong on the decl itself will be
6781 : : added to this list.
6782 : : If EXPR is not NULL, any expressions that need to be evaluated as
6783 : : part of evaluating variably modified types will be stored in *EXPR.
6784 : : If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6785 : : set to indicate whether operands in *EXPR can be used in constant
6786 : : expressions.
6787 : : DEPRECATED_STATE is a deprecated_states value indicating whether
6788 : : deprecation/unavailability warnings should be suppressed.
6789 : :
6790 : : In the TYPENAME case, DECLARATOR is really an absolute declarator.
6791 : : It may also be so in the PARM case, for a prototype where the
6792 : : argument type is specified but not the name.
6793 : :
6794 : : This function is where the complicated C meanings of `static'
6795 : : and `extern' are interpreted. */
6796 : :
6797 : : static tree
6798 : 306580823 : grokdeclarator (const struct c_declarator *declarator,
6799 : : struct c_declspecs *declspecs,
6800 : : enum decl_context decl_context, bool initialized, tree *width,
6801 : : tree *decl_attrs, tree *expr, bool *expr_const_operands,
6802 : : enum deprecated_states deprecated_state)
6803 : : {
6804 : 306580823 : tree type = declspecs->type;
6805 : 306580823 : bool threadp = declspecs->thread_p;
6806 : 306580823 : bool constexprp = declspecs->constexpr_p;
6807 : 306580823 : enum c_storage_class storage_class = declspecs->storage_class;
6808 : 306580823 : int constp;
6809 : 306580823 : int restrictp;
6810 : 306580823 : int volatilep;
6811 : 306580823 : int atomicp;
6812 : 306580823 : int type_quals = TYPE_UNQUALIFIED;
6813 : 306580823 : tree name = NULL_TREE;
6814 : 306580823 : bool funcdef_flag = false;
6815 : 306580823 : bool funcdef_syntax = false;
6816 : 306580823 : bool size_varies = false;
6817 : 306580823 : bool size_error = false;
6818 : 306580823 : tree decl_attr = declspecs->decl_attr;
6819 : 306580823 : int array_ptr_quals = TYPE_UNQUALIFIED;
6820 : 306580823 : tree array_ptr_attrs = NULL_TREE;
6821 : 306580823 : bool array_parm_static = false;
6822 : 306580823 : bool array_parm_vla_unspec_p = false;
6823 : 306580823 : tree returned_attrs = NULL_TREE;
6824 : 306580823 : tree decl_id_attrs = NULL_TREE;
6825 : 306580823 : bool bitfield = width != NULL;
6826 : 306580823 : tree element_type;
6827 : 306580823 : tree orig_qual_type = NULL;
6828 : 306580823 : size_t orig_qual_indirect = 0;
6829 : 306580823 : struct c_arg_info *arg_info = 0;
6830 : 306580823 : addr_space_t as1, as2, address_space;
6831 : 306580823 : location_t loc = UNKNOWN_LOCATION;
6832 : 306580823 : tree expr_dummy;
6833 : 306580823 : bool expr_const_operands_dummy;
6834 : 306580823 : enum c_declarator_kind first_non_attr_kind;
6835 : 306580823 : unsigned int alignas_align = 0;
6836 : :
6837 : 306580823 : if (type == NULL_TREE)
6838 : : {
6839 : : /* This can occur for auto on a parameter in C23 mode. Set a
6840 : : dummy type here so subsequent code can give diagnostics for
6841 : : this case. */
6842 : 2 : gcc_assert (declspecs->c23_auto_p);
6843 : 2 : gcc_assert (decl_context == PARM);
6844 : 2 : type = declspecs->type = integer_type_node;
6845 : : }
6846 : 306580823 : if (TREE_CODE (type) == ERROR_MARK)
6847 : 29 : return error_mark_node;
6848 : 306580794 : if (expr == NULL)
6849 : : {
6850 : 37785 : expr = &expr_dummy;
6851 : 37785 : expr_dummy = NULL_TREE;
6852 : : }
6853 : 306580794 : if (expr_const_operands == NULL)
6854 : 188287088 : expr_const_operands = &expr_const_operands_dummy;
6855 : :
6856 : 306580794 : if (declspecs->expr)
6857 : : {
6858 : 914 : if (*expr)
6859 : 7 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6860 : : declspecs->expr);
6861 : : else
6862 : 907 : *expr = declspecs->expr;
6863 : : }
6864 : 306580794 : *expr_const_operands = declspecs->expr_const_operands;
6865 : :
6866 : 306580794 : if (decl_context == FUNCDEF)
6867 : 35377292 : funcdef_flag = true, decl_context = NORMAL;
6868 : :
6869 : : /* Look inside a declarator for the name being declared
6870 : : and get it as an IDENTIFIER_NODE, for an error message. */
6871 : 306580794 : {
6872 : 306580794 : const struct c_declarator *decl = declarator;
6873 : :
6874 : 306580794 : first_non_attr_kind = cdk_attrs;
6875 : 375066143 : while (decl)
6876 : 375066143 : switch (decl->kind)
6877 : : {
6878 : 1133526 : case cdk_array:
6879 : 1133526 : loc = decl->id_loc;
6880 : : /* FALL THRU. */
6881 : :
6882 : 68478677 : case cdk_function:
6883 : 68478677 : case cdk_pointer:
6884 : 68478677 : funcdef_syntax = (decl->kind == cdk_function);
6885 : 68478677 : if (first_non_attr_kind == cdk_attrs)
6886 : 66399514 : first_non_attr_kind = decl->kind;
6887 : 68478677 : decl = decl->declarator;
6888 : 68478677 : break;
6889 : :
6890 : 6672 : case cdk_attrs:
6891 : 6672 : decl = decl->declarator;
6892 : 6672 : break;
6893 : :
6894 : 306580794 : case cdk_id:
6895 : 306580794 : loc = decl->id_loc;
6896 : 306580794 : if (decl->u.id.id)
6897 : : name = decl->u.id.id;
6898 : 306580794 : decl_id_attrs = decl->u.id.attrs;
6899 : 306580794 : if (first_non_attr_kind == cdk_attrs)
6900 : 240181280 : first_non_attr_kind = decl->kind;
6901 : : decl = 0;
6902 : : break;
6903 : :
6904 : 0 : default:
6905 : 0 : gcc_unreachable ();
6906 : : }
6907 : 306580794 : if (name == NULL_TREE)
6908 : : {
6909 : 122857012 : gcc_assert (decl_context == PARM
6910 : : || decl_context == TYPENAME
6911 : : || (decl_context == FIELD
6912 : : && declarator->kind == cdk_id));
6913 : 122857012 : gcc_assert (!initialized);
6914 : : }
6915 : : }
6916 : :
6917 : : /* An enum type specifier (": specifier-qualifier-list") may only be
6918 : : specified when the enum is being defined or in an empty
6919 : : declaration of the form "enum identifier enum-type-specifier;".
6920 : : Except for the case of an empty declaration that has additional
6921 : : declaration specifiers, all invalid contexts (declarations that
6922 : : aren't empty, type names, parameter declarations, member
6923 : : declarations) pass through grokdeclarator. */
6924 : 306580794 : if (declspecs->enum_type_specifier_ref_p)
6925 : 6 : error_at (loc, "%<enum%> underlying type may not be specified here");
6926 : :
6927 : : /* A function definition's declarator must have the form of
6928 : : a function declarator. */
6929 : :
6930 : 306580794 : if (funcdef_flag && !funcdef_syntax)
6931 : : return NULL_TREE;
6932 : :
6933 : : /* If this looks like a function definition, make it one,
6934 : : even if it occurs where parms are expected.
6935 : : Then store_parm_decls will reject it and not use it as a parm. */
6936 : 306580763 : if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6937 : 22656 : decl_context = PARM;
6938 : :
6939 : 306580763 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6940 : : {
6941 : 306580739 : if (declspecs->unavailable_p)
6942 : 28 : error_unavailable_use (declspecs->type, declspecs->decl_attr);
6943 : 306580711 : else if (declspecs->deprecated_p
6944 : 46 : && deprecated_state != DEPRECATED_SUPPRESS)
6945 : 38 : warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6946 : : }
6947 : :
6948 : 306580763 : if ((decl_context == NORMAL || decl_context == FIELD)
6949 : 67081090 : && current_scope == file_scope
6950 : 365764801 : && c_type_variably_modified_p (type))
6951 : : {
6952 : 3 : if (name)
6953 : 3 : error_at (loc, "variably modified %qE at file scope", name);
6954 : : else
6955 : 0 : error_at (loc, "variably modified field at file scope");
6956 : 3 : type = integer_type_node;
6957 : : }
6958 : :
6959 : 306580763 : size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6960 : :
6961 : : /* Diagnose defaulting to "int". */
6962 : :
6963 : 306580763 : if (declspecs->default_int_p)
6964 : : {
6965 : : /* Issue a warning if this is an ISO C 99 program or if
6966 : : -Wreturn-type and this is a function, or if -Wimplicit;
6967 : : prefer the former warning since it is more explicit. */
6968 : 9764 : if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6969 : 1229 : && funcdef_flag)
6970 : 702 : warn_about_return_type = 1;
6971 : : else
6972 : : {
6973 : 9062 : if (name)
6974 : 9052 : permerror_opt (loc, OPT_Wimplicit_int,
6975 : : "type defaults to %<int%> in declaration "
6976 : : "of %qE", name);
6977 : : else
6978 : 10 : permerror_opt (loc, OPT_Wimplicit_int,
6979 : : "type defaults to %<int%> in type name");
6980 : : }
6981 : : }
6982 : :
6983 : : /* Adjust the type if a bit-field is being declared,
6984 : : -funsigned-bitfields applied and the type is not explicitly
6985 : : "signed". */
6986 : 306580763 : if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6987 : 40 : && TREE_CODE (type) == INTEGER_TYPE)
6988 : 38 : type = c_common_unsigned_type (type);
6989 : :
6990 : : /* Figure out the type qualifiers for the declaration. There are
6991 : : two ways a declaration can become qualified. One is something
6992 : : like `const int i' where the `const' is explicit. Another is
6993 : : something like `typedef const int CI; CI i' where the type of the
6994 : : declaration contains the `const'. A third possibility is that
6995 : : there is a type qualifier on the element type of a typedefed
6996 : : array type, in which case we should extract that qualifier so
6997 : : that c_apply_type_quals_to_decl receives the full list of
6998 : : qualifiers to work with (C90 is not entirely clear about whether
6999 : : duplicate qualifiers should be diagnosed in this case, but it
7000 : : seems most appropriate to do so). */
7001 : 306580763 : element_type = strip_array_types (type);
7002 : 306580763 : constp = declspecs->const_p + TYPE_READONLY (element_type);
7003 : 306580763 : restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
7004 : 306580763 : volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
7005 : 306580763 : atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
7006 : 306580763 : as1 = declspecs->address_space;
7007 : 306580763 : as2 = TYPE_ADDR_SPACE (element_type);
7008 : 306580763 : address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
7009 : :
7010 : 306580763 : if (constp > 1)
7011 : 25 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
7012 : 306580763 : if (restrictp > 1)
7013 : 6 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
7014 : 306580763 : if (volatilep > 1)
7015 : 15 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
7016 : 306580763 : if (atomicp > 1)
7017 : 9 : pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
7018 : :
7019 : 306580763 : if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
7020 : 0 : error_at (loc, "conflicting named address spaces (%s vs %s)",
7021 : : c_addr_space_name (as1), c_addr_space_name (as2));
7022 : :
7023 : 306580763 : if ((TREE_CODE (type) == ARRAY_TYPE
7024 : 306407243 : || first_non_attr_kind == cdk_array)
7025 : 307646992 : && TYPE_QUALS (element_type))
7026 : : {
7027 : 73 : orig_qual_type = type;
7028 : 73 : type = c_build_qualified_type (type, TYPE_UNQUALIFIED);
7029 : : }
7030 : 306580763 : type_quals = ((constp ? TYPE_QUAL_CONST : 0)
7031 : 306580763 : | (restrictp ? TYPE_QUAL_RESTRICT : 0)
7032 : 306580763 : | (volatilep ? TYPE_QUAL_VOLATILE : 0)
7033 : 306580763 : | (atomicp ? TYPE_QUAL_ATOMIC : 0)
7034 : 306580763 : | ENCODE_QUAL_ADDR_SPACE (address_space));
7035 : 306580763 : if (type_quals != TYPE_QUALS (element_type))
7036 : 12540251 : orig_qual_type = NULL_TREE;
7037 : :
7038 : : /* Applying the _Atomic qualifier to an array type (through the use
7039 : : of typedefs or typeof) must be detected here. If the qualifier
7040 : : is introduced later, any appearance of applying it to an array is
7041 : : actually applying it to an element of that array. */
7042 : 306580763 : if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
7043 : 6 : error_at (loc, "%<_Atomic%>-qualified array type");
7044 : :
7045 : : /* Warn about storage classes that are invalid for certain
7046 : : kinds of declarations (parameters, typenames, etc.). */
7047 : :
7048 : 306580763 : if (funcdef_flag
7049 : 35377261 : && (threadp
7050 : : || constexprp
7051 : 35377259 : || storage_class == csc_auto
7052 : 35377259 : || storage_class == csc_register
7053 : 35377214 : || storage_class == csc_typedef))
7054 : : {
7055 : 47 : if (storage_class == csc_auto)
7056 : 42 : pedwarn (loc,
7057 : 81 : (current_scope == file_scope) ? 0 : OPT_Wpedantic,
7058 : : "function definition declared %<auto%>");
7059 : 50 : if (storage_class == csc_register)
7060 : 3 : error_at (loc, "function definition declared %<register%>");
7061 : 50 : if (storage_class == csc_typedef)
7062 : 3 : error_at (loc, "function definition declared %<typedef%>");
7063 : 50 : if (threadp)
7064 : 2 : error_at (loc, "function definition declared %qs",
7065 : 2 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7066 : 50 : threadp = false;
7067 : : /* The parser ensures a constexpr function definition never
7068 : : reaches here. */
7069 : 50 : gcc_assert (!constexprp);
7070 : 50 : if (storage_class == csc_auto
7071 : 50 : || storage_class == csc_register
7072 : : || storage_class == csc_typedef)
7073 : 55 : storage_class = csc_none;
7074 : : }
7075 : 306580713 : else if (decl_context != NORMAL && (storage_class != csc_none
7076 : 243749517 : || threadp
7077 : 243748934 : || constexprp
7078 : 243748932 : || declspecs->c23_auto_p))
7079 : : {
7080 : 587 : if (decl_context == PARM
7081 : 587 : && storage_class == csc_register
7082 : 568 : && !constexprp
7083 : 566 : && !declspecs->c23_auto_p)
7084 : : ;
7085 : : else
7086 : : {
7087 : 21 : switch (decl_context)
7088 : : {
7089 : 0 : case FIELD:
7090 : 0 : if (name)
7091 : 0 : error_at (loc, "storage class specified for structure "
7092 : : "field %qE", name);
7093 : : else
7094 : 0 : error_at (loc, "storage class specified for structure field");
7095 : : break;
7096 : 21 : case PARM:
7097 : 21 : if (name)
7098 : 7 : error_at (loc, "storage class specified for parameter %qE",
7099 : : name);
7100 : : else
7101 : 14 : error_at (loc, "storage class specified for unnamed parameter");
7102 : : break;
7103 : 0 : default:
7104 : 0 : error_at (loc, "storage class specified for typename");
7105 : 0 : break;
7106 : : }
7107 : 306580763 : storage_class = csc_none;
7108 : 306580763 : threadp = false;
7109 : 306580763 : constexprp = false;
7110 : : }
7111 : : }
7112 : 306580126 : else if (storage_class == csc_extern
7113 : 306580126 : && initialized
7114 : 34538351 : && !funcdef_flag)
7115 : : {
7116 : : /* 'extern' with initialization is invalid if not at file scope. */
7117 : 28 : if (current_scope == file_scope)
7118 : : {
7119 : : /* It is fine to have 'extern const' when compiling at C
7120 : : and C++ intersection. */
7121 : 19 : if (!(warn_cxx_compat && constp))
7122 : 18 : warning_at (loc, 0, "%qE initialized and declared %<extern%>",
7123 : : name);
7124 : : }
7125 : : else
7126 : 9 : error_at (loc, "%qE has both %<extern%> and initializer", name);
7127 : : }
7128 : 306580098 : else if (current_scope == file_scope)
7129 : : {
7130 : 60057426 : if (storage_class == csc_auto)
7131 : 7 : error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
7132 : : name);
7133 : 60057426 : if (pedantic && storage_class == csc_register)
7134 : 4 : pedwarn (input_location, OPT_Wpedantic,
7135 : : "file-scope declaration of %qE specifies %<register%>", name);
7136 : : }
7137 : : else
7138 : : {
7139 : 246522672 : if (storage_class == csc_extern && funcdef_flag)
7140 : 3 : error_at (loc, "nested function %qE declared %<extern%>", name);
7141 : 246522669 : else if (threadp && storage_class == csc_none)
7142 : : {
7143 : 14 : error_at (loc, "function-scope %qE implicitly auto and declared "
7144 : : "%qs", name,
7145 : 7 : declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
7146 : 7 : threadp = false;
7147 : : }
7148 : : }
7149 : :
7150 : : /* Now figure out the structure of the declarator proper.
7151 : : Descend through it, creating more complex types, until we reach
7152 : : the declared identifier (or NULL_TREE, in an absolute declarator).
7153 : : At each stage we maintain an unqualified version of the type
7154 : : together with any qualifiers that should be applied to it with
7155 : : c_build_qualified_type; this way, array types including
7156 : : multidimensional array types are first built up in unqualified
7157 : : form and then the qualified form is created with
7158 : : TYPE_MAIN_VARIANT pointing to the unqualified form. */
7159 : :
7160 : 375066111 : while (declarator && declarator->kind != cdk_id)
7161 : : {
7162 : 68485348 : if (type == error_mark_node)
7163 : : {
7164 : 38 : declarator = declarator->declarator;
7165 : 38 : continue;
7166 : : }
7167 : :
7168 : : /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
7169 : : a cdk_pointer (for *...),
7170 : : a cdk_function (for ...(...)),
7171 : : a cdk_attrs (for nested attributes),
7172 : : or a cdk_id (for the name being declared
7173 : : or the place in an absolute declarator
7174 : : where the name was omitted).
7175 : : For the last case, we have just exited the loop.
7176 : :
7177 : : At this point, TYPE is the type of elements of an array,
7178 : : or for a function to return, or for a pointer to point to.
7179 : : After this sequence of ifs, TYPE is the type of the
7180 : : array or function or pointer, and DECLARATOR has had its
7181 : : outermost layer removed. */
7182 : :
7183 : 68485310 : if (array_ptr_quals != TYPE_UNQUALIFIED
7184 : 68485310 : || array_ptr_attrs != NULL_TREE
7185 : 68485310 : || array_parm_static)
7186 : : {
7187 : : /* Only the innermost declarator (making a parameter be of
7188 : : array type which is converted to pointer type)
7189 : : may have static or type qualifiers. */
7190 : 1 : error_at (loc, "static or type qualifiers in non-parameter array declarator");
7191 : 1 : array_ptr_quals = TYPE_UNQUALIFIED;
7192 : 1 : array_ptr_attrs = NULL_TREE;
7193 : 1 : array_parm_static = false;
7194 : : }
7195 : :
7196 : 68485310 : switch (declarator->kind)
7197 : : {
7198 : 6672 : case cdk_attrs:
7199 : 6672 : {
7200 : : /* A declarator with embedded attributes. */
7201 : 6672 : tree attrs = declarator->u.attrs;
7202 : 6672 : const struct c_declarator *inner_decl;
7203 : 6672 : int attr_flags = 0;
7204 : 6672 : declarator = declarator->declarator;
7205 : : /* Standard attribute syntax precisely defines what entity
7206 : : an attribute in each position appertains to, so only
7207 : : apply laxity about positioning to GNU attribute syntax.
7208 : : Standard attributes applied to a function or array
7209 : : declarator apply exactly to that type; standard
7210 : : attributes applied to the identifier apply to the
7211 : : declaration rather than to the type, and are specified
7212 : : using a cdk_id declarator rather than using
7213 : : cdk_attrs. */
7214 : 6672 : inner_decl = declarator;
7215 : 6672 : while (inner_decl->kind == cdk_attrs)
7216 : 0 : inner_decl = inner_decl->declarator;
7217 : 6672 : if (!cxx11_attribute_p (attrs))
7218 : : {
7219 : 6574 : if (inner_decl->kind == cdk_id)
7220 : : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7221 : : else if (inner_decl->kind == cdk_function)
7222 : : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7223 : : else if (inner_decl->kind == cdk_array)
7224 : 6672 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7225 : : }
7226 : 6672 : attrs = c_warn_type_attributes (type, attrs);
7227 : 6672 : returned_attrs = decl_attributes (&type,
7228 : : chainon (returned_attrs, attrs),
7229 : : attr_flags);
7230 : 6672 : break;
7231 : : }
7232 : 1133516 : case cdk_array:
7233 : 1133516 : {
7234 : 1133516 : tree itype = NULL_TREE;
7235 : 1133516 : tree size = declarator->u.array.dimen;
7236 : : /* The index is a signed object `sizetype' bits wide. */
7237 : 1133516 : tree index_type = c_common_signed_type (sizetype);
7238 : :
7239 : 1133516 : array_ptr_quals = declarator->u.array.quals;
7240 : 1133516 : array_ptr_attrs = declarator->u.array.attrs;
7241 : 1133516 : array_parm_static = declarator->u.array.static_p;
7242 : 1133516 : array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
7243 : :
7244 : 1133516 : declarator = declarator->declarator;
7245 : :
7246 : : /* Check for some types that there cannot be arrays of. */
7247 : :
7248 : 1133516 : if (VOID_TYPE_P (type))
7249 : : {
7250 : 11 : if (name)
7251 : 9 : error_at (loc, "declaration of %qE as array of voids", name);
7252 : : else
7253 : 2 : error_at (loc, "declaration of type name as array of voids");
7254 : 11 : type = error_mark_node;
7255 : : }
7256 : :
7257 : 1133516 : if (TREE_CODE (type) == FUNCTION_TYPE)
7258 : : {
7259 : 3 : if (name)
7260 : 2 : error_at (loc, "declaration of %qE as array of functions",
7261 : : name);
7262 : : else
7263 : 1 : error_at (loc, "declaration of type name as array of "
7264 : : "functions");
7265 : 3 : type = error_mark_node;
7266 : : }
7267 : :
7268 : 21994 : if (pedantic && !in_system_header_at (input_location)
7269 : 1148340 : && flexible_array_type_p (type))
7270 : 20 : pedwarn (loc, OPT_Wpedantic,
7271 : : "invalid use of structure with flexible array member");
7272 : :
7273 : 1133516 : if (size == error_mark_node)
7274 : 119 : type = error_mark_node;
7275 : :
7276 : 1133516 : if (type == error_mark_node)
7277 : 133 : continue;
7278 : :
7279 : 1133383 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7280 : : {
7281 : 0 : type = error_mark_node;
7282 : 0 : continue;
7283 : : }
7284 : :
7285 : : /* If size was specified, set ITYPE to a range-type for
7286 : : that size. Otherwise, ITYPE remains null. finish_decl
7287 : : may figure it out from an initial value. */
7288 : :
7289 : 1133383 : if (size)
7290 : : {
7291 : 964783 : bool size_maybe_const = true;
7292 : 964783 : bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7293 : 964783 : && !TREE_OVERFLOW (size));
7294 : 964783 : bool this_size_varies = false;
7295 : :
7296 : : /* Strip NON_LVALUE_EXPRs since we aren't using as an
7297 : : lvalue. */
7298 : 964792 : STRIP_TYPE_NOPS (size);
7299 : :
7300 : 964783 : if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7301 : : {
7302 : 16 : if (name)
7303 : 14 : error_at (loc, "size of array %qE has non-integer type",
7304 : : name);
7305 : : else
7306 : 2 : error_at (loc,
7307 : : "size of unnamed array has non-integer type");
7308 : 16 : size = integer_one_node;
7309 : 16 : size_int_const = true;
7310 : 16 : size_error = true;
7311 : : }
7312 : : /* This can happen with enum forward declaration. */
7313 : 964767 : else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7314 : : {
7315 : 0 : if (name)
7316 : 0 : error_at (loc, "size of array %qE has incomplete type",
7317 : : name);
7318 : : else
7319 : 0 : error_at (loc, "size of unnamed array has incomplete "
7320 : : "type");
7321 : 0 : size = integer_one_node;
7322 : 0 : size_int_const = true;
7323 : 0 : size_error = true;
7324 : : }
7325 : :
7326 : 964783 : size = c_fully_fold (size, false, &size_maybe_const);
7327 : :
7328 : 964783 : if (pedantic && size_maybe_const && integer_zerop (size))
7329 : : {
7330 : 3 : if (name)
7331 : 3 : pedwarn (loc, OPT_Wpedantic,
7332 : : "ISO C forbids zero-size array %qE", name);
7333 : : else
7334 : 0 : pedwarn (loc, OPT_Wpedantic,
7335 : : "ISO C forbids zero-size array");
7336 : : }
7337 : :
7338 : 964783 : if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7339 : : {
7340 : 942483 : constant_expression_warning (size);
7341 : 942483 : if (tree_int_cst_sgn (size) < 0)
7342 : : {
7343 : 715 : if (name)
7344 : 712 : error_at (loc, "size of array %qE is negative", name);
7345 : : else
7346 : 3 : error_at (loc, "size of unnamed array is negative");
7347 : 715 : size = integer_one_node;
7348 : 715 : size_int_const = true;
7349 : 715 : size_error = true;
7350 : : }
7351 : : /* Handle a size folded to an integer constant but
7352 : : not an integer constant expression. */
7353 : 942483 : if (!size_int_const)
7354 : : {
7355 : : /* If this is a file scope declaration of an
7356 : : ordinary identifier, this is invalid code;
7357 : : diagnosing it here and not subsequently
7358 : : treating the type as variable-length avoids
7359 : : more confusing diagnostics later. */
7360 : 154 : if ((decl_context == NORMAL || decl_context == FIELD)
7361 : 142 : && current_scope == file_scope)
7362 : 14 : pedwarn (input_location, 0,
7363 : : "variably modified %qE at file scope",
7364 : : name);
7365 : : else
7366 : : this_size_varies = size_varies = true;
7367 : 154 : warn_variable_length_array (name, size);
7368 : : }
7369 : : }
7370 : 22300 : else if ((decl_context == NORMAL || decl_context == FIELD)
7371 : 13219 : && current_scope == file_scope)
7372 : : {
7373 : 21 : error_at (loc, "variably modified %qE at file scope", name);
7374 : 21 : size = integer_one_node;
7375 : : }
7376 : : else
7377 : : {
7378 : : /* Make sure the array size remains visibly
7379 : : nonconstant even if it is (eg) a const variable
7380 : : with known value. */
7381 : 22279 : this_size_varies = size_varies = true;
7382 : 22279 : warn_variable_length_array (name, size);
7383 : 22279 : if (sanitize_flags_p (SANITIZE_VLA)
7384 : 178 : && current_function_decl != NULL_TREE
7385 : 22439 : && decl_context == NORMAL)
7386 : : {
7387 : : /* Evaluate the array size only once. */
7388 : 152 : size = save_expr (size);
7389 : 152 : size = c_fully_fold (size, false, NULL);
7390 : 152 : size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7391 : : ubsan_instrument_vla (loc, size),
7392 : : size);
7393 : : }
7394 : : }
7395 : :
7396 : 964783 : if (integer_zerop (size) && !this_size_varies)
7397 : : {
7398 : : /* A zero-length array cannot be represented with
7399 : : an unsigned index type, which is what we'll
7400 : : get with build_index_type. Create an
7401 : : open-ended range instead. */
7402 : 2595 : itype = build_range_type (sizetype, size, NULL_TREE);
7403 : : }
7404 : : else
7405 : : {
7406 : : /* Arrange for the SAVE_EXPR on the inside of the
7407 : : MINUS_EXPR, which allows the -1 to get folded
7408 : : with the +1 that happens when building TYPE_SIZE. */
7409 : 962188 : if (size_varies)
7410 : 22789 : size = save_expr (size);
7411 : 962188 : if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7412 : 140 : size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7413 : : integer_zero_node, size);
7414 : :
7415 : : /* Compute the maximum valid index, that is, size
7416 : : - 1. Do the calculation in index_type, so that
7417 : : if it is a variable the computations will be
7418 : : done in the proper mode. */
7419 : 962188 : itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7420 : : convert (index_type, size),
7421 : : convert (index_type,
7422 : : size_one_node));
7423 : :
7424 : : /* The above overflows when size does not fit
7425 : : in index_type.
7426 : : ??? While a size of INT_MAX+1 technically shouldn't
7427 : : cause an overflow (because we subtract 1), handling
7428 : : this case seems like an unnecessary complication. */
7429 : 962188 : if (TREE_CODE (size) == INTEGER_CST
7430 : 939769 : && !int_fits_type_p (size, index_type))
7431 : : {
7432 : 6 : if (name)
7433 : 5 : error_at (loc, "size of array %qE is too large",
7434 : : name);
7435 : : else
7436 : 1 : error_at (loc, "size of unnamed array is too large");
7437 : 6 : type = error_mark_node;
7438 : 6 : continue;
7439 : : }
7440 : :
7441 : 962182 : itype = build_index_type (itype);
7442 : : }
7443 : 964777 : if (this_size_varies)
7444 : : {
7445 : 22419 : if (TREE_SIDE_EFFECTS (size))
7446 : : {
7447 : 22058 : if (*expr)
7448 : 8173 : *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7449 : : *expr, size);
7450 : : else
7451 : 13885 : *expr = size;
7452 : : }
7453 : 22419 : *expr_const_operands &= size_maybe_const;
7454 : : }
7455 : : }
7456 : 168600 : else if (decl_context == FIELD)
7457 : : {
7458 : 84589 : bool flexible_array_member = false;
7459 : 84589 : if (array_parm_vla_unspec_p)
7460 : : /* Field names can in fact have function prototype
7461 : : scope so [*] is disallowed here through making
7462 : : the field variably modified, not through being
7463 : : something other than a declaration with function
7464 : : prototype scope. */
7465 : : size_varies = true;
7466 : : else
7467 : : {
7468 : : const struct c_declarator *t = declarator;
7469 : 84587 : while (t->kind == cdk_attrs)
7470 : 0 : t = t->declarator;
7471 : 84587 : flexible_array_member = (t->kind == cdk_id);
7472 : : }
7473 : 84587 : if (flexible_array_member
7474 : 84587 : && !in_system_header_at (input_location))
7475 : 83558 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7476 : : "support flexible array members");
7477 : :
7478 : : /* ISO C99 Flexible array members are effectively
7479 : : identical to GCC's zero-length array extension. */
7480 : 84589 : if (flexible_array_member)
7481 : 84567 : itype = build_index_type (NULL_TREE);
7482 : : }
7483 : :
7484 : 1133377 : if (array_parm_vla_unspec_p)
7485 : : {
7486 : : /* C99 6.7.5.2p4 */
7487 : 142 : if (decl_context == TYPENAME)
7488 : 5 : warning (0, "%<[*]%> not in a declaration");
7489 : : size_varies = true;
7490 : : }
7491 : :
7492 : : /* Complain about arrays of incomplete types. */
7493 : 1133377 : if (!COMPLETE_TYPE_P (type))
7494 : : {
7495 : 57 : auto_diagnostic_group d;
7496 : 57 : error_at (loc, "array type has incomplete element type %qT",
7497 : : type);
7498 : : /* See if we can be more helpful. */
7499 : 57 : if (TREE_CODE (type) == ARRAY_TYPE)
7500 : : {
7501 : 29 : if (name)
7502 : 24 : inform (loc, "declaration of %qE as multidimensional "
7503 : : "array must have bounds for all dimensions "
7504 : : "except the first", name);
7505 : : else
7506 : 5 : inform (loc, "declaration of multidimensional array "
7507 : : "must have bounds for all dimensions except "
7508 : : "the first");
7509 : : }
7510 : 57 : type = error_mark_node;
7511 : 57 : }
7512 : : else
7513 : : /* When itype is NULL, a shared incomplete array type is
7514 : : returned for all array of a given type. Elsewhere we
7515 : : make sure we don't complete that type before copying
7516 : : it, but here we want to make sure we don't ever
7517 : : modify the shared type, so we gcc_assert (itype)
7518 : : below. */
7519 : : {
7520 : 1133320 : addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7521 : 1133320 : if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7522 : 0 : type = c_build_qualified_type (type,
7523 : : ENCODE_QUAL_ADDR_SPACE (as));
7524 : 1133320 : if (array_parm_vla_unspec_p)
7525 : 142 : type = c_build_array_type_unspecified (type);
7526 : : else
7527 : 1133178 : type = c_build_array_type (type, itype);
7528 : : }
7529 : :
7530 : 1133377 : if (type != error_mark_node)
7531 : : {
7532 : : /* The GCC extension for zero-length arrays differs from
7533 : : ISO flexible array members in that sizeof yields
7534 : : zero. */
7535 : 1133320 : if (size && integer_zerop (size))
7536 : : {
7537 : 2588 : gcc_assert (itype);
7538 : 2588 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7539 : 2588 : TYPE_SIZE (type) = bitsize_zero_node;
7540 : 2588 : TYPE_SIZE_UNIT (type) = size_zero_node;
7541 : 2588 : SET_TYPE_STRUCTURAL_EQUALITY (type);
7542 : : }
7543 : :
7544 : 1133320 : if (!valid_array_size_p (loc, type, name))
7545 : 33 : type = error_mark_node;
7546 : : }
7547 : :
7548 : 1133377 : if (decl_context != PARM
7549 : 819594 : && (array_ptr_quals != TYPE_UNQUALIFIED
7550 : 819594 : || array_ptr_attrs != NULL_TREE
7551 : 819593 : || array_parm_static))
7552 : : {
7553 : 2 : error_at (loc, "static or type qualifiers in non-parameter "
7554 : : "array declarator");
7555 : 2 : array_ptr_quals = TYPE_UNQUALIFIED;
7556 : 2 : array_ptr_attrs = NULL_TREE;
7557 : 2 : array_parm_static = false;
7558 : : }
7559 : 1133377 : orig_qual_indirect++;
7560 : 1133377 : break;
7561 : : }
7562 : 49429375 : case cdk_function:
7563 : 49429375 : {
7564 : : /* Say it's a definition only for the declarator closest
7565 : : to the identifier, apart possibly from some
7566 : : attributes. */
7567 : 49429375 : bool really_funcdef = false;
7568 : 49429375 : tree arg_types;
7569 : 49429375 : orig_qual_type = NULL_TREE;
7570 : 49429375 : if (funcdef_flag)
7571 : : {
7572 : 35377283 : const struct c_declarator *t = declarator->declarator;
7573 : 35377301 : while (t->kind == cdk_attrs)
7574 : 18 : t = t->declarator;
7575 : 35377283 : really_funcdef = (t->kind == cdk_id);
7576 : : }
7577 : :
7578 : : /* Declaring a function type. Make sure we have a valid
7579 : : type for the function to return. */
7580 : 49429375 : if (type == error_mark_node)
7581 : 0 : continue;
7582 : :
7583 : 49429375 : size_varies = false;
7584 : :
7585 : : /* Warn about some types functions can't return. */
7586 : 49429375 : if (TREE_CODE (type) == FUNCTION_TYPE)
7587 : : {
7588 : 0 : if (name)
7589 : 0 : error_at (loc, "%qE declared as function returning a "
7590 : : "function", name);
7591 : : else
7592 : 0 : error_at (loc, "type name declared as function "
7593 : : "returning a function");
7594 : 0 : type = integer_type_node;
7595 : : }
7596 : 49429375 : if (TREE_CODE (type) == ARRAY_TYPE)
7597 : : {
7598 : 0 : if (name)
7599 : 0 : error_at (loc, "%qE declared as function returning an array",
7600 : : name);
7601 : : else
7602 : 0 : error_at (loc, "type name declared as function returning "
7603 : : "an array");
7604 : 0 : type = integer_type_node;
7605 : : }
7606 : :
7607 : : /* Construct the function type and go to the next
7608 : : inner layer of declarator. */
7609 : 49429375 : arg_info = declarator->u.arg_info;
7610 : 49429375 : arg_types = grokparms (arg_info, really_funcdef);
7611 : :
7612 : : /* Type qualifiers before the return type of the function
7613 : : qualify the return type, not the function type. */
7614 : 49429375 : if (type_quals)
7615 : : {
7616 : 235 : const enum c_declspec_word ignored_quals_list[] =
7617 : : {
7618 : : cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7619 : : cdw_atomic, cdw_number_of_elements
7620 : : };
7621 : 235 : location_t specs_loc
7622 : 235 : = smallest_type_quals_location (declspecs->locations,
7623 : : ignored_quals_list);
7624 : 235 : if (specs_loc == UNKNOWN_LOCATION)
7625 : 139 : specs_loc = declspecs->locations[cdw_typedef];
7626 : 139 : if (specs_loc == UNKNOWN_LOCATION)
7627 : 15 : specs_loc = loc;
7628 : :
7629 : : /* Type qualifiers on a function return type are
7630 : : normally permitted by the standard but have no
7631 : : effect, so give a warning at -Wreturn-type.
7632 : : Qualifiers on a void return type are banned on
7633 : : function definitions in ISO C; GCC used to used
7634 : : them for noreturn functions. The resolution of C11
7635 : : DR#423 means qualifiers (other than _Atomic) are
7636 : : actually removed from the return type when
7637 : : determining the function type. For C23, _Atomic is
7638 : : removed as well. */
7639 : 235 : int quals_used = type_quals;
7640 : 235 : if (flag_isoc23)
7641 : : quals_used = 0;
7642 : 65 : else if (flag_isoc11)
7643 : 31 : quals_used &= TYPE_QUAL_ATOMIC;
7644 : 65 : if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7645 : 5 : pedwarn (specs_loc, 0,
7646 : : "function definition has qualified void "
7647 : : "return type");
7648 : : else
7649 : 230 : warning_at (specs_loc, OPT_Wignored_qualifiers,
7650 : : "type qualifiers ignored on function "
7651 : : "return type");
7652 : :
7653 : : /* Ensure an error for restrict on invalid types; the
7654 : : DR#423 resolution is not entirely clear about
7655 : : this. */
7656 : 235 : if (flag_isoc11
7657 : 201 : && (type_quals & TYPE_QUAL_RESTRICT)
7658 : 241 : && (!POINTER_TYPE_P (type)
7659 : 4 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7660 : 4 : error_at (loc, "invalid use of %<restrict%>");
7661 : 235 : type = c_build_qualified_type (type, quals_used);
7662 : : }
7663 : 49429375 : type_quals = TYPE_UNQUALIFIED;
7664 : :
7665 : 98858750 : type = c_build_function_type (type, arg_types,
7666 : 49429375 : arg_info->no_named_args_stdarg_p);
7667 : 49429375 : declarator = declarator->declarator;
7668 : :
7669 : : /* Set the TYPE_CONTEXTs for each tagged type which is local to
7670 : : the formal parameter list of this FUNCTION_TYPE to point to
7671 : : the FUNCTION_TYPE node itself. */
7672 : 49429375 : {
7673 : 49429375 : c_arg_tag *tag;
7674 : 49429375 : unsigned ix;
7675 : :
7676 : 424495752 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7677 : 134 : TYPE_CONTEXT (tag->type) = type;
7678 : : }
7679 : : break;
7680 : : }
7681 : 17915747 : case cdk_pointer:
7682 : 17915747 : {
7683 : : /* Merge any constancy or volatility into the target type
7684 : : for the pointer. */
7685 : 17915747 : if ((type_quals & TYPE_QUAL_ATOMIC)
7686 : 1839 : && TREE_CODE (type) == FUNCTION_TYPE)
7687 : : {
7688 : 2 : error_at (loc,
7689 : : "%<_Atomic%>-qualified function type");
7690 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7691 : : }
7692 : 17915745 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7693 : 1701 : && type_quals)
7694 : 0 : pedwarn (loc, OPT_Wpedantic,
7695 : : "ISO C forbids qualified function types");
7696 : 17914046 : if (type_quals)
7697 : 5893096 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7698 : : orig_qual_indirect);
7699 : 17915747 : orig_qual_type = NULL_TREE;
7700 : 17915747 : size_varies = false;
7701 : :
7702 : : /* When the pointed-to type involves components of variable size,
7703 : : care must be taken to ensure that the size evaluation code is
7704 : : emitted early enough to dominate all the possible later uses
7705 : : and late enough for the variables on which it depends to have
7706 : : been assigned.
7707 : :
7708 : : This is expected to happen automatically when the pointed-to
7709 : : type has a name/declaration of it's own, but special attention
7710 : : is required if the type is anonymous. */
7711 : 17915747 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7712 : : {
7713 : 5940 : bool bind_p = decl_context == TYPENAME
7714 : : || decl_context == FIELD
7715 : 5940 : || decl_context == PARM;
7716 : 11373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7717 : : }
7718 : :
7719 : 17915747 : type = c_build_pointer_type (type);
7720 : :
7721 : : /* Process type qualifiers (such as const or volatile)
7722 : : that were given inside the `*'. */
7723 : 17915747 : type_quals = declarator->u.pointer_quals;
7724 : :
7725 : 17915747 : declarator = declarator->declarator;
7726 : 17915747 : break;
7727 : : }
7728 : 0 : default:
7729 : 0 : gcc_unreachable ();
7730 : : }
7731 : : }
7732 : 306580763 : *decl_attrs = chainon (returned_attrs, *decl_attrs);
7733 : 306580763 : *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7734 : :
7735 : : /* Now TYPE has the actual type, apart from any qualifiers in
7736 : : TYPE_QUALS. */
7737 : :
7738 : : /* Warn about address space used for things other than static memory or
7739 : : pointers. */
7740 : 306580763 : address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7741 : 306580763 : if (!ADDR_SPACE_GENERIC_P (address_space))
7742 : : {
7743 : 10 : if (decl_context == NORMAL)
7744 : : {
7745 : 10 : switch (storage_class)
7746 : : {
7747 : 0 : case csc_auto:
7748 : 0 : error ("%qs combined with %<auto%> qualifier for %qE",
7749 : : c_addr_space_name (address_space), name);
7750 : 0 : break;
7751 : 0 : case csc_register:
7752 : 0 : error ("%qs combined with %<register%> qualifier for %qE",
7753 : : c_addr_space_name (address_space), name);
7754 : 0 : break;
7755 : 6 : case csc_none:
7756 : 6 : if (current_function_scope)
7757 : : {
7758 : 0 : error ("%qs specified for auto variable %qE",
7759 : : c_addr_space_name (address_space), name);
7760 : 0 : break;
7761 : : }
7762 : : break;
7763 : : case csc_static:
7764 : : case csc_extern:
7765 : : case csc_typedef:
7766 : : break;
7767 : 0 : default:
7768 : 0 : gcc_unreachable ();
7769 : : }
7770 : : }
7771 : 0 : else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7772 : : {
7773 : 0 : if (name)
7774 : 0 : error ("%qs specified for parameter %qE",
7775 : : c_addr_space_name (address_space), name);
7776 : : else
7777 : 0 : error ("%qs specified for unnamed parameter",
7778 : : c_addr_space_name (address_space));
7779 : : }
7780 : 0 : else if (decl_context == FIELD)
7781 : : {
7782 : 0 : if (name)
7783 : 0 : error ("%qs specified for structure field %qE",
7784 : : c_addr_space_name (address_space), name);
7785 : : else
7786 : 0 : error ("%qs specified for structure field",
7787 : : c_addr_space_name (address_space));
7788 : : }
7789 : : }
7790 : :
7791 : : /* Check the type and width of a bit-field. */
7792 : 306580763 : if (bitfield)
7793 : : {
7794 : 51882 : check_bitfield_type_and_width (loc, &type, width, name);
7795 : : /* C11 makes it implementation-defined (6.7.2.1#5) whether
7796 : : atomic types are permitted for bit-fields; we have no code to
7797 : : make bit-field accesses atomic, so disallow them. */
7798 : 51882 : if (type_quals & TYPE_QUAL_ATOMIC)
7799 : : {
7800 : 2 : if (name)
7801 : 1 : error_at (loc, "bit-field %qE has atomic type", name);
7802 : : else
7803 : 1 : error_at (loc, "bit-field has atomic type");
7804 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
7805 : : }
7806 : : }
7807 : :
7808 : : /* Reject invalid uses of _Alignas. */
7809 : 306580763 : if (declspecs->alignas_p)
7810 : : {
7811 : 188 : if (storage_class == csc_typedef)
7812 : 1 : error_at (loc, "alignment specified for typedef %qE", name);
7813 : 187 : else if (storage_class == csc_register)
7814 : 1 : error_at (loc, "alignment specified for %<register%> object %qE",
7815 : : name);
7816 : 186 : else if (decl_context == PARM)
7817 : : {
7818 : 2 : if (name)
7819 : 1 : error_at (loc, "alignment specified for parameter %qE", name);
7820 : : else
7821 : 1 : error_at (loc, "alignment specified for unnamed parameter");
7822 : : }
7823 : 184 : else if (bitfield)
7824 : : {
7825 : 0 : if (name)
7826 : 0 : error_at (loc, "alignment specified for bit-field %qE", name);
7827 : : else
7828 : 0 : error_at (loc, "alignment specified for unnamed bit-field");
7829 : : }
7830 : 184 : else if (TREE_CODE (type) == FUNCTION_TYPE)
7831 : 1 : error_at (loc, "alignment specified for function %qE", name);
7832 : 183 : else if (declspecs->align_log != -1 && TYPE_P (type))
7833 : : {
7834 : 156 : alignas_align = 1U << declspecs->align_log;
7835 : 156 : if (alignas_align < min_align_of_type (type))
7836 : : {
7837 : 26 : if (name)
7838 : 25 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7839 : : "alignment of %qE", name);
7840 : : else
7841 : 1 : error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7842 : : "alignment of unnamed field");
7843 : : alignas_align = 0;
7844 : : }
7845 : : }
7846 : : }
7847 : :
7848 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
7849 : :
7850 : 306580763 : if (storage_class == csc_typedef)
7851 : : {
7852 : 4233105 : tree decl;
7853 : 4233105 : if ((type_quals & TYPE_QUAL_ATOMIC)
7854 : 11187 : && TREE_CODE (type) == FUNCTION_TYPE)
7855 : : {
7856 : 0 : error_at (loc,
7857 : : "%<_Atomic%>-qualified function type");
7858 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7859 : : }
7860 : 4233105 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7861 : 609 : && type_quals)
7862 : 0 : pedwarn (loc, OPT_Wpedantic,
7863 : : "ISO C forbids qualified function types");
7864 : 4232496 : if (type_quals)
7865 : 29852 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7866 : : orig_qual_indirect);
7867 : 8466210 : decl = build_decl (declarator->id_loc,
7868 : 4233105 : TYPE_DECL, declarator->u.id.id, type);
7869 : 4233105 : if (declspecs->explicit_signed_p)
7870 : 348682 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7871 : 4233105 : if (declspecs->inline_p)
7872 : 4 : pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7873 : 4233105 : if (declspecs->noreturn_p)
7874 : 1 : pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7875 : :
7876 : 4233105 : if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7877 : : {
7878 : 1825 : struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7879 : :
7880 : 1825 : if (b != NULL
7881 : 40 : && b->decl != NULL_TREE
7882 : 40 : && (B_IN_CURRENT_SCOPE (b)
7883 : 4 : || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7884 : 1861 : && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7885 : : {
7886 : 7 : auto_diagnostic_group d;
7887 : 8 : if (warning_at (declarator->id_loc, OPT_Wc___compat,
7888 : : "using %qD as both a typedef and a tag is "
7889 : : "invalid in C++", decl)
7890 : 7 : && b->locus != UNKNOWN_LOCATION)
7891 : 6 : inform (b->locus, "originally defined here");
7892 : 7 : }
7893 : : }
7894 : :
7895 : 4233105 : return decl;
7896 : : }
7897 : :
7898 : : /* If this is a type name (such as, in a cast or sizeof),
7899 : : compute the type and return it now. */
7900 : :
7901 : 302347658 : if (decl_context == TYPENAME)
7902 : : {
7903 : : /* Note that the grammar rejects storage classes in typenames
7904 : : and fields. */
7905 : 118352368 : gcc_assert (storage_class == csc_none && !threadp
7906 : : && !declspecs->inline_p && !declspecs->noreturn_p);
7907 : 118352368 : if ((type_quals & TYPE_QUAL_ATOMIC)
7908 : 257 : && TREE_CODE (type) == FUNCTION_TYPE)
7909 : : {
7910 : 0 : error_at (loc,
7911 : : "%<_Atomic%>-qualified function type");
7912 : 0 : type_quals &= ~TYPE_QUAL_ATOMIC;
7913 : : }
7914 : 118352368 : else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7915 : 22 : && type_quals)
7916 : 0 : pedwarn (loc, OPT_Wpedantic,
7917 : : "ISO C forbids const or volatile function types");
7918 : 118352346 : if (type_quals)
7919 : 882 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7920 : : orig_qual_indirect);
7921 : 118352368 : return type;
7922 : : }
7923 : :
7924 : 375067 : if (pedantic && decl_context == FIELD
7925 : 184020069 : && c_type_variably_modified_p (type))
7926 : : {
7927 : : /* C99 6.7.2.1p8 */
7928 : 6 : pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7929 : : "have a variably modified type");
7930 : : }
7931 : :
7932 : : /* Aside from typedefs and type names (handle above),
7933 : : `void' at top level (not within pointer)
7934 : : is allowed only in public variables.
7935 : : We don't complain about parms either, but that is because
7936 : : a better error message can be made later. */
7937 : :
7938 : 183995290 : if (VOID_TYPE_P (type) && decl_context != PARM
7939 : 64 : && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7940 : : && (storage_class == csc_extern
7941 : 27 : || (current_scope == file_scope
7942 : 18 : && !(storage_class == csc_static
7943 : : || storage_class == csc_register)))))
7944 : : {
7945 : 15 : error_at (loc, "variable or field %qE declared void", name);
7946 : 15 : type = integer_type_node;
7947 : : }
7948 : :
7949 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7950 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7951 : :
7952 : 183058997 : {
7953 : 183058997 : tree decl;
7954 : :
7955 : 183058997 : if (decl_context == PARM)
7956 : : {
7957 : 121147305 : tree promoted_type;
7958 : 121147305 : bool array_parameter_p = false;
7959 : :
7960 : : /* A parameter declared as an array of T is really a pointer to T.
7961 : : One declared as a function is really a pointer to a function. */
7962 : :
7963 : 121147305 : if (TREE_CODE (type) == ARRAY_TYPE)
7964 : : {
7965 : 430307 : if (!size_error)
7966 : 430293 : *decl_attrs = build_arg_spec_attribute (type, array_parm_static,
7967 : : *decl_attrs);
7968 : :
7969 : : /* Transfer const-ness of array into that of type pointed to. */
7970 : 430307 : type = TREE_TYPE (type);
7971 : 430307 : if (orig_qual_type != NULL_TREE)
7972 : : {
7973 : 7 : if (orig_qual_indirect == 0)
7974 : 5 : orig_qual_type = TREE_TYPE (orig_qual_type);
7975 : : else
7976 : 2 : orig_qual_indirect--;
7977 : : }
7978 : 430307 : if (type_quals)
7979 : 47239 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
7980 : : orig_qual_indirect);
7981 : :
7982 : : /* The pointed-to type may need a decl expr (see above). */
7983 : 430307 : if (!TYPE_NAME (type) && c_type_variably_modified_p (type))
7984 : : {
7985 : 373 : bool bind_p = decl_context == TYPENAME
7986 : : || decl_context == FIELD
7987 : : || decl_context == PARM;
7988 : 373 : add_decl_expr (loc, type, bind_p ? expr : NULL, true);
7989 : : }
7990 : :
7991 : 430307 : type = c_build_pointer_type (type);
7992 : 430307 : type_quals = array_ptr_quals;
7993 : 430307 : if (type_quals)
7994 : 932 : type = c_build_qualified_type (type, type_quals);
7995 : :
7996 : : /* We don't yet implement attributes in this context. */
7997 : 430307 : if (array_ptr_attrs != NULL_TREE)
7998 : 0 : warning_at (loc, OPT_Wattributes,
7999 : : "attributes in parameter array declarator ignored");
8000 : :
8001 : : size_varies = false;
8002 : : array_parameter_p = true;
8003 : : }
8004 : 120716998 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8005 : : {
8006 : 301 : if (type_quals & TYPE_QUAL_ATOMIC)
8007 : : {
8008 : 1 : error_at (loc,
8009 : : "%<_Atomic%>-qualified function type");
8010 : 1 : type_quals &= ~TYPE_QUAL_ATOMIC;
8011 : : }
8012 : 300 : else if (type_quals)
8013 : 0 : pedwarn (loc, OPT_Wpedantic,
8014 : : "ISO C forbids qualified function types");
8015 : 1 : if (type_quals)
8016 : 0 : type = c_build_qualified_type (type, type_quals);
8017 : 301 : type = c_build_pointer_type (type);
8018 : 301 : type_quals = TYPE_UNQUALIFIED;
8019 : : }
8020 : 120716697 : else if (type_quals)
8021 : 9733804 : type = c_build_qualified_type (type, type_quals);
8022 : :
8023 : 242294610 : decl = build_decl (declarator->id_loc,
8024 : 121147305 : PARM_DECL, declarator->u.id.id, type);
8025 : 121147305 : if (size_varies)
8026 : 42 : C_DECL_VARIABLE_SIZE (decl) = 1;
8027 : 121147305 : C_ARRAY_PARAMETER (decl) = array_parameter_p;
8028 : :
8029 : : /* Compute the type actually passed in the parmlist,
8030 : : for the case where there is no prototype.
8031 : : (For example, shorts and chars are passed as ints.)
8032 : : When there is a prototype, this is overridden later. */
8033 : :
8034 : 121147305 : if (type == error_mark_node)
8035 : : promoted_type = type;
8036 : : else
8037 : 121147221 : promoted_type = c_type_promotes_to (type);
8038 : :
8039 : 121147305 : DECL_ARG_TYPE (decl) = promoted_type;
8040 : 121147305 : if (declspecs->inline_p)
8041 : 4 : pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
8042 : 121147305 : if (declspecs->noreturn_p)
8043 : 1 : pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
8044 : : }
8045 : 62847985 : else if (decl_context == FIELD)
8046 : : {
8047 : : /* Note that the grammar rejects storage classes in typenames
8048 : : and fields. */
8049 : 4249844 : gcc_assert (storage_class == csc_none && !threadp
8050 : : && !declspecs->inline_p && !declspecs->noreturn_p);
8051 : :
8052 : : /* Structure field. It may not be a function. */
8053 : :
8054 : 4249844 : if (TREE_CODE (type) == FUNCTION_TYPE)
8055 : : {
8056 : 0 : error_at (loc, "field %qE declared as a function", name);
8057 : 0 : type = c_build_pointer_type (type);
8058 : : }
8059 : 4249844 : else if (TREE_CODE (type) != ERROR_MARK
8060 : 4249844 : && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
8061 : : {
8062 : 24 : if (name)
8063 : 17 : error_at (loc, "field %qE has incomplete type", name);
8064 : : else
8065 : 7 : error_at (loc, "unnamed field has incomplete type");
8066 : 24 : type = error_mark_node;
8067 : : }
8068 : 4249820 : else if (TREE_CODE (type) == ARRAY_TYPE
8069 : 4249820 : && TYPE_DOMAIN (type) == NULL_TREE)
8070 : : {
8071 : : /* We have a flexible array member through a typedef.
8072 : : Set suitable range. Whether this is a correct position
8073 : : for a flexible array member will be determined elsewhere. */
8074 : 14 : if (!in_system_header_at (input_location))
8075 : 14 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
8076 : : "support flexible array members");
8077 : 14 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8078 : 14 : TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
8079 : : NULL_TREE);
8080 : 14 : if (orig_qual_indirect == 0)
8081 : 4249844 : orig_qual_type = NULL_TREE;
8082 : : }
8083 : 4249844 : if (type != error_mark_node
8084 : 4249844 : && !verify_type_context (loc, TCTX_FIELD, type))
8085 : 0 : type = error_mark_node;
8086 : :
8087 : 4249844 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8088 : : orig_qual_indirect);
8089 : 8499688 : decl = build_decl (declarator->id_loc,
8090 : 4249844 : FIELD_DECL, declarator->u.id.id, type);
8091 : 4249844 : DECL_NONADDRESSABLE_P (decl) = bitfield;
8092 : 4249844 : if (bitfield && !declarator->u.id.id)
8093 : 9188 : DECL_PADDING_P (decl) = 1;
8094 : :
8095 : 4249844 : if (size_varies)
8096 : 646 : C_DECL_VARIABLE_SIZE (decl) = 1;
8097 : : }
8098 : 58598141 : else if (TREE_CODE (type) == FUNCTION_TYPE)
8099 : : {
8100 : 49847846 : if (storage_class == csc_register || threadp || constexprp)
8101 : : {
8102 : 12 : error_at (loc, "invalid storage class for function %qE", name);
8103 : : }
8104 : 49847834 : else if (current_scope != file_scope)
8105 : : {
8106 : : /* Function declaration not at file scope. Storage
8107 : : classes other than `extern' are not allowed, C99
8108 : : 6.7.1p5, and `extern' makes no difference. However,
8109 : : GCC allows 'auto', perhaps with 'inline', to support
8110 : : nested functions. */
8111 : 10528 : if (storage_class == csc_auto)
8112 : 66 : pedwarn (loc, OPT_Wpedantic,
8113 : : "invalid storage class for function %qE", name);
8114 : 10462 : else if (storage_class == csc_static)
8115 : : {
8116 : 20 : error_at (loc, "invalid storage class for function %qE", name);
8117 : 20 : if (funcdef_flag)
8118 : 8 : storage_class = declspecs->storage_class = csc_none;
8119 : : else
8120 : : return NULL_TREE;
8121 : : }
8122 : : }
8123 : :
8124 : 99695668 : decl = build_decl (declarator->id_loc,
8125 : 49847834 : FUNCTION_DECL, declarator->u.id.id, type);
8126 : 49847834 : decl = build_decl_attribute_variant (decl, decl_attr);
8127 : :
8128 : 49847834 : if (type_quals & TYPE_QUAL_ATOMIC)
8129 : : {
8130 : 2 : error_at (loc,
8131 : : "%<_Atomic%>-qualified function type");
8132 : 2 : type_quals &= ~TYPE_QUAL_ATOMIC;
8133 : : }
8134 : 49847832 : else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
8135 : 9 : pedwarn (loc, OPT_Wpedantic,
8136 : : "ISO C forbids qualified function types");
8137 : :
8138 : : /* Every function declaration is an external reference
8139 : : (DECL_EXTERNAL) except for those which are not at file
8140 : : scope and are explicitly declared "auto". This is
8141 : : forbidden by standard C (C99 6.7.1p5) and is interpreted by
8142 : : GCC to signify a forward declaration of a nested function. */
8143 : 49847834 : if (storage_class == csc_auto && current_scope != file_scope)
8144 : 66 : DECL_EXTERNAL (decl) = 0;
8145 : : /* In C99, a function which is declared 'inline' with 'extern'
8146 : : is not an external reference (which is confusing). It
8147 : : means that the later definition of the function must be output
8148 : : in this file, C99 6.7.4p6. In GNU C89, a function declared
8149 : : 'extern inline' is an external reference. */
8150 : 49847768 : else if (declspecs->inline_p && storage_class != csc_static)
8151 : 34542037 : DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
8152 : 34542037 : == flag_gnu89_inline);
8153 : : else
8154 : 15305731 : DECL_EXTERNAL (decl) = !initialized;
8155 : :
8156 : : /* Record absence of global scope for `static' or `auto'. */
8157 : 49847834 : TREE_PUBLIC (decl)
8158 : 49847834 : = !(storage_class == csc_static || storage_class == csc_auto);
8159 : :
8160 : : /* For a function definition, record the argument information
8161 : : block where store_parm_decls will look for it. */
8162 : 49847834 : if (funcdef_flag)
8163 : 35377254 : current_function_arg_info = arg_info;
8164 : :
8165 : 49847834 : if (declspecs->default_int_p)
8166 : 9220 : C_FUNCTION_IMPLICIT_INT (decl) = 1;
8167 : :
8168 : : /* Record presence of `inline' and `_Noreturn', if it is
8169 : : reasonable. */
8170 : 49847834 : if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
8171 : : {
8172 : 48736 : if (declspecs->inline_p)
8173 : 5 : pedwarn (loc, 0, "cannot inline function %<main%>");
8174 : 48736 : if (declspecs->noreturn_p)
8175 : 1 : pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
8176 : : }
8177 : : else
8178 : : {
8179 : 49799098 : if (declspecs->inline_p)
8180 : : /* Record that the function is declared `inline'. */
8181 : 34699206 : DECL_DECLARED_INLINE_P (decl) = 1;
8182 : 49799098 : if (declspecs->noreturn_p)
8183 : : {
8184 : 22955 : if (flag_isoc99)
8185 : 22952 : pedwarn_c99 (loc, OPT_Wpedantic,
8186 : : "ISO C99 does not support %<_Noreturn%>");
8187 : : else
8188 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
8189 : : "ISO C90 does not support %<_Noreturn%>");
8190 : 22955 : TREE_THIS_VOLATILE (decl) = 1;
8191 : : }
8192 : : }
8193 : :
8194 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8195 : : behavior) to create an 'extern' declaration for a
8196 : : function if there is a global declaration that is
8197 : : 'static' and the global declaration is not visible.
8198 : : (If the static declaration _is_ currently visible,
8199 : : the 'extern' declaration is taken to refer to that decl.) */
8200 : 49847834 : if (!initialized
8201 : 14470571 : && TREE_PUBLIC (decl)
8202 : 14462923 : && current_scope != file_scope)
8203 : : {
8204 : 8874 : tree global_decl = identifier_global_value (declarator->u.id.id);
8205 : 8874 : tree visible_decl = lookup_name (declarator->u.id.id);
8206 : :
8207 : 8874 : if (global_decl
8208 : 8874 : && global_decl != visible_decl
8209 : 1713 : && VAR_OR_FUNCTION_DECL_P (global_decl)
8210 : 1713 : && !TREE_PUBLIC (global_decl))
8211 : 2 : error_at (loc, "function previously declared %<static%> "
8212 : : "redeclared %<extern%>");
8213 : : }
8214 : : }
8215 : : else
8216 : : {
8217 : : /* It's a variable. */
8218 : : /* An uninitialized decl with `extern' is a reference. */
8219 : 8750295 : int extern_ref = !initialized && storage_class == csc_extern;
8220 : :
8221 : 8750295 : if (constexprp)
8222 : : {
8223 : : /* The type of a constexpr variable must not be variably
8224 : : modified, volatile, atomic or restrict qualified or
8225 : : have a member with such a qualifier. const
8226 : : qualification is implicitly added, and, at file scope,
8227 : : has internal linkage. */
8228 : 352 : if (c_type_variably_modified_p (type))
8229 : 1 : error_at (loc, "%<constexpr%> object has variably modified "
8230 : : "type");
8231 : 352 : if (type_quals
8232 : 352 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8233 : 9 : error_at (loc, "invalid qualifiers for %<constexpr%> object");
8234 : : else
8235 : : {
8236 : 343 : tree type_no_array = strip_array_types (type);
8237 : 343 : if (RECORD_OR_UNION_TYPE_P (type_no_array)
8238 : 343 : && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8239 : 8 : error_at (loc, "invalid qualifiers for field of "
8240 : : "%<constexpr%> object");
8241 : : }
8242 : 352 : type_quals |= TYPE_QUAL_CONST;
8243 : 352 : if (current_scope == file_scope)
8244 : 294 : storage_class = csc_static;
8245 : : }
8246 : :
8247 : 8750295 : type = c_build_qualified_type (type, type_quals, orig_qual_type,
8248 : : orig_qual_indirect);
8249 : :
8250 : : /* C99 6.2.2p7: It is invalid (compile-time undefined
8251 : : behavior) to create an 'extern' declaration for a
8252 : : variable if there is a global declaration that is
8253 : : 'static' and the global declaration is not visible.
8254 : : (If the static declaration _is_ currently visible,
8255 : : the 'extern' declaration is taken to refer to that decl.) */
8256 : 8750295 : if (extern_ref && current_scope != file_scope)
8257 : : {
8258 : 1569 : tree global_decl = identifier_global_value (declarator->u.id.id);
8259 : 1569 : tree visible_decl = lookup_name (declarator->u.id.id);
8260 : :
8261 : 1569 : if (global_decl
8262 : 1569 : && global_decl != visible_decl
8263 : 282 : && VAR_P (global_decl)
8264 : 282 : && !TREE_PUBLIC (global_decl))
8265 : 8 : error_at (loc, "variable previously declared %<static%> "
8266 : : "redeclared %<extern%>");
8267 : : }
8268 : :
8269 : 17500590 : decl = build_decl (declarator->id_loc,
8270 : 8750295 : VAR_DECL, declarator->u.id.id, type);
8271 : 8750295 : if (size_varies)
8272 : 7411 : C_DECL_VARIABLE_SIZE (decl) = 1;
8273 : 8750295 : if (constexprp)
8274 : 352 : C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8275 : :
8276 : 8750295 : if (declspecs->inline_p)
8277 : 4 : pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8278 : 8750295 : if (declspecs->noreturn_p)
8279 : 1 : pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8280 : :
8281 : : /* At file scope, an initialized extern declaration may follow
8282 : : a static declaration. In that case, DECL_EXTERNAL will be
8283 : : reset later in start_decl. */
8284 : 8750295 : DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8285 : :
8286 : : /* At file scope, the presence of a `static' or `register' storage
8287 : : class specifier, or the absence of all storage class specifiers
8288 : : makes this declaration a definition (perhaps tentative). Also,
8289 : : the absence of `static' makes it public. */
8290 : 8750295 : if (current_scope == file_scope)
8291 : : {
8292 : 1151221 : TREE_PUBLIC (decl) = storage_class != csc_static;
8293 : 1151221 : TREE_STATIC (decl) = !extern_ref;
8294 : : }
8295 : : /* Not at file scope, only `static' makes a static definition. */
8296 : : else
8297 : : {
8298 : 7599074 : TREE_STATIC (decl) = (storage_class == csc_static);
8299 : 7599074 : TREE_PUBLIC (decl) = extern_ref;
8300 : : }
8301 : :
8302 : : // NB: Set a tentative TLS model to avoid tls_model attribute
8303 : : // warnings due to lack of thread storage duration. It will
8304 : : // be updated by c_decl_attributes later.
8305 : 8750295 : if (threadp)
8306 : 2836 : set_decl_tls_model (decl, TLS_MODEL_REAL);
8307 : : }
8308 : :
8309 : 183995278 : if ((storage_class == csc_extern
8310 : 135123488 : || (storage_class == csc_none
8311 : 134674468 : && TREE_CODE (type) == FUNCTION_TYPE
8312 : 800308 : && !funcdef_flag))
8313 : 184298930 : && c_type_variably_modified_p (type))
8314 : : {
8315 : : /* C99 6.7.5.2p2 */
8316 : 6 : if (TREE_CODE (type) == FUNCTION_TYPE)
8317 : 4 : error_at (loc, "non-nested function with variably modified type");
8318 : : else
8319 : 2 : error_at (loc, "object with variably modified type must have "
8320 : : "no linkage");
8321 : : }
8322 : :
8323 : : /* For nested functions disqualify ones taking VLAs by value
8324 : : from inlining since the middle-end cannot deal with this.
8325 : : ??? We should arrange for those to be passed by reference
8326 : : with emitting the copy on the caller side in the frontend. */
8327 : 183995278 : if (storage_class == csc_none
8328 : 134674468 : && TREE_CODE (type) == FUNCTION_TYPE)
8329 : 4154489 : for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8330 : : {
8331 : 3354221 : tree arg = TREE_VALUE (al);
8332 : 3354221 : if (arg != error_mark_node
8333 : 3354221 : && C_TYPE_VARIABLE_SIZE (arg))
8334 : : {
8335 : 40 : DECL_UNINLINABLE (decl) = 1;
8336 : 40 : break;
8337 : : }
8338 : : }
8339 : :
8340 : : /* Record `register' declaration for warnings on &
8341 : : and in case doing stupid register allocation. */
8342 : :
8343 : 183995278 : if (storage_class == csc_register
8344 : 3593 : && TREE_CODE (type) != FUNCTION_TYPE)
8345 : : {
8346 : 3587 : C_DECL_REGISTER (decl) = 1;
8347 : 3587 : DECL_REGISTER (decl) = 1;
8348 : : }
8349 : :
8350 : : /* Record constancy and volatility. */
8351 : 183995278 : c_apply_type_quals_to_decl (type_quals, decl);
8352 : :
8353 : : /* Apply _Alignas specifiers. */
8354 : 183995278 : if (alignas_align)
8355 : : {
8356 : 123 : SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8357 : 123 : DECL_USER_ALIGN (decl) = 1;
8358 : : }
8359 : :
8360 : : /* If a type has volatile components, it should be stored in memory.
8361 : : Otherwise, the fact that those components are volatile
8362 : : will be ignored, and would even crash the compiler.
8363 : : Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8364 : 183995278 : if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8365 : 183995278 : && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8366 : : || TREE_CODE (decl) == RESULT_DECL))
8367 : : {
8368 : : /* It is not an error for a structure with volatile fields to
8369 : : be declared register, but reset DECL_REGISTER since it
8370 : : cannot actually go in a register. */
8371 : 184 : int was_reg = C_DECL_REGISTER (decl);
8372 : 184 : C_DECL_REGISTER (decl) = 0;
8373 : 184 : DECL_REGISTER (decl) = 0;
8374 : 184 : c_mark_addressable (decl);
8375 : 184 : C_DECL_REGISTER (decl) = was_reg;
8376 : : }
8377 : :
8378 : : /* This is the earliest point at which we might know the assembler
8379 : : name of a variable. Thus, if it's known before this, die horribly. */
8380 : 183995278 : gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8381 : : || !DECL_ASSEMBLER_NAME_SET_P (decl));
8382 : :
8383 : 183995278 : if (warn_cxx_compat
8384 : 24521 : && VAR_P (decl)
8385 : 7545 : && TREE_PUBLIC (decl)
8386 : 2802 : && TREE_STATIC (decl)
8387 : 2353 : && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8388 : 2221 : || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8389 : 183995425 : && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8390 : 4 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8391 : : "non-local variable %qD with anonymous type is "
8392 : : "questionable in C++", decl);
8393 : :
8394 : : return decl;
8395 : : }
8396 : : }
8397 : :
8398 : : /* Decode the parameter-list info for a function type or function definition.
8399 : : The argument is the value returned by `get_parm_info' (or made in c-parse.c
8400 : : if there is an identifier list instead of a parameter decl list).
8401 : : These two functions are separate because when a function returns
8402 : : or receives functions then each is called multiple times but the order
8403 : : of calls is different. The last call to `grokparms' is always the one
8404 : : that contains the formal parameter names of a function definition.
8405 : :
8406 : : Return a list of arg types to use in the FUNCTION_TYPE for this function.
8407 : :
8408 : : FUNCDEF_FLAG is true for a function definition, false for
8409 : : a mere declaration. A nonempty identifier-list gets an error message
8410 : : when FUNCDEF_FLAG is false. */
8411 : :
8412 : : static tree
8413 : 49429375 : grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8414 : : {
8415 : 49429375 : tree arg_types = arg_info->types;
8416 : :
8417 : 49429375 : if (funcdef_flag && arg_info->had_vla_unspec)
8418 : : {
8419 : : /* A function definition isn't function prototype scope C99 6.2.1p4. */
8420 : : /* C99 6.7.5.2p4 */
8421 : 2 : error ("%<[*]%> not allowed in other than function prototype scope");
8422 : : }
8423 : :
8424 : 715326 : if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc23
8425 : 49432415 : && !in_system_header_at (input_location))
8426 : 3040 : warning (OPT_Wstrict_prototypes,
8427 : : "function declaration isn%'t a prototype");
8428 : :
8429 : 49429375 : if (arg_types == error_mark_node)
8430 : : /* Don't set TYPE_ARG_TYPES in this case. */
8431 : : return NULL_TREE;
8432 : :
8433 : 98086414 : else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8434 : : {
8435 : 8684 : if (!funcdef_flag)
8436 : : {
8437 : 13 : permerror_opt (input_location,
8438 : 13 : OPT_Wdeclaration_missing_parameter_type,
8439 : : "parameter names (without types) in "
8440 : : "function declaration");
8441 : 13 : arg_info->parms = NULL_TREE;
8442 : : }
8443 : : else
8444 : 8671 : arg_info->parms = arg_info->types;
8445 : :
8446 : 8684 : arg_info->types = NULL_TREE;
8447 : 8684 : return NULL_TREE;
8448 : : }
8449 : : else
8450 : : {
8451 : 49420691 : tree parm, type, typelt;
8452 : 49420691 : unsigned int parmno;
8453 : :
8454 : : /* In C23, convert () to (void). */
8455 : 49420691 : if (flag_isoc23
8456 : 40394165 : && !arg_types
8457 : 764860 : && !arg_info->parms
8458 : 764860 : && !arg_info->no_named_args_stdarg_p)
8459 : : {
8460 : 764719 : arg_types = arg_info->types = void_list_node;
8461 : 764719 : arg_info->c23_empty_parens = 1;
8462 : : }
8463 : :
8464 : : /* If there is a parameter of incomplete type in a definition,
8465 : : this is an error. In a declaration this is valid, and a
8466 : : struct or union type may be completed later, before any calls
8467 : : or definition of the function. In the case where the tag was
8468 : : first declared within the parameter list, a warning has
8469 : : already been given. If a parameter has void type, then
8470 : : this has already received an error (constraint violation in C2Y,
8471 : : previously implicitly undefined behavior). */
8472 : :
8473 : 49420691 : for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8474 : 169608875 : parm;
8475 : 120188184 : parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8476 : : {
8477 : 120188184 : type = TREE_VALUE (typelt);
8478 : 120188184 : if (type == error_mark_node)
8479 : 61 : continue;
8480 : :
8481 : 120188123 : if (!COMPLETE_TYPE_P (type))
8482 : : {
8483 : 40 : if (funcdef_flag)
8484 : : {
8485 : 13 : if (DECL_NAME (parm))
8486 : 13 : error_at (input_location,
8487 : : "parameter %u (%q+D) has incomplete type",
8488 : : parmno, parm);
8489 : : else
8490 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
8491 : : "parameter %u has incomplete type",
8492 : : parmno);
8493 : :
8494 : 13 : TREE_VALUE (typelt) = error_mark_node;
8495 : 13 : TREE_TYPE (parm) = error_mark_node;
8496 : 13 : arg_types = NULL_TREE;
8497 : : }
8498 : : }
8499 : :
8500 : 120188123 : if (DECL_NAME (parm) && TREE_USED (parm))
8501 : 17501 : warn_if_shadowing (parm);
8502 : : }
8503 : : return arg_types;
8504 : : }
8505 : : }
8506 : :
8507 : : /* Allocate and initialize a c_arg_info structure from the parser's
8508 : : obstack. */
8509 : :
8510 : : struct c_arg_info *
8511 : 49429393 : build_arg_info (void)
8512 : : {
8513 : 49429393 : struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8514 : 49429393 : ret->parms = NULL_TREE;
8515 : 49429393 : ret->tags = NULL;
8516 : 49429393 : ret->types = NULL_TREE;
8517 : 49429393 : ret->others = NULL_TREE;
8518 : 49429393 : ret->pending_sizes = NULL;
8519 : 49429393 : ret->had_vla_unspec = 0;
8520 : 49429393 : ret->no_named_args_stdarg_p = 0;
8521 : 49429393 : ret->c23_empty_parens = 0;
8522 : 49429393 : return ret;
8523 : : }
8524 : :
8525 : : /* Take apart the current scope and return a c_arg_info structure with
8526 : : info on a parameter list just parsed.
8527 : :
8528 : : This structure is later fed to 'grokparms' and 'store_parm_decls'.
8529 : :
8530 : : ELLIPSIS being true means the argument list ended in '...' so don't
8531 : : append a sentinel (void_list_node) to the end of the type-list.
8532 : :
8533 : : EXPR is NULL or an expression that needs to be evaluated for the
8534 : : side effects of array size expressions in the parameters. */
8535 : :
8536 : : struct c_arg_info *
8537 : 48648384 : get_parm_info (bool ellipsis, tree expr)
8538 : : {
8539 : 48648384 : struct c_binding *b = current_scope->bindings;
8540 : 48648384 : struct c_arg_info *arg_info = build_arg_info ();
8541 : :
8542 : 48648384 : tree parms = NULL_TREE;
8543 : 48648384 : vec<c_arg_tag, va_gc> *tags = NULL;
8544 : 48648384 : tree types = NULL_TREE;
8545 : 48648384 : tree others = NULL_TREE;
8546 : :
8547 : 48648384 : bool gave_void_only_once_err = false;
8548 : :
8549 : 48648384 : arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8550 : :
8551 : : /* The bindings in this scope must not get put into a block.
8552 : : We will take care of deleting the binding nodes. */
8553 : 48648384 : current_scope->bindings = 0;
8554 : :
8555 : : /* This function is only called if there was *something* on the
8556 : : parameter list. */
8557 : 48648384 : gcc_assert (b);
8558 : :
8559 : : /* A parameter list consisting solely of 'void' indicates that the
8560 : : function takes no arguments. But if the 'void' is qualified
8561 : : (by 'const' or 'volatile'), or has a storage class specifier
8562 : : ('register'), then the behavior is undefined; issue an error.
8563 : : Typedefs for 'void' are OK (see DR#157). */
8564 : 48648384 : if (b->prev == 0 /* one binding */
8565 : 12990307 : && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8566 : 12990307 : && !DECL_NAME (b->decl) /* anonymous */
8567 : 50428375 : && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8568 : : {
8569 : 936179 : if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8570 : 936179 : || C_DECL_REGISTER (b->decl))
8571 : 96 : error_at (b->locus, "%<void%> as only parameter may not be qualified");
8572 : :
8573 : : /* There cannot be an ellipsis. */
8574 : 936179 : if (ellipsis)
8575 : 17 : error_at (b->locus, "%<void%> must be the only parameter");
8576 : :
8577 : 936179 : arg_info->types = void_list_node;
8578 : 936179 : return arg_info;
8579 : : }
8580 : :
8581 : 47712205 : if (!ellipsis)
8582 : 47501694 : types = void_list_node;
8583 : :
8584 : : /* Break up the bindings list into parms, tags, types, and others;
8585 : : apply sanity checks; purge the name-to-decl bindings. */
8586 : 167900836 : while (b)
8587 : : {
8588 : 120188631 : tree decl = b->decl;
8589 : 120188631 : tree type = TREE_TYPE (decl);
8590 : 120188631 : c_arg_tag tag;
8591 : 120188631 : const char *keyword;
8592 : :
8593 : 120188631 : switch (TREE_CODE (decl))
8594 : : {
8595 : 120188307 : case PARM_DECL:
8596 : 120188307 : if (b->id)
8597 : : {
8598 : 116639637 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8599 : 116639637 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8600 : : }
8601 : :
8602 : : /* Check for forward decls that never got their actual decl. */
8603 : 120188307 : if (TREE_ASM_WRITTEN (decl))
8604 : 4 : error_at (b->locus,
8605 : : "parameter %q+D has just a forward declaration", decl);
8606 : : /* Check for (..., void, ...) and named void parameters and issue an
8607 : : error. */
8608 : 120188303 : else if (VOID_TYPE_P (type))
8609 : : {
8610 : 114 : if (!gave_void_only_once_err)
8611 : : {
8612 : 114 : error_at (b->locus,
8613 : : "%<void%> must be the only parameter and unnamed");
8614 : 114 : gave_void_only_once_err = true;
8615 : : }
8616 : : }
8617 : : else
8618 : : {
8619 : : /* Valid parameter, add it to the list. */
8620 : 120188189 : DECL_CHAIN (decl) = parms;
8621 : 120188189 : parms = decl;
8622 : :
8623 : : /* Since there is a prototype, args are passed in their
8624 : : declared types. The back end may override this later. */
8625 : 120188189 : DECL_ARG_TYPE (decl) = type;
8626 : 120188189 : types = tree_cons (0, type, types);
8627 : : }
8628 : : break;
8629 : :
8630 : 21 : case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8631 : 19 : case UNION_TYPE: keyword = "union"; goto tag;
8632 : 94 : case RECORD_TYPE: keyword = "struct"; goto tag;
8633 : 134 : tag:
8634 : : /* Types may not have tag-names, in which case the type
8635 : : appears in the bindings list with b->id NULL. */
8636 : 134 : if (b->id)
8637 : : {
8638 : 92 : gcc_assert (I_TAG_BINDING (b->id) == b);
8639 : 92 : I_TAG_BINDING (b->id) = b->shadowed;
8640 : : }
8641 : :
8642 : : /* Warn about any struct, union or enum tags defined in a
8643 : : parameter list. The scope of such types is limited to
8644 : : the parameter list, which is rarely if ever desirable
8645 : : (it's impossible to call such a function with type-
8646 : : correct arguments). An anonymous union parm type is
8647 : : meaningful as a GNU extension, so don't warn for that. */
8648 : 134 : if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8649 : : {
8650 : 115 : if (b->id)
8651 : : {
8652 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8653 : 92 : if (!flag_isoc23 || !COMPLETE_TYPE_P (decl))
8654 : 72 : warning_at (b->locus, 0,
8655 : : "%<%s %E%> declared inside parameter list"
8656 : : " will not be visible outside of this definition or"
8657 : : " declaration", keyword, b->id);
8658 : : }
8659 : : else
8660 : : /* The %s will be one of 'struct', 'union', or 'enum'. */
8661 : 23 : warning_at (b->locus, 0,
8662 : : "anonymous %s declared inside parameter list"
8663 : : " will not be visible outside of this definition or"
8664 : : " declaration", keyword);
8665 : : }
8666 : :
8667 : 134 : tag.id = b->id;
8668 : 134 : tag.type = decl;
8669 : 134 : vec_safe_push (tags, tag);
8670 : 134 : break;
8671 : :
8672 : 20 : case FUNCTION_DECL:
8673 : : /* FUNCTION_DECLs appear when there is an implicit function
8674 : : declaration in the parameter list. */
8675 : 20 : gcc_assert (b->nested || seen_error ());
8676 : 20 : goto set_shadowed;
8677 : :
8678 : 136 : case CONST_DECL:
8679 : 136 : case TYPE_DECL:
8680 : : /* CONST_DECLs appear here when we have an embedded enum,
8681 : : and TYPE_DECLs appear here when we have an embedded struct
8682 : : or union. No warnings for this - we already warned about the
8683 : : type itself. */
8684 : :
8685 : : /* When we reinsert this decl in the function body, we need
8686 : : to reconstruct whether it was marked as nested. */
8687 : 136 : gcc_assert (!b->nested);
8688 : 136 : DECL_CHAIN (decl) = others;
8689 : 136 : others = decl;
8690 : : /* fall through */
8691 : :
8692 : 190 : case ERROR_MARK:
8693 : 190 : set_shadowed:
8694 : : /* error_mark_node appears here when we have an undeclared
8695 : : variable. Just throw it away. */
8696 : 190 : if (b->id)
8697 : : {
8698 : 56 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8699 : 56 : I_SYMBOL_BINDING (b->id) = b->shadowed;
8700 : : }
8701 : : break;
8702 : :
8703 : : /* Other things that might be encountered. */
8704 : 0 : case LABEL_DECL:
8705 : 0 : case VAR_DECL:
8706 : 0 : default:
8707 : 0 : gcc_unreachable ();
8708 : : }
8709 : :
8710 : 120188631 : b = free_binding_and_advance (b);
8711 : : }
8712 : :
8713 : 47712205 : arg_info->parms = parms;
8714 : 47712205 : arg_info->tags = tags;
8715 : 47712205 : arg_info->types = types;
8716 : 47712205 : arg_info->others = others;
8717 : 47712205 : arg_info->pending_sizes = expr;
8718 : 47712205 : arg_info->no_named_args_stdarg_p = ellipsis && !types;
8719 : 47712205 : return arg_info;
8720 : : }
8721 : :
8722 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8723 : : Define the tag as a forward-reference with location LOC if it is
8724 : : not defined. HAVE_STD_ATTRS says whether any standard attributes
8725 : : were present after the struct, union or enum keyword; ATTRS are the
8726 : : standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8727 : : whether an enum type specifier (": specifier-qualifier-list") is
8728 : : present; if so, this is called before that specifier is parsed, so
8729 : : that the tag is in scope for that specifier. Return a c_typespec
8730 : : structure for the type specifier. */
8731 : :
8732 : : struct c_typespec
8733 : 1069547 : parser_xref_tag (location_t loc, enum tree_code code, tree name,
8734 : : bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8735 : : {
8736 : 1069547 : struct c_typespec ret;
8737 : 1069547 : tree ref;
8738 : 1069547 : location_t refloc;
8739 : :
8740 : 1069547 : ret.expr = NULL_TREE;
8741 : 1069547 : ret.expr_const_operands = true;
8742 : 1069547 : ret.has_enum_type_specifier = has_enum_type_specifier;
8743 : :
8744 : : /* If a cross reference is requested, look up the type already
8745 : : defined for this tag and return it. If an enum type specifier is
8746 : : present, only a definition in the current scope is relevant. */
8747 : :
8748 : 1069547 : ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8749 : :
8750 : : /* If the visble type is still being defined, see if there is
8751 : : an earlier definition (which may be complete). We do not
8752 : : have to loop because nested redefinitions are not allowed. */
8753 : 1069547 : if (flag_isoc23 && ref && C_TYPE_BEING_DEFINED (ref))
8754 : : {
8755 : 84534 : tree vis = previous_tag (ref);
8756 : 84534 : if (vis)
8757 : 16 : ref = vis;
8758 : : }
8759 : :
8760 : : /* If this is the right type of tag, return what we found.
8761 : : (This reference will be shadowed by shadow_tag later if appropriate.)
8762 : : If this is the wrong type of tag, do not return it. If it was the
8763 : : wrong type in the same scope, we will have had an error
8764 : : message already; if in a different scope and declaring
8765 : : a name, pending_xref_error will give an error message; but if in a
8766 : : different scope and not declaring a name, this tag should
8767 : : shadow the previous declaration of a different type of tag, and
8768 : : this would not work properly if we return the reference found.
8769 : : (For example, with "struct foo" in an outer scope, "union foo;"
8770 : : must shadow that tag with a new one of union type.) */
8771 : 2139094 : ret.kind = (ref
8772 : 1069547 : ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8773 : 93416 : : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8774 : 1069547 : if (ref && TREE_CODE (ref) == code)
8775 : : {
8776 : 976097 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8777 : 976097 : if (C_TYPE_DEFINED_IN_STRUCT (ref)
8778 : 5 : && loc != UNKNOWN_LOCATION
8779 : 976102 : && warn_cxx_compat)
8780 : : {
8781 : 5 : auto_diagnostic_group d;
8782 : 5 : switch (code)
8783 : : {
8784 : 2 : case ENUMERAL_TYPE:
8785 : 2 : if (warning_at (loc, OPT_Wc___compat,
8786 : : ("enum type defined in struct or union "
8787 : : "is not visible in C++")))
8788 : 2 : inform (refloc, "enum type defined here");
8789 : : break;
8790 : 2 : case RECORD_TYPE:
8791 : 2 : if (warning_at (loc, OPT_Wc___compat,
8792 : : ("struct defined in struct or union "
8793 : : "is not visible in C++")))
8794 : 2 : inform (refloc, "struct defined here");
8795 : : break;
8796 : 1 : case UNION_TYPE:
8797 : 1 : if (warning_at (loc, OPT_Wc___compat,
8798 : : ("union defined in struct or union "
8799 : : "is not visible in C++")))
8800 : 1 : inform (refloc, "union defined here");
8801 : : break;
8802 : 0 : default:
8803 : 0 : gcc_unreachable();
8804 : : }
8805 : 5 : }
8806 : :
8807 : 976097 : ret.spec = ref;
8808 : 976097 : return ret;
8809 : : }
8810 : :
8811 : : /* If no such tag is yet defined, create a forward-reference node
8812 : : and record it as the "definition".
8813 : : When a real declaration of this type is found,
8814 : : the forward-reference will be altered into a real type. */
8815 : :
8816 : 93450 : ref = make_node (code);
8817 : 93450 : if (flag_isoc23 || code == ENUMERAL_TYPE)
8818 : 71264 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8819 : 71264 : if (code == ENUMERAL_TYPE)
8820 : : {
8821 : : /* Give the type a default layout like unsigned int
8822 : : to avoid crashing if it does not get defined. */
8823 : 249 : SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8824 : 249 : SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8825 : 249 : TYPE_USER_ALIGN (ref) = 0;
8826 : 249 : TYPE_UNSIGNED (ref) = 1;
8827 : 249 : TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8828 : 249 : TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8829 : 249 : TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8830 : 249 : ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8831 : : }
8832 : :
8833 : 93450 : pushtag (loc, name, ref);
8834 : 93450 : decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8835 : 93450 : if (in_underspecified_init)
8836 : 9 : error_at (loc, "%qT declared in underspecified object initializer",
8837 : : ref);
8838 : :
8839 : 93450 : ret.spec = ref;
8840 : 93450 : return ret;
8841 : : }
8842 : :
8843 : : /* Get the struct, enum or union (CODE says which) with tag NAME.
8844 : : Define the tag as a forward-reference if it is not defined.
8845 : : Return a tree for the type. */
8846 : :
8847 : : tree
8848 : 0 : xref_tag (enum tree_code code, tree name)
8849 : : {
8850 : 0 : return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8851 : 0 : false).spec;
8852 : : }
8853 : :
8854 : : /* Make sure that the tag NAME is defined *in the current scope*
8855 : : at least as a forward reference.
8856 : : LOC is the location of the struct's definition.
8857 : : CODE says which kind of tag NAME ought to be.
8858 : :
8859 : : This stores the current value of the file static STRUCT_PARSE_INFO
8860 : : in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8861 : : new c_struct_parse_info structure. The old value of
8862 : : STRUCT_PARSE_INFO is restored in finish_struct. */
8863 : :
8864 : : tree
8865 : 1157722 : start_struct (location_t loc, enum tree_code code, tree name,
8866 : : class c_struct_parse_info **enclosing_struct_parse_info)
8867 : : {
8868 : : /* If there is already a tag defined at this scope
8869 : : (as a forward reference), just return it. */
8870 : :
8871 : 1157722 : tree ref = NULL_TREE;
8872 : 1157722 : location_t refloc = UNKNOWN_LOCATION;
8873 : :
8874 : 1157722 : if (name != NULL_TREE)
8875 : 478061 : ref = lookup_tag (code, name, true, &refloc);
8876 : :
8877 : : /* For C23, even if we already have a completed definition,
8878 : : we do not use it. We will check for consistency later.
8879 : : If we are in a nested redefinition the type is not
8880 : : complete. We will then detect this below. */
8881 : 1157722 : if (flag_isoc23 && ref && TYPE_SIZE (ref))
8882 : : ref = NULL_TREE;
8883 : :
8884 : 1157617 : if (ref && TREE_CODE (ref) == code)
8885 : : {
8886 : 24768 : if (TYPE_STUB_DECL (ref))
8887 : 24768 : refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8888 : :
8889 : 24768 : if (TYPE_SIZE (ref))
8890 : : {
8891 : 11 : auto_diagnostic_group d;
8892 : 11 : if (code == UNION_TYPE)
8893 : 2 : error_at (loc, "redefinition of %<union %E%>", name);
8894 : : else
8895 : 9 : error_at (loc, "redefinition of %<struct %E%>", name);
8896 : 11 : if (refloc != UNKNOWN_LOCATION)
8897 : 11 : inform (refloc, "originally defined here");
8898 : : /* Don't create structures using a name already in use. */
8899 : 11 : ref = NULL_TREE;
8900 : 11 : }
8901 : 24757 : else if (C_TYPE_BEING_DEFINED (ref))
8902 : : {
8903 : 17 : if (code == UNION_TYPE)
8904 : 1 : error_at (loc, "nested redefinition of %<union %E%>", name);
8905 : : else
8906 : 16 : error_at (loc, "nested redefinition of %<struct %E%>", name);
8907 : : /* Don't bother to report "originally defined here" for a
8908 : : nested redefinition; the original definition should be
8909 : : obvious. */
8910 : : /* Don't create structures that contain themselves. */
8911 : : ref = NULL_TREE;
8912 : : }
8913 : : }
8914 : :
8915 : : /* Otherwise create a forward-reference just so the tag is in scope. */
8916 : :
8917 : 24754 : if (ref == NULL_TREE || TREE_CODE (ref) != code)
8918 : : {
8919 : 1132982 : ref = make_node (code);
8920 : 1132982 : if (flag_isoc23)
8921 : 905140 : SET_TYPE_STRUCTURAL_EQUALITY (ref);
8922 : 1132982 : pushtag (loc, name, ref);
8923 : : }
8924 : :
8925 : 1157722 : C_TYPE_BEING_DEFINED (ref) = 1;
8926 : 2345612 : for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8927 : 1187890 : TYPE_PACKED (v) = flag_pack_struct;
8928 : :
8929 : 1157722 : *enclosing_struct_parse_info = struct_parse_info;
8930 : 1157722 : struct_parse_info = new c_struct_parse_info ();
8931 : 1157722 : struct_parse_info->refloc = refloc;
8932 : :
8933 : : /* FIXME: This will issue a warning for a use of a type defined
8934 : : within a statement expr used within sizeof, et. al. This is not
8935 : : terribly serious as C++ doesn't permit statement exprs within
8936 : : sizeof anyhow. */
8937 : 1157722 : if (warn_cxx_compat
8938 : 574 : && (in_sizeof || in_typeof || in_alignof || in_countof))
8939 : 10 : warning_at (loc, OPT_Wc___compat,
8940 : : "defining type in %qs expression is invalid in C++",
8941 : : (in_sizeof ? "sizeof"
8942 : 4 : : in_typeof ? "typeof"
8943 : 1 : : in_alignof ? "alignof"
8944 : : : "_Countof"));
8945 : :
8946 : 1157722 : if (in_underspecified_init)
8947 : 18 : error_at (loc, "%qT defined in underspecified object initializer", ref);
8948 : :
8949 : 1157722 : return ref;
8950 : : }
8951 : :
8952 : : /* Process the specs, declarator and width (NULL if omitted)
8953 : : of a structure component, returning a FIELD_DECL node.
8954 : : WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8955 : : DECL_ATTRS is as for grokdeclarator.
8956 : :
8957 : : LOC is the location of the structure component.
8958 : :
8959 : : This is done during the parsing of the struct declaration.
8960 : : The FIELD_DECL nodes are chained together and the lot of them
8961 : : are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8962 : :
8963 : : tree
8964 : 4249873 : grokfield (location_t loc,
8965 : : struct c_declarator *declarator, struct c_declspecs *declspecs,
8966 : : tree width, tree *decl_attrs, tree *expr)
8967 : : {
8968 : 4249873 : tree value;
8969 : :
8970 : 4249873 : if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8971 : 19813 : && width == NULL_TREE)
8972 : : {
8973 : : /* This is an unnamed decl.
8974 : :
8975 : : If we have something of the form "union { list } ;" then this
8976 : : is the anonymous union extension. Similarly for struct.
8977 : :
8978 : : If this is something of the form "struct foo;", then
8979 : : If MS or Plan 9 extensions are enabled, this is handled as
8980 : : an anonymous struct.
8981 : : Otherwise this is a forward declaration of a structure tag.
8982 : :
8983 : : If this is something of the form "foo;" and foo is a TYPE_DECL, then
8984 : : If foo names a structure or union without a tag, then this
8985 : : is an anonymous struct (this is permitted by C11).
8986 : : If MS or Plan 9 extensions are enabled and foo names a
8987 : : structure, then again this is an anonymous struct.
8988 : : Otherwise this is an error.
8989 : :
8990 : : Oh what a horrid tangled web we weave. I wonder if MS consciously
8991 : : took this from Plan 9 or if it was an accident of implementation
8992 : : that took root before someone noticed the bug... */
8993 : :
8994 : 10625 : tree type = declspecs->type;
8995 : 10625 : bool ok = false;
8996 : :
8997 : 10625 : if (RECORD_OR_UNION_TYPE_P (type)
8998 : 10619 : && (flag_ms_extensions
8999 : 10609 : || flag_plan9_extensions
9000 : 10594 : || !declspecs->typedef_p))
9001 : : {
9002 : 10612 : if (flag_ms_extensions || flag_plan9_extensions)
9003 : : ok = true;
9004 : 10587 : else if (TYPE_NAME (type) == NULL)
9005 : : ok = true;
9006 : : else
9007 : : ok = false;
9008 : : }
9009 : : if (!ok)
9010 : : {
9011 : 29 : pedwarn (loc, 0, "declaration does not declare anything");
9012 : 29 : return NULL_TREE;
9013 : : }
9014 : 10596 : if (flag_isoc99)
9015 : 10574 : pedwarn_c99 (loc, OPT_Wpedantic,
9016 : : "ISO C99 doesn%'t support unnamed structs/unions");
9017 : : else
9018 : 22 : pedwarn_c99 (loc, OPT_Wpedantic,
9019 : : "ISO C90 doesn%'t support unnamed structs/unions");
9020 : : }
9021 : :
9022 : 4249844 : value = grokdeclarator (declarator, declspecs, FIELD, false,
9023 : 4249844 : width ? &width : NULL, decl_attrs, expr, NULL,
9024 : : DEPRECATED_NORMAL);
9025 : :
9026 : 4249844 : finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
9027 : 4249844 : DECL_INITIAL (value) = width;
9028 : 4249844 : if (width)
9029 : 51882 : SET_DECL_C_BIT_FIELD (value);
9030 : :
9031 : 4249844 : if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
9032 : : {
9033 : : /* If we currently have a binding for this field, set the
9034 : : in_struct field in the binding, so that we warn about lookups
9035 : : which find it. */
9036 : 1312 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
9037 : 1312 : if (b != NULL)
9038 : : {
9039 : : /* If the in_struct field is not yet set, push it on a list
9040 : : to be cleared when this struct is finished. */
9041 : 93 : if (!b->in_struct)
9042 : : {
9043 : 91 : struct_parse_info->fields.safe_push (b);
9044 : 91 : b->in_struct = 1;
9045 : : }
9046 : : }
9047 : : }
9048 : :
9049 : : return value;
9050 : : }
9051 : :
9052 : : /* Subroutine of detect_field_duplicates: return whether X and Y,
9053 : : which are both fields in the same struct, have duplicate field
9054 : : names. */
9055 : :
9056 : : static bool
9057 : 4238561 : is_duplicate_field (tree x, tree y)
9058 : : {
9059 : 4238561 : if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
9060 : : return true;
9061 : :
9062 : : /* When using -fplan9-extensions, an anonymous field whose name is a
9063 : : typedef can duplicate a field name. */
9064 : 4238560 : if (flag_plan9_extensions
9065 : 4238560 : && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
9066 : : {
9067 : 0 : tree xt, xn, yt, yn;
9068 : :
9069 : 0 : xt = TREE_TYPE (x);
9070 : 0 : if (DECL_NAME (x) != NULL_TREE)
9071 : 0 : xn = DECL_NAME (x);
9072 : 0 : else if (RECORD_OR_UNION_TYPE_P (xt)
9073 : 0 : && TYPE_NAME (xt) != NULL_TREE
9074 : 0 : && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
9075 : 0 : xn = DECL_NAME (TYPE_NAME (xt));
9076 : : else
9077 : : xn = NULL_TREE;
9078 : :
9079 : 0 : yt = TREE_TYPE (y);
9080 : 0 : if (DECL_NAME (y) != NULL_TREE)
9081 : 0 : yn = DECL_NAME (y);
9082 : 0 : else if (RECORD_OR_UNION_TYPE_P (yt)
9083 : 0 : && TYPE_NAME (yt) != NULL_TREE
9084 : 0 : && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
9085 : 0 : yn = DECL_NAME (TYPE_NAME (yt));
9086 : : else
9087 : : yn = NULL_TREE;
9088 : :
9089 : 0 : if (xn != NULL_TREE && xn == yn)
9090 : 0 : return true;
9091 : : }
9092 : :
9093 : : return false;
9094 : : }
9095 : :
9096 : : /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
9097 : : to HTAB, giving errors for any duplicates. */
9098 : :
9099 : : static void
9100 : 79269 : detect_field_duplicates_hash (tree fieldlist,
9101 : : hash_table<nofree_ptr_hash <tree_node> > *htab)
9102 : : {
9103 : 79269 : tree x, y;
9104 : 79269 : tree_node **slot;
9105 : :
9106 : 1394750 : for (x = fieldlist; x ; x = DECL_CHAIN (x))
9107 : 1315481 : if ((y = DECL_NAME (x)) != NULL_TREE)
9108 : : {
9109 : 1295708 : slot = htab->find_slot (y, INSERT);
9110 : 1295708 : if (*slot)
9111 : : {
9112 : 15 : error ("duplicate member %q+D", x);
9113 : 15 : DECL_NAME (x) = NULL_TREE;
9114 : : }
9115 : 1295708 : *slot = y;
9116 : : }
9117 : 19773 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9118 : : {
9119 : 11299 : detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
9120 : :
9121 : : /* When using -fplan9-extensions, an anonymous field whose
9122 : : name is a typedef can duplicate a field name. */
9123 : 11299 : if (flag_plan9_extensions
9124 : 15 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9125 : 11313 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
9126 : : {
9127 : 2 : tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
9128 : 2 : slot = htab->find_slot (xn, INSERT);
9129 : 2 : if (*slot)
9130 : 0 : error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
9131 : 2 : *slot = xn;
9132 : : }
9133 : : }
9134 : 79269 : }
9135 : :
9136 : : /* Generate an error for any duplicate field names in FIELDLIST. Munge
9137 : : the list such that this does not present a problem later. */
9138 : :
9139 : : static void
9140 : 1157816 : detect_field_duplicates (tree fieldlist)
9141 : : {
9142 : 1157816 : tree x, y;
9143 : 1157816 : int timeout = 10;
9144 : :
9145 : : /* If the struct is the list of instance variables of an Objective-C
9146 : : class, then we need to check all the instance variables of
9147 : : superclasses when checking for duplicates (since you can't have
9148 : : an instance variable in a subclass with the same name as an
9149 : : instance variable in a superclass). We pass on this job to the
9150 : : Objective-C compiler. objc_detect_field_duplicates() will return
9151 : : false if we are not checking the list of instance variables and
9152 : : the C frontend should proceed with the standard field duplicate
9153 : : checks. If we are checking the list of instance variables, the
9154 : : ObjC frontend will do the check, emit the errors if needed, and
9155 : : then return true. */
9156 : 1157816 : if (c_dialect_objc ())
9157 : 0 : if (objc_detect_field_duplicates (false))
9158 : : return;
9159 : :
9160 : : /* First, see if there are more than "a few" fields.
9161 : : This is trivially true if there are zero or one fields. */
9162 : 1157816 : if (!fieldlist || !DECL_CHAIN (fieldlist))
9163 : : return;
9164 : : x = fieldlist;
9165 : 3412914 : do {
9166 : 3412914 : timeout--;
9167 : 3412914 : if (DECL_NAME (x) == NULL_TREE
9168 : 3412914 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9169 : : timeout = 0;
9170 : 3412914 : x = DECL_CHAIN (x);
9171 : 3412914 : } while (timeout > 0 && x);
9172 : :
9173 : : /* If there were "few" fields and no anonymous structures or unions,
9174 : : avoid the overhead of allocating a hash table. Instead just do
9175 : : the nested traversal thing. */
9176 : 954511 : if (timeout > 0)
9177 : : {
9178 : 2768973 : for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
9179 : : /* When using -fplan9-extensions, we can have duplicates
9180 : : between typedef names and fields. */
9181 : 1882432 : if (DECL_NAME (x)
9182 : 1882432 : || (flag_plan9_extensions
9183 : 0 : && DECL_NAME (x) == NULL_TREE
9184 : 0 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9185 : 0 : && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
9186 : 0 : && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
9187 : : {
9188 : 6120457 : for (y = fieldlist; y != x; y = TREE_CHAIN (y))
9189 : 4238561 : if (is_duplicate_field (y, x))
9190 : : {
9191 : 1 : error ("duplicate member %q+D", x);
9192 : 1 : DECL_NAME (x) = NULL_TREE;
9193 : : }
9194 : : }
9195 : : }
9196 : : else
9197 : : {
9198 : 67970 : hash_table<nofree_ptr_hash <tree_node> > htab (37);
9199 : 67970 : detect_field_duplicates_hash (fieldlist, &htab);
9200 : 67970 : }
9201 : : }
9202 : :
9203 : : /* Finish up struct info used by -Wc++-compat. */
9204 : :
9205 : : static void
9206 : 575 : warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
9207 : : location_t record_loc)
9208 : : {
9209 : 575 : unsigned int ix;
9210 : 575 : tree x;
9211 : 575 : struct c_binding *b;
9212 : :
9213 : 575 : if (fieldlist == NULL_TREE)
9214 : : {
9215 : 9 : if (code == RECORD_TYPE)
9216 : 6 : warning_at (record_loc, OPT_Wc___compat,
9217 : : "empty struct has size 0 in C, size 1 in C++");
9218 : : else
9219 : 3 : warning_at (record_loc, OPT_Wc___compat,
9220 : : "empty union has size 0 in C, size 1 in C++");
9221 : : }
9222 : :
9223 : : /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
9224 : : the current struct. We do this now at the end of the struct
9225 : : because the flag is used to issue visibility warnings, and we
9226 : : only want to issue those warnings if the type is referenced
9227 : : outside of the struct declaration. */
9228 : 623 : FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
9229 : 48 : C_TYPE_DEFINED_IN_STRUCT (x) = 1;
9230 : :
9231 : : /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
9232 : : typedefs used when declaring fields in this struct. If the name
9233 : : of any of the fields is also a typedef name then the struct would
9234 : : not parse in C++, because the C++ lookup rules say that the
9235 : : typedef name would be looked up in the context of the struct, and
9236 : : would thus be the field rather than the typedef. */
9237 : 575 : if (!struct_parse_info->typedefs_seen.is_empty ()
9238 : 67 : && fieldlist != NULL_TREE)
9239 : : {
9240 : : /* Use a hash_set<tree> using the name of the typedef. We can use
9241 : : a hash_set<tree> because identifiers are interned. */
9242 : 67 : hash_set<tree> tset;
9243 : :
9244 : 254 : FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
9245 : 120 : tset.add (DECL_NAME (x));
9246 : :
9247 : 387 : for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
9248 : : {
9249 : 320 : if (DECL_NAME (x) != NULL_TREE
9250 : 320 : && tset.contains (DECL_NAME (x)))
9251 : : {
9252 : 2 : warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9253 : : "using %qD as both field and typedef name is "
9254 : : "invalid in C++", x);
9255 : : /* FIXME: It would be nice to report the location where
9256 : : the typedef name is used. */
9257 : : }
9258 : : }
9259 : 67 : }
9260 : :
9261 : : /* For each field which has a binding and which was not defined in
9262 : : an enclosing struct, clear the in_struct field. */
9263 : 666 : FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9264 : 91 : b->in_struct = 0;
9265 : 575 : }
9266 : :
9267 : : /* Function to help qsort sort FIELD_DECLs by name order. */
9268 : :
9269 : : static int
9270 : 19951556 : field_decl_cmp (const void *x_p, const void *y_p)
9271 : : {
9272 : 19951556 : const tree *const x = (const tree *) x_p;
9273 : 19951556 : const tree *const y = (const tree *) y_p;
9274 : :
9275 : 19951556 : if (DECL_NAME (*x) == DECL_NAME (*y))
9276 : : /* A nontype is "greater" than a type. */
9277 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9278 : 19951556 : if (DECL_NAME (*x) == NULL_TREE)
9279 : : return -1;
9280 : 19951556 : if (DECL_NAME (*y) == NULL_TREE)
9281 : : return 1;
9282 : 19951556 : if (DECL_NAME (*x) < DECL_NAME (*y))
9283 : 9824459 : return -1;
9284 : : return 1;
9285 : : }
9286 : :
9287 : : /* If this structure or union completes the type of any previous
9288 : : variable declaration, lay it out and output its rtl. */
9289 : : static void
9290 : 1333630 : finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9291 : : {
9292 : 1333779 : for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9293 : : {
9294 : 149 : tree decl = TREE_VALUE (x);
9295 : 149 : if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9296 : 0 : layout_array_type (TREE_TYPE (decl));
9297 : 149 : if (TREE_CODE (decl) != TYPE_DECL)
9298 : : {
9299 : 149 : relayout_decl (decl);
9300 : 149 : if (c_dialect_objc ())
9301 : 0 : objc_check_decl (decl);
9302 : 149 : rest_of_decl_compilation (decl, toplevel, 0);
9303 : : }
9304 : : }
9305 : 1333630 : }
9306 : :
9307 : : /* Determine whether the FIELD_DECL X is a flexible array member according to
9308 : : the following info:
9309 : : A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9310 : : B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9311 : : or "[1]";
9312 : : C. flag_strict_flex_arrays;
9313 : : D. the attribute strict_flex_array that is attached to the field
9314 : : if presenting.
9315 : : Return TRUE when it's a flexible array member, FALSE otherwise. */
9316 : :
9317 : : static bool
9318 : 4249924 : is_flexible_array_member_p (bool is_last_field,
9319 : : tree x)
9320 : : {
9321 : : /* If not the last field, return false. */
9322 : 4249924 : if (!is_last_field)
9323 : : return false;
9324 : :
9325 : : /* If not an array field, return false. */
9326 : 1622119 : if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9327 : : return false;
9328 : :
9329 : 522588 : bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9330 : 522588 : bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9331 : 522588 : bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x));
9332 : :
9333 : 522588 : unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9334 : :
9335 : 522588 : switch (strict_flex_array_level)
9336 : : {
9337 : : case 0:
9338 : : /* Default, all trailing arrays are flexible array members. */
9339 : : return true;
9340 : 77 : case 1:
9341 : : /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9342 : 77 : if (is_one_element_array)
9343 : : return true;
9344 : : /* FALLTHROUGH. */
9345 : 141 : case 2:
9346 : : /* Level 2: all "[0]", and "[]" are flexible array members. */
9347 : 141 : if (is_zero_length_array)
9348 : : return true;
9349 : : /* FALLTHROUGH. */
9350 : 201 : case 3:
9351 : : /* Level 3: Only "[]" are flexible array members. */
9352 : 201 : if (is_flexible_array)
9353 : : return true;
9354 : : break;
9355 : 0 : default:
9356 : 0 : gcc_unreachable ();
9357 : : }
9358 : : return false;
9359 : : }
9360 : :
9361 : : /* Recompute TYPE_CANONICAL for variants of the type including qualified
9362 : : versions of the type and related pointer types after an aggregate type
9363 : : has been finalized.
9364 : : Will not update array types, pointers to array types, function
9365 : : types and other derived types created while the type was still
9366 : : incomplete, those will remain TYPE_STRUCTURAL_EQUALITY_P. */
9367 : :
9368 : : static void
9369 : 1169505 : c_update_type_canonical (tree t)
9370 : : {
9371 : 1169505 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
9372 : 2365244 : for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
9373 : : {
9374 : 1195739 : if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
9375 : : {
9376 : 26234 : if (!TYPE_QUALS (x))
9377 : 25612 : TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
9378 : : else
9379 : : {
9380 : 622 : tree
9381 : 622 : c = c_build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
9382 : 622 : if (TYPE_STRUCTURAL_EQUALITY_P (c))
9383 : : {
9384 : 610 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
9385 : 610 : if (c == x)
9386 : 610 : TYPE_CANONICAL (x) = x;
9387 : : else
9388 : : {
9389 : : /* build_qualified_type for this function unhelpfully
9390 : : moved c from some later spot in TYPE_MAIN_VARIANT (t)
9391 : : chain to right after t (or created it there). Move
9392 : : it right before x and process c and then x. */
9393 : 0 : gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
9394 : 0 : if (l != t)
9395 : : {
9396 : 0 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
9397 : 0 : TYPE_NEXT_VARIANT (l) = c;
9398 : 0 : TYPE_NEXT_VARIANT (c) = x;
9399 : : }
9400 : 0 : TYPE_CANONICAL (c) = c;
9401 : 0 : x = c;
9402 : : }
9403 : : }
9404 : : else
9405 : 12 : TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
9406 : : }
9407 : : }
9408 : 1169505 : else if (x != t)
9409 : 0 : continue;
9410 : 1263576 : for (tree p = TYPE_POINTER_TO (x); p; p = TYPE_NEXT_PTR_TO (p))
9411 : : {
9412 : 67837 : if (!TYPE_STRUCTURAL_EQUALITY_P (p))
9413 : 0 : continue;
9414 : 67837 : if (TYPE_CANONICAL (x) != x || TYPE_REF_CAN_ALIAS_ALL (p))
9415 : 2498 : TYPE_CANONICAL (p)
9416 : 4996 : = c_build_pointer_type_for_mode (TYPE_CANONICAL (x), TYPE_MODE (p),
9417 : : false);
9418 : : else
9419 : 65339 : TYPE_CANONICAL (p) = p;
9420 : 67837 : c_update_type_canonical (p);
9421 : : }
9422 : : }
9423 : 1169505 : }
9424 : :
9425 : : /* Verify the argument of the counted_by attribute of each of the
9426 : : FIELDS_WITH_COUNTED_BY is a valid field of the containing structure,
9427 : : STRUCT_TYPE, Report error and remove the corresponding attribute
9428 : : when it's not. */
9429 : :
9430 : : static void
9431 : 157 : verify_counted_by_attribute (tree struct_type,
9432 : : auto_vec<tree> *fields_with_counted_by)
9433 : : {
9434 : 642 : for (tree field_decl : *fields_with_counted_by)
9435 : : {
9436 : 171 : tree attr_counted_by = lookup_attribute ("counted_by",
9437 : 171 : DECL_ATTRIBUTES (field_decl));
9438 : :
9439 : 171 : if (!attr_counted_by)
9440 : 0 : continue;
9441 : :
9442 : : /* If there is an counted_by attribute attached to the field,
9443 : : verify it. */
9444 : :
9445 : 171 : tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by));
9446 : :
9447 : : /* Verify the argument of the attrbute is a valid field of the
9448 : : containing structure. */
9449 : :
9450 : 171 : tree counted_by_field = lookup_field (struct_type, fieldname);
9451 : :
9452 : : /* Error when the field is not found in the containing structure and
9453 : : remove the corresponding counted_by attribute from the field_decl. */
9454 : 171 : if (!counted_by_field)
9455 : : {
9456 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9457 : : "argument %qE to the %<counted_by%> attribute"
9458 : : " is not a field declaration in the same structure"
9459 : : " as %qD", fieldname, field_decl);
9460 : 4 : DECL_ATTRIBUTES (field_decl)
9461 : 8 : = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl));
9462 : : }
9463 : : else
9464 : : /* Error when the field is not with an integer type. */
9465 : : {
9466 : 209 : while (TREE_CHAIN (counted_by_field))
9467 : 42 : counted_by_field = TREE_CHAIN (counted_by_field);
9468 : 167 : tree real_field = TREE_VALUE (counted_by_field);
9469 : :
9470 : 167 : if (!INTEGRAL_TYPE_P (TREE_TYPE (real_field)))
9471 : : {
9472 : 4 : error_at (DECL_SOURCE_LOCATION (field_decl),
9473 : : "argument %qE to the %<counted_by%> attribute"
9474 : : " is not a field declaration with an integer type",
9475 : : fieldname);
9476 : 4 : DECL_ATTRIBUTES (field_decl)
9477 : 8 : = remove_attribute ("counted_by",
9478 : 4 : DECL_ATTRIBUTES (field_decl));
9479 : : }
9480 : : }
9481 : : }
9482 : 157 : }
9483 : :
9484 : : /* TYPE is a struct or union that we're applying may_alias to after the body is
9485 : : parsed. Fixup any POINTER_TO types. */
9486 : :
9487 : : static void
9488 : 353 : c_fixup_may_alias (tree type)
9489 : : {
9490 : 418 : for (tree t = TYPE_POINTER_TO (type); t; t = TYPE_NEXT_PTR_TO (t))
9491 : 190 : for (tree v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
9492 : 125 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
9493 : 353 : }
9494 : :
9495 : : /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9496 : : LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9497 : : FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9498 : : ATTRIBUTES are attributes to be applied to the structure.
9499 : :
9500 : : ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9501 : : the struct was started. */
9502 : :
9503 : : tree
9504 : 1157816 : finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9505 : : class c_struct_parse_info *enclosing_struct_parse_info,
9506 : : tree *expr)
9507 : : {
9508 : 1157816 : tree x;
9509 : 1157816 : bool toplevel = file_scope == current_scope;
9510 : :
9511 : : /* If this type was previously laid out as a forward reference,
9512 : : make sure we lay it out again. */
9513 : :
9514 : 1157816 : TYPE_SIZE (t) = NULL_TREE;
9515 : :
9516 : 1157816 : decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9517 : :
9518 : 1157816 : if (pedantic)
9519 : : {
9520 : 7369 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9521 : : {
9522 : 7350 : if (DECL_NAME (x) != NULL_TREE)
9523 : : break;
9524 : 23 : if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9525 : : break;
9526 : : }
9527 : :
9528 : 7353 : if (x == NULL_TREE)
9529 : : {
9530 : 19 : if (TREE_CODE (t) == UNION_TYPE)
9531 : : {
9532 : 5 : if (fieldlist)
9533 : 2 : pedwarn (loc, OPT_Wpedantic, "union has no named members");
9534 : : else
9535 : 3 : pedwarn (loc, OPT_Wpedantic, "union has no members");
9536 : : }
9537 : : else
9538 : : {
9539 : 14 : if (fieldlist)
9540 : 3 : pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9541 : : else
9542 : 11 : pedwarn (loc, OPT_Wpedantic, "struct has no members");
9543 : : }
9544 : : }
9545 : : }
9546 : :
9547 : : /* Install struct as DECL_CONTEXT of each field decl.
9548 : : Also process specified field sizes, found in the DECL_INITIAL,
9549 : : storing 0 there after the type has been changed to precision equal
9550 : : to its width, rather than the precision of the specified standard
9551 : : type. (Correct layout requires the original type to have been preserved
9552 : : until now.) */
9553 : :
9554 : 1157816 : bool saw_named_field = false;
9555 : 1157816 : auto_vec<tree> fields_with_counted_by;
9556 : 5407780 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9557 : : {
9558 : : /* Whether this field is the last field of the structure or union.
9559 : : for UNION, any field is the last field of it. */
9560 : 4249964 : bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9561 : 4249964 : || (TREE_CODE (t) == UNION_TYPE);
9562 : :
9563 : 4249964 : if (TREE_TYPE (x) == error_mark_node)
9564 : 40 : continue;
9565 : :
9566 : 4249924 : DECL_CONTEXT (x) = t;
9567 : :
9568 : 4249924 : tree t1 = strip_array_types (TREE_TYPE (x));
9569 : : /* If any field is const, the structure type is pseudo-const. */
9570 : 4249924 : if (TREE_READONLY (x))
9571 : 64244 : C_TYPE_FIELDS_READONLY (t) = 1;
9572 : : else
9573 : : {
9574 : : /* A field that is pseudo-const makes the structure likewise. */
9575 : 4185680 : if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9576 : 641 : C_TYPE_FIELDS_READONLY (t) = 1;
9577 : : }
9578 : :
9579 : : /* Any field that is volatile means variables of this type must be
9580 : : treated in some ways as volatile. */
9581 : 4249924 : if (TREE_THIS_VOLATILE (x))
9582 : : {
9583 : 367 : C_TYPE_FIELDS_VOLATILE (t) = 1;
9584 : 367 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9585 : : }
9586 : :
9587 : : /* Any field that is volatile, restrict-qualified or atomic
9588 : : means the type cannot be used for a constexpr object. */
9589 : 4249924 : if (TYPE_QUALS (t1)
9590 : 4249924 : & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9591 : 1279 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9592 : 4248645 : else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9593 : 31 : C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9594 : :
9595 : : /* Any field of nominal variable size implies structure is too. */
9596 : 4249924 : if (C_DECL_VARIABLE_SIZE (x))
9597 : 649 : C_TYPE_VARIABLE_SIZE (t) = 1;
9598 : :
9599 : : /* If any field is variably modified, record this fact. */
9600 : 4249924 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9601 : 707 : C_TYPE_VARIABLY_MODIFIED (t) = 1;
9602 : :
9603 : 4249924 : if (DECL_C_BIT_FIELD (x))
9604 : : {
9605 : 51891 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9606 : 51891 : DECL_SIZE (x) = bitsize_int (width);
9607 : 51891 : DECL_BIT_FIELD (x) = 1;
9608 : : }
9609 : :
9610 : 4249924 : if (TYPE_PACKED (t)
9611 : 4249924 : && (DECL_BIT_FIELD (x)
9612 : 3235 : || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9613 : 4442 : DECL_PACKED (x) = 1;
9614 : :
9615 : : /* Detect flexible array member in an invalid context. */
9616 : 4249924 : if (c_flexible_array_member_type_p (TREE_TYPE (x)))
9617 : : {
9618 : 84580 : if (TREE_CODE (t) == UNION_TYPE)
9619 : 29 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9620 : : "flexible array member in union is a GCC extension");
9621 : 84551 : else if (!is_last_field)
9622 : : {
9623 : 3 : error_at (DECL_SOURCE_LOCATION (x),
9624 : : "flexible array member not at end of struct");
9625 : 3 : TREE_TYPE (x) = error_mark_node;
9626 : : }
9627 : 84548 : else if (!saw_named_field)
9628 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9629 : : "flexible array member in a struct with no named "
9630 : : "members is a GCC extension");
9631 : :
9632 : : /* If there is a counted_by attribute attached to this field,
9633 : : record it here and do more verification later after the
9634 : : whole structure is complete. */
9635 : 84580 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9636 : 77 : fields_with_counted_by.safe_push (x);
9637 : : }
9638 : :
9639 : 4249924 : if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
9640 : : /* If there is a counted_by attribute attached to this field,
9641 : : record it here and do more verification later after the
9642 : : whole structure is complete. */
9643 : 701332 : if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x)))
9644 : 94 : fields_with_counted_by.safe_push (x);
9645 : :
9646 : 25324 : if (pedantic && TREE_CODE (t) == RECORD_TYPE
9647 : 4270986 : && flexible_array_type_p (TREE_TYPE (x)))
9648 : 18 : pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9649 : : "invalid use of structure with flexible array member");
9650 : :
9651 : : /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9652 : 4249924 : DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9653 : :
9654 : : /* Set TYPE_INCLUDES_FLEXARRAY for the context of x, t.
9655 : : when x is an array and is the last field.
9656 : : There is only one last_field for a structure type, but there might
9657 : : be multiple last_fields for a union type, therefore we should ORed
9658 : : the result for multiple last_fields. */
9659 : 4249924 : if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
9660 : 692676 : TYPE_INCLUDES_FLEXARRAY (t)
9661 : 1385352 : |= is_last_field && c_flexible_array_member_type_p (TREE_TYPE (x));
9662 : : /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t
9663 : : when x is an union or record and is the last field. */
9664 : 3557248 : else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9665 : 458357 : TYPE_INCLUDES_FLEXARRAY (t)
9666 : 916714 : |= is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x));
9667 : :
9668 : 4249924 : if (warn_flex_array_member_not_at_end
9669 : 56 : && !is_last_field
9670 : 22 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
9671 : 4249936 : && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)))
9672 : 5 : warning_at (DECL_SOURCE_LOCATION (x),
9673 : 5 : OPT_Wflex_array_member_not_at_end,
9674 : : "structure containing a flexible array member"
9675 : : " is not at the end of another structure");
9676 : :
9677 : 4249924 : if (DECL_NAME (x)
9678 : 4249924 : || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9679 : : saw_named_field = true;
9680 : :
9681 : 7807172 : if (AGGREGATE_TYPE_P (TREE_TYPE (x))
9682 : 4708281 : && TYPE_TYPELESS_STORAGE (TREE_TYPE (x)))
9683 : 441036 : TYPE_TYPELESS_STORAGE (t) = true;
9684 : : }
9685 : :
9686 : 1157816 : detect_field_duplicates (fieldlist);
9687 : :
9688 : : /* Now we have the nearly final fieldlist. Record it,
9689 : : then lay out the structure or union (including the fields). */
9690 : :
9691 : 1157816 : TYPE_FIELDS (t) = fieldlist;
9692 : :
9693 : 1157816 : maybe_apply_pragma_scalar_storage_order (t);
9694 : :
9695 : 1157816 : layout_type (t);
9696 : :
9697 : 1157816 : if (TYPE_SIZE_UNIT (t)
9698 : 1157816 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9699 : 1157222 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9700 : 2315038 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9701 : 1 : error ("type %qT is too large", t);
9702 : :
9703 : : /* Give bit-fields their proper types and rewrite the type of array fields
9704 : : with scalar component if the enclosing type has reverse storage order. */
9705 : 5407780 : for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9706 : : {
9707 : 4249964 : if (TREE_CODE (field) == FIELD_DECL
9708 : 4249964 : && DECL_INITIAL (field)
9709 : 4301865 : && TREE_TYPE (field) != error_mark_node)
9710 : : {
9711 : 51891 : unsigned HOST_WIDE_INT width
9712 : 51891 : = tree_to_uhwi (DECL_INITIAL (field));
9713 : 51891 : tree type = TREE_TYPE (field);
9714 : 51891 : if (width != TYPE_PRECISION (type))
9715 : : {
9716 : 37741 : if (TREE_CODE (type) == BITINT_TYPE
9717 : 37811 : && width >= (TYPE_UNSIGNED (type) ? 1 : 2))
9718 : 161 : TREE_TYPE (field)
9719 : 322 : = build_bitint_type (width, TYPE_UNSIGNED (type));
9720 : : else
9721 : 37580 : TREE_TYPE (field)
9722 : 37580 : = c_build_bitfield_integer_type (width,
9723 : 37580 : TYPE_UNSIGNED (type));
9724 : 37741 : if (tree attr = c_hardbool_type_attr (type))
9725 : 281 : decl_attributes (&TREE_TYPE (field),
9726 : : copy_list (attr),
9727 : : 0, NULL_TREE);
9728 : 37741 : SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9729 : : }
9730 : 51891 : DECL_INITIAL (field) = NULL_TREE;
9731 : : }
9732 : 4198073 : else if (TYPE_REVERSE_STORAGE_ORDER (t)
9733 : 693 : && TREE_CODE (field) == FIELD_DECL
9734 : 4198766 : && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9735 : : {
9736 : 106 : tree ftype = TREE_TYPE (field);
9737 : 106 : tree ctype = strip_array_types (ftype);
9738 : 106 : if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9739 : : {
9740 : 93 : tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9741 : 93 : tree *typep = &fmain_type;
9742 : 95 : do {
9743 : 95 : *typep = build_distinct_type_copy (*typep);
9744 : 95 : TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9745 : 95 : typep = &TREE_TYPE (*typep);
9746 : 95 : } while (TREE_CODE (*typep) == ARRAY_TYPE);
9747 : 93 : TREE_TYPE (field)
9748 : 186 : = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9749 : : }
9750 : : }
9751 : :
9752 : : /* Warn on problematic type punning for storage order purposes. */
9753 : 4249964 : if (TREE_CODE (t) == UNION_TYPE
9754 : 835299 : && TREE_CODE (field) == FIELD_DECL
9755 : 5085263 : && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9756 : : {
9757 : 415064 : tree ftype = TREE_TYPE (field);
9758 : 415064 : if (TREE_CODE (ftype) == ARRAY_TYPE)
9759 : 305600 : ftype = strip_array_types (ftype);
9760 : 415064 : if (RECORD_OR_UNION_TYPE_P (ftype)
9761 : 415064 : && TYPE_REVERSE_STORAGE_ORDER (ftype)
9762 : 109958 : != TYPE_REVERSE_STORAGE_ORDER (t))
9763 : 1 : warning_at (DECL_SOURCE_LOCATION (field),
9764 : 1 : OPT_Wscalar_storage_order,
9765 : : "type punning toggles scalar storage order");
9766 : : }
9767 : : }
9768 : :
9769 : : /* Now we have the truly final field list.
9770 : : Store it in this type and in the variants. */
9771 : :
9772 : 1157816 : TYPE_FIELDS (t) = fieldlist;
9773 : :
9774 : : /* If there are lots of fields, sort so we can look through them fast.
9775 : : We arbitrarily consider 16 or more elts to be "a lot". */
9776 : :
9777 : 1157816 : {
9778 : 1157816 : int len = 0;
9779 : :
9780 : 4971841 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9781 : : {
9782 : 3841404 : if (len > 15 || DECL_NAME (x) == NULL)
9783 : : break;
9784 : 3814025 : len += 1;
9785 : : }
9786 : :
9787 : 1157816 : if (len > 15)
9788 : : {
9789 : 22317 : tree *field_array;
9790 : 22317 : struct lang_type *space;
9791 : 22317 : struct sorted_fields_type *space2;
9792 : :
9793 : 22317 : len += list_length (x);
9794 : :
9795 : : /* Use the same allocation policy here that make_node uses, to
9796 : : ensure that this lives as long as the rest of the struct decl.
9797 : : All decls in an inline function need to be saved. */
9798 : :
9799 : 22317 : space = ((struct lang_type *)
9800 : 22317 : ggc_internal_cleared_alloc (c_dialect_objc ()
9801 : : ? sizeof (struct lang_type)
9802 : : : offsetof (struct lang_type,
9803 : : info)));
9804 : 22317 : space2 = ((sorted_fields_type *)
9805 : 44634 : ggc_internal_alloc (sizeof (struct sorted_fields_type)
9806 : 22317 : + len * sizeof (tree)));
9807 : :
9808 : 22317 : len = 0;
9809 : 22317 : space->s = space2;
9810 : 22317 : field_array = &space2->elts[0];
9811 : 781749 : for (x = fieldlist; x; x = DECL_CHAIN (x))
9812 : : {
9813 : 762632 : field_array[len++] = x;
9814 : :
9815 : : /* If there is anonymous struct or union, break out of the loop. */
9816 : 762632 : if (DECL_NAME (x) == NULL)
9817 : : break;
9818 : : }
9819 : : /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9820 : 22317 : if (x == NULL)
9821 : : {
9822 : 19117 : TYPE_LANG_SPECIFIC (t) = space;
9823 : 19117 : TYPE_LANG_SPECIFIC (t)->s->len = len;
9824 : 19117 : field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9825 : 19117 : qsort (field_array, len, sizeof (tree), field_decl_cmp);
9826 : : }
9827 : : }
9828 : : }
9829 : :
9830 : : /* If this was supposed to be a transparent union, but we can't
9831 : : make it one, warn and turn off the flag. */
9832 : 1157816 : if (TREE_CODE (t) == UNION_TYPE
9833 : 368776 : && TYPE_TRANSPARENT_AGGR (t)
9834 : 1157859 : && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9835 : : {
9836 : 13 : TYPE_TRANSPARENT_AGGR (t) = 0;
9837 : 13 : warning_at (loc, 0, "union cannot be made transparent");
9838 : : }
9839 : :
9840 : : /* Check for consistency with previous definition. */
9841 : 1157816 : if (flag_isoc23 && NULL != enclosing_struct_parse_info)
9842 : : {
9843 : 890935 : tree vistype = previous_tag (t);
9844 : 890935 : if (vistype
9845 : 124 : && TREE_CODE (vistype) == TREE_CODE (t)
9846 : 891059 : && !C_TYPE_BEING_DEFINED (vistype))
9847 : : {
9848 : 104 : TYPE_STUB_DECL (t) = TYPE_STUB_DECL (vistype);
9849 : 104 : if (c_type_variably_modified_p (t))
9850 : : {
9851 : 7 : error ("redefinition of struct or union %qT with variably "
9852 : : "modified type", t);
9853 : 7 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9854 : 7 : inform (struct_parse_info->refloc, "originally defined here");
9855 : : }
9856 : 97 : else if (!comptypes_same_p (t, vistype))
9857 : : {
9858 : 38 : error ("redefinition of struct or union %qT", t);
9859 : 38 : if (struct_parse_info->refloc != UNKNOWN_LOCATION)
9860 : 38 : inform (struct_parse_info->refloc, "originally defined here");
9861 : : }
9862 : : }
9863 : : }
9864 : :
9865 : 1157816 : C_TYPE_BEING_DEFINED (t) = 0;
9866 : :
9867 : 1157816 : if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
9868 : 674 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9869 : 353 : c_fixup_may_alias (x);
9870 : :
9871 : : /* Set type canonical based on equivalence class. */
9872 : 1157816 : if (flag_isoc23 && !C_TYPE_VARIABLE_SIZE (t))
9873 : : {
9874 : 925960 : if (c_struct_htab == NULL)
9875 : 35252 : c_struct_htab = hash_table<c_struct_hasher>::create_ggc (61);
9876 : :
9877 : 925960 : hashval_t hash = c_struct_hasher::hash (t);
9878 : :
9879 : 925960 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
9880 : 925960 : gcc_checking_assert (!TYPE_NAME (t)
9881 : : || TREE_CODE (TYPE_NAME (t)) == IDENTIFIER_NODE);
9882 : :
9883 : 925960 : tree *e = c_struct_htab->find_slot_with_hash (t, hash, INSERT);
9884 : 925960 : if (*e)
9885 : 61295 : TYPE_CANONICAL (t) = *e;
9886 : : else
9887 : : {
9888 : 864665 : TYPE_CANONICAL (t) = t;
9889 : 864665 : *e = t;
9890 : : }
9891 : 925960 : c_update_type_canonical (t);
9892 : : }
9893 : :
9894 : 1157816 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9895 : 2345985 : for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9896 : : {
9897 : 1188169 : TYPE_FIELDS (x) = TYPE_FIELDS (t);
9898 : 1188169 : TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9899 : 1188169 : TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9900 : 1188169 : TYPE_TYPELESS_STORAGE (x) = TYPE_TYPELESS_STORAGE (t);
9901 : 1188169 : C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9902 : 1188169 : C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9903 : 1188169 : C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9904 : 1188169 : C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9905 : 1188169 : C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9906 : 1188169 : C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9907 : 1188169 : TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
9908 : : }
9909 : :
9910 : : /* Update type location to the one of the definition, instead of e.g.
9911 : : a forward declaration. */
9912 : 1157816 : if (TYPE_STUB_DECL (t))
9913 : 1157816 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9914 : :
9915 : : /* Finish debugging output for this type. */
9916 : 1157816 : rest_of_type_compilation (t, toplevel);
9917 : :
9918 : 1157816 : finish_incomplete_vars (incomplete_vars, toplevel);
9919 : :
9920 : : /* Make sure a DECL_EXPR is created for structs with VLA members.
9921 : : Because we do not know the context, we always pass expr
9922 : : to force creation of a BIND_EXPR which is required in some
9923 : : contexts. */
9924 : 1157816 : if (c_type_variably_modified_p (t))
9925 : 665 : add_decl_expr (loc, t, expr, false);
9926 : :
9927 : 1157816 : if (warn_cxx_compat)
9928 : 575 : warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9929 : :
9930 : 1157816 : if (NULL != enclosing_struct_parse_info)
9931 : : {
9932 : 1117158 : delete struct_parse_info;
9933 : :
9934 : 1117158 : struct_parse_info = enclosing_struct_parse_info;
9935 : :
9936 : : /* If this struct is defined inside a struct, add it to
9937 : : struct_types. */
9938 : 1117158 : if (warn_cxx_compat
9939 : : && struct_parse_info != NULL
9940 : 350 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
9941 : 345 : struct_parse_info->struct_types.safe_push (t);
9942 : : }
9943 : :
9944 : 1157816 : if (fields_with_counted_by.length () > 0)
9945 : 157 : verify_counted_by_attribute (t, &fields_with_counted_by);
9946 : :
9947 : 1157816 : return t;
9948 : 1157816 : }
9949 : :
9950 : : static struct {
9951 : : gt_pointer_operator new_value;
9952 : : void *cookie;
9953 : : } resort_data;
9954 : :
9955 : : /* This routine compares two fields like field_decl_cmp but using the
9956 : : pointer operator in resort_data. */
9957 : :
9958 : : static int
9959 : 5417 : resort_field_decl_cmp (const void *x_p, const void *y_p)
9960 : : {
9961 : 5417 : const tree *const x = (const tree *) x_p;
9962 : 5417 : const tree *const y = (const tree *) y_p;
9963 : :
9964 : 5417 : if (DECL_NAME (*x) == DECL_NAME (*y))
9965 : : /* A nontype is "greater" than a type. */
9966 : 0 : return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9967 : 5417 : if (DECL_NAME (*x) == NULL_TREE)
9968 : : return -1;
9969 : 5417 : if (DECL_NAME (*y) == NULL_TREE)
9970 : : return 1;
9971 : 5417 : {
9972 : 5417 : tree d1 = DECL_NAME (*x);
9973 : 5417 : tree d2 = DECL_NAME (*y);
9974 : 5417 : resort_data.new_value (&d1, &d1, resort_data.cookie);
9975 : 5417 : resort_data.new_value (&d2, &d2, resort_data.cookie);
9976 : 5417 : if (d1 < d2)
9977 : 2748 : return -1;
9978 : : }
9979 : 2669 : return 1;
9980 : : }
9981 : :
9982 : : /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9983 : :
9984 : : void
9985 : 6 : resort_sorted_fields (void *obj,
9986 : : void * ARG_UNUSED (orig_obj),
9987 : : gt_pointer_operator new_value,
9988 : : void *cookie)
9989 : : {
9990 : 6 : struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9991 : 6 : resort_data.new_value = new_value;
9992 : 6 : resort_data.cookie = cookie;
9993 : 6 : qsort (&sf->elts[0], sf->len, sizeof (tree),
9994 : : resort_field_decl_cmp);
9995 : 6 : }
9996 : :
9997 : : /* Lay out the type T, and its element type, and so on. */
9998 : :
9999 : : static void
10000 : 0 : layout_array_type (tree t)
10001 : : {
10002 : 0 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
10003 : 0 : layout_array_type (TREE_TYPE (t));
10004 : 0 : layout_type (t);
10005 : 0 : }
10006 : :
10007 : : /* Begin compiling the definition of an enumeration type.
10008 : : NAME is its name (or null if anonymous).
10009 : : LOC is the enum's location.
10010 : : FIXED_UNDERLYING_TYPE is the (C23) underlying type specified in the
10011 : : definition.
10012 : : Returns the type object, as yet incomplete.
10013 : : Also records info about it so that build_enumerator
10014 : : may be used to declare the individual values as they are read. */
10015 : :
10016 : : tree
10017 : 175814 : start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
10018 : : tree fixed_underlying_type, bool potential_nesting_p)
10019 : : {
10020 : 175814 : tree enumtype = NULL_TREE;
10021 : 175814 : location_t enumloc = UNKNOWN_LOCATION;
10022 : :
10023 : : /* If this is the real definition for a previous forward reference,
10024 : : fill in the contents in the same object that used to be the
10025 : : forward reference. */
10026 : :
10027 : 175814 : if (name != NULL_TREE)
10028 : 69783 : enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
10029 : :
10030 : 69783 : if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10031 : : {
10032 : : /* If the type is currently being defined or if we have seen an
10033 : : incomplete version which is now complete, this is a nested
10034 : : redefinition. The later happens if the redefinition occurs
10035 : : inside the enum specifier itself. */
10036 : 177 : if (C_TYPE_BEING_DEFINED (enumtype)
10037 : 177 : || (potential_nesting_p && TYPE_VALUES (enumtype) != NULL_TREE))
10038 : 9 : error_at (loc, "nested redefinition of %<enum %E%>", name);
10039 : :
10040 : : /* For C23 we allow redefinitions. We set to zero and check for
10041 : : consistency later. */
10042 : 337 : if (flag_isoc23 && TYPE_VALUES (enumtype) != NULL_TREE)
10043 : : enumtype = NULL_TREE;
10044 : : }
10045 : :
10046 : 106182 : if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
10047 : : {
10048 : 175665 : enumtype = make_node (ENUMERAL_TYPE);
10049 : 175665 : TYPE_SIZE (enumtype) = NULL_TREE;
10050 : 175665 : SET_TYPE_STRUCTURAL_EQUALITY (enumtype);
10051 : 175665 : pushtag (loc, name, enumtype);
10052 : 175665 : if (fixed_underlying_type != NULL_TREE)
10053 : : {
10054 : : /* For an enum definition with a fixed underlying type, the
10055 : : type is complete during the definition and the
10056 : : enumeration constants have that type. If there was a
10057 : : tag, the type was completed in c_parser_enum_specifier.
10058 : : If not, it must be completed here. */
10059 : 12 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
10060 : 12 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
10061 : 12 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
10062 : 12 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
10063 : 12 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
10064 : 12 : TYPE_SIZE (enumtype) = NULL_TREE;
10065 : 12 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
10066 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
10067 : 12 : TYPE_CANONICAL (enumtype) = TYPE_CANONICAL (fixed_underlying_type);
10068 : 12 : c_update_type_canonical (enumtype);
10069 : 12 : layout_type (enumtype);
10070 : : }
10071 : : }
10072 : : /* Update type location to the one of the definition, instead of e.g.
10073 : : a forward declaration. */
10074 : 149 : else if (TYPE_STUB_DECL (enumtype))
10075 : : {
10076 : 149 : enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
10077 : 149 : DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
10078 : : }
10079 : :
10080 : 175814 : C_TYPE_BEING_DEFINED (enumtype) = 1;
10081 : :
10082 : 175814 : if (TYPE_VALUES (enumtype) != NULL_TREE)
10083 : : {
10084 : : /* This enum is a named one that has been declared already. */
10085 : 6 : auto_diagnostic_group d;
10086 : 6 : error_at (loc, "redeclaration of %<enum %E%>", name);
10087 : 6 : if (enumloc != UNKNOWN_LOCATION)
10088 : 6 : inform (enumloc, "originally defined here");
10089 : :
10090 : : /* Completely replace its old definition.
10091 : : The old enumerators remain defined, however. */
10092 : 6 : TYPE_VALUES (enumtype) = NULL_TREE;
10093 : 6 : }
10094 : :
10095 : 175814 : if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
10096 : 175814 : && fixed_underlying_type == NULL_TREE)
10097 : : {
10098 : 3 : error_at (loc, "%<enum%> declared with but defined without "
10099 : : "fixed underlying type");
10100 : 3 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
10101 : : }
10102 : :
10103 : 175814 : the_enum->enum_next_value = integer_zero_node;
10104 : 175814 : the_enum->enum_type = enumtype;
10105 : 175814 : the_enum->enum_overflow = 0;
10106 : :
10107 : 175814 : if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10108 : 106 : for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
10109 : 53 : TYPE_PACKED (v) = 1;
10110 : :
10111 : : /* FIXME: This will issue a warning for a use of a type defined
10112 : : within sizeof in a statement expr. This is not terribly serious
10113 : : as C++ doesn't permit statement exprs within sizeof anyhow. */
10114 : 175814 : if (warn_cxx_compat
10115 : 126 : && (in_sizeof || in_typeof || in_alignof || in_countof))
10116 : 0 : warning_at (loc, OPT_Wc___compat,
10117 : : "defining type in %qs expression is invalid in C++",
10118 : : (in_sizeof ? "sizeof"
10119 : 0 : : in_typeof ? "typeof"
10120 : 0 : : in_alignof ? "alignof"
10121 : : : "_Countof"));
10122 : :
10123 : 175814 : if (in_underspecified_init)
10124 : 6 : error_at (loc, "%qT defined in underspecified object initializer",
10125 : : enumtype);
10126 : :
10127 : 175814 : return enumtype;
10128 : : }
10129 : :
10130 : : /* After processing and defining all the values of an enumeration type,
10131 : : install their decls in the enumeration type and finish it off.
10132 : : ENUMTYPE is the type object, VALUES a list of decl-value pairs,
10133 : : and ATTRIBUTES are the specified attributes.
10134 : : Returns ENUMTYPE. */
10135 : :
10136 : : tree
10137 : 175814 : finish_enum (tree enumtype, tree values, tree attributes)
10138 : : {
10139 : 175814 : tree pair, tem;
10140 : 175814 : tree minnode = NULL_TREE, maxnode = NULL_TREE;
10141 : 175814 : int precision;
10142 : 175814 : signop sign;
10143 : 175814 : bool toplevel = (file_scope == current_scope);
10144 : 175814 : struct lang_type *lt;
10145 : :
10146 : 175814 : decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10147 : :
10148 : : /* Calculate the maximum value of any enumerator in this type. */
10149 : :
10150 : 175814 : if (values == error_mark_node)
10151 : 26 : minnode = maxnode = integer_zero_node;
10152 : : else
10153 : : {
10154 : 175788 : minnode = maxnode = TREE_VALUE (values);
10155 : 5569488 : for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
10156 : : {
10157 : 5393700 : tree value = TREE_VALUE (pair);
10158 : 5393700 : if (tree_int_cst_lt (maxnode, value))
10159 : 5251507 : maxnode = value;
10160 : 5393700 : if (tree_int_cst_lt (value, minnode))
10161 : 61905 : minnode = value;
10162 : : }
10163 : : }
10164 : :
10165 : : /* Construct the final type of this enumeration. It is the same
10166 : : as one of the integral types - the narrowest one that fits, except
10167 : : that normally we only go as narrow as int - and signed iff any of
10168 : : the values are negative. */
10169 : 175814 : sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
10170 : 175814 : precision = MAX (tree_int_cst_min_precision (minnode, sign),
10171 : : tree_int_cst_min_precision (maxnode, sign));
10172 : :
10173 : 175814 : bool wider_than_int =
10174 : 175814 : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
10175 : 175814 : || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
10176 : :
10177 : :
10178 : 175814 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
10179 : : {
10180 : : /* If the precision of the type was specified with an attribute and it
10181 : : was too small, give an error. Otherwise, use it. */
10182 : 175696 : if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
10183 : : {
10184 : 23 : if (precision > TYPE_PRECISION (enumtype))
10185 : : {
10186 : 6 : TYPE_PRECISION (enumtype) = 0;
10187 : 6 : error ("specified mode too small for enumerated values");
10188 : : }
10189 : : else
10190 : 17 : precision = TYPE_PRECISION (enumtype);
10191 : : }
10192 : : else
10193 : 175673 : TYPE_PRECISION (enumtype) = 0;
10194 : :
10195 : 175696 : if (TYPE_PACKED (enumtype)
10196 : 175617 : || precision > TYPE_PRECISION (integer_type_node)
10197 : 348736 : || TYPE_PRECISION (enumtype))
10198 : : {
10199 : 2783 : tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
10200 : 2668 : if (tem == NULL)
10201 : : {
10202 : : /* This should only occur when both signed and unsigned
10203 : : values of maximum precision occur among the
10204 : : enumerators. */
10205 : 3 : pedwarn (input_location, 0,
10206 : : "enumeration values exceed range of largest integer");
10207 : 3 : tem = widest_integer_literal_type_node;
10208 : : }
10209 : 2665 : else if (precision > TYPE_PRECISION (intmax_type_node)
10210 : 4 : && !tree_int_cst_lt (minnode,
10211 : 4 : TYPE_MIN_VALUE (intmax_type_node))
10212 : 2668 : && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
10213 : : maxnode))
10214 : 2 : pedwarn (input_location, OPT_Wpedantic,
10215 : : "enumeration values exceed range of %qs",
10216 : : sign == UNSIGNED ? "uintmax_t" : "intmax_t");
10217 : : }
10218 : : else
10219 : 173028 : tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
10220 : :
10221 : 175696 : TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
10222 : 175696 : TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
10223 : 175696 : TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
10224 : 175696 : SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
10225 : 175696 : TYPE_SIZE (enumtype) = NULL_TREE;
10226 : 175696 : TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
10227 : 175696 : ENUM_UNDERLYING_TYPE (enumtype) =
10228 : 175696 : c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
10229 : :
10230 : 527088 : TYPE_CANONICAL (enumtype) =
10231 : 175696 : TYPE_CANONICAL (ENUM_UNDERLYING_TYPE (enumtype));
10232 : 175696 : c_update_type_canonical (enumtype);
10233 : :
10234 : 175696 : layout_type (enumtype);
10235 : : }
10236 : :
10237 : 175814 : if (values != error_mark_node)
10238 : : {
10239 : : /* Change the type of the enumerators to be the enum type. We
10240 : : need to do this irrespective of the size of the enum, for
10241 : : proper type checking. Replace the DECL_INITIALs of the
10242 : : enumerators, and the value slots of the list, with copies
10243 : : that have the enum type; they cannot be modified in place
10244 : : because they may be shared (e.g. integer_zero_node) Finally,
10245 : : change the purpose slots to point to the names of the decls. */
10246 : 5745276 : for (pair = values; pair; pair = TREE_CHAIN (pair))
10247 : : {
10248 : 5569488 : tree enu = TREE_PURPOSE (pair);
10249 : 5569488 : tree ini = DECL_INITIAL (enu);
10250 : :
10251 : 5569488 : TREE_TYPE (enu) = enumtype;
10252 : :
10253 : : /* Before C23, the ISO C Standard mandates enumerators to
10254 : : have type int, even though the underlying type of an enum
10255 : : type is unspecified. However, C23 allows enumerators of
10256 : : any integer type, and if an enumeration has any
10257 : : enumerators wider than int, all enumerators have the
10258 : : enumerated type after it is parsed. Any enumerators that
10259 : : fit in int are given type int in build_enumerator (which
10260 : : is the correct type while the enumeration is being
10261 : : parsed), so no conversions are needed here if all
10262 : : enumerators fit in int. If the enum has a fixed
10263 : : underlying type, the correct type was also given in
10264 : : build_enumerator. */
10265 : 5569488 : if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
10266 : 28521 : ini = convert (enumtype, ini);
10267 : :
10268 : 5569488 : DECL_INITIAL (enu) = ini;
10269 : 5569488 : TREE_PURPOSE (pair) = DECL_NAME (enu);
10270 : : /* To match the C++ FE, store the CONST_DECL rather than just its
10271 : : value. */
10272 : 5569488 : TREE_VALUE (pair) = enu;
10273 : : }
10274 : :
10275 : 175788 : TYPE_VALUES (enumtype) = values;
10276 : : }
10277 : :
10278 : : /* Record the min/max values so that we can warn about bit-field
10279 : : enumerations that are too small for the values. */
10280 : 175814 : lt = ((struct lang_type *)
10281 : 175814 : ggc_internal_cleared_alloc (c_dialect_objc ()
10282 : : ? sizeof (struct lang_type)
10283 : : : offsetof (struct lang_type, info)));
10284 : 175814 : lt->enum_min = minnode;
10285 : 175814 : lt->enum_max = maxnode;
10286 : 175814 : TYPE_LANG_SPECIFIC (enumtype) = lt;
10287 : :
10288 : : /* Fix up all variant types of this enum type. */
10289 : 175814 : tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
10290 : 351638 : for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
10291 : : {
10292 : 175824 : C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
10293 : 175824 : if (tem == enumtype)
10294 : 175814 : continue;
10295 : 10 : TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
10296 : 10 : TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
10297 : 10 : TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
10298 : 10 : TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
10299 : 10 : TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
10300 : 10 : SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
10301 : 10 : TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
10302 : 10 : SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
10303 : 10 : TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
10304 : 10 : TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
10305 : 10 : TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
10306 : 10 : ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
10307 : 10 : TYPE_PACKED (tem) = TYPE_PACKED (enumtype);
10308 : : }
10309 : :
10310 : : /* Finish debugging output for this type. */
10311 : 175814 : rest_of_type_compilation (enumtype, toplevel);
10312 : :
10313 : 175814 : finish_incomplete_vars (incomplete_vars, toplevel);
10314 : :
10315 : : /* If this enum is defined inside a struct, add it to
10316 : : struct_types. */
10317 : 175814 : if (warn_cxx_compat
10318 : 126 : && struct_parse_info != NULL
10319 : 42 : && !in_sizeof && !in_typeof && !in_alignof && !in_countof)
10320 : 42 : struct_parse_info->struct_types.safe_push (enumtype);
10321 : :
10322 : : /* Check for consistency with previous definition */
10323 : 175814 : if (flag_isoc23)
10324 : : {
10325 : 137860 : tree vistype = previous_tag (enumtype);
10326 : 137860 : if (vistype
10327 : 33 : && TREE_CODE (vistype) == TREE_CODE (enumtype)
10328 : 137889 : && !C_TYPE_BEING_DEFINED (vistype))
10329 : : {
10330 : 29 : TYPE_STUB_DECL (enumtype) = TYPE_STUB_DECL (vistype);
10331 : 29 : if (!comptypes_same_p (enumtype, vistype))
10332 : 3 : error("conflicting redefinition of enum %qT", enumtype);
10333 : : }
10334 : : }
10335 : :
10336 : 175814 : C_TYPE_BEING_DEFINED (enumtype) = 0;
10337 : :
10338 : 175814 : return enumtype;
10339 : : }
10340 : :
10341 : : /* Build and install a CONST_DECL for one value of the
10342 : : current enumeration type (one that was begun with start_enum).
10343 : : DECL_LOC is the location of the enumerator.
10344 : : LOC is the location of the '=' operator if any, DECL_LOC otherwise.
10345 : : Return a tree-list containing the CONST_DECL and its value.
10346 : : Assignment of sequential values by default is handled here. */
10347 : :
10348 : : tree
10349 : 5569505 : build_enumerator (location_t decl_loc, location_t loc,
10350 : : struct c_enum_contents *the_enum, tree name, tree value)
10351 : : {
10352 : 5569505 : tree decl;
10353 : 5569505 : tree old_decl;
10354 : :
10355 : : /* Validate and default VALUE. */
10356 : :
10357 : 5569505 : if (value != NULL_TREE)
10358 : : {
10359 : : /* Don't issue more errors for error_mark_node (i.e. an
10360 : : undeclared identifier) - just ignore the value expression. */
10361 : 3441700 : if (value == error_mark_node)
10362 : : value = NULL_TREE;
10363 : 3441035 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
10364 : : {
10365 : 6 : error_at (loc, "enumerator value for %qE is not an integer constant",
10366 : : name);
10367 : 6 : value = NULL_TREE;
10368 : : }
10369 : : else
10370 : : {
10371 : 3441029 : if (TREE_CODE (value) != INTEGER_CST)
10372 : : {
10373 : 97 : value = c_fully_fold (value, false, NULL);
10374 : 97 : if (TREE_CODE (value) == INTEGER_CST)
10375 : 48 : pedwarn (loc, OPT_Wpedantic,
10376 : : "enumerator value for %qE is not an integer "
10377 : : "constant expression", name);
10378 : : }
10379 : 3441029 : if (TREE_CODE (value) != INTEGER_CST)
10380 : : {
10381 : 49 : error ("enumerator value for %qE is not an integer constant",
10382 : : name);
10383 : 49 : value = NULL_TREE;
10384 : : }
10385 : : else
10386 : : {
10387 : 3440980 : value = default_conversion (value);
10388 : 3440980 : constant_expression_warning (value);
10389 : : }
10390 : : }
10391 : : }
10392 : :
10393 : : /* Default based on previous value. */
10394 : : /* It should no longer be possible to have NON_LVALUE_EXPR
10395 : : in the default. */
10396 : 3441035 : if (value == NULL_TREE)
10397 : : {
10398 : 2128525 : value = the_enum->enum_next_value;
10399 : 2128525 : if (the_enum->enum_overflow)
10400 : 8 : error_at (loc, "overflow in enumeration values");
10401 : : }
10402 : 5569505 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10403 : : {
10404 : : /* Enumeration constants must fit in the fixed underlying type. */
10405 : 163 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
10406 : 4 : error_at (loc,
10407 : : "enumerator value outside the range of underlying type");
10408 : : /* Enumeration constants for an enum with fixed underlying type
10409 : : have the enum type, both inside and outside the
10410 : : definition. */
10411 : 163 : value = convert (the_enum->enum_type, value);
10412 : : }
10413 : 5569342 : else if (flag_isoc23
10414 : 5157057 : && (old_decl = lookup_name_in_scope (name, current_scope))
10415 : 35 : && old_decl != error_mark_node
10416 : 35 : && TREE_TYPE (old_decl)
10417 : 35 : && TREE_TYPE (TREE_TYPE (old_decl))
10418 : 5569376 : && TREE_CODE (old_decl) == CONST_DECL)
10419 : : {
10420 : : /* Enumeration constants in a redeclaration have the previous type. */
10421 : 34 : tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl));
10422 : 34 : if (!int_fits_type_p (value, previous_type))
10423 : : {
10424 : 2 : error_at (loc, "value of redeclared enumerator outside the range "
10425 : : "of %qT", previous_type);
10426 : 2 : locate_old_decl (old_decl);
10427 : : }
10428 : 34 : value = convert (previous_type, value);
10429 : : }
10430 : : else
10431 : : {
10432 : : /* Even though the underlying type of an enum is unspecified, the
10433 : : type of enumeration constants is explicitly defined as int
10434 : : (6.4.4.3/2 in the C99 Standard). C23 allows any integer type, and
10435 : : GCC allows such types for older standards as an extension. */
10436 : 5569308 : bool warned_range = false;
10437 : 5569308 : if (!int_fits_type_p (value,
10438 : 5569308 : (TYPE_UNSIGNED (TREE_TYPE (value))
10439 : : ? uintmax_type_node
10440 : : : intmax_type_node)))
10441 : : /* GCC does not consider its types larger than intmax_t to be
10442 : : extended integer types (although C23 would permit such types to
10443 : : be considered extended integer types if all the features
10444 : : required by <stdint.h> and <inttypes.h> macros, such as support
10445 : : for integer constants and I/O, were present), so diagnose if
10446 : : such a wider type is used. (If the wider type arose from a
10447 : : constant of such a type, that will also have been diagnosed,
10448 : : but this is the only diagnostic in the case where it arises
10449 : : from choosing a wider type automatically when adding 1
10450 : : overflows.) */
10451 : 11 : warned_range = pedwarn (loc, OPT_Wpedantic,
10452 : : "enumerator value outside the range of %qs",
10453 : 11 : TYPE_UNSIGNED (TREE_TYPE (value))
10454 : : ? "uintmax_t"
10455 : : : "intmax_t");
10456 : 5569308 : if (!warned_range && !int_fits_type_p (value, integer_type_node))
10457 : 4662 : pedwarn_c11 (loc, OPT_Wpedantic,
10458 : : "ISO C restricts enumerator values to range of %<int%> "
10459 : : "before C23");
10460 : :
10461 : : /* The ISO C Standard mandates enumerators to have type int before
10462 : : C23, even though the underlying type of an enum type is
10463 : : unspecified. C23 allows enumerators of any integer type. During
10464 : : the parsing of the enumeration, C23 specifies that constants
10465 : : representable in int have type int, constants not representable
10466 : : in int have the type of the given expression if any, and
10467 : : constants not representable in int and derived by adding 1 to the
10468 : : previous constant have the type of that constant unless the
10469 : : addition would overflow or wraparound, in which case a wider type
10470 : : of the same signedness is chosen automatically; after the
10471 : : enumeration is parsed, all the constants have the type of the
10472 : : enumeration if any do not fit in int. */
10473 : 5569308 : if (int_fits_type_p (value, integer_type_node))
10474 : 5564642 : value = convert (integer_type_node, value);
10475 : : }
10476 : :
10477 : : /* Set basis for default for next value. */
10478 : 5569505 : if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10479 : : {
10480 : 163 : tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
10481 : 163 : if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
10482 : : /* A value of 2 following a value of 1 overflows bool, but we
10483 : : cannot carry out addition directly on bool without
10484 : : promotion, and converting the result of arithmetic in a
10485 : : wider type back to bool would not produce the right result
10486 : : for this overflow check. */
10487 : 61 : the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
10488 : : else
10489 : 102 : the_enum->enum_next_value
10490 : 102 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10491 : : PLUS_EXPR, convert (underlying_type, value),
10492 : : convert (underlying_type, integer_one_node),
10493 : : false);
10494 : : }
10495 : : else
10496 : : {
10497 : : /* In a redeclaration the type can already be the enumeral type. */
10498 : 5569342 : if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
10499 : 12 : value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
10500 : 5569342 : the_enum->enum_next_value
10501 : 5569342 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10502 : : PLUS_EXPR, value, integer_one_node, false);
10503 : : }
10504 : 5569505 : the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
10505 : 5569505 : if (the_enum->enum_overflow
10506 : 5569505 : && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
10507 : : {
10508 : : /* Choose a wider type with the same signedness if
10509 : : available. */
10510 : 3289 : int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
10511 : 3289 : bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
10512 : 3289 : tree new_type = (unsignedp
10513 : 3289 : ? long_unsigned_type_node
10514 : : : long_integer_type_node);
10515 : 3289 : if (prec > TYPE_PRECISION (new_type))
10516 : 2922 : new_type = (unsignedp
10517 : 2922 : ? long_long_unsigned_type_node
10518 : : : long_long_integer_type_node);
10519 : 3289 : if (prec > TYPE_PRECISION (new_type))
10520 : 2460 : new_type = (unsignedp
10521 : 2460 : ? widest_unsigned_literal_type_node
10522 : : : widest_integer_literal_type_node);
10523 : 3289 : if (prec <= TYPE_PRECISION (new_type))
10524 : : {
10525 : 3284 : the_enum->enum_overflow = false;
10526 : 3284 : the_enum->enum_next_value
10527 : 3284 : = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
10528 : : PLUS_EXPR, convert (new_type, value),
10529 : : integer_one_node, false);
10530 : 3284 : gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
10531 : : }
10532 : : }
10533 : :
10534 : : /* Now create a declaration for the enum value name. */
10535 : :
10536 : 5569505 : decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
10537 : 5569505 : DECL_INITIAL (decl) = value;
10538 : 5569505 : DECL_CONTEXT (decl) = the_enum->enum_type;
10539 : 5569505 : pushdecl (decl);
10540 : :
10541 : 5569505 : return tree_cons (decl, value, NULL_TREE);
10542 : : }
10543 : :
10544 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
10545 : :
10546 : : tree
10547 : 0 : c_simulate_enum_decl (location_t loc, const char *name,
10548 : : vec<string_int_pair> *values_ptr)
10549 : : {
10550 : 0 : location_t saved_loc = input_location;
10551 : 0 : input_location = loc;
10552 : :
10553 : 0 : struct c_enum_contents the_enum;
10554 : 0 : tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
10555 : : NULL_TREE, false);
10556 : :
10557 : 0 : tree value_chain = NULL_TREE;
10558 : 0 : string_int_pair *value;
10559 : 0 : vec<string_int_pair> values = *values_ptr;
10560 : 0 : unsigned int i;
10561 : 0 : FOR_EACH_VEC_ELT (values, i, value)
10562 : : {
10563 : 0 : tree decl = build_enumerator (loc, loc, &the_enum,
10564 : : get_identifier (value->first),
10565 : : build_int_cst (integer_type_node,
10566 : 0 : value->second));
10567 : 0 : TREE_CHAIN (decl) = value_chain;
10568 : 0 : value_chain = decl;
10569 : : }
10570 : :
10571 : 0 : finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10572 : :
10573 : 0 : input_location = saved_loc;
10574 : 0 : return enumtype;
10575 : : }
10576 : :
10577 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10578 : :
10579 : : tree
10580 : 0 : c_simulate_record_decl (location_t loc, const char *name,
10581 : : array_slice<const tree> fields)
10582 : : {
10583 : 0 : location_t saved_loc = input_location;
10584 : 0 : input_location = loc;
10585 : :
10586 : 0 : class c_struct_parse_info *struct_info;
10587 : 0 : tree ident = get_identifier (name);
10588 : 0 : tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10589 : :
10590 : 0 : for (unsigned int i = 0; i < fields.size (); ++i)
10591 : : {
10592 : 0 : DECL_FIELD_CONTEXT (fields[i]) = type;
10593 : 0 : if (i > 0)
10594 : 0 : DECL_CHAIN (fields[i - 1]) = fields[i];
10595 : : }
10596 : :
10597 : 0 : finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10598 : :
10599 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
10600 : 0 : set_underlying_type (decl);
10601 : 0 : lang_hooks.decls.pushdecl (decl);
10602 : :
10603 : 0 : input_location = saved_loc;
10604 : 0 : return type;
10605 : : }
10606 : :
10607 : : /* Create the FUNCTION_DECL for a function definition.
10608 : : DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10609 : : the declaration; they describe the function's name and the type it returns,
10610 : : but twisted together in a fashion that parallels the syntax of C.
10611 : :
10612 : : This function creates a binding context for the function body
10613 : : as well as setting up the FUNCTION_DECL in current_function_decl.
10614 : :
10615 : : Returns true on success. If the DECLARATOR is not suitable for a function
10616 : : (it defines a datum instead), we return false to report a parse error. */
10617 : :
10618 : : bool
10619 : 35377292 : start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10620 : : tree attributes)
10621 : : {
10622 : 35377292 : tree decl1, old_decl;
10623 : 35377292 : tree restype, resdecl;
10624 : 35377292 : location_t loc;
10625 : 35377292 : location_t result_loc;
10626 : 35377292 : tree expr = NULL;
10627 : :
10628 : 35377292 : current_function_returns_value = 0; /* Assume, until we see it does. */
10629 : 35377292 : current_function_returns_null = 0;
10630 : 35377292 : current_function_returns_abnormally = 0;
10631 : 35377292 : warn_about_return_type = 0;
10632 : 35377292 : c_switch_stack = NULL;
10633 : :
10634 : : /* Indicate no valid break/continue context. */
10635 : 35377292 : in_statement = 0;
10636 : :
10637 : 35377292 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10638 : : &attributes, &expr, NULL, DEPRECATED_NORMAL);
10639 : 35377292 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10640 : :
10641 : : /* If the declarator is not suitable for a function definition,
10642 : : cause a syntax error. */
10643 : 35377292 : if (decl1 == NULL_TREE
10644 : 35377261 : || TREE_CODE (decl1) != FUNCTION_DECL)
10645 : : return false;
10646 : :
10647 : : /* Nested functions may have variably modified (return) type.
10648 : : Make sure the size expression is evaluated at this point. */
10649 : 35377254 : if (expr && !current_scope->parm_flag)
10650 : 11 : add_stmt (fold_convert (void_type_node, expr));
10651 : :
10652 : 35377254 : loc = DECL_SOURCE_LOCATION (decl1);
10653 : :
10654 : : /* A nested function is not global. */
10655 : 35377254 : if (current_function_decl != NULL_TREE)
10656 : 1575 : TREE_PUBLIC (decl1) = 0;
10657 : :
10658 : 35377254 : c_decl_attributes (&decl1, attributes, 0);
10659 : :
10660 : 35377254 : if (DECL_DECLARED_INLINE_P (decl1)
10661 : 34698199 : && DECL_UNINLINABLE (decl1)
10662 : 35377264 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10663 : : {
10664 : 3 : auto_urlify_attributes sentinel;
10665 : 3 : warning_at (loc, OPT_Wattributes,
10666 : : "inline function %qD given attribute %qs",
10667 : : decl1, "noinline");
10668 : 3 : }
10669 : :
10670 : : /* Handle gnu_inline attribute. */
10671 : 35377254 : if (declspecs->inline_p
10672 : 34698202 : && !flag_gnu89_inline
10673 : 34664212 : && TREE_CODE (decl1) == FUNCTION_DECL
10674 : 70041466 : && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10675 : 155795 : || current_function_decl))
10676 : : {
10677 : 34508489 : if (declspecs->storage_class != csc_static)
10678 : 34508483 : DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10679 : : }
10680 : :
10681 : 35377254 : announce_function (decl1);
10682 : :
10683 : 35377254 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10684 : : {
10685 : 1 : error_at (loc, "return type is an incomplete type");
10686 : : /* Make it return void instead. */
10687 : 1 : TREE_TYPE (decl1)
10688 : 2 : = c_build_function_type (void_type_node,
10689 : 1 : TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10690 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10691 : : }
10692 : :
10693 : 35377254 : if (warn_about_return_type)
10694 : 1404 : permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
10695 : 0 : : (warn_return_type > 0 ? OPT_Wreturn_type
10696 : : : OPT_Wimplicit_int),
10697 : : "return type defaults to %<int%>");
10698 : :
10699 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
10700 : : error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10701 : 35377254 : DECL_INITIAL (decl1) = error_mark_node;
10702 : :
10703 : : /* If this definition isn't a prototype and we had a prototype declaration
10704 : : before, copy the arg type info from that prototype. */
10705 : 35377254 : old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10706 : 35377254 : if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10707 : 35151587 : old_decl = NULL_TREE;
10708 : :
10709 : 35377254 : current_function_prototype_locus = UNKNOWN_LOCATION;
10710 : 35377254 : current_function_prototype_built_in = false;
10711 : 35377254 : current_function_prototype_arg_types = NULL_TREE;
10712 : 35377254 : tree newtype = TREE_TYPE (decl1);
10713 : 35377254 : tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10714 : 35377254 : if (!prototype_p (newtype))
10715 : : {
10716 : 13117 : tree oldrt = TREE_TYPE (oldtype);
10717 : 13117 : tree newrt = TREE_TYPE (newtype);
10718 : 13117 : if (old_decl != NULL_TREE
10719 : 316 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10720 : 13433 : && comptypes (oldrt, newrt))
10721 : : {
10722 : 297 : if (stdarg_p (oldtype))
10723 : : {
10724 : 1 : auto_diagnostic_group d;
10725 : 1 : warning_at (loc, 0, "%q+D defined as variadic function "
10726 : : "without prototype", decl1);
10727 : 1 : locate_old_decl (old_decl);
10728 : 1 : }
10729 : 297 : TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10730 : 297 : current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10731 : 297 : current_function_prototype_built_in
10732 : 297 : = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10733 : 297 : current_function_prototype_arg_types
10734 : 297 : = TYPE_ARG_TYPES (newtype);
10735 : : }
10736 : 13117 : if (TREE_PUBLIC (decl1))
10737 : : {
10738 : : /* If there is an external prototype declaration of this
10739 : : function, record its location but do not copy information
10740 : : to this decl. This may be an invisible declaration
10741 : : (built-in or in a scope which has finished) or simply
10742 : : have more refined argument types than any declaration
10743 : : found above. */
10744 : 12702 : struct c_binding *b;
10745 : 12977 : for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10746 : 711 : if (B_IN_SCOPE (b, external_scope))
10747 : : break;
10748 : 12702 : if (b)
10749 : : {
10750 : 436 : tree ext_decl, ext_type;
10751 : 436 : ext_decl = b->decl;
10752 : 436 : ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10753 : 436 : if (TREE_CODE (ext_type) == FUNCTION_TYPE
10754 : 872 : && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10755 : 436 : TREE_TYPE (ext_type)))
10756 : : {
10757 : 416 : current_function_prototype_locus
10758 : 416 : = DECL_SOURCE_LOCATION (ext_decl);
10759 : 416 : current_function_prototype_built_in
10760 : 416 : = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10761 : 416 : current_function_prototype_arg_types
10762 : 416 : = TYPE_ARG_TYPES (ext_type);
10763 : : }
10764 : : }
10765 : : }
10766 : : }
10767 : :
10768 : : /* Optionally warn about C23 compatibility. */
10769 : 35377254 : if (warn_deprecated_non_prototype
10770 : 34 : && old_decl != NULL_TREE
10771 : 5 : && TREE_CODE (oldtype) == FUNCTION_TYPE
10772 : 5 : && !TYPE_ARG_TYPES (oldtype)
10773 : 5 : && !TYPE_NO_NAMED_ARGS_STDARG_P (oldtype)
10774 : 35377259 : && (TYPE_ARG_TYPES (newtype)
10775 : 3 : && TREE_VALUE (TYPE_ARG_TYPES (newtype)) != void_type_node))
10776 : : {
10777 : 3 : bool warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
10778 : : "ISO C23 does not allow defining"
10779 : : " parameters for function %qE declared"
10780 : : " without parameters",
10781 : : decl1);
10782 : 3 : if (warned)
10783 : 3 : inform (DECL_SOURCE_LOCATION (old_decl), "declared here");
10784 : : }
10785 : :
10786 : : /* Optionally warn of old-fashioned def with no previous prototype. */
10787 : 35377254 : if (warn_strict_prototypes
10788 : 107308 : && old_decl != error_mark_node
10789 : 107308 : && !prototype_p (TREE_TYPE (decl1))
10790 : 35377255 : && C_DECL_ISNT_PROTOTYPE (old_decl))
10791 : 1 : warning_at (loc, OPT_Wstrict_prototypes,
10792 : : "function declaration isn%'t a prototype");
10793 : : /* Optionally warn of any global def with no previous prototype. */
10794 : 35377253 : else if (warn_missing_prototypes
10795 : 107009 : && old_decl != error_mark_node
10796 : 107009 : && TREE_PUBLIC (decl1)
10797 : 73916 : && !MAIN_NAME_P (DECL_NAME (decl1))
10798 : 73880 : && C_DECL_ISNT_PROTOTYPE (old_decl)
10799 : 35377894 : && !DECL_DECLARED_INLINE_P (decl1))
10800 : 4 : warning_at (loc, OPT_Wmissing_prototypes,
10801 : : "no previous prototype for %qD", decl1);
10802 : : /* Optionally warn of any def with no previous prototype
10803 : : if the function has already been used. */
10804 : 35377249 : else if (warn_missing_prototypes
10805 : 107005 : && old_decl != NULL_TREE
10806 : 74243 : && old_decl != error_mark_node
10807 : 74243 : && TREE_USED (old_decl)
10808 : 35384338 : && !prototype_p (TREE_TYPE (old_decl)))
10809 : 0 : warning_at (loc, OPT_Wmissing_prototypes,
10810 : : "%qD was used with no prototype before its definition", decl1);
10811 : : /* Optionally warn of any global def with no previous declaration. */
10812 : 35377249 : else if (warn_missing_declarations
10813 : 4 : && TREE_PUBLIC (decl1)
10814 : 3 : && old_decl == NULL_TREE
10815 : 3 : && !MAIN_NAME_P (DECL_NAME (decl1))
10816 : 35377252 : && !DECL_DECLARED_INLINE_P (decl1))
10817 : 2 : warning_at (loc, OPT_Wmissing_declarations,
10818 : : "no previous declaration for %qD",
10819 : : decl1);
10820 : : /* Optionally warn of any def with no previous declaration
10821 : : if the function has already been used. */
10822 : 35377247 : else if (warn_missing_declarations
10823 : 2 : && old_decl != NULL_TREE
10824 : 0 : && old_decl != error_mark_node
10825 : 0 : && TREE_USED (old_decl)
10826 : 35377247 : && C_DECL_IMPLICIT (old_decl))
10827 : 0 : warning_at (loc, OPT_Wmissing_declarations,
10828 : : "%qD was used with no declaration before its definition", decl1);
10829 : :
10830 : : /* This function exists in static storage.
10831 : : (This does not mean `static' in the C sense!) */
10832 : 35377254 : TREE_STATIC (decl1) = 1;
10833 : :
10834 : : /* This is the earliest point at which we might know the assembler
10835 : : name of the function. Thus, if it's set before this, die horribly. */
10836 : 35377254 : gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10837 : :
10838 : : /* If #pragma weak was used, mark the decl weak now. */
10839 : 35377254 : if (current_scope == file_scope)
10840 : 35375679 : maybe_apply_pragma_weak (decl1);
10841 : :
10842 : : /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10843 : 35377254 : if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10844 : : {
10845 : 3201 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10846 : 3201 : != integer_type_node)
10847 : 5 : pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10848 : 3196 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10849 : 1 : pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10850 : : decl1);
10851 : :
10852 : 3201 : check_main_parameter_types (decl1);
10853 : :
10854 : 3201 : if (!TREE_PUBLIC (decl1))
10855 : 0 : pedwarn (loc, OPT_Wmain,
10856 : : "%qD is normally a non-static function", decl1);
10857 : : }
10858 : :
10859 : 35377254 : tree parms = current_function_arg_info->parms;
10860 : 35377254 : if (old_decl)
10861 : : {
10862 : 225667 : location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10863 : 225667 : warn_parm_array_mismatch (origloc, old_decl, parms);
10864 : : }
10865 : :
10866 : : /* To enable versions to be created across TU's we mark and mangle all
10867 : : non-default versioned functions. */
10868 : 35377254 : if (TREE_CODE (decl1) == FUNCTION_DECL
10869 : : && !TARGET_HAS_FMV_TARGET_ATTRIBUTE
10870 : : && get_target_version (decl1).is_valid ())
10871 : : {
10872 : : maybe_mark_function_versioned (decl1);
10873 : : if (current_scope != file_scope)
10874 : : error ("versioned definitions are only allowed at file scope");
10875 : : }
10876 : :
10877 : : /* Record the decl so that the function name is defined.
10878 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
10879 : : use the old decl. */
10880 : :
10881 : 35377254 : current_function_decl = pushdecl (decl1);
10882 : :
10883 : 35377254 : if (tree access = build_attr_access_from_parms (parms, false))
10884 : 56409 : decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
10885 : : old_decl);
10886 : :
10887 : 35377254 : push_scope ();
10888 : 35377254 : declare_parm_level ();
10889 : :
10890 : : /* Set the result decl source location to the location of the typespec. */
10891 : 4005444 : result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10892 : 35377254 : ? loc : declspecs->locations[cdw_typespec]);
10893 : 35377254 : restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10894 : 35377254 : resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10895 : 35377254 : DECL_ARTIFICIAL (resdecl) = 1;
10896 : 35377254 : DECL_IGNORED_P (resdecl) = 1;
10897 : 35377254 : DECL_RESULT (current_function_decl) = resdecl;
10898 : :
10899 : 35377254 : start_fname_decls ();
10900 : :
10901 : 35377254 : return true;
10902 : : }
10903 : :
10904 : : /* Subroutine of store_parm_decls which handles new-style function
10905 : : definitions (prototype format). The parms already have decls, so we
10906 : : need only record them as in effect and complain if any redundant
10907 : : old-style parm decls were written. */
10908 : : static void
10909 : 35364150 : store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10910 : : {
10911 : 35364150 : tree decl;
10912 : 35364150 : c_arg_tag *tag;
10913 : 35364150 : unsigned ix;
10914 : :
10915 : 35364150 : if (current_scope->bindings)
10916 : : {
10917 : 7 : error_at (DECL_SOURCE_LOCATION (fndecl),
10918 : : "old-style parameter declarations in prototyped "
10919 : : "function definition");
10920 : :
10921 : : /* Get rid of the old-style declarations. */
10922 : 7 : pop_scope ();
10923 : 7 : push_scope ();
10924 : : }
10925 : : /* Don't issue this warning for nested functions, and don't issue this
10926 : : warning if we got here because ARG_INFO_TYPES was error_mark_node
10927 : : (this happens when a function definition has just an ellipsis in
10928 : : its parameter list). */
10929 : 35364143 : else if (!in_system_header_at (input_location)
10930 : 628711 : && !current_function_scope
10931 : 627283 : && arg_info->types != error_mark_node
10932 : 35991426 : && !arg_info->c23_empty_parens)
10933 : 575114 : warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10934 : : "traditional C rejects ISO C style function definitions");
10935 : :
10936 : : /* Now make all the parameter declarations visible in the function body.
10937 : : We can bypass most of the grunt work of pushdecl. */
10938 : 132825058 : for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10939 : : {
10940 : 97460908 : DECL_CONTEXT (decl) = current_function_decl;
10941 : 97460908 : if (DECL_NAME (decl))
10942 : : {
10943 : 97460346 : bind (DECL_NAME (decl), decl, current_scope,
10944 : : /*invisible=*/false, /*nested=*/false,
10945 : : UNKNOWN_LOCATION);
10946 : 97460346 : if (!TREE_USED (decl))
10947 : 97443101 : warn_if_shadowing (decl);
10948 : : }
10949 : : else
10950 : 562 : pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
10951 : : "ISO C does not support omitting parameter names in "
10952 : : "function definitions before C23");
10953 : : }
10954 : :
10955 : : /* Record the parameter list in the function declaration. */
10956 : 35364150 : DECL_ARGUMENTS (fndecl) = arg_info->parms;
10957 : :
10958 : : /* Now make all the ancillary declarations visible, likewise. */
10959 : 35364206 : for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10960 : : {
10961 : 56 : DECL_CONTEXT (decl) = current_function_decl;
10962 : 56 : if (DECL_NAME (decl))
10963 : 0 : bind (DECL_NAME (decl), decl, current_scope,
10964 : : /*invisible=*/false,
10965 : 0 : /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10966 : : UNKNOWN_LOCATION);
10967 : : }
10968 : :
10969 : : /* And all the tag declarations. */
10970 : 35364260 : FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10971 : 56 : if (tag->id)
10972 : 23 : bind (tag->id, tag->type, current_scope,
10973 : : /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10974 : 35364150 : }
10975 : :
10976 : : /* Subroutine of store_parm_decls which handles old-style function
10977 : : definitions (separate parameter list and declarations). */
10978 : :
10979 : : static void
10980 : 13104 : store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10981 : : {
10982 : 13104 : struct c_binding *b;
10983 : 13104 : tree parm, decl, last;
10984 : 13104 : tree parmids = arg_info->parms;
10985 : 13104 : hash_set<tree> seen_args;
10986 : :
10987 : 13104 : if (!in_system_header_at (input_location))
10988 : : {
10989 : 13101 : if (flag_isoc23)
10990 : 1349 : pedwarn (DECL_SOURCE_LOCATION (fndecl),
10991 : 1349 : OPT_Wold_style_definition, "old-style function definition");
10992 : : else
10993 : 11752 : warning_at (DECL_SOURCE_LOCATION (fndecl),
10994 : 11752 : OPT_Wold_style_definition,
10995 : : "old-style function definition");
10996 : : }
10997 : :
10998 : 13104 : if (current_scope->had_vla_unspec)
10999 : 1 : error ("%<[*]%> not allowed in other than function prototype scope");
11000 : :
11001 : : /* Match each formal parameter name with its declaration. Save each
11002 : : decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
11003 : 47373 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11004 : : {
11005 : 34269 : if (TREE_VALUE (parm) == NULL_TREE)
11006 : : {
11007 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
11008 : : "parameter name missing from parameter list");
11009 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
11010 : 0 : continue;
11011 : : }
11012 : :
11013 : 34269 : b = I_SYMBOL_BINDING (TREE_VALUE (parm));
11014 : 34269 : if (b && B_IN_CURRENT_SCOPE (b))
11015 : : {
11016 : 22649 : decl = b->decl;
11017 : : /* Skip erroneous parameters. */
11018 : 22649 : if (decl == error_mark_node)
11019 : 2 : continue;
11020 : : /* If we got something other than a PARM_DECL it is an error. */
11021 : 22647 : if (TREE_CODE (decl) != PARM_DECL)
11022 : : {
11023 : 7 : error_at (DECL_SOURCE_LOCATION (decl),
11024 : : "%qD declared as a non-parameter", decl);
11025 : 7 : continue;
11026 : : }
11027 : : /* If the declaration is already marked, we have a duplicate
11028 : : name. Complain and ignore the duplicate. */
11029 : 22640 : else if (seen_args.contains (decl))
11030 : : {
11031 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11032 : : "multiple parameters named %qD", decl);
11033 : 0 : TREE_PURPOSE (parm) = NULL_TREE;
11034 : 0 : continue;
11035 : : }
11036 : : /* If the declaration says "void", complain and turn it into
11037 : : an int. */
11038 : 22640 : else if (VOID_TYPE_P (TREE_TYPE (decl)))
11039 : : {
11040 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
11041 : : "parameter %qD declared with void type", decl);
11042 : 0 : TREE_TYPE (decl) = integer_type_node;
11043 : 0 : DECL_ARG_TYPE (decl) = integer_type_node;
11044 : 0 : layout_decl (decl, 0);
11045 : : }
11046 : 22640 : warn_if_shadowing (decl);
11047 : : }
11048 : : /* If no declaration found, default to int. */
11049 : : else
11050 : : {
11051 : : /* FIXME diagnostics: This should be the location of the argument,
11052 : : not the FNDECL. E.g., for an old-style declaration
11053 : :
11054 : : int f10(v) { blah; }
11055 : :
11056 : : We should use the location of the V, not the F10.
11057 : : Unfortunately, the V is an IDENTIFIER_NODE which has no
11058 : : location. In the future we need locations for c_arg_info
11059 : : entries.
11060 : :
11061 : : See gcc.dg/Wshadow-3.c for an example of this problem. */
11062 : 11620 : decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
11063 : 11620 : PARM_DECL, TREE_VALUE (parm), integer_type_node);
11064 : 11620 : DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
11065 : 11620 : pushdecl (decl);
11066 : 11620 : warn_if_shadowing (decl);
11067 : :
11068 : 11620 : if (flag_isoc99)
11069 : 118 : permerror_opt (DECL_SOURCE_LOCATION (decl),
11070 : 118 : OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
11071 : : decl);
11072 : : else
11073 : 11502 : warning_at (DECL_SOURCE_LOCATION (decl),
11074 : 11502 : OPT_Wmissing_parameter_type,
11075 : : "type of %qD defaults to %<int%>", decl);
11076 : : }
11077 : :
11078 : 34260 : TREE_PURPOSE (parm) = decl;
11079 : 34260 : seen_args.add (decl);
11080 : : }
11081 : :
11082 : : /* Now examine the parms chain for incomplete declarations
11083 : : and declarations with no corresponding names. */
11084 : :
11085 : 47452 : for (b = current_scope->bindings; b; b = b->prev)
11086 : : {
11087 : 34348 : parm = b->decl;
11088 : 34348 : if (TREE_CODE (parm) != PARM_DECL)
11089 : 79 : continue;
11090 : :
11091 : 34269 : if (TREE_TYPE (parm) != error_mark_node
11092 : 34269 : && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
11093 : : {
11094 : 0 : error_at (DECL_SOURCE_LOCATION (parm),
11095 : : "parameter %qD has incomplete type", parm);
11096 : 0 : TREE_TYPE (parm) = error_mark_node;
11097 : : }
11098 : :
11099 : 34269 : if (!seen_args.contains (parm))
11100 : : {
11101 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11102 : : "declaration for parameter %qD but no such parameter",
11103 : : parm);
11104 : :
11105 : : /* Pretend the parameter was not missing.
11106 : : This gets us to a standard state and minimizes
11107 : : further error messages. */
11108 : 9 : parmids = chainon (parmids, tree_cons (parm, 0, 0));
11109 : : }
11110 : : }
11111 : :
11112 : : /* Chain the declarations together in the order of the list of
11113 : : names. Store that chain in the function decl, replacing the
11114 : : list of names. Update the current scope to match. */
11115 : 13104 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
11116 : :
11117 : 13112 : for (parm = parmids; parm; parm = TREE_CHAIN (parm))
11118 : 8680 : if (TREE_PURPOSE (parm))
11119 : : break;
11120 : 13104 : if (parm && TREE_PURPOSE (parm))
11121 : : {
11122 : 8672 : last = TREE_PURPOSE (parm);
11123 : 8672 : DECL_ARGUMENTS (fndecl) = last;
11124 : :
11125 : 34270 : for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
11126 : 25598 : if (TREE_PURPOSE (parm))
11127 : : {
11128 : 25597 : DECL_CHAIN (last) = TREE_PURPOSE (parm);
11129 : 25598 : last = TREE_PURPOSE (parm);
11130 : : }
11131 : 8672 : DECL_CHAIN (last) = NULL_TREE;
11132 : : }
11133 : :
11134 : : /* If there was a previous prototype,
11135 : : set the DECL_ARG_TYPE of each argument according to
11136 : : the type previously specified, and report any mismatches. */
11137 : :
11138 : 13104 : if (current_function_prototype_arg_types)
11139 : : {
11140 : 171 : tree type;
11141 : 171 : for (parm = DECL_ARGUMENTS (fndecl),
11142 : 171 : type = current_function_prototype_arg_types;
11143 : 373 : parm || (type != NULL_TREE
11144 : 166 : && TREE_VALUE (type) != error_mark_node
11145 : 166 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
11146 : 202 : parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
11147 : : {
11148 : 212 : if (parm == NULL_TREE
11149 : 206 : || type == NULL_TREE
11150 : 418 : || (TREE_VALUE (type) != error_mark_node
11151 : 192 : && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
11152 : : {
11153 : 10 : if (current_function_prototype_built_in)
11154 : 6 : warning_at (DECL_SOURCE_LOCATION (fndecl),
11155 : 6 : 0, "number of arguments doesn%'t match "
11156 : : "built-in prototype");
11157 : : else
11158 : : {
11159 : : /* FIXME diagnostics: This should be the location of
11160 : : FNDECL, but there is bug when a prototype is
11161 : : declared inside function context, but defined
11162 : : outside of it (e.g., gcc.dg/pr15698-2.c). In
11163 : : which case FNDECL gets the location of the
11164 : : prototype, not the definition. */
11165 : 4 : error_at (input_location,
11166 : : "number of arguments doesn%'t match prototype");
11167 : :
11168 : 4 : error_at (current_function_prototype_locus,
11169 : : "prototype declaration");
11170 : : }
11171 : : break;
11172 : : }
11173 : : /* Type for passing arg must be consistent with that
11174 : : declared for the arg. ISO C says we take the unqualified
11175 : : type for parameters declared with qualified type. */
11176 : 202 : if (TREE_TYPE (parm) != error_mark_node
11177 : 201 : && TREE_VALUE (type) != error_mark_node
11178 : 389 : && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11179 : 187 : != TYPE_ATOMIC (TREE_VALUE (type)))
11180 : 185 : || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
11181 : 185 : TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
11182 : : {
11183 : 36 : if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
11184 : 36 : == TYPE_ATOMIC (TREE_VALUE (type)))
11185 : 36 : && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
11186 : 34 : == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
11187 : : {
11188 : : /* Adjust argument to match prototype. E.g. a previous
11189 : : `int foo(float);' prototype causes
11190 : : `int foo(x) float x; {...}' to be treated like
11191 : : `int foo(float x) {...}'. This is particularly
11192 : : useful for argument types like uid_t. */
11193 : 17 : DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
11194 : :
11195 : : /* ??? Is it possible to get here with a
11196 : : built-in prototype or will it always have
11197 : : been diagnosed as conflicting with an
11198 : : old-style definition and discarded? */
11199 : 17 : if (current_function_prototype_built_in)
11200 : 2 : warning_at (DECL_SOURCE_LOCATION (parm),
11201 : 2 : OPT_Wpedantic, "promoted argument %qD "
11202 : : "doesn%'t match built-in prototype", parm);
11203 : : else
11204 : : {
11205 : 15 : pedwarn (DECL_SOURCE_LOCATION (parm),
11206 : 15 : OPT_Wpedantic, "promoted argument %qD "
11207 : : "doesn%'t match prototype", parm);
11208 : 15 : pedwarn (current_function_prototype_locus, OPT_Wpedantic,
11209 : : "prototype declaration");
11210 : : }
11211 : : }
11212 : : else
11213 : : {
11214 : 19 : if (current_function_prototype_built_in)
11215 : 10 : warning_at (DECL_SOURCE_LOCATION (parm),
11216 : 10 : 0, "argument %qD doesn%'t match "
11217 : : "built-in prototype", parm);
11218 : : else
11219 : : {
11220 : 9 : error_at (DECL_SOURCE_LOCATION (parm),
11221 : : "argument %qD doesn%'t match prototype", parm);
11222 : 9 : error_at (current_function_prototype_locus,
11223 : : "prototype declaration");
11224 : : }
11225 : : }
11226 : : }
11227 : : }
11228 : 171 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
11229 : : }
11230 : :
11231 : : /* Otherwise, create a prototype that would match. */
11232 : :
11233 : : else
11234 : : {
11235 : 12933 : tree actual = NULL_TREE, last = NULL_TREE, type;
11236 : :
11237 : 46996 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
11238 : : {
11239 : 34063 : type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
11240 : 34063 : if (last)
11241 : 25547 : TREE_CHAIN (last) = type;
11242 : : else
11243 : : actual = type;
11244 : 34063 : last = type;
11245 : : }
11246 : 12933 : type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
11247 : 12933 : if (last)
11248 : 8516 : TREE_CHAIN (last) = type;
11249 : : else
11250 : : actual = type;
11251 : :
11252 : : /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
11253 : : of the type of this function, but we need to avoid having this
11254 : : affect the types of other similarly-typed functions, so we must
11255 : : first force the generation of an identical (but separate) type
11256 : : node for the relevant function type. The new node we create
11257 : : will be a variant of the main variant of the original function
11258 : : type. */
11259 : :
11260 : 12933 : TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
11261 : :
11262 : 12933 : TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
11263 : : }
11264 : 13104 : }
11265 : :
11266 : : /* Store parameter declarations passed in ARG_INFO into the current
11267 : : function declaration. */
11268 : :
11269 : : void
11270 : 0 : store_parm_decls_from (struct c_arg_info *arg_info)
11271 : : {
11272 : 0 : current_function_arg_info = arg_info;
11273 : 0 : store_parm_decls ();
11274 : 0 : }
11275 : :
11276 : : /* Called by walk_tree to look for and update context-less labels
11277 : : or labels with context in the parent function. */
11278 : :
11279 : : static tree
11280 : 8127 : set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
11281 : : {
11282 : 8127 : tree ctx = static_cast<tree>(data);
11283 : 8127 : if (TREE_CODE (*tp) == LABEL_EXPR
11284 : 8127 : && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
11285 : 2 : || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
11286 : : {
11287 : 72 : DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
11288 : 72 : *walk_subtrees = 0;
11289 : : }
11290 : :
11291 : 8127 : return NULL_TREE;
11292 : : }
11293 : :
11294 : : /* Store the parameter declarations into the current function declaration.
11295 : : This is called after parsing the parameter declarations, before
11296 : : digesting the body of the function.
11297 : :
11298 : : For an old-style definition, construct a prototype out of the old-style
11299 : : parameter declarations and inject it into the function's type. */
11300 : :
11301 : : void
11302 : 35377254 : store_parm_decls (void)
11303 : : {
11304 : 35377254 : tree fndecl = current_function_decl;
11305 : 35377254 : bool proto;
11306 : :
11307 : : /* The argument information block for FNDECL. */
11308 : 35377254 : struct c_arg_info *arg_info = current_function_arg_info;
11309 : 35377254 : current_function_arg_info = 0;
11310 : :
11311 : : /* True if this definition is written with a prototype. In C23, an
11312 : : empty argument list was converted to (void) in grokparms; in
11313 : : older C standard versions, it does not give the function a type
11314 : : with a prototype for future calls. */
11315 : 35377254 : proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
11316 : :
11317 : 13104 : if (proto)
11318 : 35364150 : store_parm_decls_newstyle (fndecl, arg_info);
11319 : : else
11320 : 13104 : store_parm_decls_oldstyle (fndecl, arg_info);
11321 : :
11322 : : /* The next call to push_scope will be a function body. */
11323 : :
11324 : 35377254 : next_is_function_body = true;
11325 : :
11326 : : /* Write a record describing this function definition to the prototypes
11327 : : file (if requested). */
11328 : :
11329 : 35377254 : gen_aux_info_record (fndecl, 1, 0, proto);
11330 : :
11331 : : /* Initialize the RTL code for the function. */
11332 : 35377254 : allocate_struct_function (fndecl, false);
11333 : :
11334 : 35377254 : if (warn_unused_local_typedefs)
11335 : 3108832 : cfun->language = ggc_cleared_alloc<language_function> ();
11336 : :
11337 : : /* Begin the statement tree for this function. */
11338 : 35377254 : DECL_SAVED_TREE (fndecl) = push_stmt_list ();
11339 : :
11340 : : /* ??? Insert the contents of the pending sizes list into the function
11341 : : to be evaluated. The only reason left to have this is
11342 : : void foo(int n, int array[n++])
11343 : : because we throw away the array type in favor of a pointer type, and
11344 : : thus won't naturally see the SAVE_EXPR containing the increment. All
11345 : : other pending sizes would be handled by gimplify_parameters. */
11346 : 35377254 : if (arg_info->pending_sizes)
11347 : : {
11348 : : /* In very special circumstances, e.g. for code like
11349 : : _Atomic int i = 5;
11350 : : void f (int a[i += 2]) {}
11351 : : we need to execute the atomic assignment on function entry.
11352 : : But in this case, it is not just a straight store, it has the
11353 : : op= form, which means that build_atomic_assign has generated
11354 : : gotos, labels, etc. Because at that time the function decl
11355 : : for F has not been created yet, those labels do not have any
11356 : : function context. But we have the fndecl now, so update the
11357 : : labels accordingly. gimplify_expr would crash otherwise.
11358 : : Or with nested functions the labels could be created with parent
11359 : : function's context, while when the statement is emitted at the
11360 : : start of the nested function, it needs the nested function's
11361 : : context. */
11362 : 295 : walk_tree_without_duplicates (&arg_info->pending_sizes,
11363 : : set_labels_context_r, fndecl);
11364 : 295 : add_stmt (arg_info->pending_sizes);
11365 : : }
11366 : 35377254 : }
11367 : :
11368 : : /* Store PARM_DECLs in PARMS into scope temporarily. Used for
11369 : : c_finish_omp_declare_simd for function prototypes. No diagnostics
11370 : : should be done. */
11371 : :
11372 : : void
11373 : 240 : temp_store_parm_decls (tree fndecl, tree parms)
11374 : : {
11375 : 240 : push_scope ();
11376 : 678 : for (tree p = parms; p; p = DECL_CHAIN (p))
11377 : : {
11378 : 438 : DECL_CONTEXT (p) = fndecl;
11379 : 438 : if (DECL_NAME (p))
11380 : 319 : bind (DECL_NAME (p), p, current_scope,
11381 : : /*invisible=*/false, /*nested=*/false,
11382 : : UNKNOWN_LOCATION);
11383 : : }
11384 : 240 : }
11385 : :
11386 : : /* Undo what temp_store_parm_decls did. */
11387 : :
11388 : : void
11389 : 240 : temp_pop_parm_decls (void)
11390 : : {
11391 : : /* Clear all bindings in this temporary scope, so that
11392 : : pop_scope doesn't create a BLOCK. */
11393 : 240 : struct c_binding *b = current_scope->bindings;
11394 : 240 : current_scope->bindings = NULL;
11395 : 564 : for (; b; b = free_binding_and_advance (b))
11396 : : {
11397 : 324 : gcc_assert (TREE_CODE (b->decl) == PARM_DECL
11398 : : || b->decl == error_mark_node);
11399 : 324 : gcc_assert (I_SYMBOL_BINDING (b->id) == b);
11400 : 324 : I_SYMBOL_BINDING (b->id) = b->shadowed;
11401 : 324 : if (b->shadowed && b->shadowed->u.type)
11402 : 0 : TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
11403 : : }
11404 : 240 : pop_scope ();
11405 : 240 : }
11406 : :
11407 : :
11408 : : /* Finish up a function declaration and compile that function
11409 : : all the way to assembler language output. Then free the storage
11410 : : for the function definition.
11411 : :
11412 : : This is called after parsing the body of the function definition. */
11413 : :
11414 : : void
11415 : 35377252 : finish_function (location_t end_loc)
11416 : : {
11417 : 35377252 : tree fndecl = current_function_decl;
11418 : :
11419 : 35377252 : if (c_dialect_objc ())
11420 : 0 : objc_finish_function ();
11421 : :
11422 : 35377252 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
11423 : 35377250 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11424 : :
11425 : : /* Must mark the RESULT_DECL as being in this function. */
11426 : :
11427 : 35377252 : if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
11428 : 35377252 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11429 : :
11430 : 35425959 : if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
11431 : 48706 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
11432 : 35425958 : == integer_type_node && flag_isoc99)
11433 : : {
11434 : : /* Hack. We don't want the middle-end to warn that this return
11435 : : is unreachable, so we mark its location as special. Using
11436 : : UNKNOWN_LOCATION has the problem that it gets clobbered in
11437 : : annotate_one_with_locus. A cleaner solution might be to
11438 : : ensure ! should_carry_locus_p (stmt), but that needs a flag.
11439 : : */
11440 : 47343 : c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
11441 : : }
11442 : :
11443 : : /* Tie off the statement tree for this function. */
11444 : 35377252 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11445 : :
11446 : 35377252 : finish_fname_decls ();
11447 : :
11448 : : /* Complain if there's no return statement only if option specified on
11449 : : command line. */
11450 : 35377252 : if (warn_return_type > 0
11451 : 3108855 : && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
11452 : 2913238 : && !current_function_returns_value && !current_function_returns_null
11453 : : /* Don't complain if we are no-return. */
11454 : 72 : && !current_function_returns_abnormally
11455 : : /* Don't complain if we are declared noreturn. */
11456 : 21 : && !TREE_THIS_VOLATILE (fndecl)
11457 : : /* Don't warn for main(). */
11458 : 18 : && !MAIN_NAME_P (DECL_NAME (fndecl))
11459 : : /* Or if they didn't actually specify a return type. */
11460 : 17 : && !C_FUNCTION_IMPLICIT_INT (fndecl)
11461 : : /* Normally, with -Wreturn-type, flow will complain, but we might
11462 : : optimize out static functions. */
11463 : 17 : && !TREE_PUBLIC (fndecl)
11464 : 6 : && targetm.warn_func_return (fndecl)
11465 : 38486107 : && warning (OPT_Wreturn_type,
11466 : : "no return statement in function returning non-void"))
11467 : 5 : suppress_warning (fndecl, OPT_Wreturn_type);
11468 : :
11469 : : /* Complain about parameters that are only set, but never otherwise used. */
11470 : 35377252 : if (warn_unused_but_set_parameter)
11471 : : {
11472 : 3044926 : tree decl;
11473 : :
11474 : 3044926 : for (decl = DECL_ARGUMENTS (fndecl);
11475 : 11380568 : decl;
11476 : 8335642 : decl = DECL_CHAIN (decl))
11477 : 8335642 : if (TREE_USED (decl)
11478 : 8274939 : && TREE_CODE (decl) == PARM_DECL
11479 : 8274939 : && !DECL_READ_P (decl)
11480 : 47 : && DECL_NAME (decl)
11481 : 47 : && !DECL_ARTIFICIAL (decl)
11482 : 8335689 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_))
11483 : 47 : warning_at (DECL_SOURCE_LOCATION (decl),
11484 : 47 : OPT_Wunused_but_set_parameter_,
11485 : : "parameter %qD set but not used", decl);
11486 : : }
11487 : :
11488 : : /* Complain about locally defined typedefs that are not used in this
11489 : : function. */
11490 : 35377252 : maybe_warn_unused_local_typedefs ();
11491 : :
11492 : : /* Possibly warn about unused parameters. */
11493 : 35377252 : if (warn_unused_parameter)
11494 : 2951037 : do_warn_unused_parameter (fndecl);
11495 : :
11496 : : /* Store the end of the function, so that we get good line number
11497 : : info for the epilogue. */
11498 : 35377252 : cfun->function_end_locus = end_loc;
11499 : :
11500 : : /* Finalize the ELF visibility for the function. */
11501 : 35377252 : c_determine_visibility (fndecl);
11502 : :
11503 : : /* For GNU C extern inline functions disregard inline limits. */
11504 : 35377252 : if (DECL_EXTERNAL (fndecl)
11505 : 34537004 : && DECL_DECLARED_INLINE_P (fndecl)
11506 : 69914253 : && (flag_gnu89_inline
11507 : 34506062 : || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
11508 : 34536532 : DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
11509 : :
11510 : : /* Genericize before inlining. Delay genericizing nested functions
11511 : : until their parent function is genericized. Since finalizing
11512 : : requires GENERIC, delay that as well. */
11513 : :
11514 : 70754504 : if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
11515 : 70754502 : && !undef_nested_function)
11516 : : {
11517 : 35377244 : if (!decl_function_context (fndecl))
11518 : : {
11519 : 35375669 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
11520 : 35375669 : c_genericize (fndecl);
11521 : :
11522 : : /* ??? Objc emits functions after finalizing the compilation unit.
11523 : : This should be cleaned up later and this conditional removed. */
11524 : 35375669 : if (symtab->global_info_ready)
11525 : : {
11526 : 0 : cgraph_node::add_new_function (fndecl, false);
11527 : 0 : return;
11528 : : }
11529 : 35375669 : cgraph_node::finalize_function (fndecl, false);
11530 : : }
11531 : : else
11532 : : {
11533 : : /* Register this function with cgraph just far enough to get it
11534 : : added to our parent's nested function list. Handy, since the
11535 : : C front end doesn't have such a list. */
11536 : 1575 : (void) cgraph_node::get_create (fndecl);
11537 : : }
11538 : : }
11539 : :
11540 : 35377252 : if (!decl_function_context (fndecl))
11541 : 35375677 : undef_nested_function = false;
11542 : :
11543 : 35377252 : if (cfun->language != NULL)
11544 : : {
11545 : 3108831 : ggc_free (cfun->language);
11546 : 3108831 : cfun->language = NULL;
11547 : : }
11548 : :
11549 : : /* We're leaving the context of this function, so zap cfun.
11550 : : It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
11551 : : tree_rest_of_compilation. */
11552 : 35377252 : set_cfun (NULL);
11553 : 35377252 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
11554 : 35377252 : current_function_decl = NULL;
11555 : : }
11556 : :
11557 : : /* Check the declarations given in a for-loop for satisfying the C99
11558 : : constraints. If exactly one such decl is found, return it. LOC is
11559 : : the location of the opening parenthesis of the for loop. The last
11560 : : parameter allows you to control the "for loop initial declarations
11561 : : are only allowed in C99 mode". Normally, you should pass
11562 : : flag_isoc99 as that parameter. But in some cases (Objective-C
11563 : : foreach loop, for example) we want to run the checks in this
11564 : : function even if not in C99 mode, so we allow the caller to turn
11565 : : off the error about not being in C99 mode.
11566 : : */
11567 : :
11568 : : tree
11569 : 24962 : check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
11570 : : {
11571 : 24962 : struct c_binding *b;
11572 : 24962 : tree one_decl = NULL_TREE;
11573 : 24962 : int n_decls = 0;
11574 : :
11575 : 24962 : if (!turn_off_iso_c99_error)
11576 : : {
11577 : 1 : static bool hint = true;
11578 : : /* If we get here, declarations have been used in a for loop without
11579 : : the C99 for loop scope. This doesn't make much sense, so don't
11580 : : allow it. */
11581 : 1 : auto_diagnostic_group d;
11582 : 1 : error_at (loc, "%<for%> loop initial declarations "
11583 : : "are only allowed in C99 or C11 mode");
11584 : 1 : if (hint)
11585 : : {
11586 : 1 : inform (loc,
11587 : : "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11588 : : "%<-std=gnu11%> to compile your code");
11589 : 1 : hint = false;
11590 : : }
11591 : 1 : return NULL_TREE;
11592 : 1 : }
11593 : : else
11594 : 24961 : pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11595 : : "initial declarations");
11596 : :
11597 : : /* C99 subclause 6.8.5 paragraph 3:
11598 : :
11599 : : [#3] The declaration part of a for statement shall only
11600 : : declare identifiers for objects having storage class auto or
11601 : : register.
11602 : :
11603 : : It isn't clear whether, in this sentence, "identifiers" binds to
11604 : : "shall only declare" or to "objects" - that is, whether all identifiers
11605 : : declared must be identifiers for objects, or whether the restriction
11606 : : only applies to those that are. (A question on this in comp.std.c
11607 : : in November 2000 received no answer.) We implement the strictest
11608 : : interpretation, to avoid creating an extension which later causes
11609 : : problems.
11610 : :
11611 : : This constraint was removed in C23. */
11612 : :
11613 : 50002 : for (b = current_scope->bindings; b; b = b->prev)
11614 : : {
11615 : 25041 : tree id = b->id;
11616 : 25041 : tree decl = b->decl;
11617 : :
11618 : 25041 : if (!id)
11619 : 27 : continue;
11620 : :
11621 : 25014 : switch (TREE_CODE (decl))
11622 : : {
11623 : 24966 : case VAR_DECL:
11624 : 24966 : {
11625 : 24966 : location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11626 : 24966 : if (TREE_STATIC (decl))
11627 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11628 : : "declaration of static variable %qD in %<for%> "
11629 : : "loop initial declaration", decl);
11630 : 24961 : else if (DECL_EXTERNAL (decl))
11631 : 5 : pedwarn_c11 (decl_loc, OPT_Wpedantic,
11632 : : "declaration of %<extern%> variable %qD in %<for%> "
11633 : : "loop initial declaration", decl);
11634 : : }
11635 : : break;
11636 : :
11637 : 6 : case RECORD_TYPE:
11638 : 6 : pedwarn_c11 (loc, OPT_Wpedantic,
11639 : : "%<struct %E%> declared in %<for%> loop initial "
11640 : : "declaration", id);
11641 : 6 : break;
11642 : 5 : case UNION_TYPE:
11643 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11644 : : "%<union %E%> declared in %<for%> loop initial "
11645 : : "declaration",
11646 : : id);
11647 : 5 : break;
11648 : 5 : case ENUMERAL_TYPE:
11649 : 5 : pedwarn_c11 (loc, OPT_Wpedantic,
11650 : : "%<enum %E%> declared in %<for%> loop "
11651 : : "initial declaration", id);
11652 : 5 : break;
11653 : 32 : default:
11654 : 32 : pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
11655 : : "%qD in %<for%> loop initial declaration", decl);
11656 : : }
11657 : :
11658 : 25014 : n_decls++;
11659 : 25014 : one_decl = decl;
11660 : : }
11661 : :
11662 : 24961 : return n_decls == 1 ? one_decl : NULL_TREE;
11663 : : }
11664 : :
11665 : : /* Save and reinitialize the variables
11666 : : used during compilation of a C function. */
11667 : :
11668 : : void
11669 : 1603 : c_push_function_context (void)
11670 : : {
11671 : 1603 : struct language_function *p = cfun->language;
11672 : : /* cfun->language might have been already allocated by the use of
11673 : : -Wunused-local-typedefs. In that case, just re-use it. */
11674 : 1603 : if (p == NULL)
11675 : 1547 : cfun->language = p = ggc_cleared_alloc<language_function> ();
11676 : :
11677 : 1603 : p->base.x_stmt_tree = c_stmt_tree;
11678 : 1603 : c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11679 : 1603 : p->x_in_statement = in_statement;
11680 : 1603 : p->x_switch_stack = c_switch_stack;
11681 : 1603 : p->loop_names = loop_names;
11682 : 1603 : loop_names = vNULL;
11683 : 1603 : p->loop_names_hash = loop_names_hash;
11684 : 1603 : loop_names_hash = NULL;
11685 : 1603 : p->arg_info = current_function_arg_info;
11686 : 1603 : p->returns_value = current_function_returns_value;
11687 : 1603 : p->returns_null = current_function_returns_null;
11688 : 1603 : p->returns_abnormally = current_function_returns_abnormally;
11689 : 1603 : p->warn_about_return_type = warn_about_return_type;
11690 : :
11691 : 1603 : push_function_context ();
11692 : 1603 : }
11693 : :
11694 : : /* Restore the variables used during compilation of a C function. */
11695 : :
11696 : : void
11697 : 1603 : c_pop_function_context (void)
11698 : : {
11699 : 1603 : struct language_function *p;
11700 : :
11701 : 1603 : pop_function_context ();
11702 : 1603 : p = cfun->language;
11703 : :
11704 : : /* When -Wunused-local-typedefs is in effect, cfun->languages is
11705 : : used to store data throughout the life time of the current cfun,
11706 : : So don't deallocate it. */
11707 : 1603 : if (!warn_unused_local_typedefs)
11708 : 1547 : cfun->language = NULL;
11709 : :
11710 : 1603 : if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11711 : 1603 : && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11712 : : {
11713 : : /* Stop pointing to the local nodes about to be freed. */
11714 : : /* But DECL_INITIAL must remain nonzero so we know this
11715 : : was an actual function definition. */
11716 : 0 : DECL_INITIAL (current_function_decl) = error_mark_node;
11717 : 0 : DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11718 : : }
11719 : :
11720 : 1603 : c_stmt_tree = p->base.x_stmt_tree;
11721 : 1603 : p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11722 : 1603 : in_statement = p->x_in_statement;
11723 : 1603 : c_switch_stack = p->x_switch_stack;
11724 : 1603 : loop_names.release ();
11725 : 1603 : loop_names = p->loop_names;
11726 : 1603 : p->loop_names = vNULL;
11727 : 1603 : delete loop_names_hash;
11728 : 1603 : loop_names_hash = p->loop_names_hash;
11729 : 1603 : p->loop_names_hash = NULL;
11730 : 1603 : current_function_arg_info = p->arg_info;
11731 : 1603 : current_function_returns_value = p->returns_value;
11732 : 1603 : current_function_returns_null = p->returns_null;
11733 : 1603 : current_function_returns_abnormally = p->returns_abnormally;
11734 : 1603 : warn_about_return_type = p->warn_about_return_type;
11735 : 1603 : }
11736 : :
11737 : : /* The functions below are required for functionality of doing
11738 : : function at once processing in the C front end. Currently these
11739 : : functions are not called from anywhere in the C front end, but as
11740 : : these changes continue, that will change. */
11741 : :
11742 : : /* Returns the stmt_tree (if any) to which statements are currently
11743 : : being added. If there is no active statement-tree, NULL is
11744 : : returned. */
11745 : :
11746 : : stmt_tree
11747 : 961750130 : current_stmt_tree (void)
11748 : : {
11749 : 961750130 : return &c_stmt_tree;
11750 : : }
11751 : :
11752 : : /* Return the global value of T as a symbol. */
11753 : :
11754 : : tree
11755 : 3992662 : identifier_global_value (tree t)
11756 : : {
11757 : 3992662 : struct c_binding *b;
11758 : :
11759 : 3993629 : for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11760 : 3986402 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11761 : 3985435 : return b->decl;
11762 : :
11763 : : return NULL_TREE;
11764 : : }
11765 : :
11766 : : /* Return the global value of tag T as a symbol. */
11767 : :
11768 : : tree
11769 : 12 : identifier_global_tag (tree t)
11770 : : {
11771 : 12 : struct c_binding *b;
11772 : :
11773 : 12 : for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11774 : 11 : if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11775 : 11 : return b->decl;
11776 : :
11777 : : return NULL_TREE;
11778 : : }
11779 : :
11780 : : /* Returns non-zero (result of __has_builtin) if NAME refers to a built-in
11781 : : function or function-like operator. */
11782 : :
11783 : : int
11784 : 24706 : names_builtin_p (const char *name)
11785 : : {
11786 : 24706 : tree id = get_identifier (name);
11787 : 24706 : if (tree decl = identifier_global_value (id))
11788 : 24619 : return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11789 : :
11790 : : /* Also detect common reserved C words that aren't strictly built-in
11791 : : functions. */
11792 : 87 : switch (C_RID_CODE (id))
11793 : : {
11794 : : case RID_BUILTIN_ASSOC_BARRIER:
11795 : : case RID_BUILTIN_CONVERTVECTOR:
11796 : : case RID_BUILTIN_HAS_ATTRIBUTE:
11797 : : case RID_BUILTIN_SHUFFLE:
11798 : : case RID_BUILTIN_SHUFFLEVECTOR:
11799 : : case RID_BUILTIN_STDC:
11800 : : case RID_BUILTIN_COUNTED_BY_REF:
11801 : : case RID_CHOOSE_EXPR:
11802 : : case RID_OFFSETOF:
11803 : : case RID_TYPES_COMPATIBLE_P:
11804 : : case RID_C23_VA_START:
11805 : : case RID_VA_ARG:
11806 : : return 1;
11807 : 66 : default:
11808 : 66 : break;
11809 : : }
11810 : :
11811 : 66 : return 0;
11812 : : }
11813 : :
11814 : : /* In C, the only C-linkage public declaration is at file scope. */
11815 : :
11816 : : tree
11817 : 5 : c_linkage_bindings (tree name)
11818 : : {
11819 : 5 : return identifier_global_value (name);
11820 : : }
11821 : :
11822 : : /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11823 : : otherwise the name is found in ridpointers from RID_INDEX. */
11824 : :
11825 : : void
11826 : 3392010 : record_builtin_type (enum rid rid_index, const char *name, tree type)
11827 : : {
11828 : 3392010 : tree id, decl;
11829 : 3392010 : if (name == 0)
11830 : 1582938 : id = ridpointers[(int) rid_index];
11831 : : else
11832 : 1809072 : id = get_identifier (name);
11833 : 3392010 : decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11834 : 3392010 : pushdecl (decl);
11835 : 3392010 : if (debug_hooks->type_decl)
11836 : 3392010 : debug_hooks->type_decl (decl, false);
11837 : 3392010 : }
11838 : :
11839 : : /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11840 : :
11841 : : struct c_parm *
11842 : 121124649 : build_c_parm (struct c_declspecs *specs, tree attrs,
11843 : : struct c_declarator *declarator,
11844 : : location_t loc)
11845 : : {
11846 : 121124649 : struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11847 : 121124649 : ret->specs = specs;
11848 : 121124649 : ret->attrs = attrs;
11849 : 121124649 : ret->declarator = declarator;
11850 : 121124649 : ret->loc = loc;
11851 : 121124649 : return ret;
11852 : : }
11853 : :
11854 : : /* Return a declarator with nested attributes. TARGET is the inner
11855 : : declarator to which these attributes apply. ATTRS are the
11856 : : attributes. */
11857 : :
11858 : : struct c_declarator *
11859 : 6672 : build_attrs_declarator (tree attrs, struct c_declarator *target)
11860 : : {
11861 : 6672 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11862 : 6672 : ret->kind = cdk_attrs;
11863 : 6672 : ret->declarator = target;
11864 : 6672 : ret->u.attrs = attrs;
11865 : 6672 : return ret;
11866 : : }
11867 : :
11868 : : /* Return a declarator for a function with arguments specified by ARGS
11869 : : and return type specified by TARGET. */
11870 : :
11871 : : struct c_declarator *
11872 : 49429393 : build_function_declarator (struct c_arg_info *args,
11873 : : struct c_declarator *target)
11874 : : {
11875 : 49429393 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11876 : 49429393 : ret->kind = cdk_function;
11877 : 49429393 : ret->declarator = target;
11878 : 49429393 : ret->u.arg_info = args;
11879 : 49429393 : return ret;
11880 : : }
11881 : :
11882 : : /* Return a declarator for the identifier IDENT (which may be
11883 : : NULL_TREE for an abstract declarator). */
11884 : :
11885 : : struct c_declarator *
11886 : 306582348 : build_id_declarator (tree ident)
11887 : : {
11888 : 306582348 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11889 : 306582348 : ret->kind = cdk_id;
11890 : 306582348 : ret->declarator = 0;
11891 : 306582348 : ret->u.id.id = ident;
11892 : 306582348 : ret->u.id.attrs = NULL_TREE;
11893 : : /* Default value - may get reset to a more precise location. */
11894 : 306582348 : ret->id_loc = input_location;
11895 : 306582348 : return ret;
11896 : : }
11897 : :
11898 : : /* Return something to represent absolute declarators containing a *.
11899 : : TARGET is the absolute declarator that the * contains.
11900 : : TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11901 : : to apply to the pointer type. */
11902 : :
11903 : : struct c_declarator *
11904 : 17915887 : make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11905 : : struct c_declarator *target)
11906 : : {
11907 : 17915887 : tree attrs;
11908 : 17915887 : int quals = 0;
11909 : 17915887 : struct c_declarator *itarget = target;
11910 : 17915887 : struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11911 : 17915887 : if (type_quals_attrs)
11912 : : {
11913 : 17915887 : attrs = type_quals_attrs->attrs;
11914 : 17915887 : quals = quals_from_declspecs (type_quals_attrs);
11915 : 17915887 : if (attrs != NULL_TREE)
11916 : 6400 : itarget = build_attrs_declarator (attrs, target);
11917 : : }
11918 : 17915887 : ret->kind = cdk_pointer;
11919 : 17915887 : ret->declarator = itarget;
11920 : 17915887 : ret->u.pointer_quals = quals;
11921 : 17915887 : return ret;
11922 : : }
11923 : :
11924 : : /* Return a pointer to a structure for an empty list of declaration
11925 : : specifiers. */
11926 : :
11927 : : struct c_declspecs *
11928 : 443959985 : build_null_declspecs (void)
11929 : : {
11930 : 443959985 : struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11931 : 443959985 : memset (ret, 0, sizeof *ret);
11932 : 443959985 : ret->align_log = -1;
11933 : 443959985 : ret->typespec_word = cts_none;
11934 : 443959985 : ret->storage_class = csc_none;
11935 : 443959985 : ret->expr_const_operands = true;
11936 : 443959985 : ret->typespec_kind = ctsk_none;
11937 : 443959985 : ret->address_space = ADDR_SPACE_GENERIC;
11938 : 443959985 : return ret;
11939 : : }
11940 : :
11941 : : /* Add the address space ADDRSPACE to the declaration specifiers
11942 : : SPECS, returning SPECS. */
11943 : :
11944 : : struct c_declspecs *
11945 : 174 : declspecs_add_addrspace (location_t location,
11946 : : struct c_declspecs *specs, addr_space_t as)
11947 : : {
11948 : 174 : specs->non_sc_seen_p = true;
11949 : 174 : specs->declspecs_seen_p = true;
11950 : 174 : specs->non_std_attrs_seen_p = true;
11951 : :
11952 : 174 : if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11953 : 0 : && specs->address_space != as)
11954 : 0 : error ("incompatible address space qualifiers %qs and %qs",
11955 : : c_addr_space_name (as),
11956 : : c_addr_space_name (specs->address_space));
11957 : : else
11958 : : {
11959 : 174 : specs->address_space = as;
11960 : 174 : specs->locations[cdw_address_space] = location;
11961 : : }
11962 : 174 : return specs;
11963 : : }
11964 : :
11965 : : /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11966 : : returning SPECS. */
11967 : :
11968 : : struct c_declspecs *
11969 : 16260521 : declspecs_add_qual (location_t loc,
11970 : : struct c_declspecs *specs, tree qual)
11971 : : {
11972 : 16260521 : enum rid i;
11973 : 16260521 : bool dupe = false;
11974 : 16260521 : specs->non_sc_seen_p = true;
11975 : 16260521 : specs->declspecs_seen_p = true;
11976 : 16260521 : specs->non_std_attrs_seen_p = true;
11977 : 16260521 : gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11978 : : && C_IS_RESERVED_WORD (qual));
11979 : 16260521 : i = C_RID_CODE (qual);
11980 : 16260521 : location_t prev_loc = UNKNOWN_LOCATION;
11981 : 16260521 : switch (i)
11982 : : {
11983 : 12808226 : case RID_CONST:
11984 : 12808226 : dupe = specs->const_p;
11985 : 12808226 : specs->const_p = true;
11986 : 12808226 : prev_loc = specs->locations[cdw_const];
11987 : 12808226 : specs->locations[cdw_const] = loc;
11988 : 12808226 : break;
11989 : 95548 : case RID_VOLATILE:
11990 : 95548 : dupe = specs->volatile_p;
11991 : 95548 : specs->volatile_p = true;
11992 : 95548 : prev_loc = specs->locations[cdw_volatile];
11993 : 95548 : specs->locations[cdw_volatile] = loc;
11994 : 95548 : break;
11995 : 3337798 : case RID_RESTRICT:
11996 : 3337798 : dupe = specs->restrict_p;
11997 : 3337798 : specs->restrict_p = true;
11998 : 3337798 : prev_loc = specs->locations[cdw_restrict];
11999 : 3337798 : specs->locations[cdw_restrict] = loc;
12000 : 3337798 : break;
12001 : 18949 : case RID_ATOMIC:
12002 : 18949 : dupe = specs->atomic_p;
12003 : 18949 : specs->atomic_p = true;
12004 : 18949 : prev_loc = specs->locations[cdw_atomic];
12005 : 18949 : specs->locations[cdw_atomic] = loc;
12006 : 18949 : break;
12007 : 0 : default:
12008 : 0 : gcc_unreachable ();
12009 : : }
12010 : 16260521 : if (dupe)
12011 : : {
12012 : 56 : bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
12013 : : "duplicate %qE declaration specifier", qual);
12014 : 56 : if (!warned
12015 : 52 : && warn_duplicate_decl_specifier
12016 : 33 : && prev_loc >= RESERVED_LOCATION_COUNT
12017 : 33 : && !from_macro_expansion_at (prev_loc)
12018 : 77 : && !from_macro_expansion_at (loc))
12019 : 12 : warning_at (loc, OPT_Wduplicate_decl_specifier,
12020 : : "duplicate %qE declaration specifier", qual);
12021 : : }
12022 : 16260521 : return specs;
12023 : : }
12024 : :
12025 : : /* Add the type specifier TYPE to the declaration specifiers SPECS,
12026 : : returning SPECS. */
12027 : :
12028 : : struct c_declspecs *
12029 : 323877093 : declspecs_add_type (location_t loc, struct c_declspecs *specs,
12030 : : struct c_typespec spec)
12031 : : {
12032 : 323877093 : tree type = spec.spec;
12033 : 323877093 : specs->non_sc_seen_p = true;
12034 : 323877093 : specs->declspecs_seen_p = true;
12035 : 323877093 : specs->non_std_attrs_seen_p = true;
12036 : 323877093 : specs->typespec_kind = spec.kind;
12037 : 323877093 : if (TREE_DEPRECATED (type))
12038 : 56 : specs->deprecated_p = true;
12039 : 323877093 : if (TREE_UNAVAILABLE (type))
12040 : 40 : specs->unavailable_p = true;
12041 : :
12042 : : /* As a type specifier is present, "auto" must be used as a storage
12043 : : class specifier, not for type deduction. */
12044 : 323877093 : if (specs->c23_auto_p)
12045 : : {
12046 : 115 : specs->c23_auto_p = false;
12047 : 115 : if (specs->storage_class != csc_none)
12048 : 1 : error ("multiple storage classes in declaration specifiers");
12049 : 114 : else if (specs->thread_p)
12050 : 1 : error ("%qs used with %<auto%>",
12051 : 1 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
12052 : 113 : else if (specs->constexpr_p)
12053 : : /* auto may only be used with another storage class specifier,
12054 : : such as constexpr, if the type is inferred. */
12055 : 2 : error ("%<auto%> used with %<constexpr%>");
12056 : : else
12057 : 111 : specs->storage_class = csc_auto;
12058 : : }
12059 : :
12060 : : /* Handle type specifier keywords. */
12061 : 323877093 : if (TREE_CODE (type) == IDENTIFIER_NODE
12062 : 81115313 : && C_IS_RESERVED_WORD (type)
12063 : 404992406 : && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
12064 : : {
12065 : 81115313 : enum rid i = C_RID_CODE (type);
12066 : 81115313 : if (specs->type)
12067 : : {
12068 : 58 : error_at (loc, "two or more data types in declaration specifiers");
12069 : 58 : return specs;
12070 : : }
12071 : 81115255 : if ((int) i <= (int) RID_LAST_MODIFIER)
12072 : : {
12073 : : /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
12074 : 21374704 : bool dupe = false;
12075 : 21374704 : switch (i)
12076 : : {
12077 : 10537123 : case RID_LONG:
12078 : 10537123 : if (specs->long_long_p)
12079 : : {
12080 : 104 : error_at (loc, "%<long long long%> is too long for GCC");
12081 : 104 : break;
12082 : : }
12083 : 10537019 : if (specs->long_p)
12084 : : {
12085 : 2890075 : if (specs->typespec_word == cts_double)
12086 : : {
12087 : 15 : error_at (loc,
12088 : : "both %qs and %qs in declaration specifiers",
12089 : : "long long", "double");
12090 : 15 : break;
12091 : : }
12092 : 2890060 : pedwarn_c90 (loc, OPT_Wlong_long,
12093 : : "ISO C90 does not support %<long long%>");
12094 : 2890060 : specs->long_long_p = 1;
12095 : 2890060 : specs->locations[cdw_long_long] = loc;
12096 : 2890060 : break;
12097 : : }
12098 : 7646944 : if (specs->short_p)
12099 : 77 : error_at (loc,
12100 : : "both %qs and %qs in declaration specifiers",
12101 : : "long", "short");
12102 : 7646867 : else if (specs->typespec_word == cts_auto_type)
12103 : 1 : error_at (loc,
12104 : : "both %qs and %qs in declaration specifiers",
12105 : : "long", "__auto_type");
12106 : : else if (specs->typespec_word == cts_void)
12107 : 5 : error_at (loc,
12108 : : "both %qs and %qs in declaration specifiers",
12109 : : "long", "void");
12110 : : else if (specs->typespec_word == cts_int_n)
12111 : 19 : error_at (loc,
12112 : : "both %qs and %<__int%d%> in declaration specifiers",
12113 : 19 : "long", int_n_data[specs->u.int_n_idx].bitsize);
12114 : : else if (specs->typespec_word == cts_bool)
12115 : 3 : error_at (loc,
12116 : : "both %qs and %qs in declaration specifiers",
12117 : : "long", "_Bool");
12118 : : else if (specs->typespec_word == cts_bitint)
12119 : 3 : error_at (loc,
12120 : : "both %qs and %qs in declaration specifiers",
12121 : : "long", "_BitInt");
12122 : : else if (specs->typespec_word == cts_char)
12123 : 21 : error_at (loc,
12124 : : "both %qs and %qs in declaration specifiers",
12125 : : "long", "char");
12126 : : else if (specs->typespec_word == cts_float)
12127 : 7 : error_at (loc,
12128 : : "both %qs and %qs in declaration specifiers",
12129 : : "long", "float");
12130 : : else if (specs->typespec_word == cts_floatn_nx)
12131 : 0 : error_at (loc,
12132 : : "both %qs and %<_Float%d%s%> in declaration "
12133 : : "specifiers", "long",
12134 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12135 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12136 : : ? "x"
12137 : : : "");
12138 : : else if (specs->typespec_word == cts_dfloat32)
12139 : 4 : error_at (loc,
12140 : : "both %qs and %qs in declaration specifiers",
12141 : : "long", "_Decimal32");
12142 : : else if (specs->typespec_word == cts_dfloat64)
12143 : 4 : error_at (loc,
12144 : : "both %qs and %qs in declaration specifiers",
12145 : : "long", "_Decimal64");
12146 : : else if (specs->typespec_word == cts_dfloat128)
12147 : 4 : error_at (loc,
12148 : : "both %qs and %qs in declaration specifiers",
12149 : : "long", "_Decimal128");
12150 : : else if (specs->typespec_word == cts_dfloat64x)
12151 : 0 : error_at (loc,
12152 : : "both %qs and %qs in declaration specifiers",
12153 : : "long", "_Decimal64x");
12154 : : else
12155 : : {
12156 : 7646796 : specs->long_p = true;
12157 : 7646796 : specs->locations[cdw_long] = loc;
12158 : : }
12159 : : break;
12160 : 1666978 : case RID_SHORT:
12161 : 1666978 : dupe = specs->short_p;
12162 : 1666978 : if (specs->long_p)
12163 : 197 : error_at (loc,
12164 : : "both %qs and %qs in declaration specifiers",
12165 : : "long", "short");
12166 : 1666781 : else if (specs->typespec_word == cts_auto_type)
12167 : 1 : error_at (loc,
12168 : : "both %qs and %qs in declaration specifiers",
12169 : : "short", "__auto_type");
12170 : : else if (specs->typespec_word == cts_void)
12171 : 5 : error_at (loc,
12172 : : "both %qs and %qs in declaration specifiers",
12173 : : "short", "void");
12174 : : else if (specs->typespec_word == cts_int_n)
12175 : 19 : error_at (loc,
12176 : : "both %qs and %<__int%d%> in declaration specifiers",
12177 : 19 : "short", int_n_data[specs->u.int_n_idx].bitsize);
12178 : : else if (specs->typespec_word == cts_bool)
12179 : 3 : error_at (loc,
12180 : : "both %qs and %qs in declaration specifiers",
12181 : : "short", "_Bool");
12182 : : else if (specs->typespec_word == cts_bitint)
12183 : 1 : error_at (loc,
12184 : : "both %qs and %qs in declaration specifiers",
12185 : : "short", "_BitInt");
12186 : : else if (specs->typespec_word == cts_char)
12187 : 21 : error_at (loc,
12188 : : "both %qs and %qs in declaration specifiers",
12189 : : "short", "char");
12190 : : else if (specs->typespec_word == cts_float)
12191 : 7 : error_at (loc,
12192 : : "both %qs and %qs in declaration specifiers",
12193 : : "short", "float");
12194 : : else if (specs->typespec_word == cts_double)
12195 : 7 : error_at (loc,
12196 : : "both %qs and %qs in declaration specifiers",
12197 : : "short", "double");
12198 : : else if (specs->typespec_word == cts_floatn_nx)
12199 : 0 : error_at (loc,
12200 : : "both %qs and %<_Float%d%s%> in declaration "
12201 : : "specifiers", "short",
12202 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12203 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12204 : : ? "x"
12205 : : : "");
12206 : : else if (specs->typespec_word == cts_dfloat32)
12207 : 4 : error_at (loc,
12208 : : "both %qs and %qs in declaration specifiers",
12209 : : "short", "_Decimal32");
12210 : : else if (specs->typespec_word == cts_dfloat64)
12211 : 4 : error_at (loc,
12212 : : "both %qs and %qs in declaration specifiers",
12213 : : "short", "_Decimal64");
12214 : : else if (specs->typespec_word == cts_dfloat128)
12215 : 4 : error_at (loc,
12216 : : "both %qs and %qs in declaration specifiers",
12217 : : "short", "_Decimal128");
12218 : : else if (specs->typespec_word == cts_dfloat64x)
12219 : 0 : error_at (loc,
12220 : : "both %qs and %qs in declaration specifiers",
12221 : : "short", "_Decimal64x");
12222 : : else
12223 : : {
12224 : 1666705 : specs->short_p = true;
12225 : 1666705 : specs->locations[cdw_short] = loc;
12226 : : }
12227 : : break;
12228 : 570164 : case RID_SIGNED:
12229 : 570164 : dupe = specs->signed_p;
12230 : 570164 : if (specs->unsigned_p)
12231 : 138 : error_at (loc,
12232 : : "both %qs and %qs in declaration specifiers",
12233 : : "signed", "unsigned");
12234 : 570026 : else if (specs->typespec_word == cts_auto_type)
12235 : 1 : error_at (loc,
12236 : : "both %qs and %qs in declaration specifiers",
12237 : : "signed", "__auto_type");
12238 : : else if (specs->typespec_word == cts_void)
12239 : 5 : error_at (loc,
12240 : : "both %qs and %qs in declaration specifiers",
12241 : : "signed", "void");
12242 : : else if (specs->typespec_word == cts_bool)
12243 : 3 : error_at (loc,
12244 : : "both %qs and %qs in declaration specifiers",
12245 : : "signed", "_Bool");
12246 : : else if (specs->typespec_word == cts_float)
12247 : 7 : error_at (loc,
12248 : : "both %qs and %qs in declaration specifiers",
12249 : : "signed", "float");
12250 : : else if (specs->typespec_word == cts_double)
12251 : 21 : error_at (loc,
12252 : : "both %qs and %qs in declaration specifiers",
12253 : : "signed", "double");
12254 : : else if (specs->typespec_word == cts_floatn_nx)
12255 : 0 : error_at (loc,
12256 : : "both %qs and %<_Float%d%s%> in declaration "
12257 : : "specifiers", "signed",
12258 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12259 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12260 : : ? "x"
12261 : : : "");
12262 : : else if (specs->typespec_word == cts_dfloat32)
12263 : 4 : error_at (loc,
12264 : : "both %qs and %qs in declaration specifiers",
12265 : : "signed", "_Decimal32");
12266 : : else if (specs->typespec_word == cts_dfloat64)
12267 : 4 : error_at (loc,
12268 : : "both %qs and %qs in declaration specifiers",
12269 : : "signed", "_Decimal64");
12270 : : else if (specs->typespec_word == cts_dfloat128)
12271 : 4 : error_at (loc,
12272 : : "both %qs and %qs in declaration specifiers",
12273 : : "signed", "_Decimal128");
12274 : : else if (specs->typespec_word == cts_dfloat64x)
12275 : 0 : error_at (loc,
12276 : : "both %qs and %qs in declaration specifiers",
12277 : : "signed", "_Decimal64x");
12278 : : else
12279 : : {
12280 : 569977 : specs->signed_p = true;
12281 : 569977 : specs->locations[cdw_signed] = loc;
12282 : : }
12283 : : break;
12284 : 6053350 : case RID_UNSIGNED:
12285 : 6053350 : dupe = specs->unsigned_p;
12286 : 6053350 : if (specs->signed_p)
12287 : 139 : error_at (loc,
12288 : : "both %qs and %qs in declaration specifiers",
12289 : : "signed", "unsigned");
12290 : 6053211 : else if (specs->typespec_word == cts_auto_type)
12291 : 1 : error_at (loc,
12292 : : "both %qs and %qs in declaration specifiers",
12293 : : "unsigned", "__auto_type");
12294 : : else if (specs->typespec_word == cts_void)
12295 : 5 : error_at (loc,
12296 : : "both %qs and %qs in declaration specifiers",
12297 : : "unsigned", "void");
12298 : : else if (specs->typespec_word == cts_bool)
12299 : 3 : error_at (loc,
12300 : : "both %qs and %qs in declaration specifiers",
12301 : : "unsigned", "_Bool");
12302 : : else if (specs->typespec_word == cts_float)
12303 : 7 : error_at (loc,
12304 : : "both %qs and %qs in declaration specifiers",
12305 : : "unsigned", "float");
12306 : : else if (specs->typespec_word == cts_double)
12307 : 21 : error_at (loc,
12308 : : "both %qs and %qs in declaration specifiers",
12309 : : "unsigned", "double");
12310 : : else if (specs->typespec_word == cts_floatn_nx)
12311 : 0 : error_at (loc,
12312 : : "both %qs and %<_Float%d%s%> in declaration "
12313 : : "specifiers", "unsigned",
12314 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12315 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12316 : : ? "x"
12317 : : : "");
12318 : : else if (specs->typespec_word == cts_dfloat32)
12319 : 2 : error_at (loc,
12320 : : "both %qs and %qs in declaration specifiers",
12321 : : "unsigned", "_Decimal32");
12322 : : else if (specs->typespec_word == cts_dfloat64)
12323 : 2 : error_at (loc,
12324 : : "both %qs and %qs in declaration specifiers",
12325 : : "unsigned", "_Decimal64");
12326 : : else if (specs->typespec_word == cts_dfloat128)
12327 : 2 : error_at (loc,
12328 : : "both %qs and %qs in declaration specifiers",
12329 : : "unsigned", "_Decimal128");
12330 : : else if (specs->typespec_word == cts_dfloat64x)
12331 : 0 : error_at (loc,
12332 : : "both %qs and %qs in declaration specifiers",
12333 : : "unsigned", "_Decimal64x");
12334 : : else
12335 : : {
12336 : 6053168 : specs->unsigned_p = true;
12337 : 6053168 : specs->locations[cdw_unsigned] = loc;
12338 : : }
12339 : : break;
12340 : 2546966 : case RID_COMPLEX:
12341 : 2546966 : dupe = specs->complex_p;
12342 : 2546966 : if (!in_system_header_at (loc))
12343 : 68932 : pedwarn_c90 (loc, OPT_Wpedantic,
12344 : : "ISO C90 does not support complex types");
12345 : 2546966 : if (specs->typespec_word == cts_auto_type)
12346 : 1 : error_at (loc,
12347 : : "both %qs and %qs in declaration specifiers",
12348 : : "complex", "__auto_type");
12349 : : else if (specs->typespec_word == cts_void)
12350 : 2 : error_at (loc,
12351 : : "both %qs and %qs in declaration specifiers",
12352 : : "complex", "void");
12353 : : else if (specs->typespec_word == cts_bool)
12354 : 2 : error_at (loc,
12355 : : "both %qs and %qs in declaration specifiers",
12356 : : "complex", "_Bool");
12357 : : else if (specs->typespec_word == cts_bitint)
12358 : 1 : error_at (loc,
12359 : : "both %qs and %qs in declaration specifiers",
12360 : : "complex", "_BitInt");
12361 : : else if (specs->typespec_word == cts_dfloat32)
12362 : 1 : error_at (loc,
12363 : : "both %qs and %qs in declaration specifiers",
12364 : : "complex", "_Decimal32");
12365 : : else if (specs->typespec_word == cts_dfloat64)
12366 : 1 : error_at (loc,
12367 : : "both %qs and %qs in declaration specifiers",
12368 : : "complex", "_Decimal64");
12369 : : else if (specs->typespec_word == cts_dfloat128)
12370 : 1 : error_at (loc,
12371 : : "both %qs and %qs in declaration specifiers",
12372 : : "complex", "_Decimal128");
12373 : : else if (specs->typespec_word == cts_dfloat64x)
12374 : 0 : error_at (loc,
12375 : : "both %qs and %qs in declaration specifiers",
12376 : : "complex", "_Decimal64x");
12377 : : else if (specs->typespec_word == cts_fract)
12378 : 0 : error_at (loc,
12379 : : "both %qs and %qs in declaration specifiers",
12380 : : "complex", "_Fract");
12381 : : else if (specs->typespec_word == cts_accum)
12382 : 0 : error_at (loc,
12383 : : "both %qs and %qs in declaration specifiers",
12384 : : "complex", "_Accum");
12385 : 2546957 : else if (specs->saturating_p)
12386 : 0 : error_at (loc,
12387 : : "both %qs and %qs in declaration specifiers",
12388 : : "complex", "_Sat");
12389 : : else
12390 : : {
12391 : 2546957 : specs->complex_p = true;
12392 : 2546957 : specs->locations[cdw_complex] = loc;
12393 : : }
12394 : : break;
12395 : 123 : case RID_SAT:
12396 : 123 : dupe = specs->saturating_p;
12397 : 123 : pedwarn (loc, OPT_Wpedantic,
12398 : : "ISO C does not support saturating types");
12399 : 123 : if (specs->typespec_word == cts_int_n)
12400 : 0 : error_at (loc,
12401 : : "both %qs and %<__int%d%> in declaration specifiers",
12402 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12403 : : else if (specs->typespec_word == cts_auto_type)
12404 : 0 : error_at (loc,
12405 : : "both %qs and %qs in declaration specifiers",
12406 : : "_Sat", "__auto_type");
12407 : : else if (specs->typespec_word == cts_void)
12408 : 0 : error_at (loc,
12409 : : "both %qs and %qs in declaration specifiers",
12410 : : "_Sat", "void");
12411 : : else if (specs->typespec_word == cts_bool)
12412 : 0 : error_at (loc,
12413 : : "both %qs and %qs in declaration specifiers",
12414 : : "_Sat", "_Bool");
12415 : : else if (specs->typespec_word == cts_bitint)
12416 : 0 : error_at (loc,
12417 : : "both %qs and %qs in declaration specifiers",
12418 : : "_Sat", "_BitInt");
12419 : : else if (specs->typespec_word == cts_char)
12420 : 0 : error_at (loc,
12421 : : "both %qs and %qs in declaration specifiers",
12422 : : "_Sat", "char");
12423 : : else if (specs->typespec_word == cts_int)
12424 : 0 : error_at (loc,
12425 : : "both %qs and %qs in declaration specifiers",
12426 : : "_Sat", "int");
12427 : : else if (specs->typespec_word == cts_float)
12428 : 0 : error_at (loc,
12429 : : "both %qs and %qs in declaration specifiers",
12430 : : "_Sat", "float");
12431 : : else if (specs->typespec_word == cts_double)
12432 : 0 : error_at (loc,
12433 : : "both %qs and %qs in declaration specifiers",
12434 : : "_Sat", "double");
12435 : : else if (specs->typespec_word == cts_floatn_nx)
12436 : 0 : error_at (loc,
12437 : : "both %qs and %<_Float%d%s%> in declaration "
12438 : : "specifiers", "_Sat",
12439 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12440 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12441 : : ? "x"
12442 : : : "");
12443 : : else if (specs->typespec_word == cts_dfloat32)
12444 : 0 : error_at (loc,
12445 : : "both %qs and %qs in declaration specifiers",
12446 : : "_Sat", "_Decimal32");
12447 : : else if (specs->typespec_word == cts_dfloat64)
12448 : 0 : error_at (loc,
12449 : : "both %qs and %qs in declaration specifiers",
12450 : : "_Sat", "_Decimal64");
12451 : : else if (specs->typespec_word == cts_dfloat128)
12452 : 0 : error_at (loc,
12453 : : "both %qs and %qs in declaration specifiers",
12454 : : "_Sat", "_Decimal128");
12455 : : else if (specs->typespec_word == cts_dfloat64x)
12456 : 0 : error_at (loc,
12457 : : "both %qs and %qs in declaration specifiers",
12458 : : "_Sat", "_Decimal64x");
12459 : 123 : else if (specs->complex_p)
12460 : 0 : error_at (loc,
12461 : : "both %qs and %qs in declaration specifiers",
12462 : : "_Sat", "complex");
12463 : : else
12464 : : {
12465 : 123 : specs->saturating_p = true;
12466 : 123 : specs->locations[cdw_saturating] = loc;
12467 : : }
12468 : : break;
12469 : 0 : default:
12470 : 0 : gcc_unreachable ();
12471 : : }
12472 : :
12473 : 21374704 : if (dupe)
12474 : 378 : error_at (loc, "duplicate %qE", type);
12475 : :
12476 : 21374704 : return specs;
12477 : : }
12478 : : else
12479 : : {
12480 : : /* "void", "_Bool", "char", "int", "float", "double",
12481 : : "_FloatN", "_FloatNx", "_Decimal32", "__intN",
12482 : : "_Decimal64", "_Decimal128", "_Fract", "_Accum", "_BitInt(N)" or
12483 : : "__auto_type". */
12484 : 59740551 : if (specs->typespec_word != cts_none)
12485 : : {
12486 : 2171 : if (i == RID_BOOL)
12487 : : {
12488 : 175 : auto_diagnostic_group d;
12489 : 175 : if (specs->storage_class == csc_typedef)
12490 : 4 : error_at (loc,
12491 : : "%qs cannot be defined via %<typedef%>",
12492 : 2 : IDENTIFIER_POINTER (type));
12493 : : else
12494 : 346 : error_at (loc,
12495 : : "%qs cannot be used here",
12496 : 173 : IDENTIFIER_POINTER (type));
12497 : 175 : add_note_about_new_keyword (loc, type);
12498 : 175 : }
12499 : : else
12500 : 1996 : error_at (loc,
12501 : : "two or more data types in declaration specifiers");
12502 : 2171 : return specs;
12503 : : }
12504 : 59738380 : switch (i)
12505 : : {
12506 : 1838 : case RID_AUTO_TYPE:
12507 : 1838 : if (specs->long_p)
12508 : 1 : error_at (loc,
12509 : : "both %qs and %qs in declaration specifiers",
12510 : : "long", "__auto_type");
12511 : 1837 : else if (specs->short_p)
12512 : 1 : error_at (loc,
12513 : : "both %qs and %qs in declaration specifiers",
12514 : : "short", "__auto_type");
12515 : 1836 : else if (specs->signed_p)
12516 : 1 : error_at (loc,
12517 : : "both %qs and %qs in declaration specifiers",
12518 : : "signed", "__auto_type");
12519 : 1835 : else if (specs->unsigned_p)
12520 : 1 : error_at (loc,
12521 : : "both %qs and %qs in declaration specifiers",
12522 : : "unsigned", "__auto_type");
12523 : 1834 : else if (specs->complex_p)
12524 : 1 : error_at (loc,
12525 : : "both %qs and %qs in declaration specifiers",
12526 : : "complex", "__auto_type");
12527 : 1833 : else if (specs->saturating_p)
12528 : 0 : error_at (loc,
12529 : : "both %qs and %qs in declaration specifiers",
12530 : : "_Sat", "__auto_type");
12531 : : else
12532 : : {
12533 : 1833 : specs->typespec_word = cts_auto_type;
12534 : 1833 : specs->locations[cdw_typespec] = loc;
12535 : : }
12536 : 1838 : return specs;
12537 : 51357 : case RID_INT_N_0:
12538 : 51357 : case RID_INT_N_1:
12539 : 51357 : case RID_INT_N_2:
12540 : 51357 : case RID_INT_N_3:
12541 : 51357 : specs->u.int_n_idx = i - RID_INT_N_0;
12542 : 51357 : if (!in_system_header_at (input_location)
12543 : : /* If the INT_N type ends in "__", and so is of the format
12544 : : "__intN__", don't pedwarn. */
12545 : 51357 : && (strncmp (IDENTIFIER_POINTER (type)
12546 : 40951 : + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
12547 : 40951 : pedwarn (loc, OPT_Wpedantic,
12548 : : "ISO C does not support %<__int%d%> types",
12549 : 40951 : int_n_data[specs->u.int_n_idx].bitsize);
12550 : :
12551 : 51357 : if (specs->long_p)
12552 : 53 : error_at (loc,
12553 : : "both %<__int%d%> and %qs in declaration specifiers",
12554 : 53 : int_n_data[specs->u.int_n_idx].bitsize, "long");
12555 : 51304 : else if (specs->saturating_p)
12556 : 0 : error_at (loc,
12557 : : "both %qs and %<__int%d%> in declaration specifiers",
12558 : 0 : "_Sat", int_n_data[specs->u.int_n_idx].bitsize);
12559 : 51304 : else if (specs->short_p)
12560 : 19 : error_at (loc,
12561 : : "both %<__int%d%> and %qs in declaration specifiers",
12562 : 19 : int_n_data[specs->u.int_n_idx].bitsize, "short");
12563 : 51285 : else if (! int_n_enabled_p[specs->u.int_n_idx])
12564 : : {
12565 : 0 : specs->typespec_word = cts_int_n;
12566 : 0 : error_at (loc,
12567 : : "%<__int%d%> is not supported on this target",
12568 : 0 : int_n_data[specs->u.int_n_idx].bitsize);
12569 : : }
12570 : : else
12571 : : {
12572 : 51285 : specs->typespec_word = cts_int_n;
12573 : 51285 : specs->locations[cdw_typespec] = loc;
12574 : : }
12575 : 51357 : return specs;
12576 : 7680947 : case RID_VOID:
12577 : 7680947 : if (specs->long_p)
12578 : 44 : error_at (loc,
12579 : : "both %qs and %qs in declaration specifiers",
12580 : : "long", "void");
12581 : 7680903 : else if (specs->short_p)
12582 : 21 : error_at (loc,
12583 : : "both %qs and %qs in declaration specifiers",
12584 : : "short", "void");
12585 : 7680882 : else if (specs->signed_p)
12586 : 5 : error_at (loc,
12587 : : "both %qs and %qs in declaration specifiers",
12588 : : "signed", "void");
12589 : 7680877 : else if (specs->unsigned_p)
12590 : 5 : error_at (loc,
12591 : : "both %qs and %qs in declaration specifiers",
12592 : : "unsigned", "void");
12593 : 7680872 : else if (specs->complex_p)
12594 : 2 : error_at (loc,
12595 : : "both %qs and %qs in declaration specifiers",
12596 : : "complex", "void");
12597 : 7680870 : else if (specs->saturating_p)
12598 : 0 : error_at (loc,
12599 : : "both %qs and %qs in declaration specifiers",
12600 : : "_Sat", "void");
12601 : : else
12602 : : {
12603 : 7680870 : specs->typespec_word = cts_void;
12604 : 7680870 : specs->locations[cdw_typespec] = loc;
12605 : : }
12606 : 7680947 : return specs;
12607 : 82563 : case RID_BOOL:
12608 : 82563 : if (!in_system_header_at (loc))
12609 : 68244 : pedwarn_c90 (loc, OPT_Wpedantic,
12610 : : "ISO C90 does not support boolean types");
12611 : 82563 : if (specs->long_p)
12612 : 27 : error_at (loc,
12613 : : "both %qs and %qs in declaration specifiers",
12614 : : "long", "_Bool");
12615 : 82536 : else if (specs->short_p)
12616 : 11 : error_at (loc,
12617 : : "both %qs and %qs in declaration specifiers",
12618 : : "short", "_Bool");
12619 : 82525 : else if (specs->signed_p)
12620 : 3 : error_at (loc,
12621 : : "both %qs and %qs in declaration specifiers",
12622 : : "signed", "_Bool");
12623 : 82522 : else if (specs->unsigned_p)
12624 : 3 : error_at (loc,
12625 : : "both %qs and %qs in declaration specifiers",
12626 : : "unsigned", "_Bool");
12627 : 82519 : else if (specs->complex_p)
12628 : 2 : error_at (loc,
12629 : : "both %qs and %qs in declaration specifiers",
12630 : : "complex", "_Bool");
12631 : 82517 : else if (specs->saturating_p)
12632 : 0 : error_at (loc,
12633 : : "both %qs and %qs in declaration specifiers",
12634 : : "_Sat", "_Bool");
12635 : : else
12636 : : {
12637 : 82517 : specs->typespec_word = cts_bool;
12638 : 82517 : specs->locations[cdw_typespec] = loc;
12639 : : }
12640 : 82563 : return specs;
12641 : 8943592 : case RID_CHAR:
12642 : 8943592 : if (specs->long_p)
12643 : 44 : error_at (loc,
12644 : : "both %qs and %qs in declaration specifiers",
12645 : : "long", "char");
12646 : 8943548 : else if (specs->short_p)
12647 : 21 : error_at (loc,
12648 : : "both %qs and %qs in declaration specifiers",
12649 : : "short", "char");
12650 : 8943527 : else if (specs->saturating_p)
12651 : 0 : error_at (loc,
12652 : : "both %qs and %qs in declaration specifiers",
12653 : : "_Sat", "char");
12654 : : else
12655 : : {
12656 : 8943527 : specs->typespec_word = cts_char;
12657 : 8943527 : specs->locations[cdw_typespec] = loc;
12658 : : }
12659 : 8943592 : return specs;
12660 : 23393042 : case RID_INT:
12661 : 23393042 : if (specs->saturating_p)
12662 : 0 : error_at (loc,
12663 : : "both %qs and %qs in declaration specifiers",
12664 : : "_Sat", "int");
12665 : : else
12666 : : {
12667 : 23393042 : specs->typespec_word = cts_int;
12668 : 23393042 : specs->locations[cdw_typespec] = loc;
12669 : : }
12670 : 23393042 : return specs;
12671 : 3337033 : case RID_FLOAT:
12672 : 3337033 : if (specs->long_p)
12673 : 44 : error_at (loc,
12674 : : "both %qs and %qs in declaration specifiers",
12675 : : "long", "float");
12676 : 3336989 : else if (specs->short_p)
12677 : 21 : error_at (loc,
12678 : : "both %qs and %qs in declaration specifiers",
12679 : : "short", "float");
12680 : 3336968 : else if (specs->signed_p)
12681 : 5 : error_at (loc,
12682 : : "both %qs and %qs in declaration specifiers",
12683 : : "signed", "float");
12684 : 3336963 : else if (specs->unsigned_p)
12685 : 5 : error_at (loc,
12686 : : "both %qs and %qs in declaration specifiers",
12687 : : "unsigned", "float");
12688 : 3336958 : else if (specs->saturating_p)
12689 : 0 : error_at (loc,
12690 : : "both %qs and %qs in declaration specifiers",
12691 : : "_Sat", "float");
12692 : : else
12693 : : {
12694 : 3336958 : specs->typespec_word = cts_float;
12695 : 3336958 : specs->locations[cdw_typespec] = loc;
12696 : : }
12697 : 3337033 : return specs;
12698 : 6016539 : case RID_DOUBLE:
12699 : 6016539 : if (specs->long_long_p)
12700 : 22 : error_at (loc,
12701 : : "both %qs and %qs in declaration specifiers",
12702 : : "long long", "double");
12703 : 6016517 : else if (specs->short_p)
12704 : 21 : error_at (loc,
12705 : : "both %qs and %qs in declaration specifiers",
12706 : : "short", "double");
12707 : 6016496 : else if (specs->signed_p)
12708 : 13 : error_at (loc,
12709 : : "both %qs and %qs in declaration specifiers",
12710 : : "signed", "double");
12711 : 6016483 : else if (specs->unsigned_p)
12712 : 13 : error_at (loc,
12713 : : "both %qs and %qs in declaration specifiers",
12714 : : "unsigned", "double");
12715 : 6016470 : else if (specs->saturating_p)
12716 : 0 : error_at (loc,
12717 : : "both %qs and %qs in declaration specifiers",
12718 : : "_Sat", "double");
12719 : : else
12720 : : {
12721 : 6016470 : specs->typespec_word = cts_double;
12722 : 6016470 : specs->locations[cdw_typespec] = loc;
12723 : : }
12724 : 6016539 : return specs;
12725 : 10138127 : CASE_RID_FLOATN_NX:
12726 : 10138127 : specs->u.floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12727 : 10138127 : if (!in_system_header_at (input_location))
12728 : 51464 : pedwarn_c11 (loc, OPT_Wpedantic,
12729 : : "ISO C does not support the %<_Float%d%s%> type"
12730 : : " before C23",
12731 : 51464 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12732 : 51464 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12733 : : ? "x"
12734 : : : "");
12735 : :
12736 : 10138127 : if (specs->long_p)
12737 : 0 : error_at (loc,
12738 : : "both %qs and %<_Float%d%s%> in declaration "
12739 : : "specifiers", "long",
12740 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12741 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12742 : : ? "x"
12743 : : : "");
12744 : 10138127 : else if (specs->short_p)
12745 : 0 : error_at (loc,
12746 : : "both %qs and %<_Float%d%s%> in declaration "
12747 : : "specifiers", "short",
12748 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12749 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12750 : : ? "x"
12751 : : : "");
12752 : 10138127 : else if (specs->signed_p)
12753 : 0 : error_at (loc,
12754 : : "both %qs and %<_Float%d%s%> in declaration "
12755 : : "specifiers", "signed",
12756 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12757 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12758 : : ? "x"
12759 : : : "");
12760 : 10138127 : else if (specs->unsigned_p)
12761 : 0 : error_at (loc,
12762 : : "both %qs and %<_Float%d%s%> in declaration "
12763 : : "specifiers", "unsigned",
12764 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12765 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12766 : : ? "x"
12767 : : : "");
12768 : 10138127 : else if (specs->saturating_p)
12769 : 0 : error_at (loc,
12770 : : "both %qs and %<_Float%d%s%> in declaration "
12771 : : "specifiers", "_Sat",
12772 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12773 : 0 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12774 : : ? "x"
12775 : : : "");
12776 : 10138127 : else if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
12777 : : {
12778 : 88 : specs->typespec_word = cts_floatn_nx;
12779 : 176 : error_at (loc,
12780 : : "%<_Float%d%s%> is not supported on this target",
12781 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].n,
12782 : 88 : floatn_nx_types[specs->u.floatn_nx_idx].extended
12783 : : ? "x"
12784 : : : "");
12785 : : }
12786 : : else
12787 : : {
12788 : 10138039 : specs->typespec_word = cts_floatn_nx;
12789 : 10138039 : specs->locations[cdw_typespec] = loc;
12790 : : }
12791 : 10138127 : return specs;
12792 : 47633 : case RID_DFLOAT32:
12793 : 47633 : case RID_DFLOAT64:
12794 : 47633 : case RID_DFLOAT128:
12795 : 47633 : case RID_DFLOAT64X:
12796 : 47633 : {
12797 : 47633 : const char *str;
12798 : 47633 : if (i == RID_DFLOAT32)
12799 : : str = "_Decimal32";
12800 : : else if (i == RID_DFLOAT64)
12801 : : str = "_Decimal64";
12802 : : else if (i == RID_DFLOAT128)
12803 : : str = "_Decimal128";
12804 : : else
12805 : 47633 : str = "_Decimal64x";
12806 : 47633 : if (specs->long_long_p)
12807 : 18 : error_at (loc,
12808 : : "both %qs and %qs in declaration specifiers",
12809 : : "long long", str);
12810 : 47633 : if (specs->long_p)
12811 : 33 : error_at (loc,
12812 : : "both %qs and %qs in declaration specifiers",
12813 : : "long", str);
12814 : 47600 : else if (specs->short_p)
12815 : 18 : error_at (loc,
12816 : : "both %qs and %qs in declaration specifiers",
12817 : : "short", str);
12818 : 47582 : else if (specs->signed_p)
12819 : 6 : error_at (loc,
12820 : : "both %qs and %qs in declaration specifiers",
12821 : : "signed", str);
12822 : 47576 : else if (specs->unsigned_p)
12823 : 3 : error_at (loc,
12824 : : "both %qs and %qs in declaration specifiers",
12825 : : "unsigned", str);
12826 : 47573 : else if (specs->complex_p)
12827 : 3 : error_at (loc,
12828 : : "both %qs and %qs in declaration specifiers",
12829 : : "complex", str);
12830 : 47570 : else if (specs->saturating_p)
12831 : 0 : error_at (loc,
12832 : : "both %qs and %qs in declaration specifiers",
12833 : : "_Sat", str);
12834 : 47570 : else if (i == RID_DFLOAT32)
12835 : 15957 : specs->typespec_word = cts_dfloat32;
12836 : 31613 : else if (i == RID_DFLOAT64)
12837 : 15832 : specs->typespec_word = cts_dfloat64;
12838 : 15781 : else if (i == RID_DFLOAT128)
12839 : 15734 : specs->typespec_word = cts_dfloat128;
12840 : : else
12841 : 47 : specs->typespec_word = cts_dfloat64x;
12842 : 47633 : specs->locations[cdw_typespec] = loc;
12843 : : }
12844 : 47633 : if (!targetm.decimal_float_supported_p ())
12845 : 0 : error_at (loc,
12846 : : "decimal floating-point not supported "
12847 : : "for this target");
12848 : 47633 : pedwarn_c11 (loc, OPT_Wpedantic,
12849 : : "ISO C does not support decimal floating-point "
12850 : : "before C23");
12851 : 47633 : return specs;
12852 : 125 : case RID_FRACT:
12853 : 125 : case RID_ACCUM:
12854 : 125 : {
12855 : 125 : const char *str;
12856 : 125 : if (i == RID_FRACT)
12857 : : str = "_Fract";
12858 : : else
12859 : 62 : str = "_Accum";
12860 : 125 : if (specs->complex_p)
12861 : 0 : error_at (loc,
12862 : : "both %qs and %qs in declaration specifiers",
12863 : : "complex", str);
12864 : 125 : else if (i == RID_FRACT)
12865 : 63 : specs->typespec_word = cts_fract;
12866 : : else
12867 : 62 : specs->typespec_word = cts_accum;
12868 : 125 : specs->locations[cdw_typespec] = loc;
12869 : : }
12870 : 125 : if (!targetm.fixed_point_supported_p ())
12871 : 125 : error_at (loc,
12872 : : "fixed-point types not supported for this target");
12873 : 125 : pedwarn (loc, OPT_Wpedantic,
12874 : : "ISO C does not support fixed-point types");
12875 : 125 : return specs;
12876 : 45584 : case RID_BITINT:
12877 : 45584 : if (specs->long_p)
12878 : 2 : error_at (loc,
12879 : : "both %qs and %qs in declaration specifiers",
12880 : : "long", "_BitInt");
12881 : 45582 : else if (specs->short_p)
12882 : 1 : error_at (loc,
12883 : : "both %qs and %qs in declaration specifiers",
12884 : : "short", "_BitInt");
12885 : 45581 : else if (specs->complex_p)
12886 : 1 : error_at (loc,
12887 : : "both %qs and %qs in declaration specifiers",
12888 : : "complex", "_BitInt");
12889 : 45580 : else if (specs->saturating_p)
12890 : 0 : error_at (loc,
12891 : : "both %qs and %qs in declaration specifiers",
12892 : : "_Sat", "_BitInt");
12893 : : else
12894 : : {
12895 : 45580 : specs->typespec_word = cts_bitint;
12896 : 45580 : specs->locations[cdw_typespec] = loc;
12897 : 45580 : specs->u.bitint_prec = -1;
12898 : 45580 : if (error_operand_p (spec.expr))
12899 : 7 : return specs;
12900 : 45580 : if (TREE_CODE (spec.expr) != INTEGER_CST
12901 : 45580 : || !INTEGRAL_TYPE_P (TREE_TYPE (spec.expr)))
12902 : : {
12903 : 1 : error_at (loc, "%<_BitInt%> argument is not an integer "
12904 : : "constant expression");
12905 : 1 : return specs;
12906 : : }
12907 : 45579 : if (tree_int_cst_sgn (spec.expr) <= 0)
12908 : : {
12909 : 4 : error_at (loc, "%<_BitInt%> argument %qE is not a "
12910 : : "positive integer constant expression",
12911 : : spec.expr);
12912 : 4 : return specs;
12913 : : }
12914 : 45575 : if (wi::to_widest (spec.expr) > WIDE_INT_MAX_PRECISION - 1)
12915 : : {
12916 : 2 : error_at (loc, "%<_BitInt%> argument %qE is larger than "
12917 : : "%<BITINT_MAXWIDTH%> %qd",
12918 : : spec.expr, (int) WIDE_INT_MAX_PRECISION - 1);
12919 : 2 : return specs;
12920 : : }
12921 : 45573 : specs->u.bitint_prec = tree_to_uhwi (spec.expr);
12922 : 45573 : struct bitint_info info;
12923 : 45573 : if (!targetm.c.bitint_type_info (specs->u.bitint_prec,
12924 : : &info))
12925 : : {
12926 : 0 : sorry_at (loc, "%<_BitInt(%d)%> is not supported on "
12927 : : "this target", specs->u.bitint_prec);
12928 : 0 : specs->u.bitint_prec = -1;
12929 : 0 : return specs;
12930 : : }
12931 : : }
12932 : 45577 : return specs;
12933 : : default:
12934 : : /* ObjC reserved word "id", handled below. */
12935 : : break;
12936 : : }
12937 : : }
12938 : : }
12939 : :
12940 : : /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12941 : : form of ObjC type, cases such as "int" and "long" being handled
12942 : : above), a TYPE (struct, union, enum and typeof specifiers) or an
12943 : : ERROR_MARK. In none of these cases may there have previously
12944 : : been any type specifiers. */
12945 : 242761780 : if (specs->type || specs->typespec_word != cts_none
12946 : 242761768 : || specs->long_p || specs->short_p || specs->signed_p
12947 : 242761765 : || specs->unsigned_p || specs->complex_p)
12948 : 21 : error_at (loc, "two or more data types in declaration specifiers");
12949 : 242761759 : else if (TREE_CODE (type) == TYPE_DECL)
12950 : : {
12951 : 239538857 : mark_decl_used (type, false);
12952 : 239538857 : specs->type = TREE_TYPE (type);
12953 : 239538857 : if (TREE_TYPE (type) != error_mark_node)
12954 : : {
12955 : 239538844 : specs->decl_attr = DECL_ATTRIBUTES (type);
12956 : 239538844 : specs->typedef_p = true;
12957 : 239538844 : specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12958 : 239538844 : specs->locations[cdw_typedef] = loc;
12959 : :
12960 : : /* If this typedef name is defined in a struct, then a C++
12961 : : lookup would return a different value. */
12962 : 239538844 : if (warn_cxx_compat
12963 : 239538844 : && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12964 : 2 : warning_at (loc, OPT_Wc___compat,
12965 : : "C++ lookup of %qD would return a field, not a type",
12966 : : type);
12967 : :
12968 : : /* If we are parsing a struct, record that a struct field
12969 : : used a typedef. */
12970 : 239538844 : if (warn_cxx_compat && struct_parse_info != NULL)
12971 : 1751 : struct_parse_info->typedefs_seen.safe_push (type);
12972 : : }
12973 : : }
12974 : 3222902 : else if (TREE_CODE (type) == IDENTIFIER_NODE)
12975 : : {
12976 : 0 : tree t = lookup_name (type);
12977 : 0 : if (!t || TREE_CODE (t) != TYPE_DECL)
12978 : 0 : error_at (loc, "%qE fails to be a typedef or built in type", type);
12979 : 0 : else if (TREE_TYPE (t) == error_mark_node)
12980 : : ;
12981 : : else
12982 : : {
12983 : 0 : specs->type = TREE_TYPE (t);
12984 : 0 : specs->locations[cdw_typespec] = loc;
12985 : : }
12986 : : }
12987 : : else
12988 : : {
12989 : 3222902 : if (TREE_CODE (type) != ERROR_MARK)
12990 : : {
12991 : 3222697 : if (spec.kind == ctsk_typeof)
12992 : : {
12993 : 819750 : specs->typedef_p = true;
12994 : 819750 : specs->locations[cdw_typedef] = loc;
12995 : : }
12996 : 3222697 : if (spec.expr)
12997 : : {
12998 : 909 : if (specs->expr)
12999 : 0 : specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
13000 : : specs->expr, spec.expr);
13001 : : else
13002 : 909 : specs->expr = spec.expr;
13003 : 909 : specs->expr_const_operands &= spec.expr_const_operands;
13004 : : }
13005 : : }
13006 : 3222902 : specs->type = type;
13007 : 3222902 : if (spec.has_enum_type_specifier
13008 : 168 : && spec.kind != ctsk_tagdef)
13009 : 49 : specs->enum_type_specifier_ref_p = true;
13010 : : }
13011 : :
13012 : : return specs;
13013 : : }
13014 : :
13015 : : /* Add the storage class specifier or function specifier SCSPEC to the
13016 : : declaration specifiers SPECS, returning SPECS. */
13017 : :
13018 : : struct c_declspecs *
13019 : 88231896 : declspecs_add_scspec (location_t loc,
13020 : : struct c_declspecs *specs,
13021 : : tree scspec)
13022 : : {
13023 : 88231896 : enum rid i;
13024 : 88231896 : enum c_storage_class n = csc_none;
13025 : 88231896 : bool dupe = false;
13026 : 88231896 : specs->declspecs_seen_p = true;
13027 : 88231896 : specs->non_std_attrs_seen_p = true;
13028 : 88231896 : gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
13029 : : && C_IS_RESERVED_WORD (scspec));
13030 : 88231896 : i = C_RID_CODE (scspec);
13031 : 88231896 : if (specs->non_sc_seen_p)
13032 : 2074 : warning (OPT_Wold_style_declaration,
13033 : : "%qE is not at beginning of declaration", scspec);
13034 : 88231896 : switch (i)
13035 : : {
13036 : 34699230 : case RID_INLINE:
13037 : : /* C99 permits duplicate inline. Although of doubtful utility,
13038 : : it seems simplest to permit it in gnu89 mode as well, as
13039 : : there is also little utility in maintaining this as a
13040 : : difference between gnu89 and C99 inline. */
13041 : 34699230 : dupe = false;
13042 : 34699230 : specs->inline_p = true;
13043 : 34699230 : specs->locations[cdw_inline] = loc;
13044 : 34699230 : break;
13045 : 22965 : case RID_NORETURN:
13046 : : /* Duplicate _Noreturn is permitted. */
13047 : 22965 : dupe = false;
13048 : 22965 : specs->noreturn_p = true;
13049 : 22965 : specs->locations[cdw_noreturn] = loc;
13050 : 22965 : break;
13051 : 2876 : case RID_THREAD:
13052 : 2876 : dupe = specs->thread_p;
13053 : 2876 : if (specs->storage_class == csc_auto)
13054 : 2 : error ("%qE used with %<auto%>", scspec);
13055 : 2874 : else if (specs->storage_class == csc_register)
13056 : 3 : error ("%qE used with %<register%>", scspec);
13057 : 2871 : else if (specs->storage_class == csc_typedef)
13058 : 2 : error ("%qE used with %<typedef%>", scspec);
13059 : 2869 : else if (specs->constexpr_p)
13060 : 2 : error ("%qE used with %<constexpr%>", scspec);
13061 : : else
13062 : : {
13063 : 2867 : specs->thread_p = true;
13064 : 2867 : specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
13065 : 2867 : "__thread") == 0);
13066 : : /* A diagnostic is not required for the use of this
13067 : : identifier in the implementation namespace; only diagnose
13068 : : it for the C11 spelling because of existing code using
13069 : : the other spelling. */
13070 : 2867 : if (!specs->thread_gnu_p)
13071 : : {
13072 : 114 : if (flag_isoc99)
13073 : 111 : pedwarn_c99 (loc, OPT_Wpedantic,
13074 : : "ISO C99 does not support %qE", scspec);
13075 : : else
13076 : 3 : pedwarn_c99 (loc, OPT_Wpedantic,
13077 : : "ISO C90 does not support %qE", scspec);
13078 : : }
13079 : 2867 : specs->locations[cdw_thread] = loc;
13080 : : }
13081 : : break;
13082 : 262 : case RID_AUTO:
13083 : 262 : if (flag_isoc23
13084 : 223 : && specs->typespec_kind == ctsk_none
13085 : 212 : && specs->storage_class != csc_typedef)
13086 : : {
13087 : : /* "auto" potentially used for type deduction. */
13088 : 211 : if (specs->c23_auto_p)
13089 : 2 : error ("duplicate %qE", scspec);
13090 : 211 : specs->c23_auto_p = true;
13091 : 211 : return specs;
13092 : : }
13093 : 51 : n = csc_auto;
13094 : : /* auto may only be used with another storage class specifier,
13095 : : such as constexpr, if the type is inferred. */
13096 : 51 : if (specs->constexpr_p)
13097 : 2 : error ("%qE used with %<constexpr%>", scspec);
13098 : : break;
13099 : 48856471 : case RID_EXTERN:
13100 : 48856471 : n = csc_extern;
13101 : : /* Diagnose "__thread extern". */
13102 : 48856471 : if (specs->thread_p && specs->thread_gnu_p)
13103 : 2 : error ("%<__thread%> before %<extern%>");
13104 : : break;
13105 : : case RID_REGISTER:
13106 : : n = csc_register;
13107 : : break;
13108 : 413489 : case RID_STATIC:
13109 : 413489 : n = csc_static;
13110 : : /* Diagnose "__thread static". */
13111 : 413489 : if (specs->thread_p && specs->thread_gnu_p)
13112 : 1 : error ("%<__thread%> before %<static%>");
13113 : : break;
13114 : 4232867 : case RID_TYPEDEF:
13115 : 4232867 : n = csc_typedef;
13116 : 4232867 : if (specs->c23_auto_p)
13117 : : {
13118 : 1 : error ("%<typedef%> used with %<auto%>");
13119 : 1 : specs->c23_auto_p = false;
13120 : : }
13121 : : break;
13122 : 610 : case RID_CONSTEXPR:
13123 : 610 : dupe = specs->constexpr_p;
13124 : 610 : if (specs->storage_class == csc_extern)
13125 : 1 : error ("%qE used with %<extern%>", scspec);
13126 : 609 : else if (specs->storage_class == csc_typedef)
13127 : 1 : error ("%qE used with %<typedef%>", scspec);
13128 : 608 : else if (specs->storage_class == csc_auto)
13129 : : /* auto may only be used with another storage class specifier,
13130 : : such as constexpr, if the type is inferred. */
13131 : 2 : error ("%qE used with %<auto%>", scspec);
13132 : 606 : else if (specs->thread_p)
13133 : 4 : error ("%qE used with %qs", scspec,
13134 : 2 : specs->thread_gnu_p ? "__thread" : "_Thread_local");
13135 : : else
13136 : 604 : specs->constexpr_p = true;
13137 : : break;
13138 : 0 : default:
13139 : 0 : gcc_unreachable ();
13140 : : }
13141 : 88231691 : if (n != csc_none && n == specs->storage_class)
13142 : : dupe = true;
13143 : 88231677 : if (dupe)
13144 : : {
13145 : 12 : if (i == RID_THREAD)
13146 : 2 : error ("duplicate %<_Thread_local%> or %<__thread%>");
13147 : : else
13148 : 10 : error ("duplicate %qE", scspec);
13149 : : }
13150 : 88231685 : if (n != csc_none)
13151 : : {
13152 : 53506004 : if (specs->storage_class != csc_none && n != specs->storage_class)
13153 : : {
13154 : 7 : error ("multiple storage classes in declaration specifiers");
13155 : : }
13156 : : else
13157 : : {
13158 : 53505997 : specs->storage_class = n;
13159 : 53505997 : specs->locations[cdw_storage_class] = loc;
13160 : 53505997 : if (n != csc_extern && n != csc_static && specs->thread_p)
13161 : : {
13162 : 8 : error ("%qs used with %qE",
13163 : 8 : specs->thread_gnu_p ? "__thread" : "_Thread_local",
13164 : : scspec);
13165 : 8 : specs->thread_p = false;
13166 : : }
13167 : 53505997 : if (n != csc_auto && n != csc_register && n != csc_static
13168 : 53089335 : && specs->constexpr_p)
13169 : : {
13170 : 2 : error ("%<constexpr%> used with %qE", scspec);
13171 : 2 : specs->constexpr_p = false;
13172 : : }
13173 : : }
13174 : : }
13175 : : return specs;
13176 : : }
13177 : :
13178 : : /* Add the attributes ATTRS to the declaration specifiers SPECS,
13179 : : returning SPECS. */
13180 : :
13181 : : struct c_declspecs *
13182 : 35002079 : declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
13183 : : {
13184 : 35002079 : specs->attrs = chainon (attrs, specs->attrs);
13185 : 35002079 : specs->locations[cdw_attributes] = loc;
13186 : 35002079 : specs->declspecs_seen_p = true;
13187 : : /* In the case of standard attributes at the start of the
13188 : : declaration, the caller will reset this. */
13189 : 35002079 : specs->non_std_attrs_seen_p = true;
13190 : 35002079 : return specs;
13191 : : }
13192 : :
13193 : : /* Add an _Alignas specifier (expression ALIGN, or type whose
13194 : : alignment is ALIGN) to the declaration specifiers SPECS, returning
13195 : : SPECS. */
13196 : : struct c_declspecs *
13197 : 200 : declspecs_add_alignas (location_t loc,
13198 : : struct c_declspecs *specs, tree align)
13199 : : {
13200 : 200 : specs->alignas_p = true;
13201 : 200 : specs->locations[cdw_alignas] = loc;
13202 : 200 : if (align == error_mark_node)
13203 : : return specs;
13204 : :
13205 : : /* Only accept the alignment if it's valid and greater than
13206 : : the current one. Zero is invalid but by C11 required to
13207 : : be silently ignored. */
13208 : 198 : int align_log = check_user_alignment (align, false, /* warn_zero = */false);
13209 : 198 : if (align_log > specs->align_log)
13210 : 165 : specs->align_log = align_log;
13211 : : return specs;
13212 : : }
13213 : :
13214 : : /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
13215 : : specifiers with any other type specifier to determine the resulting
13216 : : type. This is where ISO C checks on complex types are made, since
13217 : : "_Complex long" is a prefix of the valid ISO C type "_Complex long
13218 : : double". Also apply postfix standard attributes to modify the type. */
13219 : :
13220 : : struct c_declspecs *
13221 : 306599353 : finish_declspecs (struct c_declspecs *specs)
13222 : : {
13223 : : /* If a type was specified as a whole, we have no modifiers and are
13224 : : done. */
13225 : 306599353 : if (specs->type != NULL_TREE)
13226 : : {
13227 : 242761713 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13228 : : && !specs->signed_p && !specs->unsigned_p
13229 : : && !specs->complex_p && !specs->c23_auto_p);
13230 : :
13231 : : /* Set a dummy type. */
13232 : 242761713 : if (TREE_CODE (specs->type) == ERROR_MARK)
13233 : 181 : specs->type = integer_type_node;
13234 : 242761713 : goto handle_postfix_attrs;
13235 : : }
13236 : :
13237 : : /* If none of "void", "_Bool", "char", "int", "float" or "double"
13238 : : has been specified, treat it as "int" unless "_Complex" is
13239 : : present and there are no other specifiers. If we just have
13240 : : "_Complex", it is equivalent to "_Complex double", but e.g.
13241 : : "_Complex short" is equivalent to "_Complex short int". */
13242 : 63837640 : if (specs->typespec_word == cts_none)
13243 : : {
13244 : 4099738 : if (specs->saturating_p)
13245 : : {
13246 : 1 : error_at (specs->locations[cdw_saturating],
13247 : : "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
13248 : 1 : if (!targetm.fixed_point_supported_p ())
13249 : 1 : error_at (specs->locations[cdw_saturating],
13250 : : "fixed-point types not supported for this target");
13251 : 1 : specs->typespec_word = cts_fract;
13252 : : }
13253 : 4099737 : else if (specs->long_p || specs->short_p
13254 : 189856 : || specs->signed_p || specs->unsigned_p)
13255 : : {
13256 : 4089303 : specs->typespec_word = cts_int;
13257 : : }
13258 : 10434 : else if (specs->complex_p)
13259 : : {
13260 : 130 : specs->typespec_word = cts_double;
13261 : 130 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13262 : : "ISO C does not support plain %<complex%> meaning "
13263 : : "%<double complex%>");
13264 : : }
13265 : 10304 : else if (specs->c23_auto_p)
13266 : : {
13267 : : /* Type to be filled in later, including applying postfix
13268 : : attributes. This warning only actually appears for
13269 : : -Wc11-c23-compat in C23 mode; in older modes, there may
13270 : : be a warning or pedwarn for implicit "int" instead, or
13271 : : other errors for use of auto at file scope. */
13272 : 93 : pedwarn_c11 (input_location, OPT_Wpedantic,
13273 : : "ISO C does not support %<auto%> type deduction "
13274 : : "before C23");
13275 : 93 : return specs;
13276 : : }
13277 : : else
13278 : : {
13279 : 10211 : specs->typespec_word = cts_int;
13280 : 10211 : specs->default_int_p = true;
13281 : : /* We don't diagnose this here because grokdeclarator will
13282 : : give more specific diagnostics according to whether it is
13283 : : a function definition. */
13284 : : }
13285 : : }
13286 : :
13287 : : /* If "signed" was specified, record this to distinguish "int" and
13288 : : "signed int" in the case of a bit-field with
13289 : : -funsigned-bitfields. */
13290 : 63837547 : specs->explicit_signed_p = specs->signed_p;
13291 : :
13292 : : /* Now compute the actual type. */
13293 : 63837547 : gcc_assert (!specs->c23_auto_p);
13294 : 63837547 : switch (specs->typespec_word)
13295 : : {
13296 : 1833 : case cts_auto_type:
13297 : 1833 : gcc_assert (!specs->long_p && !specs->short_p
13298 : : && !specs->signed_p && !specs->unsigned_p
13299 : : && !specs->complex_p);
13300 : : /* Type to be filled in later. */
13301 : 1833 : if (specs->postfix_attrs)
13302 : 2 : error ("%<__auto_type%> followed by %<[[]]%> attributes");
13303 : : break;
13304 : 7680870 : case cts_void:
13305 : 7680870 : gcc_assert (!specs->long_p && !specs->short_p
13306 : : && !specs->signed_p && !specs->unsigned_p
13307 : : && !specs->complex_p);
13308 : 7680870 : specs->type = void_type_node;
13309 : 7680870 : break;
13310 : 82517 : case cts_bool:
13311 : 82517 : gcc_assert (!specs->long_p && !specs->short_p
13312 : : && !specs->signed_p && !specs->unsigned_p
13313 : : && !specs->complex_p);
13314 : 82517 : specs->type = boolean_type_node;
13315 : 82517 : break;
13316 : 8943527 : case cts_char:
13317 : 8943527 : gcc_assert (!specs->long_p && !specs->short_p);
13318 : 8943527 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13319 : 8943527 : if (specs->signed_p)
13320 : 189231 : specs->type = signed_char_type_node;
13321 : 8754296 : else if (specs->unsigned_p)
13322 : 877332 : specs->type = unsigned_char_type_node;
13323 : : else
13324 : 7876964 : specs->type = char_type_node;
13325 : 8943527 : if (specs->complex_p)
13326 : : {
13327 : 3853 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13328 : : "ISO C does not support complex integer types");
13329 : 3853 : specs->type = build_complex_type (specs->type);
13330 : : }
13331 : : break;
13332 : 51285 : case cts_int_n:
13333 : 51285 : gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
13334 : 51285 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13335 : 51285 : if (! int_n_enabled_p[specs->u.int_n_idx])
13336 : 0 : specs->type = integer_type_node;
13337 : : else
13338 : 51285 : specs->type = (specs->unsigned_p
13339 : 51285 : ? int_n_trees[specs->u.int_n_idx].unsigned_type
13340 : : : int_n_trees[specs->u.int_n_idx].signed_type);
13341 : 51285 : if (specs->complex_p)
13342 : : {
13343 : 199 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13344 : : "ISO C does not support complex integer types");
13345 : 199 : specs->type = build_complex_type (specs->type);
13346 : : }
13347 : : break;
13348 : 27492554 : case cts_int:
13349 : 27492554 : gcc_assert (!(specs->long_p && specs->short_p));
13350 : 27492554 : gcc_assert (!(specs->signed_p && specs->unsigned_p));
13351 : 27492554 : if (specs->long_long_p)
13352 : 2890060 : specs->type = (specs->unsigned_p
13353 : 2890060 : ? long_long_unsigned_type_node
13354 : : : long_long_integer_type_node);
13355 : 24602494 : else if (specs->long_p)
13356 : 2167674 : specs->type = (specs->unsigned_p
13357 : 2167674 : ? long_unsigned_type_node
13358 : : : long_integer_type_node);
13359 : 22434820 : else if (specs->short_p)
13360 : 1666628 : specs->type = (specs->unsigned_p
13361 : 1666628 : ? short_unsigned_type_node
13362 : : : short_integer_type_node);
13363 : : else
13364 : 20768192 : specs->type = (specs->unsigned_p
13365 : 20768192 : ? unsigned_type_node
13366 : : : integer_type_node);
13367 : 27492554 : if (specs->complex_p)
13368 : : {
13369 : 14910 : pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
13370 : : "ISO C does not support complex integer types");
13371 : 14910 : specs->type = build_complex_type (specs->type);
13372 : : }
13373 : : break;
13374 : 3336958 : case cts_float:
13375 : 3336958 : gcc_assert (!specs->long_p && !specs->short_p
13376 : : && !specs->signed_p && !specs->unsigned_p);
13377 : 6673916 : specs->type = (specs->complex_p
13378 : 3336958 : ? complex_float_type_node
13379 : : : float_type_node);
13380 : 3336958 : break;
13381 : 6016600 : case cts_double:
13382 : 6016600 : gcc_assert (!specs->long_long_p && !specs->short_p
13383 : : && !specs->signed_p && !specs->unsigned_p);
13384 : 6016600 : if (specs->long_p)
13385 : : {
13386 : 2589062 : specs->type = (specs->complex_p
13387 : 2589062 : ? complex_long_double_type_node
13388 : : : long_double_type_node);
13389 : : }
13390 : : else
13391 : : {
13392 : 3427538 : specs->type = (specs->complex_p
13393 : 3427538 : ? complex_double_type_node
13394 : : : double_type_node);
13395 : : }
13396 : : break;
13397 : 10138127 : case cts_floatn_nx:
13398 : 10138127 : gcc_assert (!specs->long_p && !specs->short_p
13399 : : && !specs->signed_p && !specs->unsigned_p);
13400 : 10138127 : if (FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx) == NULL_TREE)
13401 : 88 : specs->type = integer_type_node;
13402 : 10138039 : else if (specs->complex_p)
13403 : 1535823 : specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13404 : : else
13405 : 8602216 : specs->type = FLOATN_NX_TYPE_NODE (specs->u.floatn_nx_idx);
13406 : : break;
13407 : 47570 : case cts_dfloat32:
13408 : 47570 : case cts_dfloat64:
13409 : 47570 : case cts_dfloat128:
13410 : 47570 : case cts_dfloat64x:
13411 : 47570 : gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
13412 : : && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
13413 : 47570 : if (!targetm.decimal_float_supported_p ())
13414 : 0 : specs->type = integer_type_node;
13415 : 47570 : else if (specs->typespec_word == cts_dfloat32)
13416 : 15957 : specs->type = dfloat32_type_node;
13417 : 31613 : else if (specs->typespec_word == cts_dfloat64)
13418 : 15832 : specs->type = dfloat64_type_node;
13419 : 15781 : else if (specs->typespec_word == cts_dfloat128)
13420 : 15734 : specs->type = dfloat128_type_node;
13421 : : else
13422 : 47 : specs->type = dfloat64x_type_node;
13423 : : break;
13424 : 64 : case cts_fract:
13425 : 64 : gcc_assert (!specs->complex_p);
13426 : 64 : if (!targetm.fixed_point_supported_p ())
13427 : 64 : specs->type = integer_type_node;
13428 : 0 : else if (specs->saturating_p)
13429 : : {
13430 : 0 : if (specs->long_long_p)
13431 : 0 : specs->type = specs->unsigned_p
13432 : 0 : ? sat_unsigned_long_long_fract_type_node
13433 : : : sat_long_long_fract_type_node;
13434 : 0 : else if (specs->long_p)
13435 : 0 : specs->type = specs->unsigned_p
13436 : 0 : ? sat_unsigned_long_fract_type_node
13437 : : : sat_long_fract_type_node;
13438 : 0 : else if (specs->short_p)
13439 : 0 : specs->type = specs->unsigned_p
13440 : 0 : ? sat_unsigned_short_fract_type_node
13441 : : : sat_short_fract_type_node;
13442 : : else
13443 : 0 : specs->type = specs->unsigned_p
13444 : 0 : ? sat_unsigned_fract_type_node
13445 : : : sat_fract_type_node;
13446 : : }
13447 : : else
13448 : : {
13449 : 0 : if (specs->long_long_p)
13450 : 0 : specs->type = specs->unsigned_p
13451 : 0 : ? unsigned_long_long_fract_type_node
13452 : : : long_long_fract_type_node;
13453 : 0 : else if (specs->long_p)
13454 : 0 : specs->type = specs->unsigned_p
13455 : 0 : ? unsigned_long_fract_type_node
13456 : : : long_fract_type_node;
13457 : 0 : else if (specs->short_p)
13458 : 0 : specs->type = specs->unsigned_p
13459 : 0 : ? unsigned_short_fract_type_node
13460 : : : short_fract_type_node;
13461 : : else
13462 : 0 : specs->type = specs->unsigned_p
13463 : 0 : ? unsigned_fract_type_node
13464 : : : fract_type_node;
13465 : : }
13466 : : break;
13467 : 62 : case cts_accum:
13468 : 62 : gcc_assert (!specs->complex_p);
13469 : 62 : if (!targetm.fixed_point_supported_p ())
13470 : 62 : specs->type = integer_type_node;
13471 : 0 : else if (specs->saturating_p)
13472 : : {
13473 : 0 : if (specs->long_long_p)
13474 : 0 : specs->type = specs->unsigned_p
13475 : 0 : ? sat_unsigned_long_long_accum_type_node
13476 : : : sat_long_long_accum_type_node;
13477 : 0 : else if (specs->long_p)
13478 : 0 : specs->type = specs->unsigned_p
13479 : 0 : ? sat_unsigned_long_accum_type_node
13480 : : : sat_long_accum_type_node;
13481 : 0 : else if (specs->short_p)
13482 : 0 : specs->type = specs->unsigned_p
13483 : 0 : ? sat_unsigned_short_accum_type_node
13484 : : : sat_short_accum_type_node;
13485 : : else
13486 : 0 : specs->type = specs->unsigned_p
13487 : 0 : ? sat_unsigned_accum_type_node
13488 : : : sat_accum_type_node;
13489 : : }
13490 : : else
13491 : : {
13492 : 0 : if (specs->long_long_p)
13493 : 0 : specs->type = specs->unsigned_p
13494 : 0 : ? unsigned_long_long_accum_type_node
13495 : : : long_long_accum_type_node;
13496 : 0 : else if (specs->long_p)
13497 : 0 : specs->type = specs->unsigned_p
13498 : 0 : ? unsigned_long_accum_type_node
13499 : : : long_accum_type_node;
13500 : 0 : else if (specs->short_p)
13501 : 0 : specs->type = specs->unsigned_p
13502 : 0 : ? unsigned_short_accum_type_node
13503 : : : short_accum_type_node;
13504 : : else
13505 : 0 : specs->type = specs->unsigned_p
13506 : 0 : ? unsigned_accum_type_node
13507 : : : accum_type_node;
13508 : : }
13509 : : break;
13510 : 45580 : case cts_bitint:
13511 : 45580 : gcc_assert (!specs->long_p && !specs->short_p
13512 : : && !specs->complex_p);
13513 : 45580 : if (!specs->unsigned_p && specs->u.bitint_prec == 1)
13514 : : {
13515 : 2 : error_at (specs->locations[cdw_typespec],
13516 : : "%<signed _BitInt%> argument must be at least 2");
13517 : 2 : specs->type = integer_type_node;
13518 : 2 : break;
13519 : : }
13520 : 45578 : if (specs->u.bitint_prec == -1)
13521 : 7 : specs->type = integer_type_node;
13522 : : else
13523 : : {
13524 : 67185 : pedwarn_c11 (specs->locations[cdw_typespec], OPT_Wpedantic,
13525 : : "ISO C does not support %<%s_BitInt(%d)%> before C23",
13526 : : specs->unsigned_p ? "unsigned "
13527 : 21614 : : specs->signed_p ? "signed " : "",
13528 : : specs->u.bitint_prec);
13529 : 45571 : specs->type = build_bitint_type (specs->u.bitint_prec,
13530 : 45571 : specs->unsigned_p);
13531 : : }
13532 : : break;
13533 : 0 : default:
13534 : 0 : gcc_unreachable ();
13535 : : }
13536 : 306599260 : handle_postfix_attrs:
13537 : 306599260 : if (specs->type != NULL)
13538 : : {
13539 : 306597427 : specs->postfix_attrs
13540 : 306597427 : = c_warn_type_attributes (specs->type, specs->postfix_attrs);
13541 : 306597427 : decl_attributes (&specs->type, specs->postfix_attrs, 0);
13542 : 306597427 : specs->postfix_attrs = NULL_TREE;
13543 : : }
13544 : :
13545 : : return specs;
13546 : : }
13547 : :
13548 : : /* Perform final processing on one file scope's declarations (or the
13549 : : external scope's declarations), GLOBALS. */
13550 : :
13551 : : static void
13552 : 214300 : c_write_global_declarations_1 (tree globals)
13553 : : {
13554 : 214300 : tree decl;
13555 : 214300 : bool reconsider;
13556 : :
13557 : : /* Process the decls in the order they were written. */
13558 : 388725835 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13559 : : {
13560 : : /* Check for used but undefined static functions using the C
13561 : : standard's definition of "used", and set TREE_NO_WARNING so
13562 : : that check_global_declaration doesn't repeat the check. */
13563 : 388511535 : if (TREE_CODE (decl) == FUNCTION_DECL
13564 : 370543162 : && DECL_INITIAL (decl) == NULL_TREE
13565 : 335167872 : && DECL_EXTERNAL (decl)
13566 : 723679402 : && !TREE_PUBLIC (decl))
13567 : : {
13568 : 121 : if (C_DECL_USED (decl))
13569 : : {
13570 : : /* TODO: Add OPT_Wundefined-inline. */
13571 : 30 : if (pedwarn (input_location, 0, "%q+F used but never defined",
13572 : : decl))
13573 : 29 : suppress_warning (decl /* OPT_Wundefined-inline. */);
13574 : : }
13575 : : /* For -Wunused-function warn about unused static prototypes. */
13576 : 91 : else if (warn_unused_function
13577 : 2 : && ! DECL_ARTIFICIAL (decl)
13578 : 93 : && ! warning_suppressed_p (decl, OPT_Wunused_function))
13579 : : {
13580 : 2 : if (warning (OPT_Wunused_function,
13581 : : "%q+F declared %<static%> but never defined",
13582 : : decl))
13583 : 2 : suppress_warning (decl, OPT_Wunused_function);
13584 : : }
13585 : : }
13586 : :
13587 : 388511535 : wrapup_global_declaration_1 (decl);
13588 : : }
13589 : :
13590 : 248041 : do
13591 : : {
13592 : 248041 : reconsider = false;
13593 : 501224292 : for (decl = globals; decl; decl = DECL_CHAIN (decl))
13594 : 500976251 : reconsider |= wrapup_global_declaration_2 (decl);
13595 : : }
13596 : : while (reconsider);
13597 : 214300 : }
13598 : :
13599 : : /* Preserve the external declarations scope across a garbage collect. */
13600 : : static GTY(()) tree ext_block;
13601 : :
13602 : : /* Collect all references relevant to SOURCE_FILE. */
13603 : :
13604 : : static void
13605 : 15 : collect_all_refs (const char *source_file)
13606 : : {
13607 : 15 : tree t;
13608 : 15 : unsigned i;
13609 : :
13610 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13611 : 15 : collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
13612 : :
13613 : 15 : collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
13614 : 15 : }
13615 : :
13616 : : /* Collect source file references at global level. */
13617 : :
13618 : : static void
13619 : 15 : collect_source_refs (void)
13620 : : {
13621 : 15 : tree t;
13622 : 15 : tree decls;
13623 : 15 : tree decl;
13624 : 15 : unsigned i;
13625 : :
13626 : 30 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13627 : : {
13628 : 15 : decls = DECL_INITIAL (t);
13629 : 82 : for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
13630 : 67 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13631 : 67 : collect_source_ref (DECL_SOURCE_FILE (decl));
13632 : : }
13633 : :
13634 : 44171 : for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
13635 : 44156 : if (!DECL_IS_UNDECLARED_BUILTIN (decl))
13636 : 11 : collect_source_ref (DECL_SOURCE_FILE (decl));
13637 : 15 : }
13638 : :
13639 : : /* Free attribute access data that are not needed by the middle end. */
13640 : :
13641 : : static void
13642 : 107150 : free_attr_access_data ()
13643 : : {
13644 : 107150 : struct cgraph_node *n;
13645 : :
13646 : : /* Iterate over all functions declared in the translation unit. */
13647 : 70968122 : FOR_EACH_FUNCTION (n)
13648 : : {
13649 : 132870767 : for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
13650 : 97493856 : if (tree attrs = DECL_ATTRIBUTES (parm))
13651 : 95670 : attr_access::free_lang_data (attrs);
13652 : :
13653 : 35376911 : tree fntype = TREE_TYPE (n->decl);
13654 : 35376911 : if (!fntype || fntype == error_mark_node)
13655 : 0 : continue;
13656 : 35376911 : tree attrs = TYPE_ATTRIBUTES (fntype);
13657 : 35376911 : if (!attrs)
13658 : 35145101 : continue;
13659 : :
13660 : 231810 : attr_access::free_lang_data (attrs);
13661 : : }
13662 : 107150 : }
13663 : :
13664 : : /* Perform any final parser cleanups and generate initial debugging
13665 : : information. */
13666 : :
13667 : : void
13668 : 107523 : c_parse_final_cleanups (void)
13669 : : {
13670 : 107523 : tree t;
13671 : 107523 : unsigned i;
13672 : :
13673 : : /* We don't want to do this if generating a PCH. */
13674 : 107523 : if (pch_file)
13675 : 107523 : return;
13676 : :
13677 : 107150 : timevar_stop (TV_PHASE_PARSING);
13678 : 107150 : timevar_start (TV_PHASE_DEFERRED);
13679 : :
13680 : : /* Do the Objective-C stuff. This is where all the Objective-C
13681 : : module stuff gets generated (symtab, class/protocol/selector
13682 : : lists etc). */
13683 : 107150 : if (c_dialect_objc ())
13684 : 0 : objc_write_global_declarations ();
13685 : :
13686 : : /* Close the external scope. */
13687 : 107150 : ext_block = pop_scope ();
13688 : 107150 : external_scope = 0;
13689 : 107150 : gcc_assert (!current_scope);
13690 : :
13691 : : /* Handle -fdump-ada-spec[-slim]. */
13692 : 107150 : if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
13693 : : {
13694 : : /* Build a table of files to generate specs for */
13695 : 15 : collect_source_ref (main_input_filename);
13696 : 15 : if (!flag_dump_ada_spec_slim)
13697 : 15 : collect_source_refs ();
13698 : :
13699 : 15 : dump_ada_specs (collect_all_refs, NULL);
13700 : : }
13701 : :
13702 : : /* Process all file scopes in this compilation, and the external_scope,
13703 : : through wrapup_global_declarations. */
13704 : 214300 : FOR_EACH_VEC_ELT (*all_translation_units, i, t)
13705 : 107150 : c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
13706 : 107150 : c_write_global_declarations_1 (BLOCK_VARS (ext_block));
13707 : :
13708 : : /* Call this to set cpp_implicit_aliases_done on all nodes. This is
13709 : : important for function multiversioning aliases to get resolved. */
13710 : 107150 : symtab->process_same_body_aliases ();
13711 : :
13712 : 107150 : if (!in_lto_p)
13713 : 107150 : free_attr_access_data ();
13714 : :
13715 : 107150 : timevar_stop (TV_PHASE_DEFERRED);
13716 : 107150 : timevar_start (TV_PHASE_PARSING);
13717 : :
13718 : 107150 : ext_block = NULL;
13719 : : }
13720 : :
13721 : : /* Register reserved keyword WORD as qualifier for address space AS. */
13722 : :
13723 : : void
13724 : 226134 : c_register_addr_space (const char *word, addr_space_t as)
13725 : : {
13726 : 226134 : int rid = RID_FIRST_ADDR_SPACE + as;
13727 : 226134 : tree id;
13728 : :
13729 : : /* Address space qualifiers are only supported
13730 : : in C with GNU extensions enabled. */
13731 : 226134 : if (c_dialect_objc () || flag_no_asm)
13732 : : return;
13733 : :
13734 : 213870 : id = get_identifier (word);
13735 : 213870 : C_SET_RID_CODE (id, rid);
13736 : 213870 : C_IS_RESERVED_WORD (id) = 1;
13737 : 213870 : ridpointers [rid] = id;
13738 : : }
13739 : :
13740 : : /* Return identifier to look up for omp declare reduction. */
13741 : :
13742 : : tree
13743 : 3250 : c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13744 : : {
13745 : 3250 : const char *p = NULL;
13746 : 3250 : switch (reduction_code)
13747 : : {
13748 : : case PLUS_EXPR: p = "+"; break;
13749 : 267 : case MULT_EXPR: p = "*"; break;
13750 : 155 : case MINUS_EXPR: p = "-"; break;
13751 : 31 : case BIT_AND_EXPR: p = "&"; break;
13752 : 14 : case BIT_XOR_EXPR: p = "^"; break;
13753 : 92 : case BIT_IOR_EXPR: p = "|"; break;
13754 : 67 : case TRUTH_ANDIF_EXPR: p = "&&"; break;
13755 : 78 : case TRUTH_ORIF_EXPR: p = "||"; break;
13756 : 46 : case MIN_EXPR: p = "min"; break;
13757 : 83 : case MAX_EXPR: p = "max"; break;
13758 : : default:
13759 : : break;
13760 : : }
13761 : :
13762 : 833 : if (p == NULL)
13763 : : {
13764 : 213 : if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13765 : 0 : return error_mark_node;
13766 : 213 : p = IDENTIFIER_POINTER (reduction_id);
13767 : : }
13768 : :
13769 : 3250 : const char prefix[] = "omp declare reduction ";
13770 : 3250 : size_t lenp = sizeof (prefix);
13771 : 3250 : size_t len = strlen (p);
13772 : 3250 : char *name = XALLOCAVEC (char, lenp + len);
13773 : 3250 : memcpy (name, prefix, lenp - 1);
13774 : 3250 : memcpy (name + lenp - 1, p, len + 1);
13775 : 3250 : return get_identifier (name);
13776 : : }
13777 : :
13778 : : /* Lookup REDUCTION_ID in the current scope, or create an artificial
13779 : : VAR_DECL, bind it into the current scope and return it. */
13780 : :
13781 : : tree
13782 : 161 : c_omp_reduction_decl (tree reduction_id)
13783 : : {
13784 : 161 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13785 : 161 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13786 : 47 : return b->decl;
13787 : :
13788 : 114 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13789 : : reduction_id, integer_type_node);
13790 : 114 : DECL_ARTIFICIAL (decl) = 1;
13791 : 114 : DECL_EXTERNAL (decl) = 1;
13792 : 114 : TREE_STATIC (decl) = 1;
13793 : 114 : TREE_PUBLIC (decl) = 0;
13794 : 114 : bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13795 : 114 : return decl;
13796 : : }
13797 : :
13798 : : /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13799 : :
13800 : : tree
13801 : 273 : c_omp_reduction_lookup (tree reduction_id, tree type)
13802 : : {
13803 : 273 : struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13804 : 276 : while (b)
13805 : : {
13806 : 274 : tree t;
13807 : 294 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13808 : 291 : if (comptypes (TREE_PURPOSE (t), type))
13809 : 271 : return TREE_VALUE (t);
13810 : 3 : b = b->shadowed;
13811 : : }
13812 : 2 : return error_mark_node;
13813 : : }
13814 : :
13815 : : /* Helper function called via walk_tree, to diagnose invalid
13816 : : #pragma omp declare reduction combiners or initializers. */
13817 : :
13818 : : tree
13819 : 1401 : c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13820 : : {
13821 : 1401 : tree *vars = (tree *) data;
13822 : 1401 : if (SSA_VAR_P (*tp)
13823 : 413 : && !DECL_ARTIFICIAL (*tp)
13824 : 24 : && *tp != vars[0]
13825 : 24 : && *tp != vars[1])
13826 : : {
13827 : 24 : location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13828 : 24 : if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13829 : 10 : error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13830 : : "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13831 : : *tp);
13832 : : else
13833 : 14 : error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13834 : : "to variable %qD which is not %<omp_priv%> nor "
13835 : : "%<omp_orig%>",
13836 : : *tp);
13837 : 24 : return *tp;
13838 : : }
13839 : : return NULL_TREE;
13840 : : }
13841 : :
13842 : : /* Return identifier to look up for omp declare mapper. */
13843 : :
13844 : : tree
13845 : 607 : c_omp_mapper_id (tree mapper_id)
13846 : : {
13847 : 607 : const char *p = NULL;
13848 : :
13849 : 607 : const char prefix[] = "omp declare mapper ";
13850 : :
13851 : 607 : if (mapper_id == NULL_TREE)
13852 : : p = "<default>";
13853 : 19 : else if (TREE_CODE (mapper_id) == IDENTIFIER_NODE)
13854 : 19 : p = IDENTIFIER_POINTER (mapper_id);
13855 : : else
13856 : 0 : return error_mark_node;
13857 : :
13858 : 607 : size_t lenp = sizeof (prefix);
13859 : 607 : size_t len = strlen (p);
13860 : 607 : char *name = XALLOCAVEC (char, lenp + len);
13861 : 607 : memcpy (name, prefix, lenp - 1);
13862 : 607 : memcpy (name + lenp - 1, p, len + 1);
13863 : 607 : return get_identifier (name);
13864 : : }
13865 : :
13866 : : /* Lookup MAPPER_ID in the current scope, or create an artificial
13867 : : VAR_DECL, bind it into the current scope and return it. */
13868 : :
13869 : : tree
13870 : 62 : c_omp_mapper_decl (tree mapper_id)
13871 : : {
13872 : 62 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13873 : 62 : if (b != NULL && B_IN_CURRENT_SCOPE (b))
13874 : 22 : return b->decl;
13875 : :
13876 : 40 : tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13877 : : mapper_id, integer_type_node);
13878 : 40 : DECL_ARTIFICIAL (decl) = 1;
13879 : 40 : DECL_EXTERNAL (decl) = 1;
13880 : 40 : TREE_STATIC (decl) = 1;
13881 : 40 : TREE_PUBLIC (decl) = 0;
13882 : 40 : bind (mapper_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13883 : 40 : return decl;
13884 : : }
13885 : :
13886 : : /* Lookup MAPPER_ID in the first scope where it has entry for TYPE. */
13887 : :
13888 : : tree
13889 : 1696 : c_omp_mapper_lookup (tree mapper_id, tree type)
13890 : : {
13891 : 1696 : if (!RECORD_OR_UNION_TYPE_P (type))
13892 : : return NULL_TREE;
13893 : :
13894 : 545 : mapper_id = c_omp_mapper_id (mapper_id);
13895 : :
13896 : 545 : struct c_binding *b = I_SYMBOL_BINDING (mapper_id);
13897 : 551 : while (b)
13898 : : {
13899 : 91 : tree t;
13900 : 110 : for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13901 : 104 : if (comptypes (TREE_PURPOSE (t), type))
13902 : 85 : return TREE_VALUE (t);
13903 : 6 : b = b->shadowed;
13904 : : }
13905 : : return NULL_TREE;
13906 : : }
13907 : :
13908 : : /* For C, we record a pointer to the mapper itself without wrapping it in an
13909 : : artificial function or similar. So, just return it. */
13910 : :
13911 : : tree
13912 : 82 : c_omp_extract_mapper_directive (tree mapper)
13913 : : {
13914 : 82 : return mapper;
13915 : : }
13916 : :
13917 : : /* For now we can handle singleton OMP_ARRAY_SECTIONs with custom mappers, but
13918 : : nothing more complicated. */
13919 : :
13920 : : tree
13921 : 364 : c_omp_map_array_section (location_t loc, tree t)
13922 : : {
13923 : 364 : tree low = TREE_OPERAND (t, 1);
13924 : 364 : tree len = TREE_OPERAND (t, 2);
13925 : :
13926 : 364 : if (len && integer_onep (len))
13927 : : {
13928 : 63 : t = TREE_OPERAND (t, 0);
13929 : :
13930 : 63 : if (!low)
13931 : 0 : low = integer_zero_node;
13932 : :
13933 : 63 : t = build_array_ref (loc, t, low);
13934 : : }
13935 : :
13936 : 364 : return t;
13937 : : }
13938 : :
13939 : : /* Helper function for below function. */
13940 : :
13941 : : static tree
13942 : 50786 : c_omp_scan_mapper_bindings_r (tree *tp, int *walk_subtrees, void *ptr)
13943 : : {
13944 : 50786 : tree t = *tp;
13945 : 50786 : omp_mapper_list<tree> *mlist = (omp_mapper_list<tree> *) ptr;
13946 : 50786 : tree aggr_type = NULL_TREE;
13947 : :
13948 : 50786 : if (TREE_CODE (t) == SIZEOF_EXPR
13949 : 50786 : || TREE_CODE (t) == ALIGNOF_EXPR)
13950 : : {
13951 : 0 : *walk_subtrees = 0;
13952 : 0 : return NULL_TREE;
13953 : : }
13954 : :
13955 : 50786 : if (TREE_CODE (t) == OMP_CLAUSE)
13956 : : return NULL_TREE;
13957 : :
13958 : 47502 : if (TREE_CODE (t) == COMPONENT_REF
13959 : 47502 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
13960 : 356 : aggr_type = TREE_TYPE (TREE_OPERAND (t, 0));
13961 : 47146 : else if ((TREE_CODE (t) == VAR_DECL
13962 : : || TREE_CODE (t) == PARM_DECL
13963 : : || TREE_CODE (t) == RESULT_DECL)
13964 : 5254 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t)))
13965 : 116 : aggr_type = TREE_TYPE (t);
13966 : :
13967 : 472 : if (aggr_type)
13968 : : {
13969 : 472 : tree mapper_fn = c_omp_mapper_lookup (NULL_TREE, aggr_type);
13970 : 472 : if (mapper_fn)
13971 : 65 : mlist->add_mapper (NULL_TREE, aggr_type, mapper_fn);
13972 : : }
13973 : :
13974 : : return NULL_TREE;
13975 : : }
13976 : :
13977 : : /* Scan an offload region's body, and record uses of struct- or union-typed
13978 : : variables. Add _mapper_binding_ fake clauses to *CLAUSES_PTR. */
13979 : :
13980 : : void
13981 : 1890 : c_omp_scan_mapper_bindings (location_t loc, tree *clauses_ptr, tree body)
13982 : : {
13983 : 1890 : hash_set<omp_name_type<tree>> seen_types;
13984 : 1890 : auto_vec<tree> mappers;
13985 : 1890 : omp_mapper_list<tree> mlist (&seen_types, &mappers);
13986 : :
13987 : 1890 : walk_tree_without_duplicates (&body, c_omp_scan_mapper_bindings_r, &mlist);
13988 : :
13989 : 1890 : unsigned int i;
13990 : 1890 : tree mapper;
13991 : 3819 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13992 : 39 : c_omp_find_nested_mappers (&mlist, mapper);
13993 : :
13994 : 1955 : FOR_EACH_VEC_ELT (mappers, i, mapper)
13995 : : {
13996 : 39 : if (mapper == error_mark_node)
13997 : 0 : continue;
13998 : 39 : tree mapper_name = OMP_DECLARE_MAPPER_ID (mapper);
13999 : 39 : tree decl = OMP_DECLARE_MAPPER_DECL (mapper);
14000 : :
14001 : 39 : tree c = build_omp_clause (loc, OMP_CLAUSE__MAPPER_BINDING_);
14002 : 39 : OMP_CLAUSE__MAPPER_BINDING__ID (c) = mapper_name;
14003 : 39 : OMP_CLAUSE__MAPPER_BINDING__DECL (c) = decl;
14004 : 39 : OMP_CLAUSE__MAPPER_BINDING__MAPPER (c) = mapper;
14005 : :
14006 : 39 : OMP_CLAUSE_CHAIN (c) = *clauses_ptr;
14007 : 39 : *clauses_ptr = c;
14008 : : }
14009 : 1890 : }
14010 : :
14011 : : bool
14012 : 223 : c_check_in_current_scope (tree decl)
14013 : : {
14014 : 223 : struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
14015 : 223 : return b != NULL && B_IN_CURRENT_SCOPE (b);
14016 : : }
14017 : :
14018 : : /* Search for loop or switch names. BEFORE_LABELS is last statement before
14019 : : possible labels and SWITCH_P true for a switch, false for loops.
14020 : : Searches through last statements in cur_stmt_list, stops when seeing
14021 : : BEFORE_LABELs, or statement other than LABEL_EXPR or CASE_LABEL_EXPR.
14022 : : Returns number of loop/switch names found and if any are found, sets
14023 : : *LAST_P to the canonical loop/switch name LABEL_DECL. */
14024 : :
14025 : : int
14026 : 499647 : c_get_loop_names (tree before_labels, bool switch_p, tree *last_p)
14027 : : {
14028 : 499647 : *last_p = NULL_TREE;
14029 : 999294 : if (!building_stmt_list_p ()
14030 : 499647 : || !STATEMENT_LIST_HAS_LABEL (cur_stmt_list)
14031 : 520189 : || before_labels == void_list_node)
14032 : 479105 : return 0;
14033 : :
14034 : 20542 : int ret = 0;
14035 : 20542 : tree last = NULL_TREE;
14036 : 20542 : for (tree_stmt_iterator tsi = tsi_last (cur_stmt_list);
14037 : 28653 : !tsi_end_p (tsi); tsi_prev (&tsi))
14038 : : {
14039 : 27807 : tree stmt = tsi_stmt (tsi);
14040 : 27807 : if (stmt == before_labels)
14041 : : break;
14042 : 8111 : else if (TREE_CODE (stmt) == LABEL_EXPR)
14043 : : {
14044 : 1083 : if (last == NULL_TREE)
14045 : 889 : last = LABEL_EXPR_LABEL (stmt);
14046 : : else
14047 : : {
14048 : 194 : loop_names.safe_push (LABEL_EXPR_LABEL (stmt));
14049 : 194 : ++ret;
14050 : : }
14051 : : }
14052 : 7028 : else if (TREE_CODE (stmt) != CASE_LABEL_EXPR
14053 : 4729 : && TREE_CODE (stmt) != DEBUG_BEGIN_STMT)
14054 : : break;
14055 : : }
14056 : 20542 : if (last)
14057 : : {
14058 : 889 : if (switch_p)
14059 : 124 : C_DECL_SWITCH_NAME (last) = 1;
14060 : : else
14061 : 765 : C_DECL_LOOP_NAME (last) = 1;
14062 : 889 : loop_names.safe_push (last);
14063 : 889 : ++ret;
14064 : 889 : if (loop_names.length () > 16)
14065 : : {
14066 : 20 : unsigned int first = 0, i;
14067 : 20 : tree l, c = NULL_TREE;
14068 : 20 : if (loop_names_hash == NULL)
14069 : 8 : loop_names_hash = new decl_tree_map (ret);
14070 : : else
14071 : 12 : first = loop_names.length () - ret;
14072 : 210 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14073 : : {
14074 : 170 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14075 : 48 : c = l;
14076 : 170 : gcc_checking_assert (c);
14077 : 170 : loop_names_hash->put (l, c);
14078 : 170 : if (i == first)
14079 : : break;
14080 : : }
14081 : : }
14082 : 889 : *last_p = last;
14083 : : }
14084 : : return ret;
14085 : : }
14086 : :
14087 : : /* Undoes what get_loop_names did when it returned NUM_NAMES. */
14088 : :
14089 : : void
14090 : 889 : c_release_loop_names (int num_names)
14091 : : {
14092 : 889 : unsigned len = loop_names.length () - num_names;
14093 : 889 : if (loop_names_hash)
14094 : : {
14095 : 20 : if (len <= 16)
14096 : : {
14097 : 8 : delete loop_names_hash;
14098 : 8 : loop_names_hash = NULL;
14099 : : }
14100 : : else
14101 : : {
14102 : 12 : unsigned int i;
14103 : 12 : tree l;
14104 : 42 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14105 : : {
14106 : 30 : loop_names_hash->remove (l);
14107 : 30 : if (i == len)
14108 : : break;
14109 : : }
14110 : : }
14111 : : }
14112 : 889 : loop_names.truncate (len);
14113 : 889 : }
14114 : :
14115 : : /* Finish processing of break or continue identifier operand.
14116 : : NAME is the identifier operand of break or continue and
14117 : : IS_BREAK is true iff it is break stmt. Returns the operand
14118 : : to use for BREAK_STMT or CONTINUE_STMT, either NULL_TREE or
14119 : : canonical loop/switch name LABEL_DECL. */
14120 : :
14121 : : tree
14122 : 316 : c_finish_bc_name (location_t loc, tree name, bool is_break)
14123 : : {
14124 : 316 : tree label = NULL_TREE, lab;
14125 : 457 : pedwarn_c23 (loc, OPT_Wpedantic,
14126 : : "ISO C does not support %qs statement with an identifier "
14127 : : "operand before C2Y", is_break ? "break" : "continue");
14128 : :
14129 : : /* If I_LABEL_DECL is NULL or not from current function, don't waste time
14130 : : trying to find it among loop_names, it can't be there. */
14131 : 316 : if (!loop_names.is_empty ()
14132 : 296 : && current_function_scope
14133 : 296 : && (lab = I_LABEL_DECL (name))
14134 : 290 : && DECL_CONTEXT (lab) == current_function_decl)
14135 : : {
14136 : 280 : unsigned int i;
14137 : 280 : tree l, c = NULL_TREE;
14138 : 280 : if (loop_names_hash)
14139 : : {
14140 : 86 : if (tree *val = loop_names_hash->get (lab))
14141 : 86 : label = *val;
14142 : : }
14143 : : else
14144 : 565 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14145 : : {
14146 : 363 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14147 : : c = l;
14148 : 363 : gcc_checking_assert (c);
14149 : 363 : if (l == lab)
14150 : : {
14151 : : label = c;
14152 : : break;
14153 : : }
14154 : : }
14155 : 280 : if (label)
14156 : 272 : TREE_USED (lab) = 1;
14157 : : }
14158 : 272 : if (label == NULL_TREE)
14159 : : {
14160 : 44 : auto_vec<const char *> candidates;
14161 : 44 : unsigned int i;
14162 : 44 : tree l, c = NULL_TREE;
14163 : 115 : FOR_EACH_VEC_ELT_REVERSE (loop_names, i, l)
14164 : : {
14165 : 28 : if (C_DECL_LOOP_NAME (l) || C_DECL_SWITCH_NAME (l))
14166 : : c = l;
14167 : 28 : gcc_checking_assert (c);
14168 : 42 : if (is_break || C_DECL_LOOP_NAME (c))
14169 : 25 : candidates.safe_push (IDENTIFIER_POINTER (DECL_NAME (l)));
14170 : : }
14171 : 44 : const char *hint = find_closest_string (IDENTIFIER_POINTER (name),
14172 : : &candidates);
14173 : 44 : if (hint)
14174 : : {
14175 : 22 : gcc_rich_location richloc (loc);
14176 : 22 : richloc.add_fixit_replace (hint);
14177 : 22 : if (is_break)
14178 : 11 : error_at (&richloc, "%<break%> statement operand %qE does not "
14179 : : "refer to a named loop or %<switch%>; "
14180 : : "did you mean %qs?", name, hint);
14181 : : else
14182 : 11 : error_at (&richloc, "%<continue%> statement operand %qE does not "
14183 : : "refer to a named loop; did you mean %qs?",
14184 : : name, hint);
14185 : 22 : }
14186 : 22 : else if (is_break)
14187 : 12 : error_at (loc, "%<break%> statement operand %qE does not refer to a "
14188 : : "named loop or %<switch%>", name);
14189 : : else
14190 : 10 : error_at (loc, "%<continue%> statement operand %qE does not refer to "
14191 : : "a named loop", name);
14192 : 44 : }
14193 : 272 : else if (!C_DECL_LOOP_NAME (label) && !is_break)
14194 : : {
14195 : 2 : auto_diagnostic_group d;
14196 : 2 : error_at (loc, "%<continue%> statement operand %qE refers to a named "
14197 : : "%<switch%>", name);
14198 : 2 : inform (DECL_SOURCE_LOCATION (label), "%<switch%> name defined here");
14199 : 2 : label = NULL_TREE;
14200 : 2 : }
14201 : 270 : else if (!C_DECL_LOOP_SWITCH_NAME_VALID (label))
14202 : : {
14203 : 18 : auto_diagnostic_group d;
14204 : 18 : if (C_DECL_LOOP_NAME (label))
14205 : : {
14206 : 16 : error_at (loc, "%qs statement operand %qE refers to a loop outside "
14207 : : "of its body", is_break ? "break" : "continue", name);
14208 : 16 : inform (DECL_SOURCE_LOCATION (label), "loop name defined here");
14209 : : }
14210 : : else
14211 : : {
14212 : 2 : error_at (loc, "%<break%> statement operand %qE refers to a "
14213 : : "%<switch%> outside of its body", name);
14214 : 2 : inform (DECL_SOURCE_LOCATION (label),
14215 : : "%<switch%> name defined here");
14216 : : }
14217 : 18 : label = NULL_TREE;
14218 : 18 : }
14219 : 252 : else if (label == loop_names.last () && (in_statement & IN_NAMED_STMT) != 0)
14220 : : /* If it is just a fancy reference to the innermost construct, handle it
14221 : : just like break; or continue; though tracking cheaply what is the
14222 : : innermost loop for continue when nested in switches would require
14223 : : another global variable and updating it. */
14224 : : label = NULL_TREE;
14225 : : else
14226 : 98 : C_DECL_LOOP_SWITCH_NAME_USED (label) = 1;
14227 : 316 : return label;
14228 : : }
14229 : :
14230 : : #include "gt-c-c-decl.h"
|